0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 ** This file is
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 an amalgamation
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65 of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 C source files
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76 from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 34 2e 32 2e ersion 3.6.14.2.
00b0: 20 20 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 By combining a
00c0: 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 ll the individua
00d0: 6c 20 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 l C code files i
00e0: 6e 74 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e nto this .** sin
00f0: 67 6c 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 gle large file,
0100: 74 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 the entire code
0110: 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 can be compiled
0120: 61 73 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 as a one transla
0130: 74 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 tion.** unit. T
0140: 68 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 his allows many
0150: 63 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 compilers to do
0160: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 optimizations th
0170: 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a at would not be.
0180: 2a 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 ** possible if t
0190: 68 65 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f he files were co
01a0: 6d 70 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c mpiled separatel
01b0: 79 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 y. Performance
01c0: 69 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 improvements.**
01d0: 6f 66 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 of 5% are more a
01e0: 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e re commonly seen
01f0: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 when SQLite is
0200: 63 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 compiled as a si
0210: 6e 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 ngle.** translat
0220: 69 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 ion unit..**.**
0230: 54 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c This file is all
0240: 20 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d you need to com
0250: 70 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f pile SQLite. To
0260: 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f use SQLite in o
0270: 74 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 ther.** programs
0280: 2c 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 , you need this
0290: 66 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 file and the "sq
02a0: 6c 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 lite3.h" header
02b0: 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 file that define
02c0: 73 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d s.** the program
02d0: 6d 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 ming interface t
02e0: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 o the SQLite lib
02f0: 72 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 rary. (If you d
0300: 6f 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 o not have .** t
0310: 68 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 he "sqlite3.h" h
0320: 65 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 eader file at ha
0330: 6e 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e nd, you will fin
0340: 64 20 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 d a copy in the
0350: 66 69 72 73 74 0a 2a 2a 20 35 35 33 33 20 6c 69 first.** 5533 li
0360: 6e 65 73 20 70 61 73 74 20 74 68 69 73 20 68 65 nes past this he
0370: 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 ader comment.)
0380: 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 Additional code
0390: 66 69 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 files may be.**
03a0: 6e 65 65 64 65 64 20 69 66 20 79 6f 75 20 77 61 needed if you wa
03b0: 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 nt a wrapper to
03c0: 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 65 interface SQLite
03d0: 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63 with your choic
03e0: 65 20 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d e of.** programm
03f0: 69 6e 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 ing language. T
0400: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 he code for the
0410: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e "sqlite3" comman
0420: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 d-line shell.**
0430: 69 73 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 is also in a sep
0440: 61 72 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69 arate file. Thi
0450: 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
0460: 6f 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 only code for th
0470: 65 20 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 e core.** SQLite
0480: 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 library..**.**
0490: 54 68 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f This amalgamatio
04a0: 6e 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 n was generated
04b0: 6f 6e 20 32 30 30 39 2d 30 35 2d 32 35 20 31 32 on 2009-05-25 12
04c0: 3a 33 34 3a 33 31 20 55 54 43 2e 0a 2a 2f 0a 23 :34:31 UTC..*/.#
04d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
04e0: 52 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c RE 1.#define SQL
04f0: 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
0500: 20 31 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 1.#ifndef SQLIT
0510: 45 5f 50 52 49 56 41 54 45 0a 23 20 64 65 66 69 E_PRIVATE.# defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ne SQLITE_PRIVAT
0530: 45 20 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a E static.#endif.
0540: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 #ifndef SQLITE_A
0550: 50 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 PI.# define SQLI
0560: 54 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a TE_API.#endif./*
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
0580: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49 gin file sqliteI
0590: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
05c0: 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
05d0: 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
05e0: 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
05f0: 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
0600: 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
0610: 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
0620: 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
0630: 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
0640: 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
0650: 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
0660: 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
0670: 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
0680: 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
0690: 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
06a0: 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
06b0: 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
06c0: 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
06d0: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
06e0: 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
06f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0730: 2a 2a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c ****.** Internal
0740: 20 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e interface defin
0750: 69 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 itions for SQLit
0760: 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 e..**.** @(#) $I
0770: 64 3a 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 d: sqliteInt.h,v
0780: 20 31 2e 38 36 38 20 32 30 30 39 2f 30 35 2f 30 1.868 2009/05/0
0790: 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 4 11:42:30 danie
07a0: 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
07b0: 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 #ifndef _SQLITEI
07c0: 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 NT_H_.#define _S
07d0: 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a QLITEINT_H_../*.
07e0: 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 ** Include the c
07f0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 onfiguration hea
0800: 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 der output by 'c
0810: 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27 onfigure' if we'
0820: 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 re using the.**
0830: 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 autoconf-based b
0840: 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f uild.*/.#ifdef _
0850: 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 HAVE_SQLITE_CONF
0860: 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 IG_H.#include "c
0870: 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a onfig.h".#endif.
0880: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
0890: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c Include sqliteL
08a0: 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 imit.h in the mi
08b0: 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
08c0: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f t.h ***********/
08d0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
08e0: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 Begin file sqli
08f0: 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a teLimit.h ******
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
0920: 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 ./*.** 2007 May
0930: 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
0940: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0950: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0960: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0970: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0980: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0990: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
09a0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
09b0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
09c0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
09d0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
09e0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
09f0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
0a00: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
0a10: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0a20: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0a30: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0a40: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a90: 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c .** .** This fil
0aa0: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 e defines variou
0ab0: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 s limits of what
0ac0: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 SQLite can proc
0ad0: 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 ess..**.** @(#)
0ae0: 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 $Id: sqliteLimit
0af0: 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 .h,v 1.10 2009/0
0b00: 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61 1/10 16:15:09 da
0b10: 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
0b20: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 */../*.** The ma
0b30: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
0b40: 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69 a TEXT or BLOB i
0b50: 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 73 20 n bytes. This
0b60: 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 also.** limits t
0b70: 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 he size of a row
0b80: 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 in a table or i
0b90: 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ndex..**.** The
0ba0: 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68 hard limit is th
0bb0: 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33 e ability of a 3
0bc0: 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 2-bit signed int
0bd0: 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 eger.** to count
0be0: 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d the size: 2^31-
0bf0: 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e 1 or 2147483647.
0c00: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
0c10: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 TE_MAX_LENGTH.#
0c20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
0c30: 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 X_LENGTH 1000000
0c40: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
0c50: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6d 61 * This is the ma
0c60: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a ximum number of.
0c70: 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d **.** * Colum
0c80: 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a ns in a table.**
0c90: 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e * Columns in
0ca0: 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 an index.**
0cb0: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76 * Columns in a v
0cc0: 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d iew.** * Term
0cd0: 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61 s in the SET cla
0ce0: 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 use of an UPDATE
0cf0: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 statement.**
0d00: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 * Terms in the
0d10: 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 result set of a
0d20: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
0d30: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 .** * Terms i
0d40: 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f n the GROUP BY o
0d50: 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 r ORDER BY claus
0d60: 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 es of a SELECT s
0d70: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 tatement..**
0d80: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56 * Terms in the V
0d90: 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20 ALUES clause of
0da0: 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d an INSERT statem
0db0: 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 ent.**.** The ha
0dc0: 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 rd upper limit h
0dd0: 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d ere is 32676. M
0de0: 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 65 6f ost database peo
0df0: 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c ple will.** tell
0e00: 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 you that in a w
0e10: 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 ell-normalized d
0e20: 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 atabase, you usu
0e30: 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e ally should.** n
0e40: 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61 ot have more tha
0e50: 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 n a dozen or so
0e60: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74 columns in any t
0e70: 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a able. And if.**
0e80: 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73 that is the cas
0e90: 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 e, there is no p
0ea0: 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d oint in having m
0eb0: 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a ore than a few.*
0ec0: 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69 * dozen values i
0ed0: 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 n any of the oth
0ee0: 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65 er situations de
0ef0: 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a scribed above..*
0f00: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
0f10: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 _MAX_COLUMN.# de
0f20: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
0f30: 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64 COLUMN 2000.#end
0f40: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
0f50: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
0f60: 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 a single SQL sta
0f70: 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e tement in bytes.
0f80: 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74 .**.** It used t
0f90: 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 o be the case th
0fa0: 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 73 20 at setting this
0fb0: 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f value to zero wo
0fc0: 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20 uld.** turn the
0fd0: 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74 limit off. That
0fe0: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 is no longer tr
0ff0: 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70 ue. It is not p
1000: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 ossible.** to tu
1010: 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66 rn this limit of
1020: 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 f..*/.#ifndef SQ
1030: 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e LITE_MAX_SQL_LEN
1040: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c GTH.# define SQL
1050: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 ITE_MAX_SQL_LENG
1060: 54 48 20 31 30 30 30 30 30 30 30 30 30 0a 23 65 TH 1000000000.#e
1070: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1080: 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 maximum depth of
1090: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
10a0: 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d ree. This is lim
10b0: 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 ited to .** some
10c0: 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54 extent by SQLIT
10d0: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 E_MAX_SQL_LENGTH
10e0: 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79 . But sometime y
10f0: 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e ou might .** wan
1100: 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20 t to place more
1110: 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e severe limits on
1120: 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20 the complexity
1130: 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73 of an .** expres
1140: 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 sion..**.** A va
1150: 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f lue of 0 used to
1160: 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c mean that the l
1170: 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66 imit was not enf
1180: 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68 orced..** But th
1190: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 at is no longer
11a0: 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74 true. The limit
11b0: 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79 is now strictly
11c0: 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20 enforced.** at
11d0: 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 all times..*/.#i
11e0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
11f0: 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65 _EXPR_DEPTH.# de
1200: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1210: 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a EXPR_DEPTH 1000.
1220: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1230: 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1240: 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 of terms in a c
1250: 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 ompound SELECT s
1260: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 tatement..** The
1270: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 code generator
1280: 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c for compound SEL
1290: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64 ECT statements d
12a0: 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c oes one.** level
12b0: 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f of recursion fo
12c0: 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20 r each term. A
12d0: 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63 stack overflow c
12e0: 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20 an result.** if
12f0: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 the number of te
1300: 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 rms is too large
1310: 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 . In practice,
1320: 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 most SQL.** neve
1330: 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 r has more than
1340: 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55 3 or 4 terms. U
1350: 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 se a value of 0
1360: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e to disable.** an
1370: 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e y limit on the n
1380: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 umber of terms i
1390: 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c n a compount SEL
13a0: 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ECT..*/.#ifndef
13b0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f SQLITE_MAX_COMPO
13c0: 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66 UND_SELECT.# def
13d0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 ine SQLITE_MAX_C
13e0: 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35 OMPOUND_SELECT 5
13f0: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1400: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1410: 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69 ber of opcodes i
1420: 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d n a VDBE program
1430: 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 ..** Not current
1440: 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a ly enforced..*/.
1450: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1460: 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66 AX_VDBE_OP.# def
1470: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 ine SQLITE_MAX_V
1480: 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e DBE_OP 25000.#en
1490: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d dif../*.** The m
14a0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
14b0: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e arguments to an
14c0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a SQL function..*
14d0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
14e0: 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 _MAX_FUNCTION_AR
14f0: 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 G.# define SQLIT
1500: 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 E_MAX_FUNCTION_A
1510: 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f RG 127.#endif../
1520: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
1530: 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 number of in-me
1540: 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 75 73 mory pages to us
1550: 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 e for the main d
1560: 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 atabase.** table
1570: 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 and for tempora
1580: 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 ry tables. The
1590: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 SQLITE_DEFAULT_C
15a0: 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 ACHE_SIZE.*/.#if
15b0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
15c0: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 ULT_CACHE_SIZE.#
15d0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
15e0: 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a EFAULT_CACHE_SIZ
15f0: 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23 E 2000.#endif.#
1600: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
1610: 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 FAULT_TEMP_CACHE
1620: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
1630: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 QLITE_DEFAULT_TE
1640: 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35 MP_CACHE_SIZE 5
1650: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1660: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1670: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 ber of attached
1680: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 databases. This
1690: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
16a0: 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54 0.** and 30. T
16b0: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f he upper bound o
16c0: 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 65 20 n 30 is because
16d0: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
16e0: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73 bitmap.** is us
16f0: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f ed internally to
1700: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20 track attached
1710: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 databases..*/.#i
1720: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1730: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 _ATTACHED.# defi
1740: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 ne SQLITE_MAX_AT
1750: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 TACHED 10.#endif
1760: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 .../*.** The max
1770: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 imum value of a
1780: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 ?nnn wildcard th
1790: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69 at the parser wi
17a0: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 ll accept..*/.#i
17b0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
17c0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
17d0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
17e0: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 _MAX_VARIABLE_NU
17f0: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a MBER 999.#endif.
1800: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 ./* Maximum page
1810: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65 size. The uppe
1820: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 r bound on this
1830: 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20 value is 32768.
1840: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a This a limit.**
1850: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20 imposed by the
1860: 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f necessity of sto
1870: 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 ring the value i
1880: 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67 n a 2-byte unsig
1890: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 ned integer.** a
18a0: 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 nd the fact that
18b0: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d the page size m
18c0: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
18d0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 f 2..**.** If th
18e0: 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e is limit is chan
18f0: 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f ged, then the co
1900: 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69 mpiled library i
1910: 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a s technically.**
1920: 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 incompatible wi
1930: 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 th an SQLite lib
1940: 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69 rary compiled wi
1950: 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c th a different l
1960: 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72 imit. If.** a pr
1970: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 ocess operating
1980: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 77 69 on a database wi
1990: 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f th a page-size o
19a0: 66 20 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a f 65536 bytes .*
19b0: 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20 * crashes, then
19c0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53 an instance of S
19d0: 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77 QLite compiled w
19e0: 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 ith the default
19f0: 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69 page-size .** li
1a00: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 mit will not be
1a10: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b able to rollback
1a20: 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 72 61 the aborted tra
1a30: 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 nsaction. This c
1a40: 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20 ould.** lead to
1a50: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
1a60: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
1a70: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1a80: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
1a90: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1aa0: 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a ZE 32768.#endif.
1ab0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ../*.** The defa
1ac0: 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61 ult size of a da
1ad0: 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a tabase page..*/.
1ae0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
1af0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1b00: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1b10: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
1b20: 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23 ZE 1024.#endif.#
1b30: 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c if SQLITE_DEFAUL
1b40: 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 T_PAGE_SIZE>SQLI
1b50: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1b60: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
1b70: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1b80: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
1b90: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
1ba0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 IZE SQLITE_MAX_P
1bb0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a AGE_SIZE.#endif.
1bc0: 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c ./*.** Ordinaril
1bd0: 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69 y, if no value i
1be0: 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f s explicitly pro
1bf0: 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72 vided, SQLite cr
1c00: 65 61 74 65 73 20 64 61 74 61 62 61 73 65 73 0a eates databases.
1c10: 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 69 7a ** with page siz
1c20: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
1c30: 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65 _PAGE_SIZE. Howe
1c40: 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65 ver, based on ce
1c50: 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20 rtain.** device
1c60: 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 characteristics
1c70: 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64 (sector-size and
1c80: 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20 atomic write()
1c90: 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c support),.** SQL
1ca0: 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61 ite may choose a
1cb0: 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54 larger value. T
1cc0: 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 his constant is
1cd0: 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 the maximum valu
1ce0: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c e.** SQLite will
1cf0: 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f choose on its o
1d00: 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 wn..*/.#ifndef S
1d10: 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
1d20: 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 T_PAGE_SIZE.# de
1d30: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1d40: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1d50: 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69 E 8192.#endif.#i
1d60: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 f SQLITE_MAX_DEF
1d70: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 AULT_PAGE_SIZE>S
1d80: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
1d90: 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 IZE.# undef SQLI
1da0: 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
1db0: 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e AGE_SIZE.# defin
1dc0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 e SQLITE_MAX_DEF
1dd0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53 AULT_PAGE_SIZE S
1de0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
1df0: 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a IZE.#endif.../*.
1e00: 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 ** Maximum numbe
1e10: 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e r of pages in on
1e20: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1e30: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72 .**.** This is r
1e40: 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64 eally just the d
1e50: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
1e60: 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f the max_page_co
1e70: 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 unt pragma..** T
1e80: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65 his value can be
1e90: 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69 lowered (or rai
1ea0: 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65 sed) at run-time
1eb0: 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a using that the.
1ec0: 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e ** max_page_coun
1ed0: 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e t macro..*/.#ifn
1ee0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 def SQLITE_MAX_P
1ef0: 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69 AGE_COUNT.# defi
1f00: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ne SQLITE_MAX_PA
1f10: 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31 GE_COUNT 1073741
1f20: 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 823.#endif../*.*
1f30: 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
1f40: 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 (in bytes) of t
1f50: 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 he pattern in a
1f60: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 LIKE or GLOB.**
1f70: 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 operator..*/.#if
1f80: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1f90: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e LIKE_PATTERN_LEN
1fa0: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c GTH.# define SQL
1fb0: 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 ITE_MAX_LIKE_PAT
1fc0: 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 TERN_LENGTH 5000
1fd0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 0.#endif../*****
1fe0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1ff0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a sqliteLimit.h *
2000: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2020: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
2030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
2040: 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
2050: 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
2060: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
2070: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 ********/../* Di
2080: 73 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 sable nuisance w
2090: 61 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 arnings on Borla
20a0: 6e 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a nd compilers */.
20b0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f #if defined(__BO
20c0: 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d RLANDC__).#pragm
20d0: 61 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 a warn -rch /* u
20e0: 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 nreachable code
20f0: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 */.#pragma warn
2100: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f -ccc /* Conditio
2110: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 n is always true
2120: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 or false */.#pr
2130: 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f agma warn -aus /
2140: 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 * Assigned value
2150: 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a is never used *
2160: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d /.#pragma warn -
2170: 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 csu /* Comparing
2180: 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 signed and unsi
2190: 67 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 gned */.#pragma
21a0: 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 warn -spa /* Sus
21b0: 70 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 picious pointer
21c0: 61 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 arithmetic */.#e
21d0: 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 ndif../* Needed
21e0: 66 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 for various defi
21f0: 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 nitions... */.#i
2200: 66 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 fndef _GNU_SOURC
2210: 45 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f E.# define _GNU_
2220: 53 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f SOURCE.#endif../
2230: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 *.** Include sta
2240: 6e 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c ndard header fil
2250: 65 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a es as necessary.
2260: 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 */.#ifdef HAVE_S
2270: 54 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 TDINT_H.#include
2280: 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 <stdint.h>.#end
2290: 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 if.#ifdef HAVE_I
22a0: 4e 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 NTTYPES_H.#inclu
22b0: 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a de <inttypes.h>.
22c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 #endif../*. * Th
22d0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 is macro is used
22e0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 to "hide" some
22f0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 ugliness in cast
2300: 69 6e 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61 ing an int. * va
2310: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c lue to a ptr val
2320: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 ue under the MSV
2330: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 C 64-bit compile
2340: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20 r. Casting. *
2350: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 non 64-bit value
2360: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 s to ptr types r
2370: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 esults in a "har
2380: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20 d" error with .
2390: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 * the MSVC 64-bi
23a0: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 t compiler which
23b0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 this attempts t
23c0: 6f 20 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a o avoid. . *. *
23d0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c A simple compil
23e0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 er pragma or cas
23f0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f ting sequence co
2400: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 uld not be found
2410: 0a 20 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 . * to correct t
2420: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 his in all situa
2430: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d tions, so this m
2440: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 acro was introdu
2450: 63 65 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f ced.. *. * It co
2460: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68 uld be argued th
2470: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 at the intptr_t
2480: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 type could be us
2490: 65 64 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61 ed in this. * ca
24a0: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 se, but that typ
24b0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 e is not availab
24c0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c le on all compil
24d0: 65 72 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75 ers, or . * requ
24e0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 ires the #includ
24f0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 e of specific he
2500: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66 aders which diff
2510: 65 72 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70 ers between. * p
2520: 6c 61 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64 latforms.. */.#d
2530: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
2540: 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 _TO_PTR(X) ((v
2550: 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 oid*)&((char*)0)
2560: 5b 58 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c [X]).#define SQL
2570: 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 ITE_PTR_TO_INT(X
2580: 29 20 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 ) ((int)(((cha
2590: 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 r*)X)-(char*)0))
25a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
25b0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
25c0: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
25d0: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 upport on POSIX
25e0: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
25f0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
2600: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
2610: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
2620: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
2630: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 e support, or if
2640: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f the OS is windo
2650: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 ws, these should
2660: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
2670: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 * Ticket #2739:
2680: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f The _LARGEFILE_
2690: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 SOURCE macro mus
26a0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 t appear before
26b0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 any.** system #i
26c0: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c ncludes. Hence,
26d0: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 this block of c
26e0: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ode must be the
26f0: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f very first.** co
2700: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 de in all source
2710: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 files..**.** La
2720: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
2730: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
2740: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
2750: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
2760: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
2770: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
2780: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
2790: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
27a0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
27b0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
27c0: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 achine (ex: Red
27d0: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 Hat 7.2) but you
27e0: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 want your code
27f0: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e to work.** on an
2800: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 older machine (
2810: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 ex: Red Hat 6.0)
2820: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
2830: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 e on Red Hat 7.2
2840: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 .** without this
2850: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 option, LFS is
2860: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 enable. But LFS
2870: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
2880: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a in the kernel.**
2890: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c in Red Hat 6.0,
28a0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e so the code won
28b0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 't work. Hence,
28c0: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e for maximum bin
28d0: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 ary.** portabili
28e0: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d ty you should om
28f0: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 it LFS..**.** Si
2900: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f milar is true fo
2910: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 r Mac OS X. LFS
2920: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
2930: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 ed on Mac OS X 9
2940: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 and later..*/.#
2950: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
2960: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 SABLE_LFS.# defi
2970: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 ne _LARGE_FILE
2980: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 1.# ifndef
2990: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 _FILE_OFFSET_BIT
29a0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 S.# define _FI
29b0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 LE_OFFSET_BITS 6
29c0: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 4.# endif.# defi
29d0: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f ne _LARGEFILE_SO
29e0: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a URCE 1.#endif...
29f0: 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 /*.** The SQLITE
2a00: 5f 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 _THREADSAFE macr
2a10: 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 o must be define
2a20: 64 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 d as either 0 or
2a30: 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 1..** Older ver
2a40: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
2a50: 75 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c used an optional
2a60: 20 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 THREADSAFE macr
2a70: 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 o..** We support
2a80: 20 74 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 that for legacy
2a90: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
2aa0: 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 (SQLITE_THREADSA
2ab0: 46 45 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 FE).#if defined(
2ac0: 54 48 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 THREADSAFE).# de
2ad0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 fine SQLITE_THRE
2ae0: 41 44 53 41 46 45 20 54 48 52 45 41 44 53 41 46 ADSAFE THREADSAF
2af0: 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 E.#else.# define
2b00: 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
2b10: 46 45 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 FE 1.#endif.#end
2b20: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 if../*.** The SQ
2b30: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d LITE_DEFAULT_MEM
2b40: 53 54 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 STATUS macro mus
2b50: 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 t be defined as
2b60: 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a either 0 or 1..*
2b70: 2a 20 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 * It determines
2b80: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
2b90: 68 65 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 he features rela
2ba0: 74 65 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 ted to .** SQLIT
2bb0: 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 E_CONFIG_MEMSTAT
2bc0: 55 53 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 US are available
2bd0: 20 62 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e by default or n
2be0: 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 ot. This value c
2bf0: 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 an.** be overrid
2c00: 64 65 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 den at runtime u
2c10: 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 sing the sqlite3
2c20: 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a _config() API..*
2c30: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
2c40: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 QLITE_DEFAULT_ME
2c50: 4d 53 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e MSTATUS).# defin
2c60: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
2c70: 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e _MEMSTATUS 1.#en
2c80: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 dif../*.** Exact
2c90: 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f ly one of the fo
2ca0: 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d llowing macros m
2cb0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 ust be defined i
2cc0: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 n order to.** sp
2cd0: 65 63 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f ecify which memo
2ce0: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
2cf0: 62 73 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a bsystem to use..
2d00: 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 **.** SQLITE
2d10: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 _SYSTEM_MALLOC
2d20: 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e // Use n
2d30: 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c ormal system mal
2d40: 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c loc().** SQL
2d50: 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 ITE_MEMDEBUG
2d60: 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 // De
2d70: 62 75 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 bugging version
2d80: 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 of system malloc
2d90: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 ().** SQLITE
2da0: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 _MEMORY_SIZE
2db0: 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 // inter
2dc0: 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 nal allocator #1
2dd0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d .** SQLITE_M
2de0: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 MAP_HEAP_SIZE
2df0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 // interna
2e00: 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 l mmap() allocat
2e10: 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 or.** SQLITE
2e20: 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a _POW2_MEMORY_SIZ
2e30: 45 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 E // inter
2e40: 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f nal power-of-two
2e50: 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a allocator.**.**
2e60: 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
2e70: 61 62 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 above are define
2e80: 64 2c 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 d, then set SQLI
2e90: 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 TE_SYSTEM_MALLOC
2ea0: 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 as.** the defau
2eb0: 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e lt..*/.#if defin
2ec0: 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ed(SQLITE_SYSTEM
2ed0: 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 _MALLOC)+defined
2ee0: 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 (SQLITE_MEMDEBUG
2ef0: 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 )+\. defined(
2f00: 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 SQLITE_MEMORY_SI
2f10: 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 ZE)+defined(SQLI
2f20: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a TE_MMAP_HEAP_SIZ
2f30: 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 E)+\. defined
2f40: 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d (SQLITE_POW2_MEM
2f50: 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 ORY_SIZE)>1.# er
2f60: 72 6f 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 ror "At most one
2f70: 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
2f80: 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 g compile-time c
2f90: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
2fa0: 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 ions\. is allows
2fb0: 3a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f : SQLITE_SYSTEM_
2fc0: 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d MALLOC, SQLITE_M
2fd0: 45 4d 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f EMDEBUG, SQLITE_
2fe0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 MEMORY_SIZE,\. S
2ff0: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f QLITE_MMAP_HEAP_
3000: 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 SIZE, SQLITE_POW
3010: 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 2_MEMORY_SIZE".#
3020: 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 endif.#if define
3030: 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f d(SQLITE_SYSTEM_
3040: 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 MALLOC)+defined(
3050: 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 SQLITE_MEMDEBUG)
3060: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 +\. defined(S
3070: 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a QLITE_MEMORY_SIZ
3080: 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 E)+defined(SQLIT
3090: 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 E_MMAP_HEAP_SIZE
30a0: 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 )+\. defined(
30b0: 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f SQLITE_POW2_MEMO
30c0: 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 RY_SIZE)==0.# de
30d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 fine SQLITE_SYST
30e0: 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 EM_MALLOC 1.#end
30f0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c if../*.** If SQL
3100: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
3110: 4c 49 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 LIMIT is not zer
3120: 6f 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b o, then try to k
3130: 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 eep the.** sizes
3140: 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 of memory alloc
3150: 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 ations below thi
3160: 73 20 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f s value where po
3170: 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 ssible..*/.#if !
3180: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
3190: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
31a0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
31b0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
31c0: 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a MIT 1024.#endif.
31d0: 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 ./*.** We need t
31e0: 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f o define _XOPEN_
31f0: 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 SOURCE as follow
3200: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e s in order to en
3210: 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 able.** recursiv
3220: 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 e mutexes on mos
3230: 74 20 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 t Unix systems.
3240: 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 But Mac OS X is
3250: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 different..** T
3260: 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 he _XOPEN_SOURCE
3270: 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 define causes p
3280: 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 roblems for Mac
3290: 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 OS X we are told
32a0: 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d ,.** so it is om
32b0: 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 itted there. Se
32c0: 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a e ticket #2673..
32d0: 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c **.** Later we l
32e0: 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e earn that _XOPEN
32f0: 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c _SOURCE is poorl
3300: 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 y or incorrectly
3310: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 .** implemented
3320: 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e on some systems.
3330: 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 So we avoid de
3340: 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c fining it at all
3350: 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 .** if it is alr
3360: 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 eady defined or
3370: 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 if it is unneede
3380: 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72 65 d because we are
3390: 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 .** not doing a
33a0: 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 threadsafe build
33b0: 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e . Ticket #2681.
33c0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
33d0: 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f ticket #2741..*/
33e0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 .#if !defined(_X
33f0: 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 OPEN_SOURCE) &&
3400: 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 !defined(__DARWI
3410: 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 N__) && !defined
3420: 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
3430: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
3440: 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 .# define _XOPE
3450: 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a N_SOURCE 500 /*
3460: 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c Needed to enabl
3470: 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72 73 e pthread recurs
3480: 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 ive mutexes */.#
3490: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
34a0: 20 54 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 TCL headers are
34b0: 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 only needed whe
34c0: 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 n compiling the
34d0: 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f TCL bindings..*/
34e0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
34f0: 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 ITE_TCL) || defi
3500: 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 ned(TCLSH).# inc
3510: 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e lude <tcl.h>.#en
3520: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 dif../*.** Many
3530: 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 people are faili
3540: 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 ng to set -DNDEB
3550: 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c UG=1 when compil
3560: 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 ing SQLite..** S
3570: 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 etting NDEBUG ma
3580: 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 kes the code sma
3590: 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 ller and run fas
35a0: 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c ter. So the fol
35b0: 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 lowing.** lines
35c0: 61 72 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 are added to aut
35d0: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e omatically set N
35e0: 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 DEBUG unless the
35f0: 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d -DSQLITE_DEBUG=
3600: 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 1.** option is s
3610: 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 et. Thus NDEBUG
3620: 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d becomes an opt-
3630: 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 in rather than a
3640: 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 n opt-out.** fea
3650: 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 ture..*/.#if !de
3660: 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 fined(NDEBUG) &&
3670: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
3680: 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e _DEBUG) .# defin
3690: 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 e NDEBUG 1.#endi
36a0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 f../*.** The tes
36b0: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 tcase() macro is
36c0: 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 used to aid in
36d0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
36e0: 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e . When .** doin
36f0: 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 g coverage testi
3700: 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f ng, the conditio
3710: 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 n inside the arg
3720: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 ument to.** test
3730: 63 61 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 case() must be e
3740: 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 valuated both tr
3750: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 ue and false in
3760: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 order to.** get
3770: 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 full branch cove
3780: 72 61 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 rage. The testc
3790: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 ase() macro is i
37a0: 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 nserted.** to he
37b0: 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 lp ensure adequa
37c0: 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 te test coverage
37d0: 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 in places where
37e0: 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 simple.** condi
37f0: 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f tion/decision co
3800: 76 65 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 verage is inadeq
3810: 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 uate. For examp
3820: 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a le, testcase().*
3830: 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f * can be used to
3840: 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 make sure bound
3850: 61 72 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 ary values are t
3860: 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 ested. For.** b
3870: 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 itmask tests, te
3880: 73 74 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 stcase() can be
3890: 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 used to make sur
38a0: 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 e each bit.** is
38b0: 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 significant and
38c0: 20 75 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f used at least o
38d0: 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 nce. On switch
38e0: 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 statements.** wh
38f0: 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 ere multiple cas
3900: 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d es go to the sam
3910: 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c e block of code,
3920: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 testcase().** c
3930: 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 an insure that a
3940: 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65 76 61 ll cases are eva
3950: 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 luated..**.*/.#i
3960: 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 fdef SQLITE_COVE
3970: 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 RAGE_TEST.SQLITE
3980: 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
3990: 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 sqlite3Coverage(
39a0: 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 int);.# define t
39b0: 65 73 74 63 61 73 65 28 58 29 20 20 69 66 28 20 estcase(X) if(
39c0: 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 X ){ sqlite3Cove
39d0: 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 rage(__LINE__);
39e0: 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
39f0: 20 74 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e testcase(X).#en
3a00: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 dif../*.** The T
3a10: 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 ESTONLY macro is
3a20: 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 used to enclose
3a30: 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 variable declar
3a40: 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 ations or.** oth
3a50: 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 er bits of code
3a60: 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 that are needed
3a70: 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 to support the a
3a80: 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 rguments.** with
3a90: 69 6e 20 74 65 73 74 63 61 73 65 28 29 20 61 6e in testcase() an
3aa0: 64 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f d assert() macro
3ab0: 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e s..*/.#if !defin
3ac0: 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 ed(NDEBUG) || de
3ad0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 fined(SQLITE_COV
3ae0: 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 ERAGE_TEST).# de
3af0: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 fine TESTONLY(X)
3b00: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 X.#else.# defi
3b10: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 ne TESTONLY(X).#
3b20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d endif../*.** Som
3b30: 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 etimes we need a
3b40: 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 small amount of
3b50: 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 code such as a
3b60: 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c variable initial
3b70: 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 ization.** to se
3b80: 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 tup for a later
3b90: 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
3ba0: 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 nt. We do not w
3bb0: 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f ant this code to
3bc0: 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 .** appear when
3bd0: 61 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61 assert() is disa
3be0: 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f bled. The follo
3bf0: 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 wing macro is th
3c00: 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 erefore.** used
3c10: 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 to contain that
3c20: 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 setup code. The
3c30: 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 "VVA" acronym s
3c40: 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 tands for.** "Ve
3c50: 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 rification, Vali
3c60: 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 dation, and Accr
3c70: 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 editation". In
3c80: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
3c90: 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 .** code within
3ca0: 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 VVA_ONLY() will
3cb0: 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 only run during
3cc0: 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f verification pro
3cd0: 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 cesses..*/.#ifnd
3ce0: 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 ef NDEBUG.# defi
3cf0: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 ne VVA_ONLY(X)
3d00: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 X.#else.# define
3d10: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e VVA_ONLY(X).#en
3d20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 dif../*.** The A
3d30: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 LWAYS and NEVER
3d40: 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 macros surround
3d50: 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 boolean expressi
3d60: 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 ons which .** ar
3d70: 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c e intended to al
3d80: 77 61 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 ways be true or
3d90: 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 false, respectiv
3da0: 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 ely. Such.** ex
3db0: 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 pressions could
3dc0: 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 be omitted from
3dd0: 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 the code complet
3de0: 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a ely. But they.*
3df0: 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 * are included i
3e00: 6e 20 61 20 66 65 77 20 63 61 73 65 73 20 69 6e n a few cases in
3e10: 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 order to enhanc
3e20: 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 e the resilience
3e30: 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f .** of SQLite to
3e40: 20 75 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 unexpected beha
3e50: 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 vior - to make t
3e60: 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 he code "self-he
3e70: 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 aling".** or "du
3e80: 63 74 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 ctile" rather th
3e90: 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c an being "brittl
3ea0: 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 e" and crashing
3eb0: 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 at the first.**
3ec0: 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 hint of unplanne
3ed0: 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a d behavior..**.*
3ee0: 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
3ef0: 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 , ALWAYS and NEV
3f00: 45 52 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 ER are added for
3f10: 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e defensive code.
3f20: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e .**.** When doin
3f30: 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 g coverage testi
3f40: 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 ng ALWAYS and NE
3f50: 56 45 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 VER are hard-cod
3f60: 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 ed to.** be true
3f70: 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 and false so th
3f80: 61 74 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 at the unreachab
3f90: 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 le code then spe
3fa0: 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 cify will.** not
3fb0: 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 be counted as u
3fc0: 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f ntested code..*/
3fd0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
3fe0: 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 ITE_COVERAGE_TES
3ff0: 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 T).# define ALWA
4000: 59 53 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 YS(X) (1).#
4010: 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 define NEVER(X)
4020: 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 (0).#elif
4030: 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
4040: 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
4050: 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73 int sqlite3As
4060: 73 65 72 74 28 76 6f 69 64 29 3b 0a 23 20 64 65 sert(void);.# de
4070: 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20 20 fine ALWAYS(X)
4080: 20 20 20 20 28 28 58 29 3f 31 3a 73 71 6c 69 74 ((X)?1:sqlit
4090: 65 33 41 73 73 65 72 74 28 29 29 0a 23 20 64 65 e3Assert()).# de
40a0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 fine NEVER(X)
40b0: 20 20 20 20 28 28 58 29 3f 73 71 6c 69 74 65 33 ((X)?sqlite3
40c0: 41 73 73 65 72 74 28 29 3a 30 29 0a 23 65 6c 73 Assert():0).#els
40d0: 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 e.# define ALWAY
40e0: 53 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 S(X) (X).#
40f0: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 define NEVER(X)
4100: 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 (X).#endif
4110: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 ../*.** The macr
4120: 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 o unlikely() is
4130: 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 a hint that surr
4140: 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a ounds a boolean.
4150: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ** expression th
4160: 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 at is usually fa
4170: 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 lse. Macro like
4180: 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a ly() surrounds.*
4190: 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 * a boolean expr
41a0: 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 ession that is u
41b0: 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 sually true. GC
41c0: 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 C is able to.**
41d0: 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 use these hints
41e0: 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 to generate bett
41f0: 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d er code, sometim
4200: 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e es..*/.#if defin
4210: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
4220: 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 0.# define likel
4230: 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 y(X) __builti
4240: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a n_expect((X),1).
4250: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c # define unlikel
4260: 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f y(X) __builtin_
4270: 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 expect((X),0).#e
4280: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b lse.# define lik
4290: 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a ely(X) !!(X).
42a0: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c # define unlikel
42b0: 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 y(X) !!(X).#end
42c0: 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
42d0: 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 *** Include sqli
42e0: 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 te3.h in the mid
42f0: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
4300: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4310: 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
4320: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 *** Begin file s
4330: 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a qlite3.h *******
4340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4360: 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
4370: 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
4380: 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
4390: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
43a0: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
43b0: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
43c0: 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
43d0: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
43e0: 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
43f0: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
4400: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
4410: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
4420: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
4430: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
4440: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
4450: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
4460: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
4470: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
4480: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
4490: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
44a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
44e0: 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 is header file d
44f0: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
4500: 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 face that the SQ
4510: 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 Lite library.**
4520: 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 presents to clie
4530: 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 nt programs. If
4540: 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 a C-function, s
4550: 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 tructure, dataty
4560: 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 pe,.** or consta
4570: 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f nt definition do
4580: 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e es not appear in
4590: 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e this file, then
45a0: 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 it is.** not a
45b0: 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 published API of
45c0: 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a SQLite, is subj
45d0: 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 ect to change wi
45e0: 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c thout.** notice,
45f0: 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 and should not
4600: 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 be referenced by
4610: 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 programs that u
4620: 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a se SQLite..**.**
4630: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 Some of the def
4640: 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 initions that ar
4650: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 e in this file a
4660: 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 re marked as.**
4670: 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 "experimental".
4680: 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e Experimental in
4690: 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 terfaces are nor
46a0: 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 mally new.** fea
46b0: 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 tures recently a
46c0: 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 dded to SQLite.
46d0: 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 We do not antic
46e0: 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a ipate changes.**
46f0: 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c to experimental
4700: 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 interfaces but
4710: 72 65 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20 reserve to make
4720: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66 minor changes if
4730: 0a 2a 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66 .** experience f
4740: 72 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 rom use "in the
4750: 77 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 wild" suggest su
4760: 63 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 ch changes are p
4770: 72 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 rudent..**.** Th
4780: 65 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e e official C-lan
4790: 67 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 guage API docume
47a0: 6e 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 ntation for SQLi
47b0: 74 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a te is derived.**
47c0: 20 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 from comments i
47d0: 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 n this file. Th
47e0: 69 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 is file is the a
47f0: 75 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 uthoritative sou
4800: 72 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 rce.** on how SQ
4810: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
4820: 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f are suppose to o
4830: 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 perate..**.** Th
4840: 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 e name of this f
4850: 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 ile under config
4860: 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 uration manageme
4870: 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e nt is "sqlite.h.
4880: 69 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 in"..** The make
4890: 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 file makes some
48a0: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
48b0: 20 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 this file (such
48c0: 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a as inserting.**
48d0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d the version num
48e0: 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 ber) and changes
48f0: 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 its name to "sq
4900: 6c 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 lite3.h" as.** p
4910: 61 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 art of the build
4920: 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 process..**.**
4930: 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 @(#) $Id: sqlite
4940: 2e 68 2e 69 6e 2c 76 20 31 2e 34 34 37 20 32 30 .h.in,v 1.447 20
4950: 30 39 2f 30 34 2f 33 30 20 31 35 3a 35 39 3a 35 09/04/30 15:59:5
4960: 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 6 drh Exp $.*/.#
4970: 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f ifndef _SQLITE3_
4980: 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 H_.#define _SQLI
4990: 54 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 TE3_H_.#include
49a0: 3c 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f <stdarg.h> /
49b0: 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 * Needed for the
49c0: 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 definition of v
49d0: 61 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a a_list */../*.**
49e0: 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 Make sure we ca
49f0: 6e 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 n call this stuf
4a00: 66 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 f from C++..*/.#
4a10: 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 if 0.extern "C"
4a20: 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a {.#endif.../*.**
4a30: 20 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 Add the ability
4a40: 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 to override 'ex
4a50: 74 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 tern'.*/.#ifndef
4a60: 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 SQLITE_EXTERN.#
4a70: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 define SQLITE_E
4a80: 58 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e XTERN extern.#en
4a90: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 dif../*.** These
4aa0: 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 no-op macros ar
4ab0: 65 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 e used in front
4ac0: 6f 66 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f of interfaces to
4ad0: 20 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 mark those.** i
4ae0: 6e 74 65 72 66 61 63 65 73 20 61 73 20 65 69 74 nterfaces as eit
4af0: 68 65 72 20 64 65 70 72 65 63 61 74 65 64 20 6f her deprecated o
4b00: 72 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 r experimental.
4b10: 20 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e New application
4b20: 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 s.** should not
4b30: 75 73 65 20 64 65 70 72 65 63 61 74 65 64 20 69 use deprecated i
4b40: 6e 74 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 ntrfaces - they
4b50: 61 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 are support for
4b60: 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d backwards.** com
4b70: 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e patibility only.
4b80: 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 Application wr
4b90: 69 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 iters should be
4ba0: 61 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 aware that.** ex
4bb0: 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 perimental inter
4bc0: 66 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 faces are subjec
4bd0: 74 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 t to change in p
4be0: 6f 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a oint releases..*
4bf0: 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f *.** These macro
4c00: 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 s used to resolv
4c10: 65 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e e to various kin
4c20: 64 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d ds of compiler m
4c30: 61 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 agic that.** wou
4c40: 6c 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e ld generate warn
4c50: 69 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 ing messages whe
4c60: 6e 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64 n they were used
4c70: 2e 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 . But that.** c
4c80: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e ompiler magic en
4c90: 64 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e ded up generatin
4ca0: 67 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 g such a flurry
4cb0: 6f 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a of bug reports.*
4cc0: 2a 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74 * that we have t
4cd0: 61 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 aken it all out
4ce0: 61 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f and gone back to
4cf0: 20 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a using simple.**
4d00: 20 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f noop macros..*/
4d10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4d20: 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 DEPRECATED.#defi
4d30: 6e 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 ne SQLITE_EXPERI
4d40: 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e MENTAL../*.** En
4d50: 73 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f sure these symbo
4d60: 6c 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 ls were not defi
4d70: 6e 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 ned by some prev
4d80: 69 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 ious header file
4d90: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
4da0: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 TE_VERSION.# und
4db0: 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f ef SQLITE_VERSIO
4dc0: 4e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 N.#endif.#ifdef
4dd0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
4de0: 55 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 UMBER.# undef SQ
4df0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d LITE_VERSION_NUM
4e00: 42 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a BER.#endif../*.*
4e10: 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 * CAPI3REF: Comp
4e20: 69 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 ile-Time Library
4e30: 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 Version Numbers
4e40: 20 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 {H10010} <S6010
4e50: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 0>.**.** The SQL
4e60: 49 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 ITE_VERSION and
4e70: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
4e80: 55 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 UMBER #defines i
4e90: 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 n.** the sqlite3
4ea0: 2e 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 .h file specify
4eb0: 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 the version of S
4ec0: 51 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68 QLite with which
4ed0: 0a 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 .** that header
4ee0: 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 file is associat
4ef0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 ed..**.** The "v
4f00: 65 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 ersion" of SQLit
4f10: 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 e is a string of
4f20: 20 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a the form "X.Y.Z
4f30: 22 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65 "..** The phrase
4f40: 20 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74 "alpha" or "bet
4f50: 61 22 20 6d 69 67 68 74 20 62 65 20 61 70 70 65 a" might be appe
4f60: 6e 64 65 64 20 61 66 74 65 72 20 74 68 65 20 5a nded after the Z
4f70: 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 ..** The X value
4f80: 20 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f is major versio
4f90: 6e 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20 n number always
4fa0: 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 3 in SQLite3..**
4fb0: 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c The X value onl
4fc0: 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 y changes when b
4fd0: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
4fe0: 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f bility is.** bro
4ff0: 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e ken and we inten
5000: 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b d to never break
5010: 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
5020: 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 tibility..** The
5030: 20 59 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 Y value is the
5040: 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 minor version nu
5050: 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 mber and only ch
5060: 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 anges when.** th
5070: 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 ere are major fe
5080: 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e ature enhancemen
5090: 74 73 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 ts that are forw
50a0: 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a ards compatible.
50b0: 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 ** but not backw
50c0: 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e ards compatible.
50d0: 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 .** The Z value
50e0: 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e is the release n
50f0: 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 umber and is inc
5100: 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a remented with.**
5110: 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 each release bu
5120: 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f t resets back to
5130: 20 30 20 77 68 65 6e 65 76 65 72 20 59 20 69 73 0 whenever Y is
5140: 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a incremented..**
5150: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
5160: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
5170: 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 n()] and [sqlite
5180: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 3_libversion_num
5190: 62 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 ber()]..**.** Re
51a0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
51b0: 30 31 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 011] [H10014].*/
51c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
51d0: 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 20 VERSION
51e0: 22 33 2e 36 2e 31 34 2e 32 22 0a 23 64 65 66 69 "3.6.14.2".#defi
51f0: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f ne SQLITE_VERSIO
5200: 4e 5f 4e 55 4d 42 45 52 20 20 33 30 30 36 30 31 N_NUMBER 300601
5210: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 4../*.** CAPI3RE
5220: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72 F: Run-Time Libr
5230: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 ary Version Numb
5240: 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36 ers {H10020} <S6
5250: 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0100>.** KEYWORD
5260: 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 S: sqlite3_versi
5270: 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 on.**.** These f
5280: 65 61 74 75 72 65 73 20 70 72 6f 76 69 64 65 20 eatures provide
5290: 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 the same informa
52a0: 74 69 6f 6e 20 61 73 20 74 68 65 20 5b 53 51 4c tion as the [SQL
52b0: 49 54 45 5f 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20 ITE_VERSION].**
52c0: 61 6e 64 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 and [SQLITE_VERS
52d0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 20 23 64 65 66 ION_NUMBER] #def
52e0: 69 6e 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 ines in the head
52f0: 65 72 2c 20 62 75 74 20 61 72 65 20 61 73 73 6f er, but are asso
5300: 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 ciated.** with t
5310: 68 65 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 he library inste
5320: 61 64 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 ad of the header
5330: 20 66 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 file. Cautious
5340: 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 programmers mig
5350: 68 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 61 20 ht.** include a
5360: 63 68 65 63 6b 20 69 6e 20 74 68 65 69 72 20 61 check in their a
5370: 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 76 65 pplication to ve
5380: 72 69 66 79 20 74 68 61 74 0a 2a 2a 20 73 71 6c rify that.** sql
5390: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
53a0: 6e 75 6d 62 65 72 28 29 20 61 6c 77 61 79 73 20 number() always
53b0: 72 65 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 returns the valu
53c0: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 e.** [SQLITE_VER
53d0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a SION_NUMBER]..**
53e0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
53f0: 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e libversion() fun
5400: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
5410: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 e same informati
5420: 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 on as is.** in t
5430: 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 he sqlite3_versi
5440: 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 on[] string cons
5450: 74 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 tant. The funct
5460: 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a ion is provided.
5470: 2a 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c ** for use in DL
5480: 4c 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 Ls since DLL use
5490: 72 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f rs usually do no
54a0: 74 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 t have direct ac
54b0: 63 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a cess to string.*
54c0: 2a 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 * constants with
54d0: 69 6e 20 74 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a in the DLL..**.*
54e0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
54f0: 5b 48 31 30 30 32 31 5d 20 5b 48 31 30 30 32 32 [H10021] [H10022
5500: 5d 20 5b 48 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 ] [H10023].*/.SQ
5510: 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
5520: 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73 har sqlite3_vers
5530: 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56 ion[] = SQLITE_V
5540: 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 ERSION;.SQLITE_A
5550: 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
5560: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
5570: 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f n(void);.SQLITE_
5580: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
5590: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 libversion_numbe
55a0: 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 r(void);../*.**
55b0: 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 54 CAPI3REF: Test T
55c0: 6f 20 53 65 65 20 49 66 20 54 68 65 20 4c 69 62 o See If The Lib
55d0: 72 61 72 79 20 49 73 20 54 68 72 65 61 64 73 61 rary Is Threadsa
55e0: 66 65 20 7b 48 31 30 31 30 30 7d 20 3c 53 36 30 fe {H10100} <S60
55f0: 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 100>.**.** SQLit
5600: 65 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 e can be compile
5610: 64 20 77 69 74 68 20 6f 72 20 77 69 74 68 6f 75 d with or withou
5620: 74 20 6d 75 74 65 78 65 73 2e 20 20 57 68 65 6e t mutexes. When
5630: 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f .** the [SQLITE_
5640: 54 48 52 45 41 44 53 41 46 45 5d 20 43 20 70 72 THREADSAFE] C pr
5650: 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f eprocessor macro
5660: 20 31 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73 1 or 2, mutexes
5670: 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 .** are enabled
5680: 61 6e 64 20 53 51 4c 69 74 65 20 69 73 20 74 68 and SQLite is th
5690: 72 65 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20 readsafe. When
56a0: 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 the.** [SQLITE_T
56b0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f HREADSAFE] macro
56c0: 20 69 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d is 0, .** the m
56d0: 75 74 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74 utexes are omitt
56e0: 65 64 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65 ed. Without the
56f0: 20 6d 75 74 65 78 65 73 2c 20 69 74 20 69 73 20 mutexes, it is
5700: 6e 6f 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75 not safe.** to u
5710: 73 65 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72 se SQLite concur
5720: 72 65 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 rently from more
5730: 20 74 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64 than one thread
5740: 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 ..**.** Enabling
5750: 20 6d 75 74 65 78 65 73 20 69 6e 63 75 72 73 20 mutexes incurs
5760: 61 20 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72 a measurable per
5770: 66 6f 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79 formance penalty
5780: 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64 ..** So if speed
5790: 20 69 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d is of utmost im
57a0: 70 6f 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b portance, it mak
57b0: 65 73 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61 es sense to disa
57c0: 62 6c 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 ble.** the mutex
57d0: 65 73 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78 es. But for max
57e0: 69 6d 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74 imum safety, mut
57f0: 65 78 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65 exes should be e
5800: 6e 61 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64 nabled..** The d
5810: 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 efault behavior
5820: 69 73 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74 is for mutexes t
5830: 6f 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a o be enabled..**
5840: 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
5850: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 62 ce can be used b
5860: 79 20 61 20 70 72 6f 67 72 61 6d 20 74 6f 20 6d y a program to m
5870: 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
5880: 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 e.** version of
5890: 53 51 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 SQLite that it i
58a0: 73 20 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 s linking agains
58b0: 74 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 t was compiled w
58c0: 69 74 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 ith.** the desir
58d0: 65 64 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 ed setting of th
58e0: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 e [SQLITE_THREAD
58f0: 53 41 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a SAFE] macro..**.
5900: 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
5910: 65 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f e only reports o
5920: 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 n the compile-ti
5930: 6d 65 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 me mutex setting
5940: 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 .** of the [SQLI
5950: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 TE_THREADSAFE] f
5960: 6c 61 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 lag. If SQLite
5970: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
5980: 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 .** SQLITE_THREA
5990: 44 53 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 DSAFE=1 then mut
59a0: 65 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 exes are enabled
59b0: 20 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a by default but.
59c0: 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 ** can be fully
59d0: 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 or partially dis
59e0: 61 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 abled using a ca
59f0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
5a00: 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 onfig()].** with
5a10: 20 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 the verbs [SQLI
5a20: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
5a30: 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 THREAD], [SQLITE
5a40: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 _CONFIG_MULTITHR
5a50: 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c EAD],.** or [SQL
5a60: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
5a70: 5d 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 ]. The return v
5a80: 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e alue of this fun
5a90: 63 74 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f ction shows.** o
5aa0: 6e 6c 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 nly the default
5ab0: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 compile-time set
5ac0: 74 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 ting, not any ru
5ad0: 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a n-time changes.*
5ae0: 2a 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e * to that settin
5af0: 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 g..**.** See the
5b00: 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
5b10: 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 ] documentation
5b20: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
5b30: 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a nformation..**.*
5b40: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
5b50: 5b 48 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 [H10101] [H10102
5b60: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
5b70: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 int sqlite3_thre
5b80: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f adsafe(void);../
5b90: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
5ba0: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
5bb0: 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 on Handle {H1200
5bc0: 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 0} <S40200>.** K
5bd0: 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 EYWORDS: {databa
5be0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b se connection} {
5bf0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
5c00: 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 ions}.**.** Each
5c10: 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 open SQLite dat
5c20: 61 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65 abase is represe
5c30: 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 nted by a pointe
5c40: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
5c50: 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 of.** the opaqu
5c60: 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 e structure name
5c70: 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 d "sqlite3". It
5c80: 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 is useful to th
5c90: 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 ink of an sqlite
5ca0: 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 3.** pointer as
5cb0: 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 an object. The
5cc0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
5cd0: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
5ce0: 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6()], and.** [sq
5cf0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
5d00: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
5d10: 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 its constructors
5d20: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 , and [sqlite3_c
5d30: 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 lose()].** is it
5d40: 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 s destructor. T
5d50: 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 here are many ot
5d60: 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 her interfaces (
5d70: 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 such as.** [sqli
5d80: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
5d90: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 ], [sqlite3_crea
5da0: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 te_function()],
5db0: 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
5dc0: 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 busy_timeout()]
5dd0: 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 to name but thre
5de0: 65 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 e) that are meth
5df0: 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c ods on an.** sql
5e00: 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ite3 object..*/.
5e10: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
5e20: 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a qlite3 sqlite3;.
5e30: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
5e40: 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 64-Bit Integer
5e50: 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c Types {H10200} <
5e60: 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10110>.** KEYWO
5e70: 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 RDS: sqlite_int6
5e80: 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 4 sqlite_uint64.
5e90: 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 **.** Because th
5ea0: 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d ere is no cross-
5eb0: 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 platform way to
5ec0: 73 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 specify 64-bit i
5ed0: 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 nteger types.**
5ee0: 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 SQLite includes
5ef0: 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d typedefs for 64-
5f00: 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 bit signed and u
5f10: 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 nsigned integers
5f20: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5f30: 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 te3_int64 and sq
5f40: 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 lite3_uint64 are
5f50: 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 the preferred t
5f60: 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e ype definitions.
5f70: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 .** The sqlite_i
5f80: 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f nt64 and sqlite_
5f90: 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 uint64 types are
5fa0: 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 supported for b
5fb0: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 ackwards.** comp
5fc0: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a atibility only..
5fd0: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
5fe0: 74 73 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 ts: [H10201] [H1
5ff0: 30 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 0202].*/.#ifdef
6000: 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 SQLITE_INT64_TYP
6010: 45 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 E. typedef SQLI
6020: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 TE_INT64_TYPE sq
6030: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 lite_int64;. ty
6040: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 pedef unsigned S
6050: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 QLITE_INT64_TYPE
6060: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a sqlite_uint64;.
6070: 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
6080: 53 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e SC_VER) || defin
6090: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 ed(__BORLANDC__)
60a0: 0a 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 . typedef __int
60b0: 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 64 sqlite_int64;
60c0: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 . typedef unsig
60d0: 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 ned __int64 sqli
60e0: 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 te_uint64;.#else
60f0: 0a 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 . typedef long
6100: 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f long int sqlite_
6110: 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 int64;. typedef
6120: 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
6130: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 ong int sqlite_u
6140: 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 int64;.#endif.ty
6150: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 pedef sqlite_int
6160: 36 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 64 sqlite3_int64
6170: 3b 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 ;.typedef sqlite
6180: 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f _uint64 sqlite3_
6190: 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 uint64;../*.** I
61a0: 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 f compiling for
61b0: 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 a processor that
61c0: 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 lacks floating
61d0: 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a point support,.*
61e0: 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 * substitute int
61f0: 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e eger for floatin
6200: 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 g-point..*/.#ifd
6210: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
6220: 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 LOATING_POINT.#
6230: 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 define double sq
6240: 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 lite3_int64.#end
6250: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 if../*.** CAPI3R
6260: 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 EF: Closing A Da
6270: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
6280: 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 n {H12010} <S301
6290: 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 00><S40200>.**.*
62a0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
62b0: 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 s the destructor
62c0: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 for the [sqlite
62d0: 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 3] object..**.**
62e0: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 Applications sh
62f0: 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 ould [sqlite3_fi
6300: 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a nalize | finaliz
6310: 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 e] all [prepared
6320: 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 statements].**
6330: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f and [sqlite3_blo
6340: 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d b_close | close]
6350: 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c all [BLOB handl
6360: 65 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 es] associated w
6370: 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 ith.** the [sqli
6380: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f te3] object prio
6390: 72 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 r to attempting
63a0: 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a to close the obj
63b0: 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c ect..** The [sql
63c0: 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 ite3_next_stmt()
63d0: 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 ] interface can
63e0: 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 be used to locat
63f0: 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 e all.** [prepar
6400: 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 ed statements] a
6410: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
6420: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
6430: 63 74 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 ction] if desire
6440: 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f d..** Typical co
6450: 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 de might look li
6460: 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c ke this:.**.** <
6470: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
6480: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 .** sqlite3_stmt
6490: 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c *pStmt;.** whil
64a0: 65 28 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 e( (pStmt = sqli
64b0: 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 te3_next_stmt(db
64c0: 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 , 0))!=0 ){.** &
64d0: 6e 62 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f nbsp; sqlite3_
64e0: 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b finalize(pStmt);
64f0: 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c .** }.** </pre><
6500: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
6510: 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 ** If [sqlite3_c
6520: 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b lose()] is invok
6530: 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 ed while a trans
6540: 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a action is open,.
6550: 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
6560: 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 on is automatica
6570: 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e lly rolled back.
6580: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72 .**.** The C par
6590: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
65a0: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73 e3_close(C)] mus
65b0: 74 20 62 65 20 65 69 74 68 65 72 20 61 20 4e 55 t be either a NU
65c0: 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 72 LL.** pointer or
65d0: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 an [sqlite3] ob
65e0: 6a 65 63 74 20 70 6f 69 6e 74 65 72 20 6f 62 74 ject pointer obt
65f0: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 ained.** from [s
6600: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
6610: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
6620: 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 )], or.** [sqlit
6630: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 e3_open_v2()], a
6640: 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c nd not previousl
6650: 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 y closed..**.**
6660: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
6670: 20 5b 48 31 32 30 31 31 5d 20 5b 48 31 32 30 31 [H12011] [H1201
6680: 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b 48 31 32 2] [H12013] [H12
6690: 30 31 34 5d 20 5b 48 31 32 30 31 35 5d 20 5b 48 014] [H12015] [H
66a0: 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12019].*/.SQLITE
66b0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
66c0: 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a _close(sqlite3 *
66d0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 );../*.** The ty
66e0: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 pe for a callbac
66f0: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 k function..** T
6700: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e his is legacy an
6710: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 d deprecated. I
6720: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f t is included fo
6730: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 r historical.**
6740: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e compatibility an
6750: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e d is not documen
6760: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ted..*/.typedef
6770: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 int (*sqlite3_ca
6780: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e llback)(void*,in
6790: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a t,char**, char**
67a0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
67b0: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 EF: One-Step Que
67c0: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 ry Execution Int
67d0: 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 erface {H12100}
67e0: 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10000>.**.** T
67f0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
6800: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 ) interface is a
6810: 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 convenient way
6820: 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f of running one o
6830: 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 r more.** SQL st
6840: 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 atements without
6850: 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 having to write
6860: 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 a lot of C code
6870: 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 . The UTF-8 enc
6880: 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 oded.** SQL stat
6890: 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 ements are passe
68a0: 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f d in as the seco
68b0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
68c0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a sqlite3_exec()..
68d0: 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 ** The statement
68e0: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 s are evaluated
68f0: 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c one by one until
6900: 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 either an error
6910: 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 or.** an interr
6920: 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 upt is encounter
6930: 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 ed, or until the
6940: 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 y are all done.
6950: 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 The 3rd paramet
6960: 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 er.** is an opti
6970: 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 onal callback th
6980: 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e at is invoked on
6990: 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 ce for each row
69a0: 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 of any query.**
69b0: 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 results produced
69c0: 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 by the SQL stat
69d0: 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 ements. The 5th
69e0: 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 parameter tells
69f0: 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 where.** to wri
6a00: 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 te any error mes
6a10: 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 sages..**.** The
6a20: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 error message p
6a30: 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 assed back throu
6a40: 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d gh the 5th param
6a50: 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 eter is held.**
6a60: 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
6a70: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
6a80: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 _malloc()]. To
6a90: 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c avoid a memory l
6aa0: 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c eak,.** the call
6ab0: 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 ing application
6ac0: 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c should call [sql
6ad0: 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 ite3_free()] on
6ae0: 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 any error.** mes
6af0: 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 sage returned th
6b00: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 rough the 5th pa
6b10: 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 rameter when it
6b20: 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 has finished usi
6b30: 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 ng.** the error
6b40: 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 message..**.** I
6b50: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
6b60: 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 ent in the 2nd p
6b70: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c arameter is NULL
6b80: 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 or an empty str
6b90: 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 ing.** or a stri
6ba0: 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e ng containing on
6bb0: 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 61 6e ly whitespace an
6bc0: 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e d comments, then
6bd0: 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 no SQL.** state
6be0: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 ments are evalua
6bf0: 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 ted and the data
6c00: 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e base is not chan
6c10: 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ged..**.** The s
6c20: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e qlite3_exec() in
6c30: 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 terface is imple
6c40: 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 mented in terms
6c50: 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 of.** [sqlite3_p
6c60: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 repare_v2()], [s
6c70: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 qlite3_step()],
6c80: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e and [sqlite3_fin
6c90: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 alize()]..** The
6ca0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
6cb0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 routine does not
6cc0: 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 hing to the data
6cd0: 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 base that cannot
6ce0: 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b be done.** by [
6cf0: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
6d00: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f v2()], [sqlite3_
6d10: 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 step()], and [sq
6d20: 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
6d30: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 ]..**.** The fir
6d40: 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
6d50: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
6d60: 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61 6c 69 must be an vali
6d70: 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64 d and open.** [d
6d80: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
6d90: 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 on]..**.** The d
6da0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
6db0: 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 63 on must not be c
6dc0: 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a 2a 20 5b losed while.** [
6dd0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
6de0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a is running..**.*
6df0: 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 * The calling fu
6e00: 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 nction should us
6e10: 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 e [sqlite3_free(
6e20: 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a 20 74 68 )] to free.** th
6e30: 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 e memory that *e
6e40: 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f rrmsg is left po
6e50: 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 inting at once t
6e60: 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 he error.** mess
6e70: 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 age is no longer
6e80: 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 needed..**.** T
6e90: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
6ea0: 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64 text in the 2nd
6eb0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
6ec0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a qlite3_exec()].*
6ed0: 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 75 6e * must remain un
6ee0: 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 5b 73 changed while [s
6ef0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 qlite3_exec()] i
6f00: 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a s running..**.**
6f10: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
6f20: 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48 31 32 31 * [H12101] [H121
6f30: 30 32 5d 20 5b 48 31 32 31 30 34 5d 20 5b 48 31 02] [H12104] [H1
6f40: 32 31 30 35 5d 20 5b 48 31 32 31 30 37 5d 20 5b 2105] [H12107] [
6f50: 48 31 32 31 31 30 5d 20 5b 48 31 32 31 31 33 5d H12110] [H12113]
6f60: 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20 5b 48 31 [H12116].** [H1
6f70: 32 31 31 39 5d 20 5b 48 31 32 31 32 32 5d 20 5b 2119] [H12122] [
6f80: 48 31 32 31 32 35 5d 20 5b 48 31 32 31 33 31 5d H12125] [H12131]
6f90: 20 5b 48 31 32 31 33 34 5d 20 5b 48 31 32 31 33 [H12134] [H1213
6fa0: 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a 2f 0a 53 7] [H12138].*/.S
6fb0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
6fc0: 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 lite3_exec(. sq
6fd0: 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20 lite3*,
6fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f /* An o
7000: 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a pen database */.
7010: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 const char *sq
7020: 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
7030: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7040: 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 61 SQL to be evalua
7050: 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 ted */. int (*c
7060: 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 allback)(void*,i
7070: 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a 2a nt,char**,char**
7080: 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 ), /* Callback
7090: 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
70a0: 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20 id *,
70b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 /* 1st
70d0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c argument to call
70e0: 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a back */. char *
70f0: 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 *errmsg
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7110: 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 /* Error ms
7120: 67 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a g written here *
7130: 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
7140: 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64 3REF: Result Cod
7150: 65 73 20 7b 48 31 30 32 31 30 7d 20 3c 53 31 30 es {H10210} <S10
7160: 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 700>.** KEYWORDS
7170: 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 : SQLITE_OK {err
7180: 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 or code} {error
7190: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 codes}.** KEYWOR
71a0: 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 DS: {result code
71b0: 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d } {result codes}
71c0: 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 .**.** Many SQLi
71d0: 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 te functions ret
71e0: 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 urn an integer r
71f0: 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 esult code from
7200: 74 68 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a the set shown.**
7210: 20 68 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74 here in order t
7220: 6f 20 69 6e 64 69 63 61 74 65 73 20 73 75 63 63 o indicates succ
7230: 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a ess or failure..
7240: 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20 **.** New error
7250: 63 6f 64 65 73 20 6d 61 79 20 62 65 20 61 64 64 codes may be add
7260: 65 64 20 69 6e 20 66 75 74 75 72 65 20 76 65 72 ed in future ver
7270: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e sions of SQLite.
7280: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
7290: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 [SQLITE_IOERR_R
72a0: 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 EAD | extended r
72b0: 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a esult codes].*/.
72c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f #define SQLITE_O
72d0: 4b 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 K 0
72e0: 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20 72 65 /* Successful re
72f0: 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e sult */./* begin
7300: 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f ning-of-error-co
7310: 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 des */.#define S
7320: 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20 QLITE_ERROR
7330: 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 72 1 /* SQL er
7340: 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 ror or missing d
7350: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 atabase */.#defi
7360: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e ne SQLITE_INTERN
7370: 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 49 6e AL 2 /* In
7380: 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72 ternal logic err
7390: 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0a or in SQLite */.
73a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 #define SQLITE_P
73b0: 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 20 20 ERM 3
73c0: 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d 69 73 /* Access permis
73d0: 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 sion denied */.#
73e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 42 define SQLITE_AB
73f0: 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 20 2f ORT 4 /
7400: 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 * Callback routi
7410: 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 6e 20 ne requested an
7420: 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 abort */.#define
7430: 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 20 SQLITE_BUSY
7440: 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68 65 20 5 /* The
7450: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
7460: 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 locked */.#defi
7470: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ne SQLITE_LOCKED
7480: 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20 41 20 6 /* A
7490: 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 table in the dat
74a0: 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 20 abase is locked
74b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
74c0: 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 37 E_NOMEM 7
74d0: 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 /* A malloc()
74e0: 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66 69 failed */.#defi
74f0: 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e ne SQLITE_READON
7500: 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20 41 74 LY 8 /* At
7510: 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 tempt to write a
7520: 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 readonly databa
7530: 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 se */.#define SQ
7540: 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 LITE_INTERRUPT
7550: 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 9 /* Operati
7560: 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 on terminated by
7570: 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
7580: 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20 53 pt()*/.#define S
7590: 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20 QLITE_IOERR
75a0: 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 10 /* Some k
75b0: 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20 ind of disk I/O
75c0: 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a error occurred *
75d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
75e0: 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31 31 20 _CORRUPT 11
75f0: 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
7600: 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 e disk image is
7610: 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 malformed */.#de
7620: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 46 fine SQLITE_NOTF
7630: 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f 2a 20 OUND 12 /*
7640: 4e 4f 54 20 55 53 45 44 2e 20 54 61 62 6c 65 20 NOT USED. Table
7650: 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74 20 66 6f or record not fo
7660: 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 und */.#define S
7670: 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 QLITE_FULL
7680: 20 20 31 33 20 20 20 2f 2a 20 49 6e 73 65 72 74 13 /* Insert
7690: 69 6f 6e 20 66 61 69 6c 65 64 20 62 65 63 61 75 ion failed becau
76a0: 73 65 20 64 61 74 61 62 61 73 65 20 69 73 20 66 se database is f
76b0: 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ull */.#define S
76c0: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20 QLITE_CANTOPEN
76d0: 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65 14 /* Unable
76e0: 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 to open the dat
76f0: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 abase file */.#d
7700: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 4f efine SQLITE_PRO
7710: 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 20 2f 2a TOCOL 15 /*
7720: 20 4e 4f 54 20 55 53 45 44 2e 20 44 61 74 61 62 NOT USED. Datab
7730: 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f ase lock protoco
7740: 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 l error */.#defi
7750: 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 ne SQLITE_EMPTY
7760: 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 44 61 16 /* Da
7770: 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 79 20 tabase is empty
7780: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7790: 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20 31 37 E_SCHEMA 17
77a0: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
77b0: 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 se schema change
77c0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
77d0: 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20 20 20 ITE_TOOBIG
77e0: 31 38 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 18 /* String o
77f0: 72 20 42 4c 4f 42 20 65 78 63 65 65 64 73 20 73 r BLOB exceeds s
7800: 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 ize limit */.#de
7810: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 53 fine SQLITE_CONS
7820: 54 52 41 49 4e 54 20 20 31 39 20 20 20 2f 2a 20 TRAINT 19 /*
7830: 41 62 6f 72 74 20 64 75 65 20 74 6f 20 63 6f 6e Abort due to con
7840: 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f straint violatio
7850: 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
7860: 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 ITE_MISMATCH
7870: 32 30 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 20 /* Data typ
7880: 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f 0a 23 64 e mismatch */.#d
7890: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 efine SQLITE_MIS
78a0: 55 53 45 20 20 20 20 20 20 32 31 20 20 20 2f 2a USE 21 /*
78b0: 20 4c 69 62 72 61 72 79 20 75 73 65 64 20 69 6e Library used in
78c0: 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a 23 64 65 correctly */.#de
78d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 fine SQLITE_NOLF
78e0: 53 20 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 S 22 /*
78f0: 55 73 65 73 20 4f 53 20 66 65 61 74 75 72 65 73 Uses OS features
7900: 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 6f not supported o
7910: 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65 66 69 6e n host */.#defin
7920: 65 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20 e SQLITE_AUTH
7930: 20 20 20 20 20 32 33 20 20 20 2f 2a 20 41 75 74 23 /* Aut
7940: 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 horization denie
7950: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
7960: 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 ITE_FORMAT
7970: 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 24 /* Auxiliar
7980: 79 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 y database forma
7990: 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 t error */.#defi
79a0: 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 ne SQLITE_RANGE
79b0: 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 32 6e 25 /* 2n
79c0: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 d parameter to s
79d0: 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f 75 74 20 qlite3_bind out
79e0: 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23 64 65 66 of range */.#def
79f0: 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 ine SQLITE_NOTAD
7a00: 42 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 46 B 26 /* F
7a10: 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 61 74 20 ile opened that
7a20: 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62 61 73 is not a databas
7a30: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e e file */.#defin
7a40: 65 20 53 51 4c 49 54 45 5f 52 4f 57 20 20 20 20 e SQLITE_ROW
7a50: 20 20 20 20 20 31 30 30 20 20 2f 2a 20 73 71 6c 100 /* sql
7a60: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 ite3_step() has
7a70: 61 6e 6f 74 68 65 72 20 72 6f 77 20 72 65 61 64 another row read
7a80: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
7a90: 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 20 20 20 ITE_DONE
7aa0: 31 30 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 101 /* sqlite3_
7ab0: 73 74 65 70 28 29 20 68 61 73 20 66 69 6e 69 73 step() has finis
7ac0: 68 65 64 20 65 78 65 63 75 74 69 6e 67 20 2a 2f hed executing */
7ad0: 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 ./* end-of-error
7ae0: 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a -codes */../*.**
7af0: 20 43 41 50 49 33 52 45 46 3a 20 45 78 74 65 6e CAPI3REF: Exten
7b00: 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 ded Result Codes
7b10: 20 7b 48 31 30 32 32 30 7d 20 3c 53 31 30 37 30 {H10220} <S1070
7b20: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
7b30: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 {extended error
7b40: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 code} {extended
7b50: 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 error codes}.**
7b60: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e KEYWORDS: {exten
7b70: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 7d ded result code}
7b80: 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c {extended resul
7b90: 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 t codes}.**.** I
7ba0: 6e 20 69 74 73 20 64 65 66 61 75 6c 74 20 63 6f n its default co
7bb0: 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 53 51 4c nfiguration, SQL
7bc0: 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 ite API routines
7bd0: 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 32 return one of 2
7be0: 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 6 integer.** [SQ
7bf0: 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 LITE_OK | result
7c00: 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77 65 76 65 codes]. Howeve
7c10: 72 2c 20 65 78 70 65 72 69 65 6e 63 65 20 68 61 r, experience ha
7c20: 73 20 73 68 6f 77 6e 20 74 68 61 74 20 6d 61 6e s shown that man
7c30: 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 72 65 y of.** these re
7c40: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 74 sult codes are t
7c50: 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61 69 6e 65 oo coarse-graine
7c60: 64 2e 20 20 54 68 65 79 20 64 6f 20 6e 6f 74 20 d. They do not
7c70: 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a 20 6d 75 provide as.** mu
7c80: 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ch information a
7c90: 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 20 61 73 bout problems as
7ca0: 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 programmers mig
7cb0: 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20 61 6e 20 ht like. In an
7cc0: 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20 61 64 64 effort to.** add
7cd0: 72 65 73 73 20 74 68 69 73 2c 20 6e 65 77 65 72 ress this, newer
7ce0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
7cf0: 69 74 65 20 28 76 65 72 73 69 6f 6e 20 33 2e 33 ite (version 3.3
7d00: 2e 38 20 61 6e 64 20 6c 61 74 65 72 29 20 69 6e .8 and later) in
7d10: 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70 6f 72 74 clude.** support
7d20: 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
7d30: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 74 68 61 result codes tha
7d40: 74 20 70 72 6f 76 69 64 65 20 6d 6f 72 65 20 64 t provide more d
7d50: 65 74 61 69 6c 65 64 20 69 6e 66 6f 72 6d 61 74 etailed informat
7d60: 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 65 72 72 ion.** about err
7d70: 6f 72 73 2e 20 54 68 65 20 65 78 74 65 6e 64 65 ors. The extende
7d80: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 d result codes a
7d90: 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 re enabled or di
7da0: 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 sabled.** on a p
7db0: 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e er database conn
7dc0: 65 63 74 69 6f 6e 20 62 61 73 69 73 20 75 73 69 ection basis usi
7dd0: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ng the.** [sqlit
7de0: 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
7df0: 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50 49 2e lt_codes()] API.
7e00: 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 .**.** Some of t
7e10: 68 65 20 61 76 61 69 6c 61 62 6c 65 20 65 78 74 he available ext
7e20: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
7e30: 65 73 20 61 72 65 20 6c 69 73 74 65 64 20 68 65 es are listed he
7e40: 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 re..** One may e
7e50: 78 70 65 63 74 20 74 68 65 20 6e 75 6d 62 65 72 xpect the number
7e60: 20 6f 66 20 65 78 74 65 6e 64 65 64 20 72 65 73 of extended res
7e70: 75 6c 74 20 63 6f 64 65 73 20 77 69 6c 6c 20 62 ult codes will b
7e80: 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 e expand.** over
7e90: 20 74 69 6d 65 2e 20 20 53 6f 66 74 77 61 72 65 time. Software
7ea0: 20 74 68 61 74 20 75 73 65 73 20 65 78 74 65 6e that uses exten
7eb0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
7ec0: 20 73 68 6f 75 6c 64 20 65 78 70 65 63 74 0a 2a should expect.*
7ed0: 2a 20 74 6f 20 73 65 65 20 6e 65 77 20 72 65 73 * to see new res
7ee0: 75 6c 74 20 63 6f 64 65 73 20 69 6e 20 66 75 74 ult codes in fut
7ef0: 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
7f00: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 SQLite..**.** Th
7f10: 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 73 75 e SQLITE_OK resu
7f20: 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20 6e 65 76 lt code will nev
7f30: 65 72 20 62 65 20 65 78 74 65 6e 64 65 64 2e 20 er be extended.
7f40: 20 49 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a It will always.
7f50: 2a 2a 20 62 65 20 65 78 61 63 74 6c 79 20 7a 65 ** be exactly ze
7f60: 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ro..*/.#define S
7f70: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
7f80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 (S
7f90: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 QLITE_IOERR | (1
7fa0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
7fb0: 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
7fc0: 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 53 51 _READ (SQ
7fd0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c LITE_IOERR | (2<
7fe0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
7ff0: 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 ITE_IOERR_WRITE
8000: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8010: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c ITE_IOERR | (3<<
8020: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
8030: 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 TE_IOERR_FSYNC
8040: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
8050: 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 TE_IOERR | (4<<8
8060: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8070: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
8080: 43 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 C (SQLIT
8090: 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 E_IOERR | (5<<8)
80a0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
80b0: 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 _IOERR_TRUNCATE
80c0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
80d0: 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 _IOERR | (6<<8))
80e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
80f0: 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 IOERR_FSTAT
8100: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8110: 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a IOERR | (7<<8)).
8120: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8130: 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 OERR_UNLOCK
8140: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8150: 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 OERR | (8<<8)).#
8160: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
8170: 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 ERR_RDLOCK
8180: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
8190: 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 ERR | (9<<8)).#d
81a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
81b0: 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 RR_DELETE
81c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
81d0: 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 RR | (10<<8)).#d
81e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
81f0: 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 RR_BLOCKED
8200: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8210: 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 RR | (11<<8)).#d
8220: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
8230: 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 RR_NOMEM
8240: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8250: 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 RR | (12<<8)).#d
8260: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
8270: 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 RR_ACCESS
8280: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8290: 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 RR | (13<<8)).#d
82a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
82b0: 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
82c0: 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 LOCK (SQLITE_IOE
82d0: 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 RR | (14<<8)).#d
82e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
82f0: 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 RR_LOCK
8300: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8310: 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64 RR | (15<<8)).#d
8320: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
8330: 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 RR_CLOSE
8340: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8350: 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64 RR | (16<<8)).#d
8360: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
8370: 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20 RR_DIR_CLOSE
8380: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8390: 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a 23 64 RR | (17<<8)).#d
83a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 efine SQLITE_LOC
83b0: 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 KED_SHAREDCACHE
83c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4c 4f 43 (SQLITE_LOC
83d0: 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20 29 0a 0a KED | (1<<8) )..
83e0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
83f0: 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f Flags For File O
8400: 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b pen Operations {
8410: 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e H10230} <H11120>
8420: 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H12700>.**.**
8430: 54 68 65 73 65 20 62 69 74 20 76 61 6c 75 65 73 These bit values
8440: 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f are intended fo
8450: 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 r use in the.**
8460: 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 3rd parameter to
8470: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 the [sqlite3_op
8480: 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 en_v2()] interfa
8490: 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 ce and.** in the
84a0: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
84b0: 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 o the xOpen meth
84c0: 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 od of the.** [sq
84d0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
84e0: 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 t..*/.#define SQ
84f0: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
8500: 4c 59 20 20 20 20 20 20 20 20 20 30 78 30 30 30 LY 0x000
8510: 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 00001.#define SQ
8520: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
8530: 49 54 45 20 20 20 20 20 20 20 20 30 78 30 30 30 ITE 0x000
8540: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 00002.#define SQ
8550: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
8560: 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
8570: 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 00004.#define SQ
8580: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
8590: 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 ONCLOSE 0x000
85a0: 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 00008.#define SQ
85b0: 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
85c0: 49 56 45 20 20 20 20 20 20 20 20 30 78 30 30 30 IVE 0x000
85d0: 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 00010.#define SQ
85e0: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
85f0: 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 B 0x000
8600: 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00100.#define SQ
8610: 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 LITE_OPEN_TEMP_D
8620: 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 B 0x000
8630: 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00200.#define SQ
8640: 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 LITE_OPEN_TRANSI
8650: 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30 30 ENT_DB 0x000
8660: 30 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00400.#define SQ
8670: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
8680: 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 OURNAL 0x000
8690: 30 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00800.#define SQ
86a0: 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a LITE_OPEN_TEMP_J
86b0: 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 OURNAL 0x000
86c0: 30 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 01000.#define SQ
86d0: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 LITE_OPEN_SUBJOU
86e0: 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30 30 RNAL 0x000
86f0: 30 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 02000.#define SQ
8700: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 LITE_OPEN_MASTER
8710: 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30 _JOURNAL 0x000
8720: 30 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 04000.#define SQ
8730: 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 LITE_OPEN_NOMUTE
8740: 58 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 X 0x000
8750: 30 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51 08000.#define SQ
8760: 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 LITE_OPEN_FULLMU
8770: 54 45 58 20 20 20 20 20 20 20 20 30 78 30 30 30 TEX 0x000
8780: 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 10000../*.** CAP
8790: 49 33 52 45 46 3a 20 44 65 76 69 63 65 20 43 68 I3REF: Device Ch
87a0: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 7b 48 aracteristics {H
87b0: 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 10240} <H11120>.
87c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63 **.** The xDevic
87d0: 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65 eCapabilities me
87e0: 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c thod of the [sql
87f0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
8800: 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 .** object retur
8810: 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 ns an integer wh
8820: 69 63 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 ich is a vector
8830: 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 of the these.**
8840: 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 72 65 bit values expre
8850: 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 ssing I/O charac
8860: 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 teristics of the
8870: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a mass storage.**
8880: 20 64 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c device that hol
8890: 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 ds the file that
88a0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f the [sqlite3_io
88b0: 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 _methods].** ref
88c0: 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 ers to..**.** Th
88d0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
88e0: 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d TOMIC property m
88f0: 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 eans that all wr
8900: 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 ites of.** any s
8910: 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 ize are atomic.
8920: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
8930: 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 P_ATOMICnnn valu
8940: 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 es.** mean that
8950: 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 writes of blocks
8960: 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 that are nnn by
8970: 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a tes in size and.
8980: 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 ** are aligned t
8990: 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 o an address whi
89a0: 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ch is an integer
89b0: 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 multiple of.**
89c0: 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 nnn are atomic.
89d0: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
89e0: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 P_SAFE_APPEND va
89f0: 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 lue means.** tha
8a00: 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 t when data is a
8a10: 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c ppended to a fil
8a20: 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61 e, the data is a
8a30: 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 ppended.** first
8a40: 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f then the size o
8a50: 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 f the file is ex
8a60: 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 tended, never th
8a70: 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 e other.** way a
8a80: 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 round. The SQLI
8a90: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
8aa0: 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 IAL property mea
8ab0: 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 ns that.** infor
8ac0: 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 mation is writte
8ad0: 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 n to disk in the
8ae0: 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 same order as c
8af0: 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 alls.** to xWrit
8b00: 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 e()..*/.#define
8b10: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8b20: 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 78 30 MIC 0x0
8b30: 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 0000001.#define
8b40: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8b50: 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 78 30 MIC512 0x0
8b60: 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 0000002.#define
8b70: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8b80: 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 78 30 MIC1K 0x0
8b90: 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 0000004.#define
8ba0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8bb0: 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 78 30 MIC2K 0x0
8bc0: 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 0000008.#define
8bd0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8be0: 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 78 30 MIC4K 0x0
8bf0: 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 0000010.#define
8c00: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8c10: 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 78 30 MIC8K 0x0
8c20: 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20 0000020.#define
8c30: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8c40: 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 78 30 MIC16K 0x0
8c50: 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20 0000040.#define
8c60: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8c70: 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 78 30 MIC32K 0x0
8c80: 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20 0000080.#define
8c90: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
8ca0: 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 78 30 MIC64K 0x0
8cb0: 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 0000100.#define
8cc0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 SQLITE_IOCAP_SAF
8cd0: 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78 30 E_APPEND 0x0
8ce0: 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 0000200.#define
8cf0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
8d00: 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30 UENTIAL 0x0
8d10: 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 0000400../*.** C
8d20: 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f API3REF: File Lo
8d30: 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 cking Levels {H1
8d40: 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 0250} <H11120> <
8d50: 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 H11310>.**.** SQ
8d60: 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 Lite uses one of
8d70: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 these integer v
8d80: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 alues as the sec
8d90: 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 ond.** argument
8da0: 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 to calls it make
8db0: 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 s to the xLock()
8dc0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d and xUnlock() m
8dd0: 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 ethods.** of an
8de0: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 [sqlite3_io_meth
8df0: 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ods] object..*/.
8e00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
8e10: 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 OCK_NONE
8e20: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
8e30: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 TE_LOCK_SHARED
8e40: 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
8e50: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 SQLITE_LOCK_RESE
8e60: 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 RVED 2.#def
8e70: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
8e80: 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a PENDING 3.
8e90: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
8ea0: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 OCK_EXCLUSIVE
8eb0: 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 4../*.** CAPI3
8ec0: 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 REF: Synchroniza
8ed0: 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 tion Type Flags
8ee0: 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30 {H10260} <H11120
8ef0: 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c >.**.** When SQL
8f00: 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ite invokes the
8f10: 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f xSync() method o
8f20: 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 f an.** [sqlite3
8f30: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a _io_methods] obj
8f40: 65 63 74 20 69 74 20 75 73 65 73 20 61 20 63 6f ect it uses a co
8f50: 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 mbination of.**
8f60: 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 these integer va
8f70: 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f lues as the seco
8f80: 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a nd argument..**.
8f90: 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 ** When the SQLI
8fa0: 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
8fb0: 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 flag is used, i
8fc0: 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
8fd0: 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 .** sync operati
8fe0: 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f on only needs to
8ff0: 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d flush data to m
9000: 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e ass storage. In
9010: 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ode.** informati
9020: 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 on need not be f
9030: 6c 75 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c lushed. If the l
9040: 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f ower four bits o
9050: 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71 f the flag.** eq
9060: 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f ual SQLITE_SYNC_
9070: 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61 NORMAL, that mea
9080: 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c ns to use normal
9090: 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69 fsync() semanti
90a0: 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f cs..** If the lo
90b0: 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 65 71 wer four bits eq
90c0: 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f ual SQLITE_SYNC_
90d0: 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 FULL, that means
90e0: 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f .** to use Mac O
90f0: 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 S X style fullsy
9100: 6e 63 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 nc instead of fs
9110: 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e ync()..*/.#defin
9120: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f e SQLITE_SYNC_NO
9130: 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78 30 30 RMAL 0x00
9140: 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 002.#define SQLI
9150: 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20 TE_SYNC_FULL
9160: 20 20 20 20 20 20 30 78 30 30 30 30 33 0a 23 64 0x00003.#d
9170: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e efine SQLITE_SYN
9180: 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 C_DATAONLY
9190: 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 0x00010../*.** C
91a0: 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 API3REF: OS Inte
91b0: 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20 rface Open File
91c0: 48 61 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20 Handle {H11110}
91d0: 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 <S20110>.**.** A
91e0: 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d n [sqlite3_file]
91f0: 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e object represen
9200: 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 ts an open file
9210: 69 6e 20 74 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 in the OS.** int
9220: 65 72 66 61 63 65 20 6c 61 79 65 72 2e 20 20 49 erface layer. I
9230: 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 ndividual OS int
9240: 65 72 66 61 63 65 20 69 6d 70 6c 65 6d 65 6e 74 erface implement
9250: 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 ations will.** w
9260: 61 6e 74 20 74 6f 20 73 75 62 63 6c 61 73 73 20 ant to subclass
9270: 74 68 69 73 20 6f 62 6a 65 63 74 20 62 79 20 61 this object by a
9280: 70 70 65 6e 64 69 6e 67 20 61 64 64 69 74 69 6f ppending additio
9290: 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a 20 66 6f nal fields.** fo
92a0: 72 20 74 68 65 69 72 20 6f 77 6e 20 75 73 65 2e r their own use.
92b0: 20 20 54 68 65 20 70 4d 65 74 68 6f 64 73 20 65 The pMethods e
92c0: 6e 74 72 79 20 69 73 20 61 20 70 6f 69 6e 74 65 ntry is a pointe
92d0: 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 r to an.** [sqli
92e0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 te3_io_methods]
92f0: 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 69 object that defi
9300: 6e 65 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 nes methods for
9310: 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f performing.** I/
9320: 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 O operations on
9330: 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a the open file..*
9340: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
9350: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 71 sqlite3_file sq
9360: 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73 74 72 75 lite3_file;.stru
9370: 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ct sqlite3_file
9380: 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 {. const struct
9390: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
93a0: 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 ods *pMethods;
93b0: 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 61 /* Methods for a
93c0: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d n open file */.}
93d0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
93e0: 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 F: OS Interface
93f0: 46 69 6c 65 20 56 69 72 74 75 61 6c 20 4d 65 74 File Virtual Met
9400: 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 31 hods Object {H11
9410: 31 32 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 120} <S20110>.**
9420: 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65 20 6f .** Every file o
9430: 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71 pened by the [sq
9440: 6c 69 74 65 33 5f 76 66 73 5d 20 78 4f 70 65 6e lite3_vfs] xOpen
9450: 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61 74 65 method populate
9460: 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 s an.** [sqlite3
9470: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28 6f _file] object (o
9480: 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 r, more commonly
9490: 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 , a subclass of
94a0: 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
94b0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77 69 file] object) wi
94c0: 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 th a pointer to
94d0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
94e0: 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 his object..** T
94f0: 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e his object defin
9500: 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73 20 75 es the methods u
9510: 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 sed to perform v
9520: 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69 6f 6e arious operation
9530: 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 s.** against the
9540: 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70 72 65 open file repre
9550: 73 65 6e 74 65 64 20 62 79 20 74 68 65 20 5b 73 sented by the [s
9560: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a qlite3_file] obj
9570: 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ect..**.** The f
9580: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f lags argument to
9590: 20 78 53 79 6e 63 20 6d 61 79 20 62 65 20 6f 6e xSync may be on
95a0: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59 4e e of [SQLITE_SYN
95b0: 43 5f 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 C_NORMAL] or.**
95c0: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c [SQLITE_SYNC_FUL
95d0: 4c 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 63 L]. The first c
95e0: 68 6f 69 63 65 20 69 73 20 74 68 65 20 6e 6f 72 hoice is the nor
95f0: 6d 61 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 mal fsync()..**
9600: 54 68 65 20 73 65 63 6f 6e 64 20 63 68 6f 69 63 The second choic
9610: 65 20 69 73 20 61 20 4d 61 63 20 4f 53 20 58 20 e is a Mac OS X
9620: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e 20 style fullsync.
9630: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 53 59 4e The [SQLITE_SYN
9640: 43 5f 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 C_DATAONLY].** f
9650: 6c 61 67 20 6d 61 79 20 62 65 20 4f 52 65 64 20 lag may be ORed
9660: 69 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 in to indicate t
9670: 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 64 61 74 hat only the dat
9680: 61 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a a of the file.**
9690: 20 61 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e 6f and not its ino
96a0: 64 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 de needs to be s
96b0: 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ynced..**.** The
96c0: 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
96d0: 74 6f 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 to xLock() and x
96e0: 55 6e 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65 Unlock() are one
96f0: 20 6f 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c of.** <ul>.** <
9700: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b li> [SQLITE_LOCK
9710: 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 _NONE],.** <li>
9720: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 [SQLITE_LOCK_SHA
9730: 52 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 RED],.** <li> [S
9740: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 QLITE_LOCK_RESER
9750: 56 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 VED],.** <li> [S
9760: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 QLITE_LOCK_PENDI
9770: 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 NG], or.** <li>
9780: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 [SQLITE_LOCK_EXC
9790: 4c 55 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c LUSIVE]..** </ul
97a0: 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e 63 >.** xLock() inc
97b0: 72 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e reases the lock.
97c0: 20 78 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72 65 xUnlock() decre
97d0: 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a ases the lock..*
97e0: 2a 20 54 68 65 20 78 43 68 65 63 6b 52 65 73 65 * The xCheckRese
97f0: 72 76 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 6f rvedLock() metho
9800: 64 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 d checks whether
9810: 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f any database co
9820: 6e 6e 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 nnection,.** eit
9830: 68 65 72 20 69 6e 20 74 68 69 73 20 70 72 6f 63 her in this proc
9840: 65 73 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20 6f ess or in some o
9850: 74 68 65 72 20 70 72 6f 63 65 73 73 2c 20 69 73 ther process, is
9860: 20 68 6f 6c 64 69 6e 67 20 61 20 52 45 53 45 52 holding a RESER
9870: 56 45 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c VED,.** PENDING,
9880: 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
9890: 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 ck on the file.
98a0: 20 49 74 20 72 65 74 75 72 6e 73 20 74 72 75 65 It returns true
98b0: 0a 2a 2a 20 69 66 20 73 75 63 68 20 61 20 6c 6f .** if such a lo
98c0: 63 6b 20 65 78 69 73 74 73 20 61 6e 64 20 66 61 ck exists and fa
98d0: 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a lse otherwise..*
98e0: 2a 0a 2a 2a 20 54 68 65 20 78 46 69 6c 65 43 6f *.** The xFileCo
98f0: 6e 74 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20 69 ntrol() method i
9900: 73 20 61 20 67 65 6e 65 72 69 63 20 69 6e 74 65 s a generic inte
9910: 72 66 61 63 65 20 74 68 61 74 20 61 6c 6c 6f 77 rface that allow
9920: 73 20 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 s custom.** VFS
9930: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
9940: 74 6f 20 64 69 72 65 63 74 6c 79 20 63 6f 6e 74 to directly cont
9950: 72 6f 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 rol an open file
9960: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 using the.** [s
9970: 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
9980: 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 rol()] interface
9990: 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 22 6f . The second "o
99a0: 70 22 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 p" argument is a
99b0: 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 63 n.** integer opc
99c0: 6f 64 65 2e 20 20 54 68 65 20 74 68 69 72 64 20 ode. The third
99d0: 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65 argument is a ge
99e0: 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 69 6e neric pointer in
99f0: 74 65 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 tended to.** poi
9a00: 6e 74 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 nt to a structur
9a10: 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 e that may conta
9a20: 69 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 in arguments or
9a30: 73 70 61 63 65 20 69 6e 20 77 68 69 63 68 20 74 space in which t
9a40: 6f 0a 2a 2a 20 77 72 69 74 65 20 72 65 74 75 72 o.** write retur
9a50: 6e 20 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 6e n values. Poten
9a60: 74 69 61 6c 20 75 73 65 73 20 66 6f 72 20 78 46 tial uses for xF
9a70: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 ileControl() mig
9a80: 68 74 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 6f ht be.** functio
9a90: 6e 73 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c 6f ns to enable blo
9aa0: 63 6b 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 68 cking locks with
9ab0: 20 74 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 68 timeouts, to ch
9ac0: 61 6e 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b ange the.** lock
9ad0: 69 6e 67 20 73 74 72 61 74 65 67 79 20 28 66 6f ing strategy (fo
9ae0: 72 20 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 65 r example to use
9af0: 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 dot-file locks)
9b00: 2c 20 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a 20 , to inquire.**
9b10: 61 62 6f 75 74 20 74 68 65 20 73 74 61 74 75 73 about the status
9b20: 20 6f 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 of a lock, or t
9b30: 6f 20 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f o break stale lo
9b40: 63 6b 73 2e 20 20 54 68 65 20 53 51 4c 69 74 65 cks. The SQLite
9b50: 0a 2a 2a 20 63 6f 72 65 20 72 65 73 65 72 76 65 .** core reserve
9b60: 73 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c 65 s all opcodes le
9b70: 73 73 20 74 68 61 6e 20 31 30 30 20 66 6f 72 20 ss than 100 for
9b80: 69 74 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 its own use..**
9b90: 41 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f A [SQLITE_FCNTL_
9ba0: 4c 4f 43 4b 53 54 41 54 45 20 7c 20 6c 69 73 74 LOCKSTATE | list
9bb0: 20 6f 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65 73 of opcodes] les
9bc0: 73 20 74 68 61 6e 20 31 30 30 20 69 73 20 61 76 s than 100 is av
9bd0: 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c ailable..** Appl
9be0: 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 64 65 ications that de
9bf0: 66 69 6e 65 20 61 20 63 75 73 74 6f 6d 20 78 46 fine a custom xF
9c00: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
9c10: 64 20 73 68 6f 75 6c 64 20 75 73 65 20 6f 70 63 d should use opc
9c20: 6f 64 65 73 0a 2a 2a 20 67 72 65 61 74 65 72 20 odes.** greater
9c30: 74 68 61 6e 20 31 30 30 20 74 6f 20 61 76 6f 69 than 100 to avoi
9c40: 64 20 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a d conflicts..**.
9c50: 2a 2a 20 54 68 65 20 78 53 65 63 74 6f 72 53 69 ** The xSectorSi
9c60: 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 ze() method retu
9c70: 72 6e 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 rns the sector s
9c80: 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 ize of the.** de
9c90: 76 69 63 65 20 74 68 61 74 20 75 6e 64 65 72 6c vice that underl
9ca0: 69 65 73 20 74 68 65 20 66 69 6c 65 2e 20 20 54 ies the file. T
9cb0: 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 he sector size i
9cc0: 73 20 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d s the.** minimum
9cd0: 20 77 72 69 74 65 20 74 68 61 74 20 63 61 6e 20 write that can
9ce0: 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74 be performed wit
9cf0: 68 6f 75 74 20 64 69 73 74 75 72 62 69 6e 67 0a hout disturbing.
9d00: 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 69 ** other bytes i
9d10: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 n the file. The
9d20: 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 xDeviceCharacte
9d30: 72 69 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65 74 ristics().** met
9d40: 68 6f 64 20 72 65 74 75 72 6e 73 20 61 20 62 69 hod returns a bi
9d50: 74 20 76 65 63 74 6f 72 20 64 65 73 63 72 69 62 t vector describ
9d60: 69 6e 67 20 62 65 68 61 76 69 6f 72 73 20 6f 66 ing behaviors of
9d70: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
9d80: 6e 67 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a ng device:.**.**
9d90: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 <ul>.** <li> [S
9da0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
9db0: 49 43 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c IC].** <li> [SQL
9dc0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9dd0: 35 31 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 512].** <li> [SQ
9de0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9df0: 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 C1K].** <li> [SQ
9e00: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9e10: 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 C2K].** <li> [SQ
9e20: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9e30: 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 C4K].** <li> [SQ
9e40: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9e50: 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 C8K].** <li> [SQ
9e60: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9e70: 43 31 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 C16K].** <li> [S
9e80: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
9e90: 49 43 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b IC32K].** <li> [
9ea0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
9eb0: 4d 49 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 MIC64K].** <li>
9ec0: 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 [SQLITE_IOCAP_SA
9ed0: 46 45 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c FE_APPEND].** <l
9ee0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
9ef0: 5f 53 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 _SEQUENTIAL].**
9f00: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
9f10: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
9f20: 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 MIC property mea
9f30: 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 ns that all writ
9f40: 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a es of.** any siz
9f50: 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 e are atomic. T
9f60: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
9f70: 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 ATOMICnnn values
9f80: 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 .** mean that wr
9f90: 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 ites of blocks t
9fa0: 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 hat are nnn byte
9fb0: 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a s in size and.**
9fc0: 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 are aligned to
9fd0: 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68 an address which
9fe0: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d is an integer m
9ff0: 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e ultiple of.** nn
a000: 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 n are atomic. T
a010: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
a020: 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 SAFE_APPEND valu
a030: 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 e means.** that
a040: 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 when data is app
a050: 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c ended to a file,
a060: 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70 the data is app
a070: 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 ended.** first t
a080: 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 hen the size of
a090: 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 the file is exte
a0a0: 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 nded, never the
a0b0: 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f other.** way aro
a0c0: 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 und. The SQLITE
a0d0: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
a0e0: 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 L property means
a0f0: 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 that.** informa
a100: 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 tion is written
a110: 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 to disk in the s
a120: 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c ame order as cal
a130: 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 ls.** to xWrite(
a140: 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65 61 )..**.** If xRea
a150: 64 28 29 20 72 65 74 75 72 6e 73 20 53 51 4c 49 d() returns SQLI
a160: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
a170: 45 41 44 20 69 74 20 6d 75 73 74 20 61 6c 73 6f EAD it must also
a180: 20 66 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 fill.** in the
a190: 75 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 unread portions
a1a0: 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77 69 of the buffer wi
a1b0: 74 68 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 53 th zeros. A VFS
a1c0: 20 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20 74 that.** fails t
a1d0: 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 o zero-fill shor
a1e0: 74 20 72 65 61 64 73 20 6d 69 67 68 74 20 73 65 t reads might se
a1f0: 65 6d 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 em to work. How
a200: 65 76 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 ever,.** failure
a210: 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 to zero-fill sh
a220: 6f 72 74 20 72 65 61 64 73 20 77 69 6c 6c 20 65 ort reads will e
a230: 76 65 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 74 ventually lead t
a240: 6f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f o.** database co
a250: 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 rruption..*/.typ
a260: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
a270: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 73 te3_io_methods s
a280: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
a290: 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 s;.struct sqlite
a2a0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 3_io_methods {.
a2b0: 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 int iVersion;.
a2c0: 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 int (*xClose)(s
a2d0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 qlite3_file*);.
a2e0: 20 69 6e 74 20 28 2a 78 52 65 61 64 29 28 73 71 int (*xRead)(sq
a2f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 lite3_file*, voi
a300: 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 d*, int iAmt, sq
a310: 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 lite3_int64 iOfs
a320: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 69 t);. int (*xWri
a330: 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 te)(sqlite3_file
a340: 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
a350: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 int iAmt, sqlite
a360: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 3_int64 iOfst);.
a370: 20 20 69 6e 74 20 28 2a 78 54 72 75 6e 63 61 74 int (*xTruncat
a380: 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a e)(sqlite3_file*
a390: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
a3a0: 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 size);. int (*x
a3b0: 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 66 69 Sync)(sqlite3_fi
a3c0: 6c 65 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b le*, int flags);
a3d0: 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 69 . int (*xFileSi
a3e0: 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ze)(sqlite3_file
a3f0: 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
a400: 20 2a 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20 *pSize);. int
a410: 28 2a 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 (*xLock)(sqlite3
a420: 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 _file*, int);.
a430: 69 6e 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 int (*xUnlock)(s
a440: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
a450: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68 65 t);. int (*xChe
a460: 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 28 ckReservedLock)(
a470: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
a480: 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20 nt *pResOut);.
a490: 69 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72 int (*xFileContr
a4a0: 6f 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ol)(sqlite3_file
a4b0: 2a 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 *, int op, void
a4c0: 2a 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a *pArg);. int (*
a4d0: 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c xSectorSize)(sql
a4e0: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 ite3_file*);. i
a4f0: 6e 74 20 28 2a 78 44 65 76 69 63 65 43 68 61 72 nt (*xDeviceChar
a500: 61 63 74 65 72 69 73 74 69 63 73 29 28 73 71 6c acteristics)(sql
a510: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f ite3_file*);. /
a520: 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 74 * Additional met
a530: 68 6f 64 73 20 6d 61 79 20 62 65 20 61 64 64 65 hods may be adde
a540: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
a550: 61 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ases */.};../*.*
a560: 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 6e * CAPI3REF: Stan
a570: 64 61 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 6f dard File Contro
a580: 6c 20 4f 70 63 6f 64 65 73 20 7b 48 31 31 33 31 l Opcodes {H1131
a590: 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 0} <S30800>.**.*
a5a0: 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 * These integer
a5b0: 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 6f 70 constants are op
a5c0: 63 6f 64 65 73 20 66 6f 72 20 74 68 65 20 78 46 codes for the xF
a5d0: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
a5e0: 64 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c d.** of the [sql
a5f0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
a600: 20 6f 62 6a 65 63 74 20 61 6e 64 20 66 6f 72 20 object and for
a610: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c the [sqlite3_fil
a620: 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 e_control()].**
a630: 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
a640: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e The [SQLITE_FCN
a650: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70 TL_LOCKSTATE] op
a660: 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 code is used for
a670: 20 64 65 62 75 67 67 69 6e 67 2e 20 20 54 68 69 debugging. Thi
a680: 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 73 s.** opcode caus
a690: 65 73 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 es the xFileCont
a6a0: 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 72 rol method to wr
a6b0: 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
a6c0: 73 74 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 state of.** the
a6d0: 6c 6f 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 51 lock (one of [SQ
a6e0: 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c LITE_LOCK_NONE],
a6f0: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 [SQLITE_LOCK_SH
a700: 41 52 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 ARED],.** [SQLIT
a710: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d E_LOCK_RESERVED]
a720: 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 , [SQLITE_LOCK_P
a730: 45 4e 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c ENDING], or [SQL
a740: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 ITE_LOCK_EXCLUSI
a750: 56 45 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 VE]).** into an
a760: 69 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65 integer that the
a770: 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20 70 pArg argument p
a780: 6f 69 6e 74 73 20 74 6f 2e 20 54 68 69 73 20 63 oints to. This c
a790: 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 apability.** is
a7a0: 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 used during test
a7b0: 69 6e 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 ing and only nee
a7c0: 64 73 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 ds to be support
a7d0: 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 54 ed when SQLITE_T
a7e0: 45 53 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 EST.** is define
a7f0: 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 d..*/.#define SQ
a800: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
a810: 54 41 54 45 20 20 20 20 20 20 20 20 31 0a 23 64 TATE 1.#d
a820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 47 45 54 efine SQLITE_GET
a830: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 _LOCKPROXYFILE
a840: 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
a850: 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f LITE_SET_LOCKPRO
a860: 58 59 46 49 4c 45 20 20 20 20 20 20 33 0a 23 64 XYFILE 3.#d
a870: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 41 53 efine SQLITE_LAS
a880: 54 5f 45 52 52 4e 4f 20 20 20 20 20 20 20 20 20 T_ERRNO
a890: 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 4../*.** CAP
a8a0: 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61 6e I3REF: Mutex Han
a8b0: 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c 53 32 dle {H17110} <S2
a8c0: 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0130>.**.** The
a8d0: 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77 69 74 mutex module wit
a8e0: 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66 69 6e hin SQLite defin
a8f0: 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 es [sqlite3_mute
a900: 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 x] to be an.** a
a910: 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f 72 bstract type for
a920: 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 2e a mutex object.
a930: 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 The SQLite cor
a940: 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a e never looks.**
a950: 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c at the internal
a960: 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
a970: 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d of an [sqlite3_m
a980: 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a utex]. It only.
a990: 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f ** deals with po
a9a0: 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 5b 73 inters to the [s
a9b0: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 qlite3_mutex] ob
a9c0: 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 ject..**.** Mute
a9d0: 78 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 xes are created
a9e0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d using [sqlite3_m
a9f0: 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a utex_alloc()]..*
aa00: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
aa10: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 sqlite3_mutex s
aa20: 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f qlite3_mutex;../
aa30: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
aa40: 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 6a 65 S Interface Obje
aa50: 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53 32 30 ct {H11140} <S20
aa60: 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 100>.**.** An in
aa70: 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 stance of the sq
aa80: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 lite3_vfs object
aa90: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
aaa0: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 0a 2a erface between.*
aab0: 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 * the SQLite cor
aac0: 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c e and the underl
aad0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
aae0: 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 66 73 ystem. The "vfs
aaf0: 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 6d 65 ".** in the name
ab00: 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 20 73 of the object s
ab10: 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72 74 75 tands for "virtu
ab20: 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 22 2e al file system".
ab30: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
ab40: 20 6f 66 20 74 68 65 20 69 56 65 72 73 69 6f 6e of the iVersion
ab50: 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74 69 61 field is initia
ab60: 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20 62 65 lly 1 but may be
ab70: 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20 66 75 larger in.** fu
ab80: 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
ab90: 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69 74 69 SQLite. Additi
aba0: 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61 79 20 onal fields may
abb0: 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 be appended to t
abc0: 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 68 his.** object wh
abd0: 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20 en the iVersion
abe0: 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 value is increas
abf0: 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 ed. Note that t
ac00: 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 he structure.**
ac10: 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 of the sqlite3_v
ac20: 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e 67 65 fs object change
ac30: 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 s in the transac
ac40: 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 tion between.**
ac50: 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 SQLite version 3
ac60: 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30 20 61 .5.9 and 3.6.0 a
ac70: 6e 64 20 79 65 74 20 74 68 65 20 69 56 65 72 73 nd yet the iVers
ac80: 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20 6e 6f ion field was no
ac90: 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e 0a 2a t.** modified..*
aca0: 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69 6c *.** The szOsFil
acb0: 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 73 e field is the s
acc0: 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 63 6c ize of the subcl
acd0: 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 5f 66 assed [sqlite3_f
ace0: 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 ile].** structur
acf0: 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 56 e used by this V
ad00: 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 FS. mxPathname
ad10: 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6c is the maximum l
ad20: 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70 61 ength of.** a pa
ad30: 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 20 56 thname in this V
ad40: 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 FS..**.** Regist
ad50: 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66 73 ered sqlite3_vfs
ad60: 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65 70 objects are kep
ad70: 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 t on a linked li
ad80: 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a 2a 20 st formed by.**
ad90: 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 the pNext pointe
ada0: 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 r. The [sqlite3
adb0: 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 5d _vfs_register()]
adc0: 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 .** and [sqlite3
add0: 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 _vfs_unregister(
ade0: 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d 61 )] interfaces ma
adf0: 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0a 2a nage this list.*
ae00: 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d 73 61 * in a thread-sa
ae10: 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b 73 71 fe way. The [sq
ae20: 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 lite3_vfs_find()
ae30: 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 73 ] interface.** s
ae40: 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 earches the list
ae50: 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 61 . Neither the a
ae60: 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 pplication code
ae70: 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a 20 69 nor the VFS.** i
ae80: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 mplementation sh
ae90: 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70 4e 65 ould use the pNe
aea0: 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a xt pointer..**.*
aeb0: 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69 65 6c * The pNext fiel
aec0: 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 66 69 d is the only fi
aed0: 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 eld in the sqlit
aee0: 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75 63 74 e3_vfs.** struct
aef0: 75 72 65 20 74 68 61 74 20 53 51 4c 69 74 65 20 ure that SQLite
af00: 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69 66 79 will ever modify
af10: 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f . SQLite will o
af20: 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20 6f 72 nly access.** or
af30: 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66 69 65 modify this fie
af40: 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 ld while holding
af50: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 a particular st
af60: 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 atic mutex..** T
af70: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 he application s
af80: 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f 64 69 hould never modi
af90: 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 fy anything with
afa0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
afb0: 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 fs.** object onc
afc0: 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68 61 73 e the object has
afd0: 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 been registered
afe0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d ..**.** The zNam
aff0: 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20 74 68 e field holds th
b000: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 e name of the VF
b010: 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 6e S module. The n
b020: 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65 20 75 ame must.** be u
b030: 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61 6c 6c nique across all
b040: 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a VFS modules..**
b050: 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
b060: 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 guarantee that t
b070: 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 he zFilename par
b080: 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a ameter to xOpen.
b090: 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e ** is either a N
b0a0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73 ULL pointer or s
b0b0: 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a tring obtained.*
b0c0: 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68 * from xFullPath
b0d0: 6e 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65 20 name(). SQLite
b0e0: 66 75 72 74 68 65 72 20 67 75 61 72 61 6e 74 65 further guarante
b0f0: 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 es that.** the s
b100: 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 61 tring will be va
b110: 6c 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 65 lid and unchange
b120: 64 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 29 d until xClose()
b130: 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 42 is.** called. B
b140: 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 ecause of the pr
b150: 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 73 65 2c evious sentense,
b160: 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 .** the [sqlite3
b170: 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66 65 6c _file] can safel
b180: 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 y store a pointe
b190: 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 6c 65 r to the.** file
b1a0: 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65 64 73 name if it needs
b1b0: 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 65 to remember the
b1c0: 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20 73 6f filename for so
b1d0: 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66 me reason..** If
b1e0: 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 the zFilename p
b1f0: 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f 70 65 arameter is xOpe
b200: 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e n is a NULL poin
b210: 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e 0a 2a ter then xOpen.*
b220: 2a 20 6d 75 73 74 20 69 6e 76 69 74 65 20 69 74 * must invite it
b230: 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 79 20 s own temporary
b240: 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66 69 6c name for the fil
b250: 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74 68 65 e. Whenever the
b260: 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65 20 70 .** xFilename p
b270: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c arameter is NULL
b280: 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20 62 65 it will also be
b290: 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74 the case that t
b2a0: 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61 72 61 he.** flags para
b2b0: 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63 6c 75 meter will inclu
b2c0: 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f de [SQLITE_OPEN_
b2d0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a DELETEONCLOSE]..
b2e0: 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 **.** The flags
b2f0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 argument to xOpe
b300: 6e 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c n() includes all
b310: 20 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a 20 bits set in.**
b320: 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 the flags argume
b330: 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f nt to [sqlite3_o
b340: 70 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69 pen_v2()]. Or i
b350: 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 f [sqlite3_open(
b360: 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 )].** or [sqlite
b370: 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75 3_open16()] is u
b380: 73 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20 sed, then flags
b390: 69 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73 includes at leas
b3a0: 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 t.** [SQLITE_OPE
b3b0: 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b N_READWRITE] | [
b3c0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
b3d0: 54 45 5d 2e 20 0a 2a 2a 20 49 66 20 78 4f 70 65 TE]. .** If xOpe
b3e0: 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c 65 n() opens a file
b3f0: 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 read-only then
b400: 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c 61 it sets *pOutFla
b410: 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 gs to.** include
b420: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 [SQLITE_OPEN_RE
b430: 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72 20 ADONLY]. Other
b440: 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c 61 bits in *pOutFla
b450: 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a 2a gs may be set..*
b460: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c *.** SQLite will
b470: 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 also add one of
b480: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
b490: 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 lags to the xOpe
b4a0: 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 n().** call, dep
b4b0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 62 ending on the ob
b4c0: 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65 6e 65 ject being opene
b4d0: 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a d:.**.** <ul>.**
b4e0: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f <li> [SQLITE_O
b4f0: 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 PEN_MAIN_DB].**
b500: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
b510: 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d EN_MAIN_JOURNAL]
b520: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
b530: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a E_OPEN_TEMP_DB].
b540: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
b550: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e _OPEN_TEMP_JOURN
b560: 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 AL].** <li> [SQ
b570: 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 LITE_OPEN_TRANSI
b580: 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 ENT_DB].** <li>
b590: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 [SQLITE_OPEN_SU
b5a0: 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 BJOURNAL].** <li
b5b0: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
b5c0: 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a MASTER_JOURNAL].
b5d0: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
b5e0: 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70 6c he file I/O impl
b5f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 75 ementation can u
b600: 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74 79 se the object ty
b610: 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 pe flags to.** c
b620: 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 69 74 hange the way it
b630: 20 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c 65 deals with file
b640: 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c s. For example,
b650: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a an application.
b660: 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ** that does not
b670: 20 63 61 72 65 20 61 62 6f 75 74 20 63 72 61 73 care about cras
b680: 68 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72 6f h recovery or ro
b690: 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61 6b llback might mak
b6a0: 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f 66 e.** the open of
b6b0: 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 a journal file
b6c0: 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65 73 a no-op. Writes
b6d0: 20 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c to this journal
b6e0: 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 would.** also b
b6f0: 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61 6e e no-ops, and an
b700: 79 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 y attempt to rea
b710: 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 6f d the journal wo
b720: 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 uld return.** SQ
b730: 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72 20 LITE_IOERR. Or
b740: 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
b750: 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e 69 on might recogni
b760: 7a 65 20 74 68 61 74 20 61 20 64 61 74 61 62 61 ze that a databa
b770: 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c 20 se.** file will
b780: 62 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61 6c be doing page-al
b790: 69 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65 61 igned sector rea
b7a0: 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 69 6e ds and writes in
b7b0: 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 a random.** ord
b7c0: 65 72 20 61 6e 64 20 73 65 74 20 75 70 20 69 74 er and set up it
b7d0: 73 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d 20 s I/O subsystem
b7e0: 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a accordingly..**.
b7f0: 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 ** SQLite might
b800: 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 also add one of
b810: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c the following fl
b820: 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e ags to the xOpen
b830: 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c method:.**.** <
b840: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c ul>.** <li> [SQL
b850: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
b860: 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 NCLOSE].** <li>
b870: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 [SQLITE_OPEN_EXC
b880: 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e LUSIVE].** </ul>
b890: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 .**.** The [SQLI
b8a0: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
b8b0: 43 4c 4f 53 45 5d 20 66 6c 61 67 20 6d 65 61 6e CLOSE] flag mean
b8c0: 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c s the file shoul
b8d0: 64 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20 d be.** deleted
b8e0: 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
b8f0: 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f d. The [SQLITE_
b900: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
b910: 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 SE].** will be s
b920: 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61 74 et for TEMP dat
b930: 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c 73 abases, journals
b940: 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75 72 and for subjour
b950: 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nals..**.** The
b960: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 [SQLITE_OPEN_EXC
b970: 4c 55 53 49 56 45 5d 20 66 6c 61 67 20 6d 65 61 LUSIVE] flag mea
b980: 6e 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 ns the file shou
b990: 6c 64 20 62 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 ld be opened.**
b9a0: 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 for exclusive ac
b9b0: 63 65 73 73 2e 20 20 54 68 69 73 20 66 6c 61 67 cess. This flag
b9c0: 20 69 73 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 is set for all
b9d0: 66 69 6c 65 73 20 65 78 63 65 70 74 0a 2a 2a 20 files except.**
b9e0: 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 for the main dat
b9f0: 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
ba00: 2a 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 * At least szOsF
ba10: 69 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d ile bytes of mem
ba20: 6f 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 ory are allocate
ba30: 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 d by SQLite.** t
ba40: 6f 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c o hold the [sql
ba50: 69 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 ite3_file] struc
ba60: 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 ture passed as t
ba70: 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 he third.** argu
ba80: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 ment to xOpen.
ba90: 54 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 The xOpen method
baa0: 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 does not have t
bab0: 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 o.** allocate th
bac0: 65 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 e structure; it
bad0: 73 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c should just fill
bae0: 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 it in..**.** Th
baf0: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 e flags argument
bb00: 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 6d 61 to xAccess() ma
bb10: 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 y be [SQLITE_ACC
bb20: 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74 ESS_EXISTS].** t
bb30: 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 65 o test for the e
bb40: 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 66 69 xistence of a fi
bb50: 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 le, or [SQLITE_A
bb60: 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 5d CCESS_READWRITE]
bb70: 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65 74 to.** test whet
bb80: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 72 65 her a file is re
bb90: 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 adable and writa
bba0: 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f ble, or [SQLITE_
bbb0: 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 ACCESS_READ].**
bbc0: 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 72 20 to test whether
bbd0: 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c 65 61 a file is at lea
bbe0: 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 20 54 st readable. T
bbf0: 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 61 he file can be a
bc00: 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a .** directory..*
bc10: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c *.** SQLite will
bc20: 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 always allocate
bc30: 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68 at least mxPath
bc40: 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72 name+1 bytes for
bc50: 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 the.** output b
bc60: 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e uffer xFullPathn
bc70: 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74 20 ame. The exact
bc80: 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 size of the outp
bc90: 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 ut buffer.** is
bca0: 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20 61 also passed as a
bcb0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 6f parameter to bo
bcc0: 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 th methods. If
bcd0: 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
bce0: 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 r.** is not larg
bcf0: 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 e enough, [SQLIT
bd00: 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 E_CANTOPEN] shou
bd10: 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ld be returned.
bd20: 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a Since this is.**
bd30: 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61 handled as a fa
bd40: 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c tal error by SQL
bd50: 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 ite, vfs impleme
bd60: 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ntations should
bd70: 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 endeavor.** to p
bd80: 72 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 revent this by s
bd90: 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d etting mxPathnam
bda0: 65 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e e to a sufficien
bdb0: 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e tly large value.
bdc0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 .**.** The xRand
bdd0: 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70 omness(), xSleep
bde0: 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74 (), and xCurrent
bdf0: 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63 65 Time() interface
be00: 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74 72 s.** are not str
be10: 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20 ictly a part of
be20: 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 the filesystem,
be30: 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 but they are.**
be40: 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 included in the
be50: 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f VFS structure fo
be60: 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a r completeness..
be70: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 ** The xRandomne
be80: 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 74 ss() function at
be90: 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72 6e tempts to return
bea0: 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a 2a nBytes bytes.**
beb0: 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79 of good-quality
bec0: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f randomness into
bed0: 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74 75 zOut. The retu
bee0: 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 rn value is.** t
bef0: 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65 72 he actual number
bf00: 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e of bytes of ran
bf10: 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 domness obtained
bf20: 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70 28 ..** The xSleep(
bf30: 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73 20 ) method causes
bf40: 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 the calling thre
bf50: 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72 20 ad to sleep for
bf60: 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65 20 at.** least the
bf70: 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 number of micros
bf80: 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20 54 econds given. T
bf90: 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28 he xCurrentTime(
bfa0: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 ).** method retu
bfb0: 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 rns a Julian Day
bfc0: 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 Number for the
bfd0: 63 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e 64 current date and
bfe0: 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 time..**.*/.typ
bff0: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
c000: 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33 5f te3_vfs sqlite3_
c010: 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 vfs;.struct sqli
c020: 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20 te3_vfs {. int
c030: 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20 iVersion;
c040: 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 /* Structur
c050: 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 e version number
c060: 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 */. int szOsFi
c070: 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f le; /
c080: 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 * Size of subcla
c090: 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c ssed sqlite3_fil
c0a0: 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 e */. int mxPat
c0b0: 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 hname;
c0c0: 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 /* Maximum file
c0d0: 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 pathname length
c0e0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 */. sqlite3_vfs
c0f0: 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a *pNext; /*
c100: 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65 64 Next registered
c110: 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 VFS */. const
c120: 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
c130: 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
c140: 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 is virtual file
c150: 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 system */. void
c160: 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 *pAppData;
c170: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
c180: 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 to application-s
c190: 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a pecific data */.
c1a0: 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 int (*xOpen)(s
c1b0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
c1c0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
c1d0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 sqlite3_file*,.
c1e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
c1f0: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f t flags, int *pO
c200: 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 utFlags);. int
c210: 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74 (*xDelete)(sqlit
c220: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
c230: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 har *zName, int
c240: 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 syncDir);. int
c250: 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69 74 (*xAccess)(sqlit
c260: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
c270: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 har *zName, int
c280: 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 flags, int *pRes
c290: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 Out);. int (*xF
c2a0: 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c ullPathname)(sql
c2b0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
c2c0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
c2d0: 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f t nOut, char *zO
c2e0: 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 ut);. void *(*x
c2f0: 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f DlOpen)(sqlite3_
c300: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 vfs*, const char
c310: 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 *zFilename);.
c320: 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 void (*xDlError)
c330: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 (sqlite3_vfs*, i
c340: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a nt nByte, char *
c350: 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 zErrMsg);. void
c360: 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c (*(*xDlSym)(sql
c370: 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c ite3_vfs*,void*,
c380: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 const char *zSy
c390: 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20 mbol))(void);.
c3a0: 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 void (*xDlClose)
c3b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 (sqlite3_vfs*, v
c3c0: 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 oid*);. int (*x
c3d0: 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 Randomness)(sqli
c3e0: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 te3_vfs*, int nB
c3f0: 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 yte, char *zOut)
c400: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 ;. int (*xSleep
c410: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 )(sqlite3_vfs*,
c420: 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 int microseconds
c430: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72 );. int (*xCurr
c440: 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65 33 entTime)(sqlite3
c450: 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b _vfs*, double*);
c460: 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c 61 73 . int (*xGetLas
c470: 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f tError)(sqlite3_
c480: 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 vfs*, int, char
c490: 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 69 65 *);. /* New fie
c4a0: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e lds may be appen
c4b0: 64 65 64 20 69 6e 20 66 69 67 75 72 65 20 76 65 ded in figure ve
c4c0: 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69 56 65 rsions. The iVe
c4d0: 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 rsion. ** value
c4e0: 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20 will increment
c4f0: 77 68 65 6e 65 76 65 72 20 74 68 69 73 20 68 61 whenever this ha
c500: 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ppens. */.};../*
c510: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c .** CAPI3REF: Fl
c520: 61 67 73 20 66 6f 72 20 74 68 65 20 78 41 63 63 ags for the xAcc
c530: 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64 20 7b ess VFS method {
c540: 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34 30 3e H11190} <H11140>
c550: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 .**.** These int
c560: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 eger constants c
c570: 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 an be used as th
c580: 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
c590: 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 r to.** the xAcc
c5a0: 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e ess method of an
c5b0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
c5c0: 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 bject. {END} Th
c5d0: 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 ey determine.**
c5e0: 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 what kind of per
c5f0: 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41 63 missions the xAc
c600: 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20 6c cess method is l
c610: 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 ooking for..** W
c620: 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ith SQLITE_ACCES
c630: 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78 41 S_EXISTS, the xA
c640: 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 ccess method.**
c650: 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 77 68 simply checks wh
c660: 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 65 ether the file e
c670: 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68 20 53 xists..** With S
c680: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
c690: 44 57 52 49 54 45 2c 20 74 68 65 20 78 41 63 63 DWRITE, the xAcc
c6a0: 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 ess method.** ch
c6b0: 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 ecks whether the
c6c0: 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72 65 file is both re
c6d0: 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 adable and writa
c6e0: 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c ble..** With SQL
c6f0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 2c ITE_ACCESS_READ,
c700: 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 the xAccess met
c710: 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 77 68 hod.** checks wh
c720: 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 69 ether the file i
c730: 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 s readable..*/.#
c740: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 define SQLITE_AC
c750: 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 20 30 CESS_EXISTS 0
c760: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
c770: 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 ACCESS_READWRITE
c780: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
c790: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 20 20 E_ACCESS_READ
c7a0: 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 2../*.** CAPI
c7b0: 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69 7a 65 3REF: Initialize
c7c0: 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 The SQLite Libr
c7d0: 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c 53 32 ary {H10130} <S2
c7e0: 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0000><S30100>.**
c7f0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
c800: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 initialize() rou
c810: 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a 65 73 tine initializes
c820: 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c the.** SQLite l
c830: 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c ibrary. The sql
c840: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
c850: 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c routine.** deall
c860: 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73 6f 75 ocates any resou
c870: 72 63 65 73 20 74 68 61 74 20 77 65 72 65 20 61 rces that were a
c880: 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 llocated by sqli
c890: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
c8a0: 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 ..**.** A call t
c8b0: 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 o sqlite3_initia
c8c0: 6c 69 7a 65 28 29 20 69 73 20 61 6e 20 22 65 66 lize() is an "ef
c8d0: 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69 66 fective" call if
c8e0: 20 69 74 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 it is.** the fi
c8f0: 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 rst time sqlite3
c900: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 _initialize() is
c910: 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 20 invoked during
c920: 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a the lifetime of.
c930: 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 2c 20 ** the process,
c940: 6f 72 20 69 66 20 69 74 20 69 73 20 74 68 65 20 or if it is the
c950: 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 first time sqlit
c960: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
c970: 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f is invoked.** fo
c980: 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 llowing a call t
c990: 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f o sqlite3_shutdo
c9a0: 77 6e 28 29 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 wn(). Only an e
c9b0: 66 66 65 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a ffective call.**
c9c0: 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 of sqlite3_init
c9d0: 69 61 6c 69 7a 65 28 29 20 64 6f 65 73 20 61 6e ialize() does an
c9e0: 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e y initialization
c9f0: 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c . All other cal
ca00: 6c 73 0a 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65 ls.** are harmle
ca10: 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a ss no-ops..**.**
ca20: 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 A call to sqlit
ca30: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 69 73 e3_shutdown() is
ca40: 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 20 an "effective"
ca50: 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20 74 68 call if it is th
ca60: 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 e first.** call
ca70: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 to sqlite3_shutd
ca80: 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68 65 20 own() since the
ca90: 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 last sqlite3_ini
caa0: 74 69 61 6c 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 tialize(). Only
cab0: 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69 76 65 .** an effective
cac0: 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
cad0: 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f 65 73 _shutdown() does
cae0: 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c 69 7a any deinitializ
caf0: 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 ation..** All ot
cb00: 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c her calls to sql
cb10: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
cb20: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d are harmless no-
cb30: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 ops..**.** Among
cb40: 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c 20 73 other things, s
cb50: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
cb60: 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 e() shall invoke
cb70: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 .** sqlite3_os_i
cb80: 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c nit(). Similarl
cb90: 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 y, sqlite3_shutd
cba0: 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 own().** shall i
cbb0: 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73 nvoke sqlite3_os
cbc0: 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 _end()..**.** Th
cbd0: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 e sqlite3_initia
cbe0: 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 72 lize() routine r
cbf0: 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f eturns [SQLITE_O
cc00: 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a K] on success..*
cc10: 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20 72 65 * If for some re
cc20: 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e ason, sqlite3_in
cc30: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 75 6e itialize() is un
cc40: 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 able to initiali
cc50: 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 ze.** the librar
cc60: 79 20 28 70 65 72 68 61 70 73 20 69 74 20 69 73 y (perhaps it is
cc70: 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 unable to alloc
cc80: 61 74 65 20 61 20 6e 65 65 64 65 64 20 72 65 73 ate a needed res
cc90: 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20 61 73 ource such.** as
cca0: 20 61 20 6d 75 74 65 78 29 20 69 74 20 72 65 74 a mutex) it ret
ccb0: 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 urns an [error c
ccc0: 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 ode] other than
ccd0: 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a [SQLITE_OK]..**.
cce0: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 ** The sqlite3_i
ccf0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 nitialize() rout
cd00: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e ine is called in
cd10: 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79 ternally by many
cd20: 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 other.** SQLite
cd30: 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f 20 74 interfaces so t
cd40: 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69 hat an applicati
cd50: 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 73 20 on usually does
cd60: 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 not need to.** i
cd70: 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e nvoke sqlite3_in
cd80: 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 itialize() direc
cd90: 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c tly. For exampl
cda0: 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e e, [sqlite3_open
cdb0: 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c ()].** calls sql
cdc0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
cdd0: 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74 65 20 ) so the SQLite
cde0: 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62 65 20 library will be
cdf0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a automatically.**
ce00: 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 initialized whe
ce10: 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 n [sqlite3_open(
ce20: 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 )] is called if
ce30: 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20 69 6e it has not be in
ce40: 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 itialized.** alr
ce50: 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 eady. However,
ce60: 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d if SQLite is com
ce70: 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 5b piled with the [
ce80: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
ce90: 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 INIT].** compile
cea0: 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68 -time option, th
ceb0: 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 en the automatic
cec0: 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
ced0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 3_initialize().*
cee0: 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20 61 6e * are omitted an
cef0: 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f d the applicatio
cf00: 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 n must call sqli
cf10: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
cf20: 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69 directly.** pri
cf30: 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 or to using any
cf40: 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 other SQLite int
cf50: 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d 61 78 erface. For max
cf60: 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69 74 79 imum portability
cf70: 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63 6f 6d ,.** it is recom
cf80: 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c mended that appl
cf90: 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79 73 20 ications always
cfa0: 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 invoke sqlite3_i
cfb0: 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 nitialize().** d
cfc0: 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20 74 6f irectly prior to
cfd0: 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 using any other
cfe0: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
cff0: 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c 65 61 e. Future relea
d000: 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 ses.** of SQLite
d010: 20 6d 61 79 20 72 65 71 75 69 72 65 20 74 68 69 may require thi
d020: 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 s. In other wor
d030: 64 73 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 ds, the behavior
d040: 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20 77 68 exhibited.** wh
d050: 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d en SQLite is com
d060: 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 4c 49 piled with [SQLI
d070: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
d080: 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 74 ] might become t
d090: 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 he.** default be
d0a0: 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 havior in some f
d0b0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 20 6f 66 uture release of
d0c0: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 SQLite..**.** T
d0d0: 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e he sqlite3_os_in
d0e0: 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 it() routine doe
d0f0: 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 s operating-syst
d100: 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 69 em specific.** i
d110: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
d120: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
d130: 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ary. The sqlite
d140: 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 3_os_end().** ro
d150: 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 utine undoes the
d160: 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c 69 74 effect of sqlit
d170: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54 e3_os_init(). T
d180: 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 ypical tasks.**
d190: 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65 performed by the
d1a0: 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c se routines incl
d1b0: 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f ude allocation o
d1c0: 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a r deallocation.*
d1d0: 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65 73 6f * of static reso
d1e0: 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a urces, initializ
d1f0: 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 ation of global
d200: 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 variables,.** se
d210: 74 74 69 6e 67 20 75 70 20 61 20 64 65 66 61 75 tting up a defau
d220: 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d lt [sqlite3_vfs]
d230: 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65 74 74 module, or sett
d240: 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65 66 61 ing up.** a defa
d250: 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ult configuratio
d260: 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 n using [sqlite3
d270: 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a _config()]..**.*
d280: 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
d290: 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 69 n should never i
d2a0: 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73 71 6c nvoke either sql
d2b0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a ite3_os_init().*
d2c0: 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f * or sqlite3_os_
d2d0: 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79 2e 20 end() directly.
d2e0: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
d2f0: 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 should only inv
d300: 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 oke.** sqlite3_i
d310: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 20 nitialize() and
d320: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
d330: 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 (). The sqlite3
d340: 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e _os_init().** in
d350: 74 65 72 66 61 63 65 20 69 73 20 63 61 6c 6c 65 terface is calle
d360: 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 d automatically
d370: 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 by sqlite3_initi
d380: 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73 alize() and.** s
d390: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 qlite3_os_end()
d3a0: 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c is called by sql
d3b0: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e ite3_shutdown().
d3c0: 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a Appropriate.**
d3d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
d3e0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f for sqlite3_os_
d3f0: 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 init() and sqlit
d400: 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 e3_os_end().** a
d410: 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 re built into SQ
d420: 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69 73 20 Lite when it is
d430: 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75 6e 69 compiled for uni
d440: 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72 20 6f x, windows, or o
d450: 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 75 69 s/2..** When bui
d460: 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61 lt for other pla
d470: 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20 74 68 tforms (using th
d480: 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 e [SQLITE_OS_OTH
d490: 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 ER=1] compile-ti
d4a0: 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 me.** option) th
d4b0: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
d4c0: 73 74 20 73 75 70 70 6c 79 20 61 20 73 75 69 74 st supply a suit
d4d0: 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
d4e0: 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 ion for.** sqlit
d4f0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
d500: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
d510: 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 ). An applicati
d520: 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 on-supplied.** i
d530: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
d540: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
d550: 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 () or sqlite3_os
d560: 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 _end().** must r
d570: 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b eturn [SQLITE_OK
d580: 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 ] on success and
d590: 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 some other [err
d5a0: 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a or code] upon.**
d5b0: 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c failure..*/.SQL
d5c0: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
d5d0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 te3_initialize(v
d5e0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 oid);.SQLITE_API
d5f0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75 int sqlite3_shu
d600: 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c tdown(void);.SQL
d610: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
d620: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
d630: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
d640: 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 t sqlite3_os_end
d650: 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 (void);../*.** C
d660: 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 API3REF: Configu
d670: 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74 65 20 ring The SQLite
d680: 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30 30 7d Library {H14100}
d690: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32 30 30 <S20000><S30200
d6a0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
d6b0: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 L.**.** The sqli
d6c0: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 te3_config() int
d6d0: 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
d6e0: 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f o make global co
d6f0: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 nfiguration.** c
d700: 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 74 65 hanges to SQLite
d710: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 75 6e in order to tun
d720: 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65 20 e SQLite to the
d730: 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 20 6f specific needs o
d740: 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 f.** the applica
d750: 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 tion. The defau
d760: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e lt configuration
d770: 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 is recommended
d780: 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c for most.** appl
d790: 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 ications and so
d7a0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
d7b0: 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 usually not nece
d7c0: 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a ssary. It is.**
d7d0: 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 75 70 provided to sup
d7e0: 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c 69 63 port rare applic
d7f0: 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 ations with unus
d800: 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a ual needs..**.**
d810: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e The sqlite3_con
d820: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
d830: 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 is not threadsaf
d840: 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 e. The applicat
d850: 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 ion.** must insu
d860: 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 re that no other
d870: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
d880: 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 es are invoked b
d890: 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 y other.** threa
d8a0: 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33 ds while sqlite3
d8b0: 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 75 6e _config() is run
d8c0: 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 6d 6f ning. Furthermo
d8d0: 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 re, sqlite3_conf
d8e0: 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 ig().** may only
d8f0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f be invoked prio
d900: 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 r to library ini
d910: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e tialization usin
d920: 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e g.** [sqlite3_in
d930: 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 itialize()] or a
d940: 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 62 79 fter shutdown by
d950: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f [sqlite3_shutdo
d960: 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 wn()]..** Note,
d970: 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20 73 71 however, that sq
d980: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 lite3_config() c
d990: 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 an be called as
d9a0: 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 part of the.** i
d9b0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
d9c0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
d9d0: 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 defined [sqlite3
d9e0: 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a _os_init()]..**.
d9f0: 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
da00: 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
da10: 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 6e 20 _config() is an
da20: 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 integer.** [SQLI
da30: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
da40: 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 THREAD | configu
da50: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 ration option] t
da60: 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a hat determines.*
da70: 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74 79 20 * what property
da80: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74 6f 20 of SQLite is to
da90: 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 be configured.
daa0: 53 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d Subsequent argum
dab0: 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 ents.** vary dep
dac0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 ending on the [S
dad0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e QLITE_CONFIG_SIN
dae0: 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 GLETHREAD | conf
daf0: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
db00: 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 ].** in the firs
db10: 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
db20: 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 * When a configu
db30: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 ration option is
db40: 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f set, sqlite3_co
db50: 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b nfig() returns [
db60: 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 SQLITE_OK]..** I
db70: 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 f the option is
db80: 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 unknown or SQLit
db90: 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 e is unable to s
dba0: 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a et the option.**
dbb0: 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
dbc0: 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e ne returns a non
dbd0: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 -zero [error cod
dbe0: 65 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 e]..**.** Requir
dbf0: 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 ements:.** [H141
dc00: 30 33 5d 20 5b 48 31 34 31 30 36 5d 20 5b 48 31 03] [H14106] [H1
dc10: 34 31 32 30 5d 20 5b 48 31 34 31 32 33 5d 20 5b 4120] [H14123] [
dc20: 48 31 34 31 32 36 5d 20 5b 48 31 34 31 32 39 5d H14126] [H14129]
dc30: 20 5b 48 31 34 31 33 32 5d 20 5b 48 31 34 31 33 [H14132] [H1413
dc40: 35 5d 0a 2a 2a 20 5b 48 31 34 31 33 38 5d 20 5b 5].** [H14138] [
dc50: 48 31 34 31 34 31 5d 20 5b 48 31 34 31 34 34 5d H14141] [H14144]
dc60: 20 5b 48 31 34 31 34 37 5d 20 5b 48 31 34 31 35 [H14147] [H1415
dc70: 30 5d 20 5b 48 31 34 31 35 33 5d 20 5b 48 31 34 0] [H14153] [H14
dc80: 31 35 36 5d 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 156] [H14159].**
dc90: 20 5b 48 31 34 31 36 32 5d 20 5b 48 31 34 31 36 [H14162] [H1416
dca0: 35 5d 20 5b 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 5] [H14168].*/.S
dcb0: 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
dcc0: 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
dcd0: 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 t sqlite3_config
dce0: 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a (int, ...);../*.
dcf0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
dd00: 66 69 67 75 72 65 20 64 61 74 61 62 61 73 65 20 figure database
dd10: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20 7b 48 31 connections {H1
dd20: 34 32 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 4200} <S20000>.*
dd30: 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
dd40: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
dd50: 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 _db_config() int
dd60: 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
dd70: 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75 72 61 o make configura
dd80: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 tion.** changes
dd90: 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 to a [database c
dda0: 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 onnection]. The
ddb0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 73 69 interface is si
ddc0: 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c milar to.** [sql
ddd0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65 ite3_config()] e
dde0: 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 63 xcept that the c
ddf0: 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74 6f 20 hanges apply to
de00: 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 a single.** [dat
de10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
de20: 5d 20 28 73 70 65 63 69 66 69 65 64 20 69 6e 20 ] (specified in
de30: 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
de40: 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c nt). The.** sql
de50: 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
de60: 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 6f interface can o
de70: 6e 6c 79 20 62 65 20 75 73 65 64 20 69 6d 6d 65 nly be used imme
de80: 64 69 61 74 65 6c 79 20 61 66 74 65 72 0a 2a 2a diately after.**
de90: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
dea0: 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 72 65 61 nnection is crea
deb0: 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ted using [sqlit
dec0: 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b e3_open()],.** [
ded0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
dee0: 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f ], or [sqlite3_o
def0: 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a pen_v2()]. .**.
df00: 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ** The second ar
df10: 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 gument to sqlite
df20: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 3_db_config(D,V,
df30: 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a 2a 2a 20 ...) is the.**
df40: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 76 65 configuration ve
df50: 72 62 20 2d 20 61 6e 20 69 6e 74 65 67 65 72 20 rb - an integer
df60: 63 6f 64 65 20 74 68 61 74 20 69 6e 64 69 63 61 code that indica
df70: 74 65 73 20 77 68 61 74 0a 2a 2a 20 61 73 70 65 tes what.** aspe
df80: 63 74 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 ct of the [datab
df90: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
dfa0: 69 73 20 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 is being configu
dfb0: 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 red..** The only
dfc0: 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68 69 73 choice for this
dfd0: 20 76 61 6c 75 65 20 69 73 20 5b 53 51 4c 49 54 value is [SQLIT
dfe0: 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 E_DBCONFIG_LOOKA
dff0: 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 SIDE]..** New ve
e000: 72 62 73 20 61 72 65 20 6c 69 6b 65 6c 79 20 74 rbs are likely t
e010: 6f 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 o be added in fu
e020: 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 ture releases of
e030: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41 64 64 69 SQLite..** Addi
e040: 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 tional arguments
e050: 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 76 depend on the v
e060: 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 erb..**.** Requi
e070: 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 rements:.** [H14
e080: 32 30 33 5d 20 5b 48 31 34 32 30 36 5d 20 5b 48 203] [H14206] [H
e090: 31 34 32 30 39 5d 20 5b 48 31 34 32 31 32 5d 20 14209] [H14212]
e0a0: 5b 48 31 34 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 [H14215].*/.SQLI
e0b0: 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
e0c0: 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
e0d0: 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
e0e0: 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f (sqlite3*, int o
e0f0: 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 p, ...);../*.**
e100: 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 CAPI3REF: Memory
e110: 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 52 6f 75 74 Allocation Rout
e120: 69 6e 65 73 20 7b 48 31 30 31 35 35 7d 20 3c 53 ines {H10155} <S
e130: 32 30 31 32 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20120>.** EXPERI
e140: 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 MENTAL.**.** An
e150: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
e160: 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 object defines
e170: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 the interface be
e180: 74 77 65 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 tween SQLite.**
e190: 61 6e 64 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 and low-level me
e1a0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
e1b0: 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 routines..**.**
e1c0: 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 75 This object is u
e1d0: 73 65 64 20 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 sed in only one
e1e0: 70 6c 61 63 65 20 69 6e 20 74 68 65 20 53 51 4c place in the SQL
e1f0: 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a ite interface..*
e200: 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * A pointer to a
e210: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
e220: 69 73 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 is object is the
e230: 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
e240: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
e250: 29 5d 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 66 )] when the conf
e260: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
e270: 20 69 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 is.** [SQLITE_C
e280: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 ONFIG_MALLOC].
e290: 42 79 20 63 72 65 61 74 69 6e 67 20 61 6e 20 69 By creating an i
e2a0: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
e2b0: 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 70 61 object.** and pa
e2c0: 73 73 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c ssing it to [sql
e2d0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 64 ite3_config()] d
e2e0: 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 74 uring configurat
e2f0: 69 6f 6e 2c 20 61 6e 0a 2a 2a 20 61 70 70 6c 69 ion, an.** appli
e300: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 cation can speci
e310: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 fy an alternativ
e320: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
e330: 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a ion subsystem.**
e340: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 for SQLite to u
e350: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 se for all of it
e360: 73 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 s dynamic memory
e370: 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f needs..**.** No
e380: 74 65 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 te that SQLite c
e390: 6f 6d 65 73 20 77 69 74 68 20 61 20 62 75 69 6c omes with a buil
e3a0: 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f t-in memory allo
e3b0: 63 61 74 6f 72 20 74 68 61 74 20 69 73 0a 2a 2a cator that is.**
e3c0: 20 70 65 72 66 65 63 74 6c 79 20 61 64 65 71 75 perfectly adequ
e3d0: 61 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65 72 ate for the over
e3e0: 77 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 whelming majorit
e3f0: 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e y of application
e400: 73 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 s.** and that th
e410: 69 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c is object is onl
e420: 79 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74 69 y useful to a ti
e430: 6e 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 ny minority of a
e440: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 pplications.** w
e450: 69 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64 20 ith specialized
e460: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
e470: 6e 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 n requirements.
e480: 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 0a This object is.
e490: 2a 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75 72 ** also used dur
e4a0: 69 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20 53 ing testing of S
e4b0: 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20 74 QLite in order t
e4c0: 6f 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c 74 o specify an alt
e4d0: 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f ernative.** memo
e4e0: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 ry allocator tha
e4f0: 74 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f t simulates memo
e500: 72 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 ry out-of-memory
e510: 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a conditions in.*
e520: 2a 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 * order to verif
e530: 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 72 65 y that SQLite re
e540: 63 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c 6c covers gracefull
e550: 79 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 y from such.** c
e560: 6f 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a onditions..**.**
e570: 20 54 68 65 20 78 4d 61 6c 6c 6f 63 2c 20 78 46 The xMalloc, xF
e580: 72 65 65 2c 20 61 6e 64 20 78 52 65 61 6c 6c 6f ree, and xReallo
e590: 63 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 77 c methods must w
e5a0: 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 ork like the.**
e5b0: 6d 61 6c 6c 6f 63 28 29 2c 20 66 72 65 65 28 29 malloc(), free()
e5c0: 2c 20 61 6e 64 20 72 65 61 6c 6c 6f 63 28 29 20 , and realloc()
e5d0: 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 functions from t
e5e0: 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 he standard libr
e5f0: 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 ary..**.** xSize
e600: 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 should return t
e610: 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a he allocated siz
e620: 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c e of a memory al
e630: 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 location.** prev
e640: 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 iously obtained
e650: 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 from xMalloc or
e660: 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 61 xRealloc. The a
e670: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a 2a llocated size.**
e680: 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 is always at le
e690: 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74 68 ast as big as th
e6a0: 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 e requested size
e6b0: 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67 but may be larg
e6c0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 er..**.** The xR
e6d0: 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 65 oundup method re
e6e0: 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c 64 turns what would
e6f0: 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 be the allocate
e700: 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d d size of.** a m
e710: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
e720: 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 75 given a particu
e730: 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73 69 lar requested si
e740: 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 ze. Most memory
e750: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 .** allocators r
e760: 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 61 ound up memory a
e770: 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c 65 llocations at le
e780: 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ast to the next
e790: 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 multiple.** of 8
e7a0: 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f . Some allocato
e7b0: 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 61 rs round up to a
e7c0: 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 larger multiple
e7d0: 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 6f or to a power o
e7e0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 f 2..**.** The x
e7f0: 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 Init method init
e800: 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f ializes the memo
e810: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 ry allocator. (
e820: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 For example,.**
e830: 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 it might allocat
e840: 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 e any require mu
e850: 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c texes or initial
e860: 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 ize internal dat
e870: 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e a.** structures.
e880: 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 The xShutdown
e890: 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 method is invoke
e8a0: 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 d (indirectly) b
e8b0: 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 y.** [sqlite3_sh
e8c0: 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 utdown()] and sh
e8d0: 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 ould deallocate
e8e0: 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 any resources ac
e8f0: 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e quired.** by xIn
e900: 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 it. The pAppDat
e910: 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 a pointer is use
e920: 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 d as the only pa
e930: 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 rameter to.** xI
e940: 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 nit and xShutdow
e950: 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
e960: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d ruct sqlite3_mem
e970: 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 _methods sqlite3
e980: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 _mem_methods;.st
e990: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d ruct sqlite3_mem
e9a0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 _methods {. voi
e9b0: 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e d *(*xMalloc)(in
e9c0: 74 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d t); /* M
e9d0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
e9e0: 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
e9f0: 6f 69 64 20 28 2a 78 46 72 65 65 29 28 76 6f 69 oid (*xFree)(voi
ea00: 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a d*); /*
ea10: 20 46 72 65 65 20 61 20 70 72 69 6f 72 20 61 6c Free a prior al
ea20: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f location */. vo
ea30: 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f 63 29 28 id *(*xRealloc)(
ea40: 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 void*,int); /*
ea50: 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 Resize an alloca
ea60: 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a tion */. int (*
ea70: 78 53 69 7a 65 29 28 76 6f 69 64 2a 29 3b 20 20 xSize)(void*);
ea80: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
ea90: 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
eaa0: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a n allocation */.
eab0: 20 20 69 6e 74 20 28 2a 78 52 6f 75 6e 64 75 70 int (*xRoundup
eac0: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20 )(int);
ead0: 20 2f 2a 20 52 6f 75 6e 64 20 75 70 20 72 65 71 /* Round up req
eae0: 75 65 73 74 20 73 69 7a 65 20 74 6f 20 61 6c 6c uest size to all
eaf0: 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a ocation size */.
eb00: 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 int (*xInit)(v
eb10: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 oid*);
eb20: 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
eb30: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
eb40: 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a tor */. void (*
eb50: 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a xShutdown)(void*
eb60: 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 69 6e 69 ); /* Deini
eb70: 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
eb80: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a ry allocator */.
eb90: 20 20 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 void *pAppData
eba0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ebb0: 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
ebc0: 78 49 6e 69 74 28 29 20 61 6e 64 20 78 53 68 75 xInit() and xShu
ebd0: 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f tdown() */.};../
ebe0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
ebf0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 onfiguration Opt
ec00: 69 6f 6e 73 20 7b 48 31 30 31 36 30 7d 20 3c 53 ions {H10160} <S
ec10: 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20000>.** EXPERI
ec20: 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
ec30: 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 se constants are
ec40: 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 the available i
ec50: 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 nteger configura
ec60: 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 tion options tha
ec70: 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 t.** can be pass
ec80: 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
ec90: 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
eca0: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
ecb0: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a )] interface..**
ecc0: 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67 75 72 .** New configur
ecd0: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 ation options ma
ece0: 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 y be added in fu
ecf0: 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 ture releases of
ed00: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73 SQLite..** Exis
ed10: 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 74 69 ting configurati
ed20: 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74 on options might
ed30: 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 be discontinued
ed40: 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a . Applications.
ed50: 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 ** should check
ed60: 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 the return code
ed70: 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f from [sqlite3_co
ed80: 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 nfig()] to make
ed90: 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 sure that.** the
eda0: 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54 call worked. T
edb0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 he [sqlite3_conf
edc0: 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ig()] interface
edd0: 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a will return a.**
ede0: 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 non-zero [error
edf0: 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 63 code] if a disc
ee00: 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 ontinued or unsu
ee10: 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 72 pported configur
ee20: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 ation option.**
ee30: 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a is invoked..**.*
ee40: 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
ee50: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
ee60: 4c 45 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a LETHREAD</dt>.**
ee70: 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e <dd>There are n
ee80: 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 o arguments to t
ee90: 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 his option. Thi
eea0: 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c 65 s option disable
eeb0: 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 69 6e s.** all mutexin
eec0: 67 20 61 6e 64 20 70 75 74 73 20 53 51 4c 69 74 g and puts SQLit
eed0: 65 20 69 6e 74 6f 20 61 20 6d 6f 64 65 20 77 68 e into a mode wh
eee0: 65 72 65 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20 ere it can only
eef0: 62 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 61 20 be used.** by a
ef00: 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 3c 2f single thread.</
ef10: 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
ef20: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 LITE_CONFIG_MULT
ef30: 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 ITHREAD</dt>.**
ef40: 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f <dd>There are no
ef50: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 arguments to th
ef60: 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 is option. This
ef70: 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 option disables
ef80: 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 20 6f 6e 20 .** mutexing on
ef90: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
efa0: 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 tion] and [prepa
efb0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f red statement] o
efc0: 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 65 20 61 bjects..** The a
efd0: 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 72 65 pplication is re
efe0: 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 sponsible for se
eff0: 72 69 61 6c 69 7a 69 6e 67 20 61 63 63 65 73 73 rializing access
f000: 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 to.** [database
f010: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e connections] an
f020: 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 d [prepared stat
f030: 65 6d 65 6e 74 73 5d 2e 20 20 42 75 74 20 6f 74 ements]. But ot
f040: 68 65 72 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 her mutexes.** a
f050: 72 65 20 65 6e 61 62 6c 65 64 20 73 6f 20 74 68 re enabled so th
f060: 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 at SQLite will b
f070: 65 20 73 61 66 65 20 74 6f 20 75 73 65 20 69 6e e safe to use in
f080: 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 a multi-threade
f090: 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 d.** environment
f0a0: 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 as long as no t
f0b0: 77 6f 20 74 68 72 65 61 64 73 20 61 74 74 65 6d wo threads attem
f0c0: 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 pt to use the sa
f0d0: 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 me.** [database
f0e0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 74 20 74 connection] at t
f0f0: 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 53 he same time. S
f100: 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e ee the [threadin
f110: 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d g mode].** docum
f120: 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 entation for add
f130: 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
f140: 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ion.</dd>.**.**
f150: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
f160: 47 5f 53 45 52 49 41 4c 49 5a 45 44 3c 2f 64 74 G_SERIALIZED</dt
f170: 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 >.** <dd>There a
f180: 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 re no arguments
f190: 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 to this option.
f1a0: 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 65 6e 61 This option ena
f1b0: 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 bles.** all mute
f1c0: 78 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 xes including th
f1d0: 65 20 72 65 63 75 72 73 69 76 65 0a 2a 2a 20 6d e recursive.** m
f1e0: 75 74 65 78 65 73 20 6f 6e 20 5b 64 61 74 61 62 utexes on [datab
f1f0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
f200: 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 and [prepared st
f210: 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 atement] objects
f220: 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 ..** In this mod
f230: 65 20 28 77 68 69 63 68 20 69 73 20 74 68 65 20 e (which is the
f240: 64 65 66 61 75 6c 74 20 77 68 65 6e 20 53 51 4c default when SQL
f250: 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ite is compiled
f260: 77 69 74 68 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f with.** [SQLITE_
f270: 54 48 52 45 41 44 53 41 46 45 3d 31 5d 29 20 74 THREADSAFE=1]) t
f280: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
f290: 79 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 73 65 y will itself se
f2a0: 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 0a 2a rialize access.*
f2b0: 2a 20 74 6f 20 5b 64 61 74 61 62 61 73 65 20 63 * to [database c
f2c0: 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 onnections] and
f2d0: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
f2e0: 65 6e 74 73 5d 20 73 6f 20 74 68 61 74 20 74 68 ents] so that th
f2f0: 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e e.** application
f300: 20 69 73 20 66 72 65 65 20 74 6f 20 75 73 65 20 is free to use
f310: 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 the same [databa
f320: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f se connection] o
f330: 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 r the.** same [p
f340: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
f350: 74 5d 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 t] in different
f360: 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73 threads at the s
f370: 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 ame time..** See
f380: 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 the [threading
f390: 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74 mode] documentat
f3a0: 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ion for addition
f3b0: 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c al information.<
f3c0: 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
f3d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
f3e0: 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e LOC</dt>.** <dd>
f3f0: 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
f400: 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d s a single argum
f410: 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70 ent which is a p
f420: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
f430: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
f440: 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 [sqlite3_mem_met
f450: 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e hods] structure.
f460: 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 The argument s
f470: 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 pecifies.** alte
f480: 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 rnative low-leve
f490: 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
f4a0: 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 ion routines to
f4b0: 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 be used in place
f4c0: 20 6f 66 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 of.** the memor
f4d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 y allocation rou
f4e0: 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f tines built into
f4f0: 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a SQLite.</dd>.**
f500: 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
f510: 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c ONFIG_GETMALLOC<
f520: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
f530: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
f540: 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
f550: 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
f560: 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
f570: 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
f580: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
f590: 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
f5a0: 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d e [sqlite3_mem_m
f5b0: 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 ethods].** struc
f5c0: 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 ture is filled w
f5d0: 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c ith the currentl
f5e0: 79 20 64 65 66 69 6e 65 64 20 6d 65 6d 6f 72 79 y defined memory
f5f0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
f600: 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 ines..** This op
f610: 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 tion can be used
f620: 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 to overload the
f630: 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 default memory
f640: 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f allocation.** ro
f650: 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77 72 utines with a wr
f660: 61 70 70 65 72 20 74 68 61 74 20 73 69 6d 75 6c apper that simul
f670: 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 79 20 61 6c ations memory al
f680: 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 location failure
f690: 20 6f 72 0a 2a 2a 20 74 72 61 63 6b 73 20 6d 65 or.** tracks me
f6a0: 6d 6f 72 79 20 75 73 61 67 65 2c 20 66 6f 72 20 mory usage, for
f6b0: 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a example.</dd>.**
f6c0: 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
f6d0: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 3c ONFIG_MEMSTATUS<
f6e0: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
f6f0: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 73 69 option takes si
f700: 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 6f 66 ngle argument of
f710: 20 74 79 70 65 20 69 6e 74 2c 20 69 6e 74 65 72 type int, inter
f720: 70 72 65 74 65 64 20 61 73 20 61 20 0a 2a 2a 20 preted as a .**
f730: 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 63 68 20 65 boolean, which e
f740: 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c nables or disabl
f750: 65 73 20 74 68 65 20 63 6f 6c 6c 65 63 74 69 6f es the collectio
f760: 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f n of memory allo
f770: 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 cation .** stati
f780: 73 74 69 63 73 2e 20 57 68 65 6e 20 64 69 73 61 stics. When disa
f790: 62 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 bled, the follow
f7a0: 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 74 65 72 ing SQLite inter
f7b0: 66 61 63 65 73 20 62 65 63 6f 6d 65 20 0a 2a 2a faces become .**
f7c0: 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 6f 6e 61 6c non-operational
f7d0: 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 :.** <ul>.**
f7e0: 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
f7f0: 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 0a 2a 2a emory_used()].**
f800: 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
f810: 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 _memory_highwate
f820: 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b r()].** <li> [
f830: 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 sqlite3_soft_hea
f840: 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 p_limit()].**
f850: 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74 <li> [sqlite3_st
f860: 61 74 75 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 atus()].** </u
f870: 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a l>.** </dd>.**.*
f880: 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
f890: 46 49 47 5f 53 43 52 41 54 43 48 3c 2f 64 74 3e FIG_SCRATCH</dt>
f8a0: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
f8b0: 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 ion specifies a
f8c0: 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 static memory bu
f8d0: 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 ffer that SQLite
f8e0: 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 can use for.**
f8f0: 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 2e 20 scratch memory.
f900: 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
f910: 20 61 72 67 75 6d 65 6e 74 73 3a 20 20 41 20 70 arguments: A p
f920: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
f930: 6d 6f 72 79 2c 20 74 68 65 0a 2a 2a 20 73 69 7a mory, the.** siz
f940: 65 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 e of each scratc
f950: 68 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61 h buffer (sz), a
f960: 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
f970: 20 62 75 66 66 65 72 73 20 28 4e 29 2e 20 20 54 buffers (N). T
f980: 68 65 20 73 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e he sz.** argumen
f990: 74 20 6d 75 73 74 20 62 65 20 61 20 6d 75 6c 74 t must be a mult
f9a0: 69 70 6c 65 20 6f 66 20 31 36 2e 20 54 68 65 20 iple of 16. The
f9b0: 73 7a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f sz parameter sho
f9c0: 75 6c 64 20 62 65 20 61 20 66 65 77 20 62 79 74 uld be a few byt
f9d0: 65 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 es.** larger tha
f9e0: 6e 20 74 68 65 20 61 63 74 75 61 6c 20 73 63 72 n the actual scr
f9f0: 61 74 63 68 20 73 70 61 63 65 20 72 65 71 75 69 atch space requi
fa00: 72 65 64 20 64 75 65 20 69 6e 74 65 72 6e 61 6c red due internal
fa10: 20 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 20 54 68 overhead..** Th
fa20: 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
fa30: 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 ent should point
fa40: 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f to an allocatio
fa50: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a n of at least sz
fa60: 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f *N bytes of memo
fa70: 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 ry..** SQLite wi
fa80: 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f 72 65 20 74 ll use no more t
fa90: 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 han one scratch
faa0: 62 75 66 66 65 72 20 61 74 20 6f 6e 63 65 20 70 buffer at once p
fab0: 65 72 20 74 68 72 65 61 64 2c 20 73 6f 0a 2a 2a er thread, so.**
fac0: 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 N should be set
fad0: 20 74 6f 20 74 68 65 20 65 78 70 65 63 74 65 64 to the expected
fae0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
faf0: 6f 66 20 74 68 72 65 61 64 73 2e 20 20 54 68 65 of threads. The
fb00: 20 73 7a 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 sz.** parameter
fb10: 20 73 68 6f 75 6c 64 20 62 65 20 36 20 74 69 6d should be 6 tim
fb20: 65 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 es the size of t
fb30: 68 65 20 6c 61 72 67 65 73 74 20 64 61 74 61 62 he largest datab
fb40: 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a ase page size..*
fb50: 2a 20 53 63 72 61 74 63 68 20 62 75 66 66 65 72 * Scratch buffer
fb60: 73 20 61 72 65 20 75 73 65 64 20 61 73 20 70 61 s are used as pa
fb70: 72 74 20 6f 66 20 74 68 65 20 62 74 72 65 65 20 rt of the btree
fb80: 62 61 6c 61 6e 63 65 20 6f 70 65 72 61 74 69 6f balance operatio
fb90: 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 62 74 n. If.** The bt
fba0: 72 65 65 20 62 61 6c 61 6e 63 65 72 20 6e 65 65 ree balancer nee
fbb0: 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 ds additional me
fbc0: 6d 6f 72 79 20 62 65 79 6f 6e 64 20 77 68 61 74 mory beyond what
fbd0: 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 0a is provided by.
fbe0: 2a 2a 20 73 63 72 61 74 63 68 20 62 75 66 66 65 ** scratch buffe
fbf0: 72 73 20 6f 72 20 69 66 20 6e 6f 20 73 63 72 61 rs or if no scra
fc00: 74 63 68 20 62 75 66 66 65 72 20 73 70 61 63 65 tch buffer space
fc10: 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 is specified, t
fc20: 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 67 6f hen SQLite.** go
fc30: 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d es to [sqlite3_m
fc40: 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 6f 62 74 61 alloc()] to obta
fc50: 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 in the memory it
fc60: 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a needs.</dd>.**.
fc70: 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
fc80: 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3c 2f NFIG_PAGECACHE</
fc90: 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
fca0: 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 option specifies
fcb0: 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 a static memory
fcc0: 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c buffer that SQL
fcd0: 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 0a ite can use for.
fce0: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
fcf0: 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 page cache with
fd00: 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
fd10: 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 61 cache implemena
fd20: 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68 69 73 20 tion. .** This
fd30: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 68 configuration sh
fd40: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 ould not be used
fd50: 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 if an applicati
fd60: 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 65 0a 2a on-define page.*
fd70: 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e * cache implemen
fd80: 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 64 65 64 tation is loaded
fd90: 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 using the SQLIT
fda0: 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 E_CONFIG_PCACHE
fdb0: 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 option..** There
fdc0: 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d are three argum
fdd0: 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 ents to this opt
fde0: 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 ion: A pointer t
fdf0: 6f 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c o the.** memory,
fe00: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 the size of eac
fe10: 68 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 h page buffer (s
fe20: 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 z), and the numb
fe30: 65 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e er of pages (N).
fe40: 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d .** The sz argum
fe50: 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f ent must be a po
fe60: 77 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 wer of two betwe
fe70: 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 en 512 and 32768
fe80: 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 . The first.**
fe90: 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 argument should
fea0: 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f point to an allo
feb0: 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 cation of at lea
fec0: 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 st sz*N bytes of
fed0: 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 memory..** SQLi
fee0: 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 te will use the
fef0: 6d 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64 20 memory provided
ff00: 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67 by the first arg
ff10: 75 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66 79 ument to satisfy
ff20: 20 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e its.** memory n
ff30: 65 65 64 73 20 66 6f 72 20 74 68 65 20 66 69 72 eeds for the fir
ff40: 73 74 20 4e 20 70 61 67 65 73 20 74 68 61 74 20 st N pages that
ff50: 69 74 20 61 64 64 73 20 74 6f 20 63 61 63 68 65 it adds to cache
ff60: 2e 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61 6c . If additional
ff70: 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 6d .** page cache m
ff80: 65 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20 emory is needed
ff90: 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 beyond what is p
ffa0: 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20 rovided by this
ffb0: 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 option, then.**
ffc0: 53 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20 5b SQLite goes to [
ffd0: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
ffe0: 5d 20 66 6f 72 20 74 68 65 20 61 64 64 69 74 69 ] for the additi
fff0: 6f 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70 61 onal storage spa
10000 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 ce..** The imple
10010 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 mentation might
10020 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 use one or more
10030 6f 66 20 74 68 65 20 4e 20 62 75 66 66 65 72 73 of the N buffers
10040 20 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d to hold .** mem
10050 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 69 ory accounting i
10060 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64 nformation. </dd
10070 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10080 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f TE_CONFIG_HEAP</
10090 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
100a0 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 option specifies
100b0 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 a static memory
100c0 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c buffer that SQL
100d0 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 ite will use.**
100e0 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 for all of its d
100f0 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c ynamic memory al
10100 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 location needs b
10110 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 eyond those prov
10120 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b ided.** for by [
10130 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 SQLITE_CONFIG_SC
10140 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 RATCH] and [SQLI
10150 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
10160 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 CHE]..** There a
10170 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e re three argumen
10180 74 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f ts: A pointer to
10190 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 the memory, the
101a0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 number of.** by
101b0 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 tes in the memor
101c0 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74 68 y buffer, and th
101d0 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 e minimum alloca
101e0 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a tion size. If.*
101f0 2a 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 6e * the first poin
10200 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 20 ter (the memory
10210 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c 4c pointer) is NULL
10220 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 65 , then SQLite re
10230 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 6e verts.** to usin
10240 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d 65 g its default me
10250 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 28 mory allocator (
10260 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f the system mallo
10270 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 c() implementati
10280 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 on),.** undoing
10290 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 any prior invoca
102a0 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f tion of [SQLITE_
102b0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 CONFIG_MALLOC].
102c0 20 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 If the.** memor
102d0 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 y pointer is not
102e0 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65 72 NULL and either
102f0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
10300 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b MEMSYS3] or.** [
10310 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
10320 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66 69 6e MSYS5] are defin
10330 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c 74 ed, then the alt
10340 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 0a ernative memory.
10350 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ** allocator is
10360 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64 6c engaged to handl
10370 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65 73 e all of SQLites
10380 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
10390 6f 6e 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a on needs.</dd>.*
103a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
103b0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 CONFIG_MUTEX</dt
103c0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 >.** <dd>This op
103d0 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e tion takes a sin
103e0 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 gle argument whi
103f0 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ch is a pointer
10400 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 to an.** instanc
10410 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 e of the [sqlite
10420 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 3_mutex_methods]
10430 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
10440 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 argument specif
10450 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 ies.** alternati
10460 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 ve low-level mut
10470 65 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 ex routines to b
10480 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a e used in place.
10490 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 ** the mutex rou
104a0 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f tines built into
104b0 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a SQLite.</dd>.**
104c0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
104d0 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f ONFIG_GETMUTEX</
104e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
104f0 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 option takes a s
10500 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 ingle argument w
10510 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 hich is a pointe
10520 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 r to an.** insta
10530 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 nce of the [sqli
10540 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
10550 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 s] structure. T
10560 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d he.** [sqlite3_m
10570 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a utex_methods].**
10580 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 structure is fi
10590 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 lled with the cu
105a0 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 rrently defined
105b0 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a mutex routines..
105c0 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 ** This option c
105d0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 an be used to ov
105e0 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 erload the defau
105f0 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 lt mutex allocat
10600 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ion.** routines
10610 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 with a wrapper u
10620 73 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 sed to track mut
10630 65 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 ex usage for per
10640 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 formance.** prof
10650 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 iling or testing
10660 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f , for example.</
10670 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
10680 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b LITE_CONFIG_LOOK
10690 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 ASIDE</dt>.** <d
106a0 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
106b0 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 kes two argument
106c0 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 s that determine
106d0 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 the default.**
106e0 6d 65 6d 6f 72 79 20 61 6c 6c 63 61 74 69 6f 6e memory allcation
106f0 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d lookaside optim
10700 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 ization. The fi
10710 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
10720 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 the.** size of e
10730 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ach lookaside bu
10740 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 ffer slot and th
10750 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 e second is the
10760 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
10770 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 ts allocated to
10780 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
10790 6e 6e 65 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a nnection.</dd>.*
107a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
107b0 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 3c 2f 64 CONFIG_PCACHE</d
107c0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
107d0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
107e0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
107f0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
10800 20 74 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 to.** an [sqlit
10810 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
10820 73 5d 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 s] object. This
10830 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 object specifie
10840 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a s the interface.
10850 2a 2a 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 ** to a custom p
10860 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d age cache implem
10870 65 6e 74 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 entation. SQLit
10880 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f e makes a copy o
10890 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 f the.** object
108a0 61 6e 64 20 75 73 65 73 20 69 74 20 66 6f 72 20 and uses it for
108b0 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 page cache memor
108c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f y allocations.</
108d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
108e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 LITE_CONFIG_GETP
108f0 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 CACHE</dt>.** <d
10900 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
10910 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 kes a single arg
10920 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
10930 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a pointer to an.*
10940 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 * [sqlite3_pcach
10950 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 e_methods] objec
10960 74 2e 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65 t. SQLite copie
10970 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 s of the current
10980 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 69 .** page cache i
10990 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e mplementation in
109a0 74 6f 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 3c to that object.<
109b0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e /dd>.**.** </dl>
109c0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
109d0 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
109e0 54 48 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69 THREAD 1 /* ni
109f0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c l */.#define SQL
10a00 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 ITE_CONFIG_MULTI
10a10 54 48 52 45 41 44 20 20 20 32 20 20 2f 2a 20 6e THREAD 2 /* n
10a20 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 il */.#define SQ
10a30 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 LITE_CONFIG_SERI
10a40 41 4c 49 5a 45 44 20 20 20 20 33 20 20 2f 2a 20 ALIZED 3 /*
10a50 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nil */.#define S
10a60 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
10a70 4c 4f 43 20 20 20 20 20 20 20 20 34 20 20 2f 2a LOC 4 /*
10a80 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
10a90 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 hods* */.#define
10aa0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 SQLITE_CONFIG_G
10ab0 45 54 4d 41 4c 4c 4f 43 20 20 20 20 20 35 20 20 ETMALLOC 5
10ac0 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d /* sqlite3_mem_m
10ad0 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 ethods* */.#defi
10ae0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10af0 5f 53 43 52 41 54 43 48 20 20 20 20 20 20 20 36 _SCRATCH 6
10b00 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 /* void*, int
10b10 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 sz, int N */.#de
10b20 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
10b30 49 47 5f 50 41 47 45 43 41 43 48 45 20 20 20 20 IG_PAGECACHE
10b40 20 37 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 7 /* void*, in
10b50 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 t sz, int N */.#
10b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
10b70 4e 46 49 47 5f 48 45 41 50 20 20 20 20 20 20 20 NFIG_HEAP
10b80 20 20 20 38 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 8 /* void*,
10b90 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 6d int nByte, int m
10ba0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 in */.#define SQ
10bb0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 LITE_CONFIG_MEMS
10bc0 54 41 54 55 53 20 20 20 20 20 39 20 20 2f 2a 20 TATUS 9 /*
10bd0 62 6f 6f 6c 65 61 6e 20 2a 2f 0a 23 64 65 66 69 boolean */.#defi
10be0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10bf0 5f 4d 55 54 45 58 20 20 20 20 20 20 20 20 31 30 _MUTEX 10
10c00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 /* sqlite3_mut
10c10 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 ex_methods* */.#
10c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
10c30 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 20 20 20 NFIG_GETMUTEX
10c40 20 20 31 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 11 /* sqlite3
10c50 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 _mutex_methods*
10c60 2a 2f 0a 2f 2a 20 70 72 65 76 69 6f 75 73 6c 79 */./* previously
10c70 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43 SQLITE_CONFIG_C
10c80 48 55 4e 4b 41 4c 4c 4f 43 20 31 32 20 77 68 69 HUNKALLOC 12 whi
10c90 63 68 20 69 73 20 6e 6f 77 20 75 6e 75 73 65 64 ch is now unused
10ca0 2e 20 2a 2f 20 0a 23 64 65 66 69 6e 65 20 53 51 . */ .#define SQ
10cb0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b LITE_CONFIG_LOOK
10cc0 41 53 49 44 45 20 20 20 20 31 33 20 20 2f 2a 20 ASIDE 13 /*
10cd0 69 6e 74 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 int int */.#defi
10ce0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10cf0 5f 50 43 41 43 48 45 20 20 20 20 20 20 20 31 34 _PCACHE 14
10d00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 /* sqlite3_pca
10d10 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a che_methods* */.
10d20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
10d30 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 20 ONFIG_GETPCACHE
10d40 20 20 20 31 35 20 20 2f 2a 20 73 71 6c 69 74 65 15 /* sqlite
10d50 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
10d60 2a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 * */../*.** CAPI
10d70 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 3REF: Configurat
10d80 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 ion Options {H10
10d90 31 37 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 170} <S20000>.**
10da0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
10db0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
10dc0 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61 69 nts are the avai
10dd0 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f lable integer co
10de0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
10df0 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 ons that.** can
10e00 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
10e10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
10e20 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
10e30 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e _db_config()] in
10e40 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e terface..**.** N
10e50 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ew configuration
10e60 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 options may be
10e70 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
10e80 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
10e90 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 te..** Existing
10ea0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
10eb0 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 tions might be d
10ec0 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 iscontinued. Ap
10ed0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 plications.** sh
10ee0 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 ould check the r
10ef0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
10f00 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 [sqlite3_db_conf
10f10 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 ig()] to make su
10f20 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 re that.** the c
10f30 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 all worked. The
10f40 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e [sqlite3_db_con
10f50 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 fig()] interface
10f60 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a will return a.*
10f70 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f * non-zero [erro
10f80 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 r code] if a dis
10f90 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 continued or uns
10fa0 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 upported configu
10fb0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a ration option.**
10fc0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a is invoked..**.
10fd0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
10fe0 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c QLITE_DBCONFIG_L
10ff0 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a OOKASIDE</dt>.**
11000 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
11010 20 74 61 6b 65 73 20 74 68 72 65 65 20 61 64 64 takes three add
11020 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 itional argument
11030 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 s that determine
11040 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 the .** [lookas
11050 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
11060 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 74 ator] configurat
11070 69 6f 6e 20 66 6f 72 20 74 68 65 20 5b 64 61 74 ion for the [dat
11080 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
11090 5d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 ]..** The first
110a0 61 72 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68 argument (the th
110b0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
110c0 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e [sqlite3_db_con
110d0 66 69 67 28 29 5d 20 69 73 20 61 0a 2a 2a 20 70 fig()] is a.** p
110e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f ointer to a memo
110f0 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 ry buffer to use
11100 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d for lookaside m
11110 65 6d 6f 72 79 2e 20 20 54 68 65 20 66 69 72 73 emory. The firs
11120 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 61 t.** argument ma
11130 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 y be NULL in whi
11140 63 68 20 63 61 73 65 20 53 51 4c 69 74 65 20 77 ch case SQLite w
11150 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 ill allocate the
11160 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 62 75 lookaside.** bu
11170 66 66 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e ffer itself usin
11180 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f g [sqlite3_mallo
11190 63 28 29 5d 2e 20 20 54 68 65 20 73 65 63 6f 6e c()]. The secon
111a0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
111b0 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 e.** size of eac
111c0 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 h lookaside buff
111d0 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 er slot and the
111e0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
111f0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
11200 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 ** slots. The s
11210 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 ize of the buffe
11220 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 r in the first a
11230 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 rgument must be
11240 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 greater than.**
11250 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
11260 70 72 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73 product of the s
11270 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 econd and third
11280 61 72 67 75 6d 65 6e 74 73 2e 3c 2f 64 64 3e 0a arguments.</dd>.
11290 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 **.** </dl>.*/.#
112a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 define SQLITE_DB
112b0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
112c0 20 20 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69 1001 /* voi
112d0 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a d* int int */...
112e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
112f0 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c Enable Or Disabl
11300 65 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c e Extended Resul
11310 74 20 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d t Codes {H12200}
11320 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
11330 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 The sqlite3_exte
11340 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 nded_result_code
11350 73 28 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 s() routine enab
11360 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 les or disables
11370 74 68 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 the.** [extended
11380 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 result codes] f
11390 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65 eature of SQLite
113a0 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 . The extended r
113b0 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 esult.** codes a
113c0 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 re disabled by d
113d0 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f efault for histo
113e0 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c rical compatibil
113f0 69 74 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f ity consideratio
11400 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ns..**.** Requir
11410 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
11420 30 31 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 01] [H12202].*/.
11430 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
11440 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
11450 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c result_codes(sql
11460 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 ite3*, int onoff
11470 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
11480 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20 EF: Last Insert
11490 52 6f 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c Rowid {H12220} <
114a0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 S10700>.**.** Ea
114b0 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53 ch entry in an S
114c0 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20 QLite table has
114d0 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20 a unique 64-bit
114e0 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
114f0 72 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65 r key called the
11500 20 5b 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64 [ROWID | "rowid
11510 22 5d 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 "]. The rowid is
11520 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
11530 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 e.** as an undec
11540 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d lared column nam
11550 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f ed ROWID, OID, o
11560 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e r _ROWID_ as lon
11570 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 g as those.** na
11580 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f mes are not also
11590 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 used by explici
115a0 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c tly declared col
115b0 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 umns. If.** the
115c0 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 table has a colu
115d0 6d 6e 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45 mn of type [INTE
115e0 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d GER PRIMARY KEY]
115f0 20 74 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d then that colum
11600 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 n.** is another
11610 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f alias for the ro
11620 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 wid..**.** This
11630 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
11640 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 the [rowid] of t
11650 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a he most recent.*
11660 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e * successful [IN
11670 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64 SERT] into the d
11680 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 atabase from the
11690 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
116a0 63 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 ction].** in the
116b0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
116c0 20 20 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66 If no successf
116d0 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 ul [INSERT]s.**
116e0 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72 72 have ever occurr
116f0 65 64 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62 ed on that datab
11700 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ase connection,
11710 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
11720 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 ..**.** If an [I
11730 4e 53 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69 NSERT] occurs wi
11740 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 thin a trigger,
11750 74 68 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d then the [rowid]
11760 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 65 64 of the inserted
11770 0a 2a 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72 .** row is retur
11780 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ned by this rout
11790 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ine as long as t
117a0 68 65 20 74 72 69 67 67 65 72 20 69 73 20 72 75 he trigger is ru
117b0 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e nning..** But on
117c0 63 65 20 74 68 65 20 74 72 69 67 67 65 72 20 74 ce the trigger t
117d0 65 72 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76 erminates, the v
117e0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
117f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
11800 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 reverts to the
11810 6c 61 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72 last value inser
11820 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 74 ted before the t
11830 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a rigger fired..**
11840 0a 2a 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20 .** An [INSERT]
11850 74 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 that fails due t
11860 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 o a constraint v
11870 69 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 iolation is not
11880 61 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 a.** successful
11890 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65 [INSERT] and doe
118a0 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 s not change the
118b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
118c0 62 79 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 by this.** routi
118d0 6e 65 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54 ne. Thus INSERT
118e0 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54 OR FAIL, INSERT
118f0 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45 OR IGNORE, INSE
11900 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a RT OR ROLLBACK,.
11910 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52 ** and INSERT OR
11920 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63 ABORT make no c
11930 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65 hanges to the re
11940 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
11950 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 is.** routine wh
11960 65 6e 20 74 68 65 69 72 20 69 6e 73 65 72 74 69 en their inserti
11970 6f 6e 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20 on fails. When
11980 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 INSERT OR REPLAC
11990 45 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20 E.** encounters
119a0 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f a constraint vio
119b0 6c 61 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20 lation, it does
119c0 6e 6f 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a not fail. The.*
119d0 2a 20 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75 * INSERT continu
119e0 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e es to completion
119f0 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 after deleting
11a00 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 rows that caused
11a10 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 .** the constrai
11a20 6e 74 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e nt problem so IN
11a30 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 SERT OR REPLACE
11a40 77 69 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e will always chan
11a50 67 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e ge.** the return
11a60 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69 value of this i
11a70 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
11a80 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
11a90 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
11aa0 2c 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 , an [INSERT] is
11ab0 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a considered to.*
11ac0 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20 * be successful
11ad0 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75 even if it is su
11ae0 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 bsequently rolle
11af0 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 d back..**.** Re
11b00 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
11b10 48 31 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d H12221] [H12223]
11b20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 .**.** If a sepa
11b30 72 61 74 65 20 74 68 72 65 61 64 20 70 65 72 66 rate thread perf
11b40 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45 orms a new [INSE
11b50 52 54 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a RT] on the same.
11b60 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
11b70 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65 ection while the
11b80 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 [sqlite3_last_i
11b90 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a nsert_rowid()].*
11ba0 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 * function is ru
11bb0 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63 nning and thus c
11bc0 68 61 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20 hanges the last
11bd0 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a insert [rowid],.
11be0 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ** then the valu
11bf0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 e returned by [s
11c00 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
11c10 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a rt_rowid()] is.*
11c20 2a 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 * unpredictable
11c30 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 and might not eq
11c40 75 61 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f ual either the o
11c50 6c 64 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a ld or the new.**
11c60 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f last insert [ro
11c70 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wid]..*/.SQLITE_
11c80 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
11c90 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 4 sqlite3_last_i
11ca0 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 nsert_rowid(sqli
11cb0 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
11cc0 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68 PI3REF: Count Th
11cd0 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 e Number Of Rows
11ce0 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34 Modified {H1224
11cf0 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 0} <S10600>.**.*
11d00 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
11d10 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
11d20 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20 72 er of database r
11d30 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68 ows that were ch
11d40 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 anged.** or inse
11d50 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 rted or deleted
11d60 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 by the most rece
11d70 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53 ntly completed S
11d80 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 QL statement.**
11d90 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 on the [database
11da0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
11db0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 cified by the fi
11dc0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a rst parameter..*
11dd0 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74 * Only changes t
11de0 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79 hat are directly
11df0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
11e00 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44 e [INSERT], [UPD
11e10 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c ATE],.** or [DEL
11e20 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 ETE] statement a
11e30 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78 re counted. Aux
11e40 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 63 iliary changes c
11e50 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67 aused by.** trig
11e60 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63 6f 75 gers are not cou
11e70 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20 5b 73 nted. Use the [s
11e80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
11e90 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e nges()] function
11ea0 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20 .** to find the
11eb0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
11ec0 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 69 6e changes includin
11ed0 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 g changes caused
11ee0 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a by triggers..**
11ef0 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f 20 61 .** Changes to a
11f00 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73 view that are s
11f10 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b imulated by an [
11f20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 INSTEAD OF trigg
11f30 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63 er].** are not c
11f40 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65 ounted. Only re
11f50 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 al table changes
11f60 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a are counted..**
11f70 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e 67 .** A "row chang
11f80 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 74 e" is a change t
11f90 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f o a single row o
11fa0 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 f a single table
11fb0 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 6e .** caused by an
11fc0 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c INSERT, DELETE,
11fd0 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 or UPDATE state
11fe0 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 74 ment. Rows that
11ff0 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 20 .** are changed
12000 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 20 as side effects
12010 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f 6e of [REPLACE] con
12020 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 straint resoluti
12030 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c on,.** rollback,
12040 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
12050 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d 2c g, [DROP TABLE],
12060 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72 or by any other
12070 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 .** mechanisms d
12080 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 o not count as d
12090 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 irect row change
120a0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 s..**.** A "trig
120b0 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20 ger context" is
120c0 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75 a scope of execu
120d0 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73 tion that begins
120e0 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 and.** ends wit
120f0 68 20 74 68 65 20 73 63 72 69 70 74 20 6f 66 20 h the script of
12100 61 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 a [CREATE TRIGGE
12110 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20 0a 2a R | trigger]. .*
12120 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74 65 * Most SQL state
12130 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76 61 ments are.** eva
12140 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20 6f luated outside o
12150 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20 20 f any trigger.
12160 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f 70 This is the "top
12170 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67 67 level".** trigg
12180 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66 20 er context. If
12190 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73 20 a trigger fires
121a0 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 from the top lev
121b0 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72 69 el, a.** new tri
121c0 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73 20 gger context is
121d0 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 20 entered for the
121e0 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 74 duration of that
121f0 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72 2e one.** trigger.
12200 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63 72 Subtriggers cr
12210 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 73 eate subcontexts
12220 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 74 for their durat
12230 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ion..**.** Calli
12240 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 ng [sqlite3_exec
12250 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
12260 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69 76 step()] recursiv
12270 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 ely does.** not
12280 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72 69 create a new tri
12290 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a gger context..**
122a0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
122b0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
122c0 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20 72 mber of direct r
122d0 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 ow changes in th
122e0 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 e.** most recent
122f0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
12300 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 or DELETE state
12310 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20 ment within the
12320 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 same.** trigger
12330 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 context..**.** T
12340 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65 64 hus, when called
12350 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 from the top le
12360 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 vel, this functi
12370 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a on returns the.*
12380 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e * number of chan
12390 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 ges in the most
123a0 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 recent INSERT, U
123b0 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 PDATE, or DELETE
123c0 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f 63 .** that also oc
123d0 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74 6f curred at the to
123e0 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69 6e p level. Within
123f0 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 74 the body of a t
12400 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20 73 rigger,.** the s
12410 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 qlite3_changes()
12420 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
12430 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e 64 e called to find
12440 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
12450 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 * changes in the
12460 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
12470 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54 2c ompleted INSERT,
12480 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
12490 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 TE.** statement
124a0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 within the body
124b0 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69 67 of the same trig
124c0 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c ger..** However,
124d0 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 the number retu
124e0 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e rned does not in
124f0 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a 2a clude changes.**
12500 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74 72 caused by subtr
12510 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68 6f iggers since tho
12520 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f 77 se have their ow
12530 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a n context..**.**
12540 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 See also the [s
12550 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
12560 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 nges()] interfac
12570 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f e and the.** [co
12580 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67 unt_changes prag
12590 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ma]..**.** Requi
125a0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
125b0 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 241] [H12243].**
125c0 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 .** If a separat
125d0 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 e thread makes c
125e0 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 hanges on the sa
125f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
12600 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 ection.** while
12610 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 [sqlite3_changes
12620 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 ()] is running t
12630 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
12640 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70 turned.** is unp
12650 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e redictable and n
12660 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a ot meaningful..*
12670 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
12680 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
12690 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
126a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74 ** CAPI3REF: Tot
126b0 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 al Number Of Row
126c0 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 s Modified {H122
126d0 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 60} <S10600>.**.
126e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
126f0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
12700 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 ber of row chang
12710 65 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e es caused by [IN
12720 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 SERT],.** [UPDAT
12730 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 E] or [DELETE] s
12740 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20 tatements since
12750 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
12760 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70 nnection] was op
12770 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 ened..** The cou
12780 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 nt includes all
12790 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61 6c 6c changes from all
127a0 20 0a 2a 2a 20 5b 43 52 45 41 54 45 20 54 52 49 .** [CREATE TRI
127b0 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20 GGER | trigger]
127c0 63 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 contexts. Howev
127d0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 er,.** the count
127e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
127f0 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 e changes used t
12800 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 o implement [REP
12810 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 LACE] constraint
12820 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 s,.** do rollbac
12830 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 ks or ABORT proc
12840 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 essing, or [DROP
12850 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 TABLE] processi
12860 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e ng. The.** coun
12870 74 20 64 6f 65 73 20 6e 6f 74 20 72 6f 77 73 20 t does not rows
12880 6f 66 20 76 69 65 77 73 20 74 68 61 74 20 66 69 of views that fi
12890 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f re an [INSTEAD O
128a0 46 20 74 72 69 67 67 65 72 5d 2c 20 74 68 6f 75 F trigger], thou
128b0 67 68 20 69 66 0a 2a 2a 20 74 68 65 20 49 4e 53 gh if.** the INS
128c0 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 20 TEAD OF trigger
128d0 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 66 makes changes of
128e0 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73 65 20 its own, those
128f0 63 68 61 6e 67 65 73 20 61 72 65 0a 2a 2a 20 63 changes are.** c
12900 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68 65 20 63 ounted..** The c
12910 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e 74 hanges are count
12920 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 ed as soon as th
12930 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 e statement that
12940 20 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 0a 2a makes them is.*
12950 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28 77 68 65 * completed (whe
12960 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 n the statement
12970 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 64 handle is passed
12980 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 to [sqlite3_res
12990 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c et()] or.** [sql
129a0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
129b0 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 )..**.** See als
129c0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 o the [sqlite3_c
129d0 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 hanges()] interf
129e0 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b ace and the.** [
129f0 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 count_changes pr
12a00 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 agma]..**.** Req
12a10 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
12a20 31 32 32 36 31 5d 20 5b 48 31 32 32 36 33 5d 0a 12261] [H12263].
12a30 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 **.** If a separ
12a40 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 ate thread makes
12a50 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 changes on the
12a60 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
12a70 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c nnection.** whil
12a80 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c e [sqlite3_total
12a90 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 _changes()] is r
12aa0 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 unning then the
12ab0 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 value.** returne
12ac0 64 20 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 d is unpredictab
12ad0 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 le and not meani
12ae0 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ngful..*/.SQLITE
12af0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
12b00 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 _total_changes(s
12b10 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a qlite3*);../*.**
12b20 20 43 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72 CAPI3REF: Inter
12b30 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e rupt A Long-Runn
12b40 69 6e 67 20 51 75 65 72 79 20 7b 48 31 32 32 37 ing Query {H1227
12b50 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S30500>.**.*
12b60 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
12b70 63 61 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 causes any pendi
12b80 6e 67 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 ng database oper
12b90 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 ation to abort a
12ba0 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 nd.** return at
12bb0 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 its earliest opp
12bc0 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72 ortunity. This r
12bd0 6f 75 74 69 6e 65 20 69 73 20 74 79 70 69 63 61 outine is typica
12be0 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e lly.** called in
12bf0 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 response to a u
12c00 73 65 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20 ser action such
12c10 61 73 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e as pressing "Can
12c20 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d cel".** or Ctrl-
12c30 43 20 77 68 65 72 65 20 74 68 65 20 75 73 65 72 C where the user
12c40 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 wants a long qu
12c50 65 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f ery operation to
12c60 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 halt.** immedia
12c70 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 tely..**.** It i
12c80 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 s safe to call t
12c90 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d his routine from
12ca0 20 61 20 74 68 72 65 61 64 20 64 69 66 66 65 72 a thread differ
12cb0 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 ent from the.**
12cc0 74 68 72 65 61 64 20 74 68 61 74 20 69 73 20 63 thread that is c
12cd0 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 urrently running
12ce0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 the database op
12cf0 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 eration. But it
12d00 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20 .** is not safe
12d10 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 to call this rou
12d20 74 69 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74 tine with a [dat
12d30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
12d40 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f ] that.** is clo
12d50 73 65 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f sed or might clo
12d60 73 65 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 se before sqlite
12d70 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 3_interrupt() re
12d80 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 turns..**.** If
12d90 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e an SQL operation
12da0 20 69 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 is very nearly
12db0 66 69 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 finished at the
12dc0 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c time when.** sql
12dd0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
12de0 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e is called, then
12df0 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 it might not ha
12e00 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 ve an opportunit
12e10 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 y.** to be inter
12e20 72 75 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74 rupted and might
12e30 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d continue to com
12e40 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 pletion..**.** A
12e50 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 n SQL operation
12e60 74 68 61 74 20 69 73 20 69 6e 74 65 72 72 75 70 that is interrup
12e70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ted will return
12e80 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 [SQLITE_INTERRUP
12e90 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e T]..** If the in
12ea0 74 65 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 terrupted SQL op
12eb0 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e eration is an IN
12ec0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 SERT, UPDATE, or
12ed0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 DELETE.** that
12ee0 69 73 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70 is inside an exp
12ef0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f licit transactio
12f00 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 n, then the enti
12f10 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a re transaction.*
12f20 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 * will be rolled
12f30 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 back automatica
12f40 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 lly..**.** The s
12f50 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
12f60 28 44 29 20 63 61 6c 6c 20 69 73 20 69 6e 20 65 (D) call is in e
12f70 66 66 65 63 74 20 75 6e 74 69 6c 20 61 6c 6c 20 ffect until all
12f80 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e currently runnin
12f90 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 g.** SQL stateme
12fa0 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 nts on [database
12fb0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 63 connection] D c
12fc0 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79 20 6e 65 omplete. Any ne
12fd0 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 w SQL statements
12fe0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 .** that are sta
12ff0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73 rted after the s
13000 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
13010 28 29 20 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f () call and befo
13020 72 65 20 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 re the .** runni
13030 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 72 65 ng statements re
13040 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 20 69 aches zero are i
13050 6e 74 65 72 72 75 70 74 65 64 20 61 73 20 69 66 nterrupted as if
13060 20 74 68 65 79 20 68 61 64 20 62 65 65 6e 0a 2a they had been.*
13070 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 * running prior
13080 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 to the sqlite3_i
13090 6e 74 65 72 72 75 70 74 28 29 20 63 61 6c 6c 2e nterrupt() call.
130a0 20 20 4e 65 77 20 53 51 4c 20 73 74 61 74 65 6d New SQL statem
130b0 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ents.** that are
130c0 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 started after t
130d0 68 65 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 he running state
130e0 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65 61 63 68 ment count reach
130f0 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e es zero are.** n
13100 6f 74 20 65 66 66 65 63 74 65 64 20 62 79 20 74 ot effected by t
13110 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
13120 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20 63 61 6c rupt()..** A cal
13130 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 l to sqlite3_int
13140 65 72 72 75 70 74 28 44 29 20 74 68 61 74 20 6f errupt(D) that o
13150 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 72 65 ccurs when there
13160 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a are no running.
13170 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
13180 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 s is a no-op and
13190 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f has no effect o
131a0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 n SQL statements
131b0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 .** that are sta
131c0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73 rted after the s
131d0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
131e0 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e () call returns.
131f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
13200 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 37 31 5d nts:.** [H12271]
13210 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 [H12272].**.**
13220 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
13230 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65 connection close
13240 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 s while [sqlite3
13250 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a _interrupt()].**
13260 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e is running then
13270 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c 6c bad things will
13280 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a likely happen..
13290 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
132a0 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 id sqlite3_inter
132b0 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a rupt(sqlite3*);.
132c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
132d0 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 6e Determine If An
132e0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 SQL Statement I
132f0 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35 s Complete {H105
13300 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 10} <S70200>.**.
13310 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
13320 73 20 61 72 65 20 75 73 65 66 75 6c 20 64 75 72 s are useful dur
13330 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 ing command-line
13340 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65 72 6d input to determ
13350 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 ine if the.** cu
13360 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
13370 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20 66 6f text seems to fo
13380 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 rm a complete SQ
13390 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a L statement or.*
133a0 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 * if additional
133b0 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65 64 20 input is needed
133c0 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 before sending t
133d0 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 he text into.**
133e0 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72 73 69 SQLite for parsi
133f0 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 ng. These routi
13400 6e 65 73 20 72 65 74 75 72 6e 20 31 20 69 66 20 nes return 1 if
13410 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
13420 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 62 .** appears to b
13430 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c e a complete SQL
13440 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 73 statement. A s
13450 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 67 tatement is judg
13460 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 ed to be.** comp
13470 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 20 lete if it ends
13480 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e with a semicolon
13490 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f token and is no
134a0 74 20 61 20 70 72 65 66 69 78 20 6f 66 20 61 0a t a prefix of a.
134b0 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 ** well-formed C
134c0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 REATE TRIGGER st
134d0 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d 69 63 6f atement. Semico
134e0 6c 6f 6e 73 20 74 68 61 74 20 61 72 65 20 65 6d lons that are em
134f0 62 65 64 64 65 64 20 77 69 74 68 69 6e 0a 2a 2a bedded within.**
13500 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 string literals
13510 20 6f 72 20 71 75 6f 74 65 64 20 69 64 65 6e 74 or quoted ident
13520 69 66 69 65 72 20 6e 61 6d 65 73 20 6f 72 20 63 ifier names or c
13530 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 0a omments are not.
13540 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 74 ** independent t
13550 6f 6b 65 6e 73 20 28 74 68 65 79 20 61 72 65 20 okens (they are
13560 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 part of the toke
13570 6e 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 20 n in which they
13580 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64 65 64 29 are.** embedded)
13590 20 61 6e 64 20 74 68 75 73 20 64 6f 20 6e 6f 74 and thus do not
135a0 20 63 6f 75 6e 74 20 61 73 20 61 20 73 74 61 74 count as a stat
135b0 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72 ement terminator
135c0 2e 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a 2a . Whitespace.**
135d0 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68 and comments th
135e0 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 at follow the fi
135f0 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 nal semicolon ar
13600 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a e ignored..**.**
13610 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
13620 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 return 0 if the
13630 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69 6e 63 statement is inc
13640 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 61 0a 2a omplete. If a.*
13650 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 * memory allocat
13660 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 ion fails, then
13670 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
13680 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
13690 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 These routines d
136a0 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 o not parse the
136b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 SQL statements t
136c0 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 hus.** will not
136d0 64 65 74 65 63 74 20 73 79 6e 74 61 63 74 69 63 detect syntactic
136e0 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53 ally incorrect S
136f0 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c QL..**.** If SQL
13700 69 74 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ite has not been
13710 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 initialized usi
13720 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 ng [sqlite3_init
13730 69 61 6c 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 ialize()] prior
13740 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 .** to invoking
13750 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
13760 31 36 28 29 20 74 68 65 6e 20 73 71 6c 69 74 65 16() then sqlite
13770 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
13780 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 s invoked.** aut
13790 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 71 omatically by sq
137a0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 lite3_complete16
137b0 28 29 2e 20 20 49 66 20 74 68 61 74 20 69 6e 69 (). If that ini
137c0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c tialization fail
137d0 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 s,.** then the r
137e0 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
137f0 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 sqlite3_complet
13800 65 31 36 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f e16() will be no
13810 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 n-zero.** regard
13820 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 less of whether
13830 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 or not the input
13840 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 SQL is complete
13850 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
13860 65 6e 74 73 3a 20 5b 48 31 30 35 31 31 5d 20 5b ents: [H10511] [
13870 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 H10512].**.** Th
13880 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 e input to [sqli
13890 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 te3_complete()]
138a0 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 must be a zero-t
138b0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 erminated.** UTF
138c0 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a -8 string..**.**
138d0 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 The input to [s
138e0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 qlite3_complete1
138f0 36 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 6()] must be a z
13900 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
13910 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 * UTF-16 string
13920 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f in native byte o
13930 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rder..*/.SQLITE_
13940 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
13950 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 complete(const c
13960 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 har *sql);.SQLIT
13970 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
13980 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 3_complete16(con
13990 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a st void *sql);..
139a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
139b0 52 65 67 69 73 74 65 72 20 41 20 43 61 6c 6c 62 Register A Callb
139c0 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20 53 51 ack To Handle SQ
139d0 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f 72 73 LITE_BUSY Errors
139e0 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30 34 30 {H12310} <S4040
139f0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
13a00 75 74 69 6e 65 20 73 65 74 73 20 61 20 63 61 6c utine sets a cal
13a10 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 lback function t
13a20 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 6e 76 hat might be inv
13a30 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a oked whenever.**
13a40 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
13a50 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 64 61 ade to open a da
13a60 74 61 62 61 73 65 20 74 61 62 6c 65 20 74 68 61 tabase table tha
13a70 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 t another thread
13a80 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73 20 68 .** or process h
13a90 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a as locked..**.**
13aa0 20 49 66 20 74 68 65 20 62 75 73 79 20 63 61 6c If the busy cal
13ab0 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 lback is NULL, t
13ac0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 hen [SQLITE_BUSY
13ad0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 ] or [SQLITE_IOE
13ae0 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 RR_BLOCKED].** i
13af0 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 s returned immed
13b00 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f iately upon enco
13b10 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 untering the loc
13b20 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79 20 63 k. If the busy c
13b30 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f allback.** is no
13b40 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 t NULL, then the
13b50 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62 callback will b
13b60 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 e invoked with t
13b70 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a wo arguments..**
13b80 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 .** The first ar
13b90 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 68 61 gument to the ha
13ba0 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70 79 20 ndler is a copy
13bb0 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 of the void* poi
13bc0 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 nter which.** is
13bd0 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d the third argum
13be0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 ent to sqlite3_b
13bf0 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e 20 20 usy_handler().
13c00 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
13c10 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61 ent to.** the ha
13c20 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 ndler callback i
13c30 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
13c40 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 62 times that the b
13c50 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61 73 0a usy handler has.
13c60 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65 64 20 ** been invoked
13c70 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 for this locking
13c80 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68 65 0a event. If the.
13c90 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b ** busy callback
13ca0 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65 6e returns 0, then
13cb0 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 no additional a
13cc0 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61 64 65 ttempts are made
13cd0 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 to.** access th
13ce0 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 5b e database and [
13cf0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 SQLITE_BUSY] or
13d00 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c [SQLITE_IOERR_BL
13d10 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72 6e OCKED] is return
13d20 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 ed..** If the ca
13d30 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e llback returns n
13d40 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e on-zero, then an
13d50 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a 2a 2a other attempt.**
13d60 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e is made to open
13d70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f the database fo
13d80 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 74 68 r reading and th
13d90 65 20 63 79 63 6c 65 20 72 65 70 65 61 74 73 2e e cycle repeats.
13da0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 73 65 .**.** The prese
13db0 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20 68 61 nce of a busy ha
13dc0 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74 20 67 ndler does not g
13dd0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 69 74 uarantee that it
13de0 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 will be invoked
13df0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65 20 69 .** when there i
13e00 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f s lock contentio
13e10 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64 65 74 n. If SQLite det
13e20 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69 6e 76 ermines that inv
13e30 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 0a 2a oking the busy.*
13e40 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20 * handler could
13e50 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65 61 64 result in a dead
13e60 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f lock, it will go
13e70 20 61 68 65 61 64 20 61 6e 64 20 72 65 74 75 72 ahead and retur
13e80 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a n [SQLITE_BUSY].
13e90 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f ** or [SQLITE_IO
13ea0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 ERR_BLOCKED] ins
13eb0 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 tead of invoking
13ec0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
13ed0 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 r..** Consider a
13ee0 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72 65 20 scenario where
13ef0 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73 20 68 one process is h
13f00 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20 6c 6f olding a read lo
13f10 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 ck that.** it is
13f20 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f trying to promo
13f30 74 65 20 74 6f 20 61 20 72 65 73 65 72 76 65 64 te to a reserved
13f40 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 lock and.** a s
13f50 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 69 73 econd process is
13f60 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73 65 72 holding a reser
13f70 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20 69 74 ved lock that it
13f80 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f is trying.** to
13f90 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e 20 65 promote to an e
13fa0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 xclusive lock.
13fb0 54 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 The first proces
13fc0 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64 s cannot proceed
13fd0 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 69 .** because it i
13fe0 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 s blocked by the
13ff0 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 65 20 second and the
14000 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 63 second process c
14010 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65 65 64 annot.** proceed
14020 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62 because it is b
14030 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 66 69 locked by the fi
14040 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20 70 72 rst. If both pr
14050 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b ocesses.** invok
14060 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c e the busy handl
14070 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77 69 6c ers, neither wil
14080 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f 67 72 l make any progr
14090 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c ess. Therefore,
140a0 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74 75 72 .** SQLite retur
140b0 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ns [SQLITE_BUSY]
140c0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 70 for the first p
140d0 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67 20 74 rocess, hoping t
140e0 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69 6c 6c hat this.** will
140f0 20 69 6e 64 75 63 65 20 74 68 65 20 66 69 72 73 induce the firs
14100 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72 65 6c t process to rel
14110 65 61 73 65 20 69 74 73 20 72 65 61 64 20 6c 6f ease its read lo
14120 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 ck and allow.**
14130 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 the second proce
14140 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a ss to proceed..*
14150 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 *.** The default
14160 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 busy callback i
14170 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 s NULL..**.** Th
14180 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 e [SQLITE_BUSY]
14190 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 error is convert
141a0 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f ed to [SQLITE_IO
141b0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 ERR_BLOCKED].**
141c0 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 when SQLite is i
141d0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
141e0 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61 63 74 a large transact
141f0 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20 74 68 ion where all th
14200 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77 69 6c e.** changes wil
14210 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 l not fit into t
14220 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 he in-memory cac
14230 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c he. SQLite will
14240 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 .** already hold
14250 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
14260 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14270 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 file, but it ne
14280 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 eds.** to promot
14290 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 e this lock to E
142a0 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68 61 74 XCLUSIVE so that
142b0 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 it can spill ca
142c0 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 che.** pages int
142d0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
142e0 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61 72 6d ile without harm
142f0 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a to concurrent.*
14300 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66 20 69 * readers. If i
14310 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 t is unable to p
14320 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c romote the lock,
14330 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d then the in-mem
14340 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c ory.** cache wil
14350 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 l be left in an
14360 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 inconsistent sta
14370 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20 65 72 te and so the er
14380 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 ror.** code is p
14390 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 romoted from the
143a0 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 relatively beni
143b0 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d gn [SQLITE_BUSY]
143c0 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 to.** the more
143d0 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 severe [SQLITE_I
143e0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 OERR_BLOCKED].
143f0 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64 65 20 This error code
14400 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 promotion.** for
14410 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 ces an automatic
14420 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 rollback of the
14430 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65 20 74 changes. See t
14440 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f he.** <a href="/
14450 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 cvstrac/wiki?p=C
14460 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 orruptionFollowi
14470 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a ngBusyError">.**
14480 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f CorruptionFollo
14490 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 wingBusyError</a
144a0 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f 72 20 > wiki page for
144b0 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 a discussion of
144c0 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 why.** this is i
144d0 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 mportant..**.**
144e0 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 There can only b
144f0 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 e a single busy
14500 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64 20 handler defined
14510 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 61 74 for each.** [dat
14520 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14530 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 6e 65 ]. Setting a ne
14540 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 w busy handler c
14550 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 72 65 lears any.** pre
14560 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 6e 64 viously set hand
14570 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 ler. Note that
14580 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 calling [sqlite3
14590 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d _busy_timeout()]
145a0 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65 .** will also se
145b0 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 20 62 t or clear the b
145c0 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a usy handler..**.
145d0 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c ** The busy call
145e0 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f 74 20 back should not
145f0 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 take any actions
14600 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20 74 68 which modify th
14610 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
14620 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e nnection that in
14630 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 voked the busy h
14640 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73 75 63 andler. Any suc
14650 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 h actions.** res
14660 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 ult in undefined
14670 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a behavior..** .*
14680 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
14690 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b 48 31 32 ** [H12311] [H12
146a0 33 31 32 5d 20 5b 48 31 32 33 31 34 5d 20 5b 48 312] [H12314] [H
146b0 31 32 33 31 36 5d 20 5b 48 31 32 33 31 38 5d 0a 12316] [H12318].
146c0 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20 68 61 6e **.** A busy han
146d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c dler must not cl
146e0 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ose the database
146f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f connection.** o
14700 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 r [prepared stat
14710 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f ement] that invo
14720 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e ked the busy han
14730 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dler..*/.SQLITE_
14740 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
14750 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c busy_handler(sql
14760 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f ite3*, int(*)(vo
14770 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 id*,int), void*)
14780 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
14790 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 F: Set A Busy Ti
147a0 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c meout {H12340} <
147b0 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40410>.**.** Th
147c0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
147d0 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f a [sqlite3_busy_
147e0 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 handler | busy h
147f0 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 andler] that sle
14800 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 eps.** for a spe
14810 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 cified amount of
14820 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 time when a tab
14830 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 le is locked. T
14840 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 he handler.** wi
14850 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c ll sleep multipl
14860 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 e times until at
14870 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c least "ms" mill
14880 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 iseconds of slee
14890 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 ping.** have acc
148a0 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 umulated. {H1234
148b0 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69 3} After "ms" mi
148c0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c lliseconds of sl
148d0 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 eeping,.** the h
148e0 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30 andler returns 0
148f0 20 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73 which causes [s
14900 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74 qlite3_step()] t
14910 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c o return.** [SQL
14920 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 ITE_BUSY] or [SQ
14930 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
14940 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ED]..**.** Calli
14950 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
14960 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 with an argument
14970 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
14980 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 ual to zero.** t
14990 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 urns off all bus
149a0 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a y handlers..**.*
149b0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 * There can only
149c0 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 be a single bus
149d0 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 y handler for a
149e0 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 particular.** [d
149f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14a00 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65 on] any any give
14a10 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e n moment. If an
14a20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c other busy handl
14a30 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 er.** was define
14a40 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 d (using [sqlit
14a50 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 e3_busy_handler(
14a60 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c )]) prior to cal
14a70 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ling.** this rou
14a80 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72 tine, that other
14a90 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 busy handler is
14aa0 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 cleared..**.**
14ab0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
14ac0 20 5b 48 31 32 33 34 31 5d 20 5b 48 31 32 33 34 [H12341] [H1234
14ad0 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 3] [H12344].*/.S
14ae0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
14af0 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f lite3_busy_timeo
14b00 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ut(sqlite3*, int
14b10 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ms);../*.** CAP
14b20 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e I3REF: Convenien
14b30 63 65 20 52 6f 75 74 69 6e 65 73 20 46 6f 72 20 ce Routines For
14b40 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 65 73 20 Running Queries
14b50 7b 48 31 32 33 37 30 7d 20 3c 53 31 30 30 30 30 {H12370} <S10000
14b60 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69 >.**.** Definiti
14b70 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c 74 20 on: A <b>result
14b80 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d table</b> is mem
14b90 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75 ory data structu
14ba0 72 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 re created by th
14bb0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 e.** [sqlite3_ge
14bc0 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74 65 72 t_table()] inter
14bd0 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c 74 20 face. A result
14be0 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 table records th
14bf0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75 e.** complete qu
14c00 65 72 79 20 72 65 73 75 6c 74 73 20 66 72 6f 6d ery results from
14c10 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71 75 65 one or more que
14c20 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ries..**.** The
14c30 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c table conceptual
14c40 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65 72 20 ly has a number
14c50 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75 of rows and colu
14c60 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 mns. But.** the
14c70 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6e se numbers are n
14c80 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 ot part of the r
14c90 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74 73 65 esult table itse
14ca0 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75 lf. These.** nu
14cb0 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 6e mbers are obtain
14cc0 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 ed separately.
14cd0 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e 75 6d Let N be the num
14ce0 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 ber of rows.** a
14cf0 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 nd M be the numb
14d00 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a er of columns..*
14d10 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 *.** A result ta
14d20 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 79 20 ble is an array
14d30 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a of pointers to z
14d40 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
14d50 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a TF-8 strings..**
14d60 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b 31 29 There are (N+1)
14d70 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 *M elements in t
14d80 68 65 20 61 72 72 61 79 2e 20 20 54 68 65 20 66 he array. The f
14d90 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 73 20 irst M pointers
14da0 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f point.** to zero
14db0 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 -terminated stri
14dc0 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74 61 69 ngs that contai
14dd0 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 n the names of t
14de0 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 he columns..** T
14df0 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 he remaining ent
14e00 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74 ries all point t
14e10 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e o query results.
14e20 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 72 65 NULL values re
14e30 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 sult.** in NULL
14e40 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f pointers. All o
14e50 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 ther values are
14e60 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38 20 7a in their UTF-8 z
14e70 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
14e80 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
14e90 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 75 72 ntation as retur
14ea0 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f ned by [sqlite3_
14eb0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a column_text()]..
14ec0 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 **.** A result t
14ed0 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e 73 69 able might consi
14ee0 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 st of one or mor
14ef0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
14f00 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e ions..** It is n
14f10 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73 73 20 ot safe to pass
14f20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 64 a result table d
14f30 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69 irectly to [sqli
14f40 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 te3_free()]..**
14f50 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 73 A result table s
14f60 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63 hould be dealloc
14f70 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 ated using [sqli
14f80 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 te3_free_table()
14f90 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 ]..**.** As an e
14fa0 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20 72 65 xample of the re
14fb0 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72 6d 61 sult table forma
14fc0 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71 75 65 t, suppose a que
14fd0 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 ry result.** is
14fe0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
14ff0 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
15000 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 4e 61 re>.** Na
15010 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67 65 0a me | Age.
15020 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d ** ------
15030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
15040 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c 69 63 -.** Alic
15050 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a 2a 20 e | 43.**
15060 20 20 20 20 20 20 20 42 6f 62 20 20 20 20 20 20 Bob
15070 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20 20 20 | 28.**
15080 20 20 43 69 6e 64 79 20 20 20 20 20 20 20 7c 20 Cindy |
15090 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 21.** </pre></bl
150a0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
150b0 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 63 6f There are two co
150c0 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e 64 20 lumn (M==2) and
150d0 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d 3d 33 three rows (N==3
150e0 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a 2a 20 ). Thus the.**
150f0 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 61 73 result table has
15100 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53 75 70 8 entries. Sup
15110 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c 74 20 pose the result
15120 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 0a table is stored.
15130 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6e ** in an array n
15140 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e 20 20 ames azResult.
15150 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20 68 6f Then azResult ho
15160 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 lds this content
15170 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
15180 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 ote><pre>.**
15190 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
151a0 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a ;0] = "Name";.**
151b0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
151c0 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b [1] = "Age";
151d0 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
151e0 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c ult[2] = "Al
151f0 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 ice";.**
15200 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33 5d 20 azResult[3]
15210 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20 20 20 = "43";.**
15220 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 34 azResult[4
15230 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 ] = "Bob";.**
15240 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
15250 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 1;5] = "28";.**
15260 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
15270 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64 79 22 #91;6] = "Cindy"
15280 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
15290 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20 22 32 sult[7] = "2
152a0 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 1";.** </pre></b
152b0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
152c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 The sqlite3_get
152d0 5f 74 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f _table() functio
152e0 6e 20 65 76 61 6c 75 61 74 65 73 20 6f 6e 65 20 n evaluates one
152f0 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 or more.** semic
15300 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65 64 20 53 olon-separated S
15310 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e QL statements in
15320 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e the zero-termin
15330 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a 20 73 74 ated UTF-8.** st
15340 72 69 6e 67 20 6f 66 20 69 74 73 20 32 6e 64 20 ring of its 2nd
15350 70 61 72 61 6d 65 74 65 72 2e 20 20 49 74 20 72 parameter. It r
15360 65 74 75 72 6e 73 20 61 20 72 65 73 75 6c 74 20 eturns a result
15370 74 61 62 6c 65 20 74 6f 20 74 68 65 0a 2a 2a 20 table to the.**
15380 70 6f 69 6e 74 65 72 20 67 69 76 65 6e 20 69 6e pointer given in
15390 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 74 its 3rd paramet
153a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 er..**.** After
153b0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 the calling func
153c0 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 tion has finishe
153d0 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 73 75 d using the resu
153e0 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 0a 2a 2a lt, it should.**
153f0 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 pass the pointe
15400 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 r to the result
15410 74 61 62 6c 65 20 74 6f 20 73 71 6c 69 74 65 33 table to sqlite3
15420 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 69 6e _free_table() in
15430 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c order to.** rel
15440 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 ease the memory
15450 74 68 61 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 that was malloce
15460 64 2e 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 d. Because of t
15470 68 65 20 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 he way the.** [s
15480 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
15490 20 68 61 70 70 65 6e 73 20 77 69 74 68 69 6e 20 happens within
154a0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
154b0 65 28 29 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 e(), the calling
154c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 .** function mus
154d0 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c t not try to cal
154e0 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 l [sqlite3_free(
154f0 29 5d 20 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e )] directly. On
15500 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 ly.** [sqlite3_f
15510 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 ree_table()] is
15520 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20 able to release
15530 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 the memory prope
15540 72 6c 79 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a rly and safely..
15550 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
15560 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 3_get_table() in
15570 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 terface is imple
15580 6d 65 6e 74 65 64 20 61 73 20 61 20 77 72 61 70 mented as a wrap
15590 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 per around.** [s
155a0 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 qlite3_exec()].
155b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 The sqlite3_get
155c0 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 _table() routine
155d0 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 does not have a
155e0 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 ccess.** to any
155f0 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 internal data st
15600 72 75 63 74 75 72 65 73 20 6f 66 20 53 51 4c 69 ructures of SQLi
15610 74 65 2e 20 20 49 74 20 75 73 65 73 20 6f 6e 6c te. It uses onl
15620 79 20 74 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 y the public.**
15630 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 65 interface define
15640 64 20 68 65 72 65 2e 20 20 41 73 20 61 20 63 6f d here. As a co
15650 6e 73 65 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 nsequence, error
15660 73 20 74 68 61 74 20 6f 63 63 75 72 20 69 6e 20 s that occur in
15670 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c the.** wrapper l
15680 61 79 65 72 20 6f 75 74 73 69 64 65 20 6f 66 20 ayer outside of
15690 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 the internal [sq
156a0 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 63 61 lite3_exec()] ca
156b0 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 ll are not.** re
156c0 66 6c 65 63 74 65 64 20 69 6e 20 73 75 62 73 65 flected in subse
156d0 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b quent calls to [
156e0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
156f0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 )] or [sqlite3_e
15700 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 rrmsg()]..**.**
15710 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
15720 20 5b 48 31 32 33 37 31 5d 20 5b 48 31 32 33 37 [H12371] [H1237
15730 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b 48 31 32 3] [H12374] [H12
15740 33 37 36 5d 20 5b 48 31 32 33 37 39 5d 20 5b 48 376] [H12379] [H
15750 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12382].*/.SQLITE
15760 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
15770 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 _get_table(. sq
15780 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
15790 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 /* An open d
157a0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e atabase */. con
157b0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 st char *zSql,
157c0 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 /* SQL to be
157d0 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63 evaluated */. c
157e0 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 har ***pazResult
157f0 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 , /* Results
15800 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a of the query */.
15810 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 int *pnRow,
15820 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15830 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73 r of result rows
15840 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
15850 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e . int *pnColumn
15860 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 , /* Numb
15870 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c er of result col
15880 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 65 72 umns written her
15890 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a e */. char **pz
158a0 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 Errmsg /*
158b0 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65 Error msg writte
158c0 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c n here */.);.SQL
158d0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
158e0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
158f0 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a char **result);.
15900 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
15910 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72 69 6e Formatted Strin
15920 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e 63 74 g Printing Funct
15930 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20 3c 53 ions {H17400} <S
15940 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 70000><S20000>.*
15950 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
15960 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c 69 6b nes are workalik
15970 65 73 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74 es of the "print
15980 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 f()" family of f
15990 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d unctions.** from
159a0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
159b0 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 library..**.** T
159c0 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e he sqlite3_mprin
159d0 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 tf() and sqlite3
159e0 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74 _vmprintf() rout
159f0 69 6e 65 73 20 77 72 69 74 65 20 74 68 65 69 72 ines write their
15a00 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f .** results into
15a10 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
15a20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
15a30 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 alloc()]..** The
15a40 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 strings returne
15a50 64 20 62 79 20 74 68 65 73 65 20 74 77 6f 20 72 d by these two r
15a60 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 outines should b
15a70 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79 e.** released by
15a80 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
15a90 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65 ]. Both routine
15aa0 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 s return a.** NU
15ab0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 LL pointer if [s
15ac0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
15ad0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c is unable to al
15ae0 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a locate enough.**
15af0 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 memory to hold
15b00 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 the resulting st
15b10 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 ring..**.** In s
15b20 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
15b30 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d ) routine is sim
15b40 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 ilar to "snprint
15b50 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 f()" from.** the
15b60 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
15b70 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 ary. The result
15b80 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
15b90 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 the.** buffer s
15ba0 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 upplied as the s
15bb0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
15bc0 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 whose size is gi
15bd0 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 ven by.** the fi
15be0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e rst parameter. N
15bf0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 ote that the ord
15c00 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 er of the.** fir
15c10 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 st two parameter
15c20 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72 s is reversed fr
15c30 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 om snprintf().
15c40 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 This is an.** hi
15c50 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e storical acciden
15c60 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 t that cannot be
15c70 20 66 69 78 65 64 20 77 69 74 68 6f 75 74 20 62 fixed without b
15c80 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 reaking.** backw
15c90 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
15ca0 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 ty. Note also t
15cb0 68 61 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 hat sqlite3_snpr
15cc0 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e intf().** return
15cd0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
15ce0 74 73 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ts buffer instea
15cf0 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 d of the number
15d00 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 of.** characters
15d10 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 actually writte
15d20 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 n into the buffe
15d30 72 2e 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 r. We admit tha
15d40 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 t.** the number
15d50 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 77 72 of characters wr
15d60 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 itten would be a
15d70 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74 more useful ret
15d80 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 urn.** value but
15d90 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 we cannot chang
15da0 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 e the implementa
15db0 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f tion of sqlite3_
15dc0 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f snprintf().** no
15dd0 77 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 w without breaki
15de0 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ng compatibility
15df0 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 ..**.** As long
15e00 61 73 20 74 68 65 20 62 75 66 66 65 72 20 73 69 as the buffer si
15e10 7a 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ze is greater th
15e20 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 an zero, sqlite3
15e30 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 _snprintf().** g
15e40 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 uarantees that t
15e50 68 65 20 62 75 66 66 65 72 20 69 73 20 61 6c 77 he buffer is alw
15e60 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 ays zero-termina
15e70 74 65 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a ted. The first.
15e80 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22 ** parameter "n"
15e90 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69 is the total si
15ea0 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 ze of the buffer
15eb0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 , including spac
15ec0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 e for.** the zer
15ed0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 o terminator. S
15ee0 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 o the longest st
15ef0 72 69 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 ring that can be
15f00 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 completely.** w
15f10 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e ritten will be n
15f20 2d 31 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a -1 characters..*
15f30 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
15f40 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e nes all implemen
15f50 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 t some additiona
15f60 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 l formatting.**
15f70 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 options that are
15f80 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 useful for cons
15f90 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61 tructing SQL sta
15fa0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 tements..** All
15fb0 6f 66 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 of the usual pri
15fc0 6e 74 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67 ntf() formatting
15fd0 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20 options apply.
15fe0 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68 In addition, th
15ff0 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25 ere.** is are "%
16000 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25 q", "%Q", and "%
16010 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a z" options..**.*
16020 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 * The %q option
16030 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e works like %s in
16040 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69 74 that it substit
16050 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d utes a null-term
16060 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 inated.** string
16070 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65 from the argume
16080 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71 nt list. But %q
16090 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76 also doubles ev
160a0 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63 74 ery '\'' charact
160b0 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73 er..** %q is des
160c0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e igned for use in
160d0 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69 side a string li
160e0 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c teral. By doubl
160f0 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a ing each '\''.**
16100 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65 73 character it es
16110 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72 61 capes that chara
16120 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 cter and allows
16130 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 it to be inserte
16140 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 d into.** the st
16150 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 ring..**.** For
16160 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 example, assume
16170 74 68 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 the string varia
16180 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 ble zText contai
16190 6e 73 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f ns text as follo
161a0 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
161b0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
161c0 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 char *zText = "I
161d0 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 t's a happy day!
161e0 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c ";.** </pre></bl
161f0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
16200 4f 6e 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 One can use this
16210 20 74 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 text in an SQL
16220 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c statement as fol
16230 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lows:.**.** <blo
16240 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
16250 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 char *zSQL = s
16260 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 qlite3_mprintf("
16270 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
16280 65 20 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c e VALUES('%q')",
16290 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c zText);.** sql
162a0 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 ite3_exec(db, zS
162b0 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a QL, 0, 0, 0);.**
162c0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
162d0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c SQL);.** </pre><
162e0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
162f0 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 25 ** Because the %
16300 71 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 q format string
16310 69 73 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 is used, the '\'
16320 27 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a ' character in z
16330 54 65 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 Text.** is escap
16340 65 64 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 ed and the SQL g
16350 65 6e 65 72 61 74 65 64 20 69 73 20 61 73 20 66 enerated is as f
16360 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ollows:.**.** <b
16370 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
16380 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 ** INSERT INTO
16390 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 table1 VALUES('I
163a0 74 27 27 73 20 61 20 68 61 70 70 79 20 64 61 79 t''s a happy day
163b0 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 !').** </pre></b
163c0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
163d0 20 54 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 This is correct
163e0 2e 20 20 48 61 64 20 77 65 20 75 73 65 64 20 25 . Had we used %
163f0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c s instead of %q,
16400 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 the generated S
16410 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 QL.** would have
16420 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 looked like thi
16430 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 s:.**.** <blockq
16440 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 uote><pre>.** I
16450 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 NSERT INTO table
16460 31 20 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 1 VALUES('It's a
16470 20 68 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a happy day!');.*
16480 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
16490 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 uote>.**.** This
164a0 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 second example
164b0 69 73 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 is an SQL syntax
164c0 20 65 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 error. As a ge
164d0 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 neral rule you s
164e0 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 hould.** always
164f0 75 73 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f use %q instead o
16500 66 20 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 f %s when insert
16510 69 6e 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 ing text into a
16520 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a string literal..
16530 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 **.** The %Q opt
16540 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 ion works like %
16550 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f q except it also
16560 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f adds single quo
16570 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 tes around.** th
16580 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 e outside of the
16590 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 total string.
165a0 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 Additionally, if
165b0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
165c0 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e n the.** argumen
165d0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c t list is a NULL
165e0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 pointer, %Q sub
165f0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78 stitutes the tex
16600 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 t "NULL" (withou
16610 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 t.** single quot
16620 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 es) in place of
16630 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20 the %Q option.
16640 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c So, for example,
16650 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a one could say:.
16660 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
16670 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 e><pre>.** char
16680 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 *zSQL = sqlite3
16690 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 _mprintf("INSERT
166a0 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 INTO table VALU
166b0 45 53 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b ES(%Q)", zText);
166c0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 .** sqlite3_exe
166d0 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 c(db, zSQL, 0, 0
166e0 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 , 0);.** sqlite
166f0 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 3_free(zSQL);.**
16700 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 </pre></blockqu
16710 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ote>.**.** The c
16720 6f 64 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72 ode above will r
16730 65 6e 64 65 72 20 61 20 63 6f 72 72 65 63 74 20 ender a correct
16740 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e SQL statement in
16750 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 the zSQL.** var
16760 69 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 iable even if th
16770 65 20 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65 e zText variable
16780 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
16790 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 er..**.** The "%
167a0 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 z" formatting op
167b0 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74 tion works exact
167c0 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74 ly like "%s" wit
167d0 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f h the.** additio
167e0 6e 20 74 68 61 74 20 61 66 74 65 72 20 74 68 65 n that after the
167f0 20 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e string has been
16800 20 72 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64 read and copied
16810 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 into.** the res
16820 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 ult, [sqlite3_fr
16830 65 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 ee()] is called
16840 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 on the input str
16850 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ing. {END}.**.**
16860 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
16870 2a 20 5b 48 31 37 34 30 33 5d 20 5b 48 31 37 34 * [H17403] [H174
16880 30 36 5d 20 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 06] [H17407].*/.
16890 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
168a0 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 *sqlite3_mprintf
168b0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e (const char*,...
168c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 );.SQLITE_API ch
168d0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 ar *sqlite3_vmpr
168e0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a intf(const char*
168f0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 , va_list);.SQLI
16900 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
16910 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e ite3_snprintf(in
16920 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t,char*,const ch
16930 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a ar*, ...);../*.*
16940 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f * CAPI3REF: Memo
16950 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 ry Allocation Su
16960 62 73 79 73 74 65 6d 20 7b 48 31 37 33 30 30 7d bsystem {H17300}
16970 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20000>.**.**
16980 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 The SQLite core
16990 20 75 73 65 73 20 74 68 65 73 65 20 74 68 72 65 uses these thre
169a0 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 e routines for a
169b0 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a ll of its own.**
169c0 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 internal memory
169d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 allocation need
169e0 73 2e 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65 s. "Core" in the
169f0 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e previous senten
16a00 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 ce.** does not i
16a10 6e 63 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67 nclude operating
16a20 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 -system specific
16a30 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
16a40 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 6e ion. The.** Win
16a50 64 6f 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 dows VFS uses na
16a60 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e tive malloc() an
16a70 64 20 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d d free() for som
16a80 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a e operations..**
16a90 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
16aa0 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 malloc() routine
16ab0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
16ac0 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a er to a block.**
16ad0 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 20 6c 65 of memory at le
16ae0 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 6c ast N bytes in l
16af0 65 6e 67 74 68 2c 20 77 68 65 72 65 20 4e 20 69 ength, where N i
16b00 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e s the parameter.
16b10 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 6d .** If sqlite3_m
16b20 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e 61 62 6c alloc() is unabl
16b30 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 75 66 66 e to obtain suff
16b40 69 63 69 65 6e 74 20 66 72 65 65 0a 2a 2a 20 6d icient free.** m
16b50 65 6d 6f 72 79 2c 20 69 74 20 72 65 74 75 72 6e emory, it return
16b60 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
16b70 2e 20 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 . If the parame
16b80 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 ter N to.** sqli
16b90 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 te3_malloc() is
16ba0 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 zero or negative
16bb0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 61 then sqlite3_ma
16bc0 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 0a 2a lloc() returns.*
16bd0 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 * a NULL pointer
16be0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 ..**.** Calling
16bf0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 77 sqlite3_free() w
16c00 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 70 72 ith a pointer pr
16c10 65 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 eviously returne
16c20 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f d.** by sqlite3_
16c30 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 malloc() or sqli
16c40 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 te3_realloc() re
16c50 6c 65 61 73 65 73 20 74 68 61 74 20 6d 65 6d 6f leases that memo
16c60 72 79 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 ry so.** that it
16c70 20 6d 69 67 68 74 20 62 65 20 72 65 75 73 65 64 might be reused
16c80 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 . The sqlite3_f
16c90 72 65 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 ree() routine is
16ca0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 .** a no-op if i
16cb0 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 s called with a
16cc0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50 NULL pointer. P
16cd0 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f assing a NULL po
16ce0 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 inter.** to sqli
16cf0 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 68 61 te3_free() is ha
16d00 72 6d 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62 rmless. After b
16d10 65 69 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f eing freed, memo
16d20 72 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 69 ry.** should nei
16d30 74 68 65 72 20 62 65 20 72 65 61 64 20 6e 6f 72 ther be read nor
16d40 20 77 72 69 74 74 65 6e 2e 20 20 45 76 65 6e 20 written. Even
16d50 72 65 61 64 69 6e 67 20 70 72 65 76 69 6f 75 73 reading previous
16d60 6c 79 20 66 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f ly freed.** memo
16d70 72 79 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 ry might result
16d80 69 6e 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f in a segmentatio
16d90 6e 20 66 61 75 6c 74 20 6f 72 20 6f 74 68 65 72 n fault or other
16da0 20 73 65 76 65 72 65 20 65 72 72 6f 72 2e 0a 2a severe error..*
16db0 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 72 75 70 74 * Memory corrupt
16dc0 69 6f 6e 2c 20 61 20 73 65 67 6d 65 6e 74 61 74 ion, a segmentat
16dd0 69 6f 6e 20 66 61 75 6c 74 2c 20 6f 72 20 6f 74 ion fault, or ot
16de0 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 her severe error
16df0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 73 75 6c 74 .** might result
16e00 20 69 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65 if sqlite3_free
16e10 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 () is called wit
16e20 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 h a non-NULL poi
16e30 6e 74 65 72 20 74 68 61 74 0a 2a 2a 20 77 61 73 nter that.** was
16e40 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 not obtained fr
16e50 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
16e60 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 c() or sqlite3_r
16e70 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 ealloc()..**.**
16e80 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c The sqlite3_real
16e90 6c 6f 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 loc() interface
16ea0 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 73 69 attempts to resi
16eb0 7a 65 20 61 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 ze a.** prior me
16ec0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
16ed0 74 6f 20 62 65 20 61 74 20 6c 65 61 73 74 20 4e to be at least N
16ee0 20 62 79 74 65 73 2c 20 77 68 65 72 65 20 4e 20 bytes, where N
16ef0 69 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 is the.** second
16f00 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 parameter. The
16f10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
16f20 6f 6e 20 74 6f 20 62 65 20 72 65 73 69 7a 65 64 on to be resized
16f30 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a is the first.**
16f40 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20 parameter. If
16f50 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
16f60 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
16f70 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 ealloc().** is a
16f80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 NULL pointer th
16f90 65 6e 20 69 74 73 20 62 65 68 61 76 69 6f 72 20 en its behavior
16fa0 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 is identical to
16fb0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 calling.** sqlit
16fc0 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68 65 e3_malloc(N) whe
16fd0 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f re N is the seco
16fe0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
16ff0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
17000 29 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 )..** If the sec
17010 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ond parameter to
17020 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
17030 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a () is zero or.**
17040 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 negative then t
17050 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 65 he behavior is e
17060 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 xactly the same
17070 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 as calling.** sq
17080 6c 69 74 65 33 5f 66 72 65 65 28 50 29 20 77 68 lite3_free(P) wh
17090 65 72 65 20 50 20 69 73 20 74 68 65 20 66 69 72 ere P is the fir
170a0 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
170b0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
170c0 29 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 )..** sqlite3_re
170d0 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20 alloc() returns
170e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d a pointer to a m
170f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
17100 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20 .** of at least
17110 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 N bytes in size
17120 6f 72 20 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 or NULL if suffi
17130 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 cient memory is
17140 75 6e 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 unavailable..**
17150 49 66 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 If M is the size
17160 20 6f 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c of the prior al
17170 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d location, then m
17180 69 6e 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a in(N,M) bytes.**
17190 20 6f 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c of the prior al
171a0 6c 6f 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 location are cop
171b0 69 65 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 ied into the beg
171c0 69 6e 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 inning of buffer
171d0 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 returned.** by
171e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
171f0 29 20 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 ) and the prior
17200 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 allocation is fr
17210 65 65 64 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 eed..** If sqlit
17220 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 e3_realloc() ret
17230 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 urns NULL, then
17240 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 the prior alloca
17250 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 tion.** is not f
17260 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 reed..**.** The
17270 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20 memory returned
17280 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f by sqlite3_mallo
17290 63 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f c() and sqlite3_
172a0 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 realloc().** is
172b0 61 6c 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74 always aligned t
172c0 6f 20 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20 o at least an 8
172d0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 7b byte boundary. {
172e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 END}.**.** The d
172f0 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 efault implement
17300 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d ation of the mem
17310 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
17320 75 62 73 79 73 74 65 6d 20 75 73 65 73 0a 2a 2a ubsystem uses.**
17330 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 the malloc(), r
17340 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 ealloc() and fre
17350 65 28 29 20 70 72 6f 76 69 64 65 64 20 62 79 20 e() provided by
17360 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c the standard C l
17370 69 62 72 61 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 ibrary..** {H173
17380 38 32 7d 20 48 6f 77 65 76 65 72 2c 20 69 66 20 82} However, if
17390 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
173a0 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 ed with the.** S
173b0 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a QLITE_MEMORY_SIZ
173c0 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 E=<i>NNN</i> C p
173d0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
173e0 6f 20 28 77 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c o (where <i>NNN<
173f0 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 /i>.** is an int
17400 65 67 65 72 29 2c 20 74 68 65 6e 20 53 51 4c 69 eger), then SQLi
17410 74 65 20 63 72 65 61 74 65 20 61 20 73 74 61 74 te create a stat
17420 69 63 20 61 72 72 61 79 20 6f 66 20 61 74 20 6c ic array of at l
17430 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f east.** <i>NNN</
17440 69 3e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 i> bytes in size
17450 20 61 6e 64 20 75 73 65 73 20 74 68 61 74 20 61 and uses that a
17460 72 72 61 79 20 66 6f 72 20 61 6c 6c 20 6f 66 20 rray for all of
17470 69 74 73 20 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d its dynamic.** m
17480 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
17490 20 6e 65 65 64 73 2e 20 7b 45 4e 44 7d 20 20 41 needs. {END} A
174a0 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
174b0 20 61 6c 6c 6f 63 61 74 6f 72 20 6f 70 74 69 6f allocator optio
174c0 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 64 ns.** may be add
174d0 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
174e0 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 eases..**.** In
174f0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 SQLite version 3
17500 2e 35 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20 .5.0 and 3.5.1,
17510 69 74 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20 it was possible
17520 74 6f 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 to define.** the
17530 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d SQLITE_OMIT_MEM
17540 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 ORY_ALLOCATION w
17550 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 hich would cause
17560 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a the built-in.**
17570 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
17580 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
17590 73 20 74 6f 20 62 65 20 6f 6d 69 74 74 65 64 2e s to be omitted.
175a0 20 20 54 68 61 74 20 63 61 70 61 62 69 6c 69 74 That capabilit
175b0 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 y.** is no longe
175c0 72 20 70 72 6f 76 69 64 65 64 2e 20 20 4f 6e 6c r provided. Onl
175d0 79 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 y built-in memor
175e0 79 20 61 6c 6c 6f 63 61 74 6f 72 73 20 63 61 6e y allocators can
175f0 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 be used..**.**
17600 54 68 65 20 57 69 6e 64 6f 77 73 20 4f 53 20 69 The Windows OS i
17610 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 20 63 nterface layer c
17620 61 6c 6c 73 0a 2a 2a 20 74 68 65 20 73 79 73 74 alls.** the syst
17630 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 em malloc() and
17640 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 free() directly
17650 77 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 0a when converting.
17660 2a 2a 20 66 69 6c 65 6e 61 6d 65 73 20 62 65 74 ** filenames bet
17670 77 65 65 6e 20 74 68 65 20 55 54 46 2d 38 20 65 ween the UTF-8 e
17680 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 ncoding used by
17690 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 77 68 SQLite.** and wh
176a0 61 74 65 76 65 72 20 66 69 6c 65 6e 61 6d 65 20 atever filename
176b0 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 encoding is used
176c0 20 62 79 20 74 68 65 20 70 61 72 74 69 63 75 6c by the particul
176d0 61 72 20 57 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e ar Windows.** in
176e0 73 74 61 6c 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d stallation. Mem
176f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
17700 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 rrors are detect
17710 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 79 20 ed, but.** they
17720 61 72 65 20 72 65 70 6f 72 74 65 64 20 62 61 63 are reported bac
17730 6b 20 61 73 20 5b 53 51 4c 49 54 45 5f 43 41 4e k as [SQLITE_CAN
17740 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 TOPEN] or.** [SQ
17750 4c 49 54 45 5f 49 4f 45 52 52 5d 20 72 61 74 68 LITE_IOERR] rath
17760 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f er than [SQLITE_
17770 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 NOMEM]..**.** Re
17780 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
17790 48 31 37 33 30 33 5d 20 5b 48 31 37 33 30 34 5d H17303] [H17304]
177a0 20 5b 48 31 37 33 30 35 5d 20 5b 48 31 37 33 30 [H17305] [H1730
177b0 36 5d 20 5b 48 31 37 33 31 30 5d 20 5b 48 31 37 6] [H17310] [H17
177c0 33 31 32 5d 20 5b 48 31 37 33 31 35 5d 20 5b 48 312] [H17315] [H
177d0 31 37 33 31 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 17318].** [H1732
177e0 31 5d 20 5b 48 31 37 33 32 32 5d 20 5b 48 31 37 1] [H17322] [H17
177f0 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 323].**.** The p
17800 6f 69 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 ointer arguments
17810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 to [sqlite3_fre
17820 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
17830 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 3_realloc()].**
17840 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
17850 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e ULL or else poin
17860 74 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 ters obtained fr
17870 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e om a prior.** in
17880 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c vocation of [sql
17890 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f ite3_malloc()] o
178a0 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c r [sqlite3_reall
178b0 6f 63 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a oc()] that have.
178c0 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 ** not yet been
178d0 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 released..**.**
178e0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
178f0 6d 75 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 must not read or
17900 20 77 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 write any part
17910 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 of.** a block of
17920 20 6d 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 memory after it
17930 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 has been releas
17940 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c ed using.** [sql
17950 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 ite3_free()] or
17960 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
17970 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ()]..*/.SQLITE_A
17980 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 PI void *sqlite3
17990 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 _malloc(int);.SQ
179a0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
179b0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 qlite3_realloc(v
179c0 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 oid*, int);.SQLI
179d0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
179e0 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b te3_free(void*);
179f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
17a00 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 : Memory Allocat
17a10 6f 72 20 53 74 61 74 69 73 74 69 63 73 20 7b 48 or Statistics {H
17a20 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30 3e 0a 17370} <S30210>.
17a30 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f **.** SQLite pro
17a40 76 69 64 65 73 20 74 68 65 73 65 20 74 77 6f 20 vides these two
17a50 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 72 interfaces for r
17a60 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 eporting on the
17a70 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 status.** of the
17a80 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
17a90 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 ()], [sqlite3_fr
17aa0 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 ee()], and [sqli
17ab0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a te3_realloc()].*
17ac0 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 * routines, whic
17ad0 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 h form the built
17ae0 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 -in memory alloc
17af0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
17b00 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
17b10 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 37 31 5d nts:.** [H17371]
17b20 20 5b 48 31 37 33 37 33 5d 20 5b 48 31 37 33 37 [H17373] [H1737
17b30 34 5d 20 5b 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 4] [H17375].*/.S
17b40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
17b50 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
17b60 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 memory_used(void
17b70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 );.SQLITE_API sq
17b80 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
17b90 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 te3_memory_highw
17ba0 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c ater(int resetFl
17bb0 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ag);../*.** CAPI
17bc0 33 52 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 3REF: Pseudo-Ran
17bd0 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 dom Number Gener
17be0 61 74 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 ator {H17390} <S
17bf0 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 20000>.**.** SQL
17c00 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 ite contains a h
17c10 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 igh-quality pseu
17c20 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 do-random number
17c30 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 generator (PRNG
17c40 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c ) used to.** sel
17c50 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 ect random [ROWI
17c60 44 20 7c 20 52 4f 57 49 44 73 5d 20 77 68 65 6e D | ROWIDs] when
17c70 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 72 inserting new r
17c80 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 74 61 ecords into a ta
17c90 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 6c 72 65 ble that.** alre
17ca0 61 64 79 20 75 73 65 73 20 74 68 65 20 6c 61 72 ady uses the lar
17cb0 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 5b 52 gest possible [R
17cc0 4f 57 49 44 5d 2e 20 20 54 68 65 20 50 52 4e 47 OWID]. The PRNG
17cd0 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 66 6f is also used fo
17ce0 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 r.** the build-i
17cf0 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 72 n random() and r
17d00 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c 20 andomblob() SQL
17d10 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 functions. This
17d20 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 interface allow
17d30 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e s.** application
17d40 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 s to access the
17d50 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f 74 same PRNG for ot
17d60 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2a her purposes..**
17d70 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 .** A call to th
17d80 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 is routine store
17d90 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 61 6e s N bytes of ran
17da0 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 66 domness into buf
17db0 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 fer P..**.** The
17dc0 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 first time this
17dd0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f routine is invo
17de0 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e 74 65 ked (either inte
17df0 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 rnally or by.**
17e00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 the application)
17e10 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 65 65 the PRNG is see
17e20 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d ded using random
17e30 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a ness obtained.**
17e40 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f from the xRando
17e50 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 mness method of
17e60 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c the default [sql
17e70 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
17e80 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 73 ..** On all subs
17e90 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f equent invocatio
17ea0 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 ns, the pseudo-r
17eb0 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e andomness is gen
17ec0 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e erated.** intern
17ed0 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 ally and without
17ee0 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 recourse to the
17ef0 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 [sqlite3_vfs] x
17f00 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 Randomness.** me
17f10 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 thod..**.** Requ
17f20 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
17f30 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 7392].*/.SQLITE_
17f40 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
17f50 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 _randomness(int
17f60 4e 2c 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a N, void *P);../*
17f70 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f .** CAPI3REF: Co
17f80 6d 70 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f mpile-Time Autho
17f90 72 69 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 rization Callbac
17fa0 6b 73 20 7b 48 31 32 35 30 30 7d 20 3c 53 37 30 ks {H12500} <S70
17fb0 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 100>.**.** This
17fc0 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 routine register
17fd0 73 20 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63 s a authorizer c
17fe0 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 allback with a p
17ff0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 articular.** [da
18000 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
18010 6e 5d 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 n], supplied in
18020 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
18030 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f nt..** The autho
18040 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
18050 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c s invoked as SQL
18060 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 statements are
18070 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a being compiled.*
18080 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 * by [sqlite3_pr
18090 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 epare()] or its
180a0 76 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 variants [sqlite
180b0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 3_prepare_v2()],
180c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
180d0 70 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 pare16()] and [s
180e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
180f0 5f 76 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69 _v2()]. At vari
18100 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 ous.** points du
18110 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 ring the compila
18120 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 tion process, as
18130 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20 logic is being
18140 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 created.** to pe
18150 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63 rform various ac
18160 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f tions, the autho
18170 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
18180 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 s invoked to.**
18190 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74 see if those act
181a0 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 ions are allowed
181b0 2e 20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 . The authorize
181c0 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c r callback shoul
181d0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c d.** return [SQL
181e0 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 ITE_OK] to allow
181f0 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51 the action, [SQ
18200 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 LITE_IGNORE] to
18210 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 disallow the.**
18220 73 70 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20 specific action
18230 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51 but allow the SQ
18240 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63 L statement to c
18250 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a ontinue to be.**
18260 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53 compiled, or [S
18270 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63 QLITE_DENY] to c
18280 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 ause the entire
18290 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f SQL statement to
182a0 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20 be.** rejected
182b0 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 with an error.
182c0 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 If the authorize
182d0 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 r callback retur
182e0 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20 ns.** any value
182f0 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 other than [SQLI
18300 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c TE_IGNORE], [SQL
18310 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c ITE_OK], or [SQL
18320 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 ITE_DENY].** the
18330 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 n the [sqlite3_p
18340 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
18350 65 71 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 equivalent call
18360 74 68 61 74 20 74 72 69 67 67 65 72 65 64 0a 2a that triggered.*
18370 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 * the authorizer
18380 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 will fail with
18390 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
183a0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
183b0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
183c0 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 s [SQLITE_OK], t
183d0 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 hat means the op
183e0 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 eration.** reque
183f0 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 57 68 65 sted is ok. Whe
18400 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 n the callback r
18410 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 eturns [SQLITE_D
18420 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 ENY], the.** [sq
18430 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
18440 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e ()] or equivalen
18450 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 t call that trig
18460 67 65 72 65 64 20 74 68 65 0a 2a 2a 20 61 75 74 gered the.** aut
18470 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 horizer will fai
18480 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 l with an error
18490 6d 65 73 73 61 67 65 20 65 78 70 6c 61 69 6e 69 message explaini
184a0 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63 63 65 73 ng that.** acces
184b0 73 20 69 73 20 64 65 6e 69 65 64 2e 20 0a 2a 2a s is denied. .**
184c0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
184d0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
184e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
184f0 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ck is a copy of
18500 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 the third.** par
18510 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
18520 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
18530 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 izer() interface
18540 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 . The second par
18550 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ameter.** to the
18560 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 callback is an
18570 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f integer [SQLITE_
18580 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f COPY | action co
18590 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 de] that specifi
185a0 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 es.** the partic
185b0 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 ular action to b
185c0 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 e authorized. Th
185d0 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 e third through
185e0 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 sixth parameters
185f0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 .** to the callb
18600 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 ack are zero-ter
18610 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 minated strings
18620 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 that contain add
18630 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 itional.** detai
18640 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 ls about the act
18650 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 ion to be author
18660 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ized..**.** If t
18670 68 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 he action code i
18680 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a s [SQLITE_READ].
18690 2a 2a 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 ** and the callb
186a0 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c ack returns [SQL
186b0 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e ITE_IGNORE] then
186c0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
186d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 d statement] sta
186e0 74 65 6d 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 tement is constr
186f0 75 63 74 65 64 20 74 6f 20 73 75 62 73 74 69 74 ucted to substit
18700 75 74 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 ute.** a NULL va
18710 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 lue in place of
18720 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e the table column
18730 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 that would have
18740 0a 2a 2a 20 62 65 65 6e 20 72 65 61 64 20 69 66 .** been read if
18750 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 [SQLITE_OK] had
18760 20 62 65 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 been returned.
18770 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e The [SQLITE_IGN
18780 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 ORE].** return c
18790 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 an be used to de
187a0 6e 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 ny an untrusted
187b0 75 73 65 72 20 61 63 63 65 73 73 20 74 6f 20 69 user access to i
187c0 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c ndividual.** col
187d0 75 6d 6e 73 20 6f 66 20 61 20 74 61 62 6c 65 2e umns of a table.
187e0 0a 2a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f .** If the actio
187f0 6e 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 n code is [SQLIT
18800 45 5f 44 45 4c 45 54 45 5d 20 61 6e 64 20 74 68 E_DELETE] and th
18810 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
18820 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 47 ns.** [SQLITE_IG
18830 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 20 5b NORE] then the [
18840 44 45 4c 45 54 45 5d 20 6f 70 65 72 61 74 69 6f DELETE] operatio
18850 6e 20 70 72 6f 63 65 65 64 73 20 62 75 74 20 74 n proceeds but t
18860 68 65 0a 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20 he.** [truncate
18870 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 optimization] is
18880 20 64 69 73 61 62 6c 65 64 20 61 6e 64 20 61 6c disabled and al
18890 6c 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 l rows are delet
188a0 65 64 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e ed individually.
188b0 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72 .**.** An author
188c0 69 7a 65 72 20 69 73 20 75 73 65 64 20 77 68 65 izer is used whe
188d0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 n [sqlite3_prepa
188e0 72 65 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a re | preparing].
188f0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
18900 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73 s from an untrus
18910 74 65 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65 ted source, to e
18920 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 53 nsure that the S
18930 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
18940 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61 do not try to a
18950 63 63 65 73 73 20 64 61 74 61 20 74 68 65 79 20 ccess data they
18960 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 are not allowed
18970 74 6f 20 73 65 65 2c 20 6f 72 20 74 68 61 74 20 to see, or that
18980 74 68 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 they do not.** t
18990 72 79 20 74 6f 20 65 78 65 63 75 74 65 20 6d 61 ry to execute ma
189a0 6c 69 63 69 6f 75 73 20 73 74 61 74 65 6d 65 6e licious statemen
189b0 74 73 20 74 68 61 74 20 64 61 6d 61 67 65 20 74 ts that damage t
189c0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 46 6f he database. Fo
189d0 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e r.** example, an
189e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 application may
189f0 20 61 6c 6c 6f 77 20 61 20 75 73 65 72 20 74 6f allow a user to
18a00 20 65 6e 74 65 72 20 61 72 62 69 74 72 61 72 79 enter arbitrary
18a10 0a 2a 2a 20 53 51 4c 20 71 75 65 72 69 65 73 20 .** SQL queries
18a20 66 6f 72 20 65 76 61 6c 75 61 74 69 6f 6e 20 62 for evaluation b
18a30 79 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 42 y a database. B
18a40 75 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ut the applicati
18a50 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 on does.** not w
18a60 61 6e 74 20 74 68 65 20 75 73 65 72 20 74 6f 20 ant the user to
18a70 62 65 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 be able to make
18a80 61 72 62 69 74 72 61 72 79 20 63 68 61 6e 67 65 arbitrary change
18a90 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 s to the.** data
18aa0 62 61 73 65 2e 20 20 41 6e 20 61 75 74 68 6f 72 base. An author
18ab0 69 7a 65 72 20 63 6f 75 6c 64 20 74 68 65 6e 20 izer could then
18ac0 62 65 20 70 75 74 20 69 6e 20 70 6c 61 63 65 20 be put in place
18ad0 77 68 69 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65 while the.** use
18ae0 72 2d 65 6e 74 65 72 65 64 20 53 51 4c 20 69 73 r-entered SQL is
18af0 20 62 65 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f being [sqlite3_
18b00 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 prepare | prepar
18b10 65 64 5d 20 74 68 61 74 0a 2a 2a 20 64 69 73 61 ed] that.** disa
18b20 6c 6c 6f 77 73 20 65 76 65 72 79 74 68 69 6e 67 llows everything
18b30 20 65 78 63 65 70 74 20 5b 53 45 4c 45 43 54 5d except [SELECT]
18b40 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
18b50 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 ** Applications
18b60 74 68 61 74 20 6e 65 65 64 20 74 6f 20 70 72 6f that need to pro
18b70 63 65 73 73 20 53 51 4c 20 66 72 6f 6d 20 75 6e cess SQL from un
18b80 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 73 0a trusted sources.
18b90 2a 2a 20 6d 69 67 68 74 20 61 6c 73 6f 20 63 6f ** might also co
18ba0 6e 73 69 64 65 72 20 6c 6f 77 65 72 69 6e 67 20 nsider lowering
18bb0 72 65 73 6f 75 72 63 65 20 6c 69 6d 69 74 73 20 resource limits
18bc0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c using [sqlite3_l
18bd0 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c imit()].** and l
18be0 69 6d 69 74 69 6e 67 20 64 61 74 61 62 61 73 65 imiting database
18bf0 20 73 69 7a 65 20 75 73 69 6e 67 20 74 68 65 20 size using the
18c00 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d [max_page_count]
18c10 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 [PRAGMA].** in
18c20 61 64 64 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e addition to usin
18c30 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e g an authorizer.
18c40 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 .**.** Only a si
18c50 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a 65 72 20 ngle authorizer
18c60 63 61 6e 20 62 65 20 69 6e 20 70 6c 61 63 65 20 can be in place
18c70 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f on a database co
18c80 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 nnection.** at a
18c90 20 74 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c time. Each cal
18ca0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 l to sqlite3_set
18cb0 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f 76 65 72 _authorizer over
18cc0 72 69 64 65 73 20 74 68 65 0a 2a 2a 20 70 72 65 rides the.** pre
18cd0 76 69 6f 75 73 20 63 61 6c 6c 2e 20 20 44 69 73 vious call. Dis
18ce0 61 62 6c 65 20 74 68 65 20 61 75 74 68 6f 72 69 able the authori
18cf0 7a 65 72 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e zer by installin
18d00 67 20 61 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 g a NULL callbac
18d10 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 k..** The author
18d20 69 7a 65 72 20 69 73 20 64 69 73 61 62 6c 65 64 izer is disabled
18d30 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a by default..**.
18d40 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 ** The authorize
18d50 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 r callback must
18d60 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 not do anything
18d70 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 that will modify
18d80 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
18d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
18da0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 61 75 74 invoked the aut
18db0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
18dc0 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b ..** Note that [
18dd0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
18de0 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 v2()] and [sqlit
18df0 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 e3_step()] both
18e00 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 modify their.**
18e10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
18e20 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 ions for the mea
18e30 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 ning of "modify"
18e40 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 in this paragra
18e50 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b ph..**.** When [
18e60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
18e70 76 32 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f v2()] is used to
18e80 20 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65 prepare a state
18e90 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 ment, the.** sta
18ea0 74 65 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 tement might be
18eb0 72 65 70 72 65 70 61 72 65 64 20 64 75 72 69 6e reprepared durin
18ec0 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 g [sqlite3_step(
18ed0 29 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 )] due to a .**
18ee0 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 schema change.
18ef0 48 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 Hence, the appli
18f00 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e cation should en
18f10 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a sure that the.**
18f20 20 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 correct authori
18f30 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d zer callback rem
18f40 61 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 ains in place du
18f50 72 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 ring the [sqlite
18f60 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 3_step()]..**.**
18f70 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 Note that the a
18f80 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
18f90 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e ck is invoked on
18fa0 6c 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 ly during.** [sq
18fb0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d lite3_prepare()]
18fc0 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 or its variants
18fd0 2e 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e . Authorization
18fe0 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f is not.** perfo
18ff0 72 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 rmed during stat
19000 65 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e ement evaluation
19010 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 in [sqlite3_ste
19020 70 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 p()], unless.**
19030 61 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65 as stated in the
19040 20 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 previous paragr
19050 61 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 aph, sqlite3_ste
19060 70 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 p() invokes.** s
19070 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
19080 32 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65 2() to reprepare
19090 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 a statement aft
190a0 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e er a schema chan
190b0 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ge..**.** Requir
190c0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 ements:.** [H125
190d0 30 31 5d 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 01] [H12502] [H1
190e0 32 35 30 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 2503] [H12504] [
190f0 48 31 32 35 30 35 5d 20 5b 48 31 32 35 30 36 5d H12505] [H12506]
19100 20 5b 48 31 32 35 30 37 5d 20 5b 48 31 32 35 31 [H12507] [H1251
19110 30 5d 0a 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 0].** [H12511] [
19120 48 31 32 35 31 32 5d 20 5b 48 31 32 35 32 30 5d H12512] [H12520]
19130 20 5b 48 31 32 35 32 31 5d 20 5b 48 31 32 35 32 [H12521] [H1252
19140 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 2].*/.SQLITE_API
19150 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 int sqlite3_set
19160 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 _authorizer(. s
19170 71 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 qlite3*,. int (
19180 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e *xAuth)(void*,in
19190 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f t,const char*,co
191a0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
191b0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
191c0 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 *),. void *pUse
191d0 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 rData.);../*.**
191e0 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 CAPI3REF: Author
191f0 69 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 izer Return Code
19200 73 20 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 s {H12590} <H125
19210 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 00>.**.** The [s
19220 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
19230 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a rizer | authoriz
19240 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 er callback func
19250 74 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 tion] must.** re
19260 74 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c turn either [SQL
19270 49 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f ITE_OK] or one o
19280 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 f these two cons
19290 74 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a tants in order.*
192a0 2a 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 * to signal SQLi
192b0 74 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f te whether or no
192c0 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 t the action is
192d0 70 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 permitted. See
192e0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
192f0 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c set_authorizer |
19300 20 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 authorizer docu
19310 6d 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 mentation] for a
19320 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 dditional.** inf
19330 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 ormation..*/.#de
19340 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 fine SQLITE_DENY
19350 20 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 1 /* Abort
19360 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
19370 74 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 t with an error
19380 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
19390 45 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 E_IGNORE 2 /*
193a0 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 Don't allow acce
193b0 73 73 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 ss, but don't ge
193c0 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 nerate an error
193d0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
193e0 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 EF: Authorizer A
193f0 63 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 ction Codes {H12
19400 35 35 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 550} <H12500>.**
19410 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
19420 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 _set_authorizer(
19430 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 )] interface reg
19440 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 isters a callbac
19450 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 k function.** th
19460 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f at is invoked to
19470 20 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 authorize certa
19480 69 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 in SQL statement
19490 20 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a actions. The.*
194a0 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * second paramet
194b0 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 er to the callba
194c0 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ck is an integer
194d0 20 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 code that speci
194e0 66 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 fies.** what act
194f0 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 ion is being aut
19500 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 horized. These
19510 61 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 are the integer
19520 61 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 action codes tha
19530 74 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 t.** the authori
19540 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 zer callback may
19550 20 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a be passed..**.*
19560 2a 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 * These action c
19570 6f 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 ode values signi
19580 66 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 fy what kind of
19590 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 operation is to
195a0 62 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 be.** authorized
195b0 2e 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 . The 3rd and 4
195c0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
195d0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 the authorizati
195e0 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 on.** callback f
195f0 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 unction will be
19600 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 parameters or NU
19610 4c 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 LL depending on
19620 77 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a which of these.*
19630 2a 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 * codes is used
19640 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 as the second pa
19650 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 rameter. The 5t
19660 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
19670 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 he.** authorizer
19680 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 callback is the
19690 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
196a0 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 abase ("main", "
196b0 74 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 temp",.** etc.)
196c0 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 if applicable.
196d0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 The 6th paramete
196e0 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 r to the authori
196f0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 zer callback.**
19700 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
19710 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 he inner-most tr
19720 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 igger or view th
19730 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c at is responsibl
19740 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 e for.** the acc
19750 65 73 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e ess attempt or N
19760 55 4c 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 ULL if this acce
19770 73 73 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 ss attempt is di
19780 72 65 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 rectly from.** t
19790 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 op-level SQL cod
197a0 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 e..**.** Require
197b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 ments:.** [H1255
197c0 31 5d 20 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 1] [H12552] [H12
197d0 35 35 33 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 553] [H12554].*/
197e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
197f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 ************* 3r
19810 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 d ************ 4
19820 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a th ***********/.
19830 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
19840 52 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20 REATE_INDEX
19850 20 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 1 /* Inde
19860 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c x Name Tabl
19870 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
19880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
19890 45 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20 20 EATE_TABLE
198a0 20 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 2 /* Table
198b0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
198c0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
198d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 efine SQLITE_CRE
198e0 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 ATE_TEMP_INDEX
198f0 20 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 3 /* Index
19900 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 Name Table
19910 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
19920 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 fine SQLITE_CREA
19930 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 TE_TEMP_TABLE
19940 20 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 4 /* Table N
19950 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
19960 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
19970 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 ine SQLITE_CREAT
19980 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20 E_TEMP_TRIGGER
19990 20 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 5 /* Trigger
199a0 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 Name Table Na
199b0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
199c0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
199d0 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20 _TEMP_VIEW
199e0 36 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 6 /* View Name
199f0 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 NULL
19a00 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
19a10 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
19a20 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 37 TRIGGER 7
19a30 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 /* Trigger Na
19a40 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 me Table Name
19a50 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
19a60 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 SQLITE_CREATE_V
19a70 49 45 57 20 20 20 20 20 20 20 20 20 20 20 38 20 IEW 8
19a80 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 /* View Name
19a90 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
19aa0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19ab0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20 20 SQLITE_DELETE
19ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 20 20 9
19ad0 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
19ae0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
19af0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
19b00 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 QLITE_DROP_INDEX
19b10 20 20 20 20 20 20 20 20 20 20 20 31 30 20 20 20 10
19b20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 /* Index Name
19b30 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
19b40 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
19b50 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 LITE_DROP_TABLE
19b60 20 20 20 20 20 20 20 20 20 20 31 31 20 20 20 2f 11 /
19b70 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
19b80 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
19b90 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
19ba0 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e ITE_DROP_TEMP_IN
19bb0 44 45 58 20 20 20 20 20 20 31 32 20 20 20 2f 2a DEX 12 /*
19bc0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
19bd0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
19be0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
19bf0 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 TE_DROP_TEMP_TAB
19c00 4c 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 LE 13 /*
19c10 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
19c20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
19c30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
19c40 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 E_DROP_TEMP_TRIG
19c50 47 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20 54 GER 14 /* T
19c60 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 rigger Name T
19c70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
19c80 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
19c90 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 _DROP_TEMP_VIEW
19ca0 20 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69 15 /* Vi
19cb0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 ew Name NU
19cc0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
19cd0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
19ce0 44 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20 DROP_TRIGGER
19cf0 20 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69 16 /* Tri
19d00 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 gger Name Tab
19d10 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
19d20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
19d30 52 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20 ROP_VIEW
19d40 20 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77 17 /* View
19d50 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c Name NULL
19d60 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
19d70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
19d80 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20 SERT
19d90 20 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 18 /* Table
19da0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
19db0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
19dc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 efine SQLITE_PRA
19dd0 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 GMA
19de0 20 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 19 /* Pragma
19df0 20 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61 72 Name 1st ar
19e00 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 g or NULL */.#de
19e10 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 fine SQLITE_READ
19e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e30 20 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 20 /* Table N
19e40 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 ame Column
19e50 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 Name */.#def
19e60 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 ine SQLITE_SELEC
19e70 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
19e80 32 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 21 /* NULL
19e90 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 NULL
19ea0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
19eb0 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 ne SQLITE_TRANSA
19ec0 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 32 CTION 2
19ed0 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 2 /* Operation
19ee0 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 NULL
19ef0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
19f00 65 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 e SQLITE_UPDATE
19f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 23
19f20 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
19f30 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d Column Nam
19f40 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
19f50 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 20 SQLITE_ATTACH
19f60 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34 20 24
19f70 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 /* Filename
19f80 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
19f90 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19fa0 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20 20 SQLITE_DETACH
19fb0 20 20 20 20 20 20 20 20 20 20 20 20 32 35 20 20 25
19fc0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d /* Database Nam
19fd0 65 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 e NULL
19fe0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
19ff0 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c QLITE_ALTER_TABL
1a000 45 20 20 20 20 20 20 20 20 20 20 32 36 20 20 20 E 26
1a010 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 /* Database Name
1a020 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1a030 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1a040 4c 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20 20 LITE_REINDEX
1a050 20 20 20 20 20 20 20 20 20 20 32 37 20 20 20 2f 27 /
1a060 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
1a070 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1a080 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1a090 49 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 ITE_ANALYZE
1a0a0 20 20 20 20 20 20 20 20 20 32 38 20 20 20 2f 2a 28 /*
1a0b0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1a0c0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1a0d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1a0e0 54 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 TE_CREATE_VTABLE
1a0f0 20 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a 20 29 /*
1a100 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1a110 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 Module Name
1a120 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1a130 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 E_DROP_VTABLE
1a140 20 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20 54 30 /* T
1a150 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d able Name M
1a160 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a odule Name *
1a170 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1a180 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 _FUNCTION
1a190 20 20 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 31 /* NU
1a1a0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 46 75 LL Fu
1a1b0 6e 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f nction Name */
1a1c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1a1d0 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 SAVEPOINT
1a1e0 20 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 32 /* Ope
1a1f0 72 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76 ration Sav
1a200 65 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a epoint Name */.
1a210 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1a220 4f 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20 OPY
1a230 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 0 /* No l
1a240 6f 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f onger used */../
1a250 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
1a260 72 61 63 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 racing And Profi
1a270 6c 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b ling Functions {
1a280 48 31 32 32 38 30 7d 20 3c 53 36 30 34 30 30 3e H12280} <S60400>
1a290 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
1a2a0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
1a2b0 74 69 6e 65 73 20 72 65 67 69 73 74 65 72 20 63 tines register c
1a2c0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
1a2d0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 s that can be us
1a2e0 65 64 20 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e ed for.** tracin
1a2f0 67 20 61 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 g and profiling
1a300 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 the execution of
1a310 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
1a320 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 .**.** The callb
1a330 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 ack function reg
1a340 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 istered by sqlit
1a350 65 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e e3_trace() is in
1a360 76 6f 6b 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 voked at.** vari
1a370 6f 75 73 20 74 69 6d 65 73 20 77 68 65 6e 20 61 ous times when a
1a380 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1a390 69 73 20 62 65 69 6e 67 20 72 75 6e 20 62 79 20 is being run by
1a3a0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1a3b0 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 ..** The callbac
1a3c0 6b 20 72 65 74 75 72 6e 73 20 61 20 55 54 46 2d k returns a UTF-
1a3d0 38 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 8 rendering of t
1a3e0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1a3f0 20 74 65 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 text.** as the
1a400 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 statement first
1a410 62 65 67 69 6e 73 20 65 78 65 63 75 74 69 6e 67 begins executing
1a420 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 . Additional ca
1a430 6c 6c 62 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a llbacks occur.**
1a440 20 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72 as each trigger
1a450 65 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73 ed subprogram is
1a460 20 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63 entered. The c
1a470 61 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69 allbacks for tri
1a480 67 67 65 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e ggers.** contain
1a490 20 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d a UTF-8 SQL com
1a4a0 6d 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69 ment that identi
1a4b0 66 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72 fies the trigger
1a4c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
1a4d0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 back function re
1a4e0 67 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 gistered by sqli
1a4f0 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 te3_profile() is
1a500 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 invoked.** as e
1a510 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ach SQL statemen
1a520 74 20 66 69 6e 69 73 68 65 73 2e 20 20 54 68 65 t finishes. The
1a530 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 profile callbac
1a540 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 k contains.** th
1a550 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 e original state
1a560 6d 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 6e ment text and an
1a570 20 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 6c estimate of wal
1a580 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 l-clock time.**
1a590 6f 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 of how long that
1a5a0 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 statement took
1a5b0 74 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 to run..**.** Re
1a5c0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
1a5d0 48 31 32 32 38 31 5d 20 5b 48 31 32 32 38 32 5d H12281] [H12282]
1a5e0 20 5b 48 31 32 32 38 33 5d 20 5b 48 31 32 32 38 [H12283] [H1228
1a5f0 34 5d 20 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 4] [H12285] [H12
1a600 32 38 37 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 287] [H12288] [H
1a610 31 32 32 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 12289].** [H1229
1a620 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 0].*/.SQLITE_API
1a630 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
1a640 4e 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 NTAL void *sqlit
1a650 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 e3_trace(sqlite3
1a660 2a 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 65 29 *, void(*xTrace)
1a670 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
1a680 72 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c r*), void*);.SQL
1a690 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
1a6a0 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 XPERIMENTAL void
1a6b0 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c *sqlite3_profil
1a6c0 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 e(sqlite3*,. v
1a6d0 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 oid(*xProfile)(v
1a6e0 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a oid*,const char*
1a6f0 2c 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 ,sqlite3_uint64)
1a700 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a , void*);../*.**
1a710 20 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79 CAPI3REF: Query
1a720 20 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 Progress Callba
1a730 63 6b 73 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 cks {H12910} <S6
1a740 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0400>.**.** This
1a750 20 72 6f 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 routine configu
1a760 72 65 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 res a callback f
1a770 75 6e 63 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a unction - the.**
1a780 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
1a790 63 6b 20 2d 20 74 68 61 74 20 69 73 20 69 6e 76 ck - that is inv
1a7a0 6f 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c oked periodicall
1a7b0 79 20 64 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a y during long.**
1a7c0 20 72 75 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 running calls t
1a7d0 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 o [sqlite3_exec(
1a7e0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 )], [sqlite3_ste
1a7f0 70 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c p()] and.** [sql
1a800 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 ite3_get_table()
1a810 5d 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 ]. An example u
1a820 73 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 se for this.** i
1a830 6e 74 65 72 66 61 63 65 20 69 73 20 74 6f 20 6b nterface is to k
1a840 65 65 70 20 61 20 47 55 49 20 75 70 64 61 74 65 eep a GUI update
1a850 64 20 64 75 72 69 6e 67 20 61 20 6c 61 72 67 65 d during a large
1a860 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 query..**.** If
1a870 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 the progress ca
1a880 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e llback returns n
1a890 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 on-zero, the ope
1a8a0 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 ration is.** int
1a8b0 65 72 72 75 70 74 65 64 2e 20 20 54 68 69 73 20 errupted. This
1a8c0 66 65 61 74 75 72 65 20 63 61 6e 20 62 65 20 75 feature can be u
1a8d0 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
1a8e0 20 61 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 a.** "Cancel" b
1a8f0 75 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 utton on a GUI p
1a900 72 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62 rogress dialog b
1a910 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 ox..**.** The pr
1a920 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d ogress handler m
1a930 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 ust not do anyth
1a940 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f ing that will mo
1a950 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 dify.** the data
1a960 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1a970 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
1a980 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 progress handle
1a990 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 r..** Note that
1a9a0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
1a9b0 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 _v2()] and [sqli
1a9c0 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 te3_step()] both
1a9d0 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a modify their.**
1a9e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1a9f0 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 tions for the me
1aa00 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 aning of "modify
1aa10 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 " in this paragr
1aa20 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 aph..**.** Requi
1aa30 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
1aa40 39 31 31 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 911] [H12912] [H
1aa50 31 32 39 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 12913] [H12914]
1aa60 5b 48 31 32 39 31 35 5d 20 5b 48 31 32 39 31 36 [H12915] [H12916
1aa70 5d 20 5b 48 31 32 39 31 37 5d 20 5b 48 31 32 39 ] [H12917] [H129
1aa80 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 18].**.*/.SQLITE
1aa90 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1aaa0 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 3_progress_handl
1aab0 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 er(sqlite3*, int
1aac0 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c , int(*)(void*),
1aad0 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 void*);../*.**
1aae0 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e CAPI3REF: Openin
1aaf0 67 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 g A New Database
1ab00 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 Connection {H12
1ab10 37 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 700} <S40200>.**
1ab20 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
1ab30 65 73 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 es open an SQLit
1ab40 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1ab50 77 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 67 69 whose name is gi
1ab60 76 65 6e 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 ven by the.** fi
1ab70 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e lename argument.
1ab80 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 The filename ar
1ab90 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70 gument is interp
1aba0 72 65 74 65 64 20 61 73 20 55 54 46 2d 38 20 66 reted as UTF-8 f
1abb0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 or.** sqlite3_op
1abc0 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 en() and sqlite3
1abd0 5f 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 _open_v2() and a
1abe0 73 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 s UTF-16 in the
1abf0 6e 61 74 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f native byte.** o
1ac00 72 64 65 72 20 66 6f 72 20 73 71 6c 69 74 65 33 rder for sqlite3
1ac10 5f 6f 70 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 _open16(). A [da
1ac20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1ac30 6e 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75 n] handle is usu
1ac40 61 6c 6c 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 ally.** returned
1ac50 20 69 6e 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 in *ppDb, even
1ac60 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
1ac70 72 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 rs. The only ex
1ac80 63 65 70 74 69 6f 6e 20 69 73 20 74 68 61 74 0a ception is that.
1ac90 2a 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 ** if SQLite is
1aca0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 unable to alloca
1acb0 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c te memory to hol
1acc0 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 d the [sqlite3]
1acd0 6f 62 6a 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c object,.** a NUL
1ace0 4c 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 L will be writte
1acf0 6e 20 69 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 n into *ppDb ins
1ad00 74 65 61 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 tead of a pointe
1ad10 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 r to the [sqlite
1ad20 33 5d 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 3].** object. If
1ad30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1ad40 20 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 opened (and/or
1ad50 63 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 created) success
1ad60 66 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b fully, then.** [
1ad70 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 SQLITE_OK] is re
1ad80 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 turned. Otherwi
1ad90 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 se an [error cod
1ada0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 e] is returned.
1adb0 20 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 The.** [sqlite3
1adc0 5f 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 _errmsg()] or [s
1add0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
1ade0 29 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 )] routines can
1adf0 62 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 be used to obtai
1ae00 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 n.** an English
1ae10 6c 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70 language descrip
1ae20 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f tion of the erro
1ae30 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 r..**.** The def
1ae40 61 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f ault encoding fo
1ae50 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 r the database w
1ae60 69 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a ill be UTF-8 if.
1ae70 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ** sqlite3_open(
1ae80 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 ) or sqlite3_ope
1ae90 6e 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 n_v2() is called
1aea0 20 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 and.** UTF-16 i
1aeb0 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 n the native byt
1aec0 65 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 e order if sqlit
1aed0 65 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 e3_open16() is u
1aee0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 sed..**.** Wheth
1aef0 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 er or not an err
1af00 6f 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 or occurs when i
1af10 74 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 t is opened, res
1af20 6f 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 ources.** associ
1af30 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
1af40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1af50 6f 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c on] handle shoul
1af60 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 d be released by
1af70 0a 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 .** passing it t
1af80 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 o [sqlite3_close
1af90 28 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e ()] when it is n
1afa0 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 o longer require
1afb0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c d..**.** The sql
1afc0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 ite3_open_v2() i
1afd0 6e 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c nterface works l
1afe0 69 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e ike sqlite3_open
1aff0 28 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 ().** except tha
1b000 74 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f t it accepts two
1b010 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 additional para
1b020 6d 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 meters for addit
1b030 69 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a ional control.**
1b040 20 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 over the new da
1b050 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1b060 6e 2e 20 20 54 68 65 20 66 6c 61 67 73 20 70 61 n. The flags pa
1b070 72 61 6d 65 74 65 72 20 63 61 6e 20 74 61 6b 65 rameter can take
1b080 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 one of.** the f
1b090 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 76 ollowing three v
1b0a0 61 6c 75 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c alues, optionall
1b0b0 79 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 y combined with
1b0c0 74 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the .** [SQLITE_
1b0d0 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72 OPEN_NOMUTEX] or
1b0e0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 [SQLITE_OPEN_FU
1b0f0 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 73 3a 0a LLMUTEX] flags:.
1b100 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
1b110 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 t>[SQLITE_OPEN_R
1b120 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a EADONLY]</dt>.**
1b130 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 <dd>The databas
1b140 65 20 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 e is opened in r
1b150 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 ead-only mode.
1b160 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1b170 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 does not.** alre
1b180 61 64 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 ady exist, an er
1b190 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ror is returned.
1b1a0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1b1b0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 [SQLITE_OPEN_REA
1b1c0 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 DWRITE]</dt>.**
1b1d0 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 <dd>The database
1b1e0 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 is opened for r
1b1f0 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 eading and writi
1b200 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 ng if possible,
1b210 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e or reading.** on
1b220 6c 79 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 ly if the file i
1b230 73 20 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 s write protecte
1b240 64 20 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 d by the operati
1b250 6e 67 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65 ng system. In e
1b260 69 74 68 65 72 0a 2a 2a 20 63 61 73 65 20 74 68 ither.** case th
1b270 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
1b280 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f already exist, o
1b290 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f therwise an erro
1b2a0 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f r is returned.</
1b2b0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 dd>.**.** <dt>[S
1b2c0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
1b2d0 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f RITE] | [SQLITE_
1b2e0 4f 50 45 4e 5f 43 52 45 41 54 45 5d 3c 2f 64 74 OPEN_CREATE]</dt
1b2f0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 >.** <dd>The dat
1b300 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 abase is opened
1b310 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 for reading and
1b320 77 72 69 74 69 6e 67 2c 20 61 6e 64 20 69 73 20 writing, and is
1b330 63 72 65 61 74 65 73 20 69 74 20 69 66 0a 2a 2a creates it if.**
1b340 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 it does not alr
1b350 65 61 64 79 20 65 78 69 73 74 2e 20 54 68 69 73 eady exist. This
1b360 20 69 73 20 74 68 65 20 62 65 68 61 76 69 6f 72 is the behavior
1b370 20 74 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 that is always
1b380 75 73 65 64 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 used for.** sqli
1b390 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
1b3a0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e qlite3_open16().
1b3b0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a </dd>.** </dl>.*
1b3c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 *.** If the 3rd
1b3d0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
1b3e0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 ite3_open_v2() i
1b3f0 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65 s not one of the
1b400 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 .** combinations
1b410 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 6f 72 20 shown above or
1b420 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6d 62 69 one of the combi
1b430 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 nations shown ab
1b440 6f 76 65 20 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 ove combined.**
1b450 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 with the [SQLITE
1b460 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f _OPEN_NOMUTEX] o
1b470 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 r [SQLITE_OPEN_F
1b480 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 73 2c ULLMUTEX] flags,
1b490 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 .** then the beh
1b4a0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
1b4b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
1b4c0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f [SQLITE_OPEN_NO
1b4d0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73 MUTEX] flag is s
1b4e0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 et, then the dat
1b4f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1b500 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 65 .** opens in the
1b510 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b 74 multi-thread [t
1b520 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 61 hreading mode] a
1b530 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 69 s long as the si
1b540 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 6d ngle-thread.** m
1b550 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ode has not been
1b560 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d set at compile-
1b570 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69 time or start-ti
1b580 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 5b me. If the.** [
1b590 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c SQLITE_OPEN_FULL
1b5a0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73 MUTEX] flag is s
1b5b0 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 et then the data
1b5c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1b5d0 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 opens.** in the
1b5e0 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 serialized [thre
1b5f0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c 65 ading mode] unle
1b600 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 ss single-thread
1b610 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
1b620 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 ly selected at c
1b630 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 ompile-time or s
1b640 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a tart-time..**.**
1b650 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 If the filename
1b660 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 is ":memory:",
1b670 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 then a private,
1b680 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d temporary in-mem
1b690 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ory database.**
1b6a0 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 is created for t
1b6b0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 he connection.
1b6c0 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 This in-memory d
1b6d0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e atabase will van
1b6e0 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ish when.** the
1b6f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1b700 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 ion is closed.
1b710 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
1b720 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a of SQLite might.
1b730 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 ** make use of a
1b740 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 dditional specia
1b750 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 l filenames that
1b760 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 begin with the
1b770 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a ":" character..*
1b780 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e * It is recommen
1b790 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 ded that when a
1b7a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d database filenam
1b7b0 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 e actually does
1b7c0 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 begin with.** a
1b7d0 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f ":" character yo
1b7e0 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 u should prefix
1b7f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 the filename wit
1b800 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 h a pathname suc
1b810 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 h as.** "./" to
1b820 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e avoid ambiguity.
1b830 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1b840 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 lename is an emp
1b850 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 ty string, then
1b860 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f a private, tempo
1b870 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 rary.** on-disk
1b880 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 database will be
1b890 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 created. This
1b8a0 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65 private database
1b8b0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f will be.** auto
1b8c0 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
1b8d0 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 d as soon as the
1b8e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1b8f0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a tion is closed..
1b900 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 **.** The fourth
1b910 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
1b920 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 lite3_open_v2()
1b930 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
1b940 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 he.** [sqlite3_v
1b950 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 fs] object that
1b960 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 defines the oper
1b970 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
1b980 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 erface that.** t
1b990 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 he new database
1b9a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c connection shoul
1b9b0 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 d use. If the f
1b9c0 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 ourth parameter
1b9d0 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 is.** a NULL poi
1b9e0 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 nter then the de
1b9f0 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 fault [sqlite3_v
1ba00 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 fs] object is us
1ba10 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 ed..**.** <b>Not
1ba20 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 e to Windows use
1ba30 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 rs:</b> The enc
1ba40 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 oding used for t
1ba50 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 he filename argu
1ba60 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 ment.** of sqlit
1ba70 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 e3_open() and sq
1ba80 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 lite3_open_v2()
1ba90 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e must be UTF-8, n
1baa0 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 ot whatever.** c
1bab0 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 odepage is curre
1bac0 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 ntly defined. F
1bad0 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e ilenames contain
1bae0 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 ing internationa
1baf0 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 l.** characters
1bb00 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 must be converte
1bb10 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 d to UTF-8 prior
1bb20 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d to passing them
1bb30 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 into.** sqlite3
1bb40 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
1bb50 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a e3_open_v2()..**
1bb60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1bb70 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 :.** [H12701] [H
1bb80 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 12702] [H12703]
1bb90 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 [H12704] [H12706
1bba0 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 ] [H12707] [H127
1bbb0 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 09] [H12711].**
1bbc0 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 [H12712] [H12713
1bbd0 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 ] [H12714] [H127
1bbe0 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 17] [H12719] [H1
1bbf0 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2721] [H12723].*
1bc00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1bc10 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 sqlite3_open(.
1bc20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
1bc30 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 ename, /* Data
1bc40 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 base filename (U
1bc50 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 TF-8) */. sqlit
1bc60 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 e3 **ppDb
1bc70 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 /* OUT: SQLit
1bc80 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 e db handle */.)
1bc90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
1bca0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
1bcb0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 . const void *f
1bcc0 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 ilename, /* Da
1bcd0 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 tabase filename
1bce0 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 (UTF-16) */. sq
1bcf0 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 lite3 **ppDb
1bd00 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 /* OUT: SQ
1bd10 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a Lite db handle *
1bd20 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
1bd30 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
1bd40 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 _v2(. const cha
1bd50 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f r *filename, /
1bd60 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e * Database filen
1bd70 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 ame (UTF-8) */.
1bd80 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c sqlite3 **ppDb,
1bd90 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1bda0 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c SQLite db handl
1bdb0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
1bdc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1bdd0 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e * Flags */. con
1bde0 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 st char *zVfs
1bdf0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1be00 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 VFS module to us
1be10 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 e */.);../*.** C
1be20 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 API3REF: Error C
1be30 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 odes And Message
1be40 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 s {H12800} <S602
1be50 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
1be60 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 lite3_errcode()
1be70 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
1be80 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 s the numeric [r
1be90 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a esult code] or.*
1bea0 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
1beb0 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 lt code] for the
1bec0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 most recent fai
1bed0 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 led sqlite3_* AP
1bee0 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 I call.** associ
1bef0 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 ated with a [dat
1bf00 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1bf10 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 ]. If a prior AP
1bf20 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a I call failed.**
1bf30 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 but the most re
1bf40 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 cent API call su
1bf50 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 cceeded, the ret
1bf60 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a urn value from.*
1bf70 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 * sqlite3_errcod
1bf80 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 e() is undefined
1bf90 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 . The sqlite3_e
1bfa0 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 xtended_errcode(
1bfb0 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 ).** interface i
1bfc0 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 s the same excep
1bfd0 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 t that it always
1bfe0 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a returns the .**
1bff0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c [extended resul
1c000 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 t code] even whe
1c010 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c n extended resul
1c020 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 t codes are.** d
1c030 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 isabled..**.** T
1c040 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 he sqlite3_errms
1c050 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
1c060 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 errmsg16() retur
1c070 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 n English-langua
1c080 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 ge.** text that
1c090 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 describes the er
1c0a0 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 ror, as either U
1c0b0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 TF-8 or UTF-16 r
1c0c0 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 espectively..**
1c0d0 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 Memory to hold t
1c0e0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
1c0f0 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 string is manag
1c100 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a ed internally..*
1c110 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
1c120 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
1c130 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 to worry about f
1c140 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c reeing the resul
1c150 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 t..** However, t
1c160 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 he error string
1c170 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 might be overwri
1c180 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 tten or dealloca
1c190 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 ted by.** subseq
1c1a0 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 uent calls to ot
1c1b0 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 her SQLite inter
1c1c0 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a face functions..
1c1d0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 **.** When the s
1c1e0 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 erialized [threa
1c1f0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e ding mode] is in
1c200 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 use, it might b
1c210 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 e the.** case th
1c220 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f at a second erro
1c230 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 r occurs on a se
1c240 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e parate thread in
1c250 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 between.** the
1c260 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 time of the firs
1c270 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 t error and the
1c280 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e call to these in
1c290 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 terfaces..** Whe
1c2a0 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 n that happens,
1c2b0 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 the second error
1c2c0 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 will be reporte
1c2d0 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a d since these.**
1c2e0 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 interfaces alwa
1c2f0 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f ys report the mo
1c300 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 st recent result
1c310 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 . To avoid.** t
1c320 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 his, each thread
1c330 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c can obtain excl
1c340 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 usive use of the
1c350 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
1c360 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 ction] D.** by i
1c370 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 nvoking [sqlite3
1c380 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 _mutex_enter]([s
1c390 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d qlite3_db_mutex]
1c3a0 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 (D)) before begi
1c3b0 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 nning.** to use
1c3c0 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b D and invoking [
1c3d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1c3e0 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 ave]([sqlite3_db
1c3f0 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 _mutex](D)) afte
1c400 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 r.** all calls t
1c410 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 o the interfaces
1c420 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65 listed here are
1c430 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a completed..**.*
1c440 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 * If an interfac
1c450 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c e fails with SQL
1c460 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 ITE_MISUSE, that
1c470 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 means the inter
1c480 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f face.** was invo
1c490 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 ked incorrectly
1c4a0 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 by the applicati
1c4b0 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 on. In that cas
1c4c0 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 e, the.** error
1c4d0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 code and message
1c4e0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
1c4f0 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 be set..**.** Re
1c500 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
1c510 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d H12801] [H12802]
1c520 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 [H12803] [H1280
1c530 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 7] [H12808] [H12
1c540 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 809].*/.SQLITE_A
1c550 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
1c560 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a rrcode(sqlite3 *
1c570 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 db);.SQLITE_API
1c580 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 int sqlite3_exte
1c590 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c nded_errcode(sql
1c5a0 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 ite3 *db);.SQLIT
1c5b0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1c5c0 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 *sqlite3_errmsg
1c5d0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
1c5e0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1c5f0 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 d *sqlite3_errms
1c600 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a g16(sqlite3*);..
1c610 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1c620 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 SQL Statement Ob
1c630 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 ject {H13000} <H
1c640 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 13010>.** KEYWOR
1c650 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 DS: {prepared st
1c660 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 atement} {prepar
1c670 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a ed statements}.*
1c680 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
1c690 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
1c6a0 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e represents a sin
1c6b0 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e gle SQL statemen
1c6c0 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 t..** This objec
1c6d0 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b t is variously k
1c6e0 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 nown as a "prepa
1c6f0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f red statement" o
1c700 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 r a.** "compiled
1c710 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 SQL statement"
1c720 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 or simply as a "
1c730 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a statement"..**.*
1c740 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 * The life of a
1c750 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 statement object
1c760 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 goes something
1c770 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
1c780 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 <ol>.** <li> Cr
1c790 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 eate the object
1c7a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 using [sqlite3_p
1c7b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
1c7c0 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 a related.**
1c7d0 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c function..** <
1c7e0 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 li> Bind values
1c7f0 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 to [host paramet
1c800 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 ers] using the s
1c810 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a qlite3_bind_*().
1c820 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 ** interfac
1c830 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 es..** <li> Run
1c840 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 the SQL by calli
1c850 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ng [sqlite3_step
1c860 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ()] one or more
1c870 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 times..** <li> R
1c880 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 eset the stateme
1c890 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 nt using [sqlite
1c8a0 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 3_reset()] then
1c8b0 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 go back.**
1c8c0 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 to step 2. Do t
1c8d0 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 his zero or more
1c8e0 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 times..** <li>
1c8f0 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 Destroy the obje
1c900 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ct using [sqlite
1c910 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 3_finalize()]..*
1c920 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 * </ol>.**.** Re
1c930 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 fer to documenta
1c940 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 tion on individu
1c950 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 al methods above
1c960 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a for additional.
1c970 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a ** information..
1c980 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
1c990 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 t sqlite3_stmt s
1c9a0 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a qlite3_stmt;../*
1c9b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
1c9c0 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 n-time Limits {H
1c9d0 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 12760} <S20600>.
1c9e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
1c9f0 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 face allows the
1ca00 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 size of various
1ca10 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 constructs to be
1ca20 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 limited.** on a
1ca30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 connection by c
1ca40 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e onnection basis.
1ca50 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 The first para
1ca60 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 meter is the.**
1ca70 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
1ca80 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 tion] whose limi
1ca90 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f t is to be set o
1caa0 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a r queried. The.
1cab0 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 ** second parame
1cac0 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 ter is one of th
1cad0 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 e [limit categor
1cae0 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 ies] that define
1caf0 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 a.** class of c
1cb00 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 onstructs to be
1cb10 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 size limited. T
1cb20 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
1cb30 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 er is the.** new
1cb40 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 limit for that
1cb50 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 construct. The
1cb60 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1cb70 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a the old limit..
1cb80 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 **.** If the new
1cb90 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 limit is a nega
1cba0 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 tive number, the
1cbb0 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e limit is unchan
1cbc0 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 ged..** For the
1cbd0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f limit category o
1cbe0 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 f SQLITE_LIMIT_X
1cbf0 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a YZ there is a .*
1cc00 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 * [limits | hard
1cc10 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a upper bound].**
1cc20 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c set by a compil
1cc30 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 e-time C preproc
1cc40 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 essor macro name
1cc50 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 d .** [limits |
1cc60 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e SQLITE_MAX_XYZ].
1cc70 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 .** (The "_LIMIT
1cc80 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 _" in the name i
1cc90 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d s changed to "_M
1cca0 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 AX_".).** Attemp
1ccb0 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 ts to increase a
1ccc0 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 limit above its
1ccd0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e hard upper boun
1cce0 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c d are.** silentl
1ccf0 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 y truncated to t
1cd00 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 he hard upper li
1cd10 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 mit..**.** Run t
1cd20 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 ime limits are i
1cd30 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
1cd40 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 in applications
1cd50 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 that manage.** b
1cd60 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e oth their own in
1cd70 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 ternal database
1cd80 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 and also databas
1cd90 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 es that are cont
1cda0 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 rolled.** by unt
1cdb0 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 rusted external
1cdc0 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 sources. An exa
1cdd0 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e mple application
1cde0 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 might be a.** w
1cdf0 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 eb browser that
1ce00 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 has its own data
1ce10 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e bases for storin
1ce20 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a g history and.**
1ce30 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61 separate databa
1ce40 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 ses controlled b
1ce50 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 y JavaScript app
1ce60 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f lications downlo
1ce70 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 aded.** off the
1ce80 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 Internet. The i
1ce90 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 nternal database
1cea0 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 s can be given t
1ceb0 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 he.** large, def
1cec0 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 ault limits. Da
1ced0 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 tabases managed
1cee0 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 by external sour
1cef0 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 ces can.** be gi
1cf00 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 ven much smaller
1cf10 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 limits designed
1cf20 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 to prevent a de
1cf30 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a nial of service.
1cf40 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 ** attack. Deve
1cf50 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 lopers might als
1cf60 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 o want to use th
1cf70 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 e [sqlite3_set_a
1cf80 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 uthorizer()].**
1cf90 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 interface to fur
1cfa0 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 ther control unt
1cfb0 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 rusted SQL. The
1cfc0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
1cfd0 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 abase.** created
1cfe0 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 by an untrusted
1cff0 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 script can be c
1d000 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 ontained using t
1d010 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f he.** [max_page_
1d020 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e count] [PRAGMA].
1d030 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 .**.** New run-t
1d040 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f ime limit catego
1d050 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 ries may be adde
1d060 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
1d070 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ases..**.** Requ
1d080 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
1d090 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 2762] [H12766] [
1d0a0 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 H12769].*/.SQLIT
1d0b0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1d0c0 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 3_limit(sqlite3*
1d0d0 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 , int id, int ne
1d0e0 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 wVal);../*.** CA
1d0f0 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 PI3REF: Run-Time
1d100 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 Limit Categorie
1d110 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 s {H12790} <H127
1d120 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 60>.** KEYWORDS:
1d130 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 {limit category
1d140 7d 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 } {limit categor
1d150 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ies}.**.** These
1d160 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e constants defin
1d170 65 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 e various perfor
1d180 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 mance limits.**
1d190 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 that can be lowe
1d1a0 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 red at run-time
1d1b0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c using [sqlite3_l
1d1c0 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 imit()]..** The
1d1d0 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 synopsis of the
1d1e0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 meanings of the
1d1f0 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 various limits i
1d200 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a s shown below..*
1d210 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 * Additional inf
1d220 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 ormation is avai
1d230 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 lable at [limits
1d240 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c | Limits in SQL
1d250 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e ite]..**.** <dl>
1d260 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1d270 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e IMIT_LENGTH</dt>
1d280 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1d290 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 mum size of any
1d2a0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f string or BLOB o
1d2b0 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e r table row.<dd>
1d2c0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
1d2d0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 E_LIMIT_SQL_LENG
1d2e0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1d2f0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
1d300 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 h of an SQL stat
1d310 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ement.</dd>.**.*
1d320 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1d330 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a IT_COLUMN</dt>.*
1d340 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1d350 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 m number of colu
1d360 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 mns in a table d
1d370 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 efinition or in
1d380 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
1d390 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
1d3a0 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e or the maximum n
1d3b0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1d3c0 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 in an index.**
1d3d0 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 or in an ORDER B
1d3e0 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
1d3f0 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ause.</dd>.**.**
1d400 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1d410 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 T_EXPR_DEPTH</dt
1d420 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1d430 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 imum depth of th
1d440 65 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 e parse tree on
1d450 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c any expression.<
1d460 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1d470 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 QLITE_LIMIT_COMP
1d480 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e OUND_SELECT</dt>
1d490 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1d4a0 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 mum number of te
1d4b0 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e rms in a compoun
1d4c0 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 d SELECT stateme
1d4d0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c nt.</dd>.**.** <
1d4e0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1d4f0 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 VDBE_OP</dt>.**
1d500 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1d510 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 number of instru
1d520 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 ctions in a virt
1d530 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 ual machine prog
1d540 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 ram.** used to i
1d550 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 mplement an SQL
1d560 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a statement.</dd>.
1d570 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1d580 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f _LIMIT_FUNCTION_
1d590 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e ARG</dt>.** <dd>
1d5a0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1d5b0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1d5c0 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f on a function.</
1d5d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
1d5e0 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 LITE_LIMIT_ATTAC
1d5f0 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e HED</dt>.** <dd>
1d600 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1d610 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 er of [ATTACH |
1d620 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1d630 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 es].</dd>.**.**
1d640 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1d650 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
1d660 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
1d670 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e >The maximum len
1d680 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 gth of the patte
1d690 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rn argument to t
1d6a0 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 he [LIKE] or.**
1d6b0 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 [GLOB] operators
1d6c0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1d6d0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 >SQLITE_LIMIT_VA
1d6e0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 RIABLE_NUMBER</d
1d6f0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1d700 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1d710 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 variables in an
1d720 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 SQL statement th
1d730 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 at can.** be bou
1d740 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c nd.</dd>.** </dl
1d750 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c >.*/.#define SQL
1d760 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
1d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d780 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
1d790 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c LITE_LIMIT_SQL_L
1d7a0 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 ENGTH
1d7b0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 1.#define S
1d7c0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 QLITE_LIMIT_COLU
1d7d0 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MN
1d7e0 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 2.#define
1d7f0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 SQLITE_LIMIT_EXP
1d800 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 R_DEPTH
1d810 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
1d820 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f SQLITE_LIMIT_CO
1d830 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 MPOUND_SELECT
1d840 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 4.#defin
1d850 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 e SQLITE_LIMIT_V
1d860 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 DBE_OP
1d870 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
1d880 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1d890 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 FUNCTION_ARG
1d8a0 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 6.#def
1d8b0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1d8c0 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 _ATTACHED
1d8d0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 7.#de
1d8e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1d8f0 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c T_LIKE_PATTERN_L
1d900 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 ENGTH 8.#d
1d910 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1d920 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
1d930 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a ER 9..
1d940 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1d950 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c Compiling An SQL
1d960 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 Statement {H130
1d970 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 10} <S10000>.**
1d980 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 KEYWORDS: {SQL s
1d990 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 tatement compile
1d9a0 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 r}.**.** To exec
1d9b0 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 ute an SQL query
1d9c0 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 , it must first
1d9d0 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f be compiled into
1d9e0 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 a byte-code.**
1d9f0 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e program using on
1da00 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 e of these routi
1da10 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 nes..**.** The f
1da20 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 irst argument, "
1da30 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 db", is a [datab
1da40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
1da50 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a obtained from a.
1da60 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 73 73 ** prior success
1da70 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ful call to [sql
1da80 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 ite3_open()], [s
1da90 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1daa0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
1dab0 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 68 65 _open16()]. The
1dac0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1dad0 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 68 61 tion must not ha
1dae0 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 2e 0a ve been closed..
1daf0 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
1db00 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c argument, "zSql
1db10 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 65 6d ", is the statem
1db20 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c ent to be compil
1db30 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 ed, encoded.** a
1db40 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f s either UTF-8 o
1db50 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 20 73 r UTF-16. The s
1db60 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
1db70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 and sqlite3_pre
1db80 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 pare_v2().** int
1db90 65 72 66 61 63 65 73 20 75 73 65 20 55 54 46 2d erfaces use UTF-
1dba0 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 8, and sqlite3_p
1dbb0 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 20 73 repare16() and s
1dbc0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
1dbd0 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 _v2().** use UTF
1dbe0 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 -16..**.** If th
1dbf0 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 e nByte argument
1dc00 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
1dc10 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 ro, then zSql is
1dc20 20 72 65 61 64 20 75 70 20 74 6f 20 74 68 65 0a read up to the.
1dc30 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 ** first zero te
1dc40 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 rminator. If nBy
1dc50 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 te is non-negati
1dc60 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 ve, then it is t
1dc70 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 he maximum.** nu
1dc80 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73 20 72 mber of bytes r
1dc90 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 ead from zSql.
1dca0 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f When nByte is no
1dcb0 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0a n-negative, the.
1dcc0 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 ** zSql string e
1dcd0 6e 64 73 20 61 74 20 65 69 74 68 65 72 20 74 68 nds at either th
1dce0 65 20 66 69 72 73 74 20 27 5c 30 30 30 27 20 6f e first '\000' o
1dcf0 72 20 27 5c 75 30 30 30 30 27 20 63 68 61 72 61 r '\u0000' chara
1dd00 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e cter or.** the n
1dd10 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20 77 68 Byte-th byte, wh
1dd20 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 ichever comes fi
1dd30 72 73 74 2e 20 49 66 20 74 68 65 20 63 61 6c 6c rst. If the call
1dd40 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 er knows.** that
1dd50 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 74 the supplied st
1dd60 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d ring is nul-term
1dd70 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74 68 65 inated, then the
1dd80 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a re is a small.**
1dd90 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 performance adv
1dda0 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67 61 69 antage to be gai
1ddb0 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 61 ned by passing a
1ddc0 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65 74 65 n nByte paramete
1ddd0 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 r that.** is equ
1dde0 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 al to the number
1ddf0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
1de00 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 3c 69 input string <i
1de10 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a >including</i>.*
1de20 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e * the nul-termin
1de30 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ator bytes..**.*
1de40 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73 20 6e * If pzTail is n
1de50 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a ot NULL then *pz
1de60 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 Tail is made to
1de70 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 point to the fir
1de80 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73 74 20 st byte.** past
1de90 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 the end of the f
1dea0 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 irst SQL stateme
1deb0 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65 nt in zSql. The
1dec0 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 se routines only
1ded0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 .** compile the
1dee0 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20 first statement
1def0 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 in zSql, so *pzT
1df00 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f 69 6e ail is left poin
1df10 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61 74 20 ting to.** what
1df20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70 69 6c remains uncompil
1df30 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d ed..**.** *ppStm
1df40 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 t is left pointi
1df50 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 ng to a compiled
1df60 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
1df70 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e 20 62 ment] that can b
1df80 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 75 73 e.** executed us
1df90 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ing [sqlite3_ste
1dfa0 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 65 20 p()]. If there
1dfb0 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 is an error, *pp
1dfc0 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 Stmt is set.** t
1dfd0 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 o NULL. If the
1dfe0 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e 74 61 input text conta
1dff0 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 ins no SQL (if t
1e000 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 65 he input is an e
1e010 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f mpty.** string o
1e020 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 r a comment) the
1e030 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 n *ppStmt is set
1e040 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 to NULL..** The
1e050 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
1e060 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c re is responsibl
1e070 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 e for deleting t
1e080 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 he compiled.** S
1e090 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 QL statement usi
1e0a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 ng [sqlite3_fina
1e0b0 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74 lize()] after it
1e0c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 has finished wi
1e0d0 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 th it..** ppStmt
1e0e0 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c may not be NULL
1e0f0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
1e100 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 ss, [SQLITE_OK]
1e110 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 is returned, oth
1e120 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 erwise an [error
1e130 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
1e140 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ed..**.** The sq
1e150 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1e160 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 () and sqlite3_p
1e170 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e repare16_v2() in
1e180 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 terfaces are.**
1e190 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
1e1a0 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 all new programs
1e1b0 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 . The two older
1e1c0 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72 interfaces are r
1e1d0 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 etained.** for b
1e1e0 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
1e1f0 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69 bility, but thei
1e200 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72 r use is discour
1e210 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 aged..** In the
1e220 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c "v2" interfaces,
1e230 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 the prepared st
1e240 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 atement.** that
1e250 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 is returned (the
1e260 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 [sqlite3_stmt]
1e270 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 object) contains
1e280 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
1e290 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 * original SQL t
1e2a0 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73 ext. This causes
1e2b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
1e2c0 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ep()] interface
1e2d0 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64 to.** behave a d
1e2e0 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77 ifferently in tw
1e2f0 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f o ways:.**.** <o
1e300 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 l>.** <li>.** If
1e310 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1e320 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e hema changes, in
1e330 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69 stead of returni
1e340 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d ng [SQLITE_SCHEM
1e350 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 A] as it.** alwa
1e360 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b ys used to do, [
1e370 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1e380 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
1e390 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 ly recompile the
1e3a0 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e SQL.** statemen
1e3b0 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e t and try to run
1e3c0 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74 it again. If t
1e3d0 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 he schema has ch
1e3e0 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 anged in.** a wa
1e3f0 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 y that makes the
1e400 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f statement no lo
1e410 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c nger valid, [sql
1e420 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c ite3_step()] wil
1e430 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 l still.** retur
1e440 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 n [SQLITE_SCHEMA
1e450 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 ]. But unlike t
1e460 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 he legacy behavi
1e470 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 or, [SQLITE_SCHE
1e480 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 MA] is.** now a
1e490 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 fatal error. Ca
1e4a0 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 lling [sqlite3_p
1e4b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61 repare_v2()] aga
1e4c0 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 in will not make
1e4d0 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f the.** error go
1e4e0 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 away. Note: us
1e4f0 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 e [sqlite3_errms
1e500 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 g()] to find the
1e510 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 text.** of the
1e520 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68 parsing error th
1e530 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e at results in an
1e540 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d [SQLITE_SCHEMA]
1e550 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 return..** </li
1e560 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 >.**.** <li>.**
1e570 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 When an error oc
1e580 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 curs, [sqlite3_s
1e590 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 tep()] will retu
1e5a0 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 rn one of the de
1e5b0 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 tailed.** [error
1e5c0 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 codes] or [exte
1e5d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 nded error codes
1e5e0 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62 ]. The legacy b
1e5f0 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74 ehavior was that
1e600 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 .** [sqlite3_ste
1e610 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 p()] would only
1e620 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63 return a generic
1e630 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
1e640 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 result code.** a
1e650 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 nd you would hav
1e660 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f e to make a seco
1e670 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nd call to [sqli
1e680 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 te3_reset()] in
1e690 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 order.** to find
1e6a0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
1e6b0 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f cause of the pro
1e6c0 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22 blem. With the "
1e6d0 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 v2" prepare.** i
1e6e0 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75 nterfaces, the u
1e6f0 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e nderlying reason
1e700 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69 for the error i
1e710 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 s returned immed
1e720 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e iately..** </li>
1e730 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ol>.**.**
1e740 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1e750 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 30 31 [H13011] [H1301
1e760 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 31 33 2] [H13013] [H13
1e770 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 5b 48 014] [H13015] [H
1e780 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 5d 20 13016] [H13019]
1e790 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53 [H13021].**.*/.S
1e7a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1e7b0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 lite3_prepare(.
1e7c0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
1e7d0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1e7e0 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 base handle */.
1e7f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 const char *zSq
1e800 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 l, /* SQL
1e810 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 statement, UTF-8
1e820 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e encoded */. in
1e830 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 t nByte,
1e840 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
1e850 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 length of zSql
1e860 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 in bytes. */. s
1e870 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 qlite3_stmt **pp
1e880 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 Stmt, /* OUT: S
1e890 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 tatement handle
1e8a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1e8b0 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 **pzTail /*
1e8c0 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 OUT: Pointer to
1e8d0 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f unused portion o
1e8e0 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c f zSql */.);.SQL
1e8f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1e900 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a te3_prepare_v2(.
1e910 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
1e920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
1e930 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
1e940 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
1e950 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ql, /* SQL
1e960 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d statement, UTF-
1e970 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 8 encoded */. i
1e980 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 nt nByte,
1e990 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
1e9a0 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c m length of zSql
1e9b0 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 in bytes. */.
1e9c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 sqlite3_stmt **p
1e9d0 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 pStmt, /* OUT:
1e9e0 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 Statement handle
1e9f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1ea00 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a **pzTail /*
1ea10 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f OUT: Pointer to
1ea20 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 unused portion
1ea30 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 of zSql */.);.SQ
1ea40 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1ea50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a ite3_prepare16(.
1ea60 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
1ea70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
1ea80 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
1ea90 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 const void *zS
1eaa0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ql, /* SQL
1eab0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d statement, UTF-
1eac0 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 16 encoded */.
1ead0 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 int nByte,
1eae0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
1eaf0 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 um length of zSq
1eb00 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 l in bytes. */.
1eb10 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a sqlite3_stmt **
1eb20 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a ppStmt, /* OUT:
1eb30 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c Statement handl
1eb40 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 e */. const voi
1eb50 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f d **pzTail /
1eb60 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 * OUT: Pointer t
1eb70 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e o unused portion
1eb80 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 of zSql */.);.S
1eb90 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1eba0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
1ebb0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
1ebc0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
1ebd0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
1ebe0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1ebf0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
1ec00 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
1ec10 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a UTF-16 encoded *
1ec20 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 /. int nByte,
1ec30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
1ec40 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
1ec50 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 zSql in bytes.
1ec60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d */. sqlite3_stm
1ec70 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 t **ppStmt, /*
1ec80 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 OUT: Statement h
1ec90 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
1eca0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 void **pzTail
1ecb0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 /* OUT: Point
1ecc0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 er to unused por
1ecd0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a tion of zSql */.
1ece0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1ecf0 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53 EF: Retrieving S
1ed00 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 48 31 tatement SQL {H1
1ed10 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e 0a 2a 3100} <H13000>.*
1ed20 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
1ed30 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 ace can be used
1ed40 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 73 61 to retrieve a sa
1ed50 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20 ved copy of the
1ed60 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 original.** SQL
1ed70 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 72 65 text used to cre
1ed80 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 ate a [prepared
1ed90 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 74 68 statement] if th
1eda0 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 at statement was
1edb0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 .** compiled usi
1edc0 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 ng either [sqlit
1edd0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
1ede0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 or [sqlite3_pre
1edf0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a pare16_v2()]..**
1ee00 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1ee10 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 5b 48 :.** [H13101] [H
1ee20 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 5d 0a 13102] [H13103].
1ee30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1ee40 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1ee50 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 3_sql(sqlite3_st
1ee60 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a mt *pStmt);../*.
1ee70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 79 6e ** CAPI3REF: Dyn
1ee80 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56 amically Typed V
1ee90 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 31 35 alue Object {H15
1eea0 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 000} <S20200>.**
1eeb0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74 KEYWORDS: {prot
1eec0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
1eed0 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65 lue} {unprotecte
1eee0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d d sqlite3_value}
1eef0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 .**.** SQLite us
1ef00 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 es the sqlite3_v
1ef10 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 72 alue object to r
1ef20 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c epresent all val
1ef30 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 ues.** that can
1ef40 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 64 be stored in a d
1ef50 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 53 atabase table. S
1ef60 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d QLite uses dynam
1ef70 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 ic typing.** for
1ef80 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73 the values it s
1ef90 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 73 74 tores. Values st
1efa0 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f ored in sqlite3_
1efb0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a value objects.**
1efc0 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 72 73 can be integers
1efd0 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 , floating point
1efe0 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 values, strings
1eff0 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c , BLOBs, or NULL
1f000 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 ..**.** An sqlit
1f010 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
1f020 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70 may be either "p
1f030 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e rotected" or "un
1f040 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53 protected"..** S
1f050 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72 ome interfaces r
1f060 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74 equire a protect
1f070 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
1f080 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61 . Other interfa
1f090 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65 ces.** will acce
1f0a0 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f 74 pt either a prot
1f0b0 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70 72 ected or an unpr
1f0c0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
1f0d0 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20 value..** Every
1f0e0 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 61 interface that a
1f0f0 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 5f 76 ccepts sqlite3_v
1f100 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 20 73 alue arguments s
1f110 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 65 74 pecifies.** whet
1f120 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 72 65 her or not it re
1f130 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 63 74 quires a protect
1f140 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
1f150 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d ..**.** The term
1f160 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 61 6e s "protected" an
1f170 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 20 d "unprotected"
1f180 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 65 72 refer to whether
1f190 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 or not.** a mut
1f1a0 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 20 69 ex is held. A i
1f1b0 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 73 nternal mutex is
1f1c0 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 6f 74 held for a prot
1f1d0 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
1f1e0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 62 75 _value object bu
1f1f0 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 68 65 t no mutex is he
1f200 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74 ld for an unprot
1f210 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
1f220 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20 _value object.
1f230 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
1f240 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 6e 67 piled to be sing
1f250 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 28 le-threaded.** (
1f260 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 48 52 with [SQLITE_THR
1f270 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 20 77 EADSAFE=0] and w
1f280 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 68 72 ith [sqlite3_thr
1f290 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 75 72 eadsafe()] retur
1f2a0 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66 ning 0).** or if
1f2b0 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e 20 69 SQLite is run i
1f2c0 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 65 64 n one of reduced
1f2d0 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a mutex modes .**
1f2e0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
1f2f0 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 6f 72 SINGLETHREAD] or
1f300 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
1f310 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20 MULTITHREAD].**
1f320 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f then there is no
1f330 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 distinction bet
1f340 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 ween protected a
1f350 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a nd unprotected.*
1f360 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 * sqlite3_value
1f370 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79 objects and they
1f380 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74 can be used int
1f390 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48 erchangeably. H
1f3a0 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d owever,.** for m
1f3b0 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74 aximum code port
1f3c0 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65 ability it is re
1f3d0 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 commended that a
1f3e0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
1f3f0 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69 till make the di
1f400 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 stinction betwee
1f410 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 n between protec
1f420 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 ted and unprotec
1f430 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ted.** sqlite3_v
1f440 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65 alue objects eve
1f450 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 69 63 n when not stric
1f460 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a tly required..**
1f470 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
1f480 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 74 68 value objects th
1f490 61 74 20 61 72 65 20 70 61 73 73 65 64 20 61 73 at are passed as
1f4a0 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 74 6f parameters into
1f4b0 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
1f4c0 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 tation of [appli
1f4d0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
1f4e0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 QL functions] ar
1f4f0 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 e protected..**
1f500 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
1f510 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 e object returne
1f520 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 d by.** [sqlite3
1f530 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d _column_value()]
1f540 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e is unprotected.
1f550 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20 .** Unprotected
1f560 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
1f570 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 jects may only b
1f580 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b e used with.** [
1f590 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
1f5a0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c alue()] and [sql
1f5b0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
1f5c0 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 )]..** The [sqli
1f5d0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c te3_value_blob |
1f5e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1f5f0 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 ype()] family of
1f600 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72 .** interfaces r
1f610 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 65 64 equire protected
1f620 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
1f630 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 bjects..*/.typed
1f640 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71 ef struct Mem sq
1f650 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a lite3_value;../*
1f660 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
1f670 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 L Function Conte
1f680 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 30 30 xt Object {H1600
1f690 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 1} <S20200>.**.*
1f6a0 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e * The context in
1f6b0 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75 which an SQL fu
1f6c0 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20 nction executes
1f6d0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a is stored in an.
1f6e0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ** sqlite3_conte
1f6f0 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f xt object. A po
1f700 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 inter to an sqli
1f710 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 te3_context obje
1f720 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 ct.** is always
1f730 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
1f740 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d to [application-
1f750 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 defined SQL func
1f760 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 tions]..** The a
1f770 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
1f780 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ed SQL function
1f790 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
1f7a0 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a 2a 2a ill pass this.**
1f7b0 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68 pointer through
1f7c0 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b into calls to [
1f7d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
1f7e0 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 nt | sqlite3_res
1f7f0 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 ult()],.** [sqli
1f800 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
1f810 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 ntext()], [sqlit
1f820 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c e3_user_data()],
1f830 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e .** [sqlite3_con
1f840 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 text_db_handle()
1f850 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f ], [sqlite3_get_
1f860 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 auxdata()],.** a
1f870 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 nd/or [sqlite3_s
1f880 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a et_auxdata()]..*
1f890 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
1f8a0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1f8b0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1f8c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1f8d0 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 F: Binding Value
1f8e0 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53 74 s To Prepared St
1f8f0 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 30 30 atements {H13500
1f900 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 } <S70300>.** KE
1f910 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 70 61 YWORDS: {host pa
1f920 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 20 70 rameter} {host p
1f930 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74 arameters} {host
1f940 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d parameter name}
1f950 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 .** KEYWORDS: {S
1f960 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 53 QL parameter} {S
1f970 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b QL parameters} {
1f980 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e parameter bindin
1f990 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 g}.**.** In the
1f9a0 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e 70 75 SQL strings inpu
1f9b0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 t to [sqlite3_pr
1f9c0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
1f9d0 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a its variants,.**
1f9e0 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 62 65 literals may be
1f9f0 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 5b replaced by a [
1fa00 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 6f 6e parameter] in on
1fa10 65 20 6f 66 20 74 68 65 73 65 20 66 6f 72 6d 73 e of these forms
1fa20 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
1fa30 3c 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 <li> ?.** <li>
1fa40 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a ?NNN.** <li> :
1fa50 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 VVV.** <li> @VV
1fa60 56 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a V.** <li> $VVV.
1fa70 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 ** </ul>.**.** I
1fa80 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 n the parameter
1fa90 66 6f 72 6d 73 20 73 68 6f 77 6e 20 61 62 6f 76 forms shown abov
1faa0 65 20 4e 4e 4e 20 69 73 20 61 6e 20 69 6e 74 65 e NNN is an inte
1fab0 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20 ger literal,.**
1fac0 61 6e 64 20 56 56 56 20 69 73 20 61 6e 20 61 6c and VVV is an al
1fad0 70 68 61 2d 6e 75 6d 65 72 69 63 20 70 61 72 61 pha-numeric para
1fae0 6d 65 74 65 72 20 6e 61 6d 65 2e 20 54 68 65 20 meter name. The
1faf0 76 61 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a values of these.
1fb00 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 28 61 ** parameters (a
1fb10 6c 73 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 lso called "host
1fb20 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 parameter names
1fb30 22 20 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 " or "SQL parame
1fb40 74 65 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 ters").** can be
1fb50 20 73 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 set using the s
1fb60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 qlite3_bind_*()
1fb70 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 routines defined
1fb80 20 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 here..**.** The
1fb90 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
1fba0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 to the sqlite3_b
1fbb0 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 ind_*() routines
1fbc0 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 is always.** a
1fbd0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
1fbe0 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 sqlite3_stmt] ob
1fbf0 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 ject returned fr
1fc00 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 om.** [sqlite3_p
1fc10 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
1fc20 69 74 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a its variants..**
1fc30 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 .** The second a
1fc40 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 rgument is the i
1fc50 6e 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 ndex of the SQL
1fc60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 parameter to be
1fc70 73 65 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 set..** The left
1fc80 6d 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 most SQL paramet
1fc90 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 er has an index
1fca0 6f 66 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20 of 1. When the
1fcb0 73 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 same named.** SQ
1fcc0 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 L parameter is u
1fcd0 73 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e sed more than on
1fce0 63 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 ce, second and s
1fcf0 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 ubsequent.** occ
1fd00 75 72 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 urrences have th
1fd10 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 e same index as
1fd20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 the first occurr
1fd30 65 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 ence..** The ind
1fd40 65 78 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 ex for named par
1fd50 61 6d 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c ameters can be l
1fd60 6f 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 ooked up using t
1fd70 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 he.** [sqlite3_b
1fd80 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e ind_parameter_in
1fd90 64 65 78 28 29 5d 20 41 50 49 20 69 66 20 64 65 dex()] API if de
1fda0 73 69 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 sired. The inde
1fdb0 78 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 x.** for "?NNN"
1fdc0 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 74 68 parameters is th
1fdd0 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a e value of NNN..
1fde0 2a 2a 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 ** The NNN value
1fdf0 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
1fe00 20 31 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 1 and the [sqli
1fe10 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 te3_limit()].**
1fe20 70 61 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 parameter [SQLIT
1fe30 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1fe40 5f 4e 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c _NUMBER] (defaul
1fe50 74 20 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a t value: 999)..*
1fe60 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 *.** The third a
1fe70 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76 rgument is the v
1fe80 61 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 alue to bind to
1fe90 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a the parameter..*
1fea0 2a 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f *.** In those ro
1feb0 75 74 69 6e 65 73 20 74 68 61 74 20 68 61 76 65 utines that have
1fec0 20 61 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 a fourth argume
1fed0 6e 74 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73 nt, its value is
1fee0 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
1fef0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 70 f bytes in the p
1ff00 61 72 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 arameter. To be
1ff10 20 63 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75 clear: the valu
1ff20 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 e is the.** numb
1ff30 65 72 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f er of <u>bytes</
1ff40 75 3e 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c u> in the value,
1ff50 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 not the number
1ff60 6f 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a of characters..*
1ff70 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 * If the fourth
1ff80 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 parameter is neg
1ff90 61 74 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 ative, the lengt
1ffa0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 h of the string
1ffb0 69 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 is.** the number
1ffc0 20 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 of bytes up to
1ffd0 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 the first zero t
1ffe0 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a erminator..**.**
1fff0 20 54 68 65 20 66 69 66 74 68 20 61 72 67 75 6d The fifth argum
20000 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 ent to sqlite3_b
20010 69 6e 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 ind_blob(), sqli
20020 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c te3_bind_text(),
20030 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f and.** sqlite3_
20040 62 69 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73 bind_text16() is
20050 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 75 73 a destructor us
20060 65 64 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 ed to dispose of
20070 20 74 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 the BLOB or.**
20080 73 74 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c string after SQL
20090 69 74 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 ite has finished
200a0 20 77 69 74 68 20 69 74 2e 20 49 66 20 74 68 65 with it. If the
200b0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 fifth argument
200c0 69 73 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 is.** the specia
200d0 6c 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f l value [SQLITE_
200e0 53 54 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 STATIC], then SQ
200f0 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 Lite assumes tha
20100 74 20 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 t the.** informa
20110 74 69 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69 tion is in stati
20120 63 2c 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 c, unmanaged spa
20130 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 ce and does not
20140 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 need to be freed
20150 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74 ..** If the fift
20160 68 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 h argument has t
20170 68 65 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 he value [SQLITE
20180 5f 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 _TRANSIENT], the
20190 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 n.** SQLite make
201a0 73 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 s its own privat
201b0 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 e copy of the da
201c0 74 61 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 ta immediately,
201d0 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 before.** the sq
201e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 lite3_bind_*() r
201f0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a outine returns..
20200 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
20210 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
20220 29 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 ) routine binds
20230 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 a BLOB of length
20240 20 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 N that.** is fi
20250 6c 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 lled with zeroes
20260 2e 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 . A zeroblob us
20270 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e es a fixed amoun
20280 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 t of memory.** (
20290 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 just an integer
202a0 74 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 to hold its size
202b0 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 ) while it is be
202c0 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a ing processed..*
202d0 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 * Zeroblobs are
202e0 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 intended to serv
202f0 65 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 e as placeholder
20300 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 s for BLOBs whos
20310 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 e.** content is
20320 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 later written us
20330 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
20340 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 blob_open | incr
20350 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f emental BLOB I/O
20360 5d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 ] routines..** A
20370 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 negative value
20380 66 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 for the zeroblob
20390 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 results in a ze
203a0 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a ro-length BLOB..
203b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
203c0 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 3_bind_*() routi
203d0 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c nes must be call
203e0 65 64 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c ed after.** [sql
203f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
20400 29 5d 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 )] (and its vari
20410 61 6e 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 ants) or [sqlite
20420 33 5f 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 3_reset()] and.*
20430 2a 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 * before [sqlite
20440 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 3_step()]..** Bi
20450 6e 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 ndings are not c
20460 6c 65 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 leared by the [s
20470 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
20480 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f routine..** Unbo
20490 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 und parameters a
204a0 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 re interpreted a
204b0 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 s NULL..**.** Th
204c0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 ese routines ret
204d0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 urn [SQLITE_OK]
204e0 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e on success or an
204f0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a error code if.*
20500 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 * anything goes
20510 77 72 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f wrong. [SQLITE_
20520 52 41 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e RANGE] is return
20530 65 64 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 ed if the parame
20540 74 65 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 ter.** index is
20550 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b out of range. [
20560 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 SQLITE_NOMEM] is
20570 20 72 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c returned if mal
20580 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 loc() fails..**
20590 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 [SQLITE_MISUSE]
205a0 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 might be returne
205b0 64 20 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 d if these routi
205c0 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f nes are called o
205d0 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d n a.** virtual m
205e0 61 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 achine that is t
205f0 68 65 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f he wrong state o
20600 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 r which has alre
20610 61 64 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a ady been finaliz
20620 65 64 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e ed..** Detection
20630 20 6f 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e of misuse is un
20640 72 65 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 reliable. Appli
20650 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e cations should n
20660 6f 74 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 ot depend.** on
20670 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 SQLITE_MISUSE re
20680 74 75 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d turns. SQLITE_M
20690 49 53 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 ISUSE is intende
206a0 64 20 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a d to indicate a.
206b0 2a 2a 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 ** a logic error
206c0 20 69 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 in the applicat
206d0 69 6f 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 ion. Future ver
206e0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
206f0 6d 69 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 might.** panic r
20700 61 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 ather than retur
20710 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e n SQLITE_MISUSE.
20720 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
20730 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
20740 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 arameter_count()
20750 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ],.** [sqlite3_b
20760 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 ind_parameter_na
20770 6d 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 me()], and [sqli
20780 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
20790 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a er_index()]..**.
207a0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
207b0 0a 2a 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 .** [H13506] [H1
207c0 33 35 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 3509] [H13512] [
207d0 48 31 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d H13515] [H13518]
207e0 20 5b 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32 [H13521] [H1352
207f0 34 5d 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 4] [H13527].** [
20800 48 31 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d H13530] [H13533]
20810 20 5b 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33 [H13536] [H1353
20820 39 5d 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 9] [H13542] [H13
20830 35 34 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 545] [H13548] [H
20840 31 33 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 13551].**.*/.SQL
20850 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
20860 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 te3_bind_blob(sq
20870 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
20880 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
20890 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f nt n, void(*)(vo
208a0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
208b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
208c0 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 nd_double(sqlite
208d0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 3_stmt*, int, do
208e0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 uble);.SQLITE_AP
208f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
20900 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 nd_int(sqlite3_s
20910 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b tmt*, int, int);
20920 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
20930 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 sqlite3_bind_int
20940 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 64(sqlite3_stmt*
20950 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 , int, sqlite3_i
20960 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 nt64);.SQLITE_AP
20970 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
20980 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f nd_null(sqlite3_
20990 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c stmt*, int);.SQL
209a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
209b0 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 te3_bind_text(sq
209c0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
209d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 , const char*, i
209e0 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f nt n, void(*)(vo
209f0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
20a00 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
20a10 6e 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 nd_text16(sqlite
20a20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
20a30 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 nst void*, int,
20a40 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
20a50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
20a60 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c sqlite3_bind_val
20a70 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a ue(sqlite3_stmt*
20a80 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c , int, const sql
20a90 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
20aa0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
20ab0 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c ite3_bind_zerobl
20ac0 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a ob(sqlite3_stmt*
20ad0 2c 20 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a , int, int n);..
20ae0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
20af0 4e 75 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61 Number Of SQL Pa
20b00 72 61 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30 rameters {H13600
20b10 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a } <S70300>.**.**
20b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
20b30 6e 20 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e n be used to fin
20b40 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 d the number of
20b50 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d [SQL parameters]
20b60 0a 2a 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72 .** in a [prepar
20b70 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 ed statement].
20b80 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61 SQL parameters a
20b90 72 65 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 re tokens of the
20ba0 0a 2a 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f .** form "?", "?
20bb0 4e 4e 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 NNN", ":AAA", "$
20bc0 41 41 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 AAA", or "@AAA"
20bd0 74 68 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a that serve as.**
20be0 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f placeholders fo
20bf0 72 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 r values that ar
20c00 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f e [sqlite3_bind_
20c10 62 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a blob | bound].**
20c20 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 to the paramete
20c30 72 73 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 rs at a later ti
20c40 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 me..**.** This r
20c50 6f 75 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 outine actually
20c60 72 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 returns the inde
20c70 78 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 x of the largest
20c80 20 28 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 (rightmost).**
20c90 70 61 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 parameter. For a
20ca0 6c 6c 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20 ll forms except
20cb0 3f 4e 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 ?NNN, this will
20cc0 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 correspond to th
20cd0 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 e.** number of u
20ce0 6e 69 71 75 65 20 70 61 72 61 6d 65 74 65 72 73 nique parameters
20cf0 2e 20 20 49 66 20 70 61 72 61 6d 65 74 65 72 73 . If parameters
20d00 20 6f 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 of the ?NNN are
20d10 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 used,.** there
20d20 6d 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 may be gaps in t
20d30 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 he list..**.** S
20d40 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
20d50 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 3_bind_blob|sqli
20d60 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 te3_bind()],.**
20d70 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
20d80 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c rameter_name()],
20d90 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 and.** [sqlite3
20da0 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
20db0 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 index()]..**.**
20dc0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
20dd0 20 5b 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c [H13601].*/.SQL
20de0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
20df0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
20e00 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 er_count(sqlite3
20e10 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 _stmt*);../*.**
20e20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f CAPI3REF: Name O
20e30 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 f A Host Paramet
20e40 65 72 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 er {H13620} <S70
20e50 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 300>.**.** This
20e60 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
20e70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
20e80 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 name of the n-t
20e90 68 0a 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 h.** [SQL parame
20ea0 74 65 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 ter] in a [prepa
20eb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a red statement]..
20ec0 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 ** SQL parameter
20ed0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f s of the form "?
20ee0 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f NNN" or ":AAA" o
20ef0 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 r "@AAA" or "$AA
20f00 41 22 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d A".** have a nam
20f10 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 e which is the s
20f20 74 72 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 tring "?NNN" or
20f30 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 ":AAA" or "@AAA"
20f40 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 or "$AAA".** re
20f50 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 spectively..** I
20f60 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
20f70 68 65 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f he initial ":" o
20f80 72 20 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20 r "$" or "@" or
20f90 22 3f 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 "?".** is includ
20fa0 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
20fb0 65 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d e name..** Param
20fc0 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 eters of the for
20fd0 6d 20 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 m "?" without a
20fe0 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 following intege
20ff0 72 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a r have no name.*
21000 2a 20 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 * and are also r
21010 65 66 65 72 72 65 64 20 74 6f 20 61 73 20 22 61 eferred to as "a
21020 6e 6f 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 nonymous paramet
21030 65 72 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ers"..**.** The
21040 66 69 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d first host param
21050 65 74 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 eter has an inde
21060 78 20 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a x of 1, not 0..*
21070 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 *.** If the valu
21080 65 20 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 e n is out of ra
21090 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d nge or if the n-
210a0 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a th parameter is.
210b0 2a 2a 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 ** nameless, the
210c0 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e n NULL is return
210d0 65 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 ed. The returne
210e0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 d string is.** a
210f0 6c 77 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65 lways in UTF-8 e
21100 6e 63 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 ncoding even if
21110 74 68 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 the named parame
21120 74 65 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 ter was.** origi
21130 6e 61 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20 nally specified
21140 61 73 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 as UTF-16 in [sq
21150 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 lite3_prepare16(
21160 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 )] or.** [sqlite
21170 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 3_prepare16_v2()
21180 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 ]..**.** See als
21190 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 o: [sqlite3_bind
211a0 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 _blob|sqlite3_bi
211b0 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 nd()],.** [sqlit
211c0 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
211d0 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a r_count()], and.
211e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
211f0 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 _parameter_index
21200 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
21210 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
21220 36 32 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 621].*/.SQLITE_A
21230 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
21240 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
21250 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 meter_name(sqlit
21260 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a e3_stmt*, int);.
21270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
21280 20 49 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 Index Of A Para
21290 6d 65 74 65 72 20 57 69 74 68 20 41 20 47 69 76 meter With A Giv
212a0 65 6e 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d en Name {H13640}
212b0 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S70300>.**.**
212c0 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 Return the index
212d0 20 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d of an SQL param
212e0 65 74 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e eter given its n
212f0 61 6d 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 ame. The.** ind
21300 65 78 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 ex value returne
21310 64 20 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f d is suitable fo
21320 72 20 75 73 65 20 61 73 20 74 68 65 20 73 65 63 r use as the sec
21330 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ond.** parameter
21340 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e to [sqlite3_bin
21350 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 d_blob|sqlite3_b
21360 69 6e 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a ind()]. A zero.
21370 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ** is returned i
21380 66 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 f no matching pa
21390 72 61 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64 rameter is found
213a0 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 . The parameter
213b0 0a 2a 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 .** name must be
213c0 20 67 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 given in UTF-8
213d0 65 76 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67 even if the orig
213e0 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a inal statement.*
213f0 2a 20 77 61 73 20 70 72 65 70 61 72 65 64 20 66 * was prepared f
21400 72 6f 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20 rom UTF-16 text
21410 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 using [sqlite3_p
21420 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a repare16_v2()]..
21430 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
21440 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
21450 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
21460 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
21470 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 bind_parameter_c
21480 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 ount()], and.**
21490 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
214a0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d rameter_index()]
214b0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
214c0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 ents:.** [H13641
214d0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
214e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
214f0 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 _parameter_index
21500 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
21510 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
21520 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 e);../*.** CAPI3
21530 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 REF: Reset All B
21540 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 indings On A Pre
21550 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
21560 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30 {H13660} <S70300
21570 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 >.**.** Contrary
21580 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f to the intuitio
21590 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 n of many, [sqli
215a0 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 te3_reset()] doe
215b0 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 s not reset.** t
215c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 he [sqlite3_bind
215d0 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 _blob | bindings
215e0 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 ] on a [prepared
215f0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 statement]..**
21600 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 Use this routine
21610 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f to reset all ho
21620 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f st parameters to
21630 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 NULL..**.** Req
21640 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
21650 31 33 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13661].*/.SQLITE
21660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
21670 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 _clear_bindings(
21680 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
21690 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
216a0 20 4e 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d Number Of Colum
216b0 6e 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 ns In A Result S
216c0 65 74 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 et {H13710} <S10
216d0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 700>.**.** Retur
216e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
216f0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 columns in the r
21700 65 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e esult set return
21710 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 ed by the.** [pr
21720 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
21730 5d 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ]. This routine
21740 72 65 74 75 72 6e 73 20 30 20 69 66 20 70 53 74 returns 0 if pSt
21750 6d 74 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 mt is an SQL.**
21760 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 statement that d
21770 6f 65 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 oes not return d
21780 61 74 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ata (for example
21790 20 61 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a an [UPDATE])..*
217a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
217b0 73 3a 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a s:.** [H13711].*
217c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
217d0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
217e0 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 count(sqlite3_st
217f0 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a mt *pStmt);../*.
21800 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c ** CAPI3REF: Col
21810 75 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 umn Names In A R
21820 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32 esult Set {H1372
21830 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
21840 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
21850 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 return the name
21860 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70 assigned to a p
21870 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e articular column
21880 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c .** in the resul
21890 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 t set of a [SELE
218a0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 CT] statement.
218b0 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 The sqlite3_colu
218c0 6d 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 mn_name().** int
218d0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
218e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 pointer to a ze
218f0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
21900 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e F-8 string.** an
21910 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e d sqlite3_column
21920 5f 6e 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e _name16() return
21930 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
21940 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
21950 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e .** UTF-16 strin
21960 67 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 g. The first pa
21970 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b rameter is the [
21980 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
21990 6e 74 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c nt].** that impl
219a0 65 6d 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 ements the [SELE
219b0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 CT] statement. T
219c0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
219d0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f ter is the.** co
219e0 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 lumn number. Th
219f0 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d e leftmost colum
21a00 6e 20 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a n is number 0..*
21a10 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 *.** The returne
21a20 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 d string pointer
21a30 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 is valid until
21a40 65 69 74 68 65 72 20 74 68 65 20 5b 70 72 65 70 either the [prep
21a50 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a ared statement].
21a60 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 ** is destroyed
21a70 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 by [sqlite3_fina
21a80 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c lize()] or until
21a90 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 the next call t
21aa0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c o.** sqlite3_col
21ab0 75 6d 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 umn_name() or sq
21ac0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
21ad0 65 31 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d e16() on the sam
21ae0 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 e column..**.**
21af0 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f If sqlite3_mallo
21b00 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 c() fails during
21b10 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 the processing
21b20 6f 66 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e of either routin
21b30 65 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c e.** (for exampl
21b40 65 20 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 e during a conve
21b50 72 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 rsion from UTF-8
21b60 20 74 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e to UTF-16) then
21b70 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 a.** NULL point
21b80 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
21b90 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f **.** The name o
21ba0 66 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d f a result colum
21bb0 6e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f n is the value o
21bc0 66 20 74 68 65 20 22 41 53 22 20 63 6c 61 75 73 f the "AS" claus
21bd0 65 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f e for.** that co
21be0 6c 75 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69 lumn, if there i
21bf0 73 20 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 s an AS clause.
21c00 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
21c10 41 53 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 AS clause.** the
21c20 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
21c30 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 e column is unsp
21c40 65 63 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20 ecified and may
21c50 63 68 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f change from.** o
21c60 6e 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 ne release of SQ
21c70 4c 69 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74 Lite to the next
21c80 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
21c90 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 ents:.** [H13721
21ca0 5d 20 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37 ] [H13723] [H137
21cb0 32 34 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 24] [H13725] [H1
21cc0 33 37 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 3726] [H13727].*
21cd0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
21ce0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
21cf0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c _column_name(sql
21d00 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
21d10 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 N);.SQLITE_API c
21d20 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
21d30 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 e3_column_name16
21d40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
21d50 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 int N);../*.** C
21d60 41 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 API3REF: Source
21d70 4f 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 Of Data In A Que
21d80 72 79 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 ry Result {H1374
21d90 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
21da0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
21db0 20 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 provide a means
21dc0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 to determine wh
21dd0 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 at column of wha
21de0 74 0a 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 t.** table in wh
21df0 69 63 68 20 64 61 74 61 62 61 73 65 20 61 20 72 ich database a r
21e00 65 73 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 esult of a [SELE
21e10 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f CT] statement co
21e20 6d 65 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 mes from..** The
21e30 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
21e40 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f abase or table o
21e50 72 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 r column can be
21e60 72 65 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 returned as.** e
21e70 69 74 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 ither a UTF-8 or
21e80 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 UTF-16 string.
21e90 20 54 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 The _database_
21ea0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a routines return.
21eb0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
21ec0 6e 61 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 name, the _table
21ed0 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 _ routines retur
21ee0 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 n the table name
21ef0 2c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 , and.** the ori
21f00 67 69 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 gin_ routines re
21f10 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 turn the column
21f20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 name..** The ret
21f30 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
21f40 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 valid until the
21f50 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
21f60 65 6e 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 ent] is destroye
21f70 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 d.** using [sqli
21f80 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 te3_finalize()]
21f90 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d or until the sam
21fa0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 e information is
21fb0 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 requested.** ag
21fc0 61 69 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 ain in a differe
21fd0 6e 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a nt encoding..**.
21fe0 2a 2a 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 ** The names ret
21ff0 75 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 urned are the or
22000 69 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 iginal un-aliase
22010 64 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a d names of the.*
22020 2a 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c * database, tabl
22030 65 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a e, and column..*
22040 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
22050 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 rgument to the f
22060 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 ollowing calls i
22070 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 s a [prepared st
22080 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 atement]..** The
22090 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 se functions ret
220a0 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 urn information
220b0 61 62 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f about the Nth co
220c0 6c 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 lumn returned by
220d0 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e .** the statemen
220e0 74 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 t, where N is th
220f0 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f e second functio
22100 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a n argument..**.*
22110 2a 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c * If the Nth col
22120 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 umn returned by
22130 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
22140 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f an expression o
22150 72 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e r.** subquery an
22160 64 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d d is not a colum
22170 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c n value, then al
22180 6c 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 l of these funct
22190 69 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e ions return.** N
221a0 55 4c 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 ULL. These rout
221b0 69 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 ine might also r
221c0 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 eturn NULL if a
221d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
221e0 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 n error.** occur
221f0 73 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 s. Otherwise, t
22200 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e hey return the n
22210 61 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 ame of the attac
22220 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 hed database, ta
22230 62 6c 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d ble.** and colum
22240 6e 20 74 68 61 74 20 71 75 65 72 79 20 72 65 73 n that query res
22250 75 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 ult column was e
22260 78 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a xtracted from..*
22270 2a 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c *.** As with all
22280 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 other SQLite AP
22290 49 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 Is, those postfi
222a0 78 65 64 20 77 69 74 68 20 22 31 36 22 20 72 65 xed with "16" re
222b0 74 75 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 turn.** UTF-16 e
222c0 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 ncoded strings,
222d0 74 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 the other functi
222e0 6f 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 ons return UTF-8
222f0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 . {END}.**.** Th
22300 65 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c ese APIs are onl
22310 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 y available if t
22320 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 he library was c
22330 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
22340 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 .** [SQLITE_ENAB
22350 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
22360 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 TA] C-preprocess
22370 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 or symbol define
22380 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 d..**.** {A13751
22390 7d 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d }.** If two or m
223a0 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c ore threads call
223b0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 one or more of
223c0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
223d0 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a gainst the same.
223e0 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 ** prepared stat
223f0 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e ement and column
22400 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
22410 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c e then the resul
22420 74 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 ts are.** undefi
22430 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ned..**.** Requi
22440 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
22450 37 34 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 741] [H13742] [H
22460 31 33 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 13743] [H13744]
22470 5b 48 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36 [H13745] [H13746
22480 5d 20 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a ] [H13748].**.**
22490 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 If two or more
224a0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 threads call one
224b0 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c or more.** [sql
224c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 ite3_column_data
224d0 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 base_name | colu
224e0 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 mn metadata inte
224f0 72 66 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 rfaces].** for t
22500 68 65 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 he same [prepare
22510 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 d statement] and
22520 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a result column.*
22530 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 * at the same ti
22540 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 me then the resu
22550 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
22560 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
22570 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
22580 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 ite3_column_data
22590 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 base_name(sqlite
225a0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
225b0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
225c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
225d0 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d umn_database_nam
225e0 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
225f0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 *,int);.SQLITE_A
22600 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
22610 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 qlite3_column_ta
22620 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ble_name(sqlite3
22630 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
22640 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
22650 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
22660 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 mn_table_name16(
22670 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
22680 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
22690 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
226a0 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
226b0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
226c0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
226d0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
226e0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
226f0 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 origin_name16(sq
22700 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
22710 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
22720 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 F: Declared Data
22730 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 type Of A Query
22740 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 Result {H13760}
22750 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
22760 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
22770 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 er is a [prepare
22780 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
22790 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 If this stateme
227a0 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d nt is a [SELECT]
227b0 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 statement and t
227c0 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
227d0 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 the.** returned
227e0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
227f0 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 hat [SELECT] is
22800 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 a table column (
22810 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 not an.** expres
22820 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 sion or subquery
22830 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 ) then the decla
22840 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 red type of the
22850 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 table.** column
22860 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 is returned. If
22870 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
22880 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
22890 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 t is an.** expre
228a0 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 ssion or subquer
228b0 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 y, then a NULL p
228c0 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e ointer is return
228d0 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ed..** The retur
228e0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c ned string is al
228f0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 ways UTF-8 encod
22900 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 ed. {END}.**.**
22910 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 For example, giv
22920 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
22930 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 schema:.**.** CR
22940 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 EATE TABLE t1(c1
22950 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a VARIANT);.**.**
22960 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 and the followi
22970 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 ng statement to
22980 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a be compiled:.**.
22990 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 ** SELECT c1 + 1
229a0 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a , c1 FROM t1;.**
229b0 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
229c0 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 would return th
229d0 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e e string "VARIAN
229e0 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e T" for the secon
229f0 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 d result.** colu
22a00 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 mn (i==1), and a
22a10 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f NULL pointer fo
22a20 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 r the first resu
22a30 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 lt column (i==0)
22a40 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 ..**.** SQLite u
22a50 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d ses dynamic run-
22a60 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f time typing. So
22a70 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 just because a
22a80 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 column.** is dec
22a90 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e lared to contain
22aa0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 a particular ty
22ab0 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e pe does not mean
22ac0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 that the.** dat
22ad0 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 a stored in that
22ae0 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 column is of th
22af0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e e declared type.
22b00 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 SQLite is.** s
22b10 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 trongly typed, b
22b20 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 ut the typing is
22b30 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 dynamic not sta
22b40 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 tic. Type.** is
22b50 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
22b60 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 individual valu
22b70 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 es, not with the
22b80 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 containers.** u
22b90 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 sed to hold thos
22ba0 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 e values..**.**
22bb0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
22bc0 20 5b 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36 [H13761] [H1376
22bd0 32 5d 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 2] [H13763].*/.S
22be0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
22bf0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
22c00 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 lumn_decltype(sq
22c10 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
22c20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
22c30 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
22c40 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
22c50 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
22c60 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ,int);../*.** CA
22c70 50 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 PI3REF: Evaluate
22c80 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e An SQL Statemen
22c90 74 20 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30 t {H13200} <S100
22ca0 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 00>.**.** After
22cb0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
22cc0 65 6d 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 ement] has been
22cd0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 65 prepared using e
22ce0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 ither.** [sqlite
22cf0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
22d00 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 or [sqlite3_prep
22d10 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f are16_v2()] or o
22d20 6e 65 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 ne of the legacy
22d30 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b .** interfaces [
22d40 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
22d50 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 )] or [sqlite3_p
22d60 72 65 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69 repare16()], thi
22d70 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 s function.** mu
22d80 73 74 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 st be called one
22d90 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 or more times t
22da0 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 73 o evaluate the s
22db0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 tatement..**.**
22dc0 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 The details of t
22dd0 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 he behavior of t
22de0 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 he sqlite3_step(
22df0 29 20 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 ) interface depe
22e00 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 nd.** on whether
22e10 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 the statement w
22e20 61 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e as prepared usin
22e30 67 20 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 g the newer "v2"
22e40 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 interface.** [s
22e50 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
22e60 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 2()] and [sqlite
22e70 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 3_prepare16_v2()
22e80 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c ] or the older l
22e90 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 egacy.** interfa
22ea0 63 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ce [sqlite3_prep
22eb0 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 are()] and [sqli
22ec0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
22ed0 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 . The use of th
22ee0 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e e.** new "v2" in
22ef0 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d terface is recom
22f00 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 mended for new a
22f10 70 70 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 pplications but
22f20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e the legacy.** in
22f30 74 65 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e terface will con
22f40 74 69 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 tinue to be supp
22f50 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 orted..**.** In
22f60 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 the legacy inter
22f70 66 61 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e face, the return
22f80 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 value will be e
22f90 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 ither [SQLITE_BU
22fa0 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f SY],.** [SQLITE_
22fb0 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 DONE], [SQLITE_R
22fc0 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 OW], [SQLITE_ERR
22fd0 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f OR], or [SQLITE_
22fe0 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 MISUSE]..** With
22ff0 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 the "v2" interf
23000 61 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 ace, any of the
23010 6f 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f other [result co
23020 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 des] or.** [exte
23030 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
23040 73 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 s] might be retu
23050 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a rned as well..**
23060 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 .** [SQLITE_BUSY
23070 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 ] means that the
23080 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 database engine
23090 20 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 was unable to a
230a0 63 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 cquire the.** da
230b0 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 tabase locks it
230c0 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 needs to do its
230d0 6a 6f 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 job. If the sta
230e0 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d tement is a [COM
230f0 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 MIT].** or occur
23100 73 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 s outside of an
23110 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 explicit transac
23120 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 tion, then you c
23130 61 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 an retry the.**
23140 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 statement. If t
23150 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
23160 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 not a [COMMIT] a
23170 6e 64 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e nd occurs within
23180 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 a.** explicit t
23190 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 ransaction then
231a0 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 you should rollb
231b0 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 ack the transact
231c0 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f ion before.** co
231d0 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 ntinuing..**.**
231e0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 [SQLITE_DONE] me
231f0 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 74 61 ans that the sta
23200 74 65 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73 tement has finis
23210 68 65 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a hed executing.**
23220 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 successfully.
23230 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 sqlite3_step() s
23240 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c hould not be cal
23250 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 led again on thi
23260 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 s virtual.** mac
23270 68 69 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72 hine without fir
23280 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 st calling [sqli
23290 74 65 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 te3_reset()] to
232a0 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 reset the virtua
232b0 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 l.** machine bac
232c0 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c k to its initial
232d0 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 state..**.** If
232e0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
232f0 6e 74 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 nt being execute
23300 64 20 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61 d returns any da
23310 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 ta, then [SQLITE
23320 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 _ROW].** is retu
23330 72 6e 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 rned each time a
23340 20 6e 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61 new row of data
23350 20 69 73 20 72 65 61 64 79 20 66 6f 72 20 70 72 is ready for pr
23360 6f 63 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a ocessing by the.
23370 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 ** caller. The v
23380 61 6c 75 65 73 20 6d 61 79 20 62 65 20 61 63 63 alues may be acc
23390 65 73 73 65 64 20 75 73 69 6e 67 20 74 68 65 20 essed using the
233a0 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 [column access f
233b0 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 unctions]..** sq
233c0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 lite3_step() is
233d0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 called again to
233e0 72 65 74 72 69 65 76 65 20 74 68 65 20 6e 65 78 retrieve the nex
233f0 74 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a t row of data..*
23400 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 *.** [SQLITE_ERR
23410 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 OR] means that a
23420 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 run-time error
23430 28 73 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 (such as a const
23440 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 raint.** violati
23450 6f 6e 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 on) has occurred
23460 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 . sqlite3_step(
23470 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ) should not be
23480 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a called again on.
23490 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 ** the VM. More
234a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 information may
234b0 62 65 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c be found by call
234c0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 ing [sqlite3_err
234d0 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 msg()]..** With
234e0 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 the legacy inter
234f0 66 61 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 face, a more spe
23500 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 cific error code
23510 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a (for example,.*
23520 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 * [SQLITE_INTERR
23530 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 UPT], [SQLITE_SC
23540 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 HEMA], [SQLITE_C
23550 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 ORRUPT], and so
23560 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 forth).** can be
23570 20 6f 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c obtained by cal
23580 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 ling [sqlite3_re
23590 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a set()] on the.**
235a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
235b0 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 ment]. In the "
235c0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a v2" interface,.*
235d0 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 * the more speci
235e0 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69 fic error code i
235f0 73 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 s returned direc
23600 74 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73 tly by sqlite3_s
23610 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 tep()..**.** [SQ
23620 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 LITE_MISUSE] mea
23630 6e 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73 ns that the this
23640 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c routine was cal
23650 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74 led inappropriat
23660 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 ely..** Perhaps
23670 69 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e it was called on
23680 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
23690 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 tement] that has
236a0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e .** already been
236b0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
236c0 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 ze | finalized]
236d0 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 or on one that h
236e0 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 ad.** previously
236f0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 returned [SQLIT
23700 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c E_ERROR] or [SQL
23710 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 ITE_DONE]. Or i
23720 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 t could.** be th
23730 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 e case that the
23740 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
23750 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e nnection is bein
23760 67 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 g used by two or
23770 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 .** more threads
23780 20 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d at the same mom
23790 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a ent in time..**.
237a0 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 ** <b>Goofy Inte
237b0 72 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e rface Alert:</b>
237c0 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 In the legacy i
237d0 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 73 71 nterface, the sq
237e0 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 lite3_step().**
237f0 41 50 49 20 61 6c 77 61 79 73 20 72 65 74 75 72 API always retur
23800 6e 73 20 61 20 67 65 6e 65 72 69 63 20 65 72 72 ns a generic err
23810 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 or code, [SQLITE
23820 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 _ERROR], followi
23830 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 ng any.** error
23840 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 other than [SQLI
23850 54 45 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 TE_BUSY] and [SQ
23860 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 LITE_MISUSE]. Y
23870 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 ou must call.**
23880 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
23890 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 ] or [sqlite3_fi
238a0 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 nalize()] in ord
238b0 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f er to find one o
238c0 66 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 f the.** specifi
238d0 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 c [error codes]
238e0 74 68 61 74 20 62 65 74 74 65 72 20 64 65 73 63 that better desc
238f0 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e ribes the error.
23900 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61 .** We admit tha
23910 74 20 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66 t this is a goof
23920 79 20 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70 y design. The p
23930 72 6f 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 roblem has been
23940 66 69 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 fixed.** with th
23950 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
23960 2e 20 20 49 66 20 79 6f 75 20 70 72 65 70 61 72 . If you prepar
23970 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 e all of your SQ
23980 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 L statements.**
23990 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 using either [sq
239a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
239b0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
239c0 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 prepare16_v2()]
239d0 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 instead.** of th
239e0 65 20 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65 e legacy [sqlite
239f0 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 3_prepare()] and
23a00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
23a10 65 31 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65 e16()] interface
23a20 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d s,.** then the m
23a30 6f 72 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 ore specific [er
23a40 72 6f 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 ror codes] are r
23a50 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 eturned directly
23a60 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 .** by sqlite3_s
23a70 74 65 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 tep(). The use
23a80 6f 66 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 of the "v2" inte
23a90 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 rface is recomme
23aa0 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 nded..**.** Requ
23ab0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
23ac0 33 32 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 3202] [H15304] [
23ad0 48 31 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d H15306] [H15308]
23ae0 20 5b 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c [H15310].*/.SQL
23af0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
23b00 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 te3_step(sqlite3
23b10 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 _stmt*);../*.**
23b20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 CAPI3REF: Number
23b30 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 of columns in a
23b40 20 72 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33 result set {H13
23b50 37 37 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 770} <S10700>.**
23b60 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 .** Returns the
23b70 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 number of values
23b80 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
23b90 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
23ba0 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 t set..**.** Req
23bb0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
23bc0 31 33 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 13771] [H13772].
23bd0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
23be0 74 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 t sqlite3_data_c
23bf0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
23c00 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a t *pStmt);../*.*
23c10 2a 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 * CAPI3REF: Fund
23c20 61 6d 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 amental Datatype
23c30 73 20 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31 s {H10265} <S101
23c40 31 30 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 10><S10120>.** K
23c50 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f EYWORDS: SQLITE_
23c60 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 TEXT.**.** {H102
23c70 36 36 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20 66} Every value
23c80 69 6e 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e in SQLite has on
23c90 65 20 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d e of five fundam
23ca0 65 6e 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a ental datatypes:
23cb0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
23cc0 6c 69 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65 li> 64-bit signe
23cd0 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 d integer.** <li
23ce0 3e 20 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c > 64-bit IEEE fl
23cf0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d oating point num
23d00 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 ber.** <li> stri
23d10 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a ng.** <li> BLOB.
23d20 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 ** <li> NULL.**
23d30 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a </ul> {END}.**.*
23d40 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 * These constant
23d50 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20 s are codes for
23d60 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79 each of those ty
23d70 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 pes..**.** Note
23d80 74 68 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f that the SQLITE_
23d90 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 TEXT constant wa
23da0 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 s also used in S
23db0 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a QLite version 2.
23dc0 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 ** for a complet
23dd0 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65 ely different me
23de0 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 aning. Software
23df0 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 that links agai
23e00 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 nst both.** SQLi
23e10 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 te version 2 and
23e20 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
23e30 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 3 should use SQL
23e40 49 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a ITE3_TEXT, not.*
23e50 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a * SQLITE_TEXT..*
23e60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
23e70 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66 _INTEGER 1.#def
23e80 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ine SQLITE_FLOAT
23e90 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
23ea0 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a LITE_BLOB 4.
23eb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
23ec0 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66 ULL 5.#ifdef
23ed0 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 SQLITE_TEXT.# u
23ee0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 ndef SQLITE_TEXT
23ef0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
23f00 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20 SQLITE_TEXT
23f10 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 3.#endif.#define
23f20 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20 SQLITE3_TEXT
23f30 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 3../*.** CAPI3
23f40 52 45 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 REF: Result Valu
23f50 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20 es From A Query
23f60 7b 48 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30 {H13800} <S10700
23f70 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
23f80 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 column access fu
23f90 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 nctions}.**.** T
23fa0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f hese routines fo
23fb0 72 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 rm the "result s
23fc0 65 74 20 71 75 65 72 79 22 20 69 6e 74 65 72 66 et query" interf
23fd0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ace..**.** These
23fe0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
23ff0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
24000 75 74 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 ut a single colu
24010 6d 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e mn of the curren
24020 74 0a 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 t.** result row
24030 6f 66 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20 of a query. In
24040 65 76 65 72 79 20 63 61 73 65 20 74 68 65 20 66 every case the f
24050 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 irst argument is
24060 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f a pointer.** to
24070 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
24080 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 tatement] that i
24090 73 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 s being evaluate
240a0 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f d (the [sqlite3_
240b0 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 stmt*].** that w
240c0 61 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d as returned from
240d0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
240e0 65 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f e_v2()] or one o
240f0 66 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a f its variants).
24100 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e ** and the secon
24110 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
24120 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 e index of the c
24130 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 olumn for which
24140 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 information.** s
24150 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 hould be returne
24160 64 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 d. The leftmost
24170 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 column of the r
24180 65 73 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 esult set has th
24190 65 20 69 6e 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a e index 0..**.**
241a0 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 If the SQL stat
241b0 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 63 ement does not c
241c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 urrently point t
241d0 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f o a valid row, o
241e0 72 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 r if the.** colu
241f0 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 mn index is out
24200 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72 65 of range, the re
24210 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 sult is undefine
24220 64 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 d..** These rout
24230 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 ines may only be
24240 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 called when the
24250 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c most recent cal
24260 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 l to.** [sqlite3
24270 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 _step()] has ret
24280 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f urned [SQLITE_RO
24290 57 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a 2a W] and neither.*
242a0 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 * [sqlite3_reset
242b0 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65 33 ()] nor [sqlite3
242c0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61 76 _finalize()] hav
242d0 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 75 e been called su
242e0 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49 bsequently..** I
242f0 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 f any of these r
24300 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c outines are call
24310 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 ed after [sqlite
24320 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 3_reset()] or.**
24330 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
24340 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 5b ze()] or after [
24350 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
24360 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 has returned.**
24370 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 something other
24380 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 than [SQLITE_ROW
24390 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 ], the results a
243a0 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a re undefined..**
243b0 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 If [sqlite3_ste
243c0 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 p()] or [sqlite3
243d0 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 _reset()] or [sq
243e0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
243f0 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 20 ].** are called
24400 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e 74 from a different
24410 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 6e thread while an
24420 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 y of these routi
24430 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 69 nes.** are pendi
24440 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ng, then the res
24450 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e ults are undefin
24460 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ed..**.** The sq
24470 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
24480 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 e() routine retu
24490 72 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 rns the.** [SQLI
244a0 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 TE_INTEGER | dat
244b0 61 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20 atype code] for
244c0 74 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 the initial data
244d0 20 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 20 type.** of the
244e0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 result column.
244f0 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c The returned val
24500 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 ue is one of [SQ
24510 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a LITE_INTEGER],.*
24520 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d * [SQLITE_FLOAT]
24530 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c , [SQLITE_TEXT],
24540 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 [SQLITE_BLOB],
24550 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d or [SQLITE_NULL]
24560 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 . The value.**
24570 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
24580 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
24590 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e ) is only meanin
245a0 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 0a gful if no type.
245b0 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 ** conversions h
245c0 61 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 20 ave occurred as
245d0 64 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 2e described below.
245e0 20 20 41 66 74 65 72 20 61 20 74 79 70 65 20 63 After a type c
245f0 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 onversion,.** th
24600 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
24610 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by sqlite3_colu
24620 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e 64 mn_type() is und
24630 65 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 0a efined. Future.
24640 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 ** versions of S
24650 51 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 65 QLite may change
24660 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 the behavior of
24670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
24680 74 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 type().** follow
24690 69 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 65 ing a type conve
246a0 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rsion..**.** If
246b0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 the result is a
246c0 42 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 74 BLOB or UTF-8 st
246d0 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 71 ring then the sq
246e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
246f0 65 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 es().** routine
24700 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
24710 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
24720 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 hat BLOB or stri
24730 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 ng..** If the re
24740 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 36 sult is a UTF-16
24750 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 71 string, then sq
24760 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
24770 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a es() converts.**
24780 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55 the string to U
24790 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 65 TF-8 and then re
247a0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
247b0 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 66 of bytes..** If
247c0 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 the result is a
247d0 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 numeric value t
247e0 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 hen sqlite3_colu
247f0 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a mn_bytes() uses.
24800 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 ** [sqlite3_snpr
24810 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65 intf()] to conve
24820 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f rt that value to
24830 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 a UTF-8 string
24840 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 and returns.** t
24850 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
24860 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e es in that strin
24870 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 g..** The value
24880 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f returned does no
24890 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a 65 t include the ze
248a0 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 74 ro terminator at
248b0 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 the end.** of t
248c0 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 he string. For
248d0 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 6c clarity: the val
248e0 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
248f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
24900 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72 bytes in the str
24910 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d ing, not the num
24920 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
24930 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 73 s..**.** Strings
24940 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
24950 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
24960 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 () and sqlite3_c
24970 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 0a olumn_text16(),.
24980 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 74 ** even empty st
24990 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 79 rings, are alway
249a0 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 s zero terminate
249b0 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a d. The return.*
249c0 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c * value from sql
249d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
249e0 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 () for a zero-le
249f0 6e 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e 20 ngth BLOB is an
24a00 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f 69 arbitrary.** poi
24a10 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 65 nter, possibly e
24a20 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ven a NULL point
24a30 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
24a40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
24a50 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 69 es16() routine i
24a60 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c s similar to sql
24a70 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
24a80 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 65 s().** but leave
24a90 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 s the result in
24aa0 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 UTF-16 in native
24ab0 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 74 byte order inst
24ac0 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a ead of UTF-8..**
24ad0 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e The zero termin
24ae0 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63 6c ator is not incl
24af0 75 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f 75 uded in this cou
24b00 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62 nt..**.** The ob
24b10 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 ject returned by
24b20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
24b30 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e 0a _value()] is an.
24b40 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 ** [unprotected
24b50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
24b60 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 6f bject. An unpro
24b70 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
24b80 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d alue object.** m
24b90 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 ay only be used
24ba0 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 69 with [sqlite3_bi
24bb0 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 nd_value()] and
24bc0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
24bd0 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 20 value()]..** If
24be0 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 the [unprotected
24bf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
24c00 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 object returned
24c10 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 by.** [sqlite3_c
24c20 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 olumn_value()] i
24c30 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f 74 s used in any ot
24c40 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 69 her way, includi
24c50 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72 ng calls.** to r
24c60 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 outines like [sq
24c70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
24c80 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c )], [sqlite3_val
24c90 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f ue_text()],.** o
24ca0 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r [sqlite3_value
24cb0 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e 20 _bytes()], then
24cc0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
24cd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
24ce0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
24cf0 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 attempt to conve
24d00 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 65 rt the value whe
24d10 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 re appropriate.
24d20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c For.** example,
24d30 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c if the internal
24d40 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
24d50 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 74 is FLOAT and a t
24d60 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 ext result.** is
24d70 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 6c requested, [sql
24d80 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d ite3_snprintf()]
24d90 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 is used interna
24da0 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 lly to perform t
24db0 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e he.** conversion
24dc0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 automatically.
24dd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
24de0 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68 65 able details the
24df0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 conversions.**
24e00 74 68 61 74 20 61 72 65 20 61 70 70 6c 69 65 64 that are applied
24e10 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
24e20 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 ote>.** <table b
24e30 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 order="1">.** <t
24e40 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c r><th> Internal<
24e50 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65 71 br>Type <th> Req
24e60 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20 3c uested<br>Type <
24e70 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0a th> Conversion.
24e80 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 **.** <tr><td>
24e90 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54 NULL <td> INT
24ea0 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 75 EGER <td> Resu
24eb0 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c lt is 0.** <tr><
24ec0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
24ed0 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e > FLOAT <td>
24ee0 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a 2a Result is 0.0.*
24ef0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c * <tr><td> NULL
24f00 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 <td> TEXT
24f10 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 <td> Result i
24f20 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a s NULL pointer.*
24f30 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c * <tr><td> NULL
24f40 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 <td> BLOB
24f50 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 <td> Result i
24f60 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a s NULL pointer.*
24f70 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 * <tr><td> INTEG
24f80 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 ER <td> FLOAT
24f90 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 <td> Convert
24fa0 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f 20 from integer to
24fb0 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 float.** <tr><td
24fc0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 > INTEGER <td>
24fd0 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 TEXT <td> A
24fe0 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f SCII rendering o
24ff0 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a f the integer.**
25000 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 <tr><td> INTEGE
25010 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 R <td> BLOB
25020 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 49 <td> Same as I
25030 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a 20 NTEGER->TEXT.**
25040 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <tr><td> FLOAT
25050 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 <td> INTEGER
25060 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 <td> Convert fr
25070 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74 65 om float to inte
25080 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 ger.** <tr><td>
25090 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 FLOAT <td>
250a0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 TEXT <td> ASC
250b0 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 II rendering of
250c0 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 the float.** <tr
250d0 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c ><td> FLOAT <
250e0 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 td> BLOB <t
250f0 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41 54 d> Same as FLOAT
25100 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 ->TEXT.** <tr><t
25110 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e d> TEXT <td>
25120 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
25130 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 Use atoi().** <t
25140 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 r><td> TEXT
25150 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c <td> FLOAT <
25160 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a 2a td> Use atof().*
25170 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 * <tr><td> TEXT
25180 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 <td> BLOB
25190 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e 67 <td> No chang
251a0 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 e.** <tr><td> B
251b0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 LOB <td> INTE
251c0 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 GER <td> Conve
251d0 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 rt to TEXT then
251e0 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 use atoi().** <t
251f0 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 r><td> BLOB
25200 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c <td> FLOAT <
25210 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 td> Convert to T
25220 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f EXT then use ato
25230 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 f().** <tr><td>
25240 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 20 BLOB <td>
25250 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64 64 TEXT <td> Add
25260 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 a zero terminat
25270 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a 20 or if needed.**
25280 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c </table>.** </bl
25290 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
252a0 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65 20 The table above
252b0 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65 20 makes reference
252c0 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 to standard C li
252d0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 brary functions
252e0 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61 74 atoi().** and at
252f0 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64 6f of(). SQLite do
25300 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75 73 es not really us
25310 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e e these function
25320 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a 2a s. It has its.*
25330 2a 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e 74 * own equivalent
25340 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e internal routin
25350 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 20 es. The atoi()
25360 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 73 and atof() names
25370 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e 20 are.** used in
25380 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 72 the table for br
25390 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 73 evity and becaus
253a0 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 6c e they are famil
253b0 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43 iar to most.** C
253c0 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a programmers..**
253d0 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 .** Note that wh
253e0 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 en type conversi
253f0 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e 74 ons occur, point
25400 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 ers returned by
25410 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 prior.** calls t
25420 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e o sqlite3_column
25430 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 _blob(), sqlite3
25440 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20 _column_text(),
25450 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 and/or.** sqlite
25460 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
25470 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64 ) may be invalid
25480 61 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63 6f ated..** Type co
25490 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70 6f nversions and po
254a0 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74 69 inter invalidati
254b0 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72 0a ons might occur.
254c0 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 ** in the follow
254d0 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a ing cases:.**.**
254e0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 <ul>.** <li> Th
254f0 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e e initial conten
25500 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64 20 t is a BLOB and
25510 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
25520 65 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 ext() or.**
25530 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25540 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c text16() is call
25550 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 6d ed. A zero-term
25560 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 inator might.**
25570 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65 20 need to be
25580 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 74 72 added to the str
25590 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 ing.</li>.** <li
255a0 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f > The initial co
255b0 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 74 ntent is UTF-8 t
255c0 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f ext and sqlite3_
255d0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 column_bytes16()
255e0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 or.** sqli
255f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
25600 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 6() is called.
25610 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 The content must
25620 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a be converted.**
25630 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36 2e to UTF-16.
25640 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 </li>.** <li> Th
25650 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e e initial conten
25660 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 74 t is UTF-16 text
25670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c and sqlite3_col
25680 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a umn_bytes() or.*
25690 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 * sqlite3_c
256a0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 20 olumn_text() is
256b0 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e called. The con
256c0 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e tent must be con
256d0 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 verted.** t
256e0 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a o UTF-8.</li>.**
256f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e </ul>.**.** Con
25700 76 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 6e versions between
25710 20 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 54 UTF-16be and UT
25720 46 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 79 F-16le are alway
25730 73 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 s done in place
25740 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e and do.** not in
25750 76 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72 validate a prior
25760 20 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68 pointer, though
25770 20 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 63 of course the c
25780 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75 ontent of the bu
25790 66 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 65 ffer.** that the
257a0 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 70 prior pointer p
257b0 6f 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 61 oints to will ha
257c0 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
257d0 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a 2a . Other kinds.*
257e0 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 * of conversion
257f0 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 are done in plac
25800 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f 73 e when it is pos
25810 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 74 sible, but somet
25820 69 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72 65 imes they.** are
25830 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61 6e not possible an
25840 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65 73 d in those cases
25850 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73 20 prior pointers
25860 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e are invalidated.
25870 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65 73 .**.** The safes
25880 74 20 61 6e 64 20 65 61 73 69 65 73 74 20 74 6f t and easiest to
25890 20 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63 79 remember policy
258a0 20 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 is to invoke th
258b0 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 ese routines.**
258c0 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f in one of the fo
258d0 6c 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a 2a llowing ways:.**
258e0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 .** <ul>.** <li
258f0 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f >sqlite3_column_
25900 74 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 20 text() followed
25910 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
25920 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a n_bytes()</li>.*
25930 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 * <li>sqlite3_c
25940 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c olumn_blob() fol
25950 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 lowed by sqlite3
25960 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c _column_bytes()<
25970 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c /li>.** <li>sql
25980 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
25990 31 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 16() followed by
259a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
259b0 62 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a bytes16()</li>.*
259c0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e * </ul>.**.** In
259d0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 6f other words, yo
259e0 75 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 u should call sq
259f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
25a00 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f t(),.** sqlite3_
25a10 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f column_blob(), o
25a20 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e r sqlite3_column
25a30 5f 74 65 78 74 31 36 28 29 20 66 69 72 73 74 20 _text16() first
25a40 74 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 73 to force the res
25a50 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 ult.** into the
25a60 64 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c 20 desired format,
25a70 74 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69 then invoke sqli
25a80 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
25a90 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 () or.** sqlite3
25aa0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
25ab0 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 69 ) to find the si
25ac0 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ze of the result
25ad0 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61 . Do not mix ca
25ae0 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 lls.** to sqlite
25af0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
25b00 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d or sqlite3_colum
25b10 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63 61 n_blob() with ca
25b20 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 lls to.** sqlite
25b30 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
25b40 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20 6d (), and do not m
25b50 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ix calls to sqli
25b60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
25b70 36 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c 6c 6().** with call
25b80 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c s to sqlite3_col
25b90 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a 0a umn_bytes()..**.
25ba0 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 ** The pointers
25bb0 72 65 74 75 72 6e 65 64 20 61 72 65 20 76 61 6c returned are val
25bc0 69 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65 20 id until a type
25bd0 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 conversion occur
25be0 73 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62 65 s as.** describe
25bf0 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74 69 d above, or unti
25c00 6c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 l [sqlite3_step(
25c10 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
25c20 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 eset()] or.** [s
25c30 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
25c40 29 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 )] is called. T
25c50 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 he memory space
25c60 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74 72 used to hold str
25c70 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42 ings.** and BLOB
25c80 73 20 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d s is freed autom
25c90 61 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c 62 atically. Do <b
25ca0 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74 68 >not</b> pass th
25cb0 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 e pointers retur
25cc0 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ned.** [sqlite3_
25cd0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20 column_blob()],
25ce0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
25cf0 74 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69 6e text()], etc. in
25d00 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 to.** [sqlite3_f
25d10 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 ree()]..**.** If
25d20 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
25d30 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
25d40 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 61 s during the eva
25d50 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a luation of any.*
25d60 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 * of these routi
25d70 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20 76 nes, a default v
25d80 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 alue is returned
25d90 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 . The default v
25da0 61 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68 65 alue.** is eithe
25db0 72 20 74 68 65 20 69 6e 74 65 67 65 72 20 30 2c r the integer 0,
25dc0 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f the floating po
25dd0 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c 20 int number 0.0,
25de0 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 or a NULL.** poi
25df0 6e 74 65 72 2e 20 20 53 75 62 73 65 71 75 65 6e nter. Subsequen
25e00 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 t calls to [sqli
25e10 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 77 te3_errcode()] w
25e20 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 ill return.** [S
25e30 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a QLITE_NOMEM]..**
25e40 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
25e50 3a 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b 48 :.** [H13803] [H
25e60 31 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d 20 13806] [H13809]
25e70 5b 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31 35 [H13812] [H13815
25e80 5d 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33 38 ] [H13818] [H138
25e90 32 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a 20 21] [H13824].**
25ea0 5b 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33 30 [H13827] [H13830
25eb0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
25ec0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
25ed0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
25ee0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
25ef0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
25f00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
25f10 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 _column_bytes(sq
25f20 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
25f30 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
25f40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
25f50 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 olumn_bytes16(sq
25f60 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
25f70 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
25f80 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 PI double sqlite
25f90 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 3_column_double(
25fa0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
25fb0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
25fc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
25fd0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 _column_int(sqli
25fe0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
25ff0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
26000 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
26010 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e qlite3_column_in
26020 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 t64(sqlite3_stmt
26030 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 *, int iCol);.SQ
26040 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 LITE_API const u
26050 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 nsigned char *sq
26060 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
26070 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c t(sqlite3_stmt*,
26080 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
26090 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
260a0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
260b0 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 n_text16(sqlite3
260c0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
260d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
260e0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
260f0 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 _type(sqlite3_st
26100 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
26110 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
26120 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 e3_value *sqlite
26130 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 3_column_value(s
26140 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
26150 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t iCol);../*.**
26160 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f CAPI3REF: Destro
26170 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 y A Prepared Sta
26180 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 tement Object {H
26190 31 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e 3c 13300} <S70300><
261a0 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30100>.**.** Th
261b0 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 e sqlite3_finali
261c0 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 ze() function is
261d0 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74 called to delet
261e0 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 e a [prepared st
261f0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 atement]..** If
26200 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 the statement wa
26210 73 20 65 78 65 63 75 74 65 64 20 73 75 63 63 65 s executed succe
26220 73 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20 65 ssfully or not e
26230 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c 20 xecuted at all,
26240 74 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f then.** SQLITE_O
26250 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
26260 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 f execution of t
26270 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 he statement fai
26280 6c 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b led then an.** [
26290 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b error code] or [
262a0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
262b0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
262c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
262d0 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c tine can be call
262e0 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 ed at any point
262f0 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 75 during the execu
26300 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 5b tion of the.** [
26310 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
26320 6e 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69 72 nt]. If the vir
26330 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 tual machine has
26340 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 not.** complete
26350 64 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65 6e d execution when
26360 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
26370 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69 73 called, that is
26380 20 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 like.** encount
26390 65 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6f ering an error o
263a0 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6e r an [sqlite3_in
263b0 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72 72 terrupt | interr
263c0 75 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c upt]..** Incompl
263d0 65 74 65 20 75 70 64 61 74 65 73 20 6d 61 79 20 ete updates may
263e0 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
263f0 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 nd transactions
26400 63 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65 70 canceled,.** dep
26410 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63 69 ending on the ci
26420 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e 64 rcumstances, and
26430 20 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 the.** [error c
26440 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77 69 ode] returned wi
26450 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 42 ll be [SQLITE_AB
26460 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ORT]..**.** Requ
26470 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
26480 31 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a 2a 1302] [H11304].*
26490 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
264a0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a sqlite3_finaliz
264b0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
264c0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
264d0 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 API3REF: Reset A
264e0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
264f0 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 ent Object {H133
26500 33 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 30} <S70300>.**.
26510 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
26520 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 eset() function
26530 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 is called to res
26540 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 et a [prepared s
26550 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a tatement].** obj
26560 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20 ect back to its
26570 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72 initial state, r
26580 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78 eady to be re-ex
26590 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 ecuted..** Any S
265a0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 QL statement var
265b0 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20 iables that had
265c0 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20 values bound to
265d0 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68 them using.** th
265e0 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f e [sqlite3_bind_
265f0 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 blob | sqlite3_b
26600 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74 ind_*() API] ret
26610 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73 ain their values
26620 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65 ..** Use [sqlite
26630 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 3_clear_bindings
26640 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 ()] to reset the
26650 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a bindings..**.**
26660 20 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b 73 {H11332} The [s
26670 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d qlite3_reset(S)]
26680 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 74 interface reset
26690 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 s the [prepared
266a0 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 statement] S.**
266b0 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74 6f back to
266c0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
266d0 66 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a f its program..*
266e0 2a 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49 66 *.** {H11334} If
266f0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
26700 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
26710 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 3_step(S)] for t
26720 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
26730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
26740 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 5b nt] S returned [
26750 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b SQLITE_ROW] or [
26760 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a SQLITE_DONE],.**
26770 20 20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 or if
26780 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
26790 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f ] has never befo
267a0 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f re been called o
267b0 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 n S,.**
267c0 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 then [sqlite3_r
267d0 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 eset(S)] returns
267e0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a [SQLITE_OK]..**
267f0 0a 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66 20 .** {H11336} If
26800 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
26810 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
26820 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68 _step(S)] for th
26830 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
26840 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
26850 74 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20 61 t] S indicated a
26860 6e 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a n error, then.**
26870 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
26880 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74 e3_reset(S)] ret
26890 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 69 urns an appropri
268a0 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d ate [error code]
268b0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38 7d ..**.** {H11338}
268c0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 The [sqlite3_re
268d0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 set(S)] interfac
268e0 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 e does not chang
268f0 65 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 e the values.**
26900 20 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 20 of any
26910 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
26920 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 ob|bindings] on
26930 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
26940 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 atement] S..*/.S
26950 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
26960 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 lite3_reset(sqli
26970 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
26980 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
26990 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64 F: Create Or Red
269a0 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69 efine SQL Functi
269b0 6f 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53 32 ons {H16100} <S2
269c0 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0200>.** KEYWORD
269d0 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65 S: {function cre
269e0 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a ation routines}.
269f0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 ** KEYWORDS: {ap
26a00 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
26a10 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a d SQL function}.
26a20 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 ** KEYWORDS: {ap
26a30 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
26a40 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d d SQL functions}
26a50 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77 6f .**.** These two
26a60 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c 6c functions (coll
26a70 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 ectively known a
26a80 73 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 s "function crea
26a90 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29 0a tion routines").
26aa0 2a 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ** are used to a
26ab0 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 dd SQL functions
26ac0 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 6f or aggregates o
26ad0 72 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74 68 r to redefine th
26ae0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 e behavior.** of
26af0 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 75 existing SQL fu
26b00 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 nctions or aggre
26b10 67 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c 79 gates. The only
26b20 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 difference betw
26b30 65 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20 69 een the.** two i
26b40 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e s that the secon
26b50 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 d parameter, the
26b60 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73 63 name of the (sc
26b70 61 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20 6f alar) function o
26b80 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 r.** aggregate,
26b90 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 is encoded in UT
26ba0 46 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33 5f F-8 for sqlite3_
26bb0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
26bc0 29 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a 20 ) and UTF-16.**
26bd0 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 for sqlite3_crea
26be0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 2e te_function16().
26bf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
26c00 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
26c10 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
26c20 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63 68 ection] to which
26c30 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 the SQL.** func
26c40 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61 64 tion is to be ad
26c50 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67 6c ded. If a singl
26c60 65 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20 6d e program uses m
26c70 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 ore than one dat
26c80 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
26c90 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 ion internally,
26ca0 74 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f then SQL functio
26cb0 6e 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 64 ns must be added
26cc0 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 6f individually to
26cd0 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61 73 .** each databas
26ce0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a e connection..**
26cf0 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70 .** The second p
26d00 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
26d10 6e 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 name of the SQL
26d20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 function to be c
26d30 72 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65 64 reated or.** red
26d40 65 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65 6e efined. The len
26d50 67 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 gth of the name
26d60 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 35 is limited to 25
26d70 35 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 69 5 bytes, exclusi
26d80 76 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65 72 ve of.** the zer
26d90 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 4e o-terminator. N
26da0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d ote that the nam
26db0 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 69 e length limit i
26dc0 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 0a s in bytes, not.
26dd0 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 ** characters.
26de0 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 Any attempt to c
26df0 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f 6e reate a function
26e00 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 6e with a longer n
26e10 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 ame.** will resu
26e20 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45 52 lt in [SQLITE_ER
26e30 52 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75 72 ROR] being retur
26e40 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ned..**.** The t
26e50 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 28 hird parameter (
26e60 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65 20 nArg).** is the
26e70 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
26e80 6e 74 73 20 74 68 61 74 20 74 68 65 20 53 51 4c nts that the SQL
26e90 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
26ea0 61 67 67 72 65 67 61 74 65 20 74 61 6b 65 73 2e aggregate takes.
26eb0 20 49 66 20 74 68 69 73 20 70 61 72 61 6d 65 74 If this paramet
26ec0 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 er is negative,
26ed0 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66 75 6e then the SQL fun
26ee0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
26ef0 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65 20 61 egate may take a
26f00 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 ny number of arg
26f10 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 uments..**.** Th
26f20 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 e fourth paramet
26f30 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20 73 70 er, eTextRep, sp
26f40 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a 2a 20 ecifies what.**
26f50 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 20 74 [SQLITE_UTF8 | t
26f60 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68 ext encoding] th
26f70 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 is SQL function
26f80 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a 20 69 prefers for.** i
26f90 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 ts parameters.
26fa0 41 6e 79 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e Any SQL function
26fb0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
26fc0 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 74 should be able t
26fd0 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 o work.** work w
26fe0 69 74 68 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 ith UTF-8, UTF-1
26ff0 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31 36 62 65 6le, or UTF-16be
27000 2e 20 20 42 75 74 20 73 6f 6d 65 20 69 6d 70 6c . But some impl
27010 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 20 ementations may
27020 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 69 63 be.** more effic
27030 69 65 6e 74 20 77 69 74 68 20 6f 6e 65 20 65 6e ient with one en
27040 63 6f 64 69 6e 67 20 74 68 61 6e 20 61 6e 6f 74 coding than anot
27050 68 65 72 2e 20 20 49 74 20 69 73 20 61 6c 6c 6f her. It is allo
27060 77 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 wed to.** invoke
27070 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
27080 66 75 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 function() or sq
27090 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
270a0 63 74 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 ction16() multip
270b0 6c 65 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 le.** times with
270c0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
270d0 6f 6e 20 62 75 74 20 77 69 74 68 20 64 69 66 66 on but with diff
270e0 65 72 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 erent values of
270f0 65 54 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65 eTextRep..** Whe
27100 6e 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 n multiple imple
27110 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 mentations of th
27120 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 e same function
27130 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 are available, S
27140 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 QLite.** will pi
27150 63 6b 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 ck the one that
27160 69 6e 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 involves the lea
27170 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 st amount of dat
27180 61 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a a conversion..**
27190 20 49 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c If there is onl
271a0 79 20 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 y a single imple
271b0 6d 65 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 mentation which
271c0 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 does not care wh
271d0 61 74 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 at text.** encod
271e0 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 ing is used, the
271f0 6e 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 n the fourth arg
27200 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 ument should be
27210 5b 53 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a [SQLITE_ANY]..**
27220 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20 70 61 .** The fifth pa
27230 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 rameter is an ar
27240 62 69 74 72 61 72 79 20 70 6f 69 6e 74 65 72 2e bitrary pointer.
27250 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 The implementa
27260 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 66 tion of the.** f
27270 75 6e 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e unction can gain
27280 20 61 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 access to this
27290 70 6f 69 6e 74 65 72 20 75 73 69 6e 67 20 5b 73 pointer using [s
272a0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
272b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ()]..**.** The s
272c0 65 76 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61 eventh, eighth a
272d0 6e 64 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74 nd ninth paramet
272e0 65 72 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 ers, xFunc, xSte
272f0 70 20 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 p and xFinal, ar
27300 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f e.** pointers to
27310 20 43 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63 C-language func
27320 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
27330 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e ment the SQL fun
27340 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
27350 65 67 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20 egate. A scalar
27360 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 SQL function req
27370 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 uires an impleme
27380 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 ntation of the x
27390 46 75 6e 63 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b Func.** callback
273a0 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e only, NULL poin
273b0 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70 ters should be p
273c0 61 73 73 65 64 20 61 73 20 74 68 65 20 78 53 74 assed as the xSt
273d0 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a ep and xFinal.**
273e0 20 70 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20 parameters. An
273f0 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 aggregate SQL fu
27400 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 nction requires
27410 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f an implementatio
27420 6e 20 6f 66 20 78 53 74 65 70 0a 2a 2a 20 61 6e n of xStep.** an
27430 64 20 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c d xFinal and NUL
27440 4c 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 L should be pass
27450 65 64 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f ed for xFunc. To
27460 20 64 65 6c 65 74 65 20 61 6e 20 65 78 69 73 74 delete an exist
27470 69 6e 67 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 ing.** SQL funct
27480 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 ion or aggregate
27490 2c 20 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20 , pass NULL for
274a0 61 6c 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69 all three functi
274b0 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a on callbacks..**
274c0 0a 2a 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74 .** It is permit
274d0 74 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 ted to register
274e0 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 multiple impleme
274f0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
27500 73 61 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e same.** function
27510 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 s with the same
27520 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20 65 69 name but with ei
27530 74 68 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e ther differing n
27540 75 6d 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67 umbers of.** arg
27550 75 6d 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72 uments or differ
27560 69 6e 67 20 70 72 65 66 65 72 72 65 64 20 74 65 ing preferred te
27570 78 74 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53 xt encodings. S
27580 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a QLite will use.*
27590 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 * the implementa
275a0 74 69 6f 6e 20 6d 6f 73 74 20 63 6c 6f 73 65 6c tion most closel
275b0 79 20 6d 61 74 63 68 65 73 20 74 68 65 20 77 61 y matches the wa
275c0 79 20 69 6e 20 77 68 69 63 68 20 74 68 65 0a 2a y in which the.*
275d0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 * SQL function i
275e0 73 20 75 73 65 64 2e 20 20 41 20 66 75 6e 63 74 s used. A funct
275f0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ion implementati
27600 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 on with a non-ne
27610 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72 67 20 70 gative.** nArg p
27620 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 65 arameter is a be
27630 74 74 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20 tter match than
27640 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 a function imple
27650 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 0a 2a mentation with.*
27660 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 41 72 * a negative nAr
27670 67 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 g. A function w
27680 68 65 72 65 20 74 68 65 20 70 72 65 66 65 72 72 here the preferr
27690 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 ed text encoding
276a0 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 .** matches the
276b0 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e database encodin
276c0 67 20 69 73 20 61 20 62 65 74 74 65 72 0a 2a 2a g is a better.**
276d0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 match than a fu
276e0 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 nction where the
276f0 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 64 69 66 encoding is dif
27700 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 ferent. .** A f
27710 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 unction where th
27720 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 e encoding diffe
27730 72 65 6e 63 65 20 69 73 20 62 65 74 77 65 65 6e rence is between
27740 20 55 54 46 31 36 6c 65 20 61 6e 64 20 55 54 46 UTF16le and UTF
27750 31 36 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 16be.** is a clo
27760 73 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20 61 ser match than a
27770 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
27780 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 the encoding dif
27790 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62 65 ference is.** be
277a0 74 77 65 65 6e 20 55 54 46 38 20 61 6e 64 20 55 tween UTF8 and U
277b0 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c TF16..**.** Buil
277c0 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6d t-in functions m
277d0 61 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 ay be overloaded
277e0 20 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61 74 by new applicat
277f0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
27800 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 66 69 tions..** The fi
27810 72 73 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d rst application-
27820 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
27830 20 77 69 74 68 20 61 20 67 69 76 65 6e 20 6e 61 with a given na
27840 6d 65 20 6f 76 65 72 72 69 64 65 73 20 61 6c 6c me overrides all
27850 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e .** built-in fun
27860 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 73 61 ctions in the sa
27870 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
27880 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68 20 74 68 nection] with th
27890 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 e same name..**
278a0 53 75 62 73 65 71 75 65 6e 74 20 61 70 70 6c 69 Subsequent appli
278b0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
278c0 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 unctions of the
278d0 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f same name only o
278e0 76 65 72 72 69 64 65 20 0a 2a 2a 20 70 72 69 6f verride .** prio
278f0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 r application-de
27900 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 fined functions
27910 74 68 61 74 20 61 72 65 20 61 6e 20 65 78 61 63 that are an exac
27920 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 0a t match for the.
27930 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 ** number of par
27940 61 6d 65 74 65 72 73 20 61 6e 64 20 70 72 65 66 ameters and pref
27950 65 72 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e 0a erred encoding..
27960 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c 69 63 61 **.** An applica
27970 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
27980 63 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 ction is permitt
27990 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74 68 65 72 ed to call other
279a0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 .** SQLite inter
279b0 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 2c faces. However,
279c0 20 73 75 63 68 20 63 61 6c 6c 73 20 6d 75 73 74 such calls must
279d0 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 not.** close th
279e0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
279f0 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 ction nor finali
27a00 7a 65 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 ze or reset the
27a10 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 prepared.** stat
27a20 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20 74 ement in which t
27a30 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 he function is r
27a40 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 unning..**.** Re
27a50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
27a60 48 31 36 31 30 33 5d 20 5b 48 31 36 31 30 36 5d H16103] [H16106]
27a70 20 5b 48 31 36 31 30 39 5d 20 5b 48 31 36 31 31 [H16109] [H1611
27a80 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b 48 31 36 2] [H16118] [H16
27a90 31 32 31 5d 20 5b 48 31 36 31 32 34 5d 20 5b 48 121] [H16124] [H
27aa0 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31 33 16127].** [H1613
27ab0 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31 36 0] [H16133] [H16
27ac0 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b 48 136] [H16139] [H
27ad0 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16142].*/.SQLITE
27ae0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
27af0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
27b00 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
27b10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
27b20 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 FunctionName,.
27b30 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 int nArg,. int
27b40 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 eTextRep,. void
27b50 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 *pApp,. void (
27b60 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f *xFunc)(sqlite3_
27b70 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
27b80 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 ite3_value**),.
27b90 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
27ba0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
27bb0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
27bc0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
27bd0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 Final)(sqlite3_c
27be0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 ontext*).);.SQLI
27bf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
27c00 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
27c10 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 on16(. sqlite3
27c20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 *db,. const voi
27c30 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 d *zFunctionName
27c40 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 ,. int nArg,.
27c50 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 int eTextRep,.
27c60 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f void *pApp,. vo
27c70 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 id (*xFunc)(sqli
27c80 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 te3_context*,int
27c90 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a ,sqlite3_value**
27ca0 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 ),. void (*xSte
27cb0 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 p)(sqlite3_conte
27cc0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
27cd0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 value**),. void
27ce0 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 (*xFinal)(sqlit
27cf0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a e3_context*).);.
27d00 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
27d10 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73 20 Text Encodings
27d20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30 30 {H10267} <S50200
27d30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a > <H16100>.**.**
27d40 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 20 These constant
27d50 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 define integer c
27d60 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65 73 odes that repres
27d70 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a ent the various.
27d80 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 ** text encoding
27d90 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53 s supported by S
27da0 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e QLite..*/.#defin
27db0 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 e SQLITE_UTF8
27dc0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
27dd0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 e SQLITE_UTF16LE
27de0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
27df0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 e SQLITE_UTF16BE
27e00 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
27e10 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 20 e SQLITE_UTF16
27e20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20 4 /*
27e30 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 Use native byte
27e40 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 order */.#define
27e50 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20 20 SQLITE_ANY
27e60 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20 73 5 /* s
27e70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
27e80 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 nction only */.#
27e90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 define SQLITE_UT
27ea0 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20 20 F16_ALIGNED 8
27eb0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 /* sqlite3_cre
27ec0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e ate_collation on
27ed0 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ly */../*.** CAP
27ee0 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74 65 I3REF: Deprecate
27ef0 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 d Functions.** D
27f00 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 EPRECATED.**.**
27f10 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 These functions
27f20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 5d are [deprecated]
27f30 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d . In order to m
27f40 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 aintain.** backw
27f50 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
27f60 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 6f ty with older co
27f70 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 69 de, these functi
27f80 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a ons continue .**
27f90 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 to be supported
27fa0 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77 20 . However, new
27fb0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f applications sho
27fc0 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65 uld avoid.** the
27fd0 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 75 use of these fu
27fe0 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c nctions. To hel
27ff0 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f 70 p encourage peop
28000 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 le to avoid.** u
28010 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 74 sing these funct
28020 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f 74 ions, we are not
28030 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79 going to tell y
28040 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e ou what they do.
28050 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
28060 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 TE_OMIT_DEPRECAT
28070 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 ED.SQLITE_API SQ
28080 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 LITE_DEPRECATED
28090 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 int sqlite3_aggr
280a0 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 egate_count(sqli
280b0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 te3_context*);.S
280c0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
280d0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 _DEPRECATED int
280e0 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 sqlite3_expired(
280f0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
28100 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
28110 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 E_DEPRECATED int
28120 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 sqlite3_transfe
28130 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
28140 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74 65 e3_stmt*, sqlite
28150 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 3_stmt*);.SQLITE
28160 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
28170 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
28180 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 e3_global_recove
28190 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f r(void);.SQLITE_
281a0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 API SQLITE_DEPRE
281b0 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 74 CATED void sqlit
281c0 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 e3_thread_cleanu
281d0 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f p(void);.SQLITE_
281e0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 API SQLITE_DEPRE
281f0 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 CATED int sqlite
28200 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 3_memory_alarm(v
28210 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c oid(*)(void*,sql
28220 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 2c ite3_int64,int),
28230 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e void*,sqlite3_in
28240 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a t64);.#endif../*
28250 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 .** CAPI3REF: Ob
28260 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e 63 taining SQL Func
28270 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 56 tion Parameter V
28280 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20 3c alues {H15100} <
28290 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
282a0 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70 e C-language imp
282b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53 lementation of S
282c0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 QL functions and
282d0 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65 73 aggregates uses
282e0 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 20 .** this set of
282f0 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e interface routin
28300 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 es to access the
28310 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65 parameter value
28320 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e 63 s on.** the func
28330 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 tion or aggregat
28340 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 75 e..**.** The xFu
28350 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20 66 nc (for scalar f
28360 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53 74 unctions) or xSt
28370 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61 74 ep (for aggregat
28380 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a 2a es) parameters.*
28390 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 * to [sqlite3_cr
283a0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
283b0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 and [sqlite3_cr
283c0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
283d0 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61 6c )].** define cal
283e0 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70 6c lbacks that impl
283f0 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 ement the SQL fu
28400 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
28410 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20 34 egates..** The 4
28420 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
28430 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73 20 these callbacks
28440 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 is an array of p
28450 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 ointers to.** [p
28460 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
28470 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e _value] objects.
28480 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 5b There is one [
28490 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
284a0 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 63 bject for.** eac
284b0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
284c0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e he SQL function.
284d0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
284e0 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 are used to.**
284f0 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20 66 extract values f
28500 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 rom the [sqlite3
28510 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e _value] objects.
28520 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
28530 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 tines work only
28540 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64 20 with [protected
28550 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
28560 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 bjects..** Any a
28570 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 ttempt to use th
28580 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 20 ese routines on
28590 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 an [unprotected
285a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a sqlite3_value].*
285b0 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74 73 * object results
285c0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 in undefined be
285d0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 havior..**.** Th
285e0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 ese routines wor
285f0 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 k just like the
28600 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 63 corresponding [c
28610 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e olumn access fun
28620 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 ctions].** excep
28630 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72 6f t that these ro
28640 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69 utines take a si
28650 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 20 ngle [protected
28660 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
28670 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 bject.** pointer
28680 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b 73 instead of a [s
28690 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f qlite3_stmt*] po
286a0 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74 inter and an int
286b0 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 eger column numb
286c0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
286d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
286e0 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65 16() interface e
286f0 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31 36 xtracts a UTF-16
28700 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 string.** in th
28710 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72 e native byte-or
28720 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20 der of the host
28730 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a machine. The.**
28740 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
28750 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73 71 ext16be() and sq
28760 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
28770 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 16le() interface
28780 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46 s.** extract UTF
28790 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20 62 -16 strings as b
287a0 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 ig-endian and li
287b0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 ttle-endian resp
287c0 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ectively..**.**
287d0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
287e0 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29 e_numeric_type()
287f0 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d interface attem
28800 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 pts to apply.**
28810 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 numeric affinity
28820 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20 to the value.
28830 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
28840 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a an attempt is.**
28850 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 made to convert
28860 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e the value to an
28870 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 integer or floa
28880 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a ting point. If.
28890 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72 ** such a conver
288a0 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 sion is possible
288b0 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 without loss of
288c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e information (in
288d0 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c other.** words,
288e0 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 if the value is
288f0 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c a string that l
28900 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 ooks like a numb
28910 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 er).** then the
28920 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65 conversion is pe
28930 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77 rformed. Otherw
28940 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f ise no conversio
28950 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65 n occurs..** The
28960 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 [SQLITE_INTEGER
28970 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74 | datatype] aft
28980 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 er conversion is
28990 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
289a0 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 74 Please pay part
289b0 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f 6e icular attention
289c0 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 61 to the fact tha
289d0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65 t the pointer re
289e0 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b turned.** from [
289f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
28a00 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f ob()], [sqlite3_
28a10 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f value_text()], o
28a20 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 r.** [sqlite3_va
28a30 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 61 lue_text16()] ca
28a40 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 n be invalidated
28a50 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 by a subsequent
28a60 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
28a70 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
28a80 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 ()], [sqlite3_va
28a90 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c 20 lue_bytes16()],
28aa0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
28ab0 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 ext()],.** or [s
28ac0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
28ad0 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 t16()]..**.** Th
28ae0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 ese routines mus
28af0 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d t be called from
28b00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
28b10 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 as.** the SQL f
28b20 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70 unction that sup
28b30 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 74 plied the [sqlit
28b40 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 6d e3_value*] param
28b50 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 eters..**.** Req
28b60 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
28b70 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d 20 15103] [H15106]
28b80 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31 32 [H15109] [H15112
28b90 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35 31 ] [H15115] [H151
28ba0 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48 31 18] [H15121] [H1
28bb0 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32 37 5124].** [H15127
28bc0 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35 31 ] [H15130] [H151
28bd0 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a 33] [H15136].*/.
28be0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
28bf0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
28c00 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 alue_blob(sqlite
28c10 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
28c20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
28c30 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 3_value_bytes(sq
28c40 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
28c50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
28c60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
28c70 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 s16(sqlite3_valu
28c80 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
28c90 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 double sqlite3_v
28ca0 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 alue_double(sqli
28cb0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
28cc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
28cd0 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 te3_value_int(sq
28ce0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
28cf0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
28d00 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
28d10 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 value_int64(sqli
28d20 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
28d30 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e ITE_API const un
28d40 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c signed char *sql
28d50 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
28d60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
28d70 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
28d80 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
28d90 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c value_text16(sql
28da0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
28db0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
28dc0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
28dd0 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 ue_text16le(sqli
28de0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
28df0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
28e00 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
28e10 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 e_text16be(sqlit
28e20 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
28e30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
28e40 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 e3_value_type(sq
28e50 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
28e60 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
28e70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
28e80 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 ric_type(sqlite3
28e90 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _value*);../*.**
28ea0 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 CAPI3REF: Obtai
28eb0 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e 63 n Aggregate Func
28ec0 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48 31 tion Context {H1
28ed0 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6210} <S20200>.*
28ee0 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 *.** The impleme
28ef0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72 65 ntation of aggre
28f00 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
28f10 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 ns use this rout
28f20 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a ine to allocate.
28f30 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20 66 ** a structure f
28f40 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 72 or storing their
28f50 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 state..**.** Th
28f60 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 e first time the
28f70 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
28f80 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f 75 te_context() rou
28f90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
28fa0 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c or a.** particul
28fb0 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53 51 ar aggregate, SQ
28fc0 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20 6e Lite allocates n
28fd0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2c Bytes of memory,
28fe0 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61 74 zeroes out that
28ff0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 .** memory, and
29000 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
29010 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63 6f r to it. On seco
29020 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
29030 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 t calls to.** sq
29040 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
29050 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 68 context() for th
29060 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 65 e same aggregate
29070 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78 2c function index,
29080 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75 66 .** the same buf
29090 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e fer is returned.
290a0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
290b0 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65 ion of the aggre
290c0 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a 20 gate can use.**
290d0 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75 66 the returned buf
290e0 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 fer to accumulat
290f0 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51 e data..**.** SQ
29100 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c Lite automatical
29110 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c 6c ly frees the all
29120 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77 68 ocated buffer wh
29130 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 en the aggregate
29140 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c 75 .** query conclu
29150 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 des..**.** The f
29160 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 73 irst parameter s
29170 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79 20 hould be a copy
29180 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 of the.** [sqlit
29190 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51 4c e3_context | SQL
291a0 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 function contex
291b0 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20 66 t] that is the f
291c0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a 2a irst parameter.*
291d0 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 * to the callbac
291e0 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69 k routine that i
291f0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61 67 mplements the ag
29200 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
29210 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
29220 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
29230 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d led from the sam
29240 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 e thread in whic
29250 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67 61 h.** the aggrega
29260 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 te SQL function
29270 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a is running..**.*
29280 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
29290 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31 36 ** [H16211] [H16
292a0 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b 48 213] [H16215] [H
292b0 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16217].*/.SQLITE
292c0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
292d0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
292e0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e text(sqlite3_con
292f0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 65 text*, int nByte
29300 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 s);../*.** CAPI3
29310 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 46 REF: User Data F
29320 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 or Functions {H1
29330 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6240} <S20200>.*
29340 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
29350 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 74 _user_data() int
29360 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
29370 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 copy of.** the
29380 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 73 pointer that was
29390 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 70 the pUserData p
293a0 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 74 arameter (the 5t
293b0 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 h parameter).**
293c0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
293d0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
293e0 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 )].** and [sqlit
293f0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
29400 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 on16()] routines
29410 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79 that originally
29420 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 74 .** registered t
29430 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
29440 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e efined function.
29450 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 {END}.**.** Thi
29460 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 s routine must b
29470 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 e called from th
29480 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e e same thread in
29490 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 70 which.** the ap
294a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
294b0 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 d function is ru
294c0 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 nning..**.** Req
294d0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
294e0 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16243].*/.SQLITE
294f0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
29500 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c e3_user_data(sql
29510 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a ite3_context*);.
29520 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
29530 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
29540 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f tion For Functio
29550 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 30 ns {H16250} <S60
29560 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 600><S20200>.**.
29570 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
29580 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
29590 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
295a0 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a urns a copy of.*
295b0 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f * the pointer to
295c0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
295d0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 20 onnection] (the
295e0 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 1st parameter).*
295f0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 * of the [sqlite
29600 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
29610 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c n()].** and [sql
29620 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
29630 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e tion16()] routin
29640 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c es that original
29650 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 ly.** registered
29660 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
29670 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f defined functio
29680 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 n..**.** Require
29690 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 35 ments:.** [H1625
296a0 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
296b0 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
296c0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
296d0 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 dle(sqlite3_cont
296e0 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ext*);../*.** CA
296f0 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f 6e PI3REF: Function
29700 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61 20 Auxiliary Data
29710 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30 30 {H16270} <S20200
29720 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c >.**.** The foll
29730 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69 owing two functi
29740 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20 ons may be used
29750 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 by scalar SQL fu
29760 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73 nctions to.** as
29770 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61 sociate metadata
29780 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76 with argument v
29790 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61 alues. If the sa
297a0 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 me value is pass
297b0 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c ed to.** multipl
297c0 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 e invocations of
297d0 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75 the same SQL fu
297e0 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75 nction during qu
297f0 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75 ery execution, u
29800 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72 nder.** some cir
29810 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61 cumstances the a
29820 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61 ssociated metada
29830 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72 ta may be preser
29840 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a ved. This may.**
29850 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78 be used, for ex
29860 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20 ample, to add a
29870 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69 regular-expressi
29880 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c on matching scal
29890 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 ar.** function.
298a0 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72 The compiled ver
298b0 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 sion of the regu
298c0 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 lar expression i
298d0 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d s stored as.** m
298e0 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74 etadata associat
298f0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20 ed with the SQL
29900 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
29910 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 the regular expr
29920 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72 ession.** patter
29930 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 n. The compiled
29940 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 regular express
29950 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65 ion can be reuse
29960 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a d on multiple.**
29970 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 invocations of
29980 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f the same functio
29990 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72 n so that the or
299a0 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73 iginal pattern s
299b0 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f tring.** does no
299c0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63 t need to be rec
299d0 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20 ompiled on each
299e0 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a invocation..**.*
299f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 * The sqlite3_ge
29a00 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65 t_auxdata() inte
29a10 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
29a20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d pointer to the m
29a30 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f 63 etadata.** assoc
29a40 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 6c iated by the sql
29a50 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
29a60 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 () function with
29a70 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e the Nth argumen
29a80 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 68 t.** value to th
29a90 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
29aa0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 fined function.
29ab0 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 68 If no metadata h
29ac0 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a 20 as been ever.**
29ad0 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68 65 been set for the
29ae0 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f 66 Nth argument of
29af0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6f the function, o
29b00 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 r if the corresp
29b10 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 onding.** functi
29b20 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 on parameter has
29b30 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74 changed since t
29b40 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 73 he meta-data was
29b50 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 set,.** then sq
29b60 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
29b70 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e 55 a() returns a NU
29b80 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
29b90 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 65 * The sqlite3_se
29ba0 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65 t_auxdata() inte
29bb0 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 20 rface saves the
29bc0 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69 6e metadata.** poin
29bd0 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33 72 ted to by its 3r
29be0 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 d parameter as t
29bf0 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72 20 he metadata for
29c00 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67 75 the N-th.** argu
29c10 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70 6c ment of the appl
29c20 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
29c30 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73 65 function. Subse
29c40 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 quent.** calls t
29c50 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 o sqlite3_get_au
29c60 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72 65 xdata() might re
29c70 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c 20 turn this data,
29c80 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f 74 if it has.** not
29c90 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64 2e been destroyed.
29ca0 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 .** If it is not
29cb0 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77 69 NULL, SQLite wi
29cc0 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 ll invoke the de
29cd0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e 63 structor.** func
29ce0 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74 68 tion given by th
29cf0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
29d00 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 to sqlite3_set_a
29d10 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20 74 uxdata() on.** t
29d20 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65 6e he metadata when
29d30 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
29d40 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 ng function para
29d50 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a meter changes.**
29d60 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 4c or when the SQL
29d70 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 6c statement compl
29d80 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20 etes, whichever
29d90 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a 0a comes first..**.
29da0 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72 65 ** SQLite is fre
29db0 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64 65 e to call the de
29dc0 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72 6f structor and dro
29dd0 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61 6e p metadata on an
29de0 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f y.** parameter o
29df0 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20 61 f any function a
29e00 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68 65 t any time. The
29e10 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 20 only guarantee
29e20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 64 is that.** the d
29e30 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20 62 estructor will b
29e40 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 e called before
29e50 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73 20 the metadata is
29e60 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 dropped..**.** I
29e70 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74 61 n practice, meta
29e80 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76 65 data is preserve
29e90 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74 69 d between functi
29ea0 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20 on calls for.**
29eb0 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
29ec0 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 are constant at
29ed0 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 54 compile time. T
29ee0 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69 74 his includes lit
29ef0 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20 61 eral.** values a
29f00 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 nd SQL variables
29f10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
29f20 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 utines must be c
29f30 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 alled from the s
29f40 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 ame thread in wh
29f50 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66 ich.** the SQL f
29f60 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
29f70 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ng..**.** Requir
29f80 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 ements:.** [H162
29f90 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48 31 72] [H16274] [H1
29fa0 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20 5b 6276] [H16277] [
29fb0 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39 5d H16278] [H16279]
29fc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
29fd0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 oid *sqlite3_get
29fe0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 _auxdata(sqlite3
29ff0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e _context*, int N
2a000 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2a010 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 id sqlite3_set_a
2a020 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 uxdata(sqlite3_c
2a030 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c 20 ontext*, int N,
2a040 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 28 void*, void (*)(
2a050 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a void*));.../*.**
2a060 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 74 CAPI3REF: Const
2a070 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 70 ants Defining Sp
2a080 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f 72 ecial Destructor
2a090 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32 38 Behavior {H1028
2a0a0 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S30100>.**.*
2a0b0 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65 63 * These are spec
2a0c0 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 74 ial values for t
2a0d0 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 68 he destructor th
2a0e0 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 at is passed in
2a0f0 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 as the.** final
2a100 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 74 argument to rout
2a110 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 ines like [sqlit
2a120 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 e3_result_blob()
2a130 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 72 ]. If the destr
2a140 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 6e uctor.** argumen
2a150 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 54 t is SQLITE_STAT
2a160 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 IC, it means tha
2a170 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f t the content po
2a180 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 6e inter is constan
2a190 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e 65 t.** and will ne
2a1a0 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 20 ver change. It
2a1b0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
2a1c0 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 20 be destroyed.
2a1d0 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52 The.** SQLITE_TR
2a1e0 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d 65 ANSIENT value me
2a1f0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e ans that the con
2a200 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 tent will likely
2a210 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 68 change in.** th
2a220 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 6e e near future an
2a230 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 68 d that SQLite sh
2a240 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 ould make its ow
2a250 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f n private copy o
2a260 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 f.** the content
2a270 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
2a280 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 g..**.** The typ
2a290 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61 72 edef is necessar
2a2a0 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 y to work around
2a2b0 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65 72 problems in cer
2a2c0 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70 tain.** C++ comp
2a2d0 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63 6b ilers. See tick
2a2e0 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 70 et #2191..*/.typ
2a2f0 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 edef void (*sqli
2a300 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 te3_destructor_t
2a310 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64 65 ype)(void*);.#de
2a320 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
2a330 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74 65 IC ((sqlite
2a340 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 3_destructor_typ
2a350 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51 4c e)0).#define SQL
2a360 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20 20 ITE_TRANSIENT
2a370 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 ((sqlite3_destru
2a380 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a 2f ctor_type)-1)../
2a390 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
2a3a0 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75 6c etting The Resul
2a3b0 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e 63 t Of An SQL Func
2a3c0 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c 53 tion {H16400} <S
2a3d0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
2a3e0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
2a3f0 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75 6e used by the xFun
2a400 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c 6c c or xFinal call
2a410 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69 6d backs that.** im
2a420 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e 63 plement SQL func
2a430 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 tions and aggreg
2a440 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b 73 ates. See.** [s
2a450 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2a460 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 nction()] and [s
2a470 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2a480 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66 nction16()].** f
2a490 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
2a4a0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
2a4b0 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 These functions
2a4c0 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 20 work very much
2a4d0 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d 65 like the [parame
2a4e0 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 6d ter binding] fam
2a4f0 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74 69 ily of.** functi
2a500 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64 ons used to bind
2a510 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20 values to host
2a520 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72 parameters in pr
2a530 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2a540 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 74 s..** Refer to t
2a550 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 he [SQL paramete
2a560 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e r] documentation
2a570 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
2a580 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a information..**.
2a590 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2a5a0 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e 74 esult_blob() int
2a5b0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 erface sets the
2a5c0 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 result from.** a
2a5d0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
2a5e0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2a5f0 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77 68 o be the BLOB wh
2a600 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 70 ose content is p
2a610 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 ointed.** to by
2a620 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
2a630 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20 69 eter and which i
2a640 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20 77 s N bytes long w
2a650 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a here N is the.**
2a660 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
2a670 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2a680 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
2a690 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 73 lob() interfaces
2a6a0 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
2a6b0 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 of.** the applic
2a6c0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2a6d0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 42 nction to be a B
2a6e0 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 LOB containing a
2a6f0 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65 73 ll zero.** bytes
2a700 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e 20 and N bytes in
2a710 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69 73 size, where N is
2a720 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
2a730 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e e 2nd parameter.
2a740 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2a750 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
2a760 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
2a770 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f s the result fro
2a780 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 m.** an applicat
2a790 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2a7a0 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c 6f tion to be a flo
2a7b0 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
2a7c0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 e specified.** b
2a7d0 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65 y its 2nd argume
2a7e0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 nt..**.** The sq
2a7f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2a800 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 or() and sqlite3
2a810 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
2a820 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 ) functions.** c
2a830 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 ause the impleme
2a840 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f nted SQL functio
2a850 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 78 n to throw an ex
2a860 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69 ception..** SQLi
2a870 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72 69 te uses the stri
2a880 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ng pointed to by
2a890 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61 the.** 2nd para
2a8a0 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33 meter of sqlite3
2a8b0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 _result_error()
2a8c0 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c or sqlite3_resul
2a8d0 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61 t_error16().** a
2a8e0 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e s the text of an
2a8f0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 error message.
2a900 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65 SQLite interpre
2a910 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 ts the error.**
2a920 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66 message string f
2a930 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 rom sqlite3_resu
2a940 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54 lt_error() as UT
2a950 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20 69 F-8. SQLite.** i
2a960 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74 nterprets the st
2a970 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 ring from sqlite
2a980 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2a990 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 () as UTF-16 in
2a9a0 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f native.** byte o
2a9b0 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74 68 rder. If the th
2a9c0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
2a9d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2a9e0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 error().** or sq
2a9f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2aa00 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 69 or16() is negati
2aa10 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 ve then SQLite t
2aa20 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 6f akes as the erro
2aa30 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c 6c r.** message all
2aa40 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 68 text up through
2aa50 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 the first zero
2aa60 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 character..** If
2aa70 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d the third param
2aa80 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
2aa90 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f result_error() o
2aaa0 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 r.** sqlite3_res
2aab0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 ult_error16() is
2aac0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74 68 non-negative th
2aad0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 en SQLite takes
2aae0 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79 74 that many.** byt
2aaf0 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 es (not characte
2ab00 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 rs) from the 2nd
2ab10 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 68 parameter as th
2ab20 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e e error message.
2ab30 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2ab40 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 result_error() a
2ab50 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c nd sqlite3_resul
2ab60 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 72 t_error16().** r
2ab70 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20 70 outines make a p
2ab80 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 rivate copy of t
2ab90 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
2aba0 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a 20 text before.**
2abb0 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48 65 they return. He
2abc0 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 nce, the calling
2abd0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 function can de
2abe0 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20 6d allocate or.** m
2abf0 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20 61 odify the text a
2ac00 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72 6e fter they return
2ac10 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a without harm..*
2ac20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2ac30 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 sult_error_code(
2ac40 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67 ) function chang
2ac50 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 es the error cod
2ac60 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 e.** returned by
2ac70 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65 73 SQLite as a res
2ac80 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 ult of an error
2ac90 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 in a function.
2aca0 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 74 By default,.** t
2acb0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 he error code is
2acc0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 SQLITE_ERROR.
2acd0 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c A subsequent cal
2ace0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 l to sqlite3_res
2acf0 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f ult_error().** o
2ad00 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 r sqlite3_result
2ad10 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65 74 _error16() reset
2ad20 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 s the error code
2ad30 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 to SQLITE_ERROR
2ad40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2ad50 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 69 te3_result_toobi
2ad60 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 g() interface ca
2ad70 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 uses SQLite to t
2ad80 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a hrow an error.**
2ad90 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 indicating that
2ada0 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f a string or BLO
2adb0 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f 20 B is to long to
2adc0 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a represent..**.**
2add0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2ade0 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74 65 ult_nomem() inte
2adf0 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c rface causes SQL
2ae00 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 ite to throw an
2ae10 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 error.** indicat
2ae20 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f 72 ing that a memor
2ae30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
2ae40 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 led..**.** The s
2ae50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2ae60 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 t() interface se
2ae70 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
2ae80 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
2ae90 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2aea0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2aeb0 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67 6e the 32-bit sign
2aec0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 ed integer.** va
2aed0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 lue given in the
2aee0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2nd argument..*
2aef0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2af00 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e 74 sult_int64() int
2af10 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 erface sets the
2af20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
2af30 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 of the applicati
2af40 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
2af50 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36 34 ion to be the 64
2af60 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
2af70 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 ger.** value giv
2af80 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 en in the 2nd ar
2af90 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
2afa0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2afb0 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61 63 _null() interfac
2afc0 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 e sets the retur
2afd0 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 n value.** of th
2afe0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2aff0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2b000 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a o be NULL..**.**
2b010 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2b020 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c 69 ult_text(), sqli
2b030 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
2b040 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6(),.** sqlite3_
2b050 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 result_text16le(
2b060 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 ), and sqlite3_r
2b070 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 29 esult_text16be()
2b080 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 73 interfaces.** s
2b090 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 et the return va
2b0a0 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c 69 lue of the appli
2b0b0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
2b0c0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a unction to be.**
2b0d0 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20 77 a text string w
2b0e0 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 6e hich is represen
2b0f0 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55 54 ted as UTF-8, UT
2b100 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 F-16 native byte
2b110 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d 31 order,.** UTF-1
2b120 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 6 little endian,
2b130 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20 65 or UTF-16 big e
2b140 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76 ndian, respectiv
2b150 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 74 ely..** SQLite t
2b160 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 65 akes the text re
2b170 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 70 sult from the ap
2b180 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a plication from.*
2b190 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 * the 2nd parame
2b1a0 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ter of the sqlit
2b1b0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2b1c0 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 49 interfaces..** I
2b1d0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 f the 3rd parame
2b1e0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ter to the sqlit
2b1f0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2b200 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 interfaces.** is
2b210 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
2b220 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65 73 SQLite takes res
2b230 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74 68 ult text from th
2b240 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 0a e 2nd parameter.
2b250 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 ** through the f
2b260 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63 irst zero charac
2b270 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 ter..** If the 3
2b280 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
2b290 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
2b2a0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
2b2b0 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 ces.** is non-ne
2b2c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73 20 gative, then as
2b2d0 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74 20 many bytes (not
2b2e0 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20 74 characters) of t
2b2f0 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 he text.** point
2b300 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e 64 ed to by the 2nd
2b310 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20 74 parameter are t
2b320 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70 6c aken as the appl
2b330 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 0a ication-defined.
2b340 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 ** function resu
2b350 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 lt..** If the 4t
2b360 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
2b370 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2b380 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 t_text* interfac
2b390 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 es.** or sqlite3
2b3a0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 _result_blob is
2b3b0 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 a non-NULL point
2b3c0 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 er, then SQLite
2b3d0 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66 75 calls that.** fu
2b3e0 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64 65 nction as the de
2b3f0 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 structor on the
2b400 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73 text or BLOB res
2b410 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73 0a ult when it has.
2b420 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e ** finished usin
2b430 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a g that result..*
2b440 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 * If the 4th par
2b450 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2b460 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2b470 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f 72 t* interfaces or
2b480 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 .** sqlite3_resu
2b490 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 lt_blob is the s
2b4a0 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 pecial constant
2b4b0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 74 SQLITE_STATIC, t
2b4c0 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 73 hen SQLite.** as
2b4d0 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 74 sumes that the t
2b4e0 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 ext or BLOB resu
2b4f0 6c 74 20 69 73 20 69 6e 20 63 6f 6e 73 74 61 6e lt is in constan
2b500 74 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73 t space and does
2b510 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79 20 74 68 65 not.** copy the
2b520 20 69 74 20 6f 72 20 63 61 6c 6c 20 61 20 64 65 it or call a de
2b530 73 74 72 75 63 74 6f 72 20 77 68 65 6e 20 69 74 structor when it
2b540 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 has finished us
2b550 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e ing that result.
2b560 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 .** If the 4th p
2b570 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
2b580 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2b590 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a ext* interfaces.
2b5a0 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 ** or sqlite3_re
2b5b0 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 sult_blob is the
2b5c0 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e special constan
2b5d0 74 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 t SQLITE_TRANSIE
2b5e0 4e 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 NT.** then SQLit
2b5f0 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f e makes a copy o
2b600 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 f the result int
2b610 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 o space obtained
2b620 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 from.** from [s
2b630 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
2b640 20 62 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 before it retur
2b650 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ns..**.** The sq
2b660 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
2b670 75 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 ue() interface s
2b680 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f ets the result o
2b690 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 f.** the applica
2b6a0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2b6b0 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f ction to be a co
2b6c0 70 79 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f py the.** [unpro
2b6d0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
2b6e0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 alue] object spe
2b6f0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 32 6e cified by the 2n
2b700 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 d parameter. Th
2b710 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 e.** sqlite3_res
2b720 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 ult_value() inte
2b730 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f rface makes a co
2b740 70 79 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 py of the [sqlit
2b750 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 e3_value].** so
2b760 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 that the [sqlite
2b770 33 5f 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 3_value] specifi
2b780 65 64 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 ed in the parame
2b790 74 65 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f ter may change o
2b7a0 72 0a 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 r.** be dealloca
2b7b0 74 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 ted after sqlite
2b7c0 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
2b7d0 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 returns without
2b7e0 20 68 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f harm..** A [pro
2b7f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
2b800 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 alue] object may
2b810 20 61 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 always be used
2b820 77 68 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 where an.** [unp
2b830 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
2b840 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 _value] object i
2b850 73 20 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 s required, so e
2b860 69 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 ither.** kind of
2b870 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d [sqlite3_value]
2b880 20 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 object can be u
2b890 73 65 64 20 77 69 74 68 20 74 68 69 73 20 69 6e sed with this in
2b8a0 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 terface..**.** I
2b8b0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
2b8c0 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d are called from
2b8d0 20 77 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 within the diff
2b8e0 65 72 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 erent thread.**
2b8f0 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e than the one con
2b900 74 61 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c taining the appl
2b910 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2b920 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 function that re
2b930 63 65 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 ceived.** the [s
2b940 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 qlite3_context]
2b950 70 6f 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 pointer, the res
2b960 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e ults are undefin
2b970 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ed..**.** Requir
2b980 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 ements:.** [H164
2b990 30 33 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 03] [H16406] [H1
2b9a0 36 34 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 6409] [H16412] [
2b9b0 48 31 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d H16415] [H16418]
2b9c0 20 5b 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 [H16421] [H1642
2b9d0 34 5d 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 4].** [H16427] [
2b9e0 48 31 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d H16430] [H16433]
2b9f0 20 5b 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 [H16436] [H1643
2ba00 39 5d 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 9] [H16442] [H16
2ba10 34 34 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 445] [H16448].**
2ba20 20 5b 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 [H16451] [H1645
2ba30 34 5d 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 4] [H16457] [H16
2ba40 34 36 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 460] [H16463].*/
2ba50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2ba60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2ba70 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e blob(sqlite3_con
2ba80 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 text*, const voi
2ba90 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 d*, int, void(*)
2baa0 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
2bab0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2bac0 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
2bad0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2bae0 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 , double);.SQLIT
2baf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2bb00 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2bb10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2bb20 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 , const char*, i
2bb30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
2bb40 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2bb50 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 ult_error16(sqli
2bb60 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2bb70 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b nst void*, int);
2bb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2bb90 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2bba0 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c error_toobig(sql
2bbb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a ite3_context*);.
2bbc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2bbd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2bbe0 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 rror_nomem(sqlit
2bbf0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 e3_context*);.SQ
2bc00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2bc10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2bc20 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f or_code(sqlite3_
2bc30 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a context*, int);.
2bc40 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2bc50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
2bc60 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
2bc70 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 xt*, int);.SQLIT
2bc80 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2bc90 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 e3_result_int64(
2bca0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2bcb0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 , sqlite3_int64)
2bcc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2bcd0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2bce0 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f _null(sqlite3_co
2bcf0 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f ntext*);.SQLITE_
2bd00 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2bd10 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c _result_text(sql
2bd20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
2bd30 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c onst char*, int,
2bd40 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 void(*)(void*))
2bd50 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2bd60 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2bd70 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f _text16(sqlite3_
2bd80 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
2bd90 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 void*, int, void
2bda0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
2bdb0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2bdc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2bdd0 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 16le(sqlite3_con
2bde0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 text*, const voi
2bdf0 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 d*, int,void(*)(
2be00 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2be10 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2be20 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 _result_text16be
2be30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2be40 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
2be50 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 int,void(*)(void
2be60 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
2be70 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2be80 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 ult_value(sqlite
2be90 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 3_context*, sqli
2bea0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2beb0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2bec0 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f ite3_result_zero
2bed0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e blob(sqlite3_con
2bee0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a text*, int n);..
2bef0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2bf00 44 65 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 Define New Colla
2bf10 74 69 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b ting Sequences {
2bf20 48 31 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e H16600} <S20300>
2bf30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e .**.** These fun
2bf40 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 ctions are used
2bf50 74 6f 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 to add new colla
2bf60 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 tion sequences t
2bf70 6f 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 o the.** [databa
2bf80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 se connection] s
2bf90 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 pecified as the
2bfa0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a first argument..
2bfb0 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f **.** The name o
2bfc0 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 f the new collat
2bfd0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 ion sequence is
2bfe0 73 70 65 63 69 66 69 65 64 20 61 73 20 61 20 55 specified as a U
2bff0 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 TF-8 string.** f
2c000 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
2c010 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e e_collation() an
2c020 64 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 d sqlite3_create
2c030 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a _collation_v2().
2c040 2a 2a 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 ** and a UTF-16
2c050 73 74 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 string for sqlit
2c060 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2c070 69 6f 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 ion16(). In all
2c080 63 61 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d cases.** the nam
2c090 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 e is passed as t
2c0a0 68 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 he second functi
2c0b0 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a on argument..**.
2c0c0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 ** The third arg
2c0d0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 ument may be one
2c0e0 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 of the constant
2c0f0 73 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c s [SQLITE_UTF8],
2c100 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 .** [SQLITE_UTF1
2c110 36 4c 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 6LE], or [SQLITE
2c120 5f 55 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 _UTF16BE], indic
2c130 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 ating that the u
2c140 73 65 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 ser-supplied.**
2c150 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 routine expects
2c160 74 6f 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 to be passed poi
2c170 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 nters to strings
2c180 20 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 encoded using U
2c190 54 46 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 TF-8,.** UTF-16
2c1a0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f little-endian, o
2c1b0 72 20 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 r UTF-16 big-end
2c1c0 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c ian, respectivel
2c1d0 79 2e 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 y. The.** third
2c1e0 61 72 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 argument might a
2c1f0 6c 73 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 lso be [SQLITE_U
2c200 54 46 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 TF16] to indicat
2c210 65 20 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 e that the routi
2c220 6e 65 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f ne.** expects po
2c230 69 6e 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 inters to be UTF
2c240 2d 31 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 -16 strings in t
2c250 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f he native byte o
2c260 72 64 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 rder, or the.**
2c270 61 72 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 argument can be
2c280 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c [SQLITE_UTF16_AL
2c290 49 47 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a IGNED] if the.**
2c2a0 20 74 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 the routine exp
2c2b0 65 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f ects pointers to
2c2c0 20 31 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 16-bit word ali
2c2d0 67 6e 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 gned strings.**
2c2e0 6f 66 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 of UTF-16 in the
2c2f0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2c300 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e er..**.** A poin
2c310 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 ter to the user
2c320 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 supplied routine
2c330 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20 must be passed
2c340 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 as the fifth.**
2c350 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 argument. If it
2c360 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 is NULL, this i
2c370 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 s the same as de
2c380 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 leting the colla
2c390 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 tion.** sequence
2c3a0 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 (so that SQLite
2c3b0 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 cannot call it
2c3c0 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 anymore)..** Eac
2c3d0 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 h time the appli
2c3e0 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 cation supplied
2c3f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f function is invo
2c400 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 ked, it is passe
2c410 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 d.** as its firs
2c420 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f t parameter a co
2c430 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 py of the void*
2c440 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6f passed as the fo
2c450 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a urth argument.**
2c460 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 to sqlite3_crea
2c470 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f te_collation() o
2c480 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 r sqlite3_create
2c490 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a _collation16()..
2c4a0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e **.** The remain
2c4b0 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f ing arguments to
2c4c0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2c4d0 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e -supplied routin
2c4e0 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 e are two string
2c4f0 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 s,.** each repre
2c500 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e sented by a (len
2c510 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 gth, data) pair
2c520 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 and encoded in t
2c530 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 he encoding.** t
2c540 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 61 hat was passed a
2c550 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 s the third argu
2c560 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f ment when the co
2c570 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2c580 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 was.** register
2c590 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 ed. {END} The a
2c5a0 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e pplication defin
2c5b0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 ed collation rou
2c5c0 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 tine should.** r
2c5d0 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 eturn negative,
2c5e0 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 zero or positive
2c5f0 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73 74 if the first st
2c600 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 ring is less tha
2c610 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 n,.** equal to,
2c620 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
2c630 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e the second strin
2c640 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 g. i.e. (STRING1
2c650 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a - STRING2)..**.
2c660 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
2c670 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f reate_collation_
2c680 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 v2() works like
2c690 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2c6a0 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 ollation().** ex
2c6b0 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61 6b cept that it tak
2c6c0 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 es an extra argu
2c6d0 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 ment which is a
2c6e0 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a destructor for.*
2c6f0 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e * the collation.
2c700 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 The destructor
2c710 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
2c720 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 the collation is
2c730 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e .** destroyed an
2c740 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63 6f d is passed a co
2c750 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 py of the fourth
2c760 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a parameter void*
2c770 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 pointer.** of t
2c780 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 he sqlite3_creat
2c790 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 e_collation_v2()
2c7a0 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 ..** Collations
2c7b0 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 are destroyed wh
2c7c0 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 en they are over
2c7d0 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 ridden by later
2c7e0 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 calls to the.**
2c7f0 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 collation creati
2c800 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 on functions or
2c810 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 when the [databa
2c820 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 se connection] i
2c830 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e s closed.** usin
2c840 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 g [sqlite3_close
2c850 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ()]..**.** See a
2c860 6c 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 lso: [sqlite3_c
2c870 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 ollation_needed(
2c880 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
2c890 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
2c8a0 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 16()]..**.** Req
2c8b0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
2c8c0 31 36 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 16603] [H16604]
2c8d0 5b 48 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 [H16606] [H16609
2c8e0 5d 20 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 ] [H16612] [H166
2c8f0 31 35 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 15] [H16618] [H1
2c900 36 36 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 6621].** [H16624
2c910 5d 20 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 ] [H16627] [H166
2c920 33 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 30].*/.SQLITE_AP
2c930 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 I int sqlite3_cr
2c940 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a eate_collation(.
2c950 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
2c960 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
2c970 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 , . int eTextRe
2c980 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
2c990 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
2c9a0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2c9b0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2c9c0 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 id*).);.SQLITE_A
2c9d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
2c9e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f reate_collation_
2c9f0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 v2(. sqlite3*,
2ca00 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2ca10 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 Name, . int eTe
2ca20 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c xtRep, . void*,
2ca30 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 . int(*xCompare
2ca40 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 )(void*,int,cons
2ca50 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 t void*,int,cons
2ca60 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 t void*),. void
2ca70 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 (*xDestroy)(void
2ca80 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
2ca90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
2caa0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
2cab0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
2cac0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d const void *zNam
2cad0 65 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 e,. int eTextRe
2cae0 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
2caf0 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
2cb00 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2cb10 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2cb20 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 id*).);../*.** C
2cb30 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 API3REF: Collati
2cb40 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 on Needed Callba
2cb50 63 6b 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 cks {H16700} <S2
2cb60 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 0300>.**.** To a
2cb70 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 void having to r
2cb80 65 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c egister all coll
2cb90 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 ation sequences
2cba0 62 65 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 before a databas
2cbb0 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 e.** can be used
2cbc0 2c 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 , a single callb
2cbd0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 ack function may
2cbe0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 77 be registered w
2cbf0 69 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 ith the.** [data
2cc00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
2cc10 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 to be called wh
2cc20 65 6e 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 enever an undefi
2cc30 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a ned collation.**
2cc40 20 73 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 sequence is req
2cc50 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 uired..**.** If
2cc60 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
2cc70 72 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 registered using
2cc80 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c the sqlite3_col
2cc90 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 lation_needed()
2cca0 41 50 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 API,.** then it
2ccb0 69 73 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 is passed the na
2ccc0 6d 65 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 mes of undefined
2ccd0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2cce0 6e 63 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a nces as strings.
2ccf0 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 ** encoded in UT
2cd00 46 2d 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 F-8. {H16703} If
2cd10 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
2cd20 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 on_needed16() is
2cd30 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 used,.** the na
2cd40 6d 65 73 20 61 72 65 20 70 61 73 73 65 64 20 61 mes are passed a
2cd50 73 20 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 s UTF-16 in mach
2cd60 69 6e 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 ine native byte
2cd70 6f 72 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c order..** A call
2cd80 20 74 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 to either funct
2cd90 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 ion replaces any
2cda0 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 existing callba
2cdb0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 ck..**.** When t
2cdc0 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 he callback is i
2cdd0 6e 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 nvoked, the firs
2cde0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 t argument passe
2cdf0 64 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f d is a copy.** o
2ce00 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
2ce10 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
2ce20 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
2ce30 64 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 d() or.** sqlite
2ce40 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 3_collation_need
2ce50 65 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 ed16(). The sec
2ce60 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
2ce70 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
2ce80 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 connection. The
2ce90 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 third argument
2cea0 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 is one of [SQLIT
2ceb0 45 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 E_UTF8], [SQLITE
2cec0 5f 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 _UTF16BE],.** or
2ced0 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 [SQLITE_UTF16LE
2cee0 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 ], indicating th
2cef0 65 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 e most desirable
2cf00 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c form of the col
2cf10 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e lation.** sequen
2cf20 63 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 ce function requ
2cf30 69 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 ired. The fourt
2cf40 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 h parameter is t
2cf50 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a he name of the.*
2cf60 2a 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 * required colla
2cf70 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a tion sequence..*
2cf80 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 *.** The callbac
2cf90 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c k function shoul
2cfa0 64 20 72 65 67 69 73 74 65 72 20 74 68 65 20 64 d register the d
2cfb0 65 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e esired collation
2cfc0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 using.** [sqlit
2cfd0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2cfe0 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 ion()], [sqlite3
2cff0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2d000 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 n16()], or.** [s
2d010 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2d020 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a llation_v2()]..*
2d030 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2d040 73 3a 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b s:.** [H16702] [
2d050 48 31 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d H16704] [H16706]
2d060 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
2d070 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 nt sqlite3_colla
2d080 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 tion_needed(. s
2d090 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 qlite3*, . void
2d0a0 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f *, . void(*)(vo
2d0b0 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 id*,sqlite3*,int
2d0c0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 eTextRep,const
2d0d0 63 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 char*).);.SQLITE
2d0e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2d0f0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
2d100 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c d16(. sqlite3*,
2d110 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 . void*,. voi
2d120 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 d(*)(void*,sqlit
2d130 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 e3*,int eTextRep
2d140 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b ,const void*).);
2d150 0a 0a 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 ../*.** Specify
2d160 74 68 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 the key for an e
2d170 6e 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73 ncrypted databas
2d180 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
2d190 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 should be.** ca
2d1a0 6c 6c 65 64 20 72 69 67 68 74 20 61 66 74 65 72 lled right after
2d1b0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e sqlite3_open().
2d1c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 .**.** The code
2d1d0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 to implement thi
2d1e0 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 s API is not ava
2d1f0 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 ilable in the pu
2d200 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 blic release.**
2d210 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 of SQLite..*/.SQ
2d220 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2d230 69 74 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 ite3_key(. sqli
2d240 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
2d250 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
2d260 74 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b tabase to be rek
2d270 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 eyed */. const
2d280 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 void *pKey, int
2d290 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 nKey /* The
2d2a0 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a key */.);../*.**
2d2b0 20 43 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 Change the key
2d2c0 6f 6e 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 on an open datab
2d2d0 61 73 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 ase. If the cur
2d2e0 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 rent database is
2d2f0 20 6e 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 not.** encrypte
2d300 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
2d310 77 69 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e will encrypt it.
2d320 20 20 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 If pNew==0 or
2d330 6e 4e 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 nNew==0, the.**
2d340 64 61 74 61 62 61 73 65 20 69 73 20 64 65 63 72 database is decr
2d350 79 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ypted..**.** The
2d360 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 code to impleme
2d370 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e nt this API is n
2d380 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 ot available in
2d390 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 the public relea
2d3a0 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e se.** of SQLite.
2d3b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
2d3c0 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 nt sqlite3_rekey
2d3d0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
2d3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d3f0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 /* Database t
2d400 6f 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a o be rekeyed */.
2d410 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
2d420 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 ey, int nKey
2d430 20 2f 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 /* The new key
2d440 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
2d450 49 33 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 I3REF: Suspend E
2d460 78 65 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 xecution For A S
2d470 68 6f 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 hort Time {H1053
2d480 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 0} <S40410>.**.*
2d490 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c * The sqlite3_sl
2d4a0 65 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 eep() function c
2d4b0 61 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e auses the curren
2d4c0 74 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 t thread to susp
2d4d0 65 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a end execution.**
2d4e0 20 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 for at least a
2d4f0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 number of millis
2d500 65 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 econds specified
2d510 20 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 in its paramete
2d520 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
2d530 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
2d540 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 does not suppor
2d550 74 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 t sleep requests
2d560 20 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 with.** millise
2d570 63 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 cond time resolu
2d580 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 tion, then the t
2d590 69 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e ime will be roun
2d5a0 64 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 ded up to.** the
2d5b0 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e nearest second.
2d5c0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d The number of m
2d5d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 illiseconds of s
2d5e0 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a leep actually.**
2d5f0 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
2d600 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
2d610 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 stem is returned
2d620 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 ..**.** SQLite i
2d630 6d 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 mplements this i
2d640 6e 74 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c nterface by call
2d650 69 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 ing the xSleep()
2d660 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 .** method of th
2d670 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 e default [sqlit
2d680 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a e3_vfs] object..
2d690 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2d6a0 74 73 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 ts: [H10533] [H1
2d6b0 30 35 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 0536].*/.SQLITE_
2d6c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2d6d0 73 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a sleep(int);../*.
2d6e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d ** CAPI3REF: Nam
2d6f0 65 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 e Of The Folder
2d700 48 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 Holding Temporar
2d710 79 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d y Files {H10310}
2d720 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20000>.**.**
2d730 49 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 If this global v
2d740 61 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 ariable is made
2d750 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 to point to a st
2d760 72 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a ring which is.**
2d770 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 the name of a f
2d780 6f 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 older (a.k.a. di
2d790 72 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 rectory), then a
2d7a0 6c 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ll temporary fil
2d7b0 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 es.** created by
2d7c0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 SQLite will be
2d7d0 70 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 placed in that d
2d7e0 69 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 irectory. If th
2d7f0 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 is variable.** i
2d800 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
2d810 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 , then SQLite pe
2d820 72 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 rforms a search
2d830 66 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 for an appropria
2d840 74 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 te.** temporary
2d850 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a file directory..
2d860 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 **.** It is not
2d870 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 safe to read or
2d880 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69 modify this vari
2d890 61 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 able in more tha
2d8a0 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 n one.** thread
2d8b0 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 at a time. It i
2d8c0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 s not safe to re
2d8d0 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 ad or modify thi
2d8e0 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 s variable.** if
2d8f0 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
2d900 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e nection] is bein
2d910 67 20 75 73 65 64 20 61 74 20 74 68 65 20 73 61 g used at the sa
2d920 6d 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 me time in a sep
2d930 61 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e arate.** thread.
2d940 0a 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 .** It is intend
2d950 65 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72 ed that this var
2d960 69 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 iable be set onc
2d970 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 e.** as part of
2d980 70 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 process initiali
2d990 7a 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 zation and befor
2d9a0 65 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 e any SQLite int
2d9b0 65 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e erface.** routin
2d9c0 65 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c es have been cal
2d9d0 6c 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 led and that thi
2d9e0 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 s variable remai
2d9f0 6e 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 n unchanged.** t
2da00 68 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a hereafter..**.**
2da10 20 54 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 The [temp_store
2da20 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d _directory pragm
2da30 61 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 a] may modify th
2da40 69 73 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 is variable and
2da50 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 cause.** it to p
2da60 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f oint to memory o
2da70 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
2da80 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 lite3_malloc].
2da90 46 75 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 Furthermore,.**
2daa0 74 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f the [temp_store_
2dab0 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 directory pragma
2dac0 5d 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 ] always assumes
2dad0 20 74 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 that any string
2dae0 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 .** that this va
2daf0 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f riable points to
2db00 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f is held in memo
2db10 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
2db20 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 .** [sqlite3_ma
2db30 6c 6c 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 lloc] and the pr
2db40 61 67 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 agma may attempt
2db50 20 74 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 to free that me
2db60 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 mory.** using [s
2db70 71 6c 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a qlite3_free]..**
2db80 20 48 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 Hence, if this
2db90 76 61 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 variable is modi
2dba0 66 69 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 fied directly, e
2dbb0 69 74 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 ither it should
2dbc0 62 65 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 be.** made NULL
2dbd0 6f 72 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 or made to point
2dbe0 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 to memory obtai
2dbf0 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ned from [sqlite
2dc00 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 3_malloc].** or
2dc10 65 6c 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 else the use of
2dc20 74 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f the [temp_store_
2dc30 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 directory pragma
2dc40 5d 20 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 ] should be avoi
2dc50 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ded..*/.SQLITE_A
2dc60 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
2dc70 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b _temp_directory;
2dc80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2dc90 3a 20 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d : Test For Auto-
2dca0 43 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 Commit Mode {H12
2dcb0 39 33 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 930} <S60200>.**
2dcc0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f KEYWORDS: {auto
2dcd0 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a commit mode}.**.
2dce0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 ** The sqlite3_g
2dcf0 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 et_autocommit()
2dd00 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2dd10 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a s non-zero or.**
2dd20 20 7a 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 zero if the giv
2dd30 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e en database conn
2dd40 65 63 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 ection is or is
2dd50 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 not in autocommi
2dd60 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 t mode,.** respe
2dd70 63 74 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f ctively. Autoco
2dd80 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 mmit mode is on
2dd90 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 by default..** A
2dda0 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 utocommit mode i
2ddb0 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 s disabled by a
2ddc0 5b 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e [BEGIN] statemen
2ddd0 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 t..** Autocommit
2dde0 20 6d 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 mode is re-enab
2ddf0 6c 65 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 led by a [COMMIT
2de00 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e ] or [ROLLBACK].
2de10 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 .**.** If certai
2de20 6e 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 n kinds of error
2de30 73 20 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 s occur on a sta
2de40 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 tement within a
2de50 6d 75 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a multi-statement.
2de60 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 ** transaction (
2de70 65 72 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 errors including
2de80 20 5b 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 [SQLITE_FULL],
2de90 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a [SQLITE_IOERR],.
2dea0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ** [SQLITE_NOMEM
2deb0 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ], [SQLITE_BUSY]
2dec0 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e , and [SQLITE_IN
2ded0 54 45 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 TERRUPT]) then t
2dee0 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f he.** transactio
2def0 6e 20 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 n might be rolle
2df00 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 d back automatic
2df10 61 6c 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 ally. The only
2df20 77 61 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f way to.** find o
2df30 75 74 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 ut whether SQLit
2df40 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 e automatically
2df50 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 rolled back the
2df60 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 transaction afte
2df70 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 r.** an error is
2df80 20 74 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e to use this fun
2df90 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
2dfa0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 another thread c
2dfb0 68 61 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 hanges the autoc
2dfc0 6f 6d 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 ommit status of
2dfd0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
2dfe0 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 connection while
2dff0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
2e000 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 running, then t
2e010 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
2e020 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e ** is undefined.
2e030 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
2e040 6e 74 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 nts: [H12931] [H
2e050 31 32 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 12932] [H12933]
2e060 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 [H12934].*/.SQLI
2e070 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2e080 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 e3_get_autocommi
2e090 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a t(sqlite3*);../*
2e0a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 .** CAPI3REF: Fi
2e0b0 6e 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 nd The Database
2e0c0 48 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 Handle Of A Prep
2e0d0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b ared Statement {
2e0e0 48 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e H13120} <S60600>
2e0f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2e100 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 e3_db_handle int
2e110 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 erface returns t
2e120 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
2e130 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a nection] handle.
2e140 2a 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 ** to which a [p
2e150 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2e160 74 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 t] belongs. The
2e170 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2e180 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e ction].** return
2e190 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 ed by sqlite3_db
2e1a0 5f 68 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 _handle is the s
2e1b0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f ame [database co
2e1c0 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 nnection] that w
2e1d0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
2e1e0 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 ument.** to the
2e1f0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
2e200 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 _v2()] call (or
2e210 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 its variants) th
2e220 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a at was used to.*
2e230 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61 * create the sta
2e240 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 tement in the fi
2e250 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a rst place..**.**
2e260 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b Requirements: [
2e270 48 31 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H13123].*/.SQLIT
2e280 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 E_API sqlite3 *s
2e290 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 qlite3_db_handle
2e2a0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b (sqlite3_stmt*);
2e2b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2e2c0 3a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 : Find the next
2e2d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2e2e0 6e 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 nt {H13140} <S60
2e2f0 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 600>.**.** This
2e300 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2e310 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
2e320 68 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 he next [prepare
2e330 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 d statement] aft
2e340 65 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f er.** pStmt asso
2e350 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
2e360 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2e370 74 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 tion] pDb. If p
2e380 53 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 Stmt is NULL.**
2e390 74 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 then this interf
2e3a0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
2e3b0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 inter to the fir
2e3c0 73 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 st prepared stat
2e3d0 65 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 ement.** associa
2e3e0 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 ted with the dat
2e3f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2e400 20 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 pDb. If no pre
2e410 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a pared statement.
2e420 2a 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65 ** satisfies the
2e430 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 conditions of t
2e440 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 his routine, it
2e450 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a returns NULL..**
2e460 0a 2a 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 .** The [databas
2e470 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f e connection] po
2e480 69 6e 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c inter D in a cal
2e490 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 l to.** [sqlite3
2e4a0 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d _next_stmt(D,S)]
2e4b0 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 must refer to a
2e4c0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a n open database.
2e4d0 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e ** connection an
2e4e0 64 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 d in particular
2e4f0 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 must not be a NU
2e500 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
2e510 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
2e520 5b 48 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 [H13143] [H13146
2e530 5d 20 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 ] [H13149] [H131
2e540 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 52].*/.SQLITE_AP
2e550 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a I sqlite3_stmt *
2e560 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d sqlite3_next_stm
2e570 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 t(sqlite3 *pDb,
2e580 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
2e590 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tmt);../*.** CAP
2e5a0 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e I3REF: Commit An
2e5b0 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 d Rollback Notif
2e5c0 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b ication Callback
2e5d0 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 s {H12950} <S604
2e5e0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
2e5f0 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f lite3_commit_hoo
2e600 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 k() interface re
2e610 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
2e620 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 ck.** function t
2e630 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 o be invoked whe
2e640 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 never a transact
2e650 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
2e660 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 ..** Any callbac
2e670 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 k set by a previ
2e680 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ous call to sqli
2e690 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
2e6a0 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d ).** for the sam
2e6b0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
2e6c0 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 ction is overrid
2e6d0 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 den..** The sqli
2e6e0 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f te3_rollback_hoo
2e6f0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 k() interface re
2e700 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
2e710 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 ck.** function t
2e720 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 o be invoked whe
2e730 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 never a transact
2e740 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
2e750 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 ..** Any callbac
2e760 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 k set by a previ
2e770 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ous call to sqli
2e780 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
2e790 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d ).** for the sam
2e7a0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
2e7b0 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 ction is overrid
2e7c0 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67 den..** The pArg
2e7d0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73 argument is pas
2e7e0 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 sed through to t
2e7f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 he callback..**
2e800 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 If the callback
2e810 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b on a commit hook
2e820 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
2e830 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 s non-zero,.** t
2e840 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 hen the commit i
2e850 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f s converted into
2e860 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a a rollback..**.
2e870 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 ** If another fu
2e880 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 nction was previ
2e890 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 ously registered
2e8a0 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61 , its.** pArg va
2e8b0 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e lue is returned.
2e8c0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
2e8d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
2e8e0 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
2e8f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
2e900 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
2e910 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
2e920 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
2e930 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2e940 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
2e950 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 e callback. Any
2e960 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d actions.** to m
2e970 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 odify the databa
2e980 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 se connection mu
2e990 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 st be deferred u
2e9a0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a ntil after the.*
2e9b0 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 * completion of
2e9c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 the [sqlite3_ste
2e9d0 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 p()] call that t
2e9e0 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d riggered the com
2e9f0 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 mit.** or rollba
2ea00 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 ck hook in the f
2ea10 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e irst place..** N
2ea20 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 ote that [sqlite
2ea30 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
2ea40 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 and [sqlite3_ste
2ea50 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 p()] both modify
2ea60 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 their.** databa
2ea70 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 se connections f
2ea80 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f or the meaning o
2ea90 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 f "modify" in th
2eaa0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a is paragraph..**
2eab0 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20 .** Registering
2eac0 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 a NULL function
2ead0 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c disables the cal
2eae0 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 lback..**.** For
2eaf0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
2eb00 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 61 this API, a tra
2eb10 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 64 nsaction is said
2eb20 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a to have been.**
2eb30 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 rolled back if
2eb40 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f 4c an explicit "ROL
2eb50 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e 74 LBACK" statement
2eb60 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f 72 is executed, or
2eb70 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 .** an error or
2eb80 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 65 constraint cause
2eb90 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 6f s an implicit ro
2eba0 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 2e llback to occur.
2ebb0 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b .** The rollback
2ebc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 callback is not
2ebd0 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 invoked if a tr
2ebe0 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 ansaction is.**
2ebf0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f automatically ro
2ec00 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 lled back becaus
2ec10 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 e the database c
2ec20 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
2ec30 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c sed..** The roll
2ec40 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 back callback is
2ec50 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 not invoked if
2ec60 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
2ec70 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 .** rolled back
2ec80 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 74 because a commit
2ec90 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
2eca0 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 ed non-zero..**
2ecb0 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e 20 <todo> Check on
2ecc0 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a this </todo>.**.
2ecd0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2ece0 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 .** [H12951] [H1
2ecf0 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 2952] [H12953] [
2ed00 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d H12954] [H12955]
2ed10 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 .** [H12961] [H1
2ed20 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 2962] [H12963] [
2ed30 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H12964].*/.SQLIT
2ed40 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
2ed50 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
2ed60 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 sqlite3*, int(*)
2ed70 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b (void*), void*);
2ed80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2ed90 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 *sqlite3_rollba
2eda0 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a ck_hook(sqlite3*
2edb0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a , void(*)(void *
2edc0 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a ), void*);../*.*
2edd0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 * CAPI3REF: Data
2ede0 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 Change Notifica
2edf0 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
2ee00 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e H12970} <S60400>
2ee10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2ee20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 e3_update_hook()
2ee30 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
2ee40 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 ters a callback
2ee50 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 function.** with
2ee60 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
2ee70 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 onnection] ident
2ee80 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 ified by the fir
2ee90 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 st argument.** t
2eea0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 o be invoked whe
2eeb0 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 never a row is u
2eec0 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 pdated, inserted
2eed0 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 or deleted..**
2eee0 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 Any callback set
2eef0 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 by a previous c
2ef00 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 all to this func
2ef10 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 tion.** for the
2ef20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
2ef30 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 nnection is over
2ef40 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ridden..**.** Th
2ef50 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
2ef60 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
2ef70 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 o the function t
2ef80 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a o invoke when a.
2ef90 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 ** row is update
2efa0 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 d, inserted or d
2efb0 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 eleted..** The f
2efc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
2efd0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
2efe0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 a copy of the t
2eff0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a hird argument.**
2f000 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 to sqlite3_upda
2f010 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 te_hook()..** Th
2f020 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 e second callbac
2f030 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e k argument is on
2f040 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 e of [SQLITE_INS
2f050 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 ERT], [SQLITE_DE
2f060 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 LETE],.** or [SQ
2f070 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 LITE_UPDATE], de
2f080 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f pending on the o
2f090 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 peration that ca
2f0a0 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 used the callbac
2f0b0 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b k.** to be invok
2f0c0 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 ed..** The third
2f0d0 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 and fourth argu
2f0e0 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c ments to the cal
2f0f0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f lback contain po
2f100 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a inters to the.**
2f110 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 database and ta
2f120 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e ble name contain
2f130 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 ing the affected
2f140 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e row..** The fin
2f150 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 al callback para
2f160 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f meter is the [ro
2f170 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e wid] of the row.
2f180 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 .** In the case
2f190 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 of an update, th
2f1a0 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 is is the [rowid
2f1b0 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 ] after the upda
2f1c0 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a te takes place..
2f1d0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 **.** The update
2f1e0 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 hook is not inv
2f1f0 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e oked when intern
2f200 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 al system tables
2f210 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 are.** modified
2f220 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 (i.e. sqlite_ma
2f230 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f ster and sqlite_
2f240 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a sequence)..**.**
2f250 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b The update hook
2f260 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
2f270 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
2f280 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
2f290 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
2f2a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2f2b0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
2f2c0 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 e update hook.
2f2d0 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 Any actions.** t
2f2e0 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 o modify the dat
2f2f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2f300 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 must be deferre
2f310 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 d until after th
2f320 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 e.** completion
2f330 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
2f340 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 step()] call tha
2f350 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 t triggered the
2f360 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 update hook..**
2f370 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 Note that [sqlit
2f380 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
2f390 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
2f3a0 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 ep()] both modif
2f3b0 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 y their.** datab
2f3c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
2f3d0 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 for the meaning
2f3e0 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 of "modify" in t
2f3f0 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a his paragraph..*
2f400 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 *.** If another
2f410 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 function was pre
2f420 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
2f430 65 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c ed, its pArg val
2f440 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 ue.** is returne
2f450 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 d. Otherwise NU
2f460 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a LL is returned..
2f470 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2f480 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37 31 5d 20 ts:.** [H12971]
2f490 5b 48 31 32 39 37 33 5d 20 5b 48 31 32 39 37 35 [H12973] [H12975
2f4a0 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48 31 32 39 ] [H12977] [H129
2f4b0 37 39 5d 20 5b 48 31 32 39 38 31 5d 20 5b 48 31 79] [H12981] [H1
2f4c0 32 39 38 33 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2983] [H12986].*
2f4d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
2f4e0 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 d *sqlite3_updat
2f4f0 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 e_hook(. sqlite
2f500 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 3*, . void(*)(v
2f510 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 oid *,int ,char
2f520 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e const *,char con
2f530 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 st *,sqlite3_int
2f540 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 64),. void*.);.
2f550 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2f560 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 Enable Or Disab
2f570 6c 65 20 53 68 61 72 65 64 20 50 61 67 65 72 20 le Shared Pager
2f580 43 61 63 68 65 20 7b 48 31 30 33 33 30 7d 20 3c Cache {H10330} <
2f590 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S30900>.** KEYWO
2f5a0 52 44 53 3a 20 7b 73 68 61 72 65 64 20 63 61 63 RDS: {shared cac
2f5b0 68 65 7d 20 7b 73 68 61 72 65 64 20 63 61 63 68 he} {shared cach
2f5c0 65 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 e mode}.**.** Th
2f5d0 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c is routine enabl
2f5e0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 es or disables t
2f5f0 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68 he sharing of th
2f600 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 e database cache
2f610 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64 .** and schema d
2f620 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62 ata structures b
2f630 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65 etween [database
2f640 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f connection | co
2f650 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f nnections].** to
2f660 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
2f670 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65 se. Sharing is e
2f680 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 nabled if the ar
2f690 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a gument is true.*
2f6a0 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69 * and disabled i
2f6b0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
2f6c0 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 s false..**.** C
2f6d0 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 ache sharing is
2f6e0 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61 enabled and disa
2f6f0 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69 bled for an enti
2f700 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 re process..** T
2f710 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 his is a change
2f720 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 as of SQLite ver
2f730 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 sion 3.5.0. In p
2f740 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 rior versions of
2f750 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 SQLite,.** shar
2f760 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 ing was enabled
2f770 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 or disabled for
2f780 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 each thread sepa
2f790 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rately..**.** Th
2f7a0 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 e cache sharing
2f7b0 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 mode set by this
2f7c0 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 interface effec
2f7d0 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e ts all subsequen
2f7e0 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 t.** calls to [s
2f7f0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
2f800 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
2f810 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
2f820 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 3_open16()]..**
2f830 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 Existing databas
2f840 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f e connections co
2f850 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 ntinue use the s
2f860 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 haring mode.** t
2f870 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 hat was in effec
2f880 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 t at the time th
2f890 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a ey were opened..
2f8a0 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 **.** Virtual ta
2f8b0 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 bles cannot be u
2f8c0 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 sed with a share
2f8d0 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 d cache. When s
2f8e0 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 hared.** cache i
2f8f0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b s enabled, the [
2f900 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
2f910 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 odule()] API use
2f920 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a d to register.**
2f930 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
2f940 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 will always retu
2f950 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a rn an error..**.
2f960 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
2f970 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
2f980 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 OK] if shared ca
2f990 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 che was enabled
2f9a0 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 or disabled.** s
2f9b0 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e uccessfully. An
2f9c0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
2f9d0 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 returned otherw
2f9e0 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 ise..**.** Share
2f9f0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 d cache is disab
2fa00 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 led by default.
2fa10 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 But this might c
2fa20 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 hange in.** futu
2fa30 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
2fa40 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 QLite. Applicat
2fa50 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 ions that care a
2fa60 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 bout shared.** c
2fa70 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f ache setting sho
2fa80 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 uld set it expli
2fa90 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 citly..**.** See
2faa0 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20 Also: [SQLite
2fab0 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 Shared-Cache Mod
2fac0 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 e].**.** Require
2fad0 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20 ments: [H10331]
2fae0 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37 [H10336] [H10337
2faf0 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 ] [H10339].*/.SQ
2fb00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2fb10 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 ite3_enable_shar
2fb20 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a ed_cache(int);..
2fb30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2fb40 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20 Attempt To Free
2fb50 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37 Heap Memory {H17
2fb60 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 340} <S30220>.**
2fb70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2fb80 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
2fb90 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d interface attem
2fba0 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79 pts to free N by
2fbb0 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d tes.** of heap m
2fbc0 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63 emory by dealloc
2fbd0 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 ating non-essent
2fbe0 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ial memory alloc
2fbf0 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 ations.** held b
2fc00 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c y the database l
2fc10 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d ibrary. {END} M
2fc20 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61 emory used to ca
2fc30 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 che database.**
2fc40 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65 pages to improve
2fc50 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 performance is
2fc60 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f an example of no
2fc70 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f n-essential memo
2fc80 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 ry..** sqlite3_r
2fc90 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
2fca0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
2fcb0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
2fcc0 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77 ally freed,.** w
2fcd0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f hich might be mo
2fce0 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 re or less than
2fcf0 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65 the amount reque
2fd00 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 sted..**.** Requ
2fd10 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34 irements: [H1734
2fd20 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 1] [H17342].*/.S
2fd30 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2fd40 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
2fd50 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a mory(int);../*.*
2fd60 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f * CAPI3REF: Impo
2fd70 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65 se A Limit On He
2fd80 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d ap Size {H17350}
2fd90 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 <S30220>.**.**
2fda0 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 The sqlite3_soft
2fdb0 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e _heap_limit() in
2fdc0 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61 terface places a
2fdd0 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a "soft" limit.**
2fde0 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f on the amount o
2fdf0 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68 f heap memory th
2fe00 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 at may be alloca
2fe10 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a ted by SQLite..*
2fe20 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c * If an internal
2fe30 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72 allocation is r
2fe40 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f equested that wo
2fe50 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a uld exceed the.*
2fe60 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 * soft heap limi
2fe70 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 t, [sqlite3_rele
2fe80 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 ase_memory()] is
2fe90 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a invoked one or.
2fea0 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f ** more times to
2feb0 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70 free up some sp
2fec0 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61 ace before the a
2fed0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72 llocation is per
2fee0 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 formed..**.** Th
2fef0 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65 e limit is calle
2ff00 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73 d "soft", becaus
2ff10 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65 e if [sqlite3_re
2ff20 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a lease_memory()].
2ff30 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73 ** cannot free s
2ff40 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79 ufficient memory
2ff50 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 to prevent the
2ff60 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 limit from being
2ff70 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68 exceeded,.** th
2ff80 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f e memory is allo
2ff90 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64 cated anyway and
2ffa0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 the current ope
2ffb0 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e ration proceeds.
2ffc0 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 .**.** A negativ
2ffd0 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20 e or zero value
2ffe0 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74 for N means that
2fff0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66 there is no sof
30000 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64 t heap limit and
30010 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c .** [sqlite3_rel
30020 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 ease_memory()] w
30030 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c ill only be call
30040 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 ed when memory i
30050 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20 s exhausted..**
30060 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 The default valu
30070 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 e for the soft h
30080 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72 eap limit is zer
30090 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 o..**.** SQLite
300a0 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66 makes a best eff
300b0 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65 ort to honor the
300c0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
300d0 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 ..** But if the
300e0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
300f0 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65 cannot be honore
30100 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c d, execution wil
30110 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69 l.** continue wi
30120 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e thout error or n
30130 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68 otification. Th
30140 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69 is is why the li
30150 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 mit is.** called
30160 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e a "soft" limit.
30170 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79 It is advisory
30180 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 only..**.** Pri
30190 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72 or to SQLite ver
301a0 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73 sion 3.5.0, this
301b0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
301c0 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65 nstrained the me
301d0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 mory.** allocate
301e0 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 d by a single th
301f0 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20 read - the same
30200 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20 thread in which
30210 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
30220 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67 runs. Beginning
30230 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72 with SQLite ver
30240 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20 sion 3.5.0, the
30250 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
30260 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f is.** applied to
30270 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68 all threads. Th
30280 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 e value specifie
30290 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68 d for the soft h
302a0 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 eap limit.** is
302b0 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f an upper bound o
302c0 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f n the total memo
302d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f ry allocation fo
302e0 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49 r all threads. I
302f0 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35 n.** version 3.5
30300 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d .0 there is no m
30310 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d echanism for lim
30320 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75 iting the heap u
30330 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 sage for.** indi
30340 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a vidual threads..
30350 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
30360 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20 ts:.** [H16351]
30370 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33 [H16352] [H16353
30380 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33 ] [H16354] [H163
30390 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 55] [H16358].*/.
303a0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
303b0 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 sqlite3_soft_hea
303c0 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f p_limit(int);../
303d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
303e0 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 xtract Metadata
303f0 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f About A Column O
30400 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 f A Table {H1285
30410 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S60300>.**.*
30420 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
30430 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 eturns metadata
30440 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 about a specific
30450 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 column of a spe
30460 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 cific.** databas
30470 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 e table accessib
30480 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 le using the [da
30490 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
304a0 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 n] handle.** pas
304b0 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
304c0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
304d0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f nt..**.** The co
304e0 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 lumn is identifi
304f0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 ed by the second
30500 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 , third and four
30510 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
30520 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
30530 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 n. The second pa
30540 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 rameter is eithe
30550 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 r the name of th
30560 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 e database.** (i
30570 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d .e. "main", "tem
30580 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 p" or an attache
30590 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 d database) cont
305a0 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 aining the speci
305b0 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 fied.** table or
305c0 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 NULL. If it is
305d0 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 NULL, then all a
305e0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
305f0 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a s are searched.*
30600 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 * for the table
30610 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 using the same a
30620 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 lgorithm used by
30630 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e the database en
30640 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c gine to.** resol
30650 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 ve unqualified t
30660 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e able references.
30670 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
30680 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 and fourth para
30690 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
306a0 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 unction are the
306b0 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
306c0 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 .** name of the
306d0 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 desired column,
306e0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 respectively. Ne
306f0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 ither of these p
30700 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 arameters.** may
30710 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 be NULL..**.**
30720 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 Metadata is retu
30730 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 rned by writing
30740 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f to the memory lo
30750 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 cations passed a
30760 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 s the 5th.** and
30770 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 subsequent para
30780 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
30790 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 unction. Any of
307a0 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 these arguments
307b0 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 may be.** NULL,
307c0 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
307d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
307e0 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 element of metad
307f0 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a ata is omitted..
30800 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
30810 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 e>.** <table bor
30820 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e der="1">.** <tr>
30830 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c <th> Parameter <
30840 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 th> Output<br>Ty
30850 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 pe <th> Descrip
30860 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c tion.**.** <tr><
30870 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e td> 5th <td> con
30880 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 st char* <td> Da
30890 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c ta type.** <tr><
308a0 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e td> 6th <td> con
308b0 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 st char* <td> Na
308c0 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f me of default co
308d0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
308e0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 .** <tr><td> 7th
308f0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
30900 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
30910 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 olumn has a NOT
30920 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a NULL constraint.
30930 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 ** <tr><td> 8th
30940 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
30950 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
30960 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 lumn is part of
30970 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a the PRIMARY KEY.
30980 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 ** <tr><td> 9th
30990 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
309a0 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
309b0 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43 lumn is [AUTOINC
309c0 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 REMENT].** </tab
309d0 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 le>.** </blockqu
309e0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ote>.**.** The m
309f0 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f emory pointed to
30a00 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65 by the characte
30a10 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 r pointers retur
30a20 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 ned for the.** d
30a30 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 eclaration type
30a40 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 and collation se
30a50 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 quence is valid
30a60 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e only until the n
30a70 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 ext.** call to a
30a80 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 ny SQLite API fu
30a90 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 nction..**.** If
30aa0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74 the specified t
30ab0 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 able is actually
30ac0 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 a view, an [err
30ad0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
30ae0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rned..**.** If t
30af0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c he specified col
30b00 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 umn is "rowid",
30b10 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 "oid" or "_rowid
30b20 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e _" and an.** [IN
30b30 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
30b40 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65 Y] column has be
30b50 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 en explicitly de
30b60 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 clared, then the
30b70 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d output.** param
30b80 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f eters are set fo
30b90 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79 r the explicitly
30ba0 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e declared column
30bb0 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f . If there is no
30bc0 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64 .** explicitly d
30bd0 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52 eclared [INTEGER
30be0 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f PRIMARY KEY] co
30bf0 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f lumn, then the o
30c00 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 utput.** paramet
30c10 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66 ers are set as f
30c20 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 ollows:.**.** <p
30c30 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20 re>.** data
30c40 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a type: "INTEGER".
30c50 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e ** collation
30c60 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41 sequence: "BINA
30c70 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e RY".** not n
30c80 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72 ull: 0.** pr
30c90 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 imary key: 1.**
30ca0 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65 auto increme
30cb0 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a nt: 0.** </pre>.
30cc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
30cd0 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 ion may load one
30ce0 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 or more schemas
30cf0 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 from database f
30d00 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 iles. If an.** e
30d10 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
30d20 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c ng this process,
30d30 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65 or if the reque
30d40 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f sted table or co
30d50 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 lumn.** cannot b
30d60 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 e found, an [err
30d70 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
30d80 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f rned and an erro
30d90 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a r message left.*
30da0 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 * in the [databa
30db0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 se connection] (
30dc0 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 to be retrieved
30dd0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 using sqlite3_er
30de0 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 rmsg())..**.** T
30df0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 his API is only
30e00 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 available if the
30e10 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d library was com
30e20 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
30e30 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 * [SQLITE_ENABLE
30e40 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
30e50 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C-preprocessor
30e60 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e symbol defined.
30e70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
30e80 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 nt sqlite3_table
30e90 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 _column_metadata
30ea0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
30eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
30ec0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 /* Connection ha
30ed0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
30ee0 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 char *zDbName,
30ef0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
30f00 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a e name or NULL *
30f10 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
30f20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 zTableName,
30f30 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f /* Table name */
30f40 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
30f50 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f ColumnName, /
30f60 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f * Column name */
30f70 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a . char const **
30f80 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f pzDataType, /
30f90 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 * OUTPUT: Declar
30fa0 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a ed data type */.
30fb0 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 char const **p
30fc0 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a zCollSeq, /*
30fd0 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 OUTPUT: Collati
30fe0 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 on sequence name
30ff0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e */. int *pNotN
31000 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ull,
31010 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 /* OUTPUT: Tru
31020 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f e if NOT NULL co
31030 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 nstraint exists
31040 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 */. int *pPrima
31050 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 ryKey,
31060 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 /* OUTPUT: True
31070 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 if column part
31080 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a of PK */. int *
31090 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 pAutoinc
310a0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 /* OUTPUT
310b0 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e : True if column
310c0 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 is auto-increme
310d0 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.);../*.**
310e0 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41 CAPI3REF: Load A
310f0 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32 n Extension {H12
31100 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 600} <S20500>.**
31110 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
31120 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69 ce loads an SQLi
31130 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 te extension lib
31140 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61 rary from the na
31150 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 med file..**.**
31160 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c {H12601} The sql
31170 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 ite3_load_extens
31180 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 ion() interface
31190 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64 attempts to load
311a0 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 an.**
311b0 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e SQLite extension
311c0 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e library contain
311d0 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a ed in the file z
311e0 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 File..**.** {H12
311f0 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70 602} The entry p
31200 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a oint is zProc..*
31210 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50 *.** {H12603} zP
31220 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e roc may be 0, in
31230 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
31240 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72 name of the entr
31250 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 y point.**
31260 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20 defaults to
31270 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 "sqlite3_extensi
31280 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 on_init"..**.**
31290 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c {H12604} The sql
312a0 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 ite3_load_extens
312b0 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 ion() interface
312c0 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 shall return.**
312d0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
312e0 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 _OK] on success
312f0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f and [SQLITE_ERRO
31300 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 R] if something
31310 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a goes wrong..**.*
31320 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e * {H12605} If an
31330 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e error occurs an
31340 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f d pzErrMsg is no
31350 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a t 0, then the.**
31360 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
31370 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f e3_load_extensio
31380 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 n()] interface s
31390 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a hall attempt to.
313a0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c ** fill
313b0 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 *pzErrMsg with
313c0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 error message te
313d0 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d xt stored in mem
313e0 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ory.**
313f0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
31400 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
31410 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c . {END} The cal
31420 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ling function.**
31430 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64 should
31440 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 free this memor
31450 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 y by calling [sq
31460 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a lite3_free()]..*
31470 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78 *.** {H12606} Ex
31480 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 tension loading
31490 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20 must be enabled
314a0 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 using.**
314b0 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c [sqlite3_enabl
314c0 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e e_load_extension
314d0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c ()] prior to cal
314e0 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a ling this API,.*
314f0 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72 * other
31500 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69 wise an error wi
31510 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a ll be returned..
31520 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
31530 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 t sqlite3_load_e
31540 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 xtension(. sqli
31550 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
31560 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 /* Load the ex
31570 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 tension into thi
31580 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 s database conne
31590 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 ction */. const
315a0 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 char *zFile,
315b0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
315c0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 shared library c
315d0 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 ontaining extens
315e0 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ion */. const c
315f0 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f har *zProc, /
31600 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 * Entry point.
31610 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 Derived from zFi
31620 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 le if 0 */. cha
31630 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 r **pzErrMsg
31640 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 /* Put error
31650 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 message here if
31660 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a not 0 */.);../*.
31670 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 ** CAPI3REF: Ena
31680 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 ble Or Disable E
31690 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 xtension Loading
316a0 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30 {H12620} <S2050
316b0 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 0>.**.** So as n
316c0 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 ot to open secur
316d0 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 ity holes in old
316e0 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 er applications
316f0 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 that are.** unpr
31700 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77 epared to deal w
31710 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f ith extension lo
31720 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 ading, and as a
31730 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 means of disabli
31740 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 ng.** extension
31750 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76 loading while ev
31760 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e aluating user-en
31770 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66 tered SQL, the f
31780 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 ollowing API.**
31790 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74 is provided to t
317a0 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 urn the [sqlite3
317b0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
317c0 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 )] mechanism on
317d0 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 and off..**.** E
317e0 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
317f0 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 is off by defau
31800 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 lt. See ticket #
31810 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 1863..**.** {H12
31820 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 621} Call the sq
31830 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 lite3_enable_loa
31840 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f d_extension() ro
31850 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 utine with onoff
31860 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ==1.**
31870 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f to turn extensio
31880 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 n loading on and
31890 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e call it with on
318a0 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a off==0 to turn.*
318b0 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61 * it ba
318c0 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a ck off again..**
318d0 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74 .** {H12622} Ext
318e0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 ension loading i
318f0 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 s off by default
31900 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
31910 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 int sqlite3_enab
31920 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le_load_extensio
31930 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 n(sqlite3 *db, i
31940 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a nt onoff);../*.*
31950 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f * CAPI3REF: Auto
31960 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 matically Load A
31970 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 n Extensions {H1
31980 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2640} <S20500>.*
31990 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61 *.** This API ca
319a0 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 n be invoked at
319b0 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20 program startup
319c0 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69 in order to regi
319d0 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d ster.** one or m
319e0 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c ore statically l
319f0 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 inked extensions
31a00 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76 that will be av
31a10 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c ailable.** to al
31a20 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 l new [database
31a30 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 connections]. {E
31a40 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ND}.**.** This r
31a50 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 outine stores a
31a60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 pointer to the e
31a70 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 xtension in an a
31a80 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 rray that is.**
31a90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 obtained from [s
31aa0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
31ab0 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20 . If you run a
31ac0 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 memory leak chec
31ad0 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 ker.** on your p
31ae0 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65 rogram and it re
31af0 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63 ports a leak bec
31b00 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72 ause of this arr
31b10 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 ay, invoke.** [s
31b20 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 qlite3_reset_aut
31b30 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 o_extension()] p
31b40 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e rior to shutdown
31b50 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d to free the mem
31b60 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ory..**.** {H126
31b70 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 41} This functio
31b80 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65 n registers an e
31b90 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 xtension entry p
31ba0 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 oint that is.**
31bb0 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 automat
31bc0 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 ically invoked w
31bd0 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64 henever a new [d
31be0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
31bf0 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on].**
31c00 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 is opened using
31c10 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
31c20 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
31c30 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 6()],.**
31c40 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 or [sqlite3_op
31c50 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 en_v2()]..**.**
31c60 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61 {H12642} Duplica
31c70 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 te extensions ar
31c80 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61 e detected so ca
31c90 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
31ca0 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d ne.** m
31cb0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 ultiple times wi
31cc0 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65 th the same exte
31cd0 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 nsion is harmles
31ce0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 s..**.** {H12643
31cf0 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 } This routine s
31d00 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
31d10 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e to the extension
31d20 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 in an array.**
31d30 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73 that is
31d40 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
31d50 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
31d60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 ]..**.** {H12644
31d70 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65 } Automatic exte
31d80 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 nsions apply acr
31d90 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e oss all threads.
31da0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
31db0 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f nt sqlite3_auto_
31dc0 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 extension(void (
31dd0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f *xEntryPoint)(vo
31de0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id));../*.** CAP
31df0 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74 I3REF: Reset Aut
31e00 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e omatic Extension
31e10 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30 Loading {H12660
31e20 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a } <S20500>.**.**
31e30 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 This function d
31e40 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 isables all prev
31e50 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 iously registere
31e60 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 d automatic.** e
31e70 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d xtensions. {END}
31e80 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20 It undoes the
31e90 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 effect of all pr
31ea0 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ior.** [sqlite3_
31eb0 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 auto_extension()
31ec0 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b ] calls..**.** {
31ed0 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e H12661} This fun
31ee0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 ction disables a
31ef0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ll previously re
31f00 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 gistered.**
31f10 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65 automatic e
31f20 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a xtensions..**.**
31f30 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66 {H12662} This f
31f40 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
31f50 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e automatic exten
31f60 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 sions in all thr
31f70 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eads..*/.SQLITE_
31f80 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
31f90 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 _reset_auto_exte
31fa0 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a nsion(void);../*
31fb0 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 .****** EXPERIME
31fc0 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 NTAL - subject t
31fd0 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
31fe0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a notice ********
31ff0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
32000 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
32010 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
32020 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72 mechanism is cur
32030 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 rently considere
32040 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 d.** to be exper
32050 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e imental. The in
32060 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 terface might ch
32070 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 ange in incompat
32080 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 ible ways..** If
32090 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c this is a probl
320a0 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e em for you, do n
320b0 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 ot use the inter
320c0 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d face at this tim
320d0 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 e..**.** When th
320e0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
320f0 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c mechanism stabil
32100 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 izes, we will de
32110 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 clare the.** int
32120 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 erface fixed, su
32130 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e pport it indefin
32140 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 itely, and remov
32150 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a e this comment..
32160 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 */../*.** Struct
32170 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 ures used by the
32180 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
32190 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 nterface.*/.type
321a0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
321b0 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f e3_vtab sqlite3_
321c0 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74 vtab;.typedef st
321d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
321e0 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f ex_info sqlite3_
321f0 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 index_info;.type
32200 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
32210 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73 e3_vtab_cursor s
32220 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
32230 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 or;.typedef stru
32240 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c ct sqlite3_modul
32250 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 e sqlite3_module
32260 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
32270 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 F: Virtual Table
32280 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d Object {H18000}
32290 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 <S20400>.** KEY
322a0 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d WORDS: sqlite3_m
322b0 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 odule {virtual t
322c0 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 able module}.**
322d0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
322e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
322f0 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c e, sometimes cal
32300 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c led a a "virtual
32310 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 table module",
32320 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 .** defines the
32330 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
32340 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 f a [virtual tab
32350 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 les]. .** This
32360 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73 structure consis
32370 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 ts mostly of met
32380 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 hods for the mod
32390 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 ule..**.** A vir
323a0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
323b0 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 e is created by
323c0 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 filling in a per
323d0 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 sistent.** insta
323e0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
323f0 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e cture and passin
32400 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 g a pointer to t
32410 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 hat instance.**
32420 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 to [sqlite3_crea
32430 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 te_module()] or
32440 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
32450 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a module_v2()]..**
32460 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f The registratio
32470 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 n remains valid
32480 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c until it is repl
32490 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72 aced by a differ
324a0 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 ent.** module or
324b0 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61 until the [data
324c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
324d0 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f closes. The co
324e0 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 ntent.** of this
324f0 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 structure must
32500 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65 not change while
32510 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65 it is registere
32520 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 d with.** any da
32530 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
32540 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c n..*/.struct sql
32550 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 ite3_module {.
32560 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 int iVersion;.
32570 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 int (*xCreate)(s
32580 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 qlite3*, void *p
32590 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Aux,.
325a0 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f int argc, co
325b0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a nst char *const*
325c0 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 argv,.
325d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
325e0 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 b **ppVTab, char
325f0 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f **);. int (*xCo
32600 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c nnect)(sqlite3*,
32610 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 void *pAux,.
32620 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
32630 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 argc, const char
32640 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 *const*argv,.
32650 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
32660 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 ite3_vtab **ppVT
32670 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 ab, char**);. i
32680 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 nt (*xBestIndex)
32690 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
326a0 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e VTab, sqlite3_in
326b0 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e dex_info*);. in
326c0 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 t (*xDisconnect)
326d0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
326e0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
326f0 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 Destroy)(sqlite3
32700 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
32710 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 int (*xOpen)(sq
32720 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
32730 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f b, sqlite3_vtab_
32740 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f cursor **ppCurso
32750 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f r);. int (*xClo
32760 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 se)(sqlite3_vtab
32770 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 _cursor*);. int
32780 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 (*xFilter)(sqli
32790 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
327a0 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f , int idxNum, co
327b0 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 nst char *idxStr
327c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
327d0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 int argc, sqli
327e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
327f0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 );. int (*xNext
32800 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 )(sqlite3_vtab_c
32810 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 ursor*);. int (
32820 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 *xEof)(sqlite3_v
32830 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 tab_cursor*);.
32840 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 int (*xColumn)(s
32850 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
32860 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e or*, sqlite3_con
32870 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 text*, int);. i
32880 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c nt (*xRowid)(sql
32890 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
328a0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
328b0 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 *pRowid);. int
328c0 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 (*xUpdate)(sqli
328d0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c te3_vtab *, int,
328e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
328f0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
32900 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 *);. int (*xBe
32910 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 gin)(sqlite3_vta
32920 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
32930 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 (*xSync)(sqlite
32940 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
32950 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 int (*xCommit)
32960 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
32970 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
32980 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 Rollback)(sqlite
32990 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
329a0 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e int (*xFindFun
329b0 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 ction)(sqlite3_v
329c0 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 tab *pVtab, int
329d0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 nArg, const char
329e0 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 *zName,.
329f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32a00 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 void (**pxFunc)(
32a10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
32a20 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
32a30 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 ue**),.
32a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f vo
32a50 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 id **ppArg);. i
32a60 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 nt (*xRename)(sq
32a70 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
32a80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
32a90 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 New);.};../*.**
32aa0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
32ab0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 l Table Indexing
32ac0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 Information {H1
32ad0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8100} <S20400>.*
32ae0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
32af0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a te3_index_info.*
32b00 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
32b10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
32b20 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 _index_info stru
32b30 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 cture and its su
32b40 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 bstructures is u
32b50 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 sed to.** pass i
32b60 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
32b70 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 and receive the
32b80 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b reply from the [
32b90 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d xBestIndex].** m
32ba0 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 ethod of a [virt
32bb0 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 ual table module
32bc0 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75 ]. The fields u
32bd0 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 nder **Inputs**
32be0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 are the.** input
32bf0 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 s to xBestIndex
32c00 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c and are read-onl
32c10 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69 y. xBestIndex i
32c20 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 nserts its.** re
32c30 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a sults into the *
32c40 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 *Outputs** field
32c50 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f s..**.** The aCo
32c60 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 nstraint[] array
32c70 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63 records WHERE c
32c80 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 lause constraint
32c90 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a s of the form:.*
32ca0 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e *.** <pre>column
32cb0 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a OP expr</pre>.*
32cc0 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73 *.** where OP is
32cd0 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c =, <, <=,
32ce0 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e >, or >=.
32cf0 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 The particular
32d00 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 operator is.**
32d10 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 stored in aConst
32d20 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 raint[].op. The
32d30 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f index of the co
32d40 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69 lumn is stored i
32d50 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 n.** aConstraint
32d60 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f [].iColumn. aCo
32d70 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c nstraint[].usabl
32d80 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65 e is TRUE if the
32d90 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20 .** expr on the
32da0 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 right-hand side
32db0 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64 can be evaluated
32dc0 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63 (and thus the c
32dd0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 onstraint.** is
32de0 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 usable) and fals
32df0 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a e if it cannot..
32e00 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 **.** The optimi
32e10 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c zer automaticall
32e20 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 y inverts terms
32e30 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 of the form "exp
32e40 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 r OP column".**
32e50 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 and makes other
32e60 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 simplifications
32e70 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 to the WHERE cla
32e80 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 use in an attemp
32e90 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d t to.** get as m
32ea0 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 any WHERE clause
32eb0 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 terms into the
32ec0 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 form shown above
32ed0 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a as possible..**
32ee0 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 The aConstraint
32ef0 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 [] array only re
32f00 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 ports WHERE clau
32f10 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 se terms in the
32f20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 correct.** form
32f30 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 that refer to th
32f40 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72 e particular vir
32f50 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 tual table being
32f60 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 queried..**.**
32f70 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
32f80 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 t the ORDER BY c
32f90 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20 lause is stored
32fa0 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a in aOrderBy[]..*
32fb0 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61 * Each term of a
32fc0 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20 OrderBy records
32fd0 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 a column of the
32fe0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e ORDER BY clause.
32ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 .**.** The [xBes
33000 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d tIndex] method m
33010 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 ust fill aConstr
33020 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68 aintUsage[] with
33030 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
33040 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d about what param
33050 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f eters to pass to
33060 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72 xFilter. If ar
33070 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a gvIndex>0 then.*
33080 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 * the right-hand
33090 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72 side of the cor
330a0 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 responding aCons
330b0 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c traint[] is eval
330c0 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 uated.** and bec
330d0 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64 omes the argvInd
330e0 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61 ex-th entry in a
330f0 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 rgv. If aConstr
33100 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 aintUsage[].omit
33110 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 .** is true, the
33120 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 n the constraint
33130 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 is assumed to b
33140 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 e fully handled
33150 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 by the.** virtua
33160 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e l table and is n
33170 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e ot checked again
33180 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a by SQLite..**.*
33190 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 * The idxNum and
331a0 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61 idxPtr values a
331b0 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20 re recorded and
331c0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a passed into the.
331d0 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 ** [xFilter] met
331e0 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 hod..** [sqlite3
331f0 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64 _free()] is used
33200 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20 to free idxPtr
33210 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a if and only iff.
33220 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 ** needToFreeIdx
33230 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a Ptr is true..**.
33240 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f ** The orderByCo
33250 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 nsumed means tha
33260 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 t output from [x
33270 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 Filter]/[xNext]
33280 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a will occur in.**
33290 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 the correct ord
332a0 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 er to satisfy th
332b0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
332c0 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 e so that no sep
332d0 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 arate.** sorting
332e0 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65 step is require
332f0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 d..**.** The est
33300 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65 imatedCost value
33310 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 is an estimate
33320 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64 of the cost of d
33330 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 oing the.** part
33340 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 icular lookup.
33350 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 A full scan of a
33360 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e table with N en
33370 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 tries should hav
33380 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e e.** a cost of N
33390 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72 . A binary sear
333a0 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 ch of a table of
333b0 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c N entries shoul
333c0 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 d have a.** cost
333d0 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c of approximatel
333e0 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 y log(N)..*/.str
333f0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
33400 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e x_info {. /* In
33410 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 puts */. int nC
33420 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 onstraint;
33430 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
33440 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f f entries in aCo
33450 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 nstraint */. st
33460 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
33470 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a ex_constraint {.
33480 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e int iColumn
33490 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
334a0 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 * Column on left
334b0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f -hand side of co
334c0 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 nstraint */.
334d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f unsigned char o
334e0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f p; /* Co
334f0 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f nstraint operato
33500 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e r */. unsign
33510 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20 ed char usable;
33520 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
33530 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 his constraint i
33540 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 s usable */.
33550 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74 int iTermOffset
33560 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ; /* Us
33570 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 ed internally -
33580 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c xBestIndex shoul
33590 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 d ignore */. }
335a0 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 *aConstraint;
335b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c /* Tabl
335c0 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73 e of WHERE claus
335d0 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f e constraints */
335e0 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b . int nOrderBy;
335f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33600 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 Number of terms
33610 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 in the ORDER BY
33620 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 clause */. str
33630 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
33640 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20 x_orderby {.
33650 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
33660 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
33670 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 lumn number */.
33680 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
33690 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a r desc; /*
336a0 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20 True for DESC.
336b0 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 False for ASC.
336c0 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 */. } *aOrderBy
336d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
336e0 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
336f0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f clause */. /* O
33700 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 utputs */. stru
33710 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 ct sqlite3_index
33720 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 _constraint_usag
33730 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76 e {. int argv
33740 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 Index;
33750 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 /* if >0, const
33760 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66 raint is part of
33770 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72 argv to xFilter
33780 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
33790 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 char omit;
337a0 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 /* Do not code
337b0 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20 a test for this
337c0 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 constraint */.
337d0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 } *aConstraintUs
337e0 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 age;. int idxNu
337f0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
33800 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 /* Number used
33810 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 to identify the
33820 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 index */. char
33830 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20 *idxStr;
33840 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 /* String
33850 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 , possibly obtai
33860 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
33870 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 _malloc */. int
33880 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 needToFreeIdxSt
33890 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 r; /* Free
338a0 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c idxStr using sql
338b0 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74 ite3_free() if t
338c0 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 rue */. int ord
338d0 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 erByConsumed;
338e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f /* True if o
338f0 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79 utput is already
33900 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f ordered */. do
33910 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f uble estimatedCo
33920 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69 st; /* Esti
33930 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73 mated cost of us
33940 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a ing this index *
33950 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c /.};.#define SQL
33960 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
33970 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65 AINT_EQ 2.#de
33980 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 fine SQLITE_INDE
33990 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 X_CONSTRAINT_GT
339a0 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 4.#define SQL
339b0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 ITE_INDEX_CONSTR
339c0 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 AINT_LE 8.#de
339d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 fine SQLITE_INDE
339e0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 X_CONSTRAINT_LT
339f0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 16.#define SQ
33a00 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
33a10 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 RAINT_GE 32.#
33a20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
33a30 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d DEX_CONSTRAINT_M
33a40 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 ATCH 64../*.** C
33a50 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 API3REF: Registe
33a60 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c r A Virtual Tabl
33a70 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e Implementation
33a80 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30 {H18200} <S2040
33a90 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
33aa0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f AL.**.** This ro
33ab0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f utine is used to
33ac0 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 register a new
33ad0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
33ae0 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 odule] name..**
33af0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 Module names mus
33b00 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 t be registered
33b10 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 before.** creati
33b20 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 ng a new [virtua
33b30 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 l table] using t
33b40 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 he module, or be
33b50 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 fore using a.**
33b60 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72 preexisting [vir
33b70 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 tual table] for
33b80 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a the module..**.*
33b90 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d * The module nam
33ba0 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 e is registered
33bb0 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 on the [database
33bc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
33bd0 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 cified.** by the
33be0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
33bf0 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 . The name of t
33c00 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 he module is giv
33c10 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 en by the .** se
33c20 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 cond parameter.
33c30 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d The third param
33c40 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 eter is a pointe
33c50 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c r to.** the impl
33c60 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
33c70 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
33c80 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 module]. The
33c90 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 fourth.** parame
33ca0 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 ter is an arbitr
33cb0 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20 ary client data
33cc0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 pointer that is
33cd0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a passed through.*
33ce0 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 * into the [xCre
33cf0 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 ate] and [xConne
33d00 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 ct] methods of t
33d10 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
33d20 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 module.** when
33d30 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 a new virtual ta
33d40 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20 ble is be being
33d50 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69 created or reini
33d60 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 tialized..**.**
33d70 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68 This interface h
33d80 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 as exactly the s
33d90 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61 ame effect as ca
33da0 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 lling.** [sqlite
33db0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 3_create_module_
33dc0 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c v2()] with a NUL
33dd0 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65 L client data de
33de0 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c structor..*/.SQL
33df0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
33e00 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
33e10 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
33e20 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 odule(. sqlite3
33e30 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
33e40 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f /* SQLite co
33e50 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 nnection to regi
33e60 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 ster module with
33e70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
33e80 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
33e90 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
33ea0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 module */. cons
33eb0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
33ec0 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 *p, /* Method
33ed0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
33ee0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 */. void *pCli
33ef0 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20 entData
33f00 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 /* Client data
33f10 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e for xCreate/xCon
33f20 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a nect */.);../*.*
33f30 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 * CAPI3REF: Regi
33f40 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 ster A Virtual T
33f50 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 able Implementat
33f60 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 ion {H18210} <S2
33f70 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0400>.** EXPERIM
33f80 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
33f90 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e routine is iden
33fa0 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 tical to the [sq
33fb0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
33fc0 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a ule()] method,.*
33fd0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
33fe0 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61 has an extra pa
33ff0 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69 rameter to speci
34000 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 fy .** a destruc
34010 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 tor function for
34020 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61 the client data
34030 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 pointer. SQLit
34040 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 e will.** invoke
34050 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
34060 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 function (if it
34070 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 is not NULL) whe
34080 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c n SQLite.** no l
34090 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 onger needs the
340a0 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e pClientData poin
340b0 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 ter. .*/.SQLITE
340c0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
340d0 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
340e0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
340f0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 le_v2(. sqlite3
34100 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
34110 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f /* SQLite co
34120 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 nnection to regi
34130 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 ster module with
34140 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
34150 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
34160 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
34170 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 module */. cons
34180 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
34190 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 *p, /* Method
341a0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
341b0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 */. void *pCli
341c0 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20 entData,
341d0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 /* Client data
341e0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e for xCreate/xCon
341f0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a nect */. void(*
34200 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 xDestroy)(void*)
34210 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 /* Module d
34220 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 estructor functi
34230 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 on */.);../*.**
34240 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
34250 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 l Table Instance
34260 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d Object {H18010}
34270 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 <S20400>.** KEY
34280 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 WORDS: sqlite3_v
34290 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e tab.** EXPERIMEN
342a0 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 TAL.**.** Every
342b0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
342c0 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 odule] implement
342d0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 ation uses a sub
342e0 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 class.** of the
342f0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
34300 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 ure to describe
34310 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 a particular ins
34320 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 tance.** of the
34330 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e [virtual table].
34340 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 Each subclass
34350 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f will.** be tailo
34360 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 red to the speci
34370 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 fic needs of the
34380 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
34390 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 tation..** The p
343a0 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 urpose of this s
343b0 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 uperclass is to
343c0 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 define certain f
343d0 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a ields that are.*
343e0 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 * common to all
343f0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
34400 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 ations..**.** Vi
34410 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 rtual tables met
34420 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 hods can set an
34430 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 error message by
34440 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 assigning a.**
34450 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 string obtained
34460 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 from [sqlite3_mp
34470 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 rintf()] to zErr
34480 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 Msg. The method
34490 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 should.** take
344a0 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 care that any pr
344b0 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 ior string is fr
344c0 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f eed by a call to
344d0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
344e0 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 ].** prior to as
344f0 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 signing a new st
34500 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e ring to zErrMsg.
34510 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f After the erro
34520 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 r message.** is
34530 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 delivered up to
34540 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 the client appli
34550 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 cation, the stri
34560 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d ng will be autom
34570 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 atically.** free
34580 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 d by sqlite3_fre
34590 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 e() and the zErr
345a0 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 Msg field will b
345b0 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 e zeroed..*/.str
345c0 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 uct sqlite3_vtab
345d0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 {. const sqlit
345e0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
345f0 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 le; /* The modu
34600 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 le for this virt
34610 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 ual table */. i
34620 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
34630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
34640 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c * Used internall
34650 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 y */. char *zEr
34660 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
34670 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
34680 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c message from sql
34690 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a ite3_mprintf() *
346a0 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 /. /* Virtual t
346b0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
346c0 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 ions will typica
346d0 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e lly add addition
346e0 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a al fields */.};.
346f0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
34700 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 Virtual Table C
34710 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 ursor Object {H
34720 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18020} <S20400>.
34730 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
34740 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
34750 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 {virtual table
34760 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 cursor}.** EXPER
34770 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 IMENTAL.**.** Ev
34780 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 ery [virtual tab
34790 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 le module] imple
347a0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 mentation uses a
347b0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 subclass of the
347c0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
347d0 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 ructure to descr
347e0 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 ibe cursors that
347f0 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a point into the.
34800 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ** [virtual tabl
34810 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a e] and are used.
34820 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 ** to loop throu
34830 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 gh the virtual t
34840 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 able. Cursors a
34850 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 re created using
34860 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
34870 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 _module.xOpen |
34880 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 xOpen] method of
34890 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 the module and
348a0 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a are destroyed.**
348b0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
348c0 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c _module.xClose |
348d0 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e xClose] method.
348e0 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 Cussors are us
348f0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 ed.** by the [xF
34900 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c ilter], [xNext],
34910 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d [xEof], [xColum
34920 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d n], and [xRowid]
34930 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 methods.** of t
34940 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 he module. Each
34950 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
34960 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 tation will defi
34970 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ne.** the conten
34980 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 t of a cursor st
34990 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 ructure to suit
349a0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a its own needs..*
349b0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 *.** This superc
349c0 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f lass exists in o
349d0 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 rder to define f
349e0 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 ields of the cur
349f0 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 sor that.** are
34a00 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d common to all im
34a10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
34a20 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
34a30 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 _vtab_cursor {.
34a40 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
34a50 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 Vtab; /* Vi
34a60 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 rtual table of t
34a70 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 his cursor */.
34a80 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
34a90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
34aa0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
34ab0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
34ac0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
34ad0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 ** CAPI3REF: Dec
34ae0 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 lare The Schema
34af0 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 Of A Virtual Tab
34b00 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 le {H18280} <S20
34b10 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
34b20 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
34b30 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 xCreate] and [xC
34b40 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 onnect] methods
34b50 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c of a.** [virtual
34b60 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 table module] c
34b70 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 all this interfa
34b80 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 ce.** to declare
34b90 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 the format (the
34ba0 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 names and datat
34bb0 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 ypes of the colu
34bc0 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 mns) of.** the v
34bd0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 irtual tables th
34be0 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f ey implement..*/
34bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
34c00 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
34c10 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c int sqlite3_decl
34c20 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 are_vtab(sqlite3
34c30 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
34c40 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 SQL);../*.** CAP
34c50 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 I3REF: Overload
34c60 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 A Function For A
34c70 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b Virtual Table {
34c80 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e H18300} <S20400>
34c90 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
34ca0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 .**.** Virtual t
34cb0 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 ables can provid
34cc0 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d e alternative im
34cd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
34ce0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 functions.** us
34cf0 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 ing the [xFindFu
34d00 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f nction] method o
34d10 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 f the [virtual t
34d20 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a able module]. .
34d30 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 ** But global ve
34d40 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 rsions of those
34d50 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 functions.** mus
34d60 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 t exist in order
34d70 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 to be overloade
34d80 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 d..**.** This AP
34d90 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 I makes sure a g
34da0 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 lobal version of
34db0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
34dc0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
34dd0 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 name and number
34de0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 of parameters e
34df0 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 xists. If no su
34e00 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 ch function exis
34e10 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 ts.** before thi
34e20 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c s API is called,
34e30 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 a new function
34e40 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 is created. The
34e50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
34e60 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 ** of the new fu
34e70 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 nction always ca
34e80 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f uses an exceptio
34e90 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 n to be thrown.
34ea0 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 So.** the new f
34eb0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 unction is not g
34ec0 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 ood for anything
34ed0 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 by itself. Its
34ee0 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 only.** purpose
34ef0 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 is to be a plac
34f00 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e eholder function
34f10 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 that can be ove
34f20 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 rloaded.** by a
34f30 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e [virtual table].
34f40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
34f50 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
34f60 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f AL int sqlite3_o
34f70 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e verload_function
34f80 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 (sqlite3*, const
34f90 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 char *zFuncName
34fa0 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a , int nArg);../*
34fb0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
34fc0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c e to the virtual
34fd0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
34fe0 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 defined above (
34ff0 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 back up.** to a
35000 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 comment remarkab
35010 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 ly similar to th
35020 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 is one) is curre
35030 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a ntly considered.
35040 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d ** to be experim
35050 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 ental. The inte
35060 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e rface might chan
35070 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 ge in incompatib
35080 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 le ways..** If t
35090 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d his is a problem
350a0 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 for you, do not
350b0 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 use the interfa
350c0 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ce at this time.
350d0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
350e0 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 virtual-table me
350f0 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a chanism stabiliz
35100 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c es, we will decl
35110 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 are the.** inter
35120 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 face fixed, supp
35130 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 ort it indefinit
35140 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 ely, and remove
35150 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a this comment..**
35160 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 .****** EXPERIME
35170 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 NTAL - subject t
35180 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
35190 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a notice ********
351a0 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ******.*/../*.**
351b0 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e CAPI3REF: A Han
351c0 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 dle To An Open B
351d0 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 LOB {H17800} <S3
351e0 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0230>.** KEYWORD
351f0 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d S: {BLOB handle}
35200 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a {BLOB handles}.
35210 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
35220 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
35230 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f represents an o
35240 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 pen BLOB on whic
35250 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c h.** [sqlite3_bl
35260 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d ob_open | increm
35270 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 ental BLOB I/O]
35280 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 can be performed
35290 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 ..** Objects of
352a0 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72 this type are cr
352b0 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 eated by [sqlite
352c0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 3_blob_open()].*
352d0 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 * and destroyed
352e0 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 by [sqlite3_blob
352f0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 _close()]..** Th
35300 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
35310 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c read()] and [sql
35320 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
35330 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a )] interfaces.**
35340 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
35350 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d read or write sm
35360 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 all subsections
35370 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 of the BLOB..**
35380 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f The [sqlite3_blo
35390 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 b_bytes()] inter
353a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
353b0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f size of the BLO
353c0 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 B in bytes..*/.t
353d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
353e0 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 lite3_blob sqlit
353f0 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_blob;../*.**
35400 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 CAPI3REF: Open A
35410 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d BLOB For Increm
35420 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 ental I/O {H1781
35430 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
35440 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
35450 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 s opens a [BLOB
35460 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d handle | handle]
35470 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 to the BLOB loc
35480 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 ated.** in row i
35490 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c Row, column zCol
354a0 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c umn, table zTabl
354b0 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 e in database zD
354c0 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 b;.** in other w
354d0 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 ords, the same B
354e0 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 LOB that would b
354f0 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a e selected by:.*
35500 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 *.** <pre>.**
35510 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e SELECT zColumn
35520 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 FROM zDb.zTable
35530 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d WHERE [rowid] =
35540 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e iRow;.** </pre>
35550 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 {END}.**.** If
35560 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
35570 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ter is non-zero,
35580 20 74 68 65 20 74 68 65 20 42 4c 4f 42 20 69 73 the the BLOB is
35590 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
355a0 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63 .** and write ac
355b0 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a cess. If it is z
355c0 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 ero, the BLOB is
355d0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
355e0 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e access..**.** N
355f0 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 ote that the dat
35600 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f abase name is no
35610 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 t the filename t
35620 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 hat contains.**
35630 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 the database but
35640 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 rather the symb
35650 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 olic name of the
35660 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a database that.*
35670 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 * is assigned wh
35680 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
35690 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 is connected usi
356a0 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 ng [ATTACH]..**
356b0 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 For the main dat
356c0 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 abase file, the
356d0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 database name is
356e0 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 "main"..** For
356f0 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 TEMP tables, the
35700 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 database name i
35710 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 s "temp"..**.**
35720 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c On success, [SQL
35730 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 ITE_OK] is retur
35740 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 ned and the new
35750 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 [BLOB handle] is
35760 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a written.** to *
35770 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 ppBlob. Otherwis
35780 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 e an [error code
35790 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
357a0 64 20 61 6e 79 20 76 61 6c 75 65 20 77 72 69 74 d any value writ
357b0 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f ten.** to *ppBlo
357c0 62 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 b should not be
357d0 75 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c used by the call
357e0 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 er..** This func
357f0 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 5b 64 tion sets the [d
35800 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
35810 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 65 20 61 on] error code a
35820 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61 63 nd message.** ac
35830 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71 cessible via [sq
35840 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d lite3_errcode()]
35850 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 and [sqlite3_er
35860 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 rmsg()]..**.** I
35870 66 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 61 f the row that a
35880 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 70 6f 69 BLOB handle poi
35890 6e 74 73 20 74 6f 20 69 73 20 6d 6f 64 69 66 69 nts to is modifi
358a0 65 64 20 62 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 ed by an.** [UPD
358b0 41 54 45 5d 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 ATE], [DELETE],
358c0 6f 72 20 62 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 or by [ON CONFLI
358d0 43 54 5d 20 73 69 64 65 2d 65 66 66 65 63 74 73 CT] side-effects
358e0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 42 4c 4f .** then the BLO
358f0 42 20 68 61 6e 64 6c 65 20 69 73 20 6d 61 72 6b B handle is mark
35900 65 64 20 61 73 20 22 65 78 70 69 72 65 64 22 2e ed as "expired".
35910 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 72 75 65 .** This is true
35920 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f if any column o
35930 66 20 74 68 65 20 72 6f 77 20 69 73 20 63 68 61 f the row is cha
35940 6e 67 65 64 2c 20 65 76 65 6e 20 61 20 63 6f 6c nged, even a col
35950 75 6d 6e 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 umn.** other tha
35960 6e 20 74 68 65 20 6f 6e 65 20 74 68 65 20 42 4c n the one the BL
35970 4f 42 20 68 61 6e 64 6c 65 20 69 73 20 6f 70 65 OB handle is ope
35980 6e 20 6f 6e 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 n on..** Calls t
35990 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f o [sqlite3_blob_
359a0 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c read()] and [sql
359b0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
359c0 29 5d 20 66 6f 72 0a 2a 2a 20 61 20 65 78 70 69 )] for.** a expi
359d0 72 65 64 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 red BLOB handle
359e0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 72 65 74 fail with an ret
359f0 75 72 6e 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c urn code of [SQL
35a00 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 ITE_ABORT]..** C
35a10 68 61 6e 67 65 73 20 77 72 69 74 74 65 6e 20 69 hanges written i
35a20 6e 74 6f 20 61 20 42 4c 4f 42 20 70 72 69 6f 72 nto a BLOB prior
35a30 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 65 78 70 to the BLOB exp
35a40 69 72 69 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a iring are not.**
35a50 20 72 6f 6c 6c 62 61 63 6b 20 62 79 20 74 68 65 rollback by the
35a60 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 expiration of t
35a70 68 65 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63 he BLOB. Such c
35a80 68 61 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e hanges will even
35a90 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 tually.** commit
35aa0 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 if the transact
35ab0 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f ion continues to
35ac0 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a completion..**.
35ad0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
35ae0 0a 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 .** [H17813] [H1
35af0 37 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 7814] [H17816] [
35b00 48 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d H17819] [H17821]
35b10 20 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c [H17824].*/.SQL
35b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
35b30 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 te3_blob_open(.
35b40 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e sqlite3*,. con
35b50 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 st char *zDb,.
35b60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
35b70 6c 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 le,. const char
35b80 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c *zColumn,. sql
35b90 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c ite3_int64 iRow,
35ba0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 . int flags,.
35bb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 sqlite3_blob **p
35bc0 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 pBlob.);../*.**
35bd0 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 CAPI3REF: Close
35be0 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 A BLOB Handle {H
35bf0 31 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 17830} <S30230>.
35c00 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 **.** Closes an
35c10 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c open [BLOB handl
35c20 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e e]..**.** Closin
35c30 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 g a BLOB shall c
35c40 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 ause the current
35c50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
35c60 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 commit.** if the
35c70 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 re are no other
35c80 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e BLOBs, no pendin
35c90 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 g prepared state
35ca0 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a ments, and the.*
35cb0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
35cc0 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 ction is in [aut
35cd0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a ocommit mode]..*
35ce0 2a 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 * If any writes
35cf0 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 were made to the
35d00 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 BLOB, they migh
35d10 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 t be held in cac
35d20 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 he.** until the
35d30 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 close operation
35d40 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 if they will fit
35d50 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c . {END}.**.** Cl
35d60 6f 73 69 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f osing the BLOB o
35d70 66 74 65 6e 20 66 6f 72 63 65 73 20 74 68 65 20 ften forces the
35d80 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 changes.** out t
35d90 6f 20 64 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 o disk and so if
35da0 20 61 6e 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 any I/O errors
35db0 6f 63 63 75 72 2c 20 74 68 65 79 20 77 69 6c 6c occur, they will
35dc0 20 6c 69 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a likely occur.**
35dd0 20 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 at the time whe
35de0 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 63 6c n the BLOB is cl
35df0 6f 73 65 64 2e 20 20 7b 48 31 37 38 33 33 7d 20 osed. {H17833}
35e00 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 Any errors that
35e10 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 occur during.**
35e20 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f closing are repo
35e30 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 rted as a non-ze
35e40 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e ro return value.
35e50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 .**.** The BLOB
35e60 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 is closed uncond
35e70 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e itionally. Even
35e80 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 if this routine
35e90 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 returns.** an e
35ea0 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 rror code, the B
35eb0 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f LOB is still clo
35ec0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 sed..**.** Requi
35ed0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 rements:.** [H17
35ee0 38 33 33 5d 20 5b 48 31 37 38 33 36 5d 20 5b 48 833] [H17836] [H
35ef0 31 37 38 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 17839].*/.SQLITE
35f00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
35f10 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 _blob_close(sqli
35f20 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a te3_blob *);../*
35f30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
35f40 74 75 72 6e 20 54 68 65 20 53 69 7a 65 20 4f 66 turn The Size Of
35f50 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 An Open BLOB {H
35f60 31 37 38 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a 17840} <S30230>.
35f70 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 **.** Returns th
35f80 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
35f90 6f 66 20 74 68 65 20 42 4c 4f 42 20 61 63 63 65 of the BLOB acce
35fa0 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 6f ssible via the o
35fb0 70 65 6e 0a 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61 pen.** []BLOB ha
35fc0 6e 64 6c 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c ndle] in its onl
35fd0 79 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a y argument..**.*
35fe0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
35ff0 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 ** [H17843].*/.S
36000 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
36010 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 lite3_blob_bytes
36020 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 (sqlite3_blob *)
36030 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
36040 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 72 6f F: Read Data Fro
36050 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 m A BLOB Increme
36060 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 ntally {H17850}
36070 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30230>.**.** T
36080 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
36090 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 used to read dat
360a0 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b a from an open [
360b0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 BLOB handle] int
360c0 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 o a.** caller-su
360d0 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e pplied buffer. N
360e0 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 bytes of data a
360f0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 re copied into b
36100 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 uffer Z.** from
36110 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 the open BLOB, s
36120 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 tarting at offse
36130 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a t iOffset..**.**
36140 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 If offset iOffs
36150 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 et is less than
36160 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 N bytes from the
36170 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 end of the BLOB
36180 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 ,.** [SQLITE_ERR
36190 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OR] is returned
361a0 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 and no data is r
361b0 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f ead. If N or iO
361c0 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 ffset is.** less
361d0 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c than zero, [SQL
361e0 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 ITE_ERROR] is re
361f0 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 turned and no da
36200 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a ta is read..**.*
36210 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 * An attempt to
36220 72 65 61 64 20 66 72 6f 6d 20 61 6e 20 65 78 70 read from an exp
36230 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c ired [BLOB handl
36240 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e e] fails with an
36250 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f .** error code o
36260 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d f [SQLITE_ABORT]
36270 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
36280 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ss, SQLITE_OK is
36290 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 returned..** Ot
362a0 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65 72 72 herwise, an [err
362b0 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b or code] or an [
362c0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
362d0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
362e0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
362f0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 ents:.** [H17853
36300 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 38 ] [H17856] [H178
36310 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 31 59] [H17862] [H1
36320 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 5b 7863] [H17865] [
36330 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 H17868].*/.SQLIT
36340 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
36350 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 3_blob_read(sqli
36360 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 te3_blob *, void
36370 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 *Z, int N, int
36380 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a iOffset);../*.**
36390 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 65 CAPI3REF: Write
363a0 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f Data Into A BLO
363b0 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 B Incrementally
363c0 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 33 30 {H17870} <S30230
363d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
363e0 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
363f0 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f write data into
36400 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 an open [BLOB h
36410 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a andle] from a.**
36420 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 caller-supplied
36430 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 buffer. N bytes
36440 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
36450 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ied from the buf
36460 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 fer Z.** into th
36470 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 e open BLOB, sta
36480 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 rting at offset
36490 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 iOffset..**.** I
364a0 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 f the [BLOB hand
364b0 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74 68 le] passed as th
364c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
364d0 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 was not opened
364e0 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 for.** writing (
364f0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
36500 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
36510 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 blob_open()] was
36520 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 zero),.** this
36530 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
36540 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c [SQLITE_READONL
36550 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 Y]..**.** This f
36560 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 unction may only
36570 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 6e 74 modify the cont
36580 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 ents of the BLOB
36590 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 ; it is.** not p
365a0 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 ossible to incre
365b0 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ase the size of
365c0 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 a BLOB using thi
365d0 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 s API..** If off
365e0 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c set iOffset is l
365f0 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 ess than N bytes
36600 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 from the end of
36610 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 the BLOB,.** [S
36620 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
36630 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
36640 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e data is written.
36650 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 If N is.** les
36660 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c s than zero [SQL
36670 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 ITE_ERROR] is re
36680 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 turned and no da
36690 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a ta is written..*
366a0 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 *.** An attempt
366b0 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e 20 65 to write to an e
366c0 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e xpired [BLOB han
366d0 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20 dle] fails with
366e0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 an.** error code
366f0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 of [SQLITE_ABOR
36700 54 5d 2e 20 20 57 72 69 74 65 73 20 74 6f 20 74 T]. Writes to t
36710 68 65 20 42 4c 4f 42 20 74 68 61 74 20 6f 63 63 he BLOB that occ
36720 75 72 72 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20 urred.** before
36730 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 the [BLOB handle
36740 5d 20 65 78 70 69 72 65 64 20 61 72 65 20 6e 6f ] expired are no
36750 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 t rolled back by
36760 20 74 68 65 0a 2a 2a 20 65 78 70 69 72 61 74 69 the.** expirati
36770 6f 6e 20 6f 66 20 74 68 65 20 68 61 6e 64 6c 65 on of the handle
36780 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 , though of cour
36790 73 65 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 se those changes
367a0 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65 20 62 might.** have b
367b0 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 een overwritten
367c0 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
367d0 20 74 68 61 74 20 65 78 70 69 72 65 64 20 74 68 that expired th
367e0 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a e BLOB handle.**
367f0 20 6f 72 20 62 79 20 6f 74 68 65 72 20 69 6e 64 or by other ind
36800 65 70 65 6e 64 65 6e 74 20 73 74 61 74 65 6d 65 ependent stateme
36810 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 nts..**.** On su
36820 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b ccess, SQLITE_OK
36830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
36840 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 20 Otherwise, an
36850 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 [error code] or
36860 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 an [extended err
36870 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
36880 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 rned..**.** Requ
36890 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
368a0 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b 7873] [H17874] [
368b0 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 5d H17875] [H17876]
368c0 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 37 [H17877] [H1787
368d0 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 37 9] [H17882] [H17
368e0 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 885].** [H17888]
368f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
36900 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
36910 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c write(sqlite3_bl
36920 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ob *, const void
36930 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 *z, int n, int
36940 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a iOffset);../*.**
36950 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
36960 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f al File System O
36970 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d 20 bjects {H11200}
36980 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 <S20100>.**.** A
36990 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73 virtual filesys
369a0 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20 tem (VFS) is an
369b0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
369c0 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c ject.** that SQL
369d0 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65 ite uses to inte
369e0 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 ract.** with the
369f0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
36a00 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d ating system. M
36a10 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64 ost SQLite build
36a20 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a s come with a.**
36a30 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 single default
36a40 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 72 VFS that is appr
36a50 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 opriate for the
36a60 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a host computer..*
36a70 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 * New VFSes can
36a80 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e be registered an
36a90 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 d existing VFSes
36aa0 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 can be unregist
36ab0 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c ered..** The fol
36ac0 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 lowing interface
36ad0 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a s are provided..
36ae0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
36af0 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 3_vfs_find() int
36b00 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
36b10 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46 pointer to a VF
36b20 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 S given its name
36b30 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 ..** Names are c
36b40 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a ase sensitive..*
36b50 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 6f * Names are zero
36b60 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
36b70 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 8 strings..** If
36b80 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 there is no mat
36b90 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ch, a NULL point
36ba0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
36bb0 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 ** If zVfsName i
36bc0 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 s NULL then the
36bd0 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 72 default VFS is r
36be0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e eturned..**.** N
36bf0 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 67 ew VFSes are reg
36c00 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 6c istered with sql
36c10 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
36c20 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 r()..** Each new
36c30 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65 VFS becomes the
36c40 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 20 default VFS if
36c50 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 the makeDflt fla
36c60 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 g is set..** The
36c70 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 65 same VFS can be
36c80 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c 74 registered mult
36c90 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 6f iple times witho
36ca0 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f ut injury..** To
36cb0 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 6e make an existin
36cc0 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 64 g VFS into the d
36cd0 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 69 efault VFS, regi
36ce0 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a ster it again.**
36cf0 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 66 with the makeDf
36d00 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 66 lt flag set. If
36d10 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 56 two different V
36d20 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a 2a FSes with the.**
36d30 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 same name are r
36d40 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 62 egistered, the b
36d50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
36d60 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 ined. If a.** V
36d70 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 FS is registered
36d80 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 61 with a name tha
36d90 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 t is NULL or an
36da0 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a empty string,.**
36db0 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
36dc0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
36dd0 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 .**.** Unregiste
36de0 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 r a VFS with the
36df0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 sqlite3_vfs_unr
36e00 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 66 egister() interf
36e10 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 ace..** If the d
36e20 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 6e efault VFS is un
36e30 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 registered, anot
36e40 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 65 her VFS is chose
36e50 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 n as.** the defa
36e60 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 65 ult. The choice
36e70 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 53 for the new VFS
36e80 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a 2a is arbitrary..*
36e90 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
36ea0 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b s:.** [H11203] [
36eb0 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 5d H11206] [H11209]
36ec0 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 31 [H11212] [H1121
36ed0 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 5] [H11218].*/.S
36ee0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
36ef0 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 3_vfs *sqlite3_v
36f00 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 fs_find(const ch
36f10 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 ar *zVfsName);.S
36f20 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
36f30 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
36f40 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c er(sqlite3_vfs*,
36f50 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a int makeDflt);.
36f60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
36f70 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
36f80 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 ister(sqlite3_vf
36f90 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 s*);../*.** CAPI
36fa0 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 3REF: Mutexes {H
36fb0 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17000} <S20000>.
36fc0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
36fd0 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65 core uses these
36fe0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68 routines for th
36ff0 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e read.** synchron
37000 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 ization. Though
37010 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65 they are intende
37020 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a d for internal.*
37030 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c * use by SQLite,
37040 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 code that links
37050 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20 against SQLite
37060 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 is.** permitted
37070 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 to use any of th
37080 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ese routines..**
37090 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73 .** The SQLite s
370a0 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 ource code conta
370b0 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 ins multiple imp
370c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
370d0 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72 of these mutex r
370e0 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 outines. An app
370f0 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 ropriate impleme
37100 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 ntation.** is se
37110 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63 lected automatic
37120 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d ally at compile-
37130 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f time. The follo
37140 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e wing.** implemen
37150 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 tations are avai
37160 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c lable in the SQL
37170 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 ite core:.**.**
37180 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 <ul>.** <li> S
37190 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a QLITE_MUTEX_OS2.
371a0 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 ** <li> SQLITE
371b0 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a _MUTEX_PTHREAD.*
371c0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
371d0 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 MUTEX_W32.** <li
371e0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 > SQLITE_MUTEX
371f0 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a _NOOP.** </ul>.*
37200 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
37210 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 MUTEX_NOOP imple
37220 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73 mentation is a s
37230 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a et of routines.*
37240 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 * that does no r
37250 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 eal locking and
37260 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 is appropriate f
37270 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 or use in.** a s
37280 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 ingle-threaded a
37290 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 pplication. The
372a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 SQLITE_MUTEX_OS
372b0 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 2,.** SQLITE_MUT
372c0 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 EX_PTHREAD, and
372d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
372e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
372f0 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 .** are appropri
37300 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f ate for use on O
37310 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 S/2, Unix, and W
37320 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 indows..**.** If
37330 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
37340 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c led with the SQL
37350 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 ITE_MUTEX_APPDEF
37360 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a preprocessor.**
37370 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 macro defined (
37380 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d with "-DSQLITE_M
37390 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c UTEX_APPDEF=1"),
373a0 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a then no mutex.*
373b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
373c0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 is included wit
373d0 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 h the library. I
373e0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a n this case the.
373f0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d ** application m
37400 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 ust supply a cus
37410 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d tom mutex implem
37420 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 entation using t
37430 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f he.** [SQLITE_CO
37440 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 NFIG_MUTEX] opti
37450 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
37460 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 3_config() funct
37470 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ion.** before ca
37480 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
37490 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e itialize() or an
374a0 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73 y other public s
374b0 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 qlite3_.** funct
374c0 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73 ion that calls s
374d0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
374e0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 e()..**.** {H170
374f0 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 11} The sqlite3_
37500 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
37510 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
37520 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
37530 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
37540 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 nter to it. {H17
37550 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72 012} If it retur
37560 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
37570 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
37580 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
37590 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 allocated. {H170
375a0 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 13} SQLite.** wi
375b0 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
375c0 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
375d0 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 n error. {H17014
375e0 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a } The argument.*
375f0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
37600 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
37610 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
37620 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
37630 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
37640 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
37650 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
37660 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
37670 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
37680 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
37690 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
376a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
376b0 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
376c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
376d0 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
376e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
376f0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
37700 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
37710 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
37720 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
37730 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a TEX_STATIC_LRU2.
37740 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b ** </ul>.**.** {
37750 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73 H17015} The firs
37760 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
37770 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
37780 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
37790 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
377a0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
377b0 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
377c0 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
377d0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
377e0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
377f0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
37800 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
37810 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20 X_FAST is used.
37820 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 {END}.** The mut
37830 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
37840 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
37850 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
37860 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
37870 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
37880 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
37890 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
378a0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
378b0 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 want to. {H1701
378c0 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6} But SQLite wi
378d0 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
378e0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
378f0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
37900 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
37910 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 eds one. {END}
37920 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
37930 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
37940 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
37950 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
37960 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
37970 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
37980 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
37990 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
379a0 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
379b0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
379c0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 X_FAST..**.** {H
379d0 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72 17017} The other
379e0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 allowed paramet
379f0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ers to sqlite3_m
37a00 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 utex_alloc() eac
37a10 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f h return.** a po
37a20 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
37a30 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 c preexisting mu
37a40 74 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72 tex. {END} Four
37a50 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
37a60 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
37a70 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
37a80 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
37a90 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
37aa0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
37ab0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
37ac0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
37ad0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
37ae0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
37af0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
37b00 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
37b10 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
37b20 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
37b30 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
37b40 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
37b50 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
37b60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
37b70 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
37b80 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
37b90 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e **.** {H17018} N
37ba0 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 ote that if one
37bb0 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d of the dynamic m
37bc0 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 utex parameters
37bd0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 (SQLITE_MUTEX_FA
37be0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ST.** or SQLITE_
37bf0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 MUTEX_RECURSIVE)
37c00 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 is used then sq
37c10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
37c20 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 c().** returns a
37c30 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 different mutex
37c40 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 on every call.
37c50 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f {H17034} But fo
37c60 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
37c70 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
37c80 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
37c90 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
37ca0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
37cb0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
37cc0 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 number..**.** {H
37cd0 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 17019} The sqlit
37ce0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 e3_mutex_free()
37cf0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
37d00 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
37d10 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 .** allocated dy
37d20 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 namic mutex. {H1
37d30 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 7020} SQLite is
37d40 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
37d50 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 ocate every.** d
37d60 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 ynamic mutex tha
37d70 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 t it allocates.
37d80 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e {A17021} The dyn
37d90 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 amic mutexes mus
37da0 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 t not be in.** u
37db0 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 se when they are
37dc0 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 deallocated. {A
37dd0 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 17022} Attemptin
37de0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 g to deallocate
37df0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 a static.** mute
37e00 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 x results in und
37e10 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
37e20 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 {H17023} SQLite
37e30 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 never deallocat
37e40 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d es.** a static m
37e50 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a utex. {END}.**.*
37e60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
37e70 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
37e80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
37e90 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
37ea0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
37eb0 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 a mutex. {H1702
37ec0 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 4} If another th
37ed0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
37ee0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
37ef0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
37f00 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
37f10 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
37f20 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
37f30 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
37f40 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 ITE_BUSY. {H1702
37f50 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 5} The sqlite3_
37f60 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
37f70 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 rface returns [S
37f80 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f QLITE_OK].** upo
37f90 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 n successful ent
37fa0 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 ry. {H17026} Mu
37fb0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
37fc0 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 ing.** SQLITE_MU
37fd0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
37fe0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c n be entered mul
37ff0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
38000 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a he same thread..
38010 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 ** {H17027} In s
38020 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
38030 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
38040 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
38050 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
38060 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
38070 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
38080 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 er. {A17028} If
38090 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
380a0 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
380b0 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e any other.** kin
380c0 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 d of mutex more
380d0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
380e0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
380f0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 ined..** {H17029
38100 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 } SQLite will ne
38110 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 ver exhibit.** s
38120 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 uch behavior in
38130 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d its own use of m
38140 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f utexes..**.** So
38150 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 me systems (for
38160 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 example, Windows
38170 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 95) do not supp
38180 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f ort the operatio
38190 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 n.** implemented
381a0 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 by sqlite3_mute
381b0 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f x_try(). On tho
381c0 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 se systems, sqli
381d0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a te3_mutex_try().
381e0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 ** will always r
381f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
38200 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 Y. {H17030} The
38210 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c SQLite core onl
38220 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 y ever uses.** s
38230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
38240 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a () as an optimiz
38250 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 ation so this is
38260 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 acceptable beha
38270 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 vior..**.** {H17
38280 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 031} The sqlite3
38290 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
382a0 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
382b0 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a utex that was.**
382c0 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 previously ente
382d0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
382e0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 thread. {A17032
382f0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a } The behavior.*
38300 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
38310 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
38320 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
38330 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ered by the.** c
38340 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 alling thread or
38350 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
38360 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 y allocated. {H
38370 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 17033} SQLite wi
38380 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 ll.** never do e
38390 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ither. {END}.**.
383a0 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 ** If the argume
383b0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
383c0 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c tex_enter(), sql
383d0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
383e0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f , or.** sqlite3_
383f0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 mutex_leave() is
38400 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
38410 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 then all three
38420 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 routines.** beha
38430 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a ve as no-ops..**
38440 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
38450 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
38460 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 d()] and [sqlite
38470 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
38480 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
38490 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
384a0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
384b0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
384c0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
384d0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
384e0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
384f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
38500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
38510 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
38520 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
38530 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
38540 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 _try(sqlite3_mut
38550 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
38560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
38570 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
38580 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
38590 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
385a0 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 x Methods Object
385b0 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 {H17120} <S2013
385c0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
385d0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
385e0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
385f0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 ucture defines t
38600 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 he low-level rou
38610 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f tines.** used to
38620 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 allocate and us
38630 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a e mutexes..**.**
38640 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 Usually, the de
38650 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c fault mutex impl
38660 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 ementations prov
38670 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 ided by SQLite a
38680 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 re.** sufficient
38690 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 , however the us
386a0 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f er has the optio
386b0 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e n of substitutin
386c0 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d g a custom.** im
386d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
386e0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 specialized dep
386f0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 loyments or syst
38700 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 ems for which SQ
38710 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 Lite.** does not
38720 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 provide a suita
38730 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
38740 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
38750 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 , the user.** cr
38760 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 eates and popula
38770 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 tes an instance
38780 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
38790 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 e to pass.** to
387a0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
387b0 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 along with the
387c0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
387d0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
387e0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 Additionally, a
387f0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
38800 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
38810 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a be used as an.*
38820 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c * output variabl
38830 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 e when querying
38840 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 the system for t
38850 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 he current mutex
38860 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
38870 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 on, using the [S
38880 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
38890 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a MUTEX] option..*
388a0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 *.** The xMutexI
388b0 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e nit method defin
388c0 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 ed by this struc
388d0 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 ture is invoked
388e0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 as.** part of sy
388f0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 stem initializat
38900 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 ion by the sqlit
38910 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
38920 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 function..** {H1
38930 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 7001} The xMutex
38940 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 Init routine sha
38950 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 ll be called by
38960 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 SQLite once for
38970 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 each.** effectiv
38980 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
38990 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d e3_initialize()]
389a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 ..**.** The xMut
389b0 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 exEnd method def
389c0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
389d0 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 ucture is invoke
389e0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
389f0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 system shutdown
38a00 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 by the sqlite3_s
38a10 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 hutdown() functi
38a20 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 on. The.** imple
38a30 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 mentation of thi
38a40 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 s method is expe
38a50 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 cted to release
38a60 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a all outstanding.
38a70 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 ** resources obt
38a80 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 ained by the mut
38a90 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 ex methods imple
38aa0 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 mentation, espec
38ab0 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f ially.** those o
38ac0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 btained by the x
38ad0 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 MutexInit method
38ae0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 . {H17003} The x
38af0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e MutexEnd().** in
38b00 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 terface shall be
38b10 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
38b20 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b r each call to [
38b30 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
38b40 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ()]..**.** The r
38b50 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d emaining seven m
38b60 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 ethods defined b
38b70 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
38b80 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a (xMutexAlloc,.*
38b90 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d * xMutexFree, xM
38ba0 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 utexEnter, xMute
38bb0 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 xTry, xMutexLeav
38bc0 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e e, xMutexHeld an
38bd0 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 d.** xMutexNothe
38be0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 ld) implement th
38bf0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 e following inte
38c00 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 rfaces (respecti
38c10 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c vely):.**.** <ul
38c20 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
38c30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
38c40 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 c()] </li>.**
38c50 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
38c60 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c utex_free()] </l
38c70 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
38c80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
38c90 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 er()] </li>.**
38ca0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
38cb0 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c mutex_try()] </l
38cc0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
38cd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
38ce0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 ve()] </li>.**
38cf0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
38d00 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f mutex_held()] </
38d10 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
38d20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
38d30 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a theld()] </li>.*
38d40 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
38d50 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 e only differenc
38d60 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 e is that the pu
38d70 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 blic sqlite3_XXX
38d80 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 functions enume
38d90 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 rated.** above s
38da0 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 ilently ignore a
38db0 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 ny invocations t
38dc0 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 hat pass a NULL
38dd0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a pointer instead.
38de0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ** of a valid mu
38df0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 tex handle. The
38e00 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
38e10 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 of the methods d
38e20 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 efined.** by thi
38e30 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
38e40 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
38e50 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 handle this case
38e60 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a , the results.**
38e70 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 of passing a NU
38e80 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 LL pointer inste
38e90 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ad of a valid mu
38ea0 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 tex handle are u
38eb0 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 ndefined.** (i.e
38ec0 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 . it is acceptab
38ed0 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e le to provide an
38ee0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
38ef0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 that segfaults i
38f00 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 f.** it is passe
38f10 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 d a NULL pointer
38f20 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 )..*/.typedef st
38f30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 ruct sqlite3_mut
38f40 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 ex_methods sqlit
38f50 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
38f60 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
38f70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b _mutex_methods {
38f80 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49 . int (*xMutexI
38f90 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e nit)(void);. in
38fa0 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76 t (*xMutexEnd)(v
38fb0 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f oid);. sqlite3_
38fc0 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41 mutex *(*xMutexA
38fd0 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f lloc)(int);. vo
38fe0 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29 id (*xMutexFree)
38ff0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
39000 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 );. void (*xMut
39010 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33 exEnter)(sqlite3
39020 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 _mutex *);. int
39030 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71 (*xMutexTry)(sq
39040 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a lite3_mutex *);.
39050 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c void (*xMutexL
39060 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 eave)(sqlite3_mu
39070 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a tex *);. int (*
39080 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69 xMutexHeld)(sqli
39090 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 te3_mutex *);.
390a0 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68 int (*xMutexNoth
390b0 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 eld)(sqlite3_mut
390c0 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ex *);.};../*.**
390d0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
390e0 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f Verification Ro
390f0 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20 utines {H17080}
39100 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30 <S20130> <S30800
39110 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
39120 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
39130 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
39140 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
39150 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 tines.** are int
39160 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
39170 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
39180 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38 atements. {H1708
39190 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 1} The SQLite co
391a0 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73 re.** never uses
391b0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
391c0 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e except inside an
391d0 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70 assert() and ap
391e0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 plications.** ar
391f0 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c e advised to fol
39200 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20 low the lead of
39210 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30 the core. {H170
39220 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c 82} The core onl
39230 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d y.** provides im
39240 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
39250 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 r these routines
39260 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 when it is comp
39270 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 iled.** with the
39280 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c SQLITE_DEBUG fl
39290 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78 ag. {A17087} Ex
392a0 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70 ternal mutex imp
392b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
392c0 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 are only require
392d0 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 d to provide the
392e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53 se routines if S
392f0 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a QLITE_DEBUG is.*
39300 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66 * defined and if
39310 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 NDEBUG is not d
39320 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 efined..**.** {H
39330 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75 17083} These rou
39340 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74 tines should ret
39350 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
39360 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61 mutex in their a
39370 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65 rgument.** is he
39380 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20 ld or not held,
39390 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79 respectively, by
393a0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 the calling thr
393b0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 ead..**.** {X170
393c0 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 84} The implemen
393d0 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 tation is not re
393e0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 quired to provid
393f0 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 ed versions of t
39400 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 hese.** routines
39410 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77 that actually w
39420 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c ork. If the impl
39430 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
39440 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b not provide work
39450 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 ing.** versions
39460 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
39470 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 s, it should at
39480 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 least provide st
39490 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a ubs that always.
394a0 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73 ** return true s
394b0 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20 o that one does
394c0 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73 not get spurious
394d0 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75 assertion failu
394e0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 res..**.** {H170
394f0 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d 85} If the argum
39500 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ent to sqlite3_m
39510 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61 utex_held() is a
39520 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 NULL pointer th
39530 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e en.** the routin
39540 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 e should return
39550 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73 1. {END} This s
39560 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74 eems counter-int
39570 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20 uitive since.**
39580 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65 clearly the mute
39590 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64 x cannot be held
395a0 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 if it does not
395b0 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a exist. But the.
395c0 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 ** the reason th
395d0 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74 e mutex does not
395e0 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73 exist is becaus
395f0 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e e the build is n
39600 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65 ot.** using mute
39610 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20 xes. And we do
39620 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73 not want the ass
39630 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 ert() containing
39640 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 the.** call to
39650 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
39660 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f ld() to fail, so
39670 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 a non-zero retu
39680 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70 rn is.** the app
39690 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74 ropriate thing t
396a0 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20 o do. {H17086}
396b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
396c0 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 x_notheld().** i
396d0 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 nterface should
396e0 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 also return 1 wh
396f0 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 en given a NULL
39700 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 pointer..*/.SQLI
39710 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
39720 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 e3_mutex_held(sq
39730 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
39740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
39750 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
39760 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
39770 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 x*);../*.** CAPI
39780 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65 3REF: Mutex Type
39790 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30 s {H17001} <H170
397a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 00>.**.** The [s
397b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
397c0 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 oc()] interface
397d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
397e0 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68 rgument.** which
397f0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 is one of these
39800 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
39810 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 ts..**.** The se
39820 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65 t of static mute
39830 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66 xes may change f
39840 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72 rom one SQLite r
39850 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a elease to the.**
39860 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74 next. Applicat
39870 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69 ions that overri
39880 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 de the built-in
39890 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74 mutex logic must
398a0 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 be.** prepared
398b0 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61 to accommodate a
398c0 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
398d0 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 mutexes..*/.#de
398e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
398f0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 X_FAST
39900 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
39910 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
39920 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65 IVE 1.#de
39930 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
39940 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
39950 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
39960 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
39970 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a _MEM 3 /*
39980 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
39990 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ) */.#define SQL
399a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
399b0 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a _MEM2 4 /*
399c0 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64 65 NOT USED */.#de
399d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
399e0 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 20 20 20 X_STATIC_OPEN
399f0 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 4 /* sqlite3
39a00 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f 0a 23 BtreeOpen() */.#
39a10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
39a20 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 TEX_STATIC_PRNG
39a30 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
39a40 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 e3_random() */.#
39a50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
39a60 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 20 TEX_STATIC_LRU
39a70 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 75 20 70 6 /* lru p
39a80 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 age list */.#def
39a90 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
39aa0 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 20 20 20 _STATIC_LRU2
39ab0 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 7 /* lru page
39ac0 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 list */../*.**
39ad0 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 CAPI3REF: Retrie
39ae0 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f 72 ve the mutex for
39af0 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a database conn
39b00 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20 ection {H17002}
39b10 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H17000>.**.** T
39b20 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 his interface re
39b30 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
39b40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 the [sqlite3_mut
39b50 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 ex] object that
39b60 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61 .** serializes a
39b70 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61 ccess to the [da
39b80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
39b90 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 n] given in the
39ba0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e argument.** when
39bb0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 the [threading
39bc0 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69 mode] is Seriali
39bd0 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b zed..** If the [
39be0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
39bf0 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 is Single-thread
39c00 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 or Multi-thread
39c10 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f then this.** ro
39c20 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 utine returns a
39c30 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
39c40 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
39c50 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
39c60 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 e3_db_mutex(sqli
39c70 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
39c80 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 PI3REF: Low-Leve
39c90 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74 l Control Of Dat
39ca0 61 62 61 73 65 20 46 69 6c 65 73 20 7b 48 31 31 abase Files {H11
39cb0 33 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 300} <S30800>.**
39cc0 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 54 68 65 .** {H11301} The
39cd0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 [sqlite3_file_c
39ce0 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 ontrol()] interf
39cf0 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72 65 ace makes a dire
39d00 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a ct call to the.*
39d10 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d * xFileControl m
39d20 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 ethod for the [s
39d30 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
39d40 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 s] object associ
39d50 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 ated.** with a p
39d60 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
39d70 73 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 se identified by
39d80 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
39d90 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20 54 ment. {H11302} T
39da0 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 he.** name of th
39db0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 e database is th
39dc0 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 e name assigned
39dd0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
39de0 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 by the.** <a hre
39df0 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 f="lang_attach.h
39e00 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 tml">ATTACH</a>
39e10 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 SQL command that
39e20 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 opened the.** d
39e30 61 74 61 62 61 73 65 2e 20 7b 48 31 31 33 30 33 atabase. {H11303
39e40 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 } To control the
39e50 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
39e60 69 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61 6d ile, use the nam
39e70 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 e "main".** or a
39e80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b NULL pointer. {
39e90 48 31 31 33 30 34 7d 20 54 68 65 20 74 68 69 72 H11304} The thir
39ea0 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 d and fourth par
39eb0 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 ameters to this
39ec0 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 routine.** are p
39ed0 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 assed directly t
39ee0 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 hrough to the se
39ef0 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70 cond and third p
39f00 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 arameters of.**
39f10 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c the xFileControl
39f20 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 31 33 30 method. {H1130
39f30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 5} The return va
39f40 6c 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65 lue of the xFile
39f50 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f Control.** metho
39f60 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 d becomes the re
39f70 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
39f80 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a is routine..**.*
39f90 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 20 74 68 * {H11306} If th
39fa0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
39fb0 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 er (zDbName) doe
39fc0 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 s not match the
39fd0 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f name of any.** o
39fe0 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c pen database fil
39ff0 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 e, then SQLITE_E
3a000 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 RROR is returned
3a010 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 69 73 20 . {H11307} This
3a020 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
3a030 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20 not remembered
3a040 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 and will not be
3a050 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c recalled by [sql
3a060 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a ite3_errcode()].
3a070 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 ** or [sqlite3_e
3a080 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33 30 rrmsg()]. {A1130
3a090 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 8} The underlyin
3a0a0 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d g xFileControl m
3a0b0 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 ethod might.** a
3a0c0 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 lso return SQLIT
3a0d0 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 31 33 30 E_ERROR. {A1130
3a0e0 39 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 9} There is no w
3a0f0 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 ay to distinguis
3a100 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 h between.** an
3a110 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d incorrect zDbNam
3a120 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f e and an SQLITE_
3a130 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f ERROR return fro
3a140 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 m the underlying
3a150 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c .** xFileControl
3a160 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a method. {END}.*
3a170 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
3a180 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
3a190 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 KSTATE].*/.SQLIT
3a1a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3a1b0 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 3_file_control(s
3a1c0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
3a1d0 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e har *zDbName, in
3a1e0 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f t op, void*);../
3a1f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
3a200 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 esting Interface
3a210 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 30 38 30 {H11400} <S3080
3a220 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
3a230 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
3a240 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 l() interface is
3a250 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 75 used to read ou
3a260 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 t internal.** st
3a270 61 74 65 20 6f 66 20 53 51 4c 69 74 65 20 61 6e ate of SQLite an
3a280 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c d to inject faul
3a290 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 66 ts into SQLite f
3a2a0 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 or testing.** pu
3a2b0 72 70 6f 73 65 73 2e 20 20 54 68 65 20 66 69 72 rposes. The fir
3a2c0 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 st parameter is
3a2d0 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 an operation cod
3a2e0 65 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 e that determine
3a2f0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c s.** the number,
3a300 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 meaning, and op
3a310 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 eration of all s
3a320 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 ubsequent parame
3a330 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ters..**.** This
3a340 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f interface is no
3a350 74 20 66 6f 72 20 75 73 65 20 62 79 20 61 70 70 t for use by app
3a360 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74 20 65 lications. It e
3a370 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 xists solely.**
3a380 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20 74 68 for verifying th
3a390 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
3a3a0 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 ion of the SQLit
3a3b0 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65 70 65 e library. Depe
3a3c0 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 nding.** on how
3a3d0 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
3a3e0 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 ry is compiled,
3a3f0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6d this interface m
3a400 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a ight not exist..
3a410 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c **.** The detail
3a420 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 s of the operati
3a430 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72 20 on codes, their
3a440 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 meanings, the pa
3a450 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65 79 rameters.** they
3a460 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 20 take, and what
3a470 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c 20 they do are all
3a480 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
3a490 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 e without notice
3a4a0 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 ..** Unlike most
3a4b0 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 of the SQLite A
3a4c0 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f PI, this functio
3a4d0 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 n is not guarant
3a4e0 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 eed to.** operat
3a4f0 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66 e consistently f
3a500 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 rom one release
3a510 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a to the next..*/.
3a520 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3a530 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 qlite3_test_cont
3a540 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 rol(int op, ...)
3a550 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3a560 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 F: Testing Inter
3a570 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e 20 43 face Operation C
3a580 6f 64 65 73 20 7b 48 31 31 34 31 30 7d 20 3c 48 odes {H11410} <H
3a590 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 11400>.**.** The
3a5a0 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 se constants are
3a5b0 20 74 68 65 20 76 61 6c 69 64 20 6f 70 65 72 61 the valid opera
3a5c0 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d 65 tion code parame
3a5d0 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 ters used.** as
3a5e0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
3a5f0 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 nt to [sqlite3_t
3a600 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a est_control()]..
3a610 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 61 72 61 **.** These para
3a620 6d 65 74 65 72 73 20 61 6e 64 20 74 68 65 69 72 meters and their
3a630 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 20 73 75 meanings are su
3a640 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 0a bject to change.
3a650 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 ** without notic
3a660 65 2e 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 e. These values
3a670 20 61 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 are for testing
3a680 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a purposes only..
3a690 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 ** Applications
3a6a0 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 61 should not use a
3a6b0 6e 79 20 6f 66 20 74 68 65 73 65 20 70 61 72 61 ny of these para
3a6c0 6d 65 74 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a meters or the.**
3a6d0 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 [sqlite3_test_c
3a6e0 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 ontrol()] interf
3a6f0 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ace..*/.#define
3a700 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3a710 50 52 4e 47 5f 53 41 56 45 20 20 20 20 20 20 20 PRNG_SAVE
3a720 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
3a730 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
3a740 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 20 RL_PRNG_RESTORE
3a750 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
3a760 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3a770 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 TCTRL_PRNG_RESET
3a780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
3a790 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3a7a0 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f TESTCTRL_BITVEC_
3a7b0 54 45 53 54 20 20 20 20 20 20 20 20 20 20 20 20 TEST
3a7c0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8.#define SQLI
3a7d0 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c TE_TESTCTRL_FAUL
3a7e0 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 20 20 20 T_INSTALL
3a7f0 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53 9.#define S
3a800 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 QLITE_TESTCTRL_B
3a810 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f ENIGN_MALLOC_HOO
3a820 4b 53 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e KS 10.#defin
3a830 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
3a840 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 L_PENDING_BYTE
3a850 20 20 20 20 20 20 20 20 20 20 31 31 0a 0a 2f 2a 11../*
3a860 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
3a870 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 Lite Runtime Sta
3a880 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 tus {H17200} <S6
3a890 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0200>.** EXPERIM
3a8a0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
3a8b0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
3a8c0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 ed to retrieve r
3a8d0 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e untime status in
3a8e0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f formation.** abo
3a8f0 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e ut the preforman
3a900 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e ce of SQLite, an
3a910 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 d optionally to
3a920 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a reset various.**
3a930 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 highwater marks
3a940 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
3a950 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 ument is an inte
3a960 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 ger code for.**
3a970 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72 the specific par
3a980 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 ameter to measur
3a990 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 e. Recognized i
3a9a0 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 nteger codes.**
3a9b0 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 are of the form
3a9c0 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d [SQLITE_STATUS_M
3a9d0 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c EMORY_USED | SQL
3a9e0 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e ITE_STATUS_...].
3a9f0 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 .** The current
3aa00 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 value of the par
3aa10 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e ameter is return
3aa20 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e ed into *pCurren
3aa30 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 t..** The highes
3aa40 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 t recorded value
3aa50 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 is returned in
3aa60 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 *pHighwater. If
3aa70 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 the.** resetFla
3aa80 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
3aa90 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f the highest reco
3aaa0 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 rd value is rese
3aab0 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 t after.** *pHig
3aac0 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65 hwater is writte
3aad0 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 n. Some paramete
3aae0 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 rs do not record
3aaf0 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 the highest.**
3ab00 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 value. For thos
3ab10 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 e parameters.**
3ab20 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
3ab30 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 en into *pHighwa
3ab40 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 ter and the rese
3ab50 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 tFlag is ignored
3ab60 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d ..** Other param
3ab70 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c eters record onl
3ab80 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20 y the highwater
3ab90 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 mark and not the
3aba0 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 current.** valu
3abb0 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 e. For these la
3abc0 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 tter parameters
3abd0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
3abe0 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e en into *pCurren
3abf0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f t..**.** This ro
3ac00 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 utine returns SQ
3ac10 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
3ac20 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 ss and a non-zer
3ac30 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 o.** [error code
3ac40 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a ] on failure..**
3ac50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
3ac60 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62 is threadsafe b
3ac70 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 ut is not atomic
3ac80 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
3ac90 63 61 6e 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 can.** called wh
3aca0 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 ile other thread
3acb0 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 s are running th
3acc0 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72 e same or differ
3acd0 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e ent SQLite.** in
3ace0 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 terfaces. Howev
3acf0 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 er the values re
3ad00 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 turned in *pCurr
3ad10 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 ent and.** *pHig
3ad20 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74 hwater reflect t
3ad30 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c he status of SQL
3ad40 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74 ite at different
3ad50 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a points in time.
3ad60 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73 ** and it is pos
3ad70 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 sible that anoth
3ad80 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20 er thread might
3ad90 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d change the param
3ada0 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 eter.** in betwe
3adb0 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65 en the times whe
3adc0 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20 n *pCurrent and
3add0 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20 *pHighwater are
3ade0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 written..**.** S
3adf0 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
3ae00 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 3_db_status()].*
3ae10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
3ae20 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
3ae30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 int sqlite3_sta
3ae40 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 tus(int op, int
3ae50 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a *pCurrent, int *
3ae60 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 pHighwater, int
3ae70 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a resetFlag);.../*
3ae80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 .** CAPI3REF: St
3ae90 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 atus Parameters
3aea0 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30 {H17250} <H17200
3aeb0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
3aec0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e L.**.** These in
3aed0 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 teger constants
3aee0 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75 designate variou
3aef0 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 s run-time statu
3af00 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 s parameters.**
3af10 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75 that can be retu
3af20 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 rned by [sqlite3
3af30 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a _status()]..**.*
3af40 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
3af50 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
3af60 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 RY_USED</dt>.**
3af70 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3af80 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e er is the curren
3af90 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f t amount of memo
3afa0 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a ry checked out.*
3afb0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
3afc0 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 _malloc()], eith
3afd0 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 er directly or i
3afe0 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a ndirectly. The.
3aff0 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64 ** figure includ
3b000 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f es calls made to
3b010 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
3b020 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69 ()] by the appli
3b030 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e cation.** and in
3b040 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 ternal memory us
3b050 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74 age by the SQLit
3b060 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61 e library. Scra
3b070 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f tch memory.** co
3b080 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c ntrolled by [SQL
3b090 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
3b0a0 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72 CH] and auxiliar
3b0b0 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 y page-cache.**
3b0c0 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 memory controlle
3b0d0 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e d by [SQLITE_CON
3b0e0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69 FIG_PAGECACHE] i
3b0f0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 s not included i
3b100 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65 n.** this parame
3b110 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 ter. The amount
3b120 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 returned is the
3b130 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f sum of the allo
3b140 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 cation.** sizes
3b150 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74 as reported by t
3b160 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 he xSize method
3b170 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f in [sqlite3_mem_
3b180 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a methods].</dd>.*
3b190 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3b1a0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
3b1b0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
3b1c0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3b1d0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
3b1e0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
3b1f0 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
3b200 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 anded to [sqlite
3b210 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 3_malloc()] or [
3b220 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
3b230 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 )] (or their.**
3b240 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c internal equival
3b250 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 ents). Only the
3b260 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
3b270 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 in the.** *pHigh
3b280 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 water parameter
3b290 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 to [sqlite3_stat
3b2a0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 us()] is of inte
3b2b0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 rest. .** The v
3b2c0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 alue written int
3b2d0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 o the *pCurrent
3b2e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 parameter is und
3b2f0 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
3b300 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3b310 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
3b320 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
3b330 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3b340 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
3b350 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20 r of pages used
3b360 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 out of the.** [p
3b370 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 agecache memory
3b380 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 allocator] that
3b390 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75 was configured u
3b3a0 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 sing .** [SQLITE
3b3b0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
3b3c0 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 E]. The.** valu
3b3d0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e e returned is in
3b3e0 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 pages, not in b
3b3f0 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ytes.</dd>.**.**
3b400 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
3b410 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
3b420 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
3b430 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3b440 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
3b450 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
3b460 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c page cache.** al
3b470 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 location which c
3b480 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 ould not be stat
3b490 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 isfied by the [S
3b4a0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
3b4b0 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 ECACHE].** buffe
3b4c0 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 r and where forc
3b4d0 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 ed to overflow t
3b4e0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
3b4f0 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 c()]. The.** re
3b500 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 turned value inc
3b510 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e ludes allocation
3b520 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 s that overflowe
3b530 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a d because they.*
3b540 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 * where too larg
3b550 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72 e (they were lar
3b560 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a ger than the "sz
3b570 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a " parameter to.*
3b580 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
3b590 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64 _PAGECACHE]) and
3b5a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 allocations tha
3b5b0 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 t overflowed bec
3b5c0 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 ause.** no space
3b5d0 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65 was left in the
3b5e0 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 page cache.</dd
3b5f0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3b600 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
3b610 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a CHE_SIZE</dt>.**
3b620 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3b630 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 ter records the
3b640 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 largest memory a
3b650 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
3b660 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b t.** handed to [
3b670 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 pagecache memory
3b680 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e allocator]. On
3b690 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ly the value ret
3b6a0 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 urned in the.**
3b6b0 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 *pHighwater para
3b6c0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
3b6d0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 3_status()] is o
3b6e0 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a f interest. .**
3b6f0 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 The value writt
3b700 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 en into the *pCu
3b710 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 rrent parameter
3b720 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 is undefined.</d
3b730 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3b740 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3b750 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 CH_USED</dt>.**
3b760 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3b770 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
3b780 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
3b790 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66 ions used out of
3b7a0 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 the.** [scratch
3b7b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
3b7c0 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 r] configured us
3b7d0 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 ing.** [SQLITE_C
3b7e0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 ONFIG_SCRATCH].
3b7f0 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
3b800 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 ned is in alloca
3b810 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e tions, not.** in
3b820 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61 bytes. Since a
3b830 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d single thread m
3b840 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 ay only have one
3b850 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 scratch allocat
3b860 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 ion.** outstandi
3b870 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73 ng at time, this
3b880 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 parameter also
3b890 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 reports the numb
3b8a0 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a er of threads.**
3b8b0 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d using scratch m
3b8c0 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d emory at the sam
3b8d0 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a e time.</dd>.**.
3b8e0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3b8f0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
3b900 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RFLOW</dt>.** <d
3b910 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3b920 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
3b930 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
3b940 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a scratch memory.*
3b950 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 * allocation whi
3b960 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ch could not be
3b970 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68 statisfied by th
3b980 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 e [SQLITE_CONFIG
3b990 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 _SCRATCH].** buf
3b9a0 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f fer and where fo
3b9b0 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 rced to overflow
3b9c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
3b9d0 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c loc()]. The val
3b9e0 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ues.** returned
3b9f0 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 include overflow
3ba00 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 s because the re
3ba10 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 quested allocati
3ba20 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 on was too.** la
3ba30 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62 rger (that is, b
3ba40 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 ecause the reque
3ba50 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 sted allocation
3ba60 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 was larger than
3ba70 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 the.** "sz" para
3ba80 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 meter to [SQLITE
3ba90 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
3baa0 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f ) and because no
3bab0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a scratch buffer.
3bac0 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 ** slots were av
3bad0 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 ailable..** </dd
3bae0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3baf0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
3bb00 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c H_SIZE</dt>.** <
3bb10 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3bb20 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 r records the la
3bb30 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c rgest memory all
3bb40 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a ocation request.
3bb50 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 ** handed to [sc
3bb60 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c ratch memory all
3bb70 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 ocator]. Only t
3bb80 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
3bb90 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 d in the.** *pHi
3bba0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 ghwater paramete
3bbb0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 r to [sqlite3_st
3bbc0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e atus()] is of in
3bbd0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 terest. .** The
3bbe0 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 value written i
3bbf0 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e nto the *pCurren
3bc00 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 t parameter is u
3bc10 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
3bc20 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3bc30 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 STATUS_PARSER_ST
3bc40 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e ACK</dt>.** <dd>
3bc50 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3bc60 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65 ecords the deepe
3bc70 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e st parser stack.
3bc80 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 It is only.**
3bc90 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 meaningful if SQ
3bca0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
3bcb0 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 with [YYTRACKMA
3bcc0 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 XSTACKDEPTH].</d
3bcd0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a d>.** </dl>.**.*
3bce0 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72 * New status par
3bcf0 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61 ameters may be a
3bd00 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 dded from time t
3bd10 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 o time..*/.#defi
3bd20 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
3bd30 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 _MEMORY_USED
3bd40 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
3bd50 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
3bd60 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20 GECACHE_USED
3bd70 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
3bd80 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
3bd90 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 ACHE_OVERFLOW
3bda0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
3bdb0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
3bdc0 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23 USED 3.#
3bdd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3bde0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
3bdf0 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66 RFLOW 4.#def
3be00 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3be10 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 S_MALLOC_SIZE
3be20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
3be30 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
3be40 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20 ARSER_STACK
3be50 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 6.#define SQ
3be60 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3be70 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20 CACHE_SIZE
3be80 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 7.#define SQLIT
3be90 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
3bea0 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a _SIZE 8.
3beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3bec0 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 Database Connec
3bed0 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37 tion Status {H17
3bee0 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 500} <S60200>.**
3bef0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3bf00 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
3bf10 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ce is used to re
3bf20 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73 trieve runtime s
3bf30 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f tatus informatio
3bf40 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 n .** about a si
3bf50 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63 ngle [database c
3bf60 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 onnection]. The
3bf70 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
3bf80 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 is the.** databa
3bf90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 se connection ob
3bfa0 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72 ject to be inter
3bfb0 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65 rogated. The se
3bfc0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
3bfd0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 is the paramete
3bfe0 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 r to interrogate
3bff0 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 . Currently, th
3c000 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 e only allowed v
3c010 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 alue.** for the
3c020 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
3c030 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 is [SQLITE_DBST
3c040 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 ATUS_LOOKASIDE_U
3c050 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f SED]..** Additio
3c060 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c nal options will
3c070 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69 likely appear i
3c080 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
3c090 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
3c0a0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 ** The current v
3c0b0 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75 alue of the requ
3c0c0 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20 ested parameter
3c0d0 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
3c0e0 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 *pCur.** and the
3c0f0 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74 highest instant
3c100 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20 aneous value is
3c110 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 written into *pH
3c120 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 iwtr. If.** the
3c130 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75 resetFlg is tru
3c140 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68 e, then the high
3c150 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 est instantaneou
3c160 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 s value is.** re
3c170 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f set back down to
3c180 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c the current val
3c190 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ue..**.** See al
3c1a0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 so: [sqlite3_sta
3c1b0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 tus()] and [sqli
3c1c0 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 te3_stmt_status(
3c1d0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
3c1e0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
3c1f0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
3c200 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69 3_db_status(sqli
3c210 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e te3*, int op, in
3c220 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 t *pCur, int *pH
3c230 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46 iwtr, int resetF
3c240 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lg);../*.** CAPI
3c250 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 3REF: Status Par
3c260 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61 ameters for data
3c270 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
3c280 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30 {H17520} <H1750
3c290 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3c2a0 41 4c 0a 2a 2a 0a 2a 2a 20 53 74 61 74 75 73 20 AL.**.** Status
3c2b0 76 65 72 62 73 20 66 6f 72 20 5b 73 71 6c 69 74 verbs for [sqlit
3c2c0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e e3_db_status()].
3c2d0 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c .**.** <dl>.** <
3c2e0 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 dt>SQLITE_DBSTAT
3c2f0 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 US_LOOKASIDE_USE
3c300 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 D</dt>.** <dd>Th
3c310 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
3c320 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
3c330 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d of lookaside mem
3c340 6f 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e ory slots curren
3c350 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f tly.** checked o
3c360 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c ut.</dd>.** </dl
3c370 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c >.*/.#define SQL
3c380 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f ITE_DBSTATUS_LOO
3c390 4b 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 KASIDE_USED
3c3a0 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 0.../*.** CAPI3R
3c3b0 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 EF: Prepared Sta
3c3c0 74 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 tement Status {H
3c3d0 31 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17550} <S60200>.
3c3e0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
3c3f0 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 **.** Each prepa
3c400 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 red statement ma
3c410 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a intains various.
3c420 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 ** [SQLITE_STMTS
3c430 54 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 TATUS_SORT | cou
3c440 6e 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 nters] that meas
3c450 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a ure the number.*
3c460 2a 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 * of times it ha
3c470 73 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 s performed spec
3c480 69 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e ific operations.
3c490 20 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 These counters
3c4a0 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 can.** be used
3c4b0 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 to monitor the p
3c4c0 65 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 erformance chara
3c4d0 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 cteristics of th
3c4e0 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 e prepared.** st
3c4f0 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 atements. For e
3c500 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e xample, if the n
3c510 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 umber of table s
3c520 74 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 teps greatly exc
3c530 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 eeds.** the numb
3c540 65 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 er of table sear
3c550 63 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 ches or result r
3c560 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 ows, that would
3c570 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 tend to indicate
3c580 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 .** that the pre
3c590 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
3c5a0 69 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 is using a full
3c5b0 74 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 table scan rathe
3c5c0 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 r than.** an ind
3c5d0 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ex. .**.** This
3c5e0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
3c5f0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 ed to retrieve a
3c600 6e 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 nd reset counter
3c610 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 values from.**
3c620 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
3c630 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 ement]. The fir
3c640 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 st argument is t
3c650 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
3c660 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 ement.** object
3c670 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 to be interrogat
3c680 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 ed. The second
3c690 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 argument.** is a
3c6a0 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 n integer code f
3c6b0 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 or a specific [S
3c6c0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
3c6d0 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d _SORT | counter]
3c6e0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 .** to be interr
3c6f0 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 ogated. .** The
3c700 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 current value of
3c710 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 the requested c
3c720 6f 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e ounter is return
3c730 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 ed..** If the re
3c740 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 setFlg is true,
3c750 74 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 then the counter
3c760 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 is reset to zer
3c770 6f 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 o after this.**
3c780 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 interface call r
3c790 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 eturns..**.** Se
3c7a0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
3c7b0 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b _status()] and [
3c7c0 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 sqlite3_db_statu
3c7d0 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f s()]..*/.SQLITE_
3c7e0 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
3c7f0 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
3c800 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 te3_stmt_status(
3c810 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
3c820 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 nt op,int resetF
3c830 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lg);../*.** CAPI
3c840 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 3REF: Status Par
3c850 61 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 ameters for prep
3c860 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 ared statements
3c870 7b 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 {H17570} <H17550
3c880 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
3c890 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 L.**.** These pr
3c8a0 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f eprocessor macro
3c8b0 73 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 s define integer
3c8c0 20 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 codes that name
3c8d0 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 counter.** valu
3c8e0 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
3c8f0 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f th the [sqlite3_
3c900 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 stmt_status()] i
3c910 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 nterface..** The
3c920 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 meanings of the
3c930 20 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 various counter
3c940 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 s are as follows
3c950 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 :.**.** <dl>.**
3c960 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 <dt>SQLITE_STMTS
3c970 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 TATUS_FULLSCAN_S
3c980 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TEP</dt>.** <dd>
3c990 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 This is the numb
3c9a0 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
3c9b0 20 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 SQLite has step
3c9c0 70 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a ped forward in.*
3c9d0 2a 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 * a table as par
3c9e0 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c t of a full tabl
3c9f0 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e e scan. Large n
3ca00 75 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 umbers for this
3ca10 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 counter.** may i
3ca20 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e ndicate opportun
3ca30 69 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 ities for perfor
3ca40 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e mance improvemen
3ca50 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 t through .** ca
3ca60 72 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 reful use of ind
3ca70 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ices.</dd>.**.**
3ca80 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 <dt>SQLITE_STMT
3ca90 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e STATUS_SORT</dt>
3caa0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 .** <dd>This is
3cab0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f the number of so
3cac0 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 rt operations th
3cad0 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 at have occurred
3cae0 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 ..** A non-zero
3caf0 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f value in this co
3cb00 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 unter may indica
3cb10 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 te an opportunit
3cb20 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d y to.** improvem
3cb30 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ent performance
3cb40 74 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 through careful
3cb50 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c use of indices.<
3cb60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e /dd>.**.** </dl>
3cb70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
3cb80 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 TE_STMTSTATUS_FU
3cb90 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 LLSCAN_STEP
3cba0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3cbb0 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 _STMTSTATUS_SORT
3cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
3cbd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3cbe0 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 Custom Page Cac
3cbf0 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 he Object.** EXP
3cc00 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
3cc10 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 The sqlite3_pcac
3cc20 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 he type is opaqu
3cc30 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d e. It is implem
3cc40 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 ented by.** the
3cc50 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 pluggable module
3cc60 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f . The SQLite co
3cc70 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 re has no knowle
3cc80 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 dge of.** its si
3cc90 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 ze or internal s
3cca0 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 tructure and nev
3ccb0 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 er deals with th
3ccc0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 e.** sqlite3_pca
3ccd0 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 che object excep
3cce0 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 t by holding and
3ccf0 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 passing pointer
3cd00 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 s.** to the obje
3cd10 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 ct..**.** See [s
3cd20 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
3cd30 74 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 thods] for addit
3cd40 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
3cd50 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
3cd60 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 ruct sqlite3_pca
3cd70 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 che sqlite3_pcac
3cd80 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 he;../*.** CAPI3
3cd90 52 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e REF: Application
3cda0 20 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 Defined Page Ca
3cdb0 63 68 65 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 che..** EXPERIME
3cdc0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
3cdd0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 sqlite3_config](
3cde0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
3cdf0 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 CACHE], ...) int
3ce00 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65 erface can.** re
3ce10 67 69 73 74 65 72 20 61 6e 20 61 6c 74 65 72 6e gister an altern
3ce20 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65 ative page cache
3ce30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3ce40 62 79 20 70 61 73 73 69 6e 67 20 69 6e 20 61 6e by passing in an
3ce50 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 .** instance of
3ce60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
3ce70 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 che_methods stru
3ce80 63 74 75 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72 cture. The major
3ce90 69 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68 ity of the .** h
3cea0 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 eap memory used
3ceb0 62 79 20 73 71 6c 69 74 65 20 69 73 20 75 73 65 by sqlite is use
3cec0 64 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61 d by the page ca
3ced0 63 68 65 20 74 6f 20 63 61 63 68 65 20 64 61 74 che to cache dat
3cee0 61 20 72 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c a read .** from,
3cef0 20 6f 72 20 72 65 61 64 79 20 74 6f 20 62 65 20 or ready to be
3cf00 77 72 69 74 74 65 6e 20 74 6f 2c 20 74 68 65 20 written to, the
3cf10 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 database file. B
3cf20 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61 y implementing a
3cf30 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 67 65 .** custom page
3cf40 20 63 61 63 68 65 20 75 73 69 6e 67 20 74 68 69 cache using thi
3cf50 73 20 41 50 49 2c 20 61 6e 20 61 70 70 6c 69 63 s API, an applic
3cf60 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f ation can contro
3cf70 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69 l more .** preci
3cf80 73 65 6c 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 sely the amount
3cf90 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d of memory consum
3cfa0 65 64 20 62 79 20 73 71 6c 69 74 65 2c 20 74 68 ed by sqlite, th
3cfb0 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 0a e way in which .
3cfc0 2a 2a 20 73 61 69 64 20 6d 65 6d 6f 72 79 20 69 ** said memory i
3cfd0 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
3cfe0 72 65 6c 65 61 73 65 64 2c 20 61 6e 64 20 74 68 released, and th
3cff0 65 20 70 6f 6c 69 63 69 65 73 20 75 73 65 64 20 e policies used
3d000 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 to .** determine
3d010 20 65 78 61 63 74 6c 79 20 77 68 69 63 68 20 70 exactly which p
3d020 61 72 74 73 20 6f 66 20 61 20 64 61 74 61 62 61 arts of a databa
3d030 73 65 20 66 69 6c 65 20 61 72 65 20 63 61 63 68 se file are cach
3d040 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 ed and for .** h
3d050 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ow long..**.** T
3d060 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
3d070 68 65 20 73 74 72 75 63 74 75 72 65 20 61 72 65 he structure are
3d080 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 20 69 6e copied to an in
3d090 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79 ternal buffer by
3d0a0 20 73 71 6c 69 74 65 0a 2a 2a 20 77 69 74 68 69 sqlite.** withi
3d0b0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 n the call to [s
3d0c0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a qlite3_config]..
3d0d0 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28 **.** The xInit(
3d0e0 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c ) method is call
3d0f0 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
3d100 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
3d110 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a 3_initialize()].
3d120 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 ** (usually only
3d130 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65 once during the
3d140 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 lifetime of the
3d150 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73 process). It is
3d160 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70 passed.** a cop
3d170 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 y of the sqlite3
3d180 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e _pcache_methods.
3d190 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63 pArg value. It c
3d1a0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 an be used to se
3d1b0 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73 t.** up global s
3d1c0 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75 tructures and mu
3d1d0 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62 texes required b
3d1e0 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67 y the custom pag
3d1f0 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c e cache .** impl
3d200 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20 ementation. The
3d210 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 xShutdown() meth
3d220 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f od is called fro
3d230 6d 20 77 69 74 68 69 6e 20 0a 2a 2a 20 5b 73 71 m within .** [sq
3d240 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
3d250 5d 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 ], if the applic
3d260 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68 ation invokes th
3d270 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62 is API. It can b
3d280 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 63 6c 65 e used.** to cle
3d290 61 6e 20 75 70 20 61 6e 79 20 6f 75 74 73 74 61 an up any outsta
3d2a0 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73 20 nding resources
3d2b0 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20 73 before process s
3d2c0 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71 75 hutdown, if requ
3d2d0 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ired..**.** The
3d2e0 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 xCreate() method
3d2f0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 is used to cons
3d300 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 truct a new cach
3d310 65 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 0a e instance. The.
3d320 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 ** first paramet
3d330 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74 er, szPage, is t
3d340 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 he size in bytes
3d350 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 68 of the pages th
3d360 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c at must.** be al
3d370 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 63 located by the c
3d380 61 63 68 65 2e 20 73 7a 50 61 67 65 20 77 69 6c ache. szPage wil
3d390 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72 l not be a power
3d3a0 20 6f 66 20 74 77 6f 2e 20 54 68 65 0a 2a 2a 20 of two. The.**
3d3b0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
3d3c0 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 bPurgeable, is
3d3d0 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 true if the cach
3d3e0 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 e being created
3d3f0 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 will.** be used
3d400 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 to cache databas
3d410 65 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f e pages read fro
3d420 6d 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20 m a file stored
3d430 6f 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 on disk, or.** f
3d440 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73 alse if it is us
3d450 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d ed for an in-mem
3d460 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68 ory database. Th
3d470 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
3d480 74 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e tation.** does n
3d490 6f 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e ot have to do an
3d4a0 79 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62 ything special b
3d4b0 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 ased on the valu
3d4c0 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65 2c e of bPurgeable,
3d4d0 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c 79 .** it is purely
3d4e0 20 61 64 76 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a advisory. .**.*
3d4f0 2a 20 54 68 65 20 78 43 61 63 68 65 73 69 7a 65 * The xCachesize
3d500 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65 () method may be
3d510 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 20 74 called at any t
3d520 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20 74 6f ime by SQLite to
3d530 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75 67 67 set the.** sugg
3d540 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20 63 61 ested maximum ca
3d550 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62 65 72 che-size (number
3d560 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 of pages stored
3d570 20 62 79 29 20 74 68 65 20 63 61 63 68 65 0a 2a by) the cache.*
3d580 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73 73 65 * instance passe
3d590 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
3d5a0 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 rgument. This is
3d5b0 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e 66 69 the value confi
3d5c0 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 gured using.** t
3d5d0 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52 41 47 he SQLite "[PRAG
3d5e0 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d 22 20 MA cache_size]"
3d5f0 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69 74 68 command. As with
3d600 20 74 68 65 20 62 50 75 72 67 65 61 62 6c 65 20 the bPurgeable
3d610 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 parameter,.** th
3d620 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
3d630 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
3d640 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 to do anything
3d650 73 70 65 63 69 61 6c 20 77 69 74 68 20 74 68 69 special with thi
3d660 73 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 s.** value, it i
3d670 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e s advisory only.
3d680 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 .**.** The xPage
3d690 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 count() method s
3d6a0 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 hould return the
3d6b0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
3d6c0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 currently.** st
3d6d0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 ored in the cach
3d6e0 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e e supplied as an
3d6f0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a argument..** .*
3d700 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 20 6d * The xFetch() m
3d710 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f ethod is used to
3d720 20 66 65 74 63 68 20 61 20 70 61 67 65 20 61 6e fetch a page an
3d730 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 d return a point
3d740 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 er to it. .** A
3d750 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69 73 20 'page', in this
3d760 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20 62 75 context, is a bu
3d770 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65 20 62 ffer of szPage b
3d780 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61 74 20 ytes aligned at
3d790 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62 6f 75 an.** 8-byte bou
3d7a0 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67 65 20 ndary. The page
3d7b0 74 6f 20 62 65 20 66 65 74 63 68 65 64 20 69 73 to be fetched is
3d7c0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
3d7d0 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a 20 6d he key. The.** m
3d7e0 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c 75 65 imimum key value
3d7f0 20 69 73 20 31 2e 20 41 66 74 65 72 20 69 74 20 is 1. After it
3d800 68 61 73 20 62 65 65 6e 20 72 65 74 72 69 65 76 has been retriev
3d810 65 64 20 75 73 69 6e 67 20 78 46 65 74 63 68 2c ed using xFetch,
3d820 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 69 73 the page .** is
3d830 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
3d840 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 e pinned..**.**
3d850 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
3d860 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 page is already
3d870 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 in the page cac
3d880 68 65 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 he, then a point
3d890 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 63 61 63 er to.** the cac
3d8a0 68 65 64 20 62 75 66 66 65 72 20 73 68 6f 75 6c hed buffer shoul
3d8b0 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 77 69 d be returned wi
3d8c0 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 th its contents
3d8d0 69 6e 74 61 63 74 2e 20 49 66 20 74 68 65 0a 2a intact. If the.*
3d8e0 2a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c * page is not al
3d8f0 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 ready in the cac
3d900 68 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 he, then the exp
3d910 65 63 74 65 64 20 62 65 68 61 76 69 6f 75 72 20 ected behaviour
3d920 6f 66 20 74 68 65 0a 2a 2a 20 63 61 63 68 65 20 of the.** cache
3d930 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
3d940 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
3d950 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 e createFlag par
3d960 61 6d 65 74 65 72 20 70 61 73 73 65 64 0a 2a 2a ameter passed.**
3d970 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f to xFetch, acco
3d980 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c rding to the fol
3d990 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a lowing table:.**
3d9a0 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 .** <table borde
3d9b0 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c r=1 width=85% al
3d9c0 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 20 ign=center>.**
3d9d0 20 3c 74 72 3e 3c 74 68 3e 63 72 65 61 74 65 46 <tr><th>createF
3d9e0 6c 61 67 3c 74 68 3e 45 78 70 65 63 74 65 64 20 lag<th>Expected
3d9f0 42 65 68 61 76 69 6f 75 72 0a 2a 2a 20 20 20 3c Behaviour.** <
3da00 74 72 3e 3c 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c tr><td>0<td>NULL
3da10 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 should be retur
3da20 6e 65 64 2e 20 4e 6f 20 6e 65 77 20 63 61 63 68 ned. No new cach
3da30 65 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74 e entry is creat
3da40 65 64 2e 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 ed..** <tr><td
3da50 3e 31 3c 74 64 3e 49 66 20 63 72 65 61 74 65 46 >1<td>If createF
3da60 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 31 2c lag is set to 1,
3da70 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 this indicates
3da80 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20 that .**
3da90 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 69 SQLite i
3daa0 73 20 68 6f 6c 64 69 6e 67 20 70 69 6e 6e 65 64 s holding pinned
3dab0 20 70 61 67 65 73 20 74 68 61 74 20 63 61 6e 20 pages that can
3dac0 62 65 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20 be unpinned.**
3dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 79 by
3dae0 20 77 72 69 74 69 6e 67 20 74 68 65 69 72 20 63 writing their c
3daf0 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 64 ontents to the d
3db00 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 0a atabase file (a.
3db10 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
3db20 20 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70 relatively exp
3db30 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e ensive operation
3db40 29 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61 ). In this situa
3db50 74 69 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 tion the.**
3db60 20 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65 cache
3db70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3db80 68 61 73 20 74 77 6f 20 63 68 6f 69 63 65 73 3a has two choices:
3db90 20 69 74 20 63 61 6e 20 72 65 74 75 72 6e 20 4e it can return N
3dba0 55 4c 4c 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ULL,.**
3dbb0 20 20 20 20 20 20 20 69 6e 20 77 68 69 63 68 20 in which
3dbc0 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c case SQLite will
3dbd0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69 attempt to unpi
3dbe0 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 0a 2a n one or more .*
3dbf0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
3dc00 20 70 61 67 65 73 20 62 65 66 6f 72 65 20 72 65 pages before re
3dc10 2d 72 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 -requesting the
3dc20 73 61 6d 65 20 70 61 67 65 2c 20 6f 72 20 69 74 same page, or it
3dc30 20 63 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 can.**
3dc40 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 20 allocate
3dc50 61 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 72 a new page and r
3dc60 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
3dc70 74 6f 20 69 74 2e 20 49 66 20 61 20 6e 65 77 0a to it. If a new.
3dc80 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
3dc90 20 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 page is alloca
3dca0 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 69 ted, then the fi
3dcb0 72 73 74 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a rst sizeof(void*
3dcc0 29 20 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 ) bytes of.**
3dcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20 it
3dce0 28 61 74 20 6c 65 61 73 74 29 20 6d 75 73 74 20 (at least) must
3dcf0 62 65 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 be zeroed before
3dd00 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e it is returned.
3dd10 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 32 3c .** <tr><td>2<
3dd20 74 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 td>If createFlag
3dd30 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 74 68 is set to 2, th
3dd40 65 6e 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 en SQLite is not
3dd50 20 68 6f 6c 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 holding any.**
3dd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
3dd70 69 6e 6e 65 64 20 70 61 67 65 73 20 61 73 73 6f inned pages asso
3dd80 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
3dd90 73 70 65 63 69 66 69 63 20 63 61 63 68 65 20 70 specific cache p
3dda0 61 73 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 assed.**
3ddb0 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 66 as the f
3ddc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
3ddd0 20 78 46 65 74 63 68 28 29 20 74 68 61 74 20 63 xFetch() that c
3dde0 61 6e 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 an be unpinned.
3ddf0 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 The.**
3de00 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c cache impl
3de10 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c ementation shoul
3de20 64 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c d attempt to all
3de30 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 ocate a new.**
3de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61 ca
3de50 63 68 65 20 65 6e 74 72 79 20 61 6e 64 20 72 65 che entry and re
3de60 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
3de70 6f 20 69 74 2e 20 41 67 61 69 6e 2c 20 74 68 65 o it. Again, the
3de80 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 first.**
3de90 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 sizeof(
3dea0 76 6f 69 64 2a 29 20 62 79 74 65 73 20 6f 66 20 void*) bytes of
3deb0 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 the page should
3dec0 62 65 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 be zeroed before
3ded0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
3dee0 20 20 20 20 69 74 20 69 73 20 72 65 74 75 72 6e it is return
3def0 65 64 2e 20 49 66 20 74 68 65 20 78 46 65 74 63 ed. If the xFetc
3df00 68 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 72 h() method retur
3df10 6e 73 20 4e 55 4c 4c 20 77 68 65 6e 20 0a 2a 2a ns NULL when .**
3df20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3df30 63 72 65 61 74 65 46 6c 61 67 3d 3d 32 2c 20 53 createFlag==2, S
3df40 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 QLite assumes th
3df50 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f at a memory allo
3df60 63 61 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 cation .**
3df70 20 20 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 failed
3df80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c and returns SQL
3df90 49 54 45 5f 4e 4f 4d 45 4d 20 74 6f 20 74 68 65 ITE_NOMEM to the
3dfa0 20 75 73 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c user..** </tabl
3dfb0 65 3e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 e>.**.** xUnpin(
3dfc0 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 ) is called by S
3dfd0 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f 69 QLite with a poi
3dfe0 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65 6e nter to a curren
3dff0 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65 0a tly pinned page.
3e000 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ** as its second
3e010 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 argument. If th
3e020 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
3e030 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20 6e r, discard, is n
3e040 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e on-zero,.** then
3e050 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 the page should
3e060 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f 6d be evicted from
3e070 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20 74 the cache. In t
3e080 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65 20 his case SQLite
3e090 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 .** assumes that
3e0a0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 the next time t
3e0b0 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72 69 he page is retri
3e0c0 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 eved from the ca
3e0d0 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 che using.** the
3e0e0 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 xFetch() method
3e0f0 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65 72 , it will be zer
3e100 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73 63 oed. If the disc
3e110 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ard parameter is
3e120 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 .** zero, then t
3e130 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69 he page is consi
3e140 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70 69 dered to be unpi
3e150 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65 20 nned. The cache
3e160 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
3e170 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 * may choose to
3e180 72 65 63 6c 61 69 6d 20 28 66 72 65 65 20 6f 72 reclaim (free or
3e190 20 72 65 63 79 63 6c 65 29 20 75 6e 70 69 6e 6e recycle) unpinn
3e1a0 65 64 20 70 61 67 65 73 20 61 74 20 61 6e 79 20 ed pages at any
3e1b0 74 69 6d 65 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 time..** SQLite
3e1c0 61 73 73 75 6d 65 73 20 74 68 61 74 20 6e 65 78 assumes that nex
3e1d0 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20 t time the page
3e1e0 69 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f is retrieved fro
3e1f0 6d 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 m the cache.** i
3e200 74 20 77 69 6c 6c 20 65 69 74 68 65 72 20 62 65 t will either be
3e210 20 7a 65 72 6f 65 64 2c 20 6f 72 20 63 6f 6e 74 zeroed, or cont
3e220 61 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 ain the same dat
3e230 61 20 74 68 61 74 20 69 74 20 64 69 64 20 77 68 a that it did wh
3e240 65 6e 20 69 74 0a 2a 2a 20 77 61 73 20 75 6e 70 en it.** was unp
3e250 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 inned..**.** The
3e260 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 72 65 cache is not re
3e270 71 75 69 72 65 64 20 74 6f 20 70 65 72 66 6f 72 quired to perfor
3e280 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63 65 20 m any reference
3e290 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69 6e 67 counting. A sing
3e2a0 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 le .** call to x
3e2b0 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73 20 74 Unpin() unpins t
3e2c0 68 65 20 70 61 67 65 20 72 65 67 61 72 64 6c 65 he page regardle
3e2d0 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 ss of the number
3e2e0 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 of prior calls
3e2f0 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28 29 2e .** to xFetch().
3e300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65 .**.** The xReke
3e310 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 y() method is us
3e320 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
3e330 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63 key value assoc
3e340 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a iated with the.*
3e350 2a 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73 * page passed as
3e360 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
3e370 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 ment from oldKey
3e380 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66 20 74 to newKey. If t
3e390 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72 65 76 he cache.** prev
3e3a0 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e 73 20 iously contains
3e3b0 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 an entry associa
3e3c0 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 2c ted with newKey,
3e3d0 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a it should be.**
3e3e0 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e 79 20 discarded. Any
3e3f0 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e 74 72 prior cache entr
3e400 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
3e410 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75 61 72 h newKey is guar
3e420 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f anteed not.** to
3e430 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a be pinned..**.*
3e440 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 63 61 * When SQLite ca
3e450 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63 61 74 lls the xTruncat
3e460 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68 65 20 e() method, the
3e470 63 61 63 68 65 20 6d 75 73 74 20 64 69 73 63 61 cache must disca
3e480 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 rd all.** existi
3e490 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 ng cache entries
3e4a0 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 with page numbe
3e4b0 72 73 20 28 6b 65 79 73 29 20 67 72 65 61 74 65 rs (keys) greate
3e4c0 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a r than or equal.
3e4d0 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 ** to the value
3e4e0 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20 70 61 of the iLimit pa
3e4f0 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 rameter passed t
3e500 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e 20 49 o xTruncate(). I
3e510 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 f any.** of thes
3e520 65 20 70 61 67 65 73 20 61 72 65 20 70 69 6e 6e e pages are pinn
3e530 65 64 2c 20 74 68 65 79 20 61 72 65 20 69 6d 70 ed, they are imp
3e540 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 licitly unpinned
3e550 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 0a 2a , meaning that.*
3e560 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20 73 61 * they can be sa
3e570 66 65 6c 79 20 64 69 73 63 61 72 64 65 64 2e 0a fely discarded..
3e580 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73 74 72 **.** The xDestr
3e590 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 oy() method is u
3e5a0 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 sed to delete a
3e5b0 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 cache allocated
3e5c0 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a 2a by xCreate()..**
3e5d0 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 All resources a
3e5e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
3e5f0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 63 he specified cac
3e600 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 he should be fre
3e610 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63 61 6c ed. After.** cal
3e620 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 72 6f ling the xDestro
3e630 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 4c 69 y() method, SQLi
3e640 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 68 65 te considers the
3e650 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 [sqlite3_pcache
3e660 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 76 *].** handle inv
3e670 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c 20 6e alid, and will n
3e680 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 20 61 ot use it with a
3e690 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 ny other sqlite3
3e6a0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 0a _pcache_methods.
3e6b0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f ** functions..*/
3e6c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
3e6d0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
3e6e0 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 70 ethods sqlite3_p
3e6f0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 cache_methods;.s
3e700 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 truct sqlite3_pc
3e710 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 ache_methods {.
3e720 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 void *pArg;. i
3e730 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 nt (*xInit)(void
3e740 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 *);. void (*xSh
3e750 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0a utdown)(void*);.
3e760 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
3e770 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74 *(*xCreate)(int
3e780 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75 szPage, int bPu
3e790 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f 69 64 rgeable);. void
3e7a0 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 28 73 (*xCachesize)(s
3e7b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 qlite3_pcache*,
3e7c0 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 29 3b int nCachesize);
3e7d0 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 63 6f . int (*xPageco
3e7e0 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 63 61 unt)(sqlite3_pca
3e7f0 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 che*);. void *(
3e800 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 65 33 *xFetch)(sqlite3
3e810 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e _pcache*, unsign
3e820 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 65 61 ed key, int crea
3e830 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 64 20 teFlag);. void
3e840 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 74 65 (*xUnpin)(sqlite
3e850 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 3_pcache*, void*
3e860 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 3b 0a , int discard);.
3e870 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 79 29 void (*xRekey)
3e880 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
3e890 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 , void*, unsigne
3e8a0 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 67 6e d oldKey, unsign
3e8b0 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 76 6f ed newKey);. vo
3e8c0 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 id (*xTruncate)(
3e8d0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c sqlite3_pcache*,
3e8e0 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d 69 74 unsigned iLimit
3e8f0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 );. void (*xDes
3e900 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 70 63 troy)(sqlite3_pc
3e910 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a ache*);.};../*.*
3e920 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 * CAPI3REF: Onli
3e930 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a 65 63 74 ne Backup Object
3e940 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
3e950 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
3e960 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 e3_backup object
3e970 20 72 65 63 6f 72 64 73 20 73 74 61 74 65 20 69 records state i
3e980 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
3e990 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f an ongoing.** o
3e9a0 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f 70 65 nline backup ope
3e9b0 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 71 6c ration. The sql
3e9c0 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 ite3_backup obje
3e9d0 63 74 20 69 73 20 63 72 65 61 74 65 64 20 62 79 ct is created by
3e9e0 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 .** a call to [s
3e9f0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
3ea00 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64 65 73 it()] and is des
3ea10 74 72 6f 79 65 64 20 62 79 20 61 20 63 61 6c 6c troyed by a call
3ea20 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
3ea30 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 5d backup_finish()]
3ea40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f ..**.** See Also
3ea50 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c : [Using the SQL
3ea60 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 ite Online Backu
3ea70 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 70 65 64 65 p API].*/.typede
3ea80 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
3ea90 5f 62 61 63 6b 75 70 20 73 71 6c 69 74 65 33 5f _backup sqlite3_
3eaa0 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 backup;../*.** C
3eab0 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 API3REF: Online
3eac0 42 61 63 6b 75 70 20 41 50 49 2e 0a 2a 2a 20 45 Backup API..** E
3ead0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3eae0 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 75 73 * This API is us
3eaf0 65 64 20 74 6f 20 6f 76 65 72 77 72 69 74 65 20 ed to overwrite
3eb00 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
3eb10 6f 6e 65 20 64 61 74 61 62 61 73 65 20 77 69 74 one database wit
3eb20 68 20 74 68 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f h that.** of ano
3eb30 74 68 65 72 2e 20 49 74 20 69 73 20 75 73 65 66 ther. It is usef
3eb40 75 6c 20 65 69 74 68 65 72 20 66 6f 72 20 63 72 ul either for cr
3eb50 65 61 74 69 6e 67 20 62 61 63 6b 75 70 73 20 6f eating backups o
3eb60 66 20 64 61 74 61 62 61 73 65 73 20 6f 72 0a 2a f databases or.*
3eb70 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 20 69 6e * for copying in
3eb80 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
3eb90 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 70 65 72 s to or from per
3eba0 73 69 73 74 65 6e 74 20 66 69 6c 65 73 2e 20 0a sistent files. .
3ebb0 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 **.** See Also:
3ebc0 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 [Using the SQLit
3ebd0 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 e Online Backup
3ebe0 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75 API].**.** Exclu
3ebf0 73 69 76 65 20 61 63 63 65 73 73 20 69 73 20 72 sive access is r
3ec00 65 71 75 69 72 65 64 20 74 6f 20 74 68 65 20 64 equired to the d
3ec10 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
3ec20 61 73 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 ase for the .**
3ec30 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 duration of the
3ec40 6f 70 65 72 61 74 69 6f 6e 2e 20 48 6f 77 65 76 operation. Howev
3ec50 65 72 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 er the source da
3ec60 74 61 62 61 73 65 20 69 73 20 6f 6e 6c 79 0a 2a tabase is only.*
3ec70 2a 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 77 68 * read-locked wh
3ec80 69 6c 65 20 69 74 20 69 73 20 61 63 74 75 61 6c ile it is actual
3ec90 6c 79 20 62 65 69 6e 67 20 72 65 61 64 2c 20 69 ly being read, i
3eca0 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a t is not locked.
3ecb0 2a 2a 20 63 6f 6e 74 69 6e 75 6f 75 73 6c 79 20 ** continuously
3ecc0 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20 6f for the entire o
3ecd0 70 65 72 61 74 69 6f 6e 2e 20 54 68 75 73 2c 20 peration. Thus,
3ece0 74 68 65 20 62 61 63 6b 75 70 20 6d 61 79 20 62 the backup may b
3ecf0 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f e.** performed o
3ed00 6e 20 61 20 6c 69 76 65 20 64 61 74 61 62 61 73 n a live databas
3ed10 65 20 77 69 74 68 6f 75 74 20 70 72 65 76 65 6e e without preven
3ed20 74 69 6e 67 20 6f 74 68 65 72 20 75 73 65 72 73 ting other users
3ed30 20 66 72 6f 6d 0a 2a 2a 20 77 72 69 74 69 6e 67 from.** writing
3ed40 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
3ed50 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64 for an extended
3ed60 20 70 65 72 69 6f 64 20 6f 66 20 74 69 6d 65 2e period of time.
3ed70 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 70 65 72 66 6f .** .** To perfo
3ed80 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 rm a backup oper
3ed90 61 74 69 6f 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c ation: .** <ol
3eda0 3e 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e >.** <li><b>
3edb0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
3edc0 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c nit()</b> is cal
3edd0 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74 led once to init
3ede0 69 61 6c 69 7a 65 20 74 68 65 0a 2a 2a 20 20 20 ialize the.**
3edf0 20 20 20 20 20 20 62 61 63 6b 75 70 2c 20 0a 2a backup, .*
3ee00 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c * <li><b>sql
3ee10 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
3ee20 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 ()</b> is called
3ee30 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d one or more tim
3ee40 65 73 20 74 6f 20 74 72 61 6e 73 66 65 72 20 0a es to transfer .
3ee50 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 64 ** the d
3ee60 61 74 61 20 62 65 74 77 65 65 6e 20 74 68 65 20 ata between the
3ee70 74 77 6f 20 64 61 74 61 62 61 73 65 73 2c 20 61 two databases, a
3ee80 6e 64 20 66 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20 nd finally.**
3ee90 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 <li><b>sqlite3
3eea0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
3eeb0 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 74 </b> is called t
3eec0 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 72 65 o release all re
3eed0 73 6f 75 72 63 65 73 20 0a 2a 2a 20 20 20 20 20 sources .**
3eee0 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 associated w
3eef0 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 6f ith the backup o
3ef00 70 65 72 61 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20 peration. .**
3ef10 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 65 72 65 20 73 </ol>.** There s
3ef20 68 6f 75 6c 64 20 62 65 20 65 78 61 63 74 6c 79 hould be exactly
3ef30 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c one call to sql
3ef40 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
3ef50 73 68 28 29 20 66 6f 72 20 65 61 63 68 0a 2a 2a sh() for each.**
3ef60 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
3ef70 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
3ef80 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a up_init()..**.**
3ef90 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b <b>sqlite3_back
3efa0 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a up_init()</b>.**
3efb0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 .** The first tw
3efc0 6f 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 o arguments pass
3efd0 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ed to [sqlite3_b
3efe0 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 72 ackup_init()] ar
3eff0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a e the database.*
3f000 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 * handle associa
3f010 74 65 64 20 77 69 74 68 20 74 68 65 20 64 65 73 ted with the des
3f020 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
3f030 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 e and the databa
3f040 73 65 20 6e 61 6d 65 20 0a 2a 2a 20 75 73 65 64 se name .** used
3f050 20 74 6f 20 61 74 74 61 63 68 20 74 68 65 20 64 to attach the d
3f060 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
3f070 61 73 65 20 74 6f 20 74 68 65 20 68 61 6e 64 6c ase to the handl
3f080 65 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 e. The database
3f090 6e 61 6d 65 0a 2a 2a 20 69 73 20 22 6d 61 69 6e name.** is "main
3f0a0 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 " for the main d
3f0b0 61 74 61 62 61 73 65 2c 20 22 74 65 6d 70 22 20 atabase, "temp"
3f0c0 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 for the temporar
3f0d0 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 0a 2a y database, or.*
3f0e0 2a 20 74 68 65 20 6e 61 6d 65 20 73 70 65 63 69 * the name speci
3f0f0 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 fied as part of
3f100 74 68 65 20 5b 41 54 54 41 43 48 5d 20 73 74 61 the [ATTACH] sta
3f110 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 64 65 tement if the de
3f120 73 74 69 6e 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 stination is.**
3f130 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 an attached data
3f140 62 61 73 65 2e 20 54 68 65 20 74 68 69 72 64 20 base. The third
3f150 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d and fourth argum
3f160 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 0a ents passed to .
3f170 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
3f180 70 5f 69 6e 69 74 28 29 20 69 64 65 6e 74 69 66 p_init() identif
3f190 79 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 y the [database
3f1a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 connection].** a
3f1b0 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 nd database name
3f1c0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 used.** to acce
3f1d0 73 73 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 ss the source da
3f1e0 74 61 62 61 73 65 2e 20 54 68 65 20 76 61 6c 75 tabase. The valu
3f1f0 65 73 20 70 61 73 73 65 64 20 66 6f 72 20 74 68 es passed for th
3f200 65 20 73 6f 75 72 63 65 20 61 6e 64 20 0a 2a 2a e source and .**
3f210 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 destination [da
3f220 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3f230 6e 5d 20 70 61 72 61 6d 65 74 65 72 73 20 6d 75 n] parameters mu
3f240 73 74 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61 st not be the sa
3f250 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 me..**.** If an
3f260 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 error occurs wit
3f270 68 69 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b hin sqlite3_back
3f280 75 70 5f 69 6e 69 74 28 29 2c 20 74 68 65 6e 20 up_init(), then
3f290 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
3f2a0 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 .** and an error
3f2b0 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
3f2c0 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 message written
3f2d0 69 6e 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 into the [databa
3f2e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a se connection] .
3f2f0 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ** passed as the
3f300 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
3f310 20 54 68 65 79 20 6d 61 79 20 62 65 20 72 65 74 They may be ret
3f320 72 69 65 76 65 64 20 75 73 69 6e 67 20 74 68 65 rieved using the
3f330 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 .** [sqlite3_err
3f340 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74 65 code()], [sqlite
3f350 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 6e 64 3_errmsg()], and
3f360 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
3f370 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e 73 2e 16()] functions.
3f380 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
3f390 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 f successful, a
3f3a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73 pointer to an [s
3f3b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f qlite3_backup] o
3f3c0 62 6a 65 63 74 20 69 73 0a 2a 2a 20 72 65 74 75 bject is.** retu
3f3d0 72 6e 65 64 2e 20 54 68 69 73 20 70 6f 69 6e 74 rned. This point
3f3e0 65 72 20 6d 61 79 20 62 65 20 75 73 65 64 20 77 er may be used w
3f3f0 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f ith the sqlite3_
3f400 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 61 6e backup_step() an
3f410 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 d.** sqlite3_bac
3f420 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 75 6e kup_finish() fun
3f430 63 74 69 6f 6e 73 20 74 6f 20 70 65 72 66 6f 72 ctions to perfor
3f440 6d 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 m the specified
3f450 62 61 63 6b 75 70 20 0a 2a 2a 20 6f 70 65 72 61 backup .** opera
3f460 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 tion..**.** <b>s
3f470 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
3f480 65 70 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46 ep()</b>.**.** F
3f490 75 6e 63 74 69 6f 6e 20 5b 73 71 6c 69 74 65 33 unction [sqlite3
3f4a0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 5d 20 _backup_step()]
3f4b0 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20 is used to copy
3f4c0 75 70 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 up to nPage page
3f4d0 73 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20 74 68 s between .** th
3f4e0 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 e source and des
3f4f0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
3f500 65 73 2c 20 77 68 65 72 65 20 6e 50 61 67 65 20 es, where nPage
3f510 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 is the value of
3f520 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 the .** second p
3f530 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 arameter passed
3f540 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3f550 70 5f 73 74 65 70 28 29 2e 20 49 66 20 6e 50 61 p_step(). If nPa
3f560 67 65 20 69 73 20 61 20 6e 65 67 61 74 69 76 65 ge is a negative
3f570 0a 2a 2a 20 76 61 6c 75 65 2c 20 61 6c 6c 20 72 .** value, all r
3f580 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 63 65 20 emaining source
3f590 70 61 67 65 73 20 61 72 65 20 63 6f 70 69 65 64 pages are copied
3f5a0 2e 20 49 66 20 74 68 65 20 72 65 71 75 69 72 65 . If the require
3f5b0 64 20 70 61 67 65 73 20 61 72 65 20 0a 2a 2a 20 d pages are .**
3f5c0 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f 70 69 succesfully copi
3f5d0 65 64 2c 20 62 75 74 20 74 68 65 72 65 20 61 72 ed, but there ar
3f5e0 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70 61 67 e still more pag
3f5f0 65 73 20 74 6f 20 63 6f 70 79 20 62 65 66 6f 72 es to copy befor
3f600 65 20 74 68 65 20 0a 2a 2a 20 62 61 63 6b 75 70 e the .** backup
3f610 20 69 73 20 63 6f 6d 70 6c 65 74 65 2c 20 69 74 is complete, it
3f620 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
3f630 5f 4f 4b 5d 2e 20 49 66 20 6e 6f 20 65 72 72 6f _OK]. If no erro
3f640 72 20 6f 63 63 75 72 65 64 20 61 6e 64 20 74 68 r occured and th
3f650 65 72 65 20 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d ere .** are no m
3f660 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 ore pages to cop
3f670 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f y, then [SQLITE_
3f680 44 4f 4e 45 5d 20 69 73 20 72 65 74 75 72 6e 65 DONE] is returne
3f690 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a d. If an error .
3f6a0 2a 2a 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 ** occurs, then
3f6b0 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
3f6c0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
3f6d0 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 5b 53 51 . As well as [SQ
3f6e0 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20 LITE_OK] and.**
3f6f0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 61 [SQLITE_DONE], a
3f700 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
3f710 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6d _backup_step() m
3f720 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 ay return [SQLIT
3f730 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20 E_READONLY],.**
3f740 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 [SQLITE_NOMEM],
3f750 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 5b [SQLITE_BUSY], [
3f760 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20 SQLITE_LOCKED],
3f770 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 or an.** [SQLITE
3f780 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 _IOERR_ACCESS |
3f790 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 SQLITE_IOERR_XXX
3f7a0 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 ] extended error
3f7b0 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 code..**.** As
3f7c0 77 65 6c 6c 20 61 73 20 74 68 65 20 63 61 73 65 well as the case
3f7d0 20 77 68 65 72 65 20 74 68 65 20 64 65 73 74 69 where the desti
3f7e0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
3f7f0 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20 file was opened
3f800 66 6f 72 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 for.** read-only
3f810 20 61 63 63 65 73 73 2c 20 73 71 6c 69 74 65 33 access, sqlite3
3f820 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6d _backup_step() m
3f830 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 ay return [SQLIT
3f840 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a E_READONLY] if.*
3f850 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f * the destinatio
3f860 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 n is an in-memor
3f870 79 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 y database with
3f880 61 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 a different page
3f890 20 73 69 7a 65 0a 2a 2a 20 66 72 6f 6d 20 74 68 size.** from th
3f8a0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
3f8b0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 e..**.** If sqli
3f8c0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
3f8d0 29 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 20 ) cannot obtain
3f8e0 61 20 72 65 71 75 69 72 65 64 20 66 69 6c 65 2d a required file-
3f8f0 73 79 73 74 65 6d 20 6c 6f 63 6b 2c 20 74 68 65 system lock, the
3f900 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 n.** the [sqlite
3f910 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 3_busy_handler |
3f920 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75 busy-handler fu
3f930 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e nction].** is in
3f940 76 6f 6b 65 64 20 28 69 66 20 6f 6e 65 20 69 73 voked (if one is
3f950 20 73 70 65 63 69 66 69 65 64 29 2e 20 49 66 20 specified). If
3f960 74 68 65 20 0a 2a 2a 20 62 75 73 79 2d 68 61 6e the .** busy-han
3f970 64 6c 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e dler returns non
3f980 2d 7a 65 72 6f 20 62 65 66 6f 72 65 20 74 68 65 -zero before the
3f990 20 6c 6f 63 6b 20 69 73 20 61 76 61 69 6c 61 62 lock is availab
3f9a0 6c 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 le, then .** [SQ
3f9b0 4c 49 54 45 5f 42 55 53 59 5d 20 69 73 20 72 65 LITE_BUSY] is re
3f9c0 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
3f9d0 6c 6c 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61 ller. In this ca
3f9e0 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a 2a se the call to.*
3f9f0 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 * sqlite3_backup
3fa00 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72 _step() can be r
3fa10 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20 49 66 etried later. If
3fa20 20 74 68 65 20 73 6f 75 72 63 65 0a 2a 2a 20 5b the source.** [
3fa30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3fa40 69 6f 6e 5d 0a 2a 2a 20 69 73 20 62 65 69 6e 67 ion].** is being
3fa50 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 74 used to write t
3fa60 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 o the source dat
3fa70 61 62 61 73 65 20 77 68 65 6e 20 73 71 6c 69 74 abase when sqlit
3fa80 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
3fa90 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 .** is called, t
3faa0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b hen [SQLITE_LOCK
3fab0 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 ED] is returned
3fac0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 41 67 61 immediately. Aga
3fad0 69 6e 2c 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 in, in this.** c
3fae0 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ase the call to
3faf0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
3fb00 74 65 70 28 29 20 63 61 6e 20 62 65 20 72 65 74 tep() can be ret
3fb10 72 69 65 64 20 6c 61 74 65 72 20 6f 6e 2e 20 49 ried later on. I
3fb20 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 f.** [SQLITE_IOE
3fb30 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 RR_ACCESS | SQLI
3fb40 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 2c 20 5b TE_IOERR_XXX], [
3fb50 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f SQLITE_NOMEM], o
3fb60 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52 45 41 r.** [SQLITE_REA
3fb70 44 4f 4e 4c 59 5d 20 69 73 20 72 65 74 75 72 6e DONLY] is return
3fb80 65 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 65 ed, then .** the
3fb90 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 re is no point i
3fba0 6e 20 72 65 74 72 79 69 6e 67 20 74 68 65 20 63 n retrying the c
3fbb0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
3fbc0 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 ackup_step(). Th
3fbd0 65 73 65 20 0a 2a 2a 20 65 72 72 6f 72 73 20 61 ese .** errors a
3fbe0 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 re considered fa
3fbf0 74 61 6c 2e 20 41 74 20 74 68 69 73 20 70 6f 69 tal. At this poi
3fc00 6e 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 nt the applicati
3fc10 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74 20 0a on must accept .
3fc20 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61 63 6b ** that the back
3fc30 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 up operation has
3fc40 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61 73 73 failed and pass
3fc50 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
3fc60 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a ation handle .**
3fc70 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f to the sqlite3_
3fc80 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 backup_finish()
3fc90 74 6f 20 72 65 6c 65 61 73 65 20 61 73 73 6f 63 to release assoc
3fca0 69 61 74 65 64 20 72 65 73 6f 75 72 63 65 73 2e iated resources.
3fcb0 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 .**.** Following
3fcc0 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
3fcd0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3fce0 70 5f 73 74 65 70 28 29 2c 20 61 6e 20 65 78 63 p_step(), an exc
3fcf0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 0a 2a lusive lock is.*
3fd00 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 * obtained on th
3fd10 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 e destination fi
3fd20 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20 72 65 le. It is not re
3fd30 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65 69 74 leased until eit
3fd40 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f her .** sqlite3_
3fd50 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 backup_finish()
3fd60 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74 68 65 is called or the
3fd70 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
3fd80 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 0a 2a n is complete .*
3fd90 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 * and sqlite3_ba
3fda0 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65 74 75 ckup_step() retu
3fdb0 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 rns [SQLITE_DONE
3fdc0 5d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c ]. Additionally,
3fdd0 20 65 61 63 68 20 74 69 6d 65 20 0a 2a 2a 20 61 each time .** a
3fde0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
3fdf0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 _backup_step() i
3fe00 73 20 6d 61 64 65 20 61 20 5b 73 68 61 72 65 64 s made a [shared
3fe10 20 6c 6f 63 6b 5d 20 69 73 20 6f 62 74 61 69 6e lock] is obtain
3fe20 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 73 6f 75 ed on.** the sou
3fe30 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c rce database fil
3fe40 65 2e 20 54 68 69 73 20 6c 6f 63 6b 20 69 73 20 e. This lock is
3fe50 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 released before
3fe60 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 the.** sqlite3_b
3fe70 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6c ackup_step() cal
3fe80 6c 20 72 65 74 75 72 6e 73 2e 20 42 65 63 61 75 l returns. Becau
3fe90 73 65 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 se the source da
3fea0 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a tabase is not.**
3feb0 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65 6e 20 locked between
3fec0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
3fed0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 _backup_step(),
3fee0 69 74 20 6d 61 79 20 62 65 20 6d 6f 64 69 66 69 it may be modifi
3fef0 65 64 20 6d 69 64 2d 77 61 79 0a 2a 2a 20 74 68 ed mid-way.** th
3ff00 72 6f 75 67 68 20 74 68 65 20 62 61 63 6b 75 70 rough the backup
3ff10 20 70 72 6f 63 65 64 75 72 65 2e 20 49 66 20 74 procedure. If t
3ff20 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
3ff30 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 se is modified b
3ff40 79 20 61 6e 0a 2a 2a 20 65 78 74 65 72 6e 61 6c y an.** external
3ff50 20 70 72 6f 63 65 73 73 20 6f 72 20 76 69 61 20 process or via
3ff60 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
3ff70 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e ction other than
3ff80 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67 0a 2a the one being.*
3ff90 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61 * used by the ba
3ffa0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ckup operation,
3ffb0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 then the backup
3ffc0 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 70 61 72 will be transpar
3ffd0 65 6e 74 6c 79 0a 2a 2a 20 72 65 73 74 61 72 74 ently.** restart
3ffe0 65 64 20 62 79 20 74 68 65 20 6e 65 78 74 20 63 ed by the next c
3fff0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
40000 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 ackup_step(). If
40010 20 74 68 65 20 73 6f 75 72 63 65 20 0a 2a 2a 20 the source .**
40020 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 database is modi
40030 66 69 65 64 20 62 79 20 74 68 65 20 75 73 69 6e fied by the usin
40040 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
40050 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 ase connection a
40060 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 62 79 20 s is used.** by
40070 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
40080 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 tion, then the b
40090 61 63 6b 75 70 20 64 61 74 61 62 61 73 65 20 69 ackup database i
400a0 73 20 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 20 s transparently
400b0 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 74 20 74 .** updated at t
400c0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a he same time..**
400d0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 .** <b>sqlite3_b
400e0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f ackup_finish()</
400f0 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71 b>.**.** Once sq
40100 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
40110 70 28 29 20 68 61 73 20 72 65 74 75 72 6e 65 64 p() has returned
40120 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 [SQLITE_DONE],
40130 6f 72 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 or when the .**
40140 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 73 68 application wish
40150 65 73 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 es to abandon th
40160 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
40170 6f 6e 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 on, the [sqlite3
40180 5f 62 61 63 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65 _backup].** obje
40190 63 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 ct should be pas
401a0 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 sed to sqlite3_b
401b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 ackup_finish().
401c0 54 68 69 73 20 72 65 6c 65 61 73 65 73 20 61 6c This releases al
401d0 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 61 l.** resources a
401e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
401f0 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
40200 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 5f ion. If sqlite3_
40210 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a backup_step().**
40220 20 68 61 73 20 6e 6f 74 20 79 65 74 20 72 65 74 has not yet ret
40230 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f urned [SQLITE_DO
40240 4e 45 5d 2c 20 74 68 65 6e 20 61 6e 79 20 61 63 NE], then any ac
40250 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 tive write-trans
40260 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a action on the.**
40270 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
40280 61 62 61 73 65 20 69 73 20 72 6f 6c 6c 65 64 20 abase is rolled
40290 62 61 63 6b 2e 20 54 68 65 20 5b 73 71 6c 69 74 back. The [sqlit
402a0 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
402b0 74 20 69 73 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 t is invalid.**
402c0 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 and may not be u
402d0 73 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 sed following a
402e0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
402f0 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e backup_finish().
40300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
40310 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
40320 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
40330 73 68 20 69 73 20 5b 53 51 4c 49 54 45 5f 4f 4b sh is [SQLITE_OK
40340 5d 20 69 66 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a ] if no error.**
40350 20 6f 63 63 75 72 72 65 64 2c 20 72 65 67 61 72 occurred, regar
40360 64 6c 65 73 73 20 6f 72 20 77 68 65 74 68 65 72 dless or whether
40370 20 6f 72 20 6e 6f 74 20 73 71 6c 69 74 65 33 5f or not sqlite3_
40380 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 77 61 backup_step() wa
40390 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 73 75 s called.** a su
403a0 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 fficient number
403b0 6f 66 20 74 69 6d 65 73 20 74 6f 20 63 6f 6d 70 of times to comp
403c0 6c 65 74 65 20 74 68 65 20 62 61 63 6b 75 70 20 lete the backup
403d0 6f 70 65 72 61 74 69 6f 6e 2e 20 4f 72 2c 20 69 operation. Or, i
403e0 66 0a 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d f.** an out-of-m
403f0 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f 6e 20 emory condition
40400 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 or IO error occu
40410 72 65 64 20 64 75 72 69 6e 67 20 61 20 63 61 6c red during a cal
40420 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f l to.** sqlite3_
40430 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 74 68 backup_step() th
40440 65 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d en [SQLITE_NOMEM
40450 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 ] or an.** [SQLI
40460 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 TE_IOERR_ACCESS
40470 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 | SQLITE_IOERR_X
40480 58 58 5d 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a XX] error code.*
40490 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 * is returned. I
404a0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
404b0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 61 error code and a
404c0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
404d0 61 72 65 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 are.** written t
404e0 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
404f0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
40500 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41 ection]..**.** A
40510 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51 4c 49 return of [SQLI
40520 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c TE_BUSY] or [SQL
40530 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d ITE_LOCKED] from
40540 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
40550 73 74 65 70 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 step() is.** not
40560 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 65 72 72 a permanent err
40570 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 or and does not
40580 61 66 66 65 63 74 20 74 68 65 20 72 65 74 75 72 affect the retur
40590 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 73 71 n value of.** sq
405a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
405b0 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e ish()..**.** <b>
405c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 sqlite3_backup_r
405d0 65 6d 61 69 6e 69 6e 67 28 29 2c 20 73 71 6c 69 emaining(), sqli
405e0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
405f0 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a ount()</b>.**.**
40600 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 Each call to sq
40610 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
40620 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 61 6c p() sets two val
40630 75 65 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 ues stored inter
40640 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 61 6e 20 5b nally.** by an [
40650 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 sqlite3_backup]
40660 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 75 6d 62 object. The numb
40670 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c er of pages stil
40680 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 0a 2a l to be backed.*
40690 2a 20 75 70 2c 20 77 68 69 63 68 20 6d 61 79 20 * up, which may
406a0 62 65 20 71 75 65 72 69 65 64 20 62 79 20 73 71 be queried by sq
406b0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d lite3_backup_rem
406c0 61 69 6e 69 6e 67 28 29 2c 20 61 6e 64 20 74 68 aining(), and th
406d0 65 20 74 6f 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65 e total.** numbe
406e0 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
406f0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
40700 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 e file, which ma
40710 79 20 62 65 20 71 75 65 72 69 65 64 20 62 79 0a y be queried by.
40720 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
40730 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 2a p_pagecount()..*
40740 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 73 20 *.** The values
40750 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 returned by thes
40760 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
40770 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 62 79 0a only updated by.
40780 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
40790 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65 p_step(). If the
407a0 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
407b0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 64 75 72 is modified dur
407c0 69 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a 2a 20 ing a backup.**
407d0 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 operation, then
407e0 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e the values are n
407f0 6f 74 20 75 70 64 61 74 65 64 20 74 6f 20 61 63 ot updated to ac
40800 63 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20 65 78 count for any ex
40810 74 72 61 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 tra.** pages tha
40820 74 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 t need to be upd
40830 61 74 65 64 20 6f 72 20 74 68 65 20 73 69 7a 65 ated or the size
40840 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 of the source d
40850 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 atabase file.**
40860 63 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 changing..**.**
40870 3c 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20 55 73 <b>Concurrent Us
40880 61 67 65 20 6f 66 20 44 61 74 61 62 61 73 65 20 age of Database
40890 48 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a Handles</b>.**.*
408a0 2a 20 54 68 65 20 73 6f 75 72 63 65 20 5b 64 61 * The source [da
408b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
408c0 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 n] may be used b
408d0 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f y the applicatio
408e0 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a 20 70 n for other.** p
408f0 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20 61 20 urposes while a
40900 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
40910 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f 72 20 is underway or
40920 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 being initialize
40930 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 d..** If SQLite
40940 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64 20 is compiled and
40950 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 73 75 configured to su
40960 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 66 65 pport threadsafe
40970 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e database.** con
40980 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e 20 74 nections, then t
40990 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
409a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 se connection ma
409b0 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63 75 72 y be used concur
409c0 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77 rently.** from w
409d0 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68 72 65 ithin other thre
409e0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 ads..**.** Howev
409f0 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 er, the applicat
40a00 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e 74 ion must guarant
40a10 65 65 20 74 68 61 74 20 74 68 65 20 64 65 73 74 ee that the dest
40a20 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
40a30 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 .** connection h
40a40 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 70 61 73 andle is not pas
40a50 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 sed to any other
40a60 20 41 50 49 20 28 62 79 20 61 6e 79 20 74 68 72 API (by any thr
40a70 65 61 64 29 20 61 66 74 65 72 20 0a 2a 2a 20 73 ead) after .** s
40a80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
40a90 69 74 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 it() is called a
40aa0 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f nd before the co
40ab0 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 61 6c 6c rresponding call
40ac0 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 to.** sqlite3_b
40ad0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 ackup_finish().
40ae0 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79 20 53 51 Unfortunately SQ
40af0 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 63 75 Lite does not cu
40b00 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 0a 2a 2a rrently check.**
40b10 20 66 6f 72 20 74 68 69 73 2c 20 69 66 20 74 68 for this, if th
40b20 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f e application do
40b30 65 73 20 75 73 65 20 74 68 65 20 64 65 73 74 69 es use the desti
40b40 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 nation [database
40b50 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 connection].**
40b60 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 for some other p
40b70 75 72 70 6f 73 65 20 64 75 72 69 6e 67 20 61 20 urpose during a
40b80 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
40b90 2c 20 74 68 69 6e 67 73 20 6d 61 79 20 61 70 70 , things may app
40ba0 65 61 72 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63 ear to.** work c
40bb0 6f 72 72 65 63 74 6c 79 20 62 75 74 20 69 6e 20 orrectly but in
40bc0 66 61 63 74 20 62 65 20 73 75 62 74 6c 79 20 6d fact be subtly m
40bd0 61 6c 66 75 6e 63 74 69 6f 6e 69 6e 67 2e 20 20 alfunctioning.
40be0 55 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 Use of the.** de
40bf0 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
40c00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 se connection wh
40c10 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 20 ile a backup is
40c20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69 67 68 in progress migh
40c30 74 0a 2a 2a 20 61 6c 73 6f 20 63 61 75 73 65 20 t.** also cause
40c40 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f 63 6b a mutex deadlock
40c50 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65 72 6d ..**.** Furtherm
40c60 6f 72 65 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 ore, if running
40c70 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 in [shared cache
40c80 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70 70 6c mode], the appl
40c90 69 63 61 74 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20 ication must.**
40ca0 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 guarantee that t
40cb0 68 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20 he shared cache
40cc0 75 73 65 64 20 62 79 20 74 68 65 20 64 65 73 74 used by the dest
40cd0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
40ce0 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 63 63 65 73 .** is not acces
40cf0 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 62 61 sed while the ba
40d00 63 6b 75 70 20 69 73 20 72 75 6e 6e 69 6e 67 2e ckup is running.
40d10 20 49 6e 20 70 72 61 63 74 69 63 65 20 74 68 69 In practice thi
40d20 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 s means.** that
40d30 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
40d40 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
40d50 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 hat the file-sys
40d60 74 65 6d 20 66 69 6c 65 20 62 65 69 6e 67 20 0a tem file being .
40d70 2a 2a 20 62 61 63 6b 65 64 20 75 70 20 74 6f 20 ** backed up to
40d80 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 is not accessed
40d90 62 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74 69 6f by any connectio
40da0 6e 20 77 69 74 68 69 6e 20 74 68 65 20 70 72 6f n within the pro
40db0 63 65 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 cess,.** not jus
40dc0 74 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63 t the specific c
40dd0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 77 onnection that w
40de0 61 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c as passed to sql
40df0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 ite3_backup_init
40e00 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 ()..**.** The [s
40e10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f qlite3_backup] o
40e20 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69 73 20 bject itself is
40e30 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65 61 64 partially thread
40e40 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65 20 0a safe. Multiple .
40e50 2a 2a 20 74 68 72 65 61 64 73 20 6d 61 79 20 73 ** threads may s
40e60 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75 6c 74 69 afely make multi
40e70 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 63 ple concurrent c
40e80 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f alls to sqlite3_
40e90 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a backup_step()..*
40ea0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 * However, the s
40eb0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 qlite3_backup_re
40ec0 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 maining() and sq
40ed0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 lite3_backup_pag
40ee0 65 63 6f 75 6e 74 28 29 0a 2a 2a 20 41 50 49 73 ecount().** APIs
40ef0 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c are not strictl
40f00 79 20 73 70 65 61 6b 69 6e 67 20 74 68 72 65 61 y speaking threa
40f10 64 73 61 66 65 2e 20 49 66 20 74 68 65 79 20 61 dsafe. If they a
40f20 72 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 re invoked at th
40f30 65 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 20 61 e.** same time a
40f40 73 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 s another thread
40f50 20 69 73 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c is invoking sql
40f60 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
40f70 28 29 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73 () it is.** poss
40f80 69 62 6c 65 20 74 68 61 74 20 74 68 65 79 20 72 ible that they r
40f90 65 74 75 72 6e 20 69 6e 76 61 6c 69 64 20 76 61 eturn invalid va
40fa0 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lues..*/.SQLITE_
40fb0 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b API sqlite3_back
40fc0 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b up *sqlite3_back
40fd0 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 up_init(. sqlit
40fe0 65 33 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 e3 *pDest,
40ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41000 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e /* Destination
41010 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
41020 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
41030 20 2a 7a 44 65 73 74 4e 61 6d 65 2c 20 20 20 20 *zDestName,
41040 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
41050 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 Destination data
41060 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 base name */. s
41070 71 6c 69 74 65 33 20 2a 70 53 6f 75 72 63 65 2c qlite3 *pSource,
41080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41090 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
410a0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
410b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
410c0 2a 7a 53 6f 75 72 63 65 4e 61 6d 65 20 20 20 20 *zSourceName
410d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
410e0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 6e ource database n
410f0 61 6d 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 ame */.);.SQLITE
41100 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
41110 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c _backup_step(sql
41120 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 ite3_backup *p,
41130 69 6e 74 20 6e 50 61 67 65 29 3b 0a 53 51 4c 49 int nPage);.SQLI
41140 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
41150 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
41160 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
41170 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *p);.SQLITE_API
41180 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b int sqlite3_back
41190 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c up_remaining(sql
411a0 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b ite3_backup *p);
411b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
411c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 sqlite3_backup_p
411d0 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 agecount(sqlite3
411e0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a _backup *p);../*
411f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 6e .** CAPI3REF: Un
41200 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f lock Notificatio
41210 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 n.** EXPERIMENTA
41220 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 72 75 6e L.**.** When run
41230 6e 69 6e 67 20 69 6e 20 73 68 61 72 65 64 2d 63 ning in shared-c
41240 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 64 61 74 ache mode, a dat
41250 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 abase operation
41260 6d 61 79 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a may fail with.**
41270 20 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b an [SQLITE_LOCK
41280 45 44 5d 20 65 72 72 6f 72 20 69 66 20 74 68 65 ED] error if the
41290 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 required locks
412a0 6f 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 on the shared-ca
412b0 63 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 che or.** indivi
412c0 64 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 dual tables with
412d0 69 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 in the shared-ca
412e0 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 che cannot be ob
412f0 74 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a 20 5b tained. See.** [
41300 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 SQLite Shared-Ca
41310 63 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20 61 20 che Mode] for a
41320 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 73 description of s
41330 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b hared-cache lock
41340 69 6e 67 2e 20 0a 2a 2a 20 54 68 69 73 20 41 50 ing. .** This AP
41350 49 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f I may be used to
41360 20 72 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c register a call
41370 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69 74 65 back that SQLite
41380 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a will invoke .**
41390 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 when the connec
413a0 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 tion currently h
413b0 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 olding the requi
413c0 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 red lock relinqu
413d0 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 54 68 69 ishes it..** Thi
413e0 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76 s API is only av
413f0 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c ailable if the l
41400 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 ibrary was compi
41410 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 led with the.**
41420 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 [SQLITE_ENABLE_U
41430 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d NLOCK_NOTIFY] C-
41440 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d preprocessor sym
41450 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a bol defined..**.
41460 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 ** See Also: [Us
41470 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 55 ing the SQLite U
41480 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 nlock Notificati
41490 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a on Feature]..**.
414a0 2a 2a 20 53 68 61 72 65 64 2d 63 61 63 68 65 20 ** Shared-cache
414b0 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 locks are releas
414c0 65 64 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 ed when a databa
414d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f se connection co
414e0 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73 20 63 ncludes.** its c
414f0 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
41500 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20 63 6f on, either by co
41510 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72 20 72 mmitting it or r
41520 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e 20 olling it back.
41530 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f .**.** When a co
41540 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20 nnection (known
41550 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 as the blocked c
41560 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 69 6c 73 onnection) fails
41570 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 to obtain a.**
41580 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 shared-cache loc
41590 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 k and SQLITE_LOC
415a0 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20 KED is returned
415b0 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2c 20 74 to the caller, t
415c0 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 79 20 6f he.** identity o
415d0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 f the database c
415e0 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 65 20 62 onnection (the b
415f0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
41600 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 on) that.** has
41610 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 71 75 69 locked the requi
41620 72 65 64 20 72 65 73 6f 75 72 63 65 20 69 73 20 red resource is
41630 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c stored internall
41640 79 2e 20 41 66 74 65 72 20 61 6e 20 0a 2a 2a 20 y. After an .**
41650 61 70 70 6c 69 63 61 74 69 6f 6e 20 72 65 63 65 application rece
41660 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45 5f 4c ives an SQLITE_L
41670 4f 43 4b 45 44 20 65 72 72 6f 72 2c 20 69 74 20 OCKED error, it
41680 6d 61 79 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 may call the.**
41690 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
416a0 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64 20 77 otify() method w
416b0 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 ith the blocked
416c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
416d0 65 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 69 72 e as .** the fir
416e0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 st argument to r
416f0 65 67 69 73 74 65 72 20 66 6f 72 20 61 20 63 61 egister for a ca
41700 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c 6c llback that will
41710 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 be invoked.** w
41720 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 hen the blocking
41730 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 connections cur
41740 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
41750 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 54 is concluded. T
41760 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 he.** callback i
41770 73 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77 s invoked from w
41780 69 74 68 69 6e 20 74 68 65 20 5b 73 71 6c 69 74 ithin the [sqlit
41790 65 33 5f 73 74 65 70 5d 20 6f 72 20 5b 73 71 6c e3_step] or [sql
417a0 69 74 65 33 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63 ite3_close].** c
417b0 61 6c 6c 20 74 68 61 74 20 63 6f 6e 63 6c 75 64 all that conclud
417c0 65 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 es the blocking
417d0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e connections tran
417e0 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 saction..**.** I
417f0 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b f sqlite3_unlock
41800 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 6c _notify() is cal
41810 6c 65 64 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 led in a multi-t
41820 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 hreaded applicat
41830 69 6f 6e 2c 0a 2a 2a 20 74 68 65 72 65 20 69 73 ion,.** there is
41840 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 a chance that t
41850 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e he blocking conn
41860 65 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61 76 65 ection will have
41870 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 6f 6e 63 already.** conc
41880 6c 75 64 65 64 20 69 74 73 20 74 72 61 6e 73 61 luded its transa
41890 63 74 69 6f 6e 20 62 79 20 74 68 65 20 74 69 6d ction by the tim
418a0 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b e sqlite3_unlock
418b0 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 69 6e 76 _notify() is inv
418c0 6f 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 69 73 oked..** If this
418d0 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 74 happens, then t
418e0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 6c he specified cal
418f0 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
41900 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a immediately,.**
41910 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 from within the
41920 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
41930 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
41940 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 ..**.** If the b
41950 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f locked connectio
41960 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e 67 20 n is attempting
41970 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74 to obtain a writ
41980 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73 e-lock on a.** s
41990 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c hared-cache tabl
419a0 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74 68 61 6e e, and more than
419b0 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 one other conne
419c0 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 ction currently
419d0 68 6f 6c 64 73 0a 2a 2a 20 61 20 72 65 61 64 2d holds.** a read-
419e0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 61 6d 65 lock on the same
419f0 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 53 51 4c table, then SQL
41a00 69 74 65 20 61 72 62 69 74 72 61 72 69 6c 79 20 ite arbitrarily
41a10 73 65 6c 65 63 74 73 20 6f 6e 65 20 6f 66 20 0a selects one of .
41a20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e ** the other con
41a30 6e 65 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 nections to use
41a40 61 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 as the blocking
41a50 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a connection..**.*
41a60 2a 20 54 68 65 72 65 20 6d 61 79 20 62 65 20 61 * There may be a
41a70 74 20 6d 6f 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 t most one unloc
41a80 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
41a90 6b 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 k registered by
41aa0 61 20 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f a .** blocked co
41ab0 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 73 71 6c nnection. If sql
41ac0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
41ad0 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77 fy() is called w
41ae0 68 65 6e 20 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b hen the.** block
41af0 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c ed connection al
41b00 72 65 61 64 79 20 68 61 73 20 61 20 72 65 67 69 ready has a regi
41b10 73 74 65 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f stered unlock-no
41b20 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a tify callback,.*
41b30 2a 20 74 68 65 6e 20 74 68 65 20 6e 65 77 20 63 * then the new c
41b40 61 6c 6c 62 61 63 6b 20 72 65 70 6c 61 63 65 73 allback replaces
41b50 20 74 68 65 20 6f 6c 64 2e 20 49 66 20 73 71 6c the old. If sql
41b60 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
41b70 66 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 fy() is.** calle
41b80 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f d with a NULL po
41b90 69 6e 74 65 72 20 61 73 20 69 74 73 20 73 65 63 inter as its sec
41ba0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68 ond argument, th
41bb0 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e 67 0a en any existing.
41bc0 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ** unlock-notify
41bd0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e callback is can
41be0 63 65 6c 6c 65 64 2e 20 54 68 65 20 62 6c 6f 63 celled. The bloc
41bf0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ked connections
41c00 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 .** unlock-notif
41c10 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 y callback may a
41c20 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65 64 20 lso be canceled
41c30 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 62 by closing the b
41c40 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 locked.** connec
41c50 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 tion using [sqli
41c60 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a te3_close()]..**
41c70 0a 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e .** The unlock-n
41c80 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 otify callback i
41c90 73 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e s not reentrant.
41ca0 20 49 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 If an applicati
41cb0 6f 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e on invokes.** an
41cc0 79 20 73 71 6c 69 74 65 33 5f 78 78 78 20 41 50 y sqlite3_xxx AP
41cd0 49 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d I functions from
41ce0 20 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 within an unloc
41cf0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
41d00 6b 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20 6f 72 k, a.** crash or
41d10 20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20 62 65 deadlock may be
41d20 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a the result..**.
41d30 2a 2a 20 55 6e 6c 65 73 73 20 64 65 61 64 6c 6f ** Unless deadlo
41d40 63 6b 20 69 73 20 64 65 74 65 63 74 65 64 20 28 ck is detected (
41d50 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71 6c 69 see below), sqli
41d60 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
41d70 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 y() always.** re
41d80 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
41d90 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 .**.** <b>Callba
41da0 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20 44 65 ck Invocation De
41db0 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 tails</b>.**.**
41dc0 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e When an unlock-n
41dd0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 otify callback i
41de0 73 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 s registered, th
41df0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 72 e application pr
41e00 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73 69 6e ovides a .** sin
41e10 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 gle void* pointe
41e20 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64 r that is passed
41e30 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
41e40 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 76 6f when it is invo
41e50 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c ked..** However,
41e60 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f the signature o
41e70 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 f the callback f
41e80 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 unction allows S
41e90 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a 2a 2a QLite to pass.**
41ea0 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f 66 20 it an array of
41eb0 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f void* context po
41ec0 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69 72 73 inters. The firs
41ed0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 t argument passe
41ee0 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 d to.** an unloc
41ef0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
41f00 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 k is a pointer t
41f10 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f o an array of vo
41f20 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a id* pointers,.**
41f30 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
41f40 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
41f50 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
41f60 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 array..**.** Whe
41f70 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e n a blocking con
41f80 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 nections transac
41f90 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 tion is conclude
41fa0 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 0a d, there may be.
41fb0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 ** more than one
41fc0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
41fd0 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72 65 67 ion that has reg
41fe0 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 istered for an u
41ff0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 nlock-notify.**
42000 63 61 6c 6c 62 61 63 6b 2e 20 49 66 20 74 77 6f callback. If two
42010 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20 62 6c or more such bl
42020 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
42030 73 20 68 61 76 65 20 73 70 65 63 69 66 69 65 64 s have specified
42040 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 61 6c the.** same cal
42050 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2c 20 lback function,
42060 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f 66 20 then instead of
42070 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c invoking the cal
42080 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a lback function.*
42090 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 * multiple times
420a0 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 , it is invoked
420b0 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20 73 65 once with the se
420c0 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 t of void* conte
420d0 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 73 xt pointers.** s
420e0 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 pecified by the
420f0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
42100 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f 67 65 ons bundled toge
42110 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61 72 72 ther into an arr
42120 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69 76 65 ay..** This give
42130 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f s the applicatio
42140 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 n an opportunity
42150 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65 20 61 to prioritize a
42160 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a 20 72 ny actions .** r
42170 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20 73 65 elated to the se
42180 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 20 64 t of unblocked d
42190 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
421a0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65 ons..**.** <b>De
421b0 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 6f 6e adlock Detection
421c0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d </b>.**.** Assum
421d0 69 6e 67 20 74 68 61 74 20 61 66 74 65 72 20 72 ing that after r
421e0 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72 20 61 egistering for a
421f0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 n unlock-notify
42200 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a 20 64 callback a .** d
42210 61 74 61 62 61 73 65 20 77 61 69 74 73 20 66 6f atabase waits fo
42220 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 74 r the callback t
42230 6f 20 62 65 20 69 73 73 75 65 64 20 62 65 66 6f o be issued befo
42240 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20 66 75 re taking any fu
42250 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f 6e 20 rther.** action
42260 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 61 73 (a reasonable as
42270 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65 6e 20 sumption), then
42280 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 20 6d using this API m
42290 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 ay cause the.**
422a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 64 application to d
422b0 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65 78 61 eadlock. For exa
422c0 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65 63 74 mple, if connect
422d0 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69 6e 67 ion X is waiting
422e0 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 for.** connecti
422f0 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63 74 69 on Y's transacti
42300 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c 75 64 on to be conclud
42310 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61 72 6c ed, and similarl
42320 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 y connection.**
42330 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20 Y is waiting on
42340 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73 20 74 connection X's t
42350 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e ransaction, then
42360 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65 63 74 neither connect
42370 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72 6f 63 ion.** will proc
42380 65 65 64 20 61 6e 64 20 74 68 65 20 73 79 73 74 eed and the syst
42390 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20 64 65 em may remain de
423a0 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66 69 6e adlocked indefin
423b0 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 itely..**.** To
423c0 61 76 6f 69 64 20 74 68 69 73 20 73 63 65 6e 61 avoid this scena
423d0 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 rio, the sqlite3
423e0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
423f0 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64 6c 6f performs deadlo
42400 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f 6e 2e ck.** detection.
42410 20 49 66 20 61 20 67 69 76 65 6e 20 63 61 6c 6c If a given call
42420 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f to sqlite3_unlo
42430 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f 75 6c ck_notify() woul
42440 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73 79 73 d put the.** sys
42450 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 tem in a deadloc
42460 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65 6e 20 ked state, then
42470 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 SQLITE_LOCKED is
42480 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
42490 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 .** unlock-notif
424a0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 y callback is re
424b0 67 69 73 74 65 72 65 64 2e 20 54 68 65 20 73 79 gistered. The sy
424c0 73 74 65 6d 20 69 73 20 73 61 69 64 20 74 6f 20 stem is said to
424d0 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61 64 6c be in.** a deadl
424e0 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66 20 63 ocked state if c
424f0 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61 73 20 onnection A has
42500 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 registered for a
42510 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a n unlock-notify.
42520 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 ** callback on t
42530 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 he conclusion of
42540 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27 73 20 connection B's
42550 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 transaction, and
42560 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42 connection.** B
42570 20 68 61 73 20 69 74 73 65 6c 66 20 72 65 67 69 has itself regi
42580 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e stered for an un
42590 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
425a0 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e 65 63 back when connec
425b0 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72 61 6e tion.** A's tran
425c0 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c saction is concl
425d0 75 64 65 64 2e 20 49 6e 64 69 72 65 63 74 20 64 uded. Indirect d
425e0 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 eadlock is also
425f0 64 65 74 65 63 74 65 64 2c 20 73 6f 0a 2a 2a 20 detected, so.**
42600 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6c the system is al
42610 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f so considered to
42620 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 64 20 69 be deadlocked i
42630 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 20 68 f connection B h
42640 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 as.** registered
42650 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
42660 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6f otify callback o
42670 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e n the conclusion
42680 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a of connection.*
42690 2a 20 43 27 73 20 74 72 61 6e 73 61 63 74 69 6f * C's transactio
426a0 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e 65 63 74 n, where connect
426b0 69 6f 6e 20 43 20 69 73 20 77 61 69 74 69 6e 67 ion C is waiting
426c0 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 on connection A
426d0 2e 20 41 6e 79 0a 2a 2a 20 6e 75 6d 62 65 72 20 . Any.** number
426e0 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 69 6e 64 of levels of ind
426f0 69 72 65 63 74 69 6f 6e 20 61 72 65 20 61 6c 6c irection are all
42700 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54 owed..**.** <b>T
42710 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 he "DROP TABLE"
42720 45 78 63 65 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a Exception</b>.**
42730 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 6c 6c 20 .** When a call
42740 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 to [sqlite3_step
42750 28 29 5d 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ()] returns SQLI
42760 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 74 20 69 73 TE_LOCKED, it is
42770 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 61 6c 77 61 almost .** alwa
42780 79 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 ys appropriate t
42790 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 75 o call sqlite3_u
427a0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 nlock_notify().
427b0 54 68 65 72 65 20 69 73 20 68 6f 77 65 76 65 72 There is however
427c0 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 65 70 74 69 ,.** one excepti
427d0 6f 6e 2e 20 57 68 65 6e 20 65 78 65 63 75 74 69 on. When executi
427e0 6e 67 20 61 20 22 44 52 4f 50 20 54 41 42 4c 45 ng a "DROP TABLE
427f0 22 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 " or "DROP INDEX
42800 22 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 " statement,.**
42810 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20 69 66 SQLite checks if
42820 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 there are any c
42830 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 urrently executi
42840 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ng SELECT statem
42850 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 62 65 6c ents.** that bel
42860 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ong to the same
42870 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 74 connection. If t
42880 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49 54 45 here are, SQLITE
42890 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a 2a 20 72 65 _LOCKED is.** re
428a0 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 turned. In this
428b0 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f case there is no
428c0 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 "blocking conne
428d0 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b ction", so invok
428e0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 ing.** sqlite3_u
428f0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 72 nlock_notify() r
42900 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 75 6e esults in the un
42910 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
42920 62 61 63 6b 20 62 65 69 6e 67 0a 2a 2a 20 69 6e back being.** in
42930 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c voked immediatel
42940 79 2e 20 49 66 20 74 68 65 20 61 70 70 6c 69 63 y. If the applic
42950 61 74 69 6f 6e 20 74 68 65 6e 20 72 65 2d 61 74 ation then re-at
42960 74 65 6d 70 74 73 20 74 68 65 20 22 44 52 4f 50 tempts the "DROP
42970 20 54 41 42 4c 45 22 0a 2a 2a 20 6f 72 20 22 44 TABLE".** or "D
42980 52 4f 50 20 49 4e 44 45 58 22 20 71 75 65 72 79 ROP INDEX" query
42990 2c 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6c 6f , an infinite lo
429a0 6f 70 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 op might be the
429b0 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e result..**.** On
429c0 65 20 77 61 79 20 61 72 6f 75 6e 64 20 74 68 69 e way around thi
429d0 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 6f 20 s problem is to
429e0 63 68 65 63 6b 20 74 68 65 20 65 78 74 65 6e 64 check the extend
429f0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 ed error code re
42a00 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 61 6e 20 turned.** by an
42a10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 sqlite3_step() c
42a20 61 6c 6c 2e 20 49 66 20 74 68 65 72 65 20 69 73 all. If there is
42a30 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e a blocking conn
42a40 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ection, then the
42a50 0a 2a 2a 20 65 78 74 65 6e 64 65 64 20 65 72 72 .** extended err
42a60 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 or code is set t
42a70 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f o SQLITE_LOCKED_
42a80 53 48 41 52 45 44 43 41 43 48 45 2e 20 4f 74 68 SHAREDCACHE. Oth
42a90 65 72 77 69 73 65 2c 20 69 6e 0a 2a 2a 20 74 68 erwise, in.** th
42aa0 65 20 73 70 65 63 69 61 6c 20 22 44 52 4f 50 20 e special "DROP
42ab0 54 41 42 4c 45 2f 49 4e 44 45 58 22 20 63 61 73 TABLE/INDEX" cas
42ac0 65 2c 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 e, the extended
42ad0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 6a 75 error code is ju
42ae0 73 74 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f st .** SQLITE_LO
42af0 43 4b 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f CKED..*/.SQLITE_
42b00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
42b10 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 unlock_notify(.
42b20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b sqlite3 *pBlock
42b30 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ed,
42b40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
42b50 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63 74 69 Waiting connecti
42b60 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 on */. void (*x
42b70 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a 2a 61 Notify)(void **a
42b80 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67 29 2c pArg, int nArg),
42b90 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 /* Callback
42ba0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f function to invo
42bb0 6b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4e ke */. void *pN
42bc0 6f 74 69 66 79 41 72 67 20 20 20 20 20 20 20 20 otifyArg
42bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
42be0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
42bf0 74 6f 20 70 61 73 73 20 74 6f 20 78 4e 6f 74 69 to pass to xNoti
42c00 66 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 fy */.);../*.**
42c10 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 68 Undo the hack th
42c20 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 at converts floa
42c30 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 73 ting point types
42c40 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 0a to integer for.
42c50 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f ** builds on pro
42c60 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 20 cessors without
42c70 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 floating point s
42c80 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 upport..*/.#ifde
42c90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
42ca0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 OATING_POINT.# u
42cb0 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 ndef double.#end
42cc0 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 if..#if 0.} /*
42cd0 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 65 End of the 'exte
42ce0 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f rn "C"' block */
42cf0 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a .#endif.#endif..
42d00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
42d10 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 68 End of sqlite3.h
42d20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
42d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
42d50 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
42d60 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
42d70 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
42d80 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
42d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
42da0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
42db0 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 69 Include hash.h i
42dc0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
42dd0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
42de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
42df0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
42e00 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e Begin file hash.
42e10 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
42e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
42e40 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
42e50 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 mber 22.**.** Th
42e60 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
42e70 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
42e80 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
42e90 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
42ea0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
42eb0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
42ec0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
42ed0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
42ee0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
42ef0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
42f00 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
42f10 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
42f20 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
42f30 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
42f40 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
42f50 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
42f60 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
42f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42fb0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
42fc0 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c s the header fil
42fd0 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69 e for the generi
42fe0 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 c hash-table imp
42ff0 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 lemenation.** us
43000 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a ed in SQLite..**
43010 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 68 2c .** $Id: hash.h,
43020 76 20 31 2e 31 35 20 32 30 30 39 2f 30 35 2f 30 v 1.15 2009/05/0
43030 32 20 31 33 3a 32 39 3a 33 38 20 64 72 68 20 45 2 13:29:38 drh E
43040 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
43050 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a _SQLITE_HASH_H_.
43060 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f #define _SQLITE_
43070 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 HASH_H_../* Forw
43080 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
43090 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e 20 of structures.
430a0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
430b0 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70 t Hash Hash;.typ
430c0 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 edef struct Hash
430d0 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a Elem HashElem;..
430e0 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 /* A complete ha
430f0 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 sh table is an i
43100 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
43110 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
43120 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 re..** The inter
43130 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 nals of this str
43140 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e ucture are inten
43150 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 ded to be opaque
43160 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f -- client.** co
43170 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 de should not at
43180 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20 tempt to access
43190 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69 or modify the fi
431a0 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 elds of this str
431b0 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 ucture.** direct
431c0 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73 ly. Change this
431d0 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 structure only
431e0 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75 by using the rou
431f0 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 tines below..**
43200 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66 However, some of
43210 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 65 73 the "procedures
43220 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 " and "functions
43230 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 " for modifying
43240 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 and.** accessing
43250 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
43260 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f are really macro
43270 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72 s, so we can't r
43280 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 eally make.** th
43290 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 70 61 is structure opa
432a0 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 que..**.** All e
432b0 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68 lements of the h
432c0 61 73 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e ash table are on
432d0 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 a single doubly
432e0 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a -linked list..**
432f0 20 48 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e Hash.first poin
43300 74 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f ts to the head o
43310 66 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a f this list..**.
43320 2a 2a 20 54 68 65 72 65 20 61 72 65 20 48 61 73 ** There are Has
43330 68 2e 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 h.htsize buckets
43340 2e 20 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 . Each bucket p
43350 6f 69 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 oints to a spot
43360 69 6e 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c in.** the global
43370 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c doubly-linked l
43380 69 73 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e ist. The conten
43390 74 73 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74 ts of the bucket
433a0 20 61 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d are the.** elem
433b0 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70 ent pointed to p
433c0 6c 75 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74 lus the next _ht
433d0 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 .count-1 element
433e0 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a s in the list..*
433f0 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65 *.** Hash.htsize
43400 20 61 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79 and Hash.ht may
43410 20 62 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68 be zero. In th
43420 61 74 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69 at case lookup i
43430 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c s done.** by a l
43440 69 6e 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 inear search of
43450 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e the global list.
43460 20 20 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c For small tabl
43470 65 73 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68 es, the .** Hash
43480 2e 68 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76 .ht table is nev
43490 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 er allocated bec
434a0 61 75 73 65 20 69 66 20 74 68 65 72 65 20 61 72 ause if there ar
434b0 65 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a e few elements.*
434c0 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 * in the table,
434d0 69 74 20 69 73 20 66 61 73 74 65 72 20 74 6f 20 it is faster to
434e0 64 6f 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 do a linear sear
434f0 63 68 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67 ch than to manag
43500 65 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 e.** the hash ta
43510 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 ble..*/.struct H
43520 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ash {. unsigned
43530 20 69 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 int htsize;
43540 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
43550 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 uckets in the ha
43560 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e sh table */. un
43570 73 69 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 signed int count
43580 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
43590 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
435a0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 this table */.
435b0 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b HashElem *first;
435c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
435d0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
435e0 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 f the array */.
435f0 20 73 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 struct _ht {
43600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 /* th
43610 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a e hash table */.
43620 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 int count;
43630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
43640 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
43650 69 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61 ies with this ha
43660 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c sh */. HashEl
43670 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 em *chain;
43680 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
43690 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 to first entry w
436a0 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f ith this hash */
436b0 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a . } *ht;.};../*
436c0 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e Each element in
436d0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
436e0 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
436f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
43700 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 .** structure.
43710 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 All elements are
43720 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e stored on a sin
43730 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 gle doubly-linke
43740 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 d list..**.** Ag
43750 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 ain, this struct
43760 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ure is intended
43770 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 to be opaque, bu
43780 74 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c t it can't reall
43790 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 y.** be opaque b
437a0 65 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 ecause it is use
437b0 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a d by macros..*/.
437c0 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 struct HashElem
437d0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 {. HashElem *ne
437e0 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20 xt, *prev;
437f0 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 /* Next and pre
43800 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 vious elements i
43810 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 n the table */.
43820 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 void *data;
43830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
43840 20 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 Data associated
43850 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 with this eleme
43860 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 nt */. const ch
43870 61 72 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b ar *pKey; int nK
43880 65 79 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f ey; /* Key asso
43890 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
438a0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a element */.};..
438b0 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 /*.** Access rou
438c0 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 tines. To delet
438d0 65 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c e, insert a NULL
438e0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c pointer..*/.SQL
438f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
43900 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 sqlite3HashInit
43910 28 48 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f (Hash*);.SQLITE_
43920 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
43930 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
43940 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 Hash*, const cha
43950 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 r *pKey, int nKe
43960 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b y, void *pData);
43970 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
43980 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
43990 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 hFind(const Hash
439a0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 *, const char *p
439b0 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a Key, int nKey);.
439c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
439d0 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 oid sqlite3HashC
439e0 6c 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a lear(Hash*);../*
439f0 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c .** Macros for l
43a00 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 ooping over all
43a10 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 elements of a ha
43a20 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 sh table. The i
43a30 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 diom is.** like
43a40 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 this:.**.** Ha
43a50 73 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 sh h;.** HashE
43a60 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e lem *p;.** ...
43a70 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 .** for(p=sqli
43a80 74 65 48 61 73 68 46 69 72 73 74 28 26 68 29 3b teHashFirst(&h);
43a90 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 p; p=sqliteHash
43aa0 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 Next(p)){.**
43ab0 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a SomeStructure *
43ac0 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 pData = sqliteHa
43ad0 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 shData(p);.**
43ae0 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e // do somethin
43af0 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 g with pData.**
43b00 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 }.*/.#define s
43b10 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 48 qliteHashFirst(H
43b20 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a ) ((H)->first).
43b30 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 #define sqliteHa
43b40 73 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 shNext(E) ((E)
43b50 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 ->next).#define
43b60 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 sqliteHashData(E
43b70 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a ) ((E)->data).
43b80 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 /* #define sqlit
43b90 65 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 eHashKey(E) (
43ba0 28 45 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f (E)->pKey) // NO
43bb0 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 T USED */./* #de
43bc0 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b fine sqliteHashK
43bd0 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e eysize(E) ((E)->
43be0 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53 nKey) // NOT US
43bf0 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d ED */../*.** Num
43c00 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
43c10 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a n a hash table.*
43c20 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c /./* #define sql
43c30 69 74 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 iteHashCount(H)
43c40 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f ((H)->count) //
43c50 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65 NOT USED */..#e
43c60 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f ndif /* _SQLITE_
43c70 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a HASH_H_ */../***
43c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
43c90 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a of hash.h ******
43ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
43cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
43ce0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
43cf0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
43d00 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
43d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
43d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
43d30 75 64 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 ude parse.h in t
43d40 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
43d50 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
43d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
43d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
43d80 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a n file parse.h *
43d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 **********/.#def
43dc0 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 ine TK_SEMI
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43de0 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
43df0 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 TK_EXPLAIN
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e10 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 2.#define TK
43e20 5f 51 55 45 52 59 20 20 20 20 20 20 20 20 20 20 _QUERY
43e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e40 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 3.#define TK_PL
43e50 41 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AN
43e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
43e70 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e #define TK_BEGIN
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e90 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 5.#de
43ea0 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 fine TK_TRANSACT
43eb0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 ION
43ec0 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 6.#defin
43ed0 65 20 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 e TK_DEFERRED
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ef0 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 7.#define T
43f00 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 K_IMMEDIATE
43f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43f20 20 20 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 8.#define TK_E
43f30 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 XCLUSIVE
43f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
43f50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d .#define TK_COMM
43f60 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IT
43f70 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 10.#d
43f80 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 efine TK_END
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43fa0 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 11.#defi
43fb0 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 ne TK_ROLLBACK
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43fd0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
43fe0 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 TK_SAVEPOINT
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44000 20 20 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 13.#define TK_
44010 52 45 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 RELEASE
44020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
44030 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 4.#define TK_TO
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44050 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 15.#
44060 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 define TK_TABLE
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44080 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 16.#def
44090 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 ine TK_CREATE
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440b0 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 17.#define
440c0 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 TK_IF
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440e0 20 20 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 18.#define TK
440f0 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 _NOT
44100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44110 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 19.#define TK_EX
44120 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20 ISTS
44130 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 20.
44140 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 #define TK_TEMP
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44160 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 21.#de
44170 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 fine TK_LP
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44190 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 22.#defin
441a0 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 e TK_RP
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
441c0 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 23.#define T
441d0 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20 K_AS
441e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
441f0 20 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 24.#define TK_C
44200 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 OMMA
44210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 25
44220 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 .#define TK_ID
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44240 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 26.#d
44250 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 efine TK_INDEXED
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44270 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 27.#defi
44280 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 ne TK_ABORT
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
442a0 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 28.#define
442b0 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 TK_AFTER
442c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
442d0 20 20 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 29.#define TK_
442e0 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 ANALYZE
442f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
44300 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43 0.#define TK_ASC
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44320 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 31.#
44330 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48 define TK_ATTACH
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44350 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 32.#def
44360 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20 ine TK_BEFORE
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44380 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 33.#define
44390 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20 TK_BY
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
443b0 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 34.#define TK
443c0 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20 _CASCADE
443d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
443e0 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 35.#define TK_CA
443f0 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ST
44400 20 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 36.
44410 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d #define TK_COLUM
44420 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 NKW
44430 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 37.#de
44440 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 fine TK_CONFLICT
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44460 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 38.#defin
44470 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20 e TK_DATABASE
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44490 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 39.#define T
444a0 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20 K_DESC
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
444c0 20 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 40.#define TK_D
444d0 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 ETACH
444e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 41
444f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48 .#define TK_EACH
44500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44510 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 42.#d
44520 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20 efine TK_FAIL
44530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44540 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 43.#defi
44550 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20 ne TK_FOR
44560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44570 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 44.#define
44580 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20 TK_IGNORE
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
445a0 20 20 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45.#define TK_
445b0 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 INITIALLY
445c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
445d0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 6.#define TK_INS
445e0 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 TEAD
445f0 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 47.#
44600 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b define TK_LIKE_K
44610 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 W
44620 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 48.#def
44630 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20 ine TK_MATCH
44640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44650 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 49.#define
44660 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20 TK_KEY
44670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44680 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 50.#define TK
44690 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20 20 _OF
446a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
446b0 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 51.#define TK_OF
446c0 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20 FSET
446d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 52.
446e0 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d #define TK_PRAGM
446f0 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 A
44700 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 53.#de
44710 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20 fine TK_RAISE
44720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44730 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 54.#defin
44740 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 e TK_REPLACE
44750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44760 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 55.#define T
44770 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20 20 K_RESTRICT
44780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44790 20 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 56.#define TK_R
447a0 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OW
447b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37 57
447c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 .#define TK_TRIG
447d0 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 GER
447e0 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 58.#d
447f0 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 efine TK_VACUUM
44800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44810 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 59.#defi
44820 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20 ne TK_VIEW
44830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44840 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 60.#define
44850 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20 TK_VIRTUAL
44860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44870 20 20 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 61.#define TK_
44880 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 REINDEX
44890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
448a0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 2.#define TK_REN
448b0 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20 20 AME
448c0 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 63.#
448d0 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f define TK_CTIME_
448e0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
448f0 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 64.#def
44900 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 ine TK_ANY
44910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44920 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 65.#define
44930 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20 TK_OR
44940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44950 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 66.#define TK
44960 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 _AND
44970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44980 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 67.#define TK_IS
44990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
449a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 68.
449b0 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 #define TK_BETWE
449c0 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EN
449d0 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 69.#de
449e0 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 fine TK_IN
449f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a00 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 70.#defin
44a10 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 e TK_ISNULL
44a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a30 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 71.#define T
44a40 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 K_NOTNULL
44a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a60 20 37 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 72.#define TK_N
44a70 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
44a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33 73
44a90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 .#define TK_EQ
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ab0 20 20 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 74.#d
44ac0 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 efine TK_GT
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ae0 20 20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 75.#defi
44af0 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 ne TK_LE
44b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b10 20 20 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 76.#define
44b20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20 TK_LT
44b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b40 20 20 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 77.#define TK_
44b50 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 GE
44b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
44b70 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 8.#define TK_ESC
44b80 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20 20 APE
44b90 20 20 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 79.#
44ba0 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 define TK_BITAND
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bc0 20 20 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 80.#def
44bd0 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 ine TK_BITOR
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bf0 20 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 81.#define
44c00 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20 TK_LSHIFT
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44c20 20 20 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 82.#define TK
44c30 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20 20 _RSHIFT
44c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44c50 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 83.#define TK_PL
44c60 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 US
44c70 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 84.
44c80 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 #define TK_MINUS
44c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ca0 20 20 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 85.#de
44cb0 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20 fine TK_STAR
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44cd0 20 20 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 86.#defin
44ce0 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 e TK_SLASH
44cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d00 20 20 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 87.#define T
44d10 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20 20 K_REM
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d30 20 38 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 88.#define TK_C
44d40 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20 20 ONCAT
44d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39 89
44d60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c .#define TK_COLL
44d70 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ATE
44d80 20 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 90.#d
44d90 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 efine TK_UMINUS
44da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44db0 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 91.#defi
44dc0 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 ne TK_UPLUS
44dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44de0 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 92.#define
44df0 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 TK_BITNOT
44e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44e10 20 20 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 93.#define TK_
44e20 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 STRING
44e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
44e40 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4.#define TK_JOI
44e50 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 N_KW
44e60 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 95.#
44e70 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 define TK_CONSTR
44e80 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 AINT
44e90 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 96.#def
44ea0 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 ine TK_DEFAULT
44eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ec0 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
44ed0 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 TK_NULL
44ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ef0 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 98.#define TK
44f00 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 _PRIMARY
44f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44f20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 99.#define TK_UN
44f30 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 20 IQUE
44f40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30 100
44f50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 .#define TK_CHEC
44f60 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
44f70 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 101.#
44f80 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 define TK_REFERE
44f90 4e 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20 NCES
44fa0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 102.#de
44fb0 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 fine TK_AUTOINCR
44fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44fd0 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 103.#defi
44fe0 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 ne TK_ON
44ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45000 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 104.#define
45010 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 TK_DELETE
45020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45030 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 105.#define T
45040 4b 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 K_UPDATE
45050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45060 20 31 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 106.#define TK_
45070 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 INSERT
45080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45090 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 07.#define TK_SE
450a0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
450b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38 108
450c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 .#define TK_DEFE
450d0 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 RRABLE
450e0 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 109.#
450f0 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 define TK_FOREIG
45100 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
45110 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 110.#de
45120 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 fine TK_DROP
45130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45140 20 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 111.#defi
45150 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 ne TK_UNION
45160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45170 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 112.#define
45180 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 TK_ALL
45190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
451a0 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 113.#define T
451b0 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20 K_EXCEPT
451c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
451d0 20 31 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 114.#define TK_
451e0 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20 INTERSECT
451f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45200 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 15.#define TK_SE
45210 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 LECT
45220 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36 116
45230 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 .#define TK_DIST
45240 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20 INCT
45250 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 117.#
45260 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 define TK_DOT
45270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45280 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 118.#de
45290 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 fine TK_FROM
452a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
452b0 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 119.#defi
452c0 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 ne TK_JOIN
452d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
452e0 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 120.#define
452f0 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 TK_USING
45300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45310 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 121.#define T
45320 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 K_ORDER
45330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45340 20 31 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 122.#define TK_
45350 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 GROUP
45360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45370 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 23.#define TK_HA
45380 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 VING
45390 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34 124
453a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 .#define TK_LIMI
453b0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
453c0 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 125.#
453d0 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 define TK_WHERE
453e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
453f0 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 126.#de
45400 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 fine TK_INTO
45410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45420 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 127.#defi
45430 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 ne TK_VALUES
45440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45450 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 128.#define
45460 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 TK_INTEGER
45470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45480 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 129.#define T
45490 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 K_FLOAT
454a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
454b0 20 31 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 130.#define TK_
454c0 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 BLOB
454d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
454e0 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 31.#define TK_RE
454f0 47 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20 GISTER
45500 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32 132
45510 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 .#define TK_VARI
45520 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 ABLE
45530 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 133.#
45540 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 define TK_CASE
45550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45560 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 134.#de
45570 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 fine TK_WHEN
45580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45590 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 135.#defi
455a0 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 ne TK_THEN
455b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
455c0 20 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 136.#define
455d0 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 TK_ELSE
455e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
455f0 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 137.#define T
45600 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 K_INDEX
45610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45620 20 31 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 138.#define TK_
45630 41 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20 ALTER
45640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45650 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 39.#define TK_AD
45660 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
45670 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30 140
45680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 .#define TK_TO_T
45690 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20 20 EXT
456a0 20 20 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 141.#
456b0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f define TK_TO_BLO
456c0 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 B
456d0 20 20 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 142.#de
456e0 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 fine TK_TO_NUMER
456f0 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IC
45700 20 20 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 143.#defi
45710 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 ne TK_TO_INT
45720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45730 20 20 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 144.#define
45740 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 TK_TO_REAL
45750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45760 20 20 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 145.#define T
45770 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20 K_END_OF_FILE
45780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45790 20 31 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 146.#define TK_
457a0 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20 20 ILLEGAL
457b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
457c0 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 50 47.#define TK_SP
457d0 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ACE
457e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 38 148
457f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c .#define TK_UNCL
45800 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20 20 OSED_STRING
45810 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 149.#
45820 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 49 define TK_FUNCTI
45830 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
45840 20 20 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 150.#de
45850 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 fine TK_COLUMN
45860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45870 20 20 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 151.#defi
45880 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 ne TK_AGG_FUNCTI
45890 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
458a0 20 20 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 152.#define
458b0 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 TK_AGG_COLUMN
458c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
458d0 20 20 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 153.#define T
458e0 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 20 K_CONST_FUNC
458f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45900 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 154../*********
45910 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 ***** End of par
45920 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a se.h ***********
45930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45950 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
45960 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
45970 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
45980 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
45990 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
459a0 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c ****/.#include <
459b0 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 stdio.h>.#includ
459c0 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e e <stdlib.h>.#in
459d0 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e clude <string.h>
459e0 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 .#include <asser
459f0 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 t.h>.#include <s
45a00 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 tddef.h>../*.**
45a10 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 If compiling for
45a20 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 a processor tha
45a30 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 t lacks floating
45a40 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a point support,.
45a50 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e ** substitute in
45a60 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 teger for floati
45a70 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 ng-point.*/.#ifd
45a80 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
45a90 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 LOATING_POINT.#
45aa0 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 define double sq
45ab0 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 lite_int64.# def
45ac0 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 ine LONGDOUBLE_T
45ad0 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 YPE sqlite_int64
45ae0 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 .# ifndef SQLITE
45af0 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 _BIG_DBL.# def
45b00 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 ine SQLITE_BIG_D
45b10 42 4c 20 28 30 78 37 66 66 66 66 66 66 66 66 66 BL (0x7fffffffff
45b20 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66 0a ffffff).# endif.
45b30 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
45b40 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
45b50 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 53 NCS 1.# define S
45b60 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
45b70 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 1.# undef SQLIT
45b80 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 E_MIXED_ENDIAN_6
45b90 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64 69 4BIT_FLOAT.#endi
45ba0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
45bb0 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 6e _BIG_DBL.# defin
45bc0 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c e SQLITE_BIG_DBL
45bd0 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a 0a (1e99).#endif..
45be0 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 /*.** OMIT_TEMPD
45bf0 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66 B is set to 1 if
45c00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d SQLITE_OMIT_TEM
45c10 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c 20 PDB is defined,
45c20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 72 or 0.** afterwar
45c30 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 6d d. Having this m
45c40 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 74 acro allows us t
45c50 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63 6f o cause the C co
45c60 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d mpiler .** to om
45c70 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 20 it code used by
45c80 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 68 TEMP tables with
45c90 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 65 out messy #ifnde
45ca0 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f f statements..*/
45cb0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
45cc0 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 69 MIT_TEMPDB.#defi
45cd0 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 31 ne OMIT_TEMPDB 1
45ce0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
45cf0 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 6e MIT_TEMPDB 0.#en
45d00 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 dif../*.** If th
45d10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
45d20 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 o is set to 1, t
45d30 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 hen NULL values
45d40 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a are considered.*
45d50 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e 20 * distinct when
45d60 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74 determining whet
45d70 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 65 her or not two e
45d80 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 73 ntries are the s
45d90 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 51 ame.** in a UNIQ
45da0 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 UE index. This
45db0 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 67 is the way Postg
45dc0 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 reSQL, Oracle, D
45dd0 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 B2, MySQL,.** OC
45de0 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 69 ELOT, and Firebi
45df0 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 rd all work. Th
45e00 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 70 e SQL92 spec exp
45e10 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 69 licitly says thi
45e20 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 20 s.** is the way
45e30 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 6f things are suppo
45e40 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a se to work..**.*
45e50 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * If the followi
45e60 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 ng macro is set
45e70 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 20 to 0, the NULLs
45e80 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 66 are indistinct f
45e90 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 69 or.** a UNIQUE i
45ea0 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 6d ndex. In this m
45eb0 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e 6c ode, you can onl
45ec0 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 y have a single
45ed0 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 6f NULL entry.** fo
45ee0 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 r a column decla
45ef0 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 69 red UNIQUE. Thi
45f00 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e 66 s is the way Inf
45f10 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 65 ormix and SQL Se
45f20 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f rver.** work..*/
45f30 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 .#define NULL_DI
45f40 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55 STINCT_FOR_UNIQU
45f50 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 E 1../*.** The "
45f60 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d file format" num
45f70 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ber is an intege
45f80 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
45f90 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a 2a ented whenever.*
45fa0 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c * the VDBE-level
45fb0 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61 file format cha
45fc0 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f nges. The follo
45fd0 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 wing macros defi
45fe0 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 65 ne the.** the de
45ff0 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61 fault file forma
46000 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61 t for new databa
46010 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69 ses and the maxi
46020 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0a mum file format.
46030 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 ** that the libr
46040 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a 2f ary can read..*/
46050 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
46060 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 MAX_FILE_FORMAT
46070 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 4.#ifndef SQLITE
46080 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f _DEFAULT_FILE_FO
46090 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 51 RMAT.# define SQ
460a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
460b0 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 69 E_FORMAT 1.#endi
460c0 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 f../*.** Provide
460d0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 a default value
460e0 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50 for SQLITE_TEMP
460f0 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69 _STORE in case i
46100 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 t is not specifi
46110 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d ed.** on the com
46120 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66 mand-line.*/.#if
46130 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
46140 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20 _STORE.# define
46150 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
46160 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a E 1.#endif../*.*
46170 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64 * GCC does not d
46180 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74 efine the offset
46190 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65 of() macro so we
461a0 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69 'll have to do i
461b0 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a t.** ourselves..
461c0 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65 */.#ifndef offse
461d0 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73 tof.#define offs
461e0 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46 etof(STRUCTURE,F
461f0 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68 IELD) ((int)((ch
46200 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45 ar*)&((STRUCTURE
46210 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65 *)0)->FIELD)).#e
46220 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 ndif../*.** Chec
46230 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 k to see if this
46240 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42 machine uses EB
46250 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c CDIC. (Yes, bel
46260 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f ieve it or.** no
46270 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69 t, there are sti
46280 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20 ll machines out
46290 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45 there that use E
462a0 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 BCDIC.).*/.#if '
462b0 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64 A' == '\301'.# d
462c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43 efine SQLITE_EBC
462d0 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 DIC 1.#else.# de
462e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49 fine SQLITE_ASCI
462f0 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a I 1.#endif../*.*
46300 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e * Integers of kn
46310 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73 own sizes. Thes
46320 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74 e typedefs might
46330 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68 change for arch
46340 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65 itectures.** whe
46350 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72 re the sizes ver
46360 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72 y. Preprocessor
46370 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69 macros are avai
46380 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68 lable so that th
46390 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62 e.** types can b
463a0 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72 e conveniently r
463b0 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 edefined at comp
463c0 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20 ile-type. Like
463d0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 this:.**.**
463e0 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54 cc '-DUINTPT
463f0 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 R_TYPE=long long
46400 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 int' ....*/.#if
46410 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 ndef UINT32_TYPE
46420 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 .# ifdef HAVE_UI
46430 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65 NT32_T.# define
46440 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e UINT32_TYPE uin
46450 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 t32_t.# else.#
46460 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 define UINT32_TY
46470 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a PE unsigned int.
46480 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
46490 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59 ifndef UINT16_TY
464a0 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f PE.# ifdef HAVE_
464b0 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 UINT16_T.# defi
464c0 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ne UINT16_TYPE u
464d0 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 int16_t.# else.#
464e0 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f define UINT16_
464f0 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68 TYPE unsigned sh
46500 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a ort int.# endif.
46510 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 #endif.#ifndef I
46520 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 NT16_TYPE.# ifde
46530 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23 f HAVE_INT16_T.#
46540 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 define INT16_T
46550 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c YPE int16_t.# el
46560 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 se.# define INT
46570 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e 16_TYPE short in
46580 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 t.# endif.#endif
46590 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54 .#ifndef UINT8_T
465a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
465b0 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 _UINT8_T.# defi
465c0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69 ne UINT8_TYPE ui
465d0 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 nt8_t.# else.#
465e0 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 define UINT8_TYP
465f0 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a E unsigned char.
46600 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
46610 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 ifndef INT8_TYPE
46620 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e .# ifdef HAVE_IN
46630 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49 T8_T.# define I
46640 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a NT8_TYPE int8_t.
46650 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
46660 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65 INT8_TYPE signe
46670 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 d char.# endif.#
46680 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f endif.#ifndef LO
46690 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20 NGDOUBLE_TYPE.#
466a0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c define LONGDOUBL
466b0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62 E_TYPE long doub
466c0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 le.#endif.typede
466d0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 f sqlite_int64 i
466e0 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64; /*
466f0 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 8-byte signed in
46700 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 teger */.typedef
46710 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75 sqlite_uint64 u
46720 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38 64; /* 8
46730 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 -byte unsigned i
46740 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 nteger */.typede
46750 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33 f UINT32_TYPE u3
46760 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2; /*
46770 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 4-byte unsigned
46780 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
46790 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ef UINT16_TYPE u
467a0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 16; /*
467b0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 2-byte unsigned
467c0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
467d0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69 def INT16_TYPE i
467e0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 16; /
467f0 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 2-byte signed
46800 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
46810 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38 ef UINT8_TYPE u8
46820 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
46830 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 1-byte unsigned
46840 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
46850 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38 def INT8_TYPE i8
46860 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
46870 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 1-byte signed
46880 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a integer */../*.*
46890 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 * Macros to dete
468a0 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 rmine whether th
468b0 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 e machine is big
468c0 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 or little endia
468d0 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 n,.** evaluated
468e0 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 at runtime..*/.#
468f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 ifdef SQLITE_AMA
46900 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 LGAMATION.SQLITE
46910 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
46920 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
46930 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 1;.#else.SQLITE_
46940 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e PRIVATE const in
46950 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 t sqlite3one;.#e
46960 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 ndif.#if defined
46970 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
46980 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
46990 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c efined(_M_IX86)\
469a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
469b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
469c0 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
469d0 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4) || defined(__
469e0 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 x86_64__).# defi
469f0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
46a00 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e IAN 0.# defin
46a10 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 e SQLITE_LITTLEE
46a20 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 NDIAN 1.# define
46a30 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
46a40 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 IVE SQLITE_UTF1
46a50 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6LE.#else.# defi
46a60 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
46a70 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a IAN (*(char *
46a80 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d )(&sqlite3one)==
46a90 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 0).# define SQLI
46aa0 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 TE_LITTLEENDIAN
46ab0 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 (*(char *)(&sqli
46ac0 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 te3one)==1).# de
46ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
46ae0 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f 6NATIVE (SQLITE_
46af0 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 BIGENDIAN?SQLITE
46b00 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f _UTF16BE:SQLITE_
46b10 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a UTF16LE).#endif.
46b20 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 ./*.** Constants
46b30 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74 for the largest
46b40 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f and smallest po
46b50 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69 ssible 64-bit si
46b60 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a gned integers..*
46b70 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61 * These macros a
46b80 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77 re designed to w
46b90 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e ork correctly on
46ba0 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64 both 32-bit and
46bb0 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 64-bit.** compi
46bc0 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 lers..*/.#define
46bd0 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20 LARGEST_INT64
46be0 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69 (0xffffffff|(((i
46bf0 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 64)0x7fffffff)<<
46c00 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 32)).#define SMA
46c10 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69 LLEST_INT64 (((i
46c20 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 64)-1) - LARGEST
46c30 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 _INT64)../* .**
46c40 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 Round up a numbe
46c50 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 r to the next la
46c60 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 rger multiple of
46c70 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 8. This is use
46c80 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d d.** to force 8-
46c90 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f byte alignment o
46ca0 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 n 64-bit archite
46cb0 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 ctures..*/.#defi
46cc0 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20 ne ROUND8(x)
46cd0 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f (((x)+7)&~7)../
46ce0 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 *.** Round down
46cf0 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d to the nearest m
46d00 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a ultiple of 8.*/.
46d10 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 #define ROUNDDOW
46d20 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a N8(x) ((x)&~7)..
46d30 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61 /*.** Assert tha
46d40 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20 t the pointer X
46d50 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e is aligned to an
46d60 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8-byte boundary
46d70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 ..*/.#define EIG
46d80 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
46d90 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a T(X) ((((char*
46da0 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29 )(X) - (char*)0)
46db0 26 37 29 3d 3d 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 &7)==0)../*.** A
46dc0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
46dd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
46de0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f cture is used to
46df0 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d store the busy-
46e00 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 handler.** callb
46e10 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 ack for a given
46e20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a sqlite handle. .
46e30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
46e40 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d .busyHandler mem
46e50 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ber of the sqlit
46e60 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e e struct contain
46e70 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 s the busy.** ca
46e80 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 llback for the d
46e90 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 atabase handle.
46ea0 45 61 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 Each pager opene
46eb0 64 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 d via the sqlite
46ec0 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 .** handle is pa
46ed0 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 ssed a pointer t
46ee0 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e o sqlite.busyHan
46ef0 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 dler. The busy-h
46f00 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 andler.** callba
46f10 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ck is currently
46f20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f invoked only fro
46f30 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 m within pager.c
46f40 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
46f50 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 uct BusyHandler
46f60 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 BusyHandler;.str
46f70 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 uct BusyHandler
46f80 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 {. int (*xFunc)
46f90 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f (void *,int); /
46fa0 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 * The busy callb
46fb0 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ack */. void *p
46fc0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
46fd0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
46fe0 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 to busy callbac
46ff0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 k */. int nBusy
47000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
47010 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 /* Incremented
47020 20 77 69 74 68 20 65 61 63 68 20 62 75 73 79 20 with each busy
47030 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a call */.};../*.*
47040 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 * Name of the ma
47050 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 ster database ta
47060 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 ble. The master
47070 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a database table.
47080 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 ** is a special
47090 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 table that holds
470a0 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 the names and a
470b0 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c ttributes of all
470c0 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 .** user tables
470d0 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a and indices..*/.
470e0 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e #define MASTER_N
470f0 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 AME "sqlit
47100 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e e_master".#defin
47110 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 e TEMP_MASTER_NA
47120 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 ME "sqlite_temp
47130 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 _master"../*.**
47140 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 The root-page of
47150 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
47160 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 base table..*/.#
47170 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f define MASTER_RO
47180 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a OT 1../*.*
47190 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
471a0 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a e schema table..
471b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d */.#define SCHEM
471c0 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f A_TABLE(x) ((!O
471d0 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d MIT_TEMPDB)&&(x=
471e0 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f =1)?TEMP_MASTER_
471f0 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 NAME:MASTER_NAME
47200 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 )../*.** A conve
47210 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 nience macro tha
47220 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 t returns the nu
47230 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
47240 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e in.** an array.
47250 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 .*/.#define Arra
47260 79 53 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e ySize(X) ((in
47270 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a t)(sizeof(X)/siz
47280 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a eof(X[0])))../*.
47290 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
472a0 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 value as a dest
472b0 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 ructor means to
472c0 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 use sqlite3DbFre
472d0 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 e()..** This is
472e0 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 an internal exte
472f0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f nsion to SQLITE_
47300 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 STATIC and SQLIT
47310 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a E_TRANSIENT..*/.
47320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
47330 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 YNAMIC ((sqlit
47340 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 e3_destructor_ty
47350 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 pe)sqlite3DbFree
47360 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 )../*.** When SQ
47370 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 LITE_OMIT_WSD is
47380 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 defined, it mea
47390 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67 ns that the targ
473a0 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 et platform does
473b0 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 .** not support
473c0 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 Writable Static
473d0 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20 Data (WSD) such
473e0 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 as global and st
473f0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a atic variables..
47400 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 ** All variables
47410 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20 must either be
47420 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 on the stack or
47430 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f dynamically allo
47440 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 cated from.** th
47450 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 e heap. When WS
47460 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 D is unsupported
47470 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 , the variable d
47480 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 eclarations scat
47490 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 tered.** through
474a0 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 out the SQLite c
474b0 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 ode must become
474c0 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 constants instea
474d0 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 d. The SQLITE_W
474e0 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 SD.** macro is u
474f0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 sed for this pur
47500 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 pose. And inste
47510 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e ad of referencin
47520 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a g the variable.*
47530 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 * directly, we u
47540 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 se its constant
47550 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b as a key to look
47560 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 up the run-time
47570 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 allocated.** buf
47580 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 fer that holds r
47590 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 eal variable. T
475a0 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 he constant is a
475b0 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 lso the initiali
475c0 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 zer.** for the r
475d0 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 un-time allocate
475e0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 d buffer..**.**
475f0 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 In the usual cas
47600 65 20 77 68 65 72 65 20 57 53 44 20 69 73 20 73 e where WSD is s
47610 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 upported, the SQ
47620 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f LITE_WSD and GLO
47630 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 BAL.** macros be
47640 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 come no-ops and
47650 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 have zero perfor
47660 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f mance impact..*/
47670 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
47680 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e MIT_WSD. #defin
47690 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e e SQLITE_WSD con
476a0 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f st. #define GLO
476b0 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 BAL(t,v) (*(t*)s
476c0 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 qlite3_wsd_find(
476d0 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a (void*)&(v), siz
476e0 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 eof(v))). #defi
476f0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ne sqlite3Global
47700 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 Config GLOBAL(st
47710 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 ruct Sqlite3Conf
47720 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 ig, sqlite3Confi
47730 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 g).SQLITE_API
47740 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f int sqlite3_wsd_
47750 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 init(int N, int
47760 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 J);.SQLITE_API
47770 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 void *sqlite3_w
47780 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c sd_find(void *K,
47790 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 int L);.#else.
477a0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
477b0 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 WSD . #define G
477c0 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 LOBAL(t,v) v. #
477d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c define sqlite3Gl
477e0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 obalConfig sqlit
477f0 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a e3Config.#endif.
47800 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
47810 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 wing macros are
47820 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 used to suppress
47830 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e compiler warnin
47840 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b gs and to.** mak
47850 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 e it clear to hu
47860 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e man readers when
47870 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 a function para
47880 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 meter is deliber
47890 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 ately .** left u
478a0 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 nused within the
478b0 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 body of a funct
478c0 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c ion. This usuall
478d0 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a y happens when.*
478e0 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 * a function is
478f0 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e called via a fun
47900 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 ction pointer. F
47910 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a or example the .
47920 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
47930 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 n of an SQL aggr
47940 65 67 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 egate step callb
47950 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 ack may not use
47960 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 the.** parameter
47970 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
47980 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
47990 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 nts passed to th
479a0 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 e aggregate,.**
479b0 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 if it knows that
479c0 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 this is enforce
479d0 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a d elsewhere..**.
479e0 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 ** When a functi
479f0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 on parameter is
47a00 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 not used at all
47a10 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 within the body
47a20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a of a function,.*
47a30 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c * it is generall
47a40 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 y named "NotUsed
47a50 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 " or "NotUsed2"
47a60 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 to make things e
47a70 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 ven clearer..**
47a80 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d However, these m
47a90 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 acros may also b
47aa0 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 e used to suppre
47ab0 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 ss warnings rela
47ac0 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 ted to.** parame
47ad0 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 ters that may or
47ae0 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 may not be used
47af0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f depending on co
47b00 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e mpilation option
47b10 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c s..** For exampl
47b20 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 e those paramete
47b30 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 rs only used in
47b40 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
47b50 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a nts. In these.**
47b60 20 63 61 73 65 73 20 74 68 65 20 70 61 72 61 6d cases the param
47b70 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 eters are named
47b80 61 73 20 70 65 72 20 74 68 65 20 75 73 75 61 6c as per the usual
47b90 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f conventions..*/
47ba0 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f .#define UNUSED_
47bb0 50 41 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f PARAMETER(x) (vo
47bc0 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 id)(x).#define U
47bd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
47be0 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 (x,y) UNUSED_PAR
47bf0 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 AMETER(x),UNUSED
47c00 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f _PARAMETER(y)../
47c10 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 *.** Forward ref
47c20 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 erences to struc
47c30 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 tures.*/.typedef
47c40 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 struct AggInfo
47c50 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 AggInfo;.typedef
47c60 20 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 struct AuthCont
47c70 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b ext AuthContext;
47c80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
47c90 42 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74 Bitvec Bitvec;.t
47ca0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f ypedef struct Ro
47cb0 77 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70 wSet RowSet;.typ
47cc0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c edef struct Coll
47cd0 53 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 Seq CollSeq;.typ
47ce0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75 edef struct Colu
47cf0 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 mn Column;.typed
47d00 65 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b ef struct Db Db;
47d10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
47d20 53 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74 Schema Schema;.t
47d30 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
47d40 70 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66 pr Expr;.typedef
47d50 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
47d60 20 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64 ExprList;.typed
47d70 65 66 20 73 74 72 75 63 74 20 46 4b 65 79 20 46 ef struct FKey F
47d80 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Key;.typedef str
47d90 75 63 74 20 46 75 6e 63 44 65 66 20 46 75 6e 63 uct FuncDef Func
47da0 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Def;.typedef str
47db0 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20 uct FuncDefHash
47dc0 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74 79 70 FuncDefHash;.typ
47dd0 65 64 65 66 20 73 74 72 75 63 74 20 49 64 4c 69 edef struct IdLi
47de0 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70 65 64 st IdList;.typed
47df0 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78 20 ef struct Index
47e00 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66 20 73 Index;.typedef s
47e10 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b truct KeyClass K
47e20 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 eyClass;.typedef
47e30 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
47e40 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 KeyInfo;.typedef
47e50 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 struct Lookasid
47e60 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70 e Lookaside;.typ
47e70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b edef struct Look
47e80 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 asideSlot Lookas
47e90 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66 ideSlot;.typedef
47ea0 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d struct Module M
47eb0 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 odule;.typedef s
47ec0 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 truct NameContex
47ed0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 t NameContext;.t
47ee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
47ef0 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64 rse Parse;.typed
47f00 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f ef struct Savepo
47f10 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74 int Savepoint;.t
47f20 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 ypedef struct Se
47f30 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70 lect Select;.typ
47f40 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c edef struct SrcL
47f50 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 ist SrcList;.typ
47f60 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 edef struct StrA
47f70 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 ccum StrAccum;.t
47f80 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 ypedef struct Ta
47f90 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 ble Table;.typed
47fa0 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c ef struct TableL
47fb0 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 ock TableLock;.t
47fc0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f ypedef struct To
47fd0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 ken Token;.typed
47fe0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
47ff0 72 53 74 61 63 6b 20 54 72 69 67 67 65 72 53 74 rStack TriggerSt
48000 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ack;.typedef str
48010 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
48020 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70 TriggerStep;.typ
48030 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 edef struct Trig
48040 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 ger Trigger;.typ
48050 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 61 edef struct Unpa
48060 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63 ckedRecord Unpac
48070 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64 kedRecord;.typed
48080 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 ef struct Walker
48090 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66 Walker;.typedef
480a0 20 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 struct WherePla
480b0 6e 20 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70 n WherePlan;.typ
480c0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 edef struct Wher
480d0 65 49 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b eInfo WhereInfo;
480e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
480f0 57 68 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 WhereLevel Where
48100 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 Level;../*.** De
48110 66 65 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 fer sourcing vdb
48120 65 2e 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 e.h and btree.h
48130 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 until after the
48140 22 75 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 "u8" and .** "Bu
48150 73 79 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 syHandler" typed
48160 65 66 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f efs. vdbe.h also
48170 20 72 65 71 75 69 72 65 73 20 61 20 66 65 77 20 requires a few
48180 6f 66 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a of the opaque.**
48190 20 70 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 pointer types (
481a0 69 2e 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 i.e. FuncDef) de
481b0 66 69 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a fined above..*/.
481c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
481d0 49 6e 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 Include btree.h
481e0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
481f0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
48200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
48210 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
48220 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 Begin file btree
48230 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
48240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
48260 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
48270 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
48280 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
48290 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
482a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
482b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
482c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
482d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
482e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
482f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
48300 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
48310 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
48320 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
48330 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
48340 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
48350 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
48360 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
48370 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
48380 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
48390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 ******.** This h
483e0 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e eader file defin
483f0 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 es the interface
48400 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
48410 20 42 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 B-Tree file.**
48420 73 75 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 subsystem. See
48430 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 comments in the
48440 73 6f 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 source code for
48450 61 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 a detailed descr
48460 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 iption.** of wha
48470 74 20 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 t each interface
48480 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a routine does..*
48490 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 62 *.** @(#) $Id: b
484a0 74 72 65 65 2e 68 2c 76 20 31 2e 31 31 34 20 32 tree.h,v 1.114 2
484b0 30 30 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 009/05/04 11:42:
484c0 33 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 30 danielk1977 E
484d0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
484e0 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e _BTREE_H_.#defin
484f0 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 e _BTREE_H_../*
48500 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e TODO: This defin
48510 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e ition is just in
48520 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20 cluded so other
48530 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e modules compile.
48540 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 It.** needs to
48550 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f be revisited..*/
48560 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
48570 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a N_BTREE_META 10.
48580 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 ./*.** If define
48590 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 d as non-zero, a
485a0 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e uto-vacuum is en
485b0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
485c0 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 . Otherwise.** i
485d0 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64 t must be turned
485e0 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74 on for each dat
485f0 61 62 61 73 65 20 75 73 69 6e 67 20 22 50 52 41 abase using "PRA
48600 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 GMA auto_vacuum
48610 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 = 1"..*/.#ifndef
48620 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
48630 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 AUTOVACUUM. #de
48640 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
48650 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 ULT_AUTOVACUUM 0
48660 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
48670 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
48680 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20 M_NONE 0
48690 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 /* Do not do aut
486a0 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 o-vacuum */.#def
486b0 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 ine BTREE_AUTOVA
486c0 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 CUUM_FULL 1
486d0 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 /* Do full au
486e0 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 to-vacuum */.#de
486f0 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 fine BTREE_AUTOV
48700 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 ACUUM_INCR 2
48710 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
48720 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a al vacuum */../*
48730 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c .** Forward decl
48740 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 arations of stru
48750 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 cture.*/.typedef
48760 20 73 74 72 75 63 74 20 42 74 72 65 65 20 42 74 struct Btree Bt
48770 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ree;.typedef str
48780 75 63 74 20 42 74 43 75 72 73 6f 72 20 42 74 43 uct BtCursor BtC
48790 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 ursor;.typedef s
487a0 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 42 truct BtShared B
487b0 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64 65 66 tShared;.typedef
487c0 20 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 struct BtreeMut
487d0 65 78 41 72 72 61 79 20 42 74 72 65 65 4d 75 74 exArray BtreeMut
487e0 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 exArray;../*.**
487f0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 72 This structure r
48800 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20 74 68 ecords all of th
48810 65 20 42 74 72 65 65 73 20 74 68 61 74 20 6e 65 e Btrees that ne
48820 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 ed to hold.** a
48830 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77 65 20 mutex before we
48840 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56 64 62 enter sqlite3Vdb
48850 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 42 74 eExec(). The Bt
48860 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72 65 20 rees are.** are
48870 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72 65 65 placed in aBtree
48880 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 [] in order of a
48890 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 Btree[]->pBt. T
488a0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63 hat way,.** we c
488b0 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61 an always lock a
488c0 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 nd unlock them a
488d0 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 ll quickly..*/.s
488e0 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 truct BtreeMutex
488f0 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d Array {. int nM
48900 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20 2a 61 utex;. Btree *a
48910 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Btree[SQLITE_MAX
48920 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b _ATTACHED+1];.};
48930 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ...SQLITE_PRIVAT
48940 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48950 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 eeOpen(. const
48960 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
48970 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 /* Name of da
48980 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f tabase file to o
48990 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 pen */. sqlite3
489a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
489b0 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 /* Associated
489c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
489d0 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a ion */. Btree *
489e0 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *,
489f0 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e /* Return open
48a00 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a Btree* here */.
48a10 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
48a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
48a30 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 ags */. int vfs
48a40 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
48a50 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 /* Flags passe
48a60 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56 46 53 d through to VFS
48a70 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 open */.);../*
48a80 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 The flags parame
48a90 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 ter to sqlite3Bt
48aa0 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 reeOpen can be t
48ab0 68 65 20 62 69 74 77 69 73 65 20 6f 72 20 6f 66 he bitwise or of
48ac0 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e the.** followin
48ad0 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 g values..**.**
48ae0 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c NOTE: These val
48af0 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 ues must match t
48b00 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
48b10 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69 PAGER_ values i
48b20 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f n.** pager.h..*/
48b30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4f .#define BTREE_O
48b40 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20 MIT_JOURNAL 1
48b50 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f /* Do not use jo
48b60 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d urnal. No argum
48b70 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ent */.#define B
48b80 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b TREE_NO_READLOCK
48b90 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65 2 /* Omit re
48ba0 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f adlocks on reado
48bb0 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65 nly files */.#de
48bc0 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52 fine BTREE_MEMOR
48bd0 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 49 Y 4 /* I
48be0 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f n-memory DB. No
48bf0 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 argument */.#de
48c00 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 4f fine BTREE_READO
48c10 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a 20 4f NLY 8 /* O
48c20 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 pen the database
48c30 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f in read-only mo
48c40 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 de */.#define BT
48c50 52 45 45 5f 52 45 41 44 57 52 49 54 45 20 20 20 REE_READWRITE
48c60 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 16 /* Open for
48c70 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e both reading an
48c80 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65 d writing */.#de
48c90 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45 41 54 fine BTREE_CREAT
48ca0 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a 20 43 E 32 /* C
48cb0 72 65 61 74 65 20 74 68 65 20 64 61 74 61 62 61 reate the databa
48cc0 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f se if it does no
48cd0 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 t exist */..SQLI
48ce0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
48cf0 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
48d00 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
48d10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48d20 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 ite3BtreeSetCach
48d30 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74 eSize(Btree*,int
48d40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
48d50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48d60 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c eeSetSafetyLevel
48d70 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 (Btree*,int,int)
48d80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
48d90 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
48da0 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 eSyncDisabled(Bt
48db0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
48dc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48dd0 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 3BtreeSetPageSiz
48de0 65 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 e(Btree*,int,int
48df0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
48e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48e10 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 3BtreeGetPageSiz
48e20 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 e(Btree*);.SQLIT
48e30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
48e40 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 lite3BtreeMaxPag
48e50 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e eCount(Btree*,in
48e60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
48e70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
48e80 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 reeGetReserve(Bt
48e90 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
48ea0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48eb0 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 3BtreeSetAutoVac
48ec0 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 uum(Btree *, int
48ed0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
48ee0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48ef0 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeGetAutoVacuum(
48f00 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Btree *);.SQLITE
48f10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
48f20 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 ite3BtreeBeginTr
48f30 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b ans(Btree*,int);
48f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
48f50 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
48f60 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 CommitPhaseOne(B
48f70 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 tree*, const cha
48f80 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c r *zMaster);.SQL
48f90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
48fa0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
48fb0 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 itPhaseTwo(Btree
48fc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
48fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
48fe0 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a reeCommit(Btree*
48ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
49000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
49010 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
49020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
49030 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49040 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 reeBeginStmt(Btr
49050 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
49060 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
49070 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
49080 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 able(Btree*, int
49090 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 *, int flags);.S
490a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
490b0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
490c0 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b InTrans(Btree*);
490d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
490e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
490f0 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 IsInReadTrans(Bt
49100 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
49110 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49120 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 3BtreeIsInBackup
49130 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
49140 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
49150 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
49160 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 a(Btree *, int,
49170 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 void(*)(void *))
49180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
49190 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
491a0 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 eSchemaLocked(Bt
491b0 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ree *);.SQLITE_P
491c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
491d0 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 e3BtreeLockTable
491e0 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 75 (Btree *, int, u
491f0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 8);.SQLITE_PRIVA
49200 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49210 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 reeSavepoint(Btr
49220 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b ee *, int, int);
49230 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
49240 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
49250 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
49260 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 name(Btree *);.S
49270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
49280 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
49290 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 3BtreeGetJournal
492a0 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 name(Btree *);.S
492b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
492c0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
492d0 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 2c 20 pyFile(Btree *,
492e0 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 Btree *);..SQLIT
492f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
49300 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 lite3BtreeIncrVa
49310 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a cuum(Btree *);..
49320 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 /* The flags par
49330 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
49340 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
49350 65 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 e can be the bit
49360 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 wise OR.** of th
49370 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 e following flag
49380 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 s:.*/.#define BT
49390 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20 20 31 REE_INTKEY 1
493a0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
493b0 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73 69 67 only 64-bit sig
493c0 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ned integer keys
493d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
493e0 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32 20 20 E_ZERODATA 2
493f0 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6b /* Table has k
49400 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 eys only - no da
49410 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 ta */.#define BT
49420 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20 20 34 REE_LEAFDATA 4
49430 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74 6f 72 /* Data stor
49440 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c ed in leaves onl
49450 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b y. Implies INTK
49460 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 EY */..SQLITE_PR
49470 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49480 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 3BtreeDropTable(
49490 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Btree*, int, int
494a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
494b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
494c0 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 reeClearTable(Bt
494d0 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 ree*, int, int*)
494e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
494f0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
49500 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 2a 2c eGetMeta(Btree*,
49510 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70 int idx, u32 *p
49520 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f 50 Value);.SQLITE_P
49530 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
49540 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
49550 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69 64 a(Btree*, int id
49560 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b 0a 53 x, u32 value);.S
49570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
49580 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 id sqlite3BtreeT
49590 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 ripAllCursors(Bt
495a0 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c ree*, int);..SQL
495b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
495c0 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
495d0 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20 or(. Btree*,
495e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
495f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54 /* BT
49600 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ree containing t
49610 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a able to open */.
49620 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 int iTable,
49630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49640 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
49650 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a of root page */.
49660 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 int wrFlag,
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49680 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 /* 1 for
49690 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20 writing. 0 for
496a0 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 read-only */. s
496b0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 truct KeyInfo*,
496c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
496d0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
496e0 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65 ument to compare
496f0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 function */. B
49700 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
49710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49720 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
49730 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74 72 write cursor str
49740 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c ucture */.);.SQL
49750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
49760 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
49770 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 orSize(void);..S
49780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
49790 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
497a0 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 oseCursor(BtCurs
497b0 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 or*);.SQLITE_PRI
497c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
497d0 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 BtreeMoveto(. B
497e0 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63 6f 6e 73 tCursor*,. cons
497f0 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 0a 20 20 t void *pKey,.
49800 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 i64 nKey,. int
49810 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 65 bias,. int *pRe
49820 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 s.);.SQLITE_PRIV
49830 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
49840 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
49850 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 2a 2c ed(. BtCursor*,
49860 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
49870 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 69 36 34 d *pUnKey,. i64
49880 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e 74 20 62 intKey,. int b
49890 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73 ias,. int *pRes
498a0 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
498b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
498c0 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
498d0 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 d(BtCursor*, int
498e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
498f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49900 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 reeDelete(BtCurs
49910 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 or*);.SQLITE_PRI
49920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
49930 42 74 72 65 65 49 6e 73 65 72 74 28 42 74 43 75 BtreeInsert(BtCu
49940 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 rsor*, const voi
49950 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 d *pKey, i64 nKe
49960 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
49970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49980 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 const void
49990 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 *pData, int nDat
499a0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 a,.
499b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
499c0 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 int nZero,
499d0 69 6e 74 20 62 69 61 73 2c 20 69 6e 74 20 73 65 int bias, int se
499e0 65 6b 52 65 73 75 6c 74 29 3b 0a 53 51 4c 49 54 ekResult);.SQLIT
499f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
49a00 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 lite3BtreeFirst(
49a10 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
49a20 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pRes);.SQLITE_PR
49a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49a40 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 3BtreeLast(BtCur
49a50 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 sor*, int *pRes)
49a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
49a70 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
49a80 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 2a 2c eNext(BtCursor*,
49a90 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c int *pRes);.SQL
49aa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
49ab0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 sqlite3BtreeEof(
49ac0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 BtCursor*);.SQLI
49ad0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
49ae0 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 qlite3BtreeFlags
49af0 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c (BtCursor*);.SQL
49b00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
49b10 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
49b20 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a 2c 20 ious(BtCursor*,
49b30 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 int *pRes);.SQLI
49b40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
49b50 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
49b60 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 ze(BtCursor*, i6
49b70 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 4 *pSize);.SQLIT
49b80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
49b90 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 42 74 lite3BtreeKey(Bt
49ba0 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 Cursor*, u32 off
49bb0 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f set, u32 amt, vo
49bc0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
49bd0 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 VATE sqlite3 *sq
49be0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
49bf0 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73 6f Db(const BtCurso
49c00 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
49c10 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
49c20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 sqlite3BtreeKeyF
49c30 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 etch(BtCursor*,
49c40 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 int *pAmt);.SQLI
49c50 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
49c60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
49c70 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 reeDataFetch(BtC
49c80 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d ursor*, int *pAm
49c90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
49ca0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49cb0 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 reeDataSize(BtCu
49cc0 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a rsor*, u32 *pSiz
49cd0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
49ce0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49cf0 72 65 65 44 61 74 61 28 42 74 43 75 72 73 6f 72 reeData(BtCursor
49d00 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 *, u32 offset, u
49d10 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 32 amt, void*);.
49d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
49d30 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
49d40 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 SetCachedRowid(B
49d50 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 tCursor*, sqlite
49d60 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 3_int64);.SQLITE
49d70 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
49d80 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 _int64 sqlite3Bt
49d90 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 reeGetCachedRowi
49da0 64 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 d(BtCursor*);..S
49db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
49dc0 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
49dd0 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 42 IntegrityCheck(B
49de0 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f tree*, int *aRoo
49df0 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e t, int nRoot, in
49e00 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 t, int*);.SQLITE
49e10 5f 50 52 49 56 41 54 45 20 73 74 72 75 63 74 20 _PRIVATE struct
49e20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74 Pager *sqlite3Bt
49e30 72 65 65 50 61 67 65 72 28 42 74 72 65 65 2a 29 reePager(Btree*)
49e40 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
49e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
49e60 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72 73 eePutData(BtCurs
49e70 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c or*, u32 offset,
49e80 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 u32 amt, void*)
49e90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
49ea0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
49eb0 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 eeCacheOverflow(
49ec0 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c BtCursor *);.SQL
49ed0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
49ee0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
49ef0 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f arCursor(BtCurso
49f00 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 r *);..#ifndef S
49f10 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 QLITE_OMIT_BTREE
49f20 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 COUNT.SQLITE_PRI
49f30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
49f40 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 BtreeCount(BtCur
49f50 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 sor *, i64 *);.#
49f60 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
49f70 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
49f80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
49f90 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
49fa0 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 nfo(BtCursor*, i
49fb0 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 nt*, int);.SQLIT
49fc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
49fd0 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
49fe0 72 4c 69 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 rList(Btree*);.#
49ff0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
4a000 77 65 20 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 we are not using
4a010 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 shared cache, t
4a020 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hen there is no
4a030 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d need to.** use m
4a040 75 74 65 78 65 73 20 74 6f 20 61 63 63 65 73 73 utexes to access
4a050 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 the BtShared st
4a060 72 75 63 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 ructures. So ma
4a070 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 ke the.** Enter
4a080 61 6e 64 20 4c 65 61 76 65 20 70 72 6f 63 65 64 and Leave proced
4a090 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a ures no-ops..*/.
4a0a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
4a0b0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
4a0c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4a0d0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 void sqlite3Bt
4a0e0 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 reeEnter(Btree*)
4a0f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4a100 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4a110 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c treeEnterAll(sql
4a120 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 ite3*);.#else.#
4a130 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4a140 72 65 65 45 6e 74 65 72 28 58 29 20 0a 23 20 64 reeEnter(X) .# d
4a150 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
4a160 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a 23 65 eeEnterAll(X).#e
4a170 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e ndif..#if !defin
4a180 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
4a190 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
4a1a0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
4a1b0 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 E.SQLITE_PRIVATE
4a1c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4a1d0 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a treeLeave(Btree*
4a1e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4a1f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4a200 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 BtreeEnterCursor
4a210 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c (BtCursor*);.SQL
4a220 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4a230 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
4a240 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 eaveCursor(BtCur
4a250 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
4a260 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4a270 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c ite3BtreeLeaveAl
4a280 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c l(sqlite3*);.SQL
4a290 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4a2a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
4a2b0 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42 utexArrayEnter(B
4a2c0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 treeMutexArray*)
4a2d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4a2e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4a2f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
4a300 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 ave(BtreeMutexAr
4a310 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ray*);.SQLITE_PR
4a320 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4a330 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
4a340 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d rayInsert(BtreeM
4a350 75 74 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65 utexArray*, Btre
4a360 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 e*);.#ifndef NDE
4a370 42 55 47 0a 20 20 2f 2a 20 54 68 65 73 65 20 72 BUG. /* These r
4a380 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
4a390 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
4a3a0 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
4a3b0 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 . */.SQLITE_PRIV
4a3c0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
4a3d0 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
4a3e0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4a3f0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
4a400 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
4a410 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 AllMutexes(sqlit
4a420 65 33 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 65 6c e3*);.#endif.#el
4a430 73 65 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c se..# define sql
4a440 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 58 ite3BtreeLeave(X
4a450 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
4a460 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
4a470 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 or(X).# define s
4a480 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
4a490 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69 Cursor(X).# defi
4a4a0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c ne sqlite3BtreeL
4a4b0 65 61 76 65 41 6c 6c 28 58 29 0a 23 20 64 65 66 eaveAll(X).# def
4a4c0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4a4d0 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
4a4e0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 X).# define sqli
4a4f0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
4a500 61 79 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66 ayLeave(X).# def
4a510 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4a520 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 MutexArrayInsert
4a530 28 58 2c 59 29 0a 0a 23 20 64 65 66 69 6e 65 20 (X,Y)..# define
4a540 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
4a550 73 4d 75 74 65 78 28 58 29 20 31 0a 23 20 64 65 sMutex(X) 1.# de
4a560 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 fine sqlite3Btre
4a570 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 eHoldsAllMutexes
4a580 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 23 (X) 1.#endif...#
4a590 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52 45 45 5f endif /* _BTREE_
4a5a0 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a H_ */../********
4a5b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 ****** End of bt
4a5c0 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ree.h **********
4a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a5f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
4a600 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
4a610 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
4a620 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
4a630 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4a640 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
4a650 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 ****** Include v
4a660 64 62 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 dbe.h in the mid
4a670 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
4a680 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4a690 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
4a6a0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
4a6b0 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a e vdbe.h *******
4a6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a6e0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
4a6f0 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
4a700 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
4a710 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
4a720 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
4a730 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
4a740 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
4a750 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
4a760 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
4a770 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
4a780 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
4a790 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
4a7a0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
4a7b0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
4a7c0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
4a7d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
4a7e0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
4a7f0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
4a800 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
4a810 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
4a820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
4a860 20 48 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 Header file for
4a870 20 74 68 65 20 56 69 72 74 75 61 6c 20 44 61 74 the Virtual Dat
4a880 61 42 61 73 65 20 45 6e 67 69 6e 65 20 28 56 44 aBase Engine (VD
4a890 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 BE).**.** This h
4a8a0 65 61 64 65 72 20 64 65 66 69 6e 65 73 20 74 68 eader defines th
4a8b0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
4a8c0 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 he virtual datab
4a8d0 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72 ase engine.** or
4a8e0 20 56 44 42 45 2e 20 20 54 68 65 20 56 44 42 45 VDBE. The VDBE
4a8f0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 61 implements an a
4a900 62 73 74 72 61 63 74 20 6d 61 63 68 69 6e 65 20 bstract machine
4a910 74 68 61 74 20 72 75 6e 73 20 61 0a 2a 2a 20 73 that runs a.** s
4a920 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d 20 74 6f imple program to
4a930 20 61 63 63 65 73 73 20 61 6e 64 20 6d 6f 64 69 access and modi
4a940 66 79 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e fy the underlyin
4a950 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a g database..**.*
4a960 2a 20 24 49 64 3a 20 76 64 62 65 2e 68 2c 76 20 * $Id: vdbe.h,v
4a970 31 2e 31 34 31 20 32 30 30 39 2f 30 34 2f 31 30 1.141 2009/04/10
4a980 20 30 30 3a 35 36 3a 32 39 20 64 72 68 20 45 78 00:56:29 drh Ex
4a990 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
4a9a0 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23 SQLITE_VDBE_H_.#
4a9b0 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56 define _SQLITE_V
4a9c0 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 DBE_H_../*.** A
4a9d0 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20 61 single VDBE is a
4a9e0 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 n opaque structu
4a9f0 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22 2e re named "Vdbe".
4aa00 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a Only routines.
4aa10 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ** in the source
4aa20 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62 65 file sqliteVdbe
4aa30 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 .c are allowed t
4aa40 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64 65 o see the inside
4aa50 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 s.** of this str
4aa60 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 ucture..*/.typed
4aa70 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20 56 ef struct Vdbe V
4aa80 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dbe;../*.** The
4aa90 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 6c names of the fol
4aaa0 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65 63 lowing types dec
4aab0 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e 74 lared in vdbeInt
4aac0 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64 0a .h are required.
4aad0 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 4f ** for the VdbeO
4aae0 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f p definition..*/
4aaf0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4ab00 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75 6e VdbeFunc VdbeFun
4ab10 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 c;.typedef struc
4ab20 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a t Mem Mem;../*.*
4ab30 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 74 72 * A single instr
4ab40 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69 uction of the vi
4ab50 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 rtual machine ha
4ab60 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 s an opcode.** a
4ab70 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 74 68 nd as many as th
4ab80 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 20 54 ree operands. T
4ab90 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 he instruction i
4aba0 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 61 73 s recorded.** as
4abb0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
4abc0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
4abd0 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 ructure:.*/.stru
4abe0 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 75 38 ct VdbeOp {. u8
4abf0 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20 opcode;
4ac00 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74 /* What operat
4ac10 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a ion to perform *
4ac20 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 /. signed char
4ac30 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f p4type; /* One o
4ac40 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 6f 6e f the P4_xxx con
4ac50 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 2a 2f stants for p4 */
4ac60 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b 20 20 . u8 opflags;
4ac70 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 63 75 /* Not cu
4ac80 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a rrently used */.
4ac90 20 20 75 38 20 70 35 3b 20 20 20 20 20 20 20 20 u8 p5;
4aca0 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68 20 70 /* Fifth p
4acb0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 75 arameter is an u
4acc0 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 nsigned characte
4acd0 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 r */. int p1;
4ace0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
4acf0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 rst operand */.
4ad00 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 int p2;
4ad10 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 /* Second p
4ad20 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 arameter (often
4ad30 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 the jump destina
4ad40 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 20 70 tion) */. int p
4ad50 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 3; /
4ad60 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 * The third para
4ad70 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e meter */. union
4ad80 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
4ad90 2a 20 66 6f 72 74 68 20 70 61 72 61 6d 65 74 65 * forth paramete
4ada0 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20 r */. int i;
4adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4adc0 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
4add0 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34 5f 49 if p4type==P4_I
4ade0 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f 69 64 NT32 */. void
4adf0 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
4ae00 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20 70 6f /* Generic po
4ae10 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63 68 61 inter */. cha
4ae20 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 r *z;
4ae30 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
4ae40 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72 69 6e o data for strin
4ae50 67 20 28 63 68 61 72 20 61 72 72 61 79 29 20 74 g (char array) t
4ae60 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36 34 20 ypes */. i64
4ae70 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20 20 20 *pI64;
4ae80 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4ae90 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 p4type is P4_INT
4aea0 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 64 */. double
4aeb0 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 *pReal;
4aec0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4aed0 74 79 70 65 20 69 73 20 50 34 5f 52 45 41 4c 20 type is P4_REAL
4aee0 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a */. FuncDef *
4aef0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 2f 2a pFunc; /*
4af00 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4af10 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45 46 20 e is P4_FUNCDEF
4af20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e 63 20 */. VdbeFunc
4af30 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20 2f 2a *pVdbeFunc; /*
4af40 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4af50 65 20 69 73 20 50 34 5f 56 44 42 45 46 55 4e 43 e is P4_VDBEFUNC
4af60 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 */. CollSeq
4af70 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f *pColl; /
4af80 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4af90 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53 45 51 pe is P4_COLLSEQ
4afa0 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 */. Mem *pMe
4afb0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f m; /
4afc0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4afd0 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a pe is P4_MEM */.
4afe0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
4aff0 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a 20 55 73 *pVtab; /* Us
4b000 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4b010 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20 20 20 s P4_VTAB */.
4b020 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
4b030 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 fo; /* Used
4b040 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4b050 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 4_KEYINFO */.
4b060 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20 20 20 int *ai;
4b070 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
4b080 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4b090 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a 20 20 4_INTARRAY */.
4b0a0 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c } p4;.#ifdef SQL
4b0b0 49 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 ITE_DEBUG. char
4b0c0 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 *zComment;
4b0d0 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 /* Comment
4b0e0 74 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 to improve reada
4b0f0 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 bility */.#endif
4b100 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
4b110 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 FILE. int cnt;
4b120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d /* Number of tim
4b140 65 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 es this instruct
4b150 69 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 ion was executed
4b160 20 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 */. u64 cycles
4b170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
4b180 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 * Total time spe
4b190 6e 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 nt executing thi
4b1a0 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f s instruction */
4b1b0 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 .#endif.};.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 ef struct VdbeOp
4b1d0 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 VdbeOp;../*.**
4b1e0 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f A smaller versio
4b1f0 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64 n of VdbeOp used
4b200 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64 for the VdbeAdd
4b210 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f OpList() functio
4b220 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 20 n because.** it
4b230 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73 70 takes up less sp
4b240 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ace..*/.struct V
4b250 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38 dbeOpList {. u8
4b260 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20 opcode;
4b270 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74 /* What operat
4b280 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a ion to perform *
4b290 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 /. signed char
4b2a0 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74 p1; /* First
4b2b0 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69 operand */. si
4b2c0 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20 20 20 gned char p2;
4b2d0 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 /* Second para
4b2e0 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 meter (often the
4b2f0 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f jump destinatio
4b300 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 n) */. signed c
4b310 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a 20 54 har p3; /* T
4b320 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a hird parameter *
4b330 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 /.};.typedef str
4b340 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 56 uct VdbeOpList V
4b350 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a dbeOpList;../*.*
4b360 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
4b370 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 74 79 70 of VdbeOp.p3typ
4b380 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e.*/.#define P4_
4b390 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20 20 2f NOTUSED 0 /
4b3a0 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d 65 74 * The P4 paramet
4b3b0 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a er is not used *
4b3c0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 59 4e /.#define P4_DYN
4b3d0 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 AMIC (-1) /* P
4b3e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 ointer to a stri
4b3f0 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ng obtained from
4b400 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 sqliteMalloc()
4b410 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 54 */.#define P4_ST
4b420 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20 ATIC (-2) /*
4b430 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 Pointer to a sta
4b440 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 tic string */.#d
4b450 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51 efine P4_COLLSEQ
4b460 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 69 73 (-4) /* P4 is
4b470 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4b480 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 CollSeq structur
4b490 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4b4a0 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20 20 2f FUNCDEF (-5) /
4b4b0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4b4c0 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66 20 73 r to a FuncDef s
4b4d0 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 tructure */.#def
4b4e0 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20 ine P4_KEYINFO
4b4f0 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 20 61 (-6) /* P4 is a
4b500 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
4b510 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
4b520 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 44 */.#define P4_VD
4b530 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20 BEFUNC (-7) /*
4b540 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
4b550 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20 73 74 to a VdbeFunc st
4b560 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 ructure */.#defi
4b570 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 20 28 ne P4_MEM (
4b580 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 -8) /* P4 is a
4b590 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d pointer to a Mem
4b5a0 2a 20 20 20 20 73 74 72 75 63 74 75 72 65 20 2a * structure *
4b5b0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 52 41 /.#define P4_TRA
4b5c0 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50 NSIENT (-9) /* P
4b5d0 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
4b5e0 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 73 74 o a transient st
4b5f0 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ring */.#define
4b600 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d 31 30 P4_VTAB (-10
4b610 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 ) /* P4 is a poi
4b620 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 nter to an sqlit
4b630 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 e3_vtab structur
4b640 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4b650 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29 20 2f MPRINTF (-11) /
4b660 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 * P4 is a string
4b670 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
4b680 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
4b690 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 52 */.#define P4_R
4b6a0 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20 2f 2a EAL (-12) /*
4b6b0 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 P4 is a 64-bit
4b6c0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
4b6d0 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 alue */.#define
4b6e0 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d 31 33 P4_INT64 (-13
4b6f0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d ) /* P4 is a 64-
4b700 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
4b710 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 er */.#define P4
4b720 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34 29 20 _INT32 (-14)
4b730 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d 62 69 /* P4 is a 32-bi
4b740 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
4b750 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 */.#define P4_I
4b760 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20 2f 2a NTARRAY (-15) /*
4b770 20 50 34 20 69 73 20 61 20 76 65 63 74 6f 72 20 P4 is a vector
4b780 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 of 32-bit intege
4b790 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 rs */../* When a
4b7a0 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d dding a P4 argum
4b7b0 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59 ent using P4_KEY
4b7c0 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 INFO, a copy of
4b7d0 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 the KeyInfo stru
4b7e0 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 cture.** is made
4b7f0 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20 . That copy is
4b800 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
4b810 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 dbe is finalized
4b820 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a . But if the.**
4b830 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f argument is P4_
4b840 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c KEYINFO_HANDOFF,
4b850 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70 the passed in p
4b860 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20 ointer is used.
4b870 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 It still.** get
4b880 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4b890 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4b8a0 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 ed so it still s
4b8b0 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 hould be obtaine
4b8c0 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 d.** from a sing
4b8d0 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 le sqliteMalloc(
4b8e0 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 ). But no copy
4b8f0 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 is made and the
4b900 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 calling.** funct
4b910 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a ion should *not*
4b920 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65 try to free the
4b930 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 KeyInfo..*/.#de
4b940 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f fine P4_KEYINFO_
4b950 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64 HANDOFF (-16).#d
4b960 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
4b970 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a _STATIC (-17)..
4b980 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 /*.** The Vdbe.a
4b990 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f ColName array co
4b9a0 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 ntains 5n Mem st
4b9b0 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20 ructures, where
4b9c0 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d n is the .** num
4b9d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f ber of columns o
4b9e0 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 f data returned
4b9f0 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
4ba00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c ..*/.#define COL
4ba10 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a NAME_NAME 0.
4ba20 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f #define COLNAME_
4ba30 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69 DECLTYPE 1.#defi
4ba40 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 ne COLNAME_DATAB
4ba50 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f ASE 2.#define CO
4ba60 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 LNAME_TABLE 3
4ba70 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4ba80 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 _COLUMN 4.#ifd
4ba90 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
4baa0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
4bab0 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d .# define COLNAM
4bac0 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20 E_N 5
4bad0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 /* Number of C
4bae0 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f OLNAME_xxx symbo
4baf0 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 ls */.#else.# if
4bb00 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
4bb10 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66 DECLTYPE.# def
4bb20 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 ine COLNAME_N
4bb30 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 1 /* Sto
4bb40 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 re only the name
4bb50 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 */.# else.# d
4bb60 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4bb70 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53 2 /* S
4bb80 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e tore the name an
4bb90 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 d decltype */.#
4bba0 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
4bbb0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
4bbc0 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 g macro converts
4bbd0 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72 a relative addr
4bbe0 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69 ess in the p2 fi
4bbf0 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 eld.** of a Vdbe
4bc00 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 Op structure int
4bc10 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d o a negative num
4bc20 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 ber so that .**
4bc30 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4bc40 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 List() knows tha
4bc50 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 t the address is
4bc60 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c relative. Call
4bc70 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f ing.** the macro
4bc80 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20 again restores
4bc90 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a the address..*/.
4bca0 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 #define ADDR(X)
4bcb0 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a (-1-(X))../*.**
4bcc0 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 The makefile sc
4bcd0 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 ans the vdbe.c s
4bce0 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 ource file and c
4bcf0 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f reates the "opco
4bd00 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 des.h".** header
4bd10 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e file that defin
4bd20 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 es a number for
4bd30 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64 each opcode used
4bd40 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f by the VDBE..*/
4bd50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4bd60 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 Include opcodes
4bd70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4bd80 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a of vdbe.h *****
4bd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4bda0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4bdb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f Begin file opco
4bdc0 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.h **********
4bdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4bdf0 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c ./* Automaticall
4be00 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f y generated. Do
4be10 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 not edit */./*
4be20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 See the mkopcode
4be30 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 h.awk script for
4be40 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 details */.#def
4be50 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 ine OP_VNext
4be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be70 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 1.#d
4be80 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 efine OP_Affinit
4be90 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4bea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
4beb0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d #define OP_Colum
4bec0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bee0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 3.#define OP_Set
4bef0 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 Cookie
4bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf10 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 4.#define OP_S
4bf20 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 eek
4bf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf40 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5.#define OP
4bf50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 _Real
4bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf70 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 130 /* sam
4bf80 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 e as TK_FLOAT
4bf90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4bfa0 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20 equence
4bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bfc0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 6.#define OP
4bfd0 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20 _Savepoint
4bfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bff0 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 7.#define
4c000 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 OP_Ge
4c010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c020 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73 78 /* s
4c030 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 ame as TK_GE
4c040 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4c050 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 _RowKey
4c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c070 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
4c080 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20 OP_SCopy
4c090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0a0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 9.#defin
4c0b0 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20 e OP_Eq
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0d0 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 74 /*
4c0e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 same as TK_EQ
4c0f0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4c100 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20 OP_OpenWrite
4c110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c120 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 10.#defin
4c130 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 e OP_NotNull
4c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c150 20 20 20 20 20 20 20 20 20 37 32 20 20 20 2f 2a 72 /*
4c160 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e same as TK_NOTN
4c170 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ULL */.#define
4c180 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 OP_If
4c190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c1a0 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 11.#defin
4c1b0 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 e OP_ToInt
4c1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c1d0 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 144 /*
4c1e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 same as TK_TO_I
4c1f0 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NT */.#define
4c200 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 OP_String8
4c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c220 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 94 /* s
4c230 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 ame as TK_STRING
4c240 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4c250 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 _CollSeq
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c270 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
4c280 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 OP_OpenRead
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2a0 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 13.#defin
4c2b0 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 e OP_Expire
4c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2d0 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 14.#def
4c2e0 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 ine OP_AutoCommi
4c2f0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4c300 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 15.#d
4c310 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 efine OP_Gt
4c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c330 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20 75
4c340 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4c350 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 GT */.#def
4c360 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 ine OP_Pagecount
4c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c380 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 16.#d
4c390 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 efine OP_Integri
4c3a0 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 tyCk
4c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 17.
4c3c0 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 #define OP_Sort
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4c3f0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 8.#define OP_Cop
4c400 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4c410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c420 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 20.#define OP_T
4c430 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 race
4c440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c450 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 21.#define OP
4c460 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 _Function
4c470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c480 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
4c490 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 OP_IfNeg
4c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4b0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
4c4c0 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20 e OP_And
4c4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4e0 20 20 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 67 /*
4c4f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 same as TK_AND
4c500 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4c510 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20 OP_Subtract
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c530 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 85 /* s
4c540 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 ame as TK_MINUS
4c550 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4c560 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20 _Noop
4c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c580 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 24.#define
4c590 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 20 20 OP_Return
4c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5b0 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 25.#defin
4c5c0 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 20 e OP_Remainder
4c5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5e0 20 20 20 20 20 20 20 20 20 38 38 20 20 20 2f 2a 88 /*
4c5f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20 same as TK_REM
4c600 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4c610 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20 20 20 OP_NewRowid
4c620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c630 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 26.#defin
4c640 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 20 20 e OP_Multiply
4c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c660 20 20 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 86 /*
4c670 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 same as TK_STAR
4c680 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4c690 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20 20 20 OP_Variable
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6b0 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 27.#defin
4c6c0 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20 e OP_String
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6e0 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 28.#def
4c6f0 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e ine OP_RealAffin
4c700 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20 ity
4c710 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 29.#d
4c720 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65 efine OP_VRename
4c730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c740 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 30.
4c750 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 #define OP_Parse
4c760 53 63 68 65 6d 61 20 20 20 20 20 20 20 20 20 20 Schema
4c770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4c780 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4f 70 1.#define OP_VOp
4c790 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 en
4c7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7b0 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 32.#define OP_C
4c7c0 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20 lose
4c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7e0 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 33.#define OP
4c7f0 5f 43 72 65 61 74 65 49 6e 64 65 78 20 20 20 20 _CreateIndex
4c800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c810 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 34.#define
4c820 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20 20 20 OP_IsUnique
4c830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c840 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 35.#defin
4c850 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 e OP_NotFound
4c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c870 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 36.#def
4c880 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 ine OP_Int64
4c890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c8a0 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 37.#d
4c8b0 65 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 efine OP_MustBeI
4c8c0 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
4c8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 38.
4c8e0 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 #define OP_Halt
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4c910 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 9.#define OP_Row
4c920 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 id
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c940 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 40.#define OP_I
4c950 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 dxLT
4c960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c970 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 41.#define OP
4c980 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20 _AddImm
4c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c9a0 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 42.#define
4c9b0 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 20 20 20 OP_Statement
4c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c9d0 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 43.#defin
4c9e0 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20 e OP_RowData
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca00 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 44.#def
4ca10 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 ine OP_MemMax
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca30 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 45.#d
4ca40 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 efine OP_Or
4ca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca60 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 20 66
4ca70 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4ca80 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 OR */.#def
4ca90 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 ine OP_NotExists
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cab0 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 46.#d
4cac0 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 efine OP_Gosub
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cae0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 47.
4caf0 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 #define OP_Divid
4cb00 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
4cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
4cb20 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 7 /* same as T
4cb30 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 K_SLASH */.#d
4cb40 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72 efine OP_Integer
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 48.
4cb70 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d #define OP_ToNum
4cb80 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20 eric
4cb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
4cba0 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 3 /* same as T
4cbb0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 K_TO_NUMERIC*/.#
4cbc0 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 define OP_Prev
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 49
4cbf0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 .#define OP_RowS
4cc00 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20 etRead
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 50.#define OP_Co
4cc30 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20 20 ncat
4cc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc50 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 89 /* same a
4cc60 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f s TK_CONCAT */
4cc70 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 .#define OP_RowS
4cc80 65 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20 etAdd
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cca0 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 51.#define OP_Bi
4ccb0 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 tAnd
4ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ccd0 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 80 /* same a
4cce0 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f s TK_BITAND */
4ccf0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c .#define OP_VCol
4cd00 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 umn
4cd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 52.#define OP_Cr
4cd30 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 eateTable
4cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd50 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53.#define OP_
4cd60 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20 Last
4cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd80 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f 54.#define O
4cd90 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20 P_SeekLe
4cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cdb0 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 55.#define
4cdc0 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20 OP_IsNull
4cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cde0 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 2a 20 71 /*
4cdf0 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c same as TK_ISNUL
4ce00 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f L */.#define O
4ce10 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20 20 P_IncrVacuum
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce30 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 56.#define
4ce40 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 20 OP_IdxRowid
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce60 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 57.#defi
4ce70 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 ne OP_ShiftRight
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce90 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f 83 /
4cea0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 * same as TK_RSH
4ceb0 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 IFT */.#define
4cec0 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20 OP_ResetCount
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cee0 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 58.#defi
4cef0 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 ne OP_ContextPus
4cf00 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h
4cf10 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 59.#de
4cf20 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 fine OP_Yield
4cf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf40 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 60.#
4cf50 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 define OP_DropTr
4cf60 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
4cf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31 61
4cf80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 .#define OP_Drop
4cf90 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 Index
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cfb0 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 62.#define OP_Id
4cfc0 78 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 xGE
4cfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cfe0 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 63.#define OP_
4cff0 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 IdxDelete
4d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d010 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f 64.#define O
4d020 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 P_Vacuum
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d040 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 65.#define
4d050 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 OP_IfNot
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d070 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 68.#defi
4d080 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20 ne OP_DropTable
4d090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0a0 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 69.#de
4d0b0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20 fine OP_SeekLt
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0d0 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 70.#
4d0e0 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 define OP_MakeRe
4d0f0 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 20 cord
4d100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 79
4d110 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c .#define OP_ToBl
4d120 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ob
4d130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d140 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 42 /* same as
4d150 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 TK_TO_BLOB */.#
4d160 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74 define OP_Result
4d170 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20 Row
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30 90
4d190 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65 .#define OP_Dele
4d1a0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1c0 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67 91.#define OP_Ag
4d1d0 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20 20 gFinal
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1f0 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 92.#define OP_
4d200 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20 Compare
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d220 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 95.#define O
4d230 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20 20 P_ShiftLeft
4d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d250 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 82 /* sa
4d260 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20 me as TK_LSHIFT
4d270 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4d280 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20 Goto
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d2a0 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 96.#define O
4d2b0 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 P_TableLock
4d2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d2d0 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
4d2e0 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20 20 OP_Clear
4d2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d300 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 98.#defi
4d310 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20 ne OP_Le
4d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d330 20 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f 76 /
4d340 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20 * same as TK_LE
4d350 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
4d360 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 OP_VerifyCookie
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d380 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 99.#defi
4d390 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 ne OP_AggStep
4d3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d3b0 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 100.#de
4d3c0 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 fine OP_ToText
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d3e0 20 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20 141
4d3f0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
4d400 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 O_TEXT */.#defi
4d410 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20 ne OP_Not
4d420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d430 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 19 /
4d440 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
4d450 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
4d460 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 OP_ToReal
4d470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d480 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20 145 /*
4d490 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 same as TK_TO_RE
4d4a0 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f AL */.#define O
4d4b0 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 P_SetNumColumns
4d4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d4d0 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 101.#define
4d4e0 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 OP_Transaction
4d4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d500 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 102.#defi
4d510 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 ne OP_VFilter
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d530 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 103.#de
4d540 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 fine OP_Ne
4d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d560 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 73
4d570 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
4d580 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
4d590 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 ne OP_VDestroy
4d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d5b0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 104.#de
4d5c0 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 fine OP_ContextP
4d5d0 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 op
4d5e0 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 105.#
4d5f0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 define OP_BitOr
4d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 81
4d620 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4d630 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 _BITOR */.#de
4d640 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 fine OP_Next
4d650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d660 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 106.#
4d670 64 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 define OP_Count
4d680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d690 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
4d6a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 .#define OP_IdxI
4d6b0 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 nsert
4d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d6d0 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 08.#define OP_Lt
4d6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d700 20 20 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 77 /* same a
4d710 73 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f s TK_LT */
4d720 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b .#define OP_Seek
4d730 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Ge
4d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d750 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 09.#define OP_In
4d760 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 sert
4d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d780 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 110.#define OP_
4d790 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 Destroy
4d7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d7b0 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 111.#define O
4d7c0 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 P_ReadCookie
4d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d7e0 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 112.#define
4d7f0 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 OP_RowSetTest
4d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d810 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 113.#defi
4d820 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 ne OP_LoadAnalys
4d830 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 is
4d840 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 114.#de
4d850 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20 fine OP_Explain
4d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d870 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 115.#
4d880 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 define OP_HaltIf
4d890 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Null
4d8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36 116
4d8b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e .#define OP_Open
4d8c0 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 Pseudo
4d8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d8e0 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 17.#define OP_Op
4d8f0 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 enEphemeral
4d900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d910 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 118.#define OP_
4d920 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Null
4d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d940 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 119.#define O
4d950 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 P_Move
4d960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d970 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 120.#define
4d980 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 OP_Blob
4d990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d9a0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 121.#defi
4d9b0 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 ne OP_Add
4d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d9d0 20 20 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 84 /
4d9e0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 * same as TK_PLU
4d9f0 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 S */.#define
4da00 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 OP_Rewind
4da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 122.#defi
4da30 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 ne OP_SeekGt
4da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da50 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 123.#de
4da60 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 fine OP_VBegin
4da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da80 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
4da90 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 define OP_VUpdat
4daa0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
4dab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 125
4dac0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 .#define OP_IfZe
4dad0 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ro
4dae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4daf0 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 26.#define OP_Bi
4db00 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 tNot
4db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4db20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 93 /* same a
4db30 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f s TK_BITNOT */
4db40 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 .#define OP_VCre
4db50 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ate
4db60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4db70 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 27.#define OP_Fo
4db80 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 und
4db90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dba0 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 128.#define OP_
4dbb0 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 IfPos
4dbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dbd0 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 129.#define O
4dbe0 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 P_NullRow
4dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dc00 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 131.#define
4dc10 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 OP_Jump
4dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dc30 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 132.#defi
4dc40 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f ne OP_Permutatio
4dc50 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
4dc60 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a 2f 2a 133../*
4dc70 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f The following o
4dc80 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 pcode values are
4dc90 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 never used */.#
4dca0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
4dcb0 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20 d_134
4dcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
4dcd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
4dce0 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20 sed_135
4dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4dd00 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 35.#define OP_No
4dd10 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20 tUsed_136
4dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dd30 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 136.#define OP_
4dd40 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20 NotUsed_137
4dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dd60 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f 137.#define O
4dd70 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20 P_NotUsed_138
4dd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dd90 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 138.#define
4dda0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20 OP_NotUsed_139
4ddb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ddc0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 139.#defi
4ddd0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34 ne OP_NotUsed_14
4dde0 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
4ddf0 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f 140.../
4de00 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63 * Properties suc
4de10 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22 h as "out2" or "
4de20 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73 jump" that are s
4de30 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63 pecified in.** c
4de40 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e omments followin
4de50 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72 g the "case" for
4de60 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20 each opcode in
4de70 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72 the vdbe.c.** ar
4de80 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62 e encoded into b
4de90 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c itvectors as fol
4dea0 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lows:.*/.#define
4deb0 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20 OPFLG_JUMP
4dec0 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 0x0001 /
4ded0 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64 * jump: P2 hold
4dee0 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a s jmp target */.
4def0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 #define OPFLG_OU
4df00 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78 T2_PRERELEASE 0x
4df10 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72 0002 /* out2-pr
4df20 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65 erelease: */.#de
4df30 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20 fine OPFLG_IN1
4df40 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
4df50 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20 4 /* in1: P1
4df60 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 is an input */.#
4df70 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 define OPFLG_IN2
4df80 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
4df90 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50 008 /* in2: P
4dfa0 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 2 is an input */
4dfb0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
4dfc0 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N3 0
4dfd0 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20 x0010 /* in3:
4dfe0 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P3 is an input
4dff0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
4e000 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20 _OUT3
4e010 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33 0x0020 /* out3
4e020 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70 : P3 is an outp
4e030 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
4e040 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 FLG_INITIALIZER
4e050 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30 {\./* 0 */ 0x0
4e060 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 0, 0x01, 0x00, 0
4e070 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c x00, 0x10, 0x08,
4e080 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x02, 0x00,\./*
4e090 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 8 */ 0x00, 0x
4e0a0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 04, 0x00, 0x05,
4e0b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
4e0c0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20 , 0x00,\./* 16
4e0d0 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 */ 0x02, 0x00, 0
4e0e0 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x01, 0x04, 0x04,
4e0f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
4e100 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78 5,\./* 24 */ 0x
4e110 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20 00, 0x04, 0x02,
4e120 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 34 0x00, 0x02, 0x04
4e130 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f , 0x00, 0x00,\./
4e140 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30 * 32 */ 0x00, 0
4e150 78 30 30 2c 20 30 78 30 32 2c 20 30 78 31 31 2c x00, 0x02, 0x11,
4e160 20 30 78 31 31 2c 20 30 78 30 32 2c 20 30 78 30 0x11, 0x02, 0x0
4e170 35 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 34 30 5, 0x00,\./* 40
4e180 20 2a 2f 20 30 78 30 32 2c 20 30 78 31 31 2c 20 */ 0x02, 0x11,
4e190 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 0x04, 0x00, 0x00
4e1a0 2c 20 30 78 30 63 2c 20 30 78 31 31 2c 20 30 78 , 0x0c, 0x11, 0x
4e1b0 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 01,\./* 48 */ 0
4e1c0 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32 31 2c x02, 0x01, 0x21,
4e1d0 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 0x08, 0x00, 0x0
4e1e0 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 5c 0a 2, 0x01, 0x11,\.
4e1f0 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31 2c 20 /* 56 */ 0x01,
4e200 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 0x02, 0x00, 0x00
4e210 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 , 0x04, 0x00, 0x
4e220 30 30 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 36 00, 0x11,\./* 6
4e230 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 4 */ 0x00, 0x00,
4e240 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 30 0x2c, 0x2c, 0x0
4e250 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 5, 0x00, 0x11, 0
4e260 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 x05,\./* 72 */
4e270 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35 0x05, 0x15, 0x15
4e280 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
4e290 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c 5c 15, 0x15, 0x00,\
4e2a0 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 63 2c ./* 80 */ 0x2c,
4e2b0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 0x2c, 0x2c, 0x2
4e2c0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
4e2d0 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 x2c, 0x2c,\./*
4e2e0 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 88 */ 0x2c, 0x2c
4e2f0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
4e300 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20 00, 0x04, 0x02,
4e310 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 0x00,\./* 96 */
4e320 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
4e330 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
4e340 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c x00, 0x00, 0x01,
4e350 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 30 \./* 104 */ 0x00
4e360 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 , 0x00, 0x01, 0x
4e370 30 32 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 02, 0x08, 0x11,
4e380 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 0x00, 0x02,\./*
4e390 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30 78 31 112 */ 0x02, 0x1
4e3a0 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 5, 0x00, 0x00, 0
4e3b0 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x10, 0x00, 0x00,
4e3c0 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a 0x02,\./* 120 *
4e3d0 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 / 0x00, 0x02, 0x
4e3e0 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 01, 0x11, 0x00,
4e3f0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30 0x00, 0x05, 0x00
4e400 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 31 ,\./* 128 */ 0x1
4e410 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c 20 30 1, 0x05, 0x02, 0
4e420 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
4e430 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x00, 0x00,\./*
4e440 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 136 */ 0x00, 0x
4e450 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
4e460 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 0x00, 0x04, 0x04
4e470 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 , 0x04,\./* 144
4e480 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a */ 0x04, 0x04,}.
4e490 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e4a0 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e End of opcodes.
4e4b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e4e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4e4f0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
4e500 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
4e510 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h ********
4e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4e530 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 ../*.** Prototyp
4e540 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20 es for the VDBE
4e550 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20 interface. See
4e560 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 comments on the
4e570 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
4e580 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 * for a descript
4e590 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 ion of what each
4e5a0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
4e5b0 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 es does..*/.SQLI
4e5c0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
4e5d0 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 *sqlite3VdbeCrea
4e5e0 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 te(sqlite3*);.SQ
4e5f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4e600 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
4e610 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 p0(Vdbe*,int);.S
4e620 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4e630 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
4e640 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op1(Vdbe*,int,in
4e650 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4e660 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
4e670 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69 beAddOp2(Vdbe*,i
4e680 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
4e690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4e6a0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4e6b0 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 3(Vdbe*,int,int,
4e6c0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
4e6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4e6e0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56 ite3VdbeAddOp4(V
4e6f0 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 dbe*,int,int,int
4e700 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20 ,int,const char
4e710 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 *zP4,int);.SQLIT
4e720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4e730 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
4e740 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f st(Vdbe*, int nO
4e750 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
4e760 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 nst *aOp);.SQLIT
4e770 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4e780 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
4e790 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 P1(Vdbe*, int ad
4e7a0 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c dr, int P1);.SQL
4e7b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4e7c0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
4e7d0 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP2(Vdbe*, int
4e7e0 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53 addr, int P2);.S
4e7f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4e800 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
4e810 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e angeP3(Vdbe*, in
4e820 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b t addr, int P3);
4e830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4e840 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
4e850 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20 ChangeP5(Vdbe*,
4e860 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50 u8 P5);.SQLITE_P
4e870 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4e880 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
4e890 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29 Vdbe*, int addr)
4e8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4e8b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
4e8c0 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 eChangeToNoop(Vd
4e8d0 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 be*, int addr, i
4e8e0 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt N);.SQLITE_PR
4e8f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
4e900 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 e3VdbeChangeP4(V
4e910 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
4e920 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c const char *zP4,
4e930 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
4e940 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4e950 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
4e960 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 e(Vdbe*, int);.S
4e970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
4e980 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62 beOp *sqlite3Vdb
4e990 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e eGetOp(Vdbe*, in
4e9a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4e9b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
4e9c0 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 beMakeLabel(Vdbe
4e9d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4e9e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
4e9f0 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29 dbeDelete(Vdbe*)
4ea00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4ea10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
4ea20 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a eMakeReady(Vdbe*
4ea30 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
4ea40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4ea50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
4ea60 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a 29 eFinalize(Vdbe*)
4ea70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4ea80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
4ea90 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 eResolveLabel(Vd
4eaa0 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 be*, int);.SQLIT
4eab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4eac0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
4ead0 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 66 Addr(Vdbe*);.#if
4eae0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
4eaf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4eb00 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
4eb10 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 beTrace(Vdbe*,FI
4eb20 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c LE*);.#endif.SQL
4eb30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4eb40 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 sqlite3VdbeRese
4eb50 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 tStepResult(Vdbe
4eb60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4eb70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
4eb80 62 65 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a beReset(Vdbe*);.
4eb90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4eba0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
4ebb0 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c etNumCols(Vdbe*,
4ebc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4ebd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4ebe0 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 VdbeSetColName(V
4ebf0 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 dbe*, int, int,
4ec00 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f const char *, vo
4ec10 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
4ec20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4ec30 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f id sqlite3VdbeCo
4ec40 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a untChanges(Vdbe*
4ec50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4ec60 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 E sqlite3 *sqlit
4ec70 65 33 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b e3VdbeDb(Vdbe*);
4ec80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4ec90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
4eca0 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f SetSql(Vdbe*, co
4ecb0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
4ecc0 20 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 n, int);.SQLITE
4ecd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
4ece0 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 lite3VdbeSwap(Vd
4ecf0 62 65 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 be*,Vdbe*);..#if
4ed00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
4ed10 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
4ed20 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ENT.SQLITE_PRIVA
4ed30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
4ed40 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 beReleaseMemory(
4ed50 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c int);.#endif.SQL
4ed60 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 ITE_PRIVATE Unpa
4ed70 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 ckedRecord *sqli
4ed80 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
4ed90 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 ack(KeyInfo*,int
4eda0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 63 68 61 ,const void*,cha
4edb0 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f r*,int);.SQLITE_
4edc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4edd0 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e ite3VdbeDeleteUn
4ede0 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 packedRecord(Unp
4edf0 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 53 ackedRecord*);.S
4ee00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4ee10 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 t sqlite3VdbeRec
4ee20 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c 63 ordCompare(int,c
4ee30 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 63 onst void*,Unpac
4ee40 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23 kedRecord*);...#
4ee50 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 ifndef NDEBUG.SQ
4ee60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4ee70 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
4ee80 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f omment(Vdbe*, co
4ee90 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
4eea0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f .# define VdbeCo
4eeb0 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 mment(X) sqlite
4eec0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a 53 3VdbeComment X.S
4eed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4eee0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
4eef0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 NoopComment(Vdbe
4ef00 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
4ef10 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 ...);.# define V
4ef20 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 dbeNoopComment(X
4ef30 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f ) sqlite3VdbeNo
4ef40 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 opComment X.#els
4ef50 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 e.# define VdbeC
4ef60 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 69 omment(X).# defi
4ef70 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 ne VdbeNoopComme
4ef80 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 nt(X).#endif..#e
4ef90 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
4efa0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
4efb0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
4efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4efe0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
4eff0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
4f000 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
4f010 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
4f020 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4f030 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
4f040 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 ***** Include pa
4f050 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 ger.h in the mid
4f060 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
4f070 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4f080 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
4f090 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
4f0a0 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a pager.h *******
4f0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f0d0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
4f0e0 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
4f0f0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
4f100 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
4f110 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
4f120 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
4f130 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
4f140 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
4f150 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
4f160 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
4f170 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
4f180 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
4f190 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
4f1a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
4f1b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
4f1c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
4f1d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
4f1e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
4f1f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
4f200 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
4f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
4f250 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
4f260 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
4f270 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 erface that the
4f280 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 sqlite page cach
4f290 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 e.** subsystem.
4f2a0 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
4f2b0 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 20 subsystem reads
4f2c0 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 6c and writes a fil
4f2d0 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 61 e a page.** at a
4f2e0 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 64 time and provid
4f2f0 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 es a journal for
4f300 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
4f310 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72 @(#) $Id: pager
4f320 2e 68 2c 76 20 31 2e 31 30 31 20 32 30 30 39 2f .h,v 1.101 2009/
4f330 30 34 2f 33 30 20 30 39 3a 31 30 3a 33 38 20 64 04/30 09:10:38 d
4f340 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
4f350 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 .*/..#ifndef _PA
4f360 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f GER_H_.#define _
4f370 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 PAGER_H_../*.**
4f380 44 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 Default maximum
4f390 73 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 size for persist
4f3a0 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ent journal file
4f3b0 73 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a s. A negative .*
4f3c0 2a 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f * value means no
4f3d0 20 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c limit. This val
4f3e0 75 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 ue may be overri
4f3f0 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a dden using the .
4f400 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a ** sqlite3PagerJ
4f410 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
4f420 29 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 ) API. See also
4f430 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f "PRAGMA journal_
4f440 73 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a size_limit"..*/.
4f450 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
4f460 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 EFAULT_JOURNAL_S
4f470 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 IZE_LIMIT. #def
4f480 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
4f490 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f LT_JOURNAL_SIZE_
4f4a0 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a LIMIT -1.#endif.
4f4b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 ./*.** The type
4f4c0 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e used to represen
4f4d0 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e t a page number.
4f4e0 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 The first page
4f4f0 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 in a file.** is
4f500 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 called page 1.
4f510 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 0 is used to re
4f520 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 present "not a p
4f530 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 age"..*/.typedef
4f540 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a u32 Pgno;../*.*
4f550 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 * Each open file
4f560 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 is managed by a
4f570 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e separate instan
4f580 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 ce of the "Pager
4f590 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a " structure..*/.
4f5a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 typedef struct P
4f5b0 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a ager Pager;../*.
4f5c0 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 ** Handle type f
4f5d0 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 or pages..*/.typ
4f5e0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 edef struct PgHd
4f5f0 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a r DbPage;../*.**
4f600 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 Page number PAG
4f610 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 ER_MJ_PGNO is ne
4f620 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 ver used in an S
4f630 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 QLite database (
4f640 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 it is.** reserve
4f650 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 d for working ar
4f660 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 ound a windows/p
4f670 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 osix incompatibi
4f680 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 lity). It is.**
4f690 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 used in the jour
4f6a0 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 nal to signify t
4f6b0 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 hat the remainde
4f6c0 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c r of the journal
4f6d0 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 file .** is dev
4f6e0 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 oted to storing
4f6f0 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
4f700 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 name - there ar
4f710 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 e no more pages
4f720 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e to.** roll back.
4f730 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f See comments fo
4f740 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 r function write
4f750 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 MasterJournal()
4f760 69 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 in pager.c .** f
4f770 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 or details..*/.#
4f780 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f define PAGER_MJ_
4f790 50 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 PGNO(x) ((Pgno)(
4f7a0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 (PENDING_BYTE/((
4f7b0 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 x)->pageSize))+1
4f7c0 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 ))../*.** Allowe
4f7d0 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 d values for the
4f7e0 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 flags parameter
4f7f0 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
4f800 4f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f Open()..**.** NO
4f810 54 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 TE: These values
4f820 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 must match the
4f830 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 corresponding BT
4f840 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 REE_ values in b
4f850 74 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 tree.h..*/.#defi
4f860 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f ne PAGER_OMIT_JO
4f870 55 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 URNAL 0x0001
4f880 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 /* Do not use a
4f890 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
4f8a0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 l */.#define PAG
4f8b0 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 ER_NO_READLOCK
4f8c0 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 0x0002 /* Om
4f8d0 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 it readlocks on
4f8e0 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a readonly files *
4f8f0 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 /../*.** Valid v
4f900 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 alues for the se
4f910 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
4f920 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 sqlite3PagerLoc
4f930 6b 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 kingMode()..*/.#
4f940 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 define PAGER_LOC
4f950 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 KINGMODE_QUERY
4f960 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 -1.#define P
4f970 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
4f980 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 _NORMAL 0.#
4f990 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 define PAGER_LOC
4f9a0 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
4f9b0 56 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 VE 1../*.** Va
4f9c0 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 lid values for t
4f9d0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
4f9e0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 nt to sqlite3Pag
4f9f0 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e erJournalMode().
4fa00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 .*/.#define PAGE
4fa10 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
4fa20 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 ERY -1.#def
4fa30 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
4fa40 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 LMODE_DELETE
4fa50 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 0 /* Commit
4fa60 62 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 by deleting jour
4fa70 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 nal file */.#def
4fa80 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
4fa90 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 LMODE_PERSIST
4faa0 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 1 /* Commit
4fab0 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e by zeroing journ
4fac0 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 al header */.#de
4fad0 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e fine PAGER_JOURN
4fae0 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 ALMODE_OFF
4faf0 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 2 /* Journa
4fb00 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 l omitted. */.#
4fb10 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
4fb20 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
4fb30 45 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d E 3 /* Comm
4fb40 69 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 it by truncating
4fb50 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 journal */.#def
4fb60 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
4fb70 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 LMODE_MEMORY
4fb80 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 4 /* In-memo
4fb90 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ry journal file
4fba0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 */../*.** The re
4fbb0 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 mainder of this
4fbc0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
4fbd0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f e declarations o
4fbe0 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a f the functions.
4fbf0 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 ** that make up
4fc00 74 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 the Pager sub-sy
4fc10 73 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f stem API. See so
4fc20 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e urce code commen
4fc30 74 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 ts for .** a det
4fc40 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f ailed descriptio
4fc50 6e 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e n of each routin
4fc60 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 e..*/../* Open a
4fc70 6e 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 nd close a Pager
4fc80 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 connection. */
4fc90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4fca0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
4fcb0 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 Open(sqlite3_vfs
4fcc0 20 2a 2c 20 50 61 67 65 72 20 2a 2a 70 70 50 61 *, Pager **ppPa
4fcd0 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ger, const char*
4fce0 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a , int,int,int);.
4fcf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4fd00 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
4fd10 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 lose(Pager *pPag
4fd20 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 er);.SQLITE_PRIV
4fd30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
4fd40 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 agerReadFilehead
4fd50 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 er(Pager*, int,
4fd60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b unsigned char*);
4fd70 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
4fd80 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 sed to configure
4fd90 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e a Pager object.
4fda0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
4fdb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
4fdc0 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
4fdd0 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a er(Pager*, int(*
4fde0 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 )(void *), void
4fdf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4fe00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
4fe10 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 agerSetReiniter(
4fe20 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a 29 28 Pager*, void(*)(
4fe30 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c 49 54 DbPage*));.SQLIT
4fe40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4fe50 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
4fe60 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 75 31 esize(Pager*, u1
4fe70 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 6*);.SQLITE_PRIV
4fe80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
4fe90 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
4fea0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 (Pager*, int);.S
4feb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4fec0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
4fed0 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 etCachesize(Page
4fee0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
4fef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
4ff00 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 lite3PagerSetSaf
4ff10 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c etyLevel(Pager*,
4ff20 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
4ff30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4ff40 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
4ff50 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e Mode(Pager *, in
4ff60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4ff70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
4ff80 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
4ff90 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 ager *, int);.SQ
4ffa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
4ffb0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
4ffc0 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 rnalSizeLimit(Pa
4ffd0 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c ger *, i64);.SQL
4ffe0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
4fff0 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c te3_backup **sql
50000 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
50010 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 tr(Pager*);../*
50020 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 Functions used t
50030 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
50040 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 ease page refere
50050 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 nces. */ .SQLITE
50060 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
50070 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
50080 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
50090 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 Pgno pgno, DbPag
500a0 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 e **ppPage, int
500b0 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e clrFlag);.#defin
500c0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
500d0 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 t(A,B,C) sqlite3
500e0 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 PagerAcquire(A,B
500f0 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 ,C,0).SQLITE_PRI
50100 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c VATE DbPage *sql
50110 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
50120 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
50130 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 gno pgno);.SQLIT
50140 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
50150 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 qlite3PagerRef(D
50160 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
50170 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
50180 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
50190 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 bPage*);../* Ope
501a0 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 rations on page
501b0 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 references. */.S
501c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
501d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
501e0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
501f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
50200 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
50210 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 ntWrite(DbPage*)
50220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
50230 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
50240 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a rMovepage(Pager*
50250 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e ,DbPage*,Pgno,in
50260 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
50270 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
50280 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
50290 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
502a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
502b0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
502c0 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 ta(DbPage *); .S
502d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
502e0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
502f0 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 GetExtra(DbPage
50300 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f *); ../* Functio
50310 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 ns used to manag
50320 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 e pager transact
50330 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 ions and savepoi
50340 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts. */.SQLITE_P
50350 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
50360 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
50370 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a (Pager*, int*);.
50380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
50390 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
503a0 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 egin(Pager*, int
503b0 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 exFlag, int);.S
503c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
503d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
503e0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 mmitPhaseOne(Pag
503f0 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a er*,const char *
50400 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 zMaster, int);.S
50410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
50420 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
50430 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
50440 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
50450 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
50460 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
50470 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
50480 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
50490 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
504a0 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 k(Pager*);.SQLIT
504b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
504c0 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
504d0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
504e0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 Pager, int n);.S
504f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
50500 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
50510 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
50520 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
50530 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a nt iSavepoint);.
50540 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 ./* Functions us
50550 65 64 20 74 6f 20 71 75 65 72 79 20 70 61 67 65 ed to query page
50560 72 20 73 74 61 74 65 20 61 6e 64 20 63 6f 6e 66 r state and conf
50570 69 67 75 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 iguration. */.SQ
50580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 LITE_PRIVATE u8
50590 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 sqlite3PagerIsre
505a0 61 64 6f 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a adonly(Pager*);.
505b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
505c0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
505d0 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b efcount(Pager*);
505e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
505f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
50600 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 te3PagerFilename
50610 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
50620 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
50630 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
50640 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65 te3PagerVfs(Page
50650 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
50660 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
50670 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
50680 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 le(Pager*);.SQLI
50690 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
506a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 char *sqlite3Pa
506b0 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 gerJournalname(P
506c0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
506d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
506e0 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 e3PagerNosync(Pa
506f0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
50700 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
50710 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 te3PagerTempSpac
50720 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 e(Pager*);.SQLIT
50730 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
50740 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 lite3PagerIsMemd
50750 62 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 b(Pager*);../* F
50760 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f unctions used to
50770 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 64 61 truncate the da
50780 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
50790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
507a0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
507b0 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 TruncateImage(Pa
507c0 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 ger*,Pgno);../*
507d0 55 73 65 64 20 62 79 20 65 6e 63 72 79 70 74 69 Used by encrypti
507e0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 2a on extensions. *
507f0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
50800 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45 HAS_CODEC.SQLITE
50810 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
50820 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
50830 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f 69 64 odec(Pager*,void
50840 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a *(*)(void*,void*
50850 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a ,Pgno,int),void*
50860 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75 );.#endif../* Fu
50870 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f nctions to suppo
50880 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 rt testing and d
50890 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 ebugging. */.#if
508a0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
508b0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
508c0 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 ITE_TEST).SQLITE
508d0 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 _PRIVATE Pgno
508e0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
508f0 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b number(DbPage*);
50900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
50910 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 int sqlite3Pag
50920 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 erIswriteable(Db
50930 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 Page*);.#endif.#
50940 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
50950 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
50960 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 int *sqlite3P
50970 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a agerStats(Pager*
50980 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
50990 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
509a0 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 PagerRefdump(Pag
509b0 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73 er*);. void dis
509c0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
509d0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a o_errors(void);.
509e0 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 void enable_si
509f0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
50a00 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 s(void);.#else.#
50a10 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f define disable_
50a20 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
50a30 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 ors().# define e
50a40 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
50a50 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 io_errors().#end
50a60 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 if..#endif /* _P
50a70 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a AGER_H_ */../***
50a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
50a90 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a of pager.h *****
50aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
50ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
50ae0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
50af0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
50b00 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
50b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
50b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
50b30 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 ude pcache.h in
50b40 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
50b50 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
50b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
50b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
50b80 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 n file pcache.h
50b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
50bc0 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 * 2008 August 05
50bd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
50be0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
50bf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
50c00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
50c10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
50c20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
50c30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
50c40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
50c50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
50c60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
50c70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
50c80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
50c90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
50ca0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
50cb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
50cc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
50cd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
50ce0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
50cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
50d30 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
50d40 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
50d50 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
50d60 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 he sqlite page c
50d70 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 ache.** subsyste
50d80 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 m. .**.** @(#) $
50d90 49 64 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31 Id: pcache.h,v 1
50da0 2e 31 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31 .19 2009/01/20 1
50db0 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31 7:06:27 danielk1
50dc0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 977 Exp $.*/..#i
50dd0 66 6e 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f fndef _PCACHE_H_
50de0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ..typedef struct
50df0 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79 PgHdr PgHdr;.ty
50e00 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61 pedef struct PCa
50e10 63 68 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a che PCache;../*.
50e20 2a 2a 20 45 76 65 72 79 20 70 61 67 65 20 69 6e ** Every page in
50e30 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 6f the cache is co
50e40 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69 ntrolled by an i
50e50 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
50e60 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
50e70 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
50e80 20 50 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20 PgHdr {. void
50e90 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *pData;
50ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e /* Con
50eb0 74 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 tent of this pag
50ec0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78 e */. void *pEx
50ed0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
50ee0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63 /* Extra c
50ef0 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64 ontent */. PgHd
50f00 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20 r *pDirty;
50f10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
50f20 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20 ansient list of
50f30 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 dirty pages */.
50f40 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f60 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 /* Page number f
50f70 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a or this page */.
50f80 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fa0 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 /* The pager th
50fb0 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 is page is part
50fc0 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c of */.#ifdef SQL
50fd0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
50fe0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20 u32 pageHash;
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51000 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65 /* Hash of page
51010 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 content */.#end
51020 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 if. u16 flags;
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61 /* PGHDR fla
51050 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 gs defined below
51060 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a */.. /********
51070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
510a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 **************.
510b0 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f ** Elements abo
510c0 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20 ve are public.
510d0 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 All that follows
510e0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70 is private to p
510f0 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 cache.c. ** and
51100 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 should not be a
51110 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72 ccessed by other
51120 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 modules.. */.
51130 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 i16 nRef;
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51150 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 /* Number of use
51160 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 rs of this page
51170 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 */. PCache *pCa
51180 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
51190 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61 /* Cache tha
511a0 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 t owns this page
511b0 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44 */.. PgHdr *pD
511c0 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20 irtyNext;
511d0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c /* Next el
511e0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 ement in list of
511f0 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a dirty pages */.
51200 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50 PgHdr *pDirtyP
51210 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 rev;
51220 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65 /* Previous ele
51230 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 ment in list of
51240 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d dirty pages */.}
51250 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73 ;../* Bit values
51260 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73 for PgHdr.flags
51270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 */.#define PGHD
51280 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20 R_DIRTY
51290 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61 0x002 /* Pa
512a0 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a ge has changed *
512b0 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
512c0 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20 NEED_SYNC
512d0 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e 0x004 /* Fsyn
512e0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a c the rollback j
512f0 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20 ournal before.
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51320 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 ** writing
51330 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65 this page to the
51340 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 database */.#de
51350 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f fine PGHDR_NEED_
51360 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30 READ 0x0
51370 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 08 /* Content i
51380 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66 s unread */.#def
51390 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f ine PGHDR_REUSE_
513a0 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31 UNLIKELY 0x01
513b0 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61 0 /* A hint tha
513c0 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b t reuse is unlik
513d0 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ely */.#define P
513e0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 GHDR_DONT_WRITE
513f0 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a 0x020 /*
51400 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f Do not write co
51410 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f ntent to disk */
51420 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
51430 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 and shutdown the
51440 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
51450 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f ystem */.SQLITE_
51460 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
51470 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c te3PcacheInitial
51480 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ize(void);.SQLIT
51490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
514a0 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 qlite3PcacheShut
514b0 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 down(void);../*
514c0 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65 Page cache buffe
514d0 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a r management:.**
514e0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
514f0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45 implement SQLITE
51500 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
51510 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 E..*/.SQLITE_PRI
51520 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
51530 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74 3PCacheBufferSet
51540 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73 up(void *, int s
51550 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 z, int n);../* C
51560 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
51570 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 r cache..** Unde
51580 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c r memory stress,
51590 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20 invoke xStress
515a0 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 to try to make p
515b0 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f ages clean..** O
515c0 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e nly clean and un
515d0 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e pinned pages can
515e0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a be reclaimed..*
515f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
51600 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
51610 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 cheOpen(. int s
51620 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 zPage,
51630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
51640 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 e of every page
51650 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
51660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
51670 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 /* Extra spa
51680 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ce associated wi
51690 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a th each page */.
516a0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
516b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
516c0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 /* True if page
516d0 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 s are on backing
516e0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 store */. int
516f0 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a (*xStress)(void*
51700 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61 , PgHdr*), /* Ca
51710 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b ll to try to mak
51720 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f e pages clean */
51730 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 . void *pStress
51740 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
51750 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
51760 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 xStress */. PC
51770 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20 ache *pToInit
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
51790 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 Preallocated spa
517a0 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68 ce for the PCach
517b0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 e */.);../* Modi
517c0 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 fy the page-size
517d0 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65 after the cache
517e0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
517f0 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d. */.SQLITE_PRI
51800 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
51810 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
51820 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 ze(PCache *, int
51830 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 );../* Return th
51840 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
51850 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
51860 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65 ct. Used to pre
51870 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72 allocate.** stor
51880 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 age space..*/.SQ
51890 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
518a0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 sqlite3PcacheSi
518b0 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e ze(void);../* On
518c0 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75 e release per su
518d0 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20 ccessful fetch.
518e0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20 Page is pinned
518f0 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a until released..
51900 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75 ** Reference cou
51910 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 nted. .*/.SQLITE
51920 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
51930 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
51940 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 PCache*, Pgno, i
51950 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50 nt createFlag, P
51960 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f gHdr**);.SQLITE_
51970 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
51980 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
51990 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 e(PgHdr*);..SQLI
519a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
519b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
519c0 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20 p(PgHdr*);
519d0 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67 /* Remove pag
519e0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a e from cache */.
519f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
51a00 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
51a10 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 eMakeDirty(PgHdr
51a20 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 *); /* Make s
51a30 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b ure page is mark
51a40 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49 ed dirty */.SQLI
51a50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
51a60 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
51a70 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20 eClean(PgHdr*);
51a80 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e /* Mark a sin
51a90 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 gle page as clea
51aa0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n */.SQLITE_PRIV
51ab0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
51ac0 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 PcacheCleanAll(P
51ad0 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d Cache*); /* M
51ae0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69 ark all dirty li
51af0 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61 st pages as clea
51b00 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 n */../* Change
51b10 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 a page number.
51b20 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63 Used by incr-vac
51b30 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 uum. */.SQLITE_P
51b40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51b50 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67 te3PcacheMove(Pg
51b60 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a Hdr*, Pgno);../*
51b70 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65 Remove all page
51b80 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20 s with pgno>x.
51b90 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20 Reset the cache
51ba0 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 if x==0 */.SQLIT
51bb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51bc0 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
51bd0 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67 cate(PCache*, Pg
51be0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 no x);../* Get a
51bf0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 list of all dir
51c00 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
51c10 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 cache, sorted by
51c20 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
51c30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
51c40 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 gHdr *sqlite3Pca
51c50 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 cheDirtyList(PCa
51c60 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 che*);../* Reset
51c70 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63 and close the c
51c80 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 ache object */.S
51c90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51ca0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
51cb0 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a Close(PCache*);.
51cc0 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20 ./* Clear flags
51cd0 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68 from pages of th
51ce0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a e page cache */.
51cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
51d00 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
51d10 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 eClearSyncFlags(
51d20 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 PCache *);../* D
51d30 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
51d40 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
51d50 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
51d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
51d70 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 cacheClear(PCach
51d80 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 e*);../* Return
51d90 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
51da0 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 of outstanding
51db0 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 page references
51dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
51dd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
51de0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 cheRefCount(PCac
51df0 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d he*);../* Increm
51e00 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 ent the referenc
51e10 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78 e count of an ex
51e20 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53 isting page */.S
51e30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51e40 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
51e50 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 Ref(PgHdr*);..SQ
51e60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
51e70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
51e80 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 geRefcount(PgHdr
51e90 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 *);../* Return t
51ea0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
51eb0 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20 of pages stored
51ec0 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a in the cache */.
51ed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
51ee0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
51ef0 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 Pagecount(PCache
51f00 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 *);..#ifdef SQLI
51f10 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f TE_CHECK_PAGES./
51f20 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 * Iterate throug
51f30 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 h all dirty page
51f40 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 s currently stor
51f50 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ed in the cache.
51f60 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 This.** interfa
51f70 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ce is only avail
51f80 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43 able if SQLITE_C
51f90 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 HECK_PAGES is de
51fa0 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a fined when the .
51fb0 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75 ** library is bu
51fc0 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ilt..*/.SQLITE_P
51fd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51fe0 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 te3PcacheIterate
51ff0 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43 Dirty(PCache *pC
52000 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74 ache, void (*xIt
52010 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23 er)(PgHdr *));.#
52020 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e endif../* Set an
52030 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73 d get the sugges
52040 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66 ted cache-size f
52050 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 or the specified
52060 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a pager-cache..**
52070 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c .** If no global
52080 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66 maximum is conf
52090 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 igured, then the
520a0 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73 system attempts
520b0 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 to limit.** the
520c0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
520d0 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79 pages cached by
520e0 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72 purgeable pager
520f0 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73 -caches to the s
52100 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67 um.** of the sug
52110 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a gested cache-siz
52120 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
52130 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52140 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 e3PcacheSetCache
52150 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 size(PCache *, i
52160 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 nt);.#ifdef SQLI
52170 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 TE_TEST.SQLITE_P
52180 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
52190 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
521a0 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a size(PCache *);.
521b0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
521c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
521d0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f ORY_MANAGEMENT./
521e0 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20 * Try to return
521f0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 memory used by t
52200 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 he pcache module
52210 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d to the main mem
52220 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49 ory heap */.SQLI
52230 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52240 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
52250 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a aseMemory(int);.
52260 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
52270 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
52280 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52290 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 qlite3PcacheStat
522a0 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a s(int*,int*,int*
522b0 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a ,int*);.#endif..
522c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
522d0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
522e0 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 eSetDefault(void
522f0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 );..#endif /* _P
52300 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a CACHE_H_ */../**
52310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
52320 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a of pcache.h ***
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
52370 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
52380 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
52390 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
523a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
523b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
523c0 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68 clude os.h in th
523d0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
523e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
523f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
52400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
52410 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a gin file os.h **
52420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
52450 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
52460 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 16.**.** The
52470 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
52480 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
52490 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
524a0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
524b0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
524c0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
524d0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
524e0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
524f0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
52500 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
52510 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
52520 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
52530 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
52540 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
52550 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
52560 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
52570 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
52580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
525d0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
525e0 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 (together with
525f0 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 is companion C s
52600 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a ource-code file.
52610 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d ** "os.c") attem
52620 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74 pt to abstract t
52630 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 he underlying op
52640 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 erating system s
52650 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51 o that.** the SQ
52660 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c Lite library wil
52670 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50 l work on both P
52680 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73 OSIX and windows
52690 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 systems..**.**
526a0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
526b0 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20 is #include-ed
526c0 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61 by sqliteInt.h a
526d0 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a nd thus ends up.
526e0 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65 ** being include
526f0 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63 d by every sourc
52700 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 e file..**.** $I
52710 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31 30 38 20 d: os.h,v 1.108
52720 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33 31 2009/02/05 16:31
52730 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :46 drh Exp $.*/
52740 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 .#ifndef _SQLITE
52750 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f _OS_H_.#define _
52760 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a SQLITE_OS_H_../*
52770 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 .** Figure out i
52780 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 f we are dealing
52790 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64 with Unix, Wind
527a0 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68 ows, or some oth
527b0 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 er.** operating
527c0 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74 system. After t
527d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
527e0 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73 ck of preprocess
527f0 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 macros,.** all
52800 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 of SQLITE_OS_UNI
52810 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e X, SQLITE_OS_WIN
52820 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c , SQLITE_OS_OS2,
52830 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f and SQLITE_OS_O
52840 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 THER .** will de
52850 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20 fined to either
52860 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20 1 or 0. One of
52870 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65 the four will be
52880 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a 1. The other .
52890 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65 ** three will be
528a0 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 0..*/.#if defin
528b0 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ed(SQLITE_OS_OTH
528c0 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f ER).# if SQLITE_
528d0 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20 OS_OTHER==1.#
528e0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f undef SQLITE_OS_
528f0 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20 UNIX.# define
52900 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
52910 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 .# undef SQLIT
52920 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66 E_OS_WIN.# def
52930 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
52940 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 N 0.# undef SQ
52950 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 LITE_OS_OS2.#
52960 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
52970 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20 _OS2 0.# else.#
52980 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f undef SQLITE_O
52990 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a S_OTHER.# endif.
529a0 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 #endif.#if !defi
529b0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ned(SQLITE_OS_UN
529c0 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IX) && !defined(
529d0 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 SQLITE_OS_OTHER)
529e0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
529f0 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66 _OS_OTHER 0.# if
52a00 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 ndef SQLITE_OS_W
52a10 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65 IN.# if define
52a20 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 d(_WIN32) || def
52a30 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64 ined(WIN32) || d
52a40 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f efined(__CYGWIN_
52a50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f _) || defined(__
52a60 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65 MINGW32__) || de
52a70 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 fined(__BORLANDC
52a80 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 __).# define
52a90 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31 SQLITE_OS_WIN 1
52aa0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
52ab0 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 LITE_OS_UNIX 0.#
52ac0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
52ad0 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20 TE_OS_OS2 0.#
52ae0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45 elif defined(__E
52af0 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 MX__) || defined
52b00 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 (_OS2) || define
52b10 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 d(OS2) || define
52b20 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69 d(_OS2_) || defi
52b30 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 ned(__OS2__).#
52b40 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
52b50 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 _OS_WIN 0.#
52b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
52b70 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 _UNIX 0.# de
52b80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
52b90 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20 S2 1.# else.#
52ba0 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
52bb0 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 E_OS_WIN 0.#
52bc0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
52bd0 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64 S_UNIX 1.# d
52be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
52bf0 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 OS2 0.# endif.#
52c00 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 else.# define
52c10 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
52c20 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
52c30 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64 E_OS_OS2 0.# end
52c40 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65 if.#else.# ifnde
52c50 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a f SQLITE_OS_WIN.
52c60 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
52c70 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69 _OS_WIN 0.# endi
52c80 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
52c90 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
52ca0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
52cb0 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
52cc0 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
52cd0 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
52ce0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 .#if defined(_WI
52cf0 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e N32_WCE).# defin
52d00 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 e SQLITE_OS_WINC
52d10 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 E 1.#else.# defi
52d20 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
52d30 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a CE 0.#endif.../*
52d40 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d .** Define the m
52d50 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 aximum size of a
52d60 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e temporary filen
52d70 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ame.*/.#if SQLIT
52d80 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 E_OS_WIN.# inclu
52d90 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 de <windows.h>.#
52da0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 define SQLITE_T
52db0 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41 EMPNAME_SIZE (MA
52dc0 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66 X_PATH+50).#elif
52dd0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 SQLITE_OS_OS2.#
52de0 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 if (__GNUC__ >
52df0 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 3 || __GNUC__ ==
52e00 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 3 && __GNUC_MIN
52e10 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65 OR__ >= 3) && de
52e20 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d fined(OS2_HIGH_M
52e30 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64 EMORY).# includ
52e40 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a e <os2safe.h> /*
52e50 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 has to be inclu
52e60 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68 ded before os2.h
52e70 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 for linking to
52e80 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a work */.# endif.
52e90 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
52ea0 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69 SDATETIME.# defi
52eb0 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d ne INCL_DOSFILEM
52ec0 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c GR.# define INCL
52ed0 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66 _DOSERRORS.# def
52ee0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 ine INCL_DOSMISC
52ef0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
52f00 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69 OSPROCESS.# defi
52f10 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c ne INCL_DOSMODUL
52f20 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e EMGR.# define IN
52f30 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53 CL_DOSSEMAPHORES
52f40 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e .# include <os2.
52f50 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63 h>.# include <uc
52f60 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 onv.h>.# define
52f70 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f SQLITE_TEMPNAME_
52f80 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48 SIZE (CCHMAXPATH
52f90 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 COMP).#else.# de
52fa0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 fine SQLITE_TEMP
52fb0 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65 NAME_SIZE 200.#e
52fc0 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20 ndif../* If the
52fd0 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 SET_FULLSYNC mac
52fe0 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 ro is not define
52ff0 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 d above, then ma
53000 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ke it.** a no-op
53010 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f .*/.#ifndef SET_
53020 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e FULLSYNC.# defin
53030 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78 e SET_FULLSYNC(x
53040 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,y).#endif../*.*
53050 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 * The default si
53060 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 ze of a disk sec
53070 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 tor.*/.#ifndef S
53080 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
53090 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69 CTOR_SIZE.# defi
530a0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
530b0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 T_SECTOR_SIZE 51
530c0 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 2.#endif../*.**
530d0 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 Temporary files
530e0 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 are named starti
530f0 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65 ng with this pre
53100 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 fix followed by
53110 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 16 random.** alp
53120 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 hanumeric charac
53130 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c ters, and no fil
53140 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65 e extension. The
53150 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 y are stored in
53160 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e the.** OS's stan
53170 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66 dard temporary f
53180 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61 ile directory, a
53190 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70 nd are deleted p
531a0 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a rior to exit..**
531b0 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65 If sqlite is be
531c0 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20 ing embedded in
531d0 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c another program,
531e0 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f you may wish to
531f0 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70 change the.** p
53200 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74 refix to reflect
53210 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20 your program's
53220 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66 name, so that if
53230 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78 your program ex
53240 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65 its.** premature
53250 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72 ly, old temporar
53260 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65 y files can be e
53270 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64 asily identified
53280 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f . This can be do
53290 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51 ne.** using -DSQ
532a0 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
532b0 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20 REFIX=myprefix_
532c0 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 on the compiler
532d0 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a command line..**
532e0 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20 .** 2006-10-31:
532f0 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65 The default pre
53300 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22 fix used to be "
53310 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74 sqlite_". But t
53320 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74 hen.** Mcafee st
53330 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69 arted using SQLi
53340 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69 te in their anti
53350 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61 -virus product a
53360 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64 nd it.** started
53370 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77 putting files w
53380 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22 ith the "sqlite"
53390 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f name in the c:/
533a0 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 temp folder..**
533b0 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e This annoyed man
533c0 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e y windows users.
533d0 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f Those users wo
533e0 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a uld then do a .*
533f0 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20 * Google search
53400 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69 for "sqlite", fi
53410 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65 nd the telephone
53420 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a numbers of the.
53430 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e ** developers an
53440 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74 d call to wake t
53450 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20 hem up at night
53460 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a and complain..**
53470 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e For this reason
53480 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 , the default na
53490 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61 me prefix is cha
534a0 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 nged to be "sqli
534b0 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 te" .** spelled
534c0 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74 backwards. So t
534d0 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72 he temp files ar
534e0 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69 e still identifi
534f0 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f ed, but.** anybo
53500 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20 dy smart enough
53510 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 to figure out th
53520 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c e code is also l
53530 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65 ikely smart.** e
53540 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68 nough to know th
53550 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 at calling the d
53560 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f eveloper will no
53570 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a t help get rid.*
53580 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a * of the file..*
53590 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
535a0 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
535b0 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 X.# define SQLIT
535c0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
535d0 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e IX "etilqs_".#en
535e0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
535f0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20 ollowing values
53600 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73 may be passed as
53610 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
53620 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 ment to.** sqlit
53630 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20 e3OsLock(). The
53640 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78 various locks ex
53650 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77 hibit the follow
53660 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a ing semantics:.*
53670 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20 *.** SHARED:
53680 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 Any number of pr
53690 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 ocesses may hold
536a0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73 a SHARED lock s
536b0 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a imultaneously..*
536c0 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73 * RESERVED: A s
536d0 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 ingle process ma
536e0 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 y hold a RESERVE
536f0 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 D lock on a file
53700 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 at.**
53710 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65 any time. Othe
53720 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 r processes may
53730 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20 hold and obtain
53740 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 new SHARED locks
53750 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20 ..** PENDING:
53760 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 A single process
53770 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44 may hold a PEND
53780 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 ING lock on a fi
53790 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 le at.**
537a0 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65 any one time
537b0 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45 . Existing SHARE
537c0 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73 D locks may pers
537d0 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a ist, but no new.
537e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48 ** SH
537f0 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62 ARED locks may b
53800 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74 e obtained by ot
53810 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a her processes..*
53820 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20 * EXCLUSIVE: An
53830 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70 EXCLUSIVE lock p
53840 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 recludes all oth
53850 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 er locks..**.**
53860 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 PENDING_LOCK may
53870 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64 not be passed d
53880 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74 irectly to sqlit
53890 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 e3OsLock(). Inst
538a0 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73 ead, a.** proces
538b0 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20 s that requests
538c0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
538d0 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f k may actually o
538e0 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a btain a PENDING.
538f0 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61 ** lock. This ca
53900 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f n be upgraded to
53910 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
53920 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65 ck by a subseque
53930 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 nt call to.** sq
53940 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a lite3OsLock()..*
53950 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 /.#define NO_LOC
53960 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 K 0.#def
53970 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ine SHARED_LOCK
53980 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45 1.#define RE
53990 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a SERVED_LOCK 2.
539a0 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f #define PENDING_
539b0 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e LOCK 3.#defin
539c0 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b e EXCLUSIVE_LOCK
539d0 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4../*.** File
539e0 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20 Locking Notes:
539f0 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69 (Mostly about wi
53a00 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73 ndows but also s
53a10 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69 ome info for Uni
53a20 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e x).**.** We cann
53a30 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45 ot use LockFileE
53a40 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c x() or UnlockFil
53a50 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39 eEx() on Win95/9
53a60 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20 8/ME because.**
53a70 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 those functions
53a80 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c are not availabl
53a90 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e e. So we use on
53aa0 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e ly LockFile() an
53ab0 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 d.** UnlockFile(
53ac0 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c )..**.** LockFil
53ad0 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74 e() prevents not
53ae0 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75 just writing bu
53af0 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62 t also reading b
53b00 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 y other processe
53b10 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c s..** A SHARED_L
53b20 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 OCK is obtained
53b30 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e by locking a sin
53b40 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f gle randomly-cho
53b50 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74 sen .** byte out
53b60 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72 of a specific r
53b70 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54 ange of bytes. T
53b80 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20 he lock byte is
53b90 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20 obtained at .**
53ba0 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65 random so two se
53bb0 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63 parate readers c
53bc0 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65 an probably acce
53bd0 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74 ss the file at t
53be0 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 he .** same time
53bf0 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72 , unless they ar
53c00 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68 e unlucky and ch
53c10 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f oose the same lo
53c20 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 ck byte..** An E
53c30 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73 XCLUSIVE_LOCK is
53c40 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
53c50 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69 king all bytes i
53c60 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 n the range..**
53c70 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 There can only b
53c80 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41 e one writer. A
53c90 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69 RESERVED_LOCK i
53ca0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
53cb0 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c cking.** a singl
53cc0 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69 e byte of the fi
53cd0 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67 le that is desig
53ce0 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73 nated as the res
53cf0 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e erved lock byte.
53d00 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f .** A PENDING_LO
53d10 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 CK is obtained b
53d20 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69 y locking a desi
53d30 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66 gnated byte diff
53d40 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 erent from.** th
53d50 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 e RESERVED_LOCK
53d60 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 byte..**.** On W
53d70 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65 inNT/2K/XP syste
53d80 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 ms, LockFileEx()
53d90 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 and UnlockFileE
53da0 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c x() are availabl
53db0 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e e,.** which mean
53dc0 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61 s we can use rea
53dd0 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 der/writer locks
53de0 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77 . When reader/w
53df0 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 riter locks.** a
53e00 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63 re used, the loc
53e10 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74 k is placed on t
53e20 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66 he same range of
53e30 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75 bytes that is u
53e40 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61 sed.** for proba
53e50 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67 bilistic locking
53e60 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e in Win95/98/ME.
53e70 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63 Hence, the loc
53e80 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77 king scheme.** w
53e90 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20 ill support two
53ea0 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65 or more Win95 re
53eb0 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20 aders or two or
53ec0 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65 more WinNT reade
53ed0 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e rs..** But a sin
53ee0 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 gle Win95 reader
53ef0 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 will lock out a
53f00 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 ll WinNT readers
53f10 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a and a single.**
53f20 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69 WinNT reader wi
53f30 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 ll lock out all
53f40 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64 other Win95 read
53f50 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ers..**.** The f
53f60 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65 ollowing #define
53f70 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61 s specify the ra
53f80 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65 nge of bytes use
53f90 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a d for locking..*
53fa0 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 * SHARED_SIZE is
53fb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
53fc0 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69 ytes available i
53fd0 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 n the pool from
53fe0 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f which.** a rando
53ff0 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74 m byte is select
54000 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20 ed for a shared
54010 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20 lock. The pool
54020 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20 of bytes for.**
54030 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67 shared locks beg
54040 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49 ins at SHARED_FI
54050 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 RST. .**.** The
54060 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 same locking str
54070 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74 ategy and.** byt
54080 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 e ranges are use
54090 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 d for Unix. Thi
540a0 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 s leaves open th
540b0 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 e possiblity of
540c0 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 having.** client
540d0 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e s on win95, winN
540e0 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 T, and unix all
540f0 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 talking to the s
54100 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a ame shared file.
54110 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 ** and all locki
54120 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 ng correctly. T
54130 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65 o do so would re
54140 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61 quire that samba
54150 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a (or whatever.**
54160 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75 tool is being u
54170 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61 sed for file sha
54180 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73 ring) implements
54190 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79 locks correctly
541a0 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 between.** wind
541b0 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49 ows and unix. I
541c0 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74 'm guessing that
541d0 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f isn't likely to
541e0 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a happen, but by.
541f0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ** using the sam
54200 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
54210 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 we are at least
54220 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73 open to the poss
54230 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c ibility..**.** L
54240 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77 ocking in window
54250 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20 s is manditory.
54260 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e For this reason
54270 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72 , we cannot stor
54280 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61 e.** actual data
54290 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73 in the bytes us
542a0 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 ed for locking.
542b0 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72 The pager never
542c0 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68 allocates.** th
542d0 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 e pages involved
542e0 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72 in locking ther
542f0 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53 efore. SHARED_S
54300 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20 IZE is selected
54310 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c so.** that all l
54320 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e ocks will fit on
54330 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65 a single page e
54340 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d ven at the minim
54350 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a um page size..**
54360 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65 PENDING_BYTE de
54370 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e fines the beginn
54380 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 ing of the locks
54390 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45 . By default PE
543a0 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73 NDING_BYTE.** is
543b0 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61 set high so tha
543c0 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 t we don't have
543d0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75 to allocate an u
543e0 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70 nused page excep
543f0 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61 t.** for very la
54400 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20 rge databases.
54410 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74 But one should t
54420 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69 est the page ski
54430 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 pping logic .**
54440 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49 by setting PENDI
54450 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20 NG_BYTE low and
54460 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 running the enti
54470 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75 re regression su
54480 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 ite..**.** Chang
54490 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ing the value of
544a0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 PENDING_BYTE re
544b0 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c sults in a subtl
544c0 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a y incompatible.*
544d0 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 * file format.
544e0 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 Depending on how
544f0 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 it is changed,
54500 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f you might not no
54510 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f tice.** the inco
54520 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68 mpatibility righ
54530 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e t away, even run
54540 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72 ning a full regr
54550 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 ession test..**
54560 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61 The default loca
54570 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f tion of PENDING_
54580 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73 BYTE is the firs
54590 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a t byte past the.
545a0 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e ** 1GB boundary.
545b0 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 .**.*/.#define P
545c0 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 ENDING_BYTE
545d0 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 sqlite3PendingB
545e0 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45 yte.#define RESE
545f0 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50 RVED_BYTE (P
54600 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23 ENDING_BYTE+1).#
54610 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49 define SHARED_FI
54620 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e RST (PENDIN
54630 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e G_BYTE+2).#defin
54640 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20 e SHARED_SIZE
54650 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 510../* .**
54660 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 Functions for ac
54670 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f cessing sqlite3_
54680 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f file methods .*/
54690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
546a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f int sqlite3OsClo
546b0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a se(sqlite3_file*
546c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
546d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 E int sqlite3OsR
546e0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ead(sqlite3_file
546f0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d *, void*, int am
54700 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a t, i64 offset);.
54710 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54720 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 nt sqlite3OsWrit
54730 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
54740 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
54750 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
54760 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
54770 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54780 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 Truncate(sqlite3
54790 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65 _file*, i64 size
547a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
547b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 E int sqlite3OsS
547c0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
547d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
547e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
547f0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 te3OsFileSize(sq
54800 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 lite3_file*, i64
54810 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 *pSize);.SQLITE
54820 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54830 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 ite3OsLock(sqlit
54840 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
54850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54860 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f nt sqlite3OsUnlo
54870 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ck(sqlite3_file*
54880 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
54890 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
548a0 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
548b0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 dLock(sqlite3_fi
548c0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 le *id, int *pRe
548d0 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sOut);.SQLITE_PR
548e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
548f0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 3OsFileControl(s
54900 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 qlite3_file*,int
54910 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 ,void*);.#define
54920 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 SQLITE_FCNTL_DB
54930 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30 _UNCHANGED 0xca0
54940 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49 93fa0.SQLITE_PRI
54950 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
54960 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c OsSectorSize(sql
54970 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a ite3_file *id);.
54980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54990 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 nt sqlite3OsDevi
549a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
549b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
549c0 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e id);../* .** Fun
549d0 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 ctions for acces
549e0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73 sing sqlite3_vfs
549f0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c methods .*/.SQL
54a00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54a10 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71 sqlite3OsOpen(sq
54a20 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
54a30 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 st char *, sqlit
54a40 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 e3_file*, int, i
54a50 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt *);.SQLITE_PR
54a60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
54a70 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 3OsDelete(sqlite
54a80 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
54a90 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c har *, int);.SQL
54aa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54ab0 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
54ac0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
54ad0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
54ae0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b , int *pResOut);
54af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54b00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
54b10 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 lPathname(sqlite
54b20 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
54b30 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 har *, int, char
54b40 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *);.#ifndef SQL
54b50 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
54b60 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 TENSION.SQLITE_P
54b70 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
54b80 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c ite3OsDlOpen(sql
54b90 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
54ba0 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
54bb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
54bc0 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 qlite3OsDlError(
54bd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
54be0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nt, char *);.SQL
54bf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
54c00 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 (*sqlite3OsDlSy
54c10 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c m(sqlite3_vfs *,
54c20 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 void *, const c
54c30 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53 har *))(void);.S
54c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
54c50 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c id sqlite3OsDlCl
54c60 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
54c70 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 *, void *);.#end
54c80 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
54c90 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
54ca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
54cb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54cc0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
54cd0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 e3_vfs *, int, c
54ce0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
54cf0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54d00 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 e3OsSleep(sqlite
54d10 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 3_vfs *, int);.S
54d20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54d30 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 t sqlite3OsCurre
54d40 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 ntTime(sqlite3_v
54d50 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a fs *, double*);.
54d60 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e ./*.** Convenien
54d70 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ce functions for
54d80 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f opening and clo
54d90 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 sing files using
54da0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c .** sqlite3_mal
54db0 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 loc() to obtain
54dc0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 space for the fi
54dd0 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 le-handle struct
54de0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ure..*/.SQLITE_P
54df0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54e00 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 e3OsOpenMalloc(s
54e10 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
54e20 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 nst char *, sqli
54e30 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 te3_file **, int
54e40 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ,int*);.SQLITE_P
54e50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54e60 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 e3OsCloseFree(sq
54e70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a lite3_file *);..
54e80 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 #endif /* _SQLIT
54e90 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a E_OS_H_ */../***
54ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
54eb0 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a of os.h ********
54ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
54ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
54f00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
54f10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
54f20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
54f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
54f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
54f50 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 ude mutex.h in t
54f60 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
54f70 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
54f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
54f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
54fa0 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a n file mutex.h *
54fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
54fe0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 * 2007 August 28
54ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
55000 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
55010 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
55020 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
55030 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
55040 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
55050 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
55060 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
55070 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
55080 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
55090 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
550a0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
550b0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
550c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
550d0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
550e0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
550f0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
55100 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
55110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
55150 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
55160 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d contains the com
55170 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61 mon header for a
55180 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 ll mutex impleme
55190 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 ntations..** The
551a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 sqliteInt.h hea
551b0 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68 der #includes th
551c0 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 is file so that
551d0 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a it is available.
551e0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 ** to all source
551f0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 files. We brea
55200 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 k it out in an e
55210 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 ffort to keep th
55220 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72 e code.** better
55230 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a organized..**.*
55240 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20 * NOTE: source
55250 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f files should *no
55260 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 t* #include this
55270 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72 header file dir
55280 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 ectly..** Source
55290 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69 files should #i
552a0 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74 nclude the sqlit
552b0 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20 eInt.h file and
552c0 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a let that file.**
552d0 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e include this on
552e0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a e indirectly..**
552f0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68 .** $Id: mutex.h
55300 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30 ,v 1.9 2008/10/0
55310 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
55320 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 xp $.*/.../*.**
55330 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20 Figure out what
55340 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 version of the c
55350 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65 ode to use. The
55360 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a choices are.**.
55370 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
55380 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e X_OMIT N
55390 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20 o mutex logic.
553a0 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20 Not even stubs.
553b0 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 The.**
553c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
553d0 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c mutexes impl
553e0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 emention cannot
553f0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a be overridden.**
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55410 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20 at
55420 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a start-time..**.*
55430 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
55440 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f _NOOP Fo
55450 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 r single-threade
55460 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 d applications.
55470 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 No.**
55480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55490 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 mutual exclus
554a0 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e ion is provided.
554b0 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20 But this.**
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
554d0 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d implem
554e0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 entation can be
554f0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a overridden at.**
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55510 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
55520 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 rt-time..**.**
55530 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
55540 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d HREADS For m
55550 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
55560 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e plications on Un
55570 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 ix..**.** SQLI
55580 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 TE_MUTEX_W32
55590 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
555a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
555b0 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a tions on Win32..
555c0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
555d0 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20 UTEX_OS2
555e0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
555f0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
55600 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 s on OS/2..*/.#i
55610 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44 f !SQLITE_THREAD
55620 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SAFE.# define SQ
55630 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a LITE_MUTEX_OMIT.
55640 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
55650 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
55660 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
55670 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 MUTEX_NOOP).# i
55680 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
55690 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
556a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
556b0 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 DS.# elif SQLIT
556c0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65 E_OS_WIN.# de
556d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
556e0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51 X_W32.# elif SQ
556f0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 LITE_OS_OS2.#
55700 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
55710 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65 UTEX_OS2.# else
55720 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
55730 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 ITE_MUTEX_NOOP.#
55740 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a endif.#endif..
55750 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
55760 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 TEX_OMIT./*.** I
55770 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f f this is a no-o
55780 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e p implementation
55790 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72 , implement ever
557a0 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73 ything as macros
557b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c ..*/.#define sql
557c0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
557d0 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33 (X) ((sqlite3
557e0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 _mutex*)8).#defi
557f0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
55800 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65 _free(X).#define
55810 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
55820 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20 nter(X).#define
55830 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
55840 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45 y(X) SQLITE
55850 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 _OK.#define sqli
55860 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
55870 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 X).#define sqlit
55880 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29 e3_mutex_held(X)
55890 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73 1.#define s
558a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
558b0 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69 held(X) 1.#defi
558c0 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ne sqlite3MutexA
558d0 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73 lloc(X) ((s
558e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 qlite3_mutex*)8)
558f0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
55900 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20 MutexInit()
55910 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 SQLITE_OK.#de
55920 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
55930 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a xEnd().#endif /*
55940 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
55950 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a OMIT_MUTEX) */..
55960 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
55970 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a End of mutex.h *
55980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
559b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
559c0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
559d0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
559e0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
55a00 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 ../*.** Each dat
55a10 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65 abase file to be
55a20 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 accessed by the
55a30 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e system is an in
55a40 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
55a50 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
55a60 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 ture. There are
55a70 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 normally two of
55a80 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
55a90 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 s.** in the sqli
55aa0 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 te.aDb[] array.
55ab0 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d aDb[0] is the m
55ac0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
55ad0 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 e and.** aDb[1]
55ae0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
55af0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c file used to hol
55b00 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c d temporary tabl
55b10 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a es. Additional.
55b20 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 ** databases may
55b30 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f be attached..*/
55b40 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63 .struct Db {. c
55b50 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
55b60 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
55b70 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a his database */.
55b80 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 Btree *pBt;
55b90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a /* The B*
55ba0 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66 Tree structure f
55bb0 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
55bc0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e file */. u8 in
55bd0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 Trans;
55be0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62 /* 0: not writab
55bf0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74 le. 1: Transact
55c00 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f ion. 2: Checkpo
55c10 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65 int */. u8 safe
55c20 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a ty_level; /*
55c30 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20 How aggressive
55c40 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20 at syncing data
55c50 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 to disk */. voi
55c60 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 d *pAux;
55c70 20 20 20 20 20 20 20 2f 2a 20 41 75 78 69 6c 69 /* Auxili
55c80 61 72 79 20 64 61 74 61 2e 20 20 55 73 75 61 6c ary data. Usual
55c90 6c 79 20 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 ly NULL */. voi
55ca0 64 20 28 2a 78 46 72 65 65 41 75 78 29 28 76 6f d (*xFreeAux)(vo
55cb0 69 64 2a 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e id*); /* Routin
55cc0 65 20 74 6f 20 66 72 65 65 20 70 41 75 78 20 2a e to free pAux *
55cd0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
55ce0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e ema; /* Poin
55cf0 74 65 72 20 74 6f 20 64 61 74 61 62 61 73 65 20 ter to database
55d00 73 63 68 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 schema (possibly
55d10 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a shared) */.};..
55d20 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
55d30 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
55d40 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 74 6f ng structure sto
55d50 72 65 73 20 61 20 64 61 74 61 62 61 73 65 20 73 res a database s
55d60 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 chema..**.** If
55d70 74 68 65 72 65 20 61 72 65 20 6e 6f 20 76 69 72 there are no vir
55d80 74 75 61 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 tual tables conf
55d90 69 67 75 72 65 64 20 69 6e 20 74 68 69 73 20 73 igured in this s
55da0 63 68 65 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 chema, the.** Sc
55db0 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c 65 hema.db variable
55dc0 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
55dd0 20 41 66 74 65 72 20 74 68 65 20 66 69 72 73 74 After the first
55de0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a virtual table.*
55df0 2a 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 * has been added
55e00 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 70 , it is set to p
55e10 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 oint to the data
55e20 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
55e30 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 .** used to crea
55e40 74 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f te the connectio
55e50 6e 2e 20 4f 6e 63 65 20 61 20 76 69 72 74 75 61 n. Once a virtua
55e60 6c 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e l table has been
55e70 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 .** added to the
55e80 20 53 63 68 65 6d 61 20 73 74 72 75 63 74 75 72 Schema structur
55e90 65 20 61 6e 64 20 74 68 65 20 53 63 68 65 6d 61 e and the Schema
55ea0 2e 64 62 20 76 61 72 69 61 62 6c 65 20 70 6f 70 .db variable pop
55eb0 75 6c 61 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 ulated, .** only
55ec0 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63 that database c
55ed0 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 onnection may us
55ee0 65 20 74 68 65 20 53 63 68 65 6d 61 20 74 6f 20 e the Schema to
55ef0 70 72 65 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 prepare .** stat
55f00 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 ements..*/.struc
55f10 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 t Schema {. int
55f20 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 schema_cookie;
55f30 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 63 /* Database sc
55f40 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d hema version num
55f50 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69 6c ber for this fil
55f60 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 e */. Hash tblH
55f70 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ash; /* A
55f80 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65 ll tables indexe
55f90 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 d by name */. H
55fa0 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20 ash idxHash;
55fb0 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 /* All (name
55fc0 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78 d) indices index
55fd0 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 ed by name */.
55fe0 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20 Hash trigHash;
55ff0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 /* All trig
56000 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20 gers indexed by
56010 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 name */. Table
56020 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f *pSeqTab; /
56030 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 * The sqlite_seq
56040 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 uence table used
56050 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e by AUTOINCREMEN
56060 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 T */. u8 file_f
56070 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 ormat; /* S
56080 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 chema format ver
56090 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 sion for this fi
560a0 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 le */. u8 enc;
560b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
560c0 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 Text encoding us
560d0 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62 ed by this datab
560e0 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 ase */. u16 fla
560f0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a gs; /*
56100 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 Flags associate
56110 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65 d with this sche
56120 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 ma */. int cach
56130 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 e_size; /*
56140 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
56150 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 to use in the ca
56160 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 che */.#ifndef S
56170 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
56180 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 ALTABLE. sqlite
56190 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 3 *db; /
561a0 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 * "Owner" connec
561b0 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e tion. See commen
561c0 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 t above */.#endi
561d0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 f.};../*.** Thes
561e0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 e macros can be
561f0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 used to test, se
56200 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 t, or clear bits
56210 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 in the .** Db.f
56220 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
56230 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 define DbHasProp
56240 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 erty(D,I,P)
56250 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 (((D)->aDb[I].pS
56260 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 chema->flags&(P)
56270 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
56280 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 DbHasAnyProperty
56290 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e (D,I,P) (((D)->
562a0 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[I].pSchema->
562b0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
562c0 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 define DbSetProp
562d0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 erty(D,I,P)
562e0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
562f0 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a ema->flags|=(P).
56300 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 #define DbClearP
56310 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
56320 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 (D)->aDb[I].pSc
56330 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 hema->flags&=~(P
56340 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 )../*.** Allowed
56350 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
56360 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a DB.flags field..
56370 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 **.** The DB_Sch
56380 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 emaLoaded flag i
56390 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 s set after the
563a0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
563b0 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 has been.** read
563c0 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 into internal h
563d0 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a ash tables..**.*
563e0 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 * DB_UnresetView
563f0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 s means that one
56400 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 or more views h
56410 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ave column names
56420 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 that.** have be
56430 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 en filled out.
56440 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 If the schema ch
56450 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c anges, these col
56460 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a umn names might.
56470 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 ** changes and s
56480 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 o the view will
56490 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 need to be reset
564a0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f ..*/.#define DB_
564b0 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 SchemaLoaded
564c0 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 0x0001 /* The s
564d0 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c chema has been l
564e0 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 oaded */.#define
564f0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 DB_UnresetViews
56500 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 0x0002 /* S
56510 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64 ome views have d
56520 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 efined column na
56530 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 mes */.#define D
56540 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 B_Empty
56550 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 0x0004 /* The
56560 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 file is empty (
56570 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 length 0 bytes)
56580 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 */../*.** The nu
56590 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e mber of differen
565a0 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 t kinds of thing
565b0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 s that can be li
565c0 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 mited.** using t
565d0 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 he sqlite3_limit
565e0 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f () interface..*/
565f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
56600 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f N_LIMIT (SQLITE_
56610 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
56620 55 4d 42 45 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 UMBER+1)../*.**
56630 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 Lookaside malloc
56640 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 is a set of fix
56650 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 ed-size buffers
56660 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 that can be used
56670 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 .** to satisfy s
56680 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d mall transient m
56690 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
566a0 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 requests for ob
566b0 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 jects.** associa
566c0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 ted with a parti
566d0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
566e0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
566f0 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 use of.** lookas
56700 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 ide malloc provi
56710 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e des a significan
56720 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e t performance en
56730 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 hancement.** (ap
56740 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f prox 10%) by avo
56750 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d iding numerous m
56760 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 alloc/free reque
56770 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e sts while parsin
56780 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 g.** SQL stateme
56790 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c nts..**.** The L
567a0 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 ookaside structu
567b0 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 re holds configu
567c0 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 ration informati
567d0 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 on about the.**
567e0 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 lookaside malloc
567f0 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 subsystem. Eac
56800 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f h available memo
56810 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e ry allocation in
56820 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 .** the lookasid
56830 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 e subsystem is s
56840 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 tored on a linke
56850 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 d list of Lookas
56860 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 ideSlot.** objec
56870 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 ts..**.** Lookas
56880 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ide allocations
56890 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 are only allowed
568a0 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 for objects tha
568b0 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 t are associated
568c0 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 .** with a parti
568d0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
568e0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 onnection. Henc
568f0 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d e, schema inform
56900 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 ation cannot.**
56910 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f be stored in loo
56920 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 20 69 kaside because i
56930 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d n shared cache m
56940 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 ode the schema i
56950 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 nformation.** is
56960 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 shared by multi
56970 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ple database con
56980 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 nections. There
56990 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 fore, while pars
569a0 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e ing.** schema in
569b0 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c formation, the L
569c0 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 ookaside.bEnable
569d0 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 d flag is cleare
569e0 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f d so that.** loo
569f0 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f kaside allocatio
56a00 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 ns are not used
56a10 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 to construct the
56a20 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e schema objects.
56a30 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 .*/.struct Looka
56a40 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b side {. u16 sz;
56a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56a60 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
56a70 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 buffer in bytes
56a80 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 */. u8 bEnable
56a90 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a d; /*
56aa0 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c False to disabl
56ab0 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 e new lookaside
56ac0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
56ad0 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 u8 bMalloced;
56ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
56af0 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 if pStart obtai
56b00 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
56b10 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 _malloc() */. i
56b20 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 nt nOut;
56b30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
56b40 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72 of buffers curr
56b50 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 ently checked ou
56b60 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 t */. int mxOut
56b70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
56b80 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b * Highwater mark
56b90 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c for nOut */. L
56ba0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 ookasideSlot *pF
56bb0 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f ree; /* List o
56bc0 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 f available buff
56bd0 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ers */. void *p
56be0 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 Start;
56bf0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f /* First byte o
56c00 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f f available memo
56c10 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f ry space */. vo
56c20 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 id *pEnd;
56c30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
56c40 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 yte past end of
56c50 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 available space
56c60 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f */.};.struct Loo
56c70 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c kasideSlot {. L
56c80 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e ookasideSlot *pN
56c90 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 ext; /* Next
56ca0 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 buffer in the li
56cb0 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 st of free buffe
56cc0 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 rs */.};../*.**
56cd0 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 A hash table for
56ce0 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
56cf0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 tions..**.** Has
56d00 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 h each FuncDef s
56d10 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e tructure into on
56d20 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 e of the FuncDef
56d30 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a Hash.a[] slots..
56d40 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 ** Collisions ar
56d50 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 e on the FuncDef
56d60 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f .pHash chain..*/
56d70 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 .struct FuncDefH
56d80 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 ash {. FuncDef
56d90 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a *a[23]; /*
56da0 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 Hash table for
56db0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a functions */.};.
56dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 ./*.** Each data
56dd0 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 base is an insta
56de0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
56df0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
56e00 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
56e10 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 .lastRowid recor
56e20 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 ds the last inse
56e30 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 rt rowid generat
56e40 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 ed by an.** inse
56e50 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 rt statement. I
56e60 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 nserts on views
56e70 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 do not affect it
56e80 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a s value. Each.*
56e90 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74 * trigger has it
56ea0 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 s own context, s
56eb0 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 o that lastRowid
56ec0 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 can be updated
56ed0 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 inside.** trigge
56ee0 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 rs as usual. Th
56ef0 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 e previous value
56f00 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 will be restore
56f10 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 d once the trigg
56f20 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 er.** exits. Up
56f30 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 on entering a be
56f40 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 fore or instead
56f50 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 of trigger, last
56f60 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c Rowid is no.** l
56f70 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 onger (since aft
56f80 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 er version 2.8.1
56f90 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2) reset to -1..
56fa0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
56fb0 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f .nChange does no
56fc0 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 t count changes
56fd0 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 within triggers
56fe0 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 and keeps no.**
56ff0 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 context. It is
57000 72 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f reset at start o
57010 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a f sqlite3_exec..
57020 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 ** The sqlite.ls
57030 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 Change represent
57040 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
57050 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 changes made by
57060 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 the last.** inse
57070 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 rt, update, or d
57080 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e elete statement.
57090 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e It remains con
570a0 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 stant throughout
570b0 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f the.** length o
570c0 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e f a statement an
570d0 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 d is then update
570e0 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 d by OP_SetCount
570f0 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a s. It keeps a.*
57100 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 * context stack
57110 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f just like lastRo
57120 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 wid so that the
57130 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 count of changes
57140 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 .** within a tri
57150 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e gger is not seen
57160 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69 outside the tri
57170 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 gger. Changes t
57180 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a o views do not.*
57190 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c * affect the val
571a0 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a ue of lsChange..
571b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 ** The sqlite.cs
571c0 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 Change keeps tra
571d0 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 ck of the number
571e0 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e of current chan
571f0 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 ges (since.** th
57200 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 e last statement
57210 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f ) and is used to
57220 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c update sqlite_l
57230 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 sChange..**.** T
57240 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 he member variab
57250 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f les sqlite.errCo
57260 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d de, sqlite.zErrM
57270 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 sg and sqlite.zE
57280 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 rrMsg16.** store
57290 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
572a0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c error code and,
572b0 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 if applicable,
572c0 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 string. The.** i
572d0 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e nternal function
572e0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 sqlite3Error()
572f0 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 is used to set t
57300 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a hese variables.*
57310 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a * consistently..
57320 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
57330 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 3 {. sqlite3_vf
57340 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
57350 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 /* OS Interf
57360 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 ace */. int nDb
57370 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
57380 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
57390 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 of backends cur
573a0 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f rently in use */
573b0 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 . Db *aDb;
573c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
573d0 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 /* All backends
573e0 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b */. int flags;
573f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57400 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e /* Miscellan
57410 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 eous flags. See
57420 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f below */. int o
57430 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 penFlags;
57440 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
57450 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 s passed to sqli
57460 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 te3_vfs.xOpen()
57470 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 */. int errCode
57480 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
57490 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e /* Most recen
574a0 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 t error code (SQ
574b0 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 LITE_*) */. int
574c0 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 errMask;
574d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 /* &
574e0 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 result codes wit
574f0 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 h this before re
57500 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 turning */. u8
57510 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 autoCommit;
57520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
57530 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c e auto-commit fl
57540 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 ag. */. u8 temp
57550 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 _store;
57560 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c /* 1: fil
57570 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 e 2: memory 0: d
57580 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d efault */. u8 m
57590 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 allocFailed;
575a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
575b0 65 20 69 66 20 77 65 20 68 61 76 65 20 73 65 65 e if we have see
575c0 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 n a malloc failu
575d0 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c re */. u8 dfltL
575e0 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 ockMode;
575f0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 /* Default
57600 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f locking-mode fo
57610 72 20 61 74 74 61 63 68 65 64 20 64 62 73 20 2a r attached dbs *
57620 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e /. u8 dfltJourn
57630 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 alMode;
57640 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 /* Default jou
57650 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 rnal mode for at
57660 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 tached dbs */.
57670 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 signed char next
57680 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a Autovac; /*
57690 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 Autovac setting
576a0 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 after VACUUM if
576b0 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 >=0 */. int ne
576c0 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 xtPagesize;
576d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 /* Pages
576e0 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d ize after VACUUM
576f0 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 if >0 */. int
57700 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 nTable;
57710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
57720 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e ber of tables in
57730 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f the database */
57740 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c . CollSeq *pDfl
57750 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 tColl;
57760 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 /* The default
57770 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
57780 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 ce (BINARY) */.
57790 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 i64 lastRowid;
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
577b0 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 * ROWID of most
577c0 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 recent insert (s
577d0 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 ee above) */. i
577e0 36 34 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 64 priorNewRowid
577f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
57800 4c 61 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 Last randomly ge
57810 6e 65 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f nerated ROWID */
57820 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 . u32 magic;
57830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57840 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 /* Magic number
57850 20 66 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 for detect libr
57860 61 72 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 ary misuse */.
57870 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 int nChange;
57880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
57890 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 Value returned
578a0 62 79 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 by sqlite3_chang
578b0 65 73 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 es() */. int nT
578c0 6f 74 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 otalChange;
578d0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
578e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
578f0 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
57900 65 73 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 es() */. sqlite
57910 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 3_mutex *mutex;
57920 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 /* Conne
57930 63 74 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 ction mutex */.
57940 20 69 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 int aLimit[SQLI
57950 54 45 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f TE_N_LIMIT]; /
57960 2a 20 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 * Limits */. st
57970 72 75 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 ruct sqlite3Init
57980 49 6e 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 Info { /* I
57990 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 nformation used
579a0 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
579b0 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 ation */. int
579c0 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
579d0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e /* When
579e0 20 62 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 back is being i
579f0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
57a00 20 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 int newTnum;
57a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
57a20 20 52 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 Rootpage of tab
57a30 6c 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c le being initial
57a40 69 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 ized */. u8 b
57a50 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 usy;
57a60 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 /* TRUE
57a70 69 66 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 if currently ini
57a80 74 69 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d tializing */. }
57a90 20 69 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 init;. int nEx
57aa0 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 tension;
57ab0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
57ac0 20 6f 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e of loaded exten
57ad0 73 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 sions */. void
57ae0 2a 2a 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 **aExtension;
57af0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 /* Arra
57b00 79 20 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 y of shared libr
57b10 61 72 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 ary handles */.
57b20 20 73 74 72 75 63 74 20 56 64 62 65 20 2a 70 56 struct Vdbe *pV
57b30 64 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f dbe; /
57b40 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 * List of active
57b50 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
57b60 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76 s */. int activ
57b70 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 eVdbeCnt;
57b80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
57b90 66 20 56 44 42 45 73 20 63 75 72 72 65 6e 74 6c f VDBEs currentl
57ba0 79 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20 y executing */.
57bb0 20 69 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e int writeVdbeCn
57bc0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
57bd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69 * Number of acti
57be0 76 65 20 56 44 42 45 73 20 74 68 61 74 20 61 72 ve VDBEs that ar
57bf0 65 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76 e writing */. v
57c00 6f 69 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f oid (*xTrace)(vo
57c10 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 id*,const char*)
57c20 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 ; /* Trac
57c30 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
57c40 76 6f 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b void *pTraceArg;
57c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
57c70 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 ument to the tra
57c80 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ce function */.
57c90 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 void (*xProfile
57ca0 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 )(void*,const ch
57cb0 61 72 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 ar*,u64); /* Pr
57cc0 6f 66 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e ofiling function
57cd0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f */. void *pPro
57ce0 66 69 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 fileArg;
57cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57d00 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 /* Argument to p
57d10 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 rofile function
57d20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d */. void *pComm
57d30 69 74 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 itArg;
57d40 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
57d50 6e 74 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c nt to xCommitCal
57d60 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 lback() */ .
57d70 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c int (*xCommitCal
57d80 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 lback)(void*);
57d90 20 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 /* Invoked at
57da0 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f every commit. */
57db0 0a 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 . void *pRollba
57dc0 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 ckArg;
57dd0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
57de0 20 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c to xRollbackCal
57df0 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 lback() */ .
57e00 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b void (*xRollback
57e10 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 Callback)(void*)
57e20 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 ; /* Invoked at
57e30 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f every commit. */
57e40 0a 20 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 . void *pUpdate
57e50 41 72 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 Arg;. void (*xU
57e60 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 pdateCallback)(v
57e70 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 oid*,int, const
57e80 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
57e90 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b *,sqlite_int64);
57ea0 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 . void(*xCollNe
57eb0 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 eded)(void*,sqli
57ec0 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 te3*,int eTextRe
57ed0 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a p,const char*);.
57ee0 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 void(*xCollNee
57ef0 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c ded16)(void*,sql
57f00 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 ite3*,int eTextR
57f10 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b ep,const void*);
57f20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 . void *pCollNe
57f30 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 ededArg;. sqlit
57f40 65 33 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 e3_value *pErr;
57f50 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 /* Most
57f60 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 recent error me
57f70 73 73 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 ssage */. char
57f80 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 *zErrMsg;
57f90 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 /* Most
57fa0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 recent error me
57fb0 73 73 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 ssage (UTF-8 enc
57fc0 6f 64 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 oded) */. char
57fd0 2a 7a 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 *zErrMsg16;
57fe0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 /* Most
57ff0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 recent error me
58000 73 73 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e ssage (UTF-16 en
58010 63 6f 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f coded) */. unio
58020 6e 20 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 n {. volatile
58030 20 69 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74 int isInterrupt
58040 65 64 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 ed; /* True if s
58050 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
58060 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
58070 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e */. double n
58080 6f 74 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 otUsed1;
58090 20 20 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f /* Spacer */
580a0 0a 20 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 . } u1;. Looka
580b0 73 69 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 side lookaside;
580c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b /* Look
580d0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e aside malloc con
580e0 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 figuration */.#i
580f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
58100 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
58110 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 int (*xAuth)(v
58120 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 oid*,int,const c
58130 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
58140 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
58150 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 st char*);.
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58170 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 /* Ac
58180 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 cess authorizati
58190 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
581a0 20 76 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b void *pAuthArg;
581b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
581c0 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 * 1st argument t
581d0 6f 20 74 68 65 20 61 63 63 65 73 73 20 61 75 74 o the access aut
581e0 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 h function */.#e
581f0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
58200 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 ITE_OMIT_PROGRES
58210 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 S_CALLBACK. int
58220 20 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f (*xProgress)(vo
58230 69 64 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 id *); /* Th
58240 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
58250 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ack */. void *p
58260 50 72 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20 ProgressArg;
58270 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
58280 6e 74 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 nt to the progre
58290 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 ss callback */.
582a0 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 int nProgressOp
582b0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f s; /
582c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f * Number of opco
582d0 64 65 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73 des for progress
582e0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e callback */.#en
582f0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
58300 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
58310 41 42 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 ABLE. Hash aMod
58320 75 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ule;
58330 20 20 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 /* populate
58340 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 d by sqlite3_cre
58350 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a ate_module() */.
58360 20 20 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 Table *pVTab;
58370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58380 2f 2a 20 76 74 61 62 20 77 69 74 68 20 61 63 74 /* vtab with act
58390 69 76 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 ive Connect/Crea
583a0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 te method */. s
583b0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 qlite3_vtab **aV
583c0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 Trans; /*
583d0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 Virtual tables w
583e0 69 74 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 ith open transac
583f0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e tions */. int n
58400 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 VTrans;
58410 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f /* Allo
58420 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56 cated size of aV
58430 54 72 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a Trans */.#endif.
58440 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 FuncDefHash aF
58450 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 unc;
58460 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 /* Hash table of
58470 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 connection func
58480 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 tions */. Hash
58490 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 aCollSeq;
584a0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 /* All
584b0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
584c0 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e ces */. BusyHan
584d0 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 dler busyHandler
584e0 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 ; /* Busy c
584f0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 allback */. int
58500 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 busyTimeout;
58510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 /* Bu
58520 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f sy handler timeo
58530 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 ut, in msec */.
58540 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d Db aDbStatic[2]
58550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
58560 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 * Static space f
58570 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 or the 2 default
58580 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 backends */.#if
58590 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 def SQLITE_SSE.
585a0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
585b0 46 65 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f Fetch; /
585c0 2a 20 55 73 65 64 20 62 79 20 53 53 45 20 74 6f * Used by SSE to
585d0 20 66 65 74 63 68 20 73 74 6f 72 65 64 20 73 74 fetch stored st
585e0 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 atements */.#end
585f0 69 66 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a if. Savepoint *
58600 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 pSavepoint;
58610 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 /* List of ac
58620 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
58630 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f */. int nSavepo
58640 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
58650 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
58660 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 non-transaction
58670 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 savepoints */.
58680 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 int nStatement;
58690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
586a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
586b0 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e d statement-tran
586c0 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 sactions */. u
586d0 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 8 isTransactionS
586e0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 avepoint; /*
586f0 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 True if the oute
58700 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 rmost savepoint
58710 69 73 20 61 20 54 53 20 2a 2f 0a 0a 23 69 66 64 is a TS */..#ifd
58720 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
58730 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 _UNLOCK_NOTIFY.
58740 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
58750 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 g variables are
58760 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 all protected by
58770 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 the STATIC_MAST
58780 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 ER . ** mutex,
58790 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d not by sqlite3.m
587a0 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 utex. They are u
587b0 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e sed by code in n
587c0 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 otify.c. . **.
587d0 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f ** When X.pUnlo
587e0 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c ckConnection==Y,
587f0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
58800 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f X is waiting fo
58810 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f r Y to. ** unlo
58820 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 ck so that it ca
58830 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a n proceed.. **.
58840 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f ** When X.pBlo
58850 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d ckingConnection=
58860 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 =Y, that means t
58870 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 hat something th
58880 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 at X tried. **
58890 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 tried to do rece
588a0 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 ntly failed with
588b0 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 an SQLITE_LOCKE
588c0 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c D error due to l
588d0 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 ocks. ** held b
588e0 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 y Y.. */. sqli
588f0 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f te3 *pBlockingCo
58900 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e nnection; /* Con
58910 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 nection that cau
58920 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 sed SQLITE_LOCKE
58930 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a D */. sqlite3 *
58940 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f pUnlockConnectio
58950 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 n; /*
58960 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 Connection to wa
58970 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a tch for unlock *
58980 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 /. void *pUnloc
58990 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 kArg;
589a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
589b0 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b ument to xUnlock
589c0 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 Notify */. void
589d0 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 (*xUnlockNotify
589e0 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b )(void **, int);
589f0 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 /* Unlock noti
58a00 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 fy callback */.
58a10 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 sqlite3 *pNextB
58a20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f locked; /
58a30 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f * Next in list o
58a40 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f f all blocked co
58a50 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e nnections */.#en
58a60 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 dif.};../*.** A
58a70 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 macro to discove
58a80 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f r the encoding o
58a90 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f f a database..*/
58aa0 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 .#define ENC(db)
58ab0 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 ((db)->aDb[0].p
58ac0 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a Schema->enc)../*
58ad0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c .** Possible val
58ae0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ues for the sqli
58af0 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 te.flags and or
58b00 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e Db.flags fields.
58b10 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 .**.** On sqlite
58b20 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 .flags, the SQLI
58b30 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 TE_InTrans value
58b40 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 means that we h
58b50 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 ave.** executed
58b60 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e a BEGIN. On Db.
58b70 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e flags, SQLITE_In
58b80 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 Trans means a st
58b90 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 atement.** trans
58ba0 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
58bb0 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 on that particu
58bc0 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c lar database fil
58bd0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
58be0 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 LITE_VdbeTrace
58bf0 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20 0x00000001
58c00 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 /* True to trace
58c10 20 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 VDBE execution
58c20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
58c30 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 E_InTrans
58c40 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 0x00000008 /*
58c50 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 True if in a tra
58c60 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 nsaction */.#def
58c70 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 ine SQLITE_Inter
58c80 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 nChanges 0x0000
58c90 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 0010 /* Uncommi
58ca0 74 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 tted Hash table
58cb0 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 changes */.#defi
58cc0 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f ne SQLITE_FullCo
58cd0 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 lNames 0x00000
58ce0 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 020 /* Show ful
58cf0 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f l column names o
58d00 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 n SELECT */.#def
58d10 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 ine SQLITE_Short
58d20 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 ColNames 0x0000
58d30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 0040 /* Show sh
58d40 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 ort columns name
58d50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
58d60 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 ITE_CountRows
58d70 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 0x00000080 /
58d80 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 * Count rows cha
58d90 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 nged by INSERT,
58da0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
58dd0 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 DELETE, or UPD
58de0 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a ATE and return *
58df0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
58e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58e10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 /*
58e20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 the count using
58e30 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a a callback. */.
58e40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
58e50 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 ullCallback 0x
58e60 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 00000100 /* Inv
58e70 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b oke the callback
58e80 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a once if the */.
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 /* r
58ec0 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 esult set is emp
58ed0 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ty */.#define SQ
58ee0 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 LITE_SqlTrace
58ef0 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20 0x00000200
58f00 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 /* Debug print S
58f10 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65 QL as it execute
58f20 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
58f30 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 ITE_VdbeListing
58f40 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 0x00000400 /
58f50 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 * Debug listings
58f60 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d of VDBE program
58f70 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
58f80 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 ITE_WriteSchema
58f90 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 0x00000800 /
58fa0 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 * OK to update S
58fb0 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a QLITE_MASTER */.
58fc0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
58fd0 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 oReadlock 0x
58fe0 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 00001000 /* Rea
58ff0 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 dlocks are omitt
59000 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 ed when .
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59030 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 ** accessing
59040 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 read-only databa
59050 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ses */.#define S
59060 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 QLITE_IgnoreChec
59070 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20 ks 0x00002000
59080 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 /* Do not enfor
59090 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 ce check constra
590a0 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ints */.#define
590b0 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
590c0 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30 mitted 0x0000400
590d0 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 0 /* For shared-
590e0 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 cache mode */.#d
590f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 efine SQLITE_Leg
59100 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 acyFileFmt 0x00
59110 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 008000 /* Creat
59120 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 e new databases
59130 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 in format 1 */.#
59140 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 define SQLITE_Fu
59150 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 llFSync 0x0
59160 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 0010000 /* Use
59170 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 full fsync on th
59180 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 e backend */.#de
59190 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 fine SQLITE_Load
591a0 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 Extension 0x000
591b0 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20000 /* Enable
591c0 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 load_extension
591d0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */..#define SQLI
591e0 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 TE_RecoveryMode
591f0 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 0x00040000 /*
59200 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 Ignore schema e
59210 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 rrors */.#define
59220 20 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 SQLITE_SharedCa
59230 63 68 65 20 20 20 20 30 78 30 30 30 38 30 30 30 che 0x0008000
59240 30 20 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72 0 /* Cache shar
59250 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a ing is enabled *
59260 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
59270 5f 43 6f 6d 6d 69 74 42 75 73 79 20 20 20 20 20 _CommitBusy
59280 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 49 0x00200000 /* I
59290 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 n the process of
592a0 20 63 6f 6d 6d 69 74 74 69 6e 67 20 2a 2f 0a 23 committing */.#
592b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 define SQLITE_Re
592c0 76 65 72 73 65 4f 72 64 65 72 20 20 20 30 78 30 verseOrder 0x0
592d0 30 34 30 30 30 30 30 20 20 2f 2a 20 52 65 76 65 0400000 /* Reve
592e0 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20 53 45 rse unordered SE
592f0 4c 45 43 54 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 LECTs */../*.**
59300 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 Possible values
59310 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d for the sqlite.m
59320 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 agic field..** T
59330 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f he numbers are o
59340 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f btained at rando
59350 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 m and have no sp
59360 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f ecial meaning, o
59370 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 ther.** than bei
59380 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d ng distinct from
59390 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f one another..*/
593a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
593b0 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 MAGIC_OPEN 0
593c0 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 xa029a697 /* Da
593d0 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a tabase is open *
593e0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
593f0 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 _MAGIC_CLOSED
59400 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 0x9f3c2d33 /* D
59410 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 atabase is close
59420 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
59430 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 ITE_MAGIC_SICK
59440 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 0x4b771290 /
59450 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 * Error and awai
59460 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 ting close */.#d
59470 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
59480 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 IC_BUSY 0xf0
59490 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 3b7906 /* Datab
594a0 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ase currently in
594b0 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 use */.#define
594c0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 SQLITE_MAGIC_ERR
594d0 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 30 OR 0xb5357930
594e0 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d /* An SQLITE_M
594f0 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 ISUSE error occu
59500 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 rred */../*.** E
59510 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ach SQL function
59520 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 is defined by a
59530 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
59540 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 e following.** s
59550 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 tructure. A poi
59560 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
59570 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 ucture is stored
59580 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 in the sqlite.a
59590 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 Func.** hash tab
595a0 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 le. When multip
595b0 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 le functions hav
595c0 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c e the same name,
595d0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a the hash table.
595e0 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c ** points to a l
595f0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 inked list of th
59600 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a ese structures..
59610 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 */.struct FuncDe
59620 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 f {. i16 nArg;
59630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
59640 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
59650 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c s. -1 means unl
59660 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 imited */. u8 i
59670 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 PrefEnc;
59680 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 /* Preferred te
59690 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c xt encoding (SQL
596a0 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 ITE_UTF8, 16LE,
596b0 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 16BE) */. u8 fl
596c0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
596d0 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 /* Some combinat
596e0 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 ion of SQLITE_FU
596f0 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a NC_* */. void *
59700 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f pUserData; /
59710 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 61 * User data para
59720 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 meter */. FuncD
59730 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ef *pNext;
59740 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e /* Next function
59750 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 with same name
59760 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e */. void (*xFun
59770 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 c)(sqlite3_conte
59780 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
59790 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 value**); /* Reg
597a0 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ular function */
597b0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 . void (*xStep)
597c0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
597d0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
597e0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 lue**); /* Aggre
597f0 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 gate step */. v
59800 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 oid (*xFinalize)
59810 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
59820 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *);
59830 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 /* Aggregate
59840 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 finalizer */. c
59850 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
59860 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 /* SQL name
59870 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e of the function.
59880 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 */. FuncDef *p
59890 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Hash; /* Ne
598a0 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72 xt with a differ
598b0 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 ent name but the
598c0 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b same hash */.};
598d0 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 ../*.** Possible
598e0 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 values for Func
598f0 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 Def.flags.*/.#de
59900 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 fine SQLITE_FUNC
59910 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f _LIKE 0x01 /
59920 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 * Candidate for
59930 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a the LIKE optimiz
59940 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ation */.#define
59950 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
59960 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 E 0x02 /* Ca
59970 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b se-sensitive LIK
59980 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 E-type function
59990 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
599a0 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 E_FUNC_EPHEM
599b0 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61 0x04 /* Ephemera
599c0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20 l. Delete with
599d0 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 VDBE */.#define
599e0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
599f0 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c COLL 0x08 /* sql
59a00 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
59a10 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61 eq() might be ca
59a20 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lled */.#define
59a30 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56 SQLITE_FUNC_PRIV
59a40 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c ATE 0x10 /* All
59a50 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 owed for interna
59a60 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 l use only */.#d
59a70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
59a80 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20 C_COUNT 0x20
59a90 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e /* Built-in coun
59aa0 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a t(*) aggregate *
59ab0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c /../*.** The fol
59ac0 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63 lowing three mac
59ad0 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c ros, FUNCTION(),
59ae0 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20 LIKEFUNC() and
59af0 41 47 47 52 45 47 41 54 45 28 29 20 61 72 65 0a AGGREGATE() are.
59b00 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 ** used to creat
59b10 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 e the initialize
59b20 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44 rs for the FuncD
59b30 65 66 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a ef structures..*
59b40 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28 *.** FUNCTION(
59b50 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 zName, nArg, iAr
59b60 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a g, bNC, xFunc).*
59b70 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
59b80 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 eate a scalar fu
59b90 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
59ba0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 n of a function
59bb0 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d zName .** im
59bc0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66 plemented by C f
59bd0 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68 unction xFunc th
59be0 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 at accepts nArg
59bf0 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a arguments. The.*
59c00 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61 73 73 * value pass
59c10 65 64 20 61 73 20 69 41 72 67 20 69 73 20 63 61 ed as iArg is ca
59c20 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20 st to a (void*)
59c30 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62 and made availab
59c40 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65 le.** as the
59c50 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 user-data (sqli
59c60 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 te3_user_data())
59c70 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f for the functio
59c80 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72 n. If .** ar
59c90 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72 gument bNC is tr
59ca0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 46 75 6e ue, then the Fun
59cb0 63 44 65 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 cDef.needCollate
59cc0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a flag is set..**
59cd0 0a 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28 .** AGGREGATE(
59ce0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 zName, nArg, iAr
59cf0 67 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 g, bNC, xStep, x
59d00 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 Final).** Us
59d10 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 ed to create an
59d20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
59d30 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d on definition im
59d40 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 plemented by.**
59d50 20 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69 the C functi
59d60 6f 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46 ons xStep and xF
59d70 69 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20 inal. The first
59d80 66 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a four parameters.
59d90 2a 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72 ** are inter
59da0 70 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61 preted in the sa
59db0 6d 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69 me way as the fi
59dc0 72 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73 rst 4 parameters
59dd0 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 to.** FUNCT
59de0 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c ION()..**.** L
59df0 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e IKEFUNC(zName, n
59e00 41 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 Arg, pArg, flags
59e10 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f ).** Used to
59e20 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 create a scalar
59e30 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
59e40 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 tion of a functi
59e50 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 on zName .**
59e60 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 that accepts nA
59e70 72 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 rg arguments and
59e80 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
59e90 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a by a call to C .
59ea0 2a 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 ** function
59eb0 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 likeFunc. Argume
59ec0 6e 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20 nt pArg is cast
59ed0 74 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e to a (void *) an
59ee0 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 d made.** av
59ef0 61 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66 ailable as the f
59f00 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 unction user-dat
59f10 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f a (sqlite3_user_
59f20 64 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 data()). The.**
59f30 20 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 FuncDef.flag
59f40 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 s variable is se
59f50 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 t to the value p
59f60 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61 assed as the fla
59f70 67 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 gs.** parame
59f80 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ter..*/.#define
59f90 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 FUNCTION(zName,
59fa0 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c nArg, iArg, bNC,
59fb0 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 xFunc) \. {nAr
59fc0 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 g, SQLITE_UTF8,
59fd0 62 4e 43 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e bNC*8, SQLITE_IN
59fe0 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 T_TO_PTR(iArg),
59ff0 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 0, xFunc, 0, 0,
5a000 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
5a010 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 ne STR_FUNCTION(
5a020 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 zName, nArg, pAr
5a030 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c g, bNC, xFunc) \
5a040 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 . {nArg, SQLITE
5a050 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41 _UTF8, bNC*8, pA
5a060 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c rg, 0, xFunc, 0,
5a070 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 0, #zName, 0}.#
5a080 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 define LIKEFUNC(
5a090 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 zName, nArg, arg
5a0a0 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 , flags) \. {nA
5a0b0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c rg, SQLITE_UTF8,
5a0c0 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 flags, (void *)
5a0d0 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 arg, 0, likeFunc
5a0e0 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 , 0, 0, #zName,
5a0f0 30 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45 0}.#define AGGRE
5a100 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 GATE(zName, nArg
5a110 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 , arg, nc, xStep
5a120 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e , xFinal) \. {n
5a130 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Arg, SQLITE_UTF8
5a140 2c 20 6e 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 , nc*8, SQLITE_I
5a150 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 NT_TO_PTR(arg),
5a160 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 0, 0, xStep,xFin
5a170 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a al,#zName,0}../*
5a180 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 .** All current
5a190 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 savepoints are s
5a1a0 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 tored in a linke
5a1b0 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 d list starting
5a1c0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 at.** sqlite3.pS
5a1d0 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 avepoint. The fi
5a1e0 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 rst element in t
5a1f0 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d he list is the m
5a200 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 ost recently.**
5a210 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 opened savepoint
5a220 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 . Savepoints are
5a230 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 added to the li
5a240 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a st by the vdbe.*
5a250 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 * OP_Savepoint i
5a260 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 nstruction..*/.s
5a270 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 truct Savepoint
5a280 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
5a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2a0 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 /* Savep
5a2b0 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 oint name (nul-t
5a2c0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 erminated) */.
5a2d0 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 Savepoint *pNext
5a2e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5a2f0 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 /* Parent sa
5a300 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 vepoint (if any)
5a310 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
5a320 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
5a330 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f used as the seco
5a340 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
5a350 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 sqlite3Savepoint
5a360 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 (),.** and as th
5a370 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
5a380 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e the OP_Savepoin
5a390 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
5a3a0 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f /.#define SAVEPO
5a3b0 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30 INT_BEGIN 0
5a3c0 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 .#define SAVEPOI
5a3d0 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a NT_RELEASE 1.
5a3e0 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e #define SAVEPOIN
5a3f0 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a T_ROLLBACK 2..
5a400 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 ./*.** Each SQLi
5a410 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 te module (virtu
5a420 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 al table definit
5a430 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20 ion) is defined
5a440 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 by an.** instanc
5a450 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5a460 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20 73 74 ng structure, st
5a470 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ored in the sqli
5a480 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 te3.aModule.** h
5a490 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 ash table..*/.st
5a4a0 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 ruct Module {.
5a4b0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
5a4c0 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 dule *pModule;
5a4d0 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b /* Callback
5a4e0 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 pointers */. c
5a4f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
5a500 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5a510 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 /* Name pass
5a520 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 ed to create_mod
5a530 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 ule() */. void
5a540 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 *pAux;
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a560 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64 20 74 /* pAux passed t
5a570 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 o create_module(
5a580 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 ) */. void (*xD
5a590 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b estroy)(void *);
5a5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
5a5b0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 odule destructor
5a5c0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a function */.};.
5a5d0 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ./*.** informati
5a5e0 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f on about each co
5a5f0 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 lumn of an SQL t
5a600 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 able is held in
5a610 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f an instance.** o
5a620 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
5a630 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 ..*/.struct Colu
5a640 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 mn {. char *zNa
5a650 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me; /* Name
5a660 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a of this column *
5a670 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b /. Expr *pDflt;
5a680 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 /* Default
5a690 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f value of this co
5a6a0 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a lumn */. char *
5a6b0 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 zType; /* Da
5a6c0 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 ta type for this
5a6d0 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 column */. cha
5a6e0 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a r *zColl; /*
5a6f0 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
5a700 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 nce. If NULL, u
5a710 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a se the default *
5a720 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 /. u8 notNull;
5a730 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
5a740 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e there is a NOT N
5a750 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a ULL constraint *
5a760 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79 /. u8 isPrimKey
5a770 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ; /* True if
5a780 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 this column is p
5a790 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 art of the PRIMA
5a7a0 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 RY KEY */. char
5a7b0 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 affinity; /*
5a7c0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 One of the SQLIT
5a7d0 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 E_AFF_... values
5a7e0 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
5a7f0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
5a800 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64 ABLE. u8 isHidd
5a810 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 en; /* True
5a820 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 if this column i
5a830 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 s 'hidden' */.#e
5a840 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
5a850 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 "Collating Sequ
5a860 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65 64 ence" is defined
5a870 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
5a880 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
5a890 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43 .** structure. C
5a8a0 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 onceptually, a c
5a8b0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5a8c0 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 e consists of a
5a8d0 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f name and.** a co
5a8e0 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 mparison routine
5a8f0 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
5a900 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20 e order of that
5a910 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 sequence..**.**
5a920 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65 There may two se
5a930 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 parate implement
5a940 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f ations of the co
5a950 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e llation function
5a960 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 , one.** that pr
5a970 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e 20 ocesses text in
5a980 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 UTF-8 encoding (
5a990 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e CollSeq.xCmp) an
5a9a0 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a d another that.*
5a9b0 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 * processes text
5a9c0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
5a9d0 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 16 (CollSeq.xCmp
5a9e0 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 16), using the m
5a9f0 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 achine.** native
5aa00 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65 byte order. Whe
5aa10 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 n a collation se
5aa20 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 quence is invoke
5aa30 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74 d, SQLite select
5aa40 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e s.** the version
5aa50 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 that will requi
5aa60 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78 70 re the least exp
5aa70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a ensive encoding.
5aa80 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c ** translations,
5aa90 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 if any..**.** T
5aaa0 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 he CollSeq.pUser
5aab0 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 member variable
5aac0 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 is an extra par
5aad0 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73 73 ameter that pass
5aae0 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 ed in.** as the
5aaf0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
5ab00 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 o the UTF-8 comp
5ab10 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c arison function,
5ab20 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 xCmp..** CollSe
5ab30 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68 65 q.pUser16 is the
5ab40 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 equivalent for
5ab50 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 the UTF-16 compa
5ab60 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a rison function,.
5ab70 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a ** xCmp16..**.**
5ab80 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 If both CollSeq
5ab90 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 .xCmp and CollSe
5aba0 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c q.xCmp16 are NUL
5abb0 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 L, it means that
5abc0 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e the.** collatin
5abd0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e g sequence is un
5abe0 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 defined. Indice
5abf0 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e s built on an un
5ac00 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 defined.** colla
5ac10 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61 ting sequence ma
5ac20 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 y not be read or
5ac30 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 written..*/.str
5ac40 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 uct CollSeq {.
5ac50 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5ac60 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
5ac70 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
5ac80 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 equence, UTF-8 e
5ac90 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 ncoded */. u8 e
5aca0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
5acb0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 /* Text encodi
5acc0 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43 ng handled by xC
5acd0 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 mp() */. u8 typ
5ace0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
5acf0 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 /* One of the SQ
5ad00 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 LITE_COLL_... va
5ad10 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 lues below */.
5ad20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20 void *pUser;
5ad30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
5ad40 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 rgument to xCmp(
5ad50 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d ) */. int (*xCm
5ad60 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f p)(void*,int, co
5ad70 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 nst void*, int,
5ad80 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 const void*);.
5ad90 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
5ada0 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 d*); /* Destruc
5adb0 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f tor for pUser */
5adc0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 .};../*.** Allow
5add0 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c ed values of Col
5ade0 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 lSeq.type:.*/.#d
5adf0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
5ae00 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 L_BINARY 1 /*
5ae10 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 The default memc
5ae20 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 mp() collating s
5ae30 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 equence */.#defi
5ae40 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e ne SQLITE_COLL_N
5ae50 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65 OCASE 2 /* The
5ae60 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 built-in NOCASE
5ae70 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5ae80 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nce */.#define S
5ae90 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 QLITE_COLL_REVER
5aea0 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69 SE 3 /* The bui
5aeb0 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f lt-in REVERSE co
5aec0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5aed0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5aee0 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 TE_COLL_USER
5aef0 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 0 /* Any other
5af00 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c user-defined col
5af10 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5af20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 */../*.** A sort
5af30 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 order can be ei
5af40 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 ther ASC or DESC
5af50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5af60 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 ITE_SO_ASC
5af70 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 0 /* Sort in a
5af80 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a scending order *
5af90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5afa0 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20 _SO_DESC 1
5afb0 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 /* Sort in asce
5afc0 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a nding order */..
5afd0 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 /*.** Column aff
5afe0 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a inity types..**.
5aff0 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f ** These used to
5b000 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e have mnemonic n
5b010 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 ame like 'i' for
5b020 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
5b030 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 GER and.** 't' f
5b040 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 or SQLITE_AFF_TE
5b050 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 XT. But we can
5b060 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70 save a little sp
5b070 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a ace and improve.
5b080 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c ** the speed a l
5b090 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 69 ittle by numberi
5b0a0 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f ng the values co
5b0b0 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a nsecutively. .*
5b0c0 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72 20 *.** But rather
5b0d0 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68 20 than start with
5b0e0 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69 6e 0 or 1, we begin
5b0f0 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61 74 with 'a'. That
5b100 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 way,.** when mu
5b110 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79 20 ltiple affinity
5b120 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61 74 types are concat
5b130 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73 74 enated into a st
5b140 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64 ring and.** used
5b150 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72 61 as the P4 opera
5b160 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62 65 nd, they will be
5b170 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e 0a more readable..
5b180 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 **.** Note also
5b190 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69 63 that the numeric
5b1a0 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75 70 types are group
5b1b0 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20 74 ed together so t
5b1c0 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66 hat testing.** f
5b1d0 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 70 or a numeric typ
5b1e0 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f e is a single co
5b1f0 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 mparison..*/.#de
5b200 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5b210 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64 65 TEXT 'a'.#de
5b220 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5b230 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64 65 NONE 'b'.#de
5b240 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5b250 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64 65 NUMERIC 'c'.#de
5b260 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5b270 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64 65 INTEGER 'd'.#de
5b280 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f fine SQLITE_AFF_
5b290 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23 64 REAL 'e'..#d
5b2a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 4e efine sqlite3IsN
5b2b0 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 58 umericAffinity(X
5b2c0 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f ) ((X)>=SQLITE_
5b2d0 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a AFF_NUMERIC)../*
5b2e0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 .** The SQLITE_A
5b2f0 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d FF_MASK values m
5b300 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69 67 asks off the sig
5b310 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f 66 nificant bits of
5b320 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 an.** affinity
5b330 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 value. .*/.#defi
5b340 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 ne SQLITE_AFF_MA
5b350 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a SK 0x67../*.
5b360 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69 ** Additional bi
5b370 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 61 t values that ca
5b380 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 61 n be ORed with a
5b390 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 6f n affinity witho
5b3a0 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 ut.** changing t
5b3b0 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a he affinity..*/.
5b3c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a #define SQLITE_J
5b3d0 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30 38 UMPIFNULL 0x08
5b3e0 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65 69 /* jumps if ei
5b3f0 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
5b400 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
5b410 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 20 SQLITE_STOREP2
5b420 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74 6f 0x10 /* Sto
5b430 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 re result in reg
5b440 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61 6e [P2] rather than
5b450 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 jump */../*.**
5b460 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 Each SQL table i
5b470 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e s represented in
5b480 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e memory by an in
5b490 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a stance of the.**
5b4a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5b4b0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c ture..**.** Tabl
5b4c0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e e.zName is the n
5b4d0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
5b4e0 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 . The case of t
5b4f0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 he original.** C
5b500 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
5b510 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c ement is stored,
5b520 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 but case is not
5b530 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 significant for
5b540 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e .** comparisons.
5b550 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f .**.** Table.nCo
5b560 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 l is the number
5b570 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5b580 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 is table. Table
5b590 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f .aCol is a.** po
5b5a0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 inter to an arra
5b5b0 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 y of Column stru
5b5c0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 ctures, one for
5b5d0 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a each column..**.
5b5e0 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 ** If the table
5b5f0 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 has an INTEGER P
5b600 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
5b610 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
5b620 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 the index of.**
5b630 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 the column that
5b640 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f is that key. O
5b650 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 therwise Table.i
5b660 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 PKey is negative
5b670 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 . Note.** that
5b680 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 the datatype of
5b690 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 the PRIMARY KEY
5b6a0 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 must be INTEGER
5b6b0 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 for this field t
5b6c0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e o.** be set. An
5b6d0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
5b6e0 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20 KEY is used as
5b6f0 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 the rowid for ea
5b700 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 ch row of.** the
5b710 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 table. If a ta
5b720 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 ble has no INTEG
5b730 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY,
5b740 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f then a random ro
5b750 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 wid.** is genera
5b760 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 ted for each row
5b770 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 of the table.
5b780 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 TF_HasPrimaryKey
5b790 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 is set if.** th
5b7a0 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20 e table has any
5b7b0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 PRIMARY KEY, INT
5b7c0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73 EGER or otherwis
5b7d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 e..**.** Table.t
5b7e0 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20 num is the page
5b7f0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 number for the r
5b800 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f oot BTree page o
5b810 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 f the table in t
5b820 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
5b830 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 ile. If Table.i
5b840 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 Db is the index
5b850 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
5b860 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a table backend.**
5b870 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d in sqlite.aDb[]
5b880 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20 . 0 is for the
5b890 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e main database an
5b8a0 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66 d 1 is for the f
5b8b0 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 ile that.** hold
5b8c0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c s temporary tabl
5b8d0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 es and indices.
5b8e0 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c If TF_Ephemeral
5b8f0 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 is set.** then
5b900 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f the table is sto
5b910 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68 red in a file th
5b920 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 at is automatica
5b930 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 lly deleted.** w
5b940 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72 hen the VDBE cur
5b950 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 sor to the table
5b960 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 is closed. In
5b970 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e this case Table.
5b980 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 tnum .** refers
5b990 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 VDBE cursor numb
5b9a0 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 er that holds th
5b9b0 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f e table open, no
5b9c0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a t to the root.**
5b9d0 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 page number. T
5b9e0 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20 ransient tables
5b9f0 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 are used to hold
5ba00 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 the results of
5ba10 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74 a.** sub-query t
5ba20 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74 hat appears inst
5ba30 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61 ead of a real ta
5ba40 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
5ba50 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 FROM clause .**
5ba60 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 of a SELECT stat
5ba70 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 ement..*/.struct
5ba80 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 Table {. sqlit
5ba90 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20 e3 *dbMem;
5baa0 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e /* DB connection
5bab0 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 used for lookas
5bac0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ide allocations.
5bad0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
5bae0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 e; /* Na
5baf0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
5bb00 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 or view */. int
5bb10 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 iPKey;
5bb20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 /* If not nega
5bb30 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 tive, use aCol[i
5bb40 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 PKey] as the pri
5bb50 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e mary key */. in
5bb60 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t nCol;
5bb70 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5bb80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 columns in this
5bb90 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d table */. Colum
5bba0 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 n *aCol;
5bbb0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
5bbc0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e bout each column
5bbd0 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e */. Index *pIn
5bbe0 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 dex; /* Li
5bbf0 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 st of SQL indexe
5bc00 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e s on this table.
5bc10 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
5bc20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
5bc30 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f ot BTree node fo
5bc40 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 r this table (se
5bc50 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f e note above) */
5bc60 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
5bc70 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 ct; /* NULL
5bc80 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 for tables. Poi
5bc90 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f nts to definitio
5bca0 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a n if a view. */.
5bcb0 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 u16 nRef;
5bcc0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5bcd0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
5bce0 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 this Table */.
5bcf0 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 u8 tabFlags;
5bd00 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
5bd10 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 TF_* values */.
5bd20 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 u8 keyConf;
5bd30 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f /* What to
5bd40 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 do in case of u
5bd50 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 niqueness confli
5bd60 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 ct on iPKey */.
5bd70 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 FKey *pFKey;
5bd80 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 /* Linked
5bd90 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 list of all fore
5bda0 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 ign keys in this
5bdb0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 table */. char
5bdc0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 *zColAff;
5bdd0 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e /* String defin
5bde0 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 ing the affinity
5bdf0 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 of each column
5be00 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
5be10 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 E_OMIT_CHECK. E
5be20 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 xpr *pCheck;
5be30 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f /* The AND o
5be40 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 f all CHECK cons
5be50 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 traints */.#endi
5be60 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
5be70 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 _OMIT_ALTERTABLE
5be80 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 . int addColOff
5be90 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 set; /* Offse
5bea0 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c t in CREATE TABL
5beb0 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 E stmt to add a
5bec0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 new column */.#e
5bed0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
5bee0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
5bef0 54 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a TABLE. Module *
5bf00 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 pMod; /*
5bf10 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 Pointer to the i
5bf20 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
5bf30 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 the module */.
5bf40 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
5bf50 56 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 Vtab; /* Pointer
5bf60 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 to the module i
5bf70 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 nstance */. int
5bf80 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 nModuleArg;
5bf90 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
5bfa0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
5bfb0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 module */. char
5bfc0 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 **azModuleArg;
5bfd0 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 /* Text of all
5bfe0 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d module args. [0]
5bff0 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 is module name
5c000 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67 */.#endif. Trig
5c010 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 ger *pTrigger;
5c020 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 /* List of trig
5c030 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70 gers stored in p
5c040 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65 Schema */. Sche
5c050 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 ma *pSchema;
5c060 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 /* Schema that
5c070 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 contains this ta
5c080 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ble */. Table *
5c090 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a pNextZombie; /*
5c0a0 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 Next on the Par
5c0b0 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 se.pZombieTab li
5c0c0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
5c0d0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 Allowed values f
5c0e0 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 or Tabe.tabFlags
5c0f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f ..*/.#define TF_
5c100 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 Readonly
5c110 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 0x01 /* Read-
5c120 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c only system tabl
5c130 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f e */.#define TF_
5c140 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 Ephemeral
5c150 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 0x02 /* An ep
5c160 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f hemeral table */
5c170 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 .#define TF_HasP
5c180 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 rimaryKey 0x04
5c190 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
5c1a0 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a a primary key *
5c1b0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 /.#define TF_Aut
5c1c0 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 oincrement 0x0
5c1d0 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 8 /* Integer
5c1e0 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 primary key is a
5c1f0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a utoincrement */.
5c200 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 #define TF_Virtu
5c210 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20 al 0x10
5c220 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 /* Is a virtu
5c230 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 al table */.#def
5c240 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 ine TF_NeedMetad
5c250 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f ata 0x20 /
5c260 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 * aCol[].zType a
5c270 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 nd aCol[].pColl
5c280 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a missing */..../*
5c290 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 .** Test to see
5c2a0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
5c2b0 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 table is a virt
5c2c0 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 ual table. This
5c2d0 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 is.** done as a
5c2e0 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 macro so that i
5c2f0 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 t will be optimi
5c300 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 zed out when vir
5c310 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 tual.** table su
5c320 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 pport is omitted
5c330 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e from the build.
5c340 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
5c350 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
5c360 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 ABLE.# define I
5c370 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
5c380 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 (((X)->tabFlags
5c390 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d & TF_Virtual)!=
5c3a0 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0).# define IsH
5c3b0 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 iddenColumn(X) (
5c3c0 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 (X)->isHidden).#
5c3d0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 else.# define I
5c3e0 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 sVirtual(X)
5c3f0 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 0.# define IsH
5c400 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 iddenColumn(X) 0
5c410 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
5c420 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 ach foreign key
5c430 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e constraint is an
5c440 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
5c450 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5c460 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f ture..**.** A fo
5c470 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 reign key is ass
5c480 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f ociated with two
5c490 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 tables. The "f
5c4a0 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a rom" table is.**
5c4b0 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
5c4c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 contains the REF
5c4d0 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 ERENCES clause t
5c4e0 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20 hat creates the
5c4f0 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 foreign.** key.
5c500 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 The "to" table
5c510 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 is the table tha
5c520 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 t is named in th
5c530 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
5c540 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 use..** Consider
5c550 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a this example:.*
5c560 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 *.** CREATE
5c570 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 TABLE ex1(.**
5c580 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 a INTEGER PR
5c590 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 IMARY KEY,.**
5c5a0 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f b INTEGER CO
5c5b0 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 NSTRAINT fk1 REF
5c5c0 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a ERENCES ex2(x).*
5c5d0 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 * );.**.** F
5c5e0 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 or foreign key "
5c5f0 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 fk1", the from-t
5c600 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e able is "ex1" an
5c610 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 d the to-table i
5c620 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 s "ex2"..**.** E
5c630 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 ach REFERENCES c
5c640 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 lause generates
5c650 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
5c660 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
5c670 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 ucture.** which
5c680 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 is attached to t
5c690 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 he from-table.
5c6a0 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 The to-table nee
5c6b0 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e d not exist when
5c6c0 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 .** the from-tab
5c6d0 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 le is created.
5c6e0 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 The existence of
5c6f0 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 the to-table is
5c700 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f not checked..*/
5c710 0a 73 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 .struct FKey {.
5c720 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 Table *pFrom;
5c730 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
5c740 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
5c750 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
5c760 75 73 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 use */. FKey *p
5c770 4e 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 NextFrom; /* Ne
5c780 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 xt foreign key i
5c790 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 n pFrom */. cha
5c7a0 72 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f r *zTo; /
5c7b0 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 * Name of table
5c7c0 74 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 that the key poi
5c7d0 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 nts to */. int
5c7e0 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a nCol; /*
5c7f0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
5c800 6e 73 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a ns in this key *
5c810 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65 /. u8 isDeferre
5c820 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 d; /* True if
5c830 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 constraint chec
5c840 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 king is deferred
5c850 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a till COMMIT */.
5c860 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b u8 updateConf;
5c870 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
5c880 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
5c890 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 that occur on UP
5c8a0 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c DATE */. u8 del
5c8b0 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 eteConf; /* H
5c8c0 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f ow to resolve co
5c8d0 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 nflicts that occ
5c8e0 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a ur on DELETE */.
5c8f0 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b u8 insertConf;
5c900 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 /* How to re
5c910 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 solve conflicts
5c920 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e that occur on IN
5c930 53 45 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74 SERT */. struct
5c940 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d sColMap { /* M
5c950 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e apping of column
5c960 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f s in pFrom to co
5c970 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a lumns in zTo */.
5c980 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 int iFrom;
5c990 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
5c9a0 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 of column in pFr
5c9b0 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a om */. char *
5c9c0 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 zCol; /*
5c9d0 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 Name of column i
5c9e0 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 n zTo. If 0 use
5c9f0 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a PRIMARY KEY */.
5ca00 20 20 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 } aCol[1];
5ca10 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 /* One entry
5ca20 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f for each of nCo
5ca30 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b l column s */.};
5ca40 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73 ../*.** SQLite s
5ca50 75 70 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66 upports many dif
5ca60 66 65 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72 ferent ways to r
5ca70 65 73 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61 esolve a constra
5ca80 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52 int.** error. R
5ca90 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69 OLLBACK processi
5caa0 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 ng means that a
5cab0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
5cac0 74 69 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74 tion.** causes t
5cad0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 he operation in
5cae0 70 72 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20 process to fail
5caf0 61 6e 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 and for the curr
5cb00 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
5cb10 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ** to be rolled
5cb20 62 61 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f back. ABORT pro
5cb30 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 cessing means th
5cb40 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 e operation in p
5cb50 72 6f 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20 rocess.** fails
5cb60 61 6e 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68 and any prior ch
5cb70 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20 anges from that
5cb80 6f 6e 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 one operation ar
5cb90 65 20 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a e backed out,.**
5cba0 20 62 75 74 20 74 68 65 20 74 72 61 6e 73 61 63 but the transac
5cbb0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c tion is not roll
5cbc0 65 64 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70 ed back. FAIL p
5cbd0 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 rocessing means
5cbe0 74 68 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72 that.** the oper
5cbf0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 ation in progres
5cc00 73 20 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75 s stops and retu
5cc10 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 rns an error cod
5cc20 65 2e 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a e. But prior.**
5cc30 20 63 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20 changes due to
5cc40 74 68 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69 the same operati
5cc50 6f 6e 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65 on are not backe
5cc60 64 20 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c d out and no rol
5cc70 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e lback.** occurs.
5cc80 20 20 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 IGNORE means t
5cc90 68 61 74 20 74 68 65 20 70 61 72 74 69 63 75 6c hat the particul
5cca0 61 72 20 72 6f 77 20 74 68 61 74 20 63 61 75 73 ar row that caus
5ccb0 65 64 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e ed the constrain
5ccc0 74 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f t.** error is no
5ccd0 74 20 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70 t inserted or up
5cce0 64 61 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69 dated. Processi
5ccf0 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 ng continues and
5cd00 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20 no error.** is
5cd10 72 65 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41 returned. REPLA
5cd20 43 45 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72 CE means that pr
5cd30 65 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 eexisting databa
5cd40 73 65 20 72 6f 77 73 20 74 68 61 74 20 63 61 75 se rows that cau
5cd50 73 65 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 sed.** a UNIQUE
5cd60 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
5cd70 74 69 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 tion are removed
5cd80 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 so that the new
5cd90 20 69 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70 insert or.** up
5cda0 64 61 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64 date can proceed
5cdb0 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f . Processing co
5cdc0 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 ntinues and no e
5cdd0 72 72 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64 rror is reported
5cde0 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54 ..**.** RESTRICT
5cdf0 2c 20 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 , SETNULL, and C
5ce00 41 53 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61 ASCADE actions a
5ce10 70 70 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 pply only to for
5ce20 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 eign keys..** RE
5ce30 53 54 52 49 43 54 20 69 73 20 74 68 65 20 73 61 STRICT is the sa
5ce40 6d 65 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20 me as ABORT for
5ce50 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67 IMMEDIATE foreig
5ce60 6e 20 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a n keys and the.*
5ce70 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41 * same as ROLLBA
5ce80 43 4b 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 CK for DEFERRED
5ce90 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d keys. SETNULL m
5cea0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f eans that the fo
5ceb0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 reign.** key is
5cec0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 set to NULL. CA
5ced0 53 43 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74 SCADE means that
5cee0 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 a DELETE or UPD
5cef0 41 54 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ATE of the.** re
5cf00 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72 ferenced table r
5cf10 6f 77 20 69 73 20 70 72 6f 70 61 67 61 74 65 64 ow is propagated
5cf20 20 69 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68 into the row th
5cf30 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 at holds the.**
5cf40 66 6f 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 foreign key..**
5cf50 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
5cf60 67 20 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65 g symbolic value
5cf70 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 72 65 s are used to re
5cf80 63 6f 72 64 20 77 68 69 63 68 20 74 79 70 65 0a cord which type.
5cf90 2a 2a 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20 ** of action to
5cfa0 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 take..*/.#define
5cfb0 20 4f 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20 OE_None 0
5cfc0 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 /* There is no
5cfd0 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68 constraint to ch
5cfe0 65 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f eck */.#define O
5cff0 45 5f 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f E_Rollback 1 /
5d000 2a 20 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61 * Fail the opera
5d010 74 69 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63 tion and rollbac
5d020 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
5d030 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f n */.#define OE_
5d040 41 62 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20 Abort 2 /*
5d050 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 Back out changes
5d060 20 62 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 but do no rollb
5d070 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ack transaction
5d080 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61 */.#define OE_Fa
5d090 69 6c 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74 il 3 /* St
5d0a0 6f 70 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e op the operation
5d0b0 20 62 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70 but leave all p
5d0c0 72 69 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a rior changes */.
5d0d0 23 64 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 #define OE_Ignor
5d0e0 65 20 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72 e 4 /* Ignor
5d0f0 65 20 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20 e the error. Do
5d100 6e 6f 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52 not do the INSER
5d110 54 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23 T or UPDATE */.#
5d120 64 65 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63 define OE_Replac
5d130 65 20 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65 e 5 /* Delete
5d140 20 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 existing record
5d150 2c 20 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54 , then do INSERT
5d160 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23 or UPDATE */..#
5d170 64 65 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69 define OE_Restri
5d180 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f ct 6 /* OE_Abo
5d190 72 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 rt for IMMEDIATE
5d1a0 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f , OE_Rollback fo
5d1b0 72 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64 r DEFERRED */.#d
5d1c0 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c efine OE_SetNull
5d1d0 20 20 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65 7 /* Set the
5d1e0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c foreign key val
5d1f0 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue to NULL */.#d
5d200 65 66 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74 efine OE_SetDflt
5d210 20 20 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65 8 /* Set the
5d220 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c foreign key val
5d230 75 65 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c ue to its defaul
5d240 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f t */.#define OE_
5d250 43 61 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20 Cascade 9 /*
5d260 43 61 73 63 61 64 65 20 74 68 65 20 63 68 61 6e Cascade the chan
5d270 67 65 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 ges */..#define
5d280 4f 45 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20 OE_Default 99
5d290 2f 2a 20 44 6f 20 77 68 61 74 65 76 65 72 20 74 /* Do whatever t
5d2a0 68 65 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f he default actio
5d2b0 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 n is */.../*.**
5d2c0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
5d2d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
5d2e0 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 ucture is passed
5d2f0 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a as the first.**
5d300 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
5d310 69 74 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61 ite3VdbeKeyCompa
5d320 72 65 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 re and is used t
5d330 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a o control the .*
5d340 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 * comparison of
5d350 74 68 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65 the two index ke
5d360 79 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65 ys..*/.struct Ke
5d370 79 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 yInfo {. sqlite
5d380 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 3 *db; /*
5d390 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
5d3a0 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 nnection */. u8
5d3b0 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 enc;
5d3c0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 /* Text encodi
5d3d0 6e 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20 ng - one of the
5d3e0 54 45 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73 TEXT_Utf* values
5d3f0 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 */. u16 nField
5d400 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
5d410 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
5d420 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 n aColl[] */. u
5d430 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 8 *aSortOrder;
5d440 20 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 /* If defined
5d450 20 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 an aSortOrder[i
5d460 5d 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 ] is true, sort
5d470 44 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 DESC */. CollSe
5d480 71 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a q *aColl[1]; /*
5d490 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
5d4a0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 nce for each ter
5d4b0 6d 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a m of the key */.
5d4c0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
5d4d0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
5d4e0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
5d4f0 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 holds informati
5d500 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69 on about a.** si
5d510 6e 67 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72 ngle index recor
5d520 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 d that has alrea
5d530 64 79 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f dy been parsed o
5d540 75 74 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 ut into individu
5d550 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a al.** values..**
5d560 0a 2a 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20 .** A record is
5d570 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 an object that c
5d580 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
5d590 6f 72 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61 ore fields of da
5d5a0 74 61 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61 ta..** Records a
5d5b0 72 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 re used to store
5d5c0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
5d5d0 61 20 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20 a table row and
5d5e0 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20 to store.** the
5d5f0 6b 65 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e key of an index.
5d600 20 20 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e A blob encodin
5d610 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73 g of a record is
5d620 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 created by.** t
5d630 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
5d640 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 opcode of the V
5d650 44 42 45 20 61 6e 64 20 69 73 20 64 69 73 61 73 DBE and is disas
5d660 73 65 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a sembled by the.*
5d670 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f * OP_Column opco
5d680 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 de..**.** This s
5d690 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 tructure holds a
5d6a0 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 record that has
5d6b0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69 already been di
5d6c0 73 61 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e sassembled.** in
5d6d0 74 6f 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65 to its constitue
5d6e0 6e 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 nt fields..*/.st
5d6f0 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 ruct UnpackedRec
5d700 6f 72 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 ord {. KeyInfo
5d710 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 *pKeyInfo; /* C
5d720 6f 6c 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72 ollation and sor
5d730 74 2d 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74 t-order informat
5d740 69 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 ion */. u16 nFi
5d750 65 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eld; /*
5d760 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
5d770 73 20 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a s in apMem[] */.
5d780 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 u16 flags;
5d790 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e /* Boolean
5d7a0 20 73 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41 settings. UNPA
5d7b0 43 4b 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a CKED_... below *
5d7c0 2f 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20 /. i64 rowid;
5d7d0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
5d7e0 62 79 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 by UNPACKED_PREF
5d7f0 49 58 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d IX_SEARCH */. M
5d800 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 em *aMem;
5d810 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a /* Values */.
5d820 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 };../*.** Allowe
5d830 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 d values of Unpa
5d840 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 ckedRecord.flags
5d850 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 .*/.#define UNPA
5d860 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 CKED_NEED_FREE
5d870 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 0x0001 /* Me
5d880 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c mory is from sql
5d890 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a ite3Malloc() */.
5d8a0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
5d8b0 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 _NEED_DESTROY 0
5d8c0 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b x0002 /* apMem[
5d8d0 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 ]s should all be
5d8e0 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 destroyed */.#d
5d8f0 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 efine UNPACKED_I
5d900 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 GNORE_ROWID 0x0
5d910 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 004 /* Ignore t
5d920 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e railing rowid on
5d930 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 key1 */.#define
5d940 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
5d950 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 Y 0x0008
5d960 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 /* Make this key
5d970 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 an epsilon larg
5d980 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e er */.#define UN
5d990 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 PACKED_PREFIX_MA
5d9a0 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 TCH 0x0010 /*
5d9b0 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 A prefix match i
5d9c0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 s considered OK
5d9d0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
5d9e0 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 KED_PREFIX_SEARC
5d9f0 48 20 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70 H 0x0020 /* A p
5da00 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 refix match is c
5da10 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a onsidered OK */.
5da20 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 ./*.** Each SQL
5da30 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 73 65 index is represe
5da40 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 nted in memory b
5da50 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 y an.** instance
5da60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
5da70 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
5da80 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f ** The columns o
5da90 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
5daa0 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78 are to be index
5dab0 65 64 20 61 72 65 20 64 65 73 63 72 69 62 65 64 ed are described
5dac0 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c .** by the aiCol
5dad0 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74 umn[] field of t
5dae0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
5daf0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 For example, sup
5db00 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20 pose.** we have
5db10 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 the following ta
5db20 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a ble and index:.*
5db30 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 *.** CREATE
5db40 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74 TABLE Ex1(c1 int
5db50 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78 , c2 int, c3 tex
5db60 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 t);.** CREAT
5db70 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45 E INDEX Ex2 ON E
5db80 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a x1(c3,c1);.**.**
5db90 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 In the Table st
5dba0 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 ructure describi
5dbb0 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 ng Ex1, nCol==3
5dbc0 62 65 63 61 75 73 65 20 74 68 65 72 65 20 61 72 because there ar
5dbd0 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d e.** three colum
5dbe0 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ns in the table.
5dbf0 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73 In the Index s
5dc00 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 tructure describ
5dc10 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c ing.** Ex2, nCol
5dc20 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f umn==2 since 2 o
5dc30 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20 f the 3 columns
5dc40 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 65 78 of Ex1 are index
5dc50 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 ed..** The value
5dc60 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20 of aiColumn is
5dc70 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d {2, 0}. aiColum
5dc80 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20 n[0]==2 because
5dc90 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f the .** first co
5dca0 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 lumn to be index
5dcb0 65 64 20 28 63 33 29 20 68 61 73 20 61 6e 20 69 ed (c3) has an i
5dcc0 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31 ndex of 2 in Ex1
5dcd0 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 .aCol[]..** The
5dce0 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f second column to
5dcf0 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 31 29 be indexed (c1)
5dd00 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
5dd10 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 0 in.** Ex1.aCo
5dd20 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61 l[], hence Ex2.a
5dd30 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a iColumn[1]==0..*
5dd40 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f *.** The Index.o
5dd50 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74 nError field det
5dd60 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 ermines whether
5dd70 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 or not the index
5dd80 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 ed columns.** mu
5dd90 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64 st be unique and
5dda0 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74 what to do if t
5ddb0 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68 hey are not. Wh
5ddc0 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 en Index.onError
5ddd0 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 =OE_None,.** it
5dde0 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f means this is no
5ddf0 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 t a unique index
5de00 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 . Otherwise it
5de10 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 is a unique inde
5de20 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c x.** and the val
5de30 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72 ue of Index.onEr
5de40 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 68 65 ror indicate the
5de50 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20 which conflict
5de60 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 resolution .** a
5de70 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c lgorithm to empl
5de80 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61 oy whenever an a
5de90 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
5dea0 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75 o insert a non-u
5deb0 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 nique.** element
5dec0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 ..*/.struct Inde
5ded0 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d x {. char *zNam
5dee0 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f e; /* Name o
5def0 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a f this index */.
5df00 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 int nColumn;
5df10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5df20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 columns in the t
5df30 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 69 able used by thi
5df40 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 s index */. int
5df50 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a *aiColumn; /*
5df60 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61 Which columns a
5df70 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 re used by this
5df80 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 20 30 index. 1st is 0
5df90 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a */. unsigned *
5dfa0 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73 aiRowEst; /* Res
5dfb0 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 ult of ANALYZE:
5dfc0 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74 Est. rows select
5dfd0 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d ed by each colum
5dfe0 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 n */. Table *pT
5dff0 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53 able; /* The S
5e000 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 QL table being i
5e010 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ndexed */. int
5e020 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 tnum; /*
5e030 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Page containing
5e040 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64 root of this ind
5e050 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 ex in database f
5e060 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 ile */. u8 onEr
5e070 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f ror; /* OE_
5e080 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 Abort, OE_Ignore
5e090 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 , OE_Replace, or
5e0a0 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 OE_None */. u8
5e0b0 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f autoIndex; /
5e0c0 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 75 74 * True if is aut
5e0d0 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 omatically creat
5e0e0 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55 ed (ex: by UNIQU
5e0f0 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 E) */. char *zC
5e100 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 olAff; /* Stri
5e110 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 ng defining the
5e120 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 affinity of each
5e130 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 column */. Ind
5e140 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a ex *pNext; /*
5e150 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 The next index
5e160 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
5e170 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a the same table *
5e180 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
5e190 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 ema; /* Schema c
5e1a0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 ontaining this i
5e1b0 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 ndex */. u8 *aS
5e1c0 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 ortOrder; /* Ar
5e1d0 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 ray of size Inde
5e1e0 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d x.nColumn. True=
5e1f0 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 =DESC, False==AS
5e200 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a C */. char **az
5e210 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 Coll; /* Array
5e220 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 of collation se
5e230 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 quence names for
5e240 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a index */.};../*
5e250 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e 20 63 .** Each token c
5e260 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 65 oming out of the
5e270 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69 6e 73 lexer is an ins
5e280 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 tance of.** this
5e290 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 6f 6b structure. Tok
5e2a0 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 73 65 ens are also use
5e2b0 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 d as part of an
5e2c0 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a expression..**.*
5e2d0 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e * Note if Token.
5e2e0 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e z==0 then Token.
5e2f0 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 dyn and Token.n
5e300 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 61 6e are undefined an
5e310 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e d.** may contain
5e320 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 random values.
5e330 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 Do not make any
5e340 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f assumptions abo
5e350 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 ut Token.dyn.**
5e360 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e and Token.n when
5e370 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a Token.z==0..*/.
5e380 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 struct Token {.
5e390 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
5e3a0 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78 74 char *z; /* Text
5e3b0 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 of the token.
5e3c0 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 Not NULL-termina
5e3d0 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ted! */. unsign
5e3e0 65 64 20 64 79 6e 20 20 20 20 3a 20 31 3b 20 20 ed dyn : 1;
5e3f0 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d 61 /* True for ma
5e400 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20 66 lloced memory, f
5e410 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63 20 alse for static
5e420 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 71 75 */. unsigned qu
5e430 6f 74 65 64 20 3a 20 31 3b 20 20 20 20 2f 2a 20 oted : 1; /*
5e440 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 73 74 True if token st
5e450 69 6c 6c 20 68 61 73 20 69 74 73 20 71 75 6f 74 ill has its quot
5e460 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 es */. unsigned
5e470 20 6e 20 20 20 20 20 20 3a 20 33 30 3b 20 20 20 n : 30;
5e480 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
5e490 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 racters in this
5e4a0 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a token */.};../*.
5e4b0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
5e4c0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
5e4d0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d contains inform
5e4e0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
5e4f0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
5e500 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 for a SELECT th
5e510 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 at contains aggr
5e520 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
5e530 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f .**.** If Expr.o
5e540 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
5e550 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 or TK_AGG_FUNCT
5e560 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 ION then Expr.pA
5e570 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 ggInfo is a.** p
5e580 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
5e590 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 tructure. The E
5e5a0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c xpr.iColumn fiel
5e5b0 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 d is the index i
5e5c0 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f n.** AggInfo.aCo
5e5d0 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 l[] or AggInfo.a
5e5e0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d Func[] of inform
5e5f0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
5e600 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
5e610 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a for that node..
5e620 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 **.** AggInfo.pG
5e630 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e roupBy and AggIn
5e640 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 fo.aFunc.pExpr p
5e650 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 oint to fields w
5e660 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 ithin the.** ori
5e670 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 ginal Select str
5e680 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 ucture that desc
5e690 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 ribes the SELECT
5e6a0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 statement. The
5e6b0 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 se.** fields do
5e6c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 not need to be f
5e6d0 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f reed when deallo
5e6e0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e cating the AggIn
5e6f0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f fo structure..*/
5e700 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 .struct AggInfo
5e710 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 {. u8 directMod
5e720 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 e; /* D
5e730 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 irect rendering
5e740 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 mode means take
5e750 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 data directly.
5e760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e770 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 ** from
5e780 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 source tables ra
5e790 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 ther than from a
5e7a0 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 ccumulators */.
5e7b0 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 u8 useSortingId
5e7c0 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 x; /* In d
5e7d0 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 irect mode, refe
5e7e0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e rence the sortin
5e7f0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 g index rather.
5e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e810 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e ** than
5e820 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
5e830 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 e */. int sorti
5e840 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f ngIdx; /
5e850 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 * Cursor number
5e860 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 of the sorting i
5e870 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 ndex */. ExprLi
5e880 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 st *pGroupBy;
5e890 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 /* The group b
5e8a0 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e y clause */. in
5e8b0 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e t nSortingColumn
5e8c0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
5e8d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
5e8e0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 e sorting index
5e8f0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 */. struct AggI
5e900 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 nfo_col { /*
5e910 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 For each column
5e920 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 used in source t
5e930 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 ables */. Tab
5e940 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
5e950 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
5e960 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 table */. int
5e970 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
5e980 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
5e990 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f number of the so
5e9a0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 urce table */.
5e9b0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
5e9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
5e9d0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 lumn number with
5e9e0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 in the source ta
5e9f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
5ea00 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 SorterColumn;
5ea10 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 /* Column nu
5ea20 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 mber in the sort
5ea30 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 ing index */.
5ea40 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
5ea50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
5ea60 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 ory location tha
5ea70 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 t acts as accumu
5ea80 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 lator */. Exp
5ea90 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 r *pExpr;
5eaa0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 /* The ori
5eab0 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e ginal expression
5eac0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 */. } *aCol;.
5ead0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 int nColumn;
5eae0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5eaf0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 er of used entri
5eb00 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a es in aCol[] */.
5eb10 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c int nColumnAll
5eb20 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d oc; /* Num
5eb30 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c ber of slots all
5eb40 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b ocated for aCol[
5eb50 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 ] */. int nAccu
5eb60 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f mulator; /
5eb70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
5eb80 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 mns that show th
5eb90 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 rough to the out
5eba0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 put..
5ebb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
5ebc0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c * Additional col
5ebd0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e umns are used on
5ebe0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 ly as parameters
5ebf0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 to.
5ec00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
5ec10 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
5ec20 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ions */. struct
5ec30 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 AggInfo_func {
5ec40 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 /* For each ag
5ec50 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
5ec60 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 */. Expr *pE
5ec70 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
5ec80 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 /* Expression e
5ec90 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 ncoding the func
5eca0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 tion */. Func
5ecb0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
5ecc0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 /* The aggr
5ecd0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 egate function i
5ece0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f mplementation */
5ecf0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 . int iMem;
5ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5ed10 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e Memory location
5ed20 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 that acts as ac
5ed30 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 cumulator */.
5ed40 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 int iDistinct;
5ed50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 /* Eph
5ed60 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 emeral table use
5ed70 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 d to enforce DIS
5ed80 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 TINCT */. } *aF
5ed90 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 unc;. int nFunc
5eda0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5edb0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
5edc0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a ies in aFunc[] *
5edd0 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c /. int nFuncAll
5ede0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e oc; /* N
5edf0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 umber of slots a
5ee00 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 llocated for aFu
5ee10 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nc[] */.};../*.*
5ee20 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 * Each node of a
5ee30 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 n expression in
5ee40 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 the parse tree i
5ee50 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a s an instance.**
5ee60 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
5ee70 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f re..**.** Expr.o
5ee80 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e p is the opcode.
5ee90 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
5eea0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
5eeb0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
5eec0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46 opcodes here. F
5eed0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 or example, the
5eee0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54 parser defines T
5eef0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e K_GE to be an in
5ef00 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65 teger.** code re
5ef10 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22 presenting the "
5ef20 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 >=" operator. Th
5ef30 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20 is same integer
5ef40 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a code is reused.*
5ef50 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74 * to represent t
5ef60 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d he greater-than-
5ef70 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72 or-equal-to oper
5ef80 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72 ator in the expr
5ef90 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a ession.** tree..
5efa0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 **.** If the exp
5efb0 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 ression is an SQ
5efc0 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e L literal (TK_IN
5efd0 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c TEGER, TK_FLOAT,
5efe0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 TK_BLOB, .** or
5eff0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65 TK_STRING), the
5f000 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e n Expr.token con
5f010 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f tains the text o
5f020 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61 f the SQL litera
5f030 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70 l. If.** the exp
5f040 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72 ression is a var
5f050 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42 iable (TK_VARIAB
5f060 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 LE), then Expr.t
5f070 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
5f080 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e e .** variable n
5f090 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66 ame. Finally, if
5f0a0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
5f0b0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
5f0c0 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 on (TK_FUNCTION)
5f0d0 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74 ,.** then Expr.t
5f0e0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
5f0f0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 e name of the fu
5f100 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 nction..**.** Ex
5f110 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78 pr.pRight and Ex
5f120 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65 pr.pLeft are the
5f130 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 left and right
5f140 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f subexpressions o
5f150 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70 f a.** binary op
5f160 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f erator. Either o
5f170 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55 r both may be NU
5f180 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 LL..**.** Expr.x
5f190 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 .pList is a list
5f1a0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66 of arguments if
5f1b0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
5f1c0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
5f1d0 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78 on,.** a CASE ex
5f1e0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49 pression or an I
5f1f0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 N expression of
5f200 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 the form "<lhs>
5f210 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 IN (<y>, <z>...)
5f220 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65 "..** Expr.x.pSe
5f230 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20 lect is used if
5f240 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
5f250 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f s a sub-select o
5f260 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 r an expression
5f270 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 of.** the form "
5f280 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54 <lhs> IN (SELECT
5f290 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45 ...)". If the E
5f2a0 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20 P_xIsSelect bit
5f2b0 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a is set in the.**
5f2c0 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b Expr.flags mask
5f2d0 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53 , then Expr.x.pS
5f2e0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20 elect is valid.
5f2f0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e Otherwise, Expr.
5f300 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76 x.pList is .** v
5f310 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 alid..**.** An e
5f320 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 xpression of the
5f330 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 form ID or ID.I
5f340 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f D refers to a co
5f350 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e lumn in a table.
5f360 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70 .** For such exp
5f370 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f ressions, Expr.o
5f380 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 p is set to TK_C
5f390 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 OLUMN and Expr.i
5f3a0 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 Table is.** the
5f3b0 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e integer cursor n
5f3c0 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20 umber of a VDBE
5f3d0 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 cursor pointing
5f3e0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e to that table an
5f3f0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d d.** Expr.iColum
5f400 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 n is the column
5f410 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 number for the s
5f420 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 pecific column.
5f430 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 If the.** expre
5f440 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 ssion is used as
5f450 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 a result in an
5f460 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54 aggregate SELECT
5f470 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 , then the.** va
5f480 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 lue is also stor
5f490 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69 ed in the Expr.i
5f4a0 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Agg column in th
5f4b0 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74 e aggregate so t
5f4c0 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 hat.** it can be
5f4d0 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20 accessed after
5f4e0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61 all aggregates a
5f4f0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a re computed..**.
5f500 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
5f510 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 sion is an unbou
5f520 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b nd variable mark
5f530 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d er (a question m
5f540 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65 ark .** characte
5f550 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 r '?' in the ori
5f560 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 ginal SQL) then
5f570 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 the Expr.iTable
5f580 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20 holds the index
5f590 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 .** number for t
5f5a0 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a hat variable..**
5f5b0 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
5f5c0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75 ssion is a subqu
5f5d0 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43 ery then Expr.iC
5f5e0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 olumn holds an i
5f5f0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 nteger.** regist
5f600 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 er number contai
5f610 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 ning the result
5f620 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e of the subquery.
5f630 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 If the.** subq
5f640 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e uery gives a con
5f650 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 stant result, th
5f660 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e en iTable is -1.
5f670 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72 If the subquer
5f680 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66 y.** gives a dif
5f690 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74 ferent answer at
5f6a0 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73 different times
5f6b0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e during statemen
5f6c0 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 t processing.**
5f6d0 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74 then iTable is t
5f6e0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 he address of a
5f6f0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 subroutine that
5f700 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62 computes the sub
5f710 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 query..**.** If
5f720 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74 the Expr is of t
5f730 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 ype OP_Column, a
5f740 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20 nd the table it
5f750 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f is selecting fro
5f760 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74 m.** is a disk t
5f770 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 able or the "old
5f780 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 .*" pseudo-table
5f790 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e , then pTab poin
5f7a0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 ts to the.** cor
5f7b0 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 responding table
5f7c0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a definition..**.
5f7d0 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f ** ALLOCATION NO
5f7e0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 TES:.**.** Expr
5f7f0 6f 62 6a 65 63 74 73 20 63 61 6e 20 75 73 65 20 objects can use
5f800 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20 a lot of memory
5f810 73 70 61 63 65 20 69 6e 20 64 61 74 61 62 61 73 space in databas
5f820 65 20 73 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a e schema. To.**
5f830 20 68 65 6c 70 20 72 65 64 75 63 65 20 6d 65 6d help reduce mem
5f840 6f 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73 ory requirements
5f850 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45 , sometimes an E
5f860 78 70 72 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 xpr object will
5f870 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e be.** truncated.
5f880 20 20 41 6e 64 20 74 6f 20 72 65 64 75 63 65 20 And to reduce
5f890 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 the number of me
5f8a0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
5f8b0 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 , sometimes.** t
5f8c0 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78 70 72 20 wo or more Expr
5f8d0 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20 62 65 20 objects will be
5f8e0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 stored in a sing
5f8f0 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 le memory alloca
5f900 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 tion,.** togethe
5f910 72 20 77 69 74 68 20 45 78 70 72 2e 74 6f 6b 65 r with Expr.toke
5f920 6e 20 61 6e 64 2f 6f 72 20 45 78 70 72 2e 73 70 n and/or Expr.sp
5f930 61 6e 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a an strings..**.*
5f940 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 * If the EP_Redu
5f950 63 65 64 2c 20 45 50 5f 53 70 61 6e 54 6f 6b 65 ced, EP_SpanToke
5f960 6e 2c 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f n, and EP_TokenO
5f970 6e 6c 79 20 66 6c 61 67 73 20 61 72 65 20 73 65 nly flags are se
5f980 74 20 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 t when.** an Exp
5f990 72 20 6f 62 6a 65 63 74 20 69 73 20 74 72 75 6e r object is trun
5f9a0 63 61 74 65 64 2e 20 20 57 68 65 6e 20 45 50 5f cated. When EP_
5f9b0 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2c 20 Reduced is set,
5f9c0 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 then all.** the
5f9d0 63 68 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 63 child Expr objec
5f9e0 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 70 ts in the Expr.p
5f9f0 4c 65 66 74 20 61 6e 64 20 45 78 70 72 2e 70 52 Left and Expr.pR
5fa00 69 67 68 74 20 73 75 62 74 72 65 65 73 0a 2a 2a ight subtrees.**
5fa10 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 are contained w
5fa20 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 6d ithin the same m
5fa30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
5fa40 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 . Note, however
5fa50 2c 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 75 , that.** the su
5fa60 62 74 72 65 65 73 20 69 6e 20 45 78 70 72 2e 78 btrees in Expr.x
5fa70 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 .pList or Expr.x
5fa80 2e 70 53 65 6c 65 63 74 20 61 72 65 20 61 6c 77 .pSelect are alw
5fa90 61 79 73 20 73 65 70 61 72 61 74 65 6c 79 0a 2a ays separately.*
5faa0 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67 * allocated, reg
5fab0 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
5fac0 65 72 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64 er or not EP_Red
5fad0 75 63 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a uced is set..*/.
5fae0 73 74 72 75 63 74 20 45 78 70 72 20 7b 0a 20 20 struct Expr {.
5faf0 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 u8 op;
5fb00 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 /* Operat
5fb10 69 6f 6e 20 70 65 72 66 6f 72 6d 65 64 20 62 79 ion performed by
5fb20 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 this node */.
5fb30 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 char affinity;
5fb40 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 /* The af
5fb50 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f finity of the co
5fb60 6c 75 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 lumn or 0 if not
5fb70 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 56 a column */. V
5fb80 56 41 5f 4f 4e 4c 59 28 75 38 20 76 76 61 46 6c VA_ONLY(u8 vvaFl
5fb90 61 67 73 3b 29 20 2f 2a 20 46 6c 61 67 73 20 75 ags;) /* Flags u
5fba0 73 65 64 20 66 6f 72 20 56 56 26 41 20 6f 6e 6c sed for VV&A onl
5fbb0 79 2e 20 20 45 56 56 41 5f 2a 20 62 65 6c 6f 77 y. EVVA_* below
5fbc0 2e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 . */. u16 flags
5fbd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
5fbe0 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 Various flags.
5fbf0 20 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 EP_* See below
5fc00 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 6f 6b 65 6e */. Token token
5fc10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ; /* A
5fc20 6e 20 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 n operand token
5fc30 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 */.. /* If the
5fc40 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 EP_TokenOnly fla
5fc50 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 g is set in the
5fc60 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c Expr.flags mask,
5fc70 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 then no. ** sp
5fc80 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ace is allocated
5fc90 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 for the fields
5fca0 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 below this point
5fcb0 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a . An attempt to.
5fcc0 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d ** access them
5fcd0 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
5fce0 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 a segfault or ma
5fcf0 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a lfunction. . **
5fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd40 2a 2a 2a 2f 0a 0a 20 20 54 6f 6b 65 6e 20 73 70 ***/.. Token sp
5fd50 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f an; /
5fd60 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 * Complete text
5fd70 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
5fd80 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 n */.. /* If th
5fd90 65 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 20 66 e EP_SpanToken f
5fda0 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 lag is set in th
5fdb0 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 e Expr.flags mas
5fdc0 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 k, then no. **
5fdd0 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 space is allocat
5fde0 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 ed for the field
5fdf0 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 s below this poi
5fe00 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 nt. An attempt t
5fe10 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 o. ** access th
5fe20 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 em will result i
5fe30 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 n a segfault or
5fe40 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 malfunction. .
5fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe90 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a *****/.. Expr *
5fea0 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 pLeft;
5feb0 20 2f 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 65 /* Left subnode
5fec0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 */. Expr *pRig
5fed0 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ht; /*
5fee0 52 69 67 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f Right subnode */
5fef0 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 . union {. E
5ff00 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 xprList *pList;
5ff10 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 /* Function
5ff20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20 arguments or in
5ff30 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 "<expr> IN (<exp
5ff40 72 2d 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20 20 r-list)" */.
5ff50 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b Select *pSelect;
5ff60 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 /* Used for
5ff70 20 73 75 62 2d 73 65 6c 65 63 74 73 20 61 6e 64 sub-selects and
5ff80 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 "<expr> IN (<se
5ff90 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 lect>)" */. } x
5ffa0 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f ;. CollSeq *pCo
5ffb0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ll; /* Th
5ffc0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 e collation type
5ffd0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f of the column o
5ffe0 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 r 0 */.. /* If
5fff0 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 the EP_Reduced f
60000 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 lag is set in th
60010 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 e Expr.flags mas
60020 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 k, then no. **
60030 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 space is allocat
60040 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 ed for the field
60050 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 s below this poi
60060 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 nt. An attempt t
60070 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 o. ** access th
60080 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 em will result i
60090 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 n a segfault or
600a0 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a malfunction.. *
600b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600f0 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61 ****/.. int iTa
60100 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 ble, iColumn;
60110 2f 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 /* When op==TK_C
60120 4f 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 OLUMN, then this
60130 20 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 expr node means
60140 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 the.
60150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
60160 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c iColumn-th fiel
60170 64 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d d of the iTable-
60180 74 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 th table. */. A
60190 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f ggInfo *pAggInfo
601a0 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 ; /* Used by
601b0 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 TK_AGG_COLUMN a
601c0 6e 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 nd TK_AGG_FUNCTI
601d0 4f 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 ON */. int iAgg
601e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
601f0 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e * Which entry in
60200 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b pAggInfo->aCol[
60210 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a ] or ->aFunc[] *
60220 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f /. int iRightJo
60230 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 inTable; /* If
60240 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 EP_FromJoin, th
60250 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 e right table of
60260 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 54 the join */. T
60270 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
60280 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 /* Table f
60290 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 or TK_COLUMN exp
602a0 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 ressions. */.#if
602b0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 SQLITE_MAX_EXPR
602c0 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e _DEPTH>0. int n
602d0 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 Height;
602e0 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 /* Height of t
602f0 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20 62 he tree headed b
60300 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 y this node */.#
60310 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
60320 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 The following ar
60330 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f e the meanings o
60340 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 f bits in the Ex
60350 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a pr.flags field..
60360 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 */.#define EP_Fr
60370 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 omJoin 0x0001
60380 20 2f 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 /* Originated i
60390 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c n ON or USING cl
603a0 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a ause of a join *
603b0 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 /.#define EP_Agg
603c0 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20 0x0002
603d0 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 /* Contains one
603e0 6f 72 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 or more aggregat
603f0 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 e functions */.#
60400 64 65 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 define EP_Resolv
60410 65 64 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 ed 0x0004 /*
60420 49 44 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 IDs have been re
60430 73 6f 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e solved to COLUMN
60440 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f s */.#define EP_
60450 45 72 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 Error 0x000
60460 38 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 8 /* Expression
60470 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 contains one or
60480 20 6d 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a more errors */.
60490 23 64 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 #define EP_Disti
604a0 6e 63 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a nct 0x0010 /*
604b0 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 Aggregate funct
604c0 69 6f 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 ion with DISTINC
604d0 54 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 T keyword */.#de
604e0 66 69 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 fine EP_VarSelec
604f0 74 20 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 t 0x0020 /* pS
60500 65 6c 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 elect is correla
60510 74 65 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e ted, not constan
60520 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f t */.#define EP_
60530 44 62 6c 51 75 6f 74 65 64 20 20 30 78 30 30 34 DblQuoted 0x004
60540 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 0 /* token.z wa
60550 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20 s originally in
60560 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 66 69 6e 65 "..." */.#define
60570 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 30 EP_InfixFunc 0
60580 78 30 30 38 30 20 20 2f 2a 20 54 72 75 65 20 66 x0080 /* True f
60590 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e 63 or an infix func
605a0 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 tion: LIKE, GLOB
605b0 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65 , etc */.#define
605c0 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30 EP_ExpCollate 0
605d0 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 x0100 /* Collat
605e0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 ing sequence spe
605f0 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74 6c cified explicitl
60600 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
60610 41 6e 79 41 66 66 20 20 20 20 20 30 78 30 32 30 AnyAff 0x020
60620 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61 0 /* Can take a
60630 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f cached column o
60640 66 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 2a f any affinity *
60650 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 69 78 /.#define EP_Fix
60660 65 64 44 65 73 74 20 20 30 78 30 34 30 30 20 20 edDest 0x0400
60670 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 64 65 64 /* Result needed
60680 20 69 6e 20 61 20 73 70 65 63 69 66 69 63 20 72 in a specific r
60690 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 65 66 69 egister */.#defi
606a0 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 20 ne EP_IntValue
606b0 20 30 78 30 38 30 30 20 20 2f 2a 20 49 6e 74 65 0x0800 /* Inte
606c0 67 65 72 20 76 61 6c 75 65 20 63 6f 6e 74 61 69 ger value contai
606d0 6e 65 64 20 69 6e 20 69 54 61 62 6c 65 20 2a 2f ned in iTable */
606e0 0a 23 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53 .#define EP_xIsS
606f0 65 6c 65 63 74 20 20 30 78 31 30 30 30 20 20 2f elect 0x1000 /
60700 2a 20 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 * x.pSelect is v
60710 61 6c 69 64 20 28 6f 74 68 65 72 77 69 73 65 20 alid (otherwise
60720 78 2e 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a x.pList is) */..
60730 23 64 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63 #define EP_Reduc
60740 65 64 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a ed 0x2000 /*
60750 20 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20 Expr struct is
60760 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 EXPR_REDUCEDSIZE
60770 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 bytes only */.#
60780 64 65 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f define EP_TokenO
60790 6e 6c 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20 nly 0x4000 /*
607a0 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 Expr struct is E
607b0 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a XPR_TOKENONLYSIZ
607c0 45 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a E bytes only */.
607d0 23 64 65 66 69 6e 65 20 45 50 5f 53 70 61 6e 54 #define EP_SpanT
607e0 6f 6b 65 6e 20 20 30 78 38 30 30 30 20 20 2f 2a oken 0x8000 /*
607f0 20 45 78 70 72 20 73 69 7a 65 20 69 73 20 45 58 Expr size is EX
60800 50 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 PR_SPANTOKENSIZE
60810 20 62 79 74 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a bytes */../*.**
60820 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
60830 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 re the meanings
60840 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45 of bits in the E
60850 78 70 72 2e 76 76 61 46 6c 61 67 73 20 66 69 65 xpr.vvaFlags fie
60860 6c 64 2e 0a 2a 2a 20 54 68 69 73 20 69 6e 66 6f ld..** This info
60870 72 6d 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 rmation is only
60880 75 73 65 64 20 77 68 65 6e 20 53 51 4c 69 74 65 used when SQLite
60890 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 is compiled wit
608a0 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 h.** SQLITE_DEBU
608b0 47 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 G defined..*/.#i
608c0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 64 65 fndef NDEBUG.#de
608d0 66 69 6e 65 20 45 56 56 41 5f 52 65 61 64 4f 6e fine EVVA_ReadOn
608e0 6c 79 54 6f 6b 65 6e 20 20 30 78 30 31 20 20 2f lyToken 0x01 /
608f0 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 69 * Expr.token.z i
60900 73 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 23 s read-only */.#
60910 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
60920 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 se macros can be
60930 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 used to test, s
60940 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 et, or clear bit
60950 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 s in the .** Exp
60960 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a r.flags field..*
60970 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 /.#define ExprHa
60980 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 sProperty(E,P)
60990 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 (((E)->flags&
609a0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 (P))==(P)).#defi
609b0 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f ne ExprHasAnyPro
609c0 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 perty(E,P) (((E
609d0 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 )->flags&(P))!=0
609e0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 ).#define ExprSe
609f0 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 tProperty(E,P)
60a00 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 (E)->flags|=(
60a10 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 P).#define ExprC
60a20 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 learProperty(E,P
60a30 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d ) (E)->flags&=
60a40 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ~(P)../*.** Macr
60a50 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 os to determine
60a60 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
60a70 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20 tes required by
60a80 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a a normal Expr .*
60a90 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 * struct, an Exp
60aa0 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 r struct with th
60ab0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 e EP_Reduced fla
60ac0 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c g set in Expr.fl
60ad0 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 ags .** and an E
60ae0 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 xpr struct with
60af0 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 the EP_TokenOnly
60b00 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 flag set..*/.#d
60b10 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 efine EXPR_FULLS
60b20 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69 IZE si
60b30 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 zeof(Expr)
60b40 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a /* Full siz
60b50 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 e */.#define EXP
60b60 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 R_REDUCEDSIZE
60b70 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 offsetof(Ex
60b80 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 pr,iTable) /* C
60b90 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a ommon features *
60ba0 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 53 /.#define EXPR_S
60bb0 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 20 20 20 20 PANTOKENSIZE
60bc0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c offsetof(Expr,
60bd0 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 pLeft) /* Fewe
60be0 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64 r features */.#d
60bf0 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45 4e efine EXPR_TOKEN
60c00 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f 66 ONLYSIZE of
60c10 66 73 65 74 6f 66 28 45 78 70 72 2c 73 70 61 6e fsetof(Expr,span
60c20 29 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 ) /* Smallest
60c30 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 0a 2f 2a possible */../*
60c40 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 .** Flags passed
60c50 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 to the sqlite3E
60c60 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f xprDup() functio
60c70 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 n. See the heade
60c80 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 r comment .** ab
60c90 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 ove sqlite3ExprD
60ca0 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 up() for details
60cb0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 ..*/.#define EXP
60cc0 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 RDUP_REDUCE
60cd0 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 0x0001 /* U
60ce0 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 sed reduced-size
60cf0 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 23 Expr nodes */.#
60d00 64 65 66 69 6e 65 20 45 58 50 52 44 55 50 5f 53 define EXPRDUP_S
60d10 50 41 4e 20 20 20 20 20 20 20 20 20 20 20 30 78 PAN 0x
60d20 30 30 30 32 20 20 2f 2a 20 4d 61 6b 65 20 61 20 0002 /* Make a
60d30 63 6f 70 79 20 6f 66 20 45 78 70 72 2e 73 70 61 copy of Expr.spa
60d40 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 n */../*.** A li
60d50 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e st of expression
60d60 73 2e 20 20 45 61 63 68 20 65 78 70 72 65 73 73 s. Each express
60d70 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c ion may optional
60d80 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d ly have a.** nam
60d90 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 e. An expr/name
60da0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e combination can
60db0 20 62 65 20 75 73 65 64 20 69 6e 20 73 65 76 65 be used in seve
60dc0 72 61 6c 20 77 61 79 73 2c 20 73 75 63 68 0a 2a ral ways, such.*
60dd0 2a 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66 * as the list of
60de0 20 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69 "expr AS ID" fi
60df0 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 elds following a
60e00 20 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 "SELECT" or in
60e10 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 the.** list of "
60e20 49 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73 ID = expr" items
60e30 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20 in an UPDATE.
60e40 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 A list of expres
60e50 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 sions can.** als
60e60 6f 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 o be used as the
60e70 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 argument to a f
60e80 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 unction, in whic
60e90 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 h case the a.zNa
60ea0 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e me.** field is n
60eb0 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 ot used..*/.stru
60ec0 63 74 20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 ct ExprList {.
60ed0 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 int nExpr;
60ee0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
60ef0 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 of expressions
60f00 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 on the list */.
60f10 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 int nAlloc;
60f20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
60f30 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c r of entries all
60f40 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a ocated below */.
60f50 20 20 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 int iECursor;
60f60 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 /* VDBE
60f70 20 43 75 72 73 6f 72 20 61 73 73 6f 63 69 61 74 Cursor associat
60f80 65 64 20 77 69 74 68 20 74 68 69 73 20 45 78 70 ed with this Exp
60f90 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 rList */. struc
60fa0 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
60fb0 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 {. Expr *pExp
60fc0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
60fd0 54 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 The list of expr
60fe0 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 essions */. c
60ff0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
61000 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 /* Token a
61010 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
61020 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a his expression *
61030 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64 /. u8 sortOrd
61040 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 er; /*
61050 31 20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20 1 for DESC or 0
61060 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 for ASC */. u
61070 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 8 done;
61080 20 20 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 /* A flag
61090 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e to indicate when
610a0 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66 processing is f
610b0 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 inished */. u
610c0 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 16 iCol;
610d0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 /* For ORD
610e0 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 ER BY, column nu
610f0 6d 62 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73 mber in result s
61100 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 et */. u16 iA
61110 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 20 lias;
61120 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 /* Index into P
61130 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f arse.aAlias[] fo
61140 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a r zName */. } *
61150 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
61160 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 /* One entry
61170 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73 for each expres
61180 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a sion */.};../*.*
61190 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
611a0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
611b0 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c can hold a simpl
611c0 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69 e list of identi
611d0 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 fiers,.** such a
611e0 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c s the list "a,b,
611f0 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 c" in the follow
61200 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a ing statements:.
61210 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 **.** INSER
61220 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 T INTO t(a,b,c)
61230 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 VALUES ...;.**
61240 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 CREATE INDEX
61250 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 idx ON t(a,b,c)
61260 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 ;.** CREATE
61270 20 54 52 49 47 47 45 52 20 74 72 69 67 20 42 45 TRIGGER trig BE
61280 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74 FORE UPDATE ON t
61290 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a (a,b,c) ...;.**.
612a0 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e ** The IdList.a.
612b0 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 73 65 idx field is use
612c0 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73 d when the IdLis
612d0 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 t represents the
612e0 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 list of.** colu
612f0 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61 mn names after a
61300 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 table name in a
61310 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 n INSERT stateme
61320 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74 nt. In the stat
61330 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ement.**.**
61340 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c INSERT INTO t(a,
61350 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 b,c) ....**.** I
61360 66 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 f "a" is the k-t
61370 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c h column of tabl
61380 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 e "t", then IdLi
61390 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a st.a[0].idx==k..
613a0 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74 */.struct IdList
613b0 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69 {. struct IdLi
613c0 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 st_item {. ch
613d0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
613e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 /* Name of the i
613f0 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 dentifier */.
61400 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 int idx;
61410 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 /* Index in s
61420 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d ome Table.aCol[]
61430 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d of a column nam
61440 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 ed zName */. }
61450 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 *a;. int nId;
61460 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
61470 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 20 of identifiers
61480 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 on the list */.
61490 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 int nAlloc;
614a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
614b0 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 ntries allocated
614c0 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a for a[] below *
614d0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
614e0 62 69 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 bitmask datatype
614f0 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 defined below i
61500 73 20 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f s used for vario
61510 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 us optimizations
61520 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 ..**.** Changing
61530 20 74 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d this from a 64-
61540 62 69 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 bit to a 32-bit
61550 74 79 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 type limits the
61560 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 number of.** tab
61570 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f les in a join to
61580 20 33 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 32 instead of 6
61590 34 2e 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 4. But it also
615a0 72 65 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 reduces the size
615b0 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 .** of the libra
615c0 72 79 20 62 79 20 37 33 38 20 62 79 74 65 73 20 ry by 738 bytes
615d0 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 on ix86..*/.type
615e0 64 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b def u64 Bitmask;
615f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 ../*.** The numb
61600 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 er of bits in a
61610 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 Bitmask. "BMS"
61620 6d 65 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 means "BitMask S
61630 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ize"..*/.#define
61640 20 42 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a BMS ((int)(siz
61650 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 eof(Bitmask)*8))
61660 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
61670 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
61680 64 65 73 63 72 69 62 65 73 20 74 68 65 20 46 52 describes the FR
61690 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 OM clause of a S
616a0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
616b0 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f .** Each table o
616c0 72 20 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 r subquery in th
616d0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 e FROM clause is
616e0 20 61 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d a separate elem
616f0 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 ent of.** the Sr
61700 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e cList.a[] array.
61710 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 .**.** With the
61720 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 addition of mult
61730 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 iple database su
61740 70 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f pport, the follo
61750 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a wing structure.*
61760 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 * can also be us
61770 65 64 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 ed to describe a
61780 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c particular tabl
61790 65 20 73 75 63 68 20 61 73 20 74 68 65 20 74 61 e such as the ta
617a0 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d ble that.** is m
617b0 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e odified by an IN
617c0 53 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 SERT, DELETE, or
617d0 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e UPDATE statemen
617e0 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 t. In standard
617f0 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 SQL,.** such a t
61800 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 able must be a s
61810 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 imple name: ID.
61820 20 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 But in SQLite,
61830 74 68 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a the table can.**
61840 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 now be identifi
61850 65 64 20 62 79 20 61 20 64 61 74 61 62 61 73 65 ed by a database
61860 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 name, a dot, th
61870 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d en the table nam
61880 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 e: ID.ID..**.**
61890 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 The jointype sta
618a0 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 rts out showing
618b0 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 the join type be
618c0 74 77 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e tween the curren
618d0 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 t table.** and t
618e0 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e he next table on
618f0 20 74 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 the list. The
61900 70 61 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 parser builds th
61910 65 20 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e e list this way.
61920 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 .** But sqlite3S
61930 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 rcListShiftJoinT
61940 79 70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 ype() later shif
61950 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 ts the jointypes
61960 20 73 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a so that each.**
61970 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 jointype expres
61980 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 ses the join bet
61990 77 65 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 ween the table a
619a0 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 nd the previous
619b0 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 table..*/.struct
619c0 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 SrcList {. i16
619d0 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a nSrc; /*
619e0 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 Number of table
619f0 73 20 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 s or subqueries
61a00 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
61a10 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c se */. i16 nAll
61a20 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 oc; /* Numb
61a30 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c er of entries al
61a40 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 located in a[] b
61a50 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 elow */. struct
61a60 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a SrcList_item {.
61a70 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62 char *zDatab
61a80 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 ase; /* Name of
61a90 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e database holdin
61aa0 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a g this table */.
61ab0 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b char *zName;
61ac0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
61ad0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
61ae0 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 char *zAlias;
61af0 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70 /* The "B" p
61b00 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 20 42 art of a "A AS B
61b10 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 " phrase. zName
61b20 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 is the "A" */.
61b30 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 Table *pTab;
61b40 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 /* An SQL t
61b50 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 able correspondi
61b60 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 ng to zName */.
61b70 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 Select *pSele
61b80 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 ct; /* A SELECT
61b90 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 statement used
61ba0 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 in place of a ta
61bb0 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 ble name */.
61bc0 75 38 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 u8 isPopulated;
61bd0 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 /* Temporary t
61be0 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 able associated
61bf0 77 69 74 68 20 53 45 4c 45 43 54 20 69 73 20 70 with SELECT is p
61c00 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 opulated */.
61c10 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 u8 jointype;
61c20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 /* Type of joi
61c30 6e 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 61 n between this a
61c40 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 ble and the prev
61c50 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e ious */. u8 n
61c60 6f 74 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a otIndexed; /*
61c70 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 True if there i
61c80 73 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 s a NOT INDEXED
61c90 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e clause */. in
61ca0 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 t iCursor;
61cb0 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
61cc0 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 or number used t
61cd0 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 74 61 o access this ta
61ce0 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 ble */. Expr
61cf0 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 *pOn; /*
61d00 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 The ON clause of
61d10 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 a join */. I
61d20 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 dList *pUsing;
61d30 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c /* The USING cl
61d40 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a ause of a join *
61d50 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f /. Bitmask co
61d60 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e lUsed; /* Bit N
61d70 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 (1<<N) set if c
61d80 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 olumn N of pTab
61d90 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 is used */. c
61da0 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 har *zIndex;
61db0 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66 /* Identifier f
61dc0 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20 rom "INDEXED BY
61dd0 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 <zIndex>" clause
61de0 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 */. Index *p
61df0 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 Index; /* Ind
61e00 65 78 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 ex structure cor
61e10 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 responding to zI
61e20 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a ndex, if any */.
61e30 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 } a[1];
61e40 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 /* One ent
61e50 72 79 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e ry for each iden
61e60 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 tifier on the li
61e70 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
61e80 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73 Permitted values
61e90 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e of the SrcList.
61ea0 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 a.jointype field
61eb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 .*/.#define JT_I
61ec0 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30 31 20 NNER 0x0001
61ed0 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f /* Any kind o
61ee0 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 f inner or cross
61ef0 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 join */.#define
61f00 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78 JT_CROSS 0x
61f10 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 0002 /* Expli
61f20 63 69 74 20 75 73 65 20 6f 66 20 74 68 65 20 43 cit use of the C
61f30 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a ROSS keyword */.
61f40 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 #define JT_NATUR
61f50 41 4c 20 20 20 30 78 30 30 30 34 20 20 20 20 2f AL 0x0004 /
61f60 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61 * True for a "na
61f70 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 tural" join */.#
61f80 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 define JT_LEFT
61f90 20 20 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a 0x0008 /*
61fa0 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e Left outer join
61fb0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 */.#define JT_R
61fc0 49 47 48 54 20 20 20 20 20 30 78 30 30 31 30 20 IGHT 0x0010
61fd0 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 /* Right oute
61fe0 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e r join */.#defin
61ff0 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 e JT_OUTER 0
62000 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 x0020 /* The
62010 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 "OUTER" keyword
62020 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 is present */.#d
62030 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 efine JT_ERROR
62040 20 20 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 0x0040 /*
62050 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 unknown or unsup
62060 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 ported join type
62070 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 */.../*.** A Wh
62080 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 erePlan object h
62090 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e olds information
620a0 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
620b0 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 a lookup.** stra
620c0 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
620d0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e object is inten
620e0 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 ded to be opaque
620f0 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 outside of the
62100 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a where.c module..
62110 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 ** It is include
62120 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 d here only so t
62130 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 hat that compile
62140 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 r will know how
62150 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e big it.** is. N
62160 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 one of the field
62170 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 s in this object
62180 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
62190 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 outside of.** th
621a0 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 e where.c module
621b0 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 ..**.** Within t
621c0 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 he union, pIdx i
621d0 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e s only used when
621e0 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 wsFlags&WHERE_I
621f0 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a NDEXED is true..
62200 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 ** pTerm is only
62210 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 used when wsFla
62220 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f gs&WHERE_MULTI_O
62230 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20 R is true. And
62240 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f pVtabIdx.** is o
62250 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 nly used when ws
62260 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 Flags&WHERE_VIRT
62270 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65 UALTABLE is true
62280 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72 20 74 . It is never t
62290 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 he.** case that
622a0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 more than one of
622b0 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e these condition
622c0 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 s is true..*/.st
622d0 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b ruct WherePlan {
622e0 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 . u32 wsFlags;
622f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62300 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 /* WHERE_* fla
62310 67 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 gs that describe
62320 20 74 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f the strategy */
62330 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20 . u32 nEq;
62340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62350 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d /* Number of =
62360 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f = constraints */
62370 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 . union {. I
62380 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 ndex *pIdx;
62390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
623a0 20 49 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 Index when WHER
623b0 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 E_INDEXED is tru
623c0 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 e */. struct
623d0 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d WhereTerm *pTerm
623e0 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 ; /* WHERE
623f0 20 63 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72 clause term for
62400 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 OR-search */.
62410 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f sqlite3_index_
62420 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 info *pVtabIdx;
62430 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c /* Virtual tabl
62440 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a e index to use *
62450 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a /. } u;.};../*.
62460 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74 ** For each nest
62470 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 ed loop in a WHE
62480 52 45 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d RE clause implem
62490 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 entation, the Wh
624a0 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 ereInfo.** struc
624b0 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 ture contains a
624c0 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 single instance
624d0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
624e0 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 e. This structu
624f0 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 re.** is intende
62500 64 20 74 6f 20 62 65 20 70 72 69 76 61 74 65 20 d to be private
62510 74 68 65 20 74 68 65 20 77 68 65 72 65 2e 63 20 the the where.c
62520 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c module and shoul
62530 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 d not be.** acce
62540 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 ss or modified b
62550 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e y other modules.
62560 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 .**.** The pIdxI
62570 6e 66 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65 nfo field is use
62580 64 20 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 d to help pick t
62590 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e he best index on
625a0 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 a.** virtual ta
625b0 62 6c 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e ble. The pIdxIn
625c0 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 fo pointer conta
625d0 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 ins indexing.**
625e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
625f0 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69 the i-th table i
62600 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
62610 65 20 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72 e before reorder
62620 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 ing..** All the
62630 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 pIdxInfo pointer
62640 73 20 61 72 65 20 66 72 65 65 64 20 62 79 20 77 s are freed by w
62650 68 65 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 hereInfoFree() i
62660 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c n where.c..** Al
62670 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 l other informat
62680 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 ion in the i-th
62690 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 WhereLevel objec
626a0 74 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 t for the i-th t
626b0 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 able.** after FR
626c0 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69 OM clause orderi
626d0 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 ng..*/.struct Wh
626e0 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 ereLevel {. Whe
626f0 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 rePlan plan;
62700 20 20 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e /* query plan
62710 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e for this elemen
62720 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c t of the FROM cl
62730 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c ause */. int iL
62740 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 eftJoin;
62750 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 /* Memory cell u
62760 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
62770 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e LEFT OUTER JOIN
62780 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 */. int iTabCu
62790 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 r; /* T
627a0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 he VDBE cursor u
627b0 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 sed to access th
627c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 e table */. int
627d0 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 iIdxCur;
627e0 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 /* The VDBE c
627f0 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 ursor used to ac
62800 63 65 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 cess pIdx */. i
62810 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 nt addrBrk;
62820 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 /* Jump her
62830 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f e to break out o
62840 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 f the loop */.
62850 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 int addrNxt;
62860 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
62870 72 65 20 74 6f 20 73 74 61 72 74 20 74 68 65 20 re to start the
62880 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 next IN combinat
62890 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 ion */. int add
628a0 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f rCont; /
628b0 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 * Jump here to c
628c0 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 ontinue with the
628d0 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 next loop cycle
628e0 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69 */. int addrFi
628f0 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 rst; /* F
62900 69 72 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e irst instruction
62910 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 of interior of
62920 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 the loop */. u8
62930 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 iFrom;
62940 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 /* Which ent
62950 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 ry in the FROM c
62960 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 lause */. u8 op
62970 2c 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 , p5;
62980 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 /* Opcode and P
62990 35 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 5 of the opcode
629a0 74 68 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f that ends the lo
629b0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 op */. int p1,
629c0 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a p2; /*
629d0 20 4f 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65 Operands of the
629e0 20 6f 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20 opcode used to
629f0 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f ends the loop */
62a00 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 . union {
62a10 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f /* Info
62a20 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 rmation that dep
62a30 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 ends on plan.wsF
62a40 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 lags */. stru
62a50 63 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e ct {. int n
62a60 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 In;
62a70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
62a80 74 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 tries in aInLoop
62a90 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 [] */. stru
62aa0 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 ct InLoop {.
62ab0 20 20 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 int iCur;
62ac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
62ad0 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 e VDBE cursor us
62ae0 65 64 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 ed by this IN op
62af0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 erator */.
62b00 20 20 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b int addrInTop;
62b10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 /* Top
62b20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a of the IN loop *
62b30 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f /. } *aInLo
62b40 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a op; /*
62b50 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
62b60 75 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 ut each nested I
62b70 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 N operator */.
62b80 20 20 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20 } in;
62b90 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
62ba0 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 when plan.wsFlag
62bb0 73 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 s&WHERE_IN_ABLE
62bc0 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 */. } u;.. /*
62bd0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 The following fi
62be0 65 6c 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f eld is really no
62bf0 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 75 t part of the cu
62c00 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 rrent level. Bu
62c10 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 t. ** we need a
62c20 20 70 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20 place to cache
62c30 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e virtual table in
62c40 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 dex information
62c50 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 for each. ** vi
62c60 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 rtual table in t
62c70 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 he FROM clause a
62c80 6e 64 20 74 68 65 20 57 68 65 72 65 4c 65 76 65 nd the WhereLeve
62c90 6c 20 73 74 72 75 63 74 75 72 65 20 69 73 0a 20 l structure is.
62ca0 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 ** a convenient
62cb0 20 70 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65 place since the
62cc0 72 65 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c re is one WhereL
62cd0 65 76 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52 evel for each FR
62ce0 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 OM clause. ** e
62cf0 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 lement.. */. s
62d00 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
62d10 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a o *pIdxInfo; /*
62d20 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 Index info for
62d30 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c n-th source tabl
62d40 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 e */.};../*.** F
62d50 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 lags appropriate
62d60 20 66 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c for the wctrlFl
62d70 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66 ags parameter of
62d80 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 sqlite3WhereBeg
62d90 69 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 in().** and the
62da0 57 68 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 WhereInfo.wctrlF
62db0 6c 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a lags member..*/.
62dc0 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 #define WHERE_OR
62dd0 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 DERBY_NORMAL 0
62de0 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a x0000 /* No-op *
62df0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
62e00 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 ORDERBY_MIN
62e10 20 30 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 0x0001 /* ORDER
62e20 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 BY processing f
62e30 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f or min() func */
62e40 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f .#define WHERE_O
62e50 52 44 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20 RDERBY_MAX
62e60 30 78 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 0x0002 /* ORDER
62e70 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f BY processing fo
62e80 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a r max() func */.
62e90 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e #define WHERE_ON
62ea0 45 50 41 53 53 5f 44 45 53 49 52 45 44 20 20 30 EPASS_DESIRED 0
62eb0 78 30 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f x0004 /* Want to
62ec0 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 do one-pass UPD
62ed0 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 ATE/DELETE */.#d
62ee0 65 66 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c efine WHERE_DUPL
62ef0 49 43 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 ICATES_OK 0x0
62f00 30 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 008 /* Ok to ret
62f10 75 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 urn a row more t
62f20 68 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 han once */.#def
62f30 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f ine WHERE_OMIT_O
62f40 50 45 4e 20 20 20 20 20 20 20 20 30 78 30 30 31 PEN 0x001
62f50 30 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 0 /* Table curso
62f60 72 20 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70 r are already op
62f70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 en */.#define WH
62f80 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 ERE_OMIT_CLOSE
62f90 20 20 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 0x0020 /* O
62fa0 6d 69 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 mit close of tab
62fb0 6c 65 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f le & index curso
62fc0 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 rs */.#define WH
62fd0 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 ERE_FORCE_TABLE
62fe0 20 20 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44 0x0040 /* D
62ff0 6f 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 o not use an ind
63000 65 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a ex-only search *
63010 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 /../*.** The WHE
63020 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 RE clause proces
63030 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 sing routine has
63040 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 two halves. Th
63050 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 e.** first part
63060 64 6f 65 73 20 74 68 65 20 73 74 61 72 74 20 6f does the start o
63070 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 f the WHERE loop
63080 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a and the second.
63090 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 ** half does the
630a0 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 tail of the WHE
630b0 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 RE loop. An ins
630c0 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 tance of.** this
630d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 structure is re
630e0 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 turned by the fi
630f0 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 rst half and pas
63100 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 sed.** into the
63110 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 second half to g
63120 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 ive some continu
63130 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 ity..*/.struct W
63140 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 hereInfo {. Par
63150 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
63160 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 /* Parsing and
63170 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
63180 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 context */. u1
63190 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 6 wctrlFlags;
631a0 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 /* Flags orig
631b0 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f inally passed to
631c0 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 sqlite3WhereBeg
631d0 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f in() */. u8 okO
631e0 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f nePass; /
631f0 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d * Ok to use one-
63200 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 pass algorithm f
63210 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c or UPDATE or DEL
63220 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ETE */. SrcList
63230 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 *pTabList;
63240 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
63250 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 of tables in the
63260 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 join */. int i
63270 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 Top;
63280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
63290 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 very beginning
632a0 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f of the WHERE loo
632b0 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 p */. int iCont
632c0 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 inue;
632d0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
632e0 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 re to continue w
632f0 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20 ith next record
63300 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b */. int iBreak;
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63320 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
63330 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 to break out of
63340 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 the loop */. i
63350 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 nt nLevel;
63360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63370 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
63380 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 d loop */. stru
63390 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a ct WhereClause *
633a0 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 pWC; /* De
633b0 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 composition of t
633c0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
633d0 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 */. WhereLevel
633e0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 a[1];
633f0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
63400 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 on about each ne
63410 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 st loop in WHERE
63420 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
63430 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69 NameContext defi
63440 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e nes a context in
63450 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 which to resolv
63460 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 e table and colu
63470 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 mn.** names. Th
63480 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 e context consis
63490 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 ts of a list of
634a0 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 63 tables (the pSrc
634b0 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a List) field and.
634c0 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d ** a list of nam
634d0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70 ed expression (p
634e0 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d EList). The nam
634f0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 ed expression li
63500 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c st may.** be NUL
63510 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f 72 L. The pSrc cor
63520 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 responds to the
63530 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 FROM clause of a
63540 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f SELECT or.** to
63550 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 the table being
63560 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20 operated on by
63570 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
63580 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a or DELETE. The.
63590 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 ** pEList corres
635a0 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 ponds to the res
635b0 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c ult set of a SEL
635c0 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 ECT and is NULL
635d0 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 for.** other sta
635e0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e tements..**.** N
635f0 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 ameContexts can
63600 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e be nested. When
63610 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 resolving names
63620 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 , the inner-most
63630 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 .** context is
63640 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e 20 searched first.
63650 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 If no match is
63660 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 found, the next
63670 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 outer.** context
63680 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66 is checked. If
63690 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20 there is still
636a0 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 no match, the ne
636b0 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 xt context.** is
636c0 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20 checked. This
636d0 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 process continue
636e0 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 s until either a
636f0 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a match is found.
63700 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 ** or all contex
63710 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57 ts are check. W
63720 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66 hen a match is f
63730 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d ound, the nRef m
63740 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 ember of.** the
63750 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 context containi
63760 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 20 ng the match is
63770 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a incremented. .**
63780 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 72 .** Each subquer
63790 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d y gets a new Nam
637a0 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70 eContext. The p
637b0 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 Next field point
637c0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 s to the.** Name
637d0 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70 Context in the p
637e0 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68 arent query. Th
637f0 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f us the process o
63800 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a f scanning the.*
63810 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 * NameContext li
63820 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 st corresponds t
63830 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f o searching thro
63840 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c 79 ugh successively
63850 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 outer.** subque
63860 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 ries looking for
63870 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 a match..*/.str
63880 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 uct NameContext
63890 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 {. Parse *pPars
638a0 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e; /* The
638b0 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c parser */. SrcL
638c0 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 ist *pSrcList;
638d0 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 /* One or more
638e0 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 tables used to r
638f0 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a esolve names */.
63900 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
63910 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e st; /* Option
63920 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 al list of named
63930 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a expressions */.
63940 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
63950 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
63960 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 of names resolv
63970 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65 ed by this conte
63980 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 xt */. int nErr
63990 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
639a0 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 Number of errors
639b0 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 encountered whi
639c0 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d le resolving nam
639d0 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 es */. u8 allow
639e0 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Agg; /*
639f0 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 Aggregate functi
63a00 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 ons allowed here
63a10 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b */. u8 hasAgg;
63a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
63a30 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65 73 ue if aggregates
63a40 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 are seen */. u
63a50 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20 8 isCheck;
63a60 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 /* True if r
63a70 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 esolving names i
63a80 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 n a CHECK constr
63a90 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 aint */. int nD
63aa0 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f epth; /
63ab0 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 75 * Depth of subqu
63ac0 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 ery recursion. 1
63ad0 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f for no recursio
63ae0 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a n */. AggInfo *
63af0 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 pAggInfo; /* I
63b00 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
63b10 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74 aggregates at t
63b20 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e his level */. N
63b30 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 ameContext *pNex
63b40 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 t; /* Next oute
63b50 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 r name context.
63b60 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d NULL for outerm
63b70 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ost */.};../*.**
63b80 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
63b90 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
63ba0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
63bb0 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e all information
63bc0 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65 .** needed to ge
63bd0 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
63be0 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 a single SELECT
63bf0 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a statement..**.**
63c00 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 nLimit is set t
63c10 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 o -1 if there is
63c20 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 no LIMIT clause
63c30 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 . nOffset is se
63c40 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 t to 0..** If th
63c50 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63 ere is a LIMIT c
63c60 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 65 lause, the parse
63c70 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f r sets nLimit to
63c80 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
63c90 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e e.** limit and n
63ca0 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 61 Offset to the va
63cb0 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65 lue of the offse
63cc0 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 65 t (or 0 if there
63cd0 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 is not.** offse
63ce0 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f t). But later o
63cf0 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f n, nLimit and nO
63d00 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65 ffset become the
63d10 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
63d20 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45 s.** in the VDBE
63d30 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 that record the
63d40 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 limit and offse
63d50 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a t counters..**.*
63d60 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d * addrOpenEphm[]
63d70 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e entries contain
63d80 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
63d90 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
63da0 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 opcodes..** The
63db0 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 73 se addresses mus
63dc0 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74 t be stored so t
63dd0 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61 hat we can go ba
63de0 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a ck and fill in.*
63df0 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f * the P4_KEYINFO
63e00 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65 and P2 paramete
63e10 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 rs later. Neith
63e20 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e er the KeyInfo n
63e30 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 or.** the number
63e40 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 of columns in P
63e50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 2 can be compute
63e60 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 d at the same ti
63e70 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f me.** as the OP_
63e80 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 OpenEphm instruc
63e90 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65 tion is coded be
63ea0 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f cause not.** eno
63eb0 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ugh information
63ec0 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 about the compou
63ed0 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 nd query is know
63ee0 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e n at that point.
63ef0 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 .** The KeyInfo
63f00 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e for addrOpenTran
63f10 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 [0] and [1] cont
63f20 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 ains collating s
63f30 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 equences.** for
63f40 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 the result set.
63f50 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 The KeyInfo for
63f60 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d addrOpenTran[2]
63f70 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 contains collat
63f80 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 ing.** sequences
63f90 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42 for the ORDER B
63fa0 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 Y clause..*/.str
63fb0 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 uct Select {. E
63fc0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
63fd0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65 /* The fie
63fe0 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c lds of the resul
63ff0 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 t */. u8 op;
64000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
64010 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f One of: TK_UNIO
64020 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 N TK_ALL TK_INTE
64030 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20 RSECT TK_EXCEPT
64040 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 */. char affini
64050 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d ty; /* M
64060 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 74 akeRecord with t
64070 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72 his affinity for
64080 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 SRT_Set */. u1
64090 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 6 selFlags;
640a0 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 /* Various
640b0 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 SF_* values */.
640c0 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 SrcList *pSrc;
640d0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 /* The F
640e0 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ROM clause */.
640f0 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 Expr *pWhere;
64100 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 /* The WH
64110 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ERE clause */.
64120 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 ExprList *pGroup
64130 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 By; /* The GR
64140 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f OUP BY clause */
64150 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 . Expr *pHaving
64160 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
64170 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a HAVING clause *
64180 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
64190 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 rderBy; /* Th
641a0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
641b0 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 e */. Select *p
641c0 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a Prior; /*
641d0 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e Prior select in
641e0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 a compound sele
641f0 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a ct statement */.
64200 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b Select *pNext;
64210 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
64220 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c select to the l
64230 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e eft in a compoun
64240 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 d */. Select *p
64250 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a Rightmost; /*
64260 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 Right-most sele
64270 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ct in a compound
64280 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e select statemen
64290 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 t */. Expr *pLi
642a0 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a mit; /*
642b0 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f LIMIT expressio
642c0 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f n. NULL means no
642d0 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 t used. */. Exp
642e0 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20 r *pOffset;
642f0 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 /* OFFSET ex
64300 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d pression. NULL m
64310 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a eans not used. *
64320 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 /. int iLimit,
64330 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 iOffset; /* Me
64340 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 68 mory registers h
64350 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f olding LIMIT & O
64360 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a FFSET counters *
64370 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e /. int addrOpen
64380 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 Ephm[3]; /* OP
64390 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 _OpenEphem opcod
643a0 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 es related to th
643b0 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a is select */.};.
643c0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
643d0 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74 alues for Select
643e0 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 .selFlags. The
643f0 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61 6e "SF" prefix stan
64400 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 ds for.** "Selec
64410 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 t Flag"..*/.#def
64420 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20 ine SF_Distinct
64430 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 0x0001 /
64440 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 * Output should
64450 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 be DISTINCT */.#
64460 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 define SF_Resolv
64470 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30 32 ed 0x0002
64480 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73 /* Identifiers
64490 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
644a0 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ved */.#define S
644b0 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 20 F_Aggregate
644c0 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 0x0004 /* Con
644d0 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 tains aggregate
644e0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 functions */.#de
644f0 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65 fine SF_UsesEphe
64500 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 20 meral 0x0008
64510 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e /* Uses the Open
64520 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 Ephemeral opcode
64530 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 */.#define SF_E
64540 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30 xpanded 0
64550 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 x0010 /* sqlite
64560 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 3SelectExpand()
64570 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a called on this *
64580 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73 /.#define SF_Has
64590 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30 TypeInfo 0x0
645a0 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 020 /* FROM sub
645b0 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61 62 queries have Tab
645c0 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a le metadata */..
645d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c ./*.** The resul
645e0 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 ts of a select c
645f0 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65 an be distribute
64600 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 d in several way
64610 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 s. The.** "SRT"
64620 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53 prefix means "S
64630 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 70 ELECT Result Typ
64640 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 e"..*/.#define S
64650 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 RT_Union
64660 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 1 /* Store resu
64670 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e lt as keys in an
64680 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e index */.#defin
64690 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 e SRT_Except
646a0 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 2 /* Remove
646b0 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e result from a UN
646c0 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 ION index */.#de
646d0 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 fine SRT_Exists
646e0 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 3 /* Stor
646f0 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c e 1 if the resul
64700 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a t is not empty *
64710 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 /.#define SRT_Di
64720 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a scard 4 /*
64730 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 Do not save the
64740 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 results anywher
64750 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 e */../* The ORD
64760 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 ER BY clause is
64770 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 ignored for all
64780 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a of the above */.
64790 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c #define Ignorabl
647a0 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 2d eOrderby(X) ((X-
647b0 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 >eDest)<=SRT_Dis
647c0 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 card)..#define S
647d0 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20 RT_Output
647e0 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 5 /* Output eac
647f0 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 h row of result
64800 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d */.#define SRT_M
64810 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 2f em 6 /
64820 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 * Store result i
64830 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 n a memory cell
64840 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 */.#define SRT_S
64850 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20 2f et 7 /
64860 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 * Store results
64870 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e as keys in an in
64880 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dex */.#define S
64890 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 RT_Table
648a0 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 8 /* Store resu
648b0 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68 20 lt as data with
648c0 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 an automatic row
648d0 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 id */.#define SR
648e0 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 T_EphemTab 9
648f0 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e /* Create tran
64900 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 sient tab and st
64910 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 ore like SRT_Tab
64920 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 le */.#define SR
64930 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 T_Coroutine 10
64940 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 /* Generate a
64950 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 single row of re
64960 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 sult */../*.** A
64970 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 structure used
64980 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 to customize the
64990 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c behavior of sql
649a0 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65 ite3Select(). Se
649b0 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 e.** comments ab
649c0 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ove sqlite3Selec
649d0 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e t() for details.
649e0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
649f0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 53 65 ct SelectDest Se
64a00 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74 lectDest;.struct
64a10 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 SelectDest {.
64a20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 20 20 u8 eDest;
64a30 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 /* How to disp
64a40 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c ose of the resul
64a50 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e ts */. u8 affin
64a60 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 ity; /* Aff
64a70 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 inity used when
64a80 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a eDest==SRT_Set *
64a90 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 /. int iParm;
64aa0 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d /* A param
64ab0 65 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65 eter used by the
64ac0 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 eDest disposal
64ad0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 method */. int
64ae0 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a iMem; /*
64af0 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77 Base register w
64b00 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 here results are
64b10 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
64b20 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t nMem;
64b30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 /* Number of reg
64b40 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 isters allocated
64b50 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 */.};../*.** Si
64b60 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ze of the column
64b70 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 cache.*/.#ifnde
64b80 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 f SQLITE_N_COLCA
64b90 43 48 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c CHE.# define SQL
64ba0 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 20 31 ITE_N_COLCACHE 1
64bb0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
64bc0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
64bd0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
64be0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
64bf0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
64c00 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
64c10 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
64c20 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
64c30 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
64c40 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
64c50 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
64c60 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
64c70 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
64c80 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
64c90 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
64ca0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
64cb0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
64cc0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
64cd0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
64ce0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
64cf0 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
64d00 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
64d10 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
64d20 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
64d30 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
64d40 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
64d50 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
64d60 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
64d70 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
64d80 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
64d90 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
64da0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
64db0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
64dc0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
64dd0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
64de0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
64df0 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
64e00 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
64e10 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
64e20 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
64e30 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
64e40 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
64e50 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
64e60 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
64e70 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
64e80 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
64e90 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
64ea0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
64eb0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
64ec0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
64ed0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
64ee0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
64ef0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
64f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
64f10 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
64f20 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
64f30 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
64f40 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
64f50 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
64f60 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
64f70 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
64f80 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
64f90 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
64fa0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
64fb0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
64fc0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
64fd0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
64fe0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
64ff0 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
65000 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
65010 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
65020 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
65030 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
65040 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
65050 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
65060 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
65070 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
65080 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
65090 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
650a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
650b0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
650c0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
650d0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
650e0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
650f0 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
65100 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
65110 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
65120 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
65130 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65140 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
65150 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
65160 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
65170 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
65180 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
65190 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
651a0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
651b0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
651c0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
651d0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
651e0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
651f0 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
65200 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
65210 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
65220 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
65230 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
65240 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
65250 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
65260 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
65270 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
65280 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
65290 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
652a0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
652b0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
652c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
652d0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
652e0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
652f0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
65300 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
65310 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
65320 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
65330 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
65340 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
65350 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
65360 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
65370 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
65380 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
65390 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
653a0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
653b0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
653c0 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20 t iCacheLevel;
653d0 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76 /* ColCache v
653e0 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61 alid when aColCa
653f0 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43 che[].iLevel<=iC
65400 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 acheLevel */. i
65410 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20 nt iCacheCnt;
65420 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75 /* Counter u
65430 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
65440 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20 aColCache[].lru
65450 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e values */. u8 n
65460 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 ColCache;
65470 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
65480 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c tries in the col
65490 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 umn cache */. u
654a0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 8 iColCache;
654b0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 /* Next entr
654c0 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74 y of the cache t
654d0 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73 o replace */. s
654e0 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
654f0 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 {. int iTable
65500 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
65510 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 able cursor numb
65520 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 er */. int iC
65530 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 olumn;
65540 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 /* Table column
65550 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38 number */. u8
65560 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20 affChange;
65570 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
65580 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73 his register has
65590 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79 had an affinity
655a0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75 change */. u
655b0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20 8 tempReg;
655c0 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20 /* iReg is
655d0 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20 a temp register
655e0 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 that needs to be
655f0 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e freed */. in
65600 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 t iLevel;
65610 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c /* Nesting l
65620 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 evel */. int
65630 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 iReg;
65640 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61 /* Reg with va
65650 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 lue of this colu
65660 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 mn. 0 means none
65670 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75 . */. int lru
65680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
65690 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 * Least recently
656a0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20 used entry has
656b0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c the smallest val
656c0 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 ue */. } aColCa
656d0 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c che[SQLITE_N_COL
656e0 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 CACHE]; /* One
656f0 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 for each column
65700 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 cache entry */.
65710 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20 u32 writeMask;
65720 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61 /* Start a
65730 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
65740 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61 on on these data
65750 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63 bases */. u32 c
65760 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20 ookieMask;
65770 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63 /* Bitmask of sc
65780 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61 hema verified da
65790 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 69 6e 74 tabases */. int
657a0 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 cookieGoto;
657b0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
657c0 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 OP_Goto to cooki
657d0 65 20 76 65 72 69 66 69 65 72 20 73 75 62 72 6f e verifier subro
657e0 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 utine */. int c
657f0 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 ookieValue[SQLIT
65800 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 E_MAX_ATTACHED+2
65810 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 ]; /* Values of
65820 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 cookies to veri
65830 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 fy */.#ifndef SQ
65840 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
65850 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 _CACHE. int nTa
65860 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 bleLock;
65870 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 /* Number of loc
65880 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b ks in aTableLock
65890 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 */. TableLock
658a0 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 *aTableLock; /*
658b0 52 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 6c Required table l
658c0 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64 2d ocks for shared-
658d0 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 cache mode */.#e
658e0 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 67 52 6f ndif. int regRo
658f0 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 wid; /* R
65900 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 egister holding
65910 72 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 20 rowid of CREATE
65920 54 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 TABLE entry */.
65930 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 int regRoot;
65940 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
65950 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 r holding root p
65960 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e age number for n
65970 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 0a 20 ew objects */..
65980 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 6f 6e /* Above is con
65990 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20 72 65 stant between re
659a0 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 cursions. Below
659b0 20 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65 is reset before
659c0 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 and after. **
659d0 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e 20 2a each recursion *
659e0 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 /.. int nVar;
659f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
65a00 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72 69 61 ber of '?' varia
65a10 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74 68 65 bles seen in the
65a20 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f 0a 20 SQL so far */.
65a30 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b 20 20 int nVarExpr;
65a40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65a50 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20 69 6e of used slots in
65a60 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a apVarExpr[] */.
65a70 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 41 6c int nVarExprAl
65a80 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 loc; /* Number
65a90 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c of allocated sl
65aa0 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 ots in apVarExpr
65ab0 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 61 [] */. Expr **a
65ac0 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f 2a 20 pVarExpr; /*
65ad0 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61 61 61 Pointers to :aaa
65ae0 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c 64 63 and $aaaa wildc
65af0 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ard expressions
65b00 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61 73 3b */. int nAlias;
65b10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
65b20 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 20 72 ber of aliased r
65b30 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e esult set column
65b40 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61 s */. int nAlia
65b50 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 4e sAlloc; /* N
65b60 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
65b70 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61 41 6c ed slots for aAl
65b80 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a ias[] */. int *
65b90 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 aAlias;
65ba0 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 /* Register used
65bb0 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73 65 64 to hold aliased
65bc0 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 result */. u8
65bd0 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 explain;
65be0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
65bf0 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 69 73 EXPLAIN flag is
65c00 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 71 75 found on the qu
65c10 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 ery */. Token s
65c20 45 72 72 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a ErrToken; /*
65c30 20 54 68 65 20 74 6f 6b 65 6e 20 61 74 20 77 68 The token at wh
65c40 69 63 68 20 74 68 65 20 65 72 72 6f 72 20 6f 63 ich the error oc
65c50 63 75 72 72 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 curred */. Toke
65c60 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 n sNameToken;
65c70 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 /* Token with u
65c80 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d nqualified schem
65c90 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f a object name */
65ca0 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f . Token sLastTo
65cb0 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c ken; /* The l
65cc0 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 ast token parsed
65cd0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
65ce0 20 2a 7a 53 71 6c 3b 20 20 20 20 2f 2a 20 41 6c *zSql; /* Al
65cf0 6c 20 53 51 4c 20 74 65 78 74 20 2a 2f 0a 20 20 l SQL text */.
65d00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69 const char *zTai
65d10 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 l; /* All SQL
65d20 74 65 78 74 20 70 61 73 74 20 74 68 65 20 6c 61 text past the la
65d30 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 70 61 72 st semicolon par
65d40 73 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a sed */. Table *
65d50 70 4e 65 77 54 61 62 6c 65 3b 20 20 20 20 2f 2a pNewTable; /*
65d60 20 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 A table being c
65d70 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 43 52 onstructed by CR
65d80 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 EATE TABLE */.
65d90 54 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72 69 Trigger *pNewTri
65da0 67 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72 69 gger; /* Tri
65db0 67 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73 74 gger under const
65dc0 72 75 63 74 20 62 79 20 61 20 43 52 45 41 54 45 ruct by a CREATE
65dd0 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 54 72 TRIGGER */. Tr
65de0 69 67 67 65 72 53 74 61 63 6b 20 2a 74 72 69 67 iggerStack *trig
65df0 53 74 61 63 6b 3b 20 20 2f 2a 20 54 72 69 67 67 Stack; /* Trigg
65e00 65 72 20 61 63 74 69 6f 6e 73 20 62 65 69 6e 67 er actions being
65e10 20 63 6f 64 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 coded */. cons
65e20 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e t char *zAuthCon
65e30 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 text; /* The 6th
65e40 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64 62 parameter to db
65e50 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63 6b ->xAuth callback
65e60 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c s */.#ifndef SQL
65e70 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
65e80 54 41 42 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 TABLE. Token sA
65e90 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
65ea0 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 /* Complete t
65eb0 65 78 74 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 ext of a module
65ec0 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 argument */. u8
65ed0 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20 20 declareVtab;
65ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
65ef0 20 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69 74 if inside sqlit
65f00 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 e3_declare_vtab(
65f10 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 ) */. int nVtab
65f20 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Lock;
65f30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 /* Number of v
65f40 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f irtual tables to
65f50 20 6c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 lock */. Table
65f60 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 **apVtabLock;
65f70 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
65f80 20 74 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c to virtual tabl
65f90 65 73 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 es needing locki
65fa0 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 ng */.#endif. i
65fb0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20 nt nHeight;
65fc0 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 /* Expres
65fd0 73 69 6f 6e 20 74 72 65 65 20 68 65 69 67 68 74 sion tree height
65fe0 20 6f 66 20 63 75 72 72 65 6e 74 20 73 75 62 2d of current sub-
65ff0 73 65 6c 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c select */. Tabl
66000 65 20 2a 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 e *pZombieTab;
66010 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 /* List of T
66020 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 able objects to
66030 64 65 6c 65 74 65 20 61 66 74 65 72 20 63 6f 64 delete after cod
66040 65 20 67 65 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 e gen */.};..#if
66050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
66060 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 VIRTUALTABLE. #
66070 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 define IN_DECLAR
66080 45 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 E_VTAB 0.#else.
66090 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c #define IN_DECL
660a0 41 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 ARE_VTAB (pParse
660b0 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 ->declareVtab).#
660c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 endif../*.** An
660d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
660e0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
660f0 75 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 ure can be decla
66100 72 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 red on a stack a
66110 6e 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 nd used.** to sa
66120 76 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 ve the Parse.zAu
66130 74 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 thContext value
66140 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 so that it can b
66150 65 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 e restored later
66160 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 ..*/.struct Auth
66170 43 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 Context {. cons
66180 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e t char *zAuthCon
66190 74 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 text; /* Put s
661a0 61 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 aved Parse.zAuth
661b0 43 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a Context here */.
661c0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
661d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
661e0 20 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 The Parse struc
661f0 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ture */.};../*.*
66200 2a 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 * Bitfield flags
66210 20 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e for P5 value in
66220 20 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f OP_Insert and O
66230 50 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 P_Delete.*/.#def
66240 69 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e ine OPFLAG_NCHAN
66250 47 45 20 20 20 20 31 20 20 20 20 2f 2a 20 53 65 GE 1 /* Se
66260 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e t to update db->
66270 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 nChange */.#defi
66280 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f ne OPFLAG_LASTRO
66290 57 49 44 20 20 32 20 20 20 20 2f 2a 20 53 65 74 WID 2 /* Set
662a0 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6c to update db->l
662b0 61 73 74 52 6f 77 69 64 20 2a 2f 0a 23 64 65 66 astRowid */.#def
662c0 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 ine OPFLAG_ISUPD
662d0 41 54 45 20 20 20 34 20 20 20 20 2f 2a 20 54 68 ATE 4 /* Th
662e0 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 is OP_Insert is
662f0 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f an sql UPDATE */
66300 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
66310 41 50 50 45 4e 44 20 20 20 20 20 38 20 20 20 20 APPEND 8
66320 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 6c /* This is likel
66330 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e y to be an appen
66340 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 d */.#define OPF
66350 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c LAG_USESEEKRESUL
66360 54 20 31 36 20 20 20 20 2f 2a 20 54 72 79 20 74 T 16 /* Try t
66370 6f 20 61 76 6f 69 64 20 61 20 73 65 65 6b 20 69 o avoid a seek i
66380 6e 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 20 n BtreeInsert()
66390 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68 20 74 */../*. * Each t
663a0 72 69 67 67 65 72 20 70 72 65 73 65 6e 74 20 69 rigger present i
663b0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 n the database s
663c0 63 68 65 6d 61 20 69 73 20 73 74 6f 72 65 64 20 chema is stored
663d0 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f as an instance o
663e0 66 0a 20 2a 20 73 74 72 75 63 74 20 54 72 69 67 f. * struct Trig
663f0 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e ger. . *. * Poin
66400 74 65 72 73 20 74 6f 20 69 6e 73 74 61 6e 63 65 ters to instance
66410 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 s of struct Trig
66420 67 65 72 20 61 72 65 20 73 74 6f 72 65 64 20 69 ger are stored i
66430 6e 20 74 77 6f 20 77 61 79 73 2e 0a 20 2a 20 31 n two ways.. * 1
66440 2e 20 49 6e 20 74 68 65 20 22 74 72 69 67 48 61 . In the "trigHa
66450 73 68 22 20 68 61 73 68 20 74 61 62 6c 65 20 28 sh" hash table (
66460 70 61 72 74 20 6f 66 20 74 68 65 20 73 71 6c 69 part of the sqli
66470 74 65 33 2a 20 74 68 61 74 20 72 65 70 72 65 73 te3* that repres
66480 65 6e 74 73 20 74 68 65 20 0a 20 2a 20 20 20 20 ents the . *
66490 64 61 74 61 62 61 73 65 29 2e 20 54 68 69 73 20 database). This
664a0 61 6c 6c 6f 77 73 20 54 72 69 67 67 65 72 20 73 allows Trigger s
664b0 74 72 75 63 74 75 72 65 73 20 74 6f 20 62 65 20 tructures to be
664c0 72 65 74 72 69 65 76 65 64 20 62 79 20 6e 61 6d retrieved by nam
664d0 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 72 69 e.. * 2. All tri
664e0 67 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 ggers associated
664f0 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 with a single t
66500 61 62 6c 65 20 66 6f 72 6d 20 61 20 6c 69 6e 6b able form a link
66510 65 64 20 6c 69 73 74 2c 20 75 73 69 6e 67 20 74 ed list, using t
66520 68 65 0a 20 2a 20 20 20 20 70 4e 65 78 74 20 6d he. * pNext m
66530 65 6d 62 65 72 20 6f 66 20 73 74 72 75 63 74 20 ember of struct
66540 54 72 69 67 67 65 72 2e 20 41 20 70 6f 69 6e 74 Trigger. A point
66550 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 er to the first
66560 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 20 element of the.
66570 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c 69 73 74 * linked list
66580 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74 68 is stored as th
66590 65 20 22 70 54 72 69 67 67 65 72 22 20 6d 65 6d e "pTrigger" mem
665a0 62 65 72 20 6f 66 20 74 68 65 20 61 73 73 6f 63 ber of the assoc
665b0 69 61 74 65 64 0a 20 2a 20 20 20 20 73 74 72 75 iated. * stru
665c0 63 74 20 54 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20 ct Table.. *. *
665d0 54 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 The "step_list"
665e0 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f member points to
665f0 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 the first eleme
66600 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c nt of a linked l
66610 69 73 74 0a 20 2a 20 63 6f 6e 74 61 69 6e 69 6e ist. * containin
66620 67 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d g the SQL statem
66630 65 6e 74 73 20 73 70 65 63 69 66 69 65 64 20 61 ents specified a
66640 73 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 s the trigger pr
66650 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63 ogram.. */.struc
66660 74 20 54 72 69 67 67 65 72 20 7b 0a 20 20 63 68 t Trigger {. ch
66670 61 72 20 2a 6e 61 6d 65 3b 20 20 20 20 20 20 20 ar *name;
66680 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d /* The nam
66690 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 e of the trigger
666a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
666b0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 63 68 61 */. cha
666c0 72 20 2a 74 61 62 6c 65 3b 20 20 20 20 20 20 20 r *table;
666d0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c /* The tabl
666e0 65 20 6f 72 20 76 69 65 77 20 74 6f 20 77 68 69 e or view to whi
666f0 63 68 20 74 68 65 20 74 72 69 67 67 65 72 20 61 ch the trigger a
66700 70 70 6c 69 65 73 20 2a 2f 0a 20 20 75 38 20 6f pplies */. u8 o
66710 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
66720 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b /* One of TK
66730 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 _DELETE, TK_UPDA
66740 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 TE, TK_INSERT
66750 20 20 20 20 20 20 2a 2f 0a 20 20 75 38 20 74 72 */. u8 tr
66760 5f 74 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 _tm;
66770 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 /* One of TRI
66780 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 GGER_BEFORE, TRI
66790 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 GGER_AFTER */.
667a0 45 78 70 72 20 2a 70 57 68 65 6e 3b 20 20 20 20 Expr *pWhen;
667b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 /* The W
667c0 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 68 HEN clause of th
667d0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28 6d 61 e expression (ma
667e0 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 y be NULL) */.
667f0 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 IdList *pColumns
66800 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 ; /* If th
66810 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 is is an UPDATE
66820 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e OF <column-list>
66830 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 20 20 trigger,.
66840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66850 20 20 20 20 20 20 20 74 68 65 20 3c 63 6f 6c 75 the <colu
66860 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 73 74 6f 72 mn-list> is stor
66870 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 54 6f 6b ed here */. Tok
66880 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 en nameToken;
66890 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 63 6f /* Token co
668a0 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 6d 65 2e 20 ntaining zName.
668b0 55 73 65 20 64 75 72 69 6e 67 20 70 61 72 73 69 Use during parsi
668c0 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 20 53 63 68 ng only */. Sch
668d0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 ema *pSchema;
668e0 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 /* Schema c
668f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 ontaining the tr
66900 69 67 67 65 72 20 2a 2f 0a 20 20 53 63 68 65 6d igger */. Schem
66910 61 20 2a 70 54 61 62 53 63 68 65 6d 61 3b 20 20 a *pTabSchema;
66920 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e /* Schema con
66930 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c taining the tabl
66940 65 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 e */. TriggerSt
66950 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b 20 2f ep *step_list; /
66960 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 20 74 * Link list of t
66970 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73 rigger program s
66980 74 65 70 73 20 20 20 20 20 20 20 20 20 20 20 20 teps
66990 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 */. Trigger *p
669a0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Next; /*
669b0 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 61 73 Next trigger as
669c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
669d0 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f e table */.};../
669e0 2a 0a 2a 2a 20 41 20 74 72 69 67 67 65 72 20 69 *.** A trigger i
669f0 73 20 65 69 74 68 65 72 20 61 20 42 45 46 4f 52 s either a BEFOR
66a00 45 20 6f 72 20 61 6e 20 41 46 54 45 52 20 74 72 E or an AFTER tr
66a10 69 67 67 65 72 2e 20 20 54 68 65 20 66 6f 6c 6c igger. The foll
66a20 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 73 0a owing constants.
66a30 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 ** determine whi
66a40 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ch. .**.** If th
66a50 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65 ere are multiple
66a60 20 74 72 69 67 67 65 72 73 2c 20 79 6f 75 20 6d triggers, you m
66a70 69 67 68 74 20 6f 66 20 73 6f 6d 65 20 42 45 46 ight of some BEF
66a80 4f 52 45 20 61 6e 64 20 73 6f 6d 65 20 41 46 54 ORE and some AFT
66a90 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 ER..** In that c
66aa0 61 73 65 73 2c 20 74 68 65 20 63 6f 6e 73 74 61 ases, the consta
66ab0 6e 74 73 20 62 65 6c 6f 77 20 63 61 6e 20 62 65 nts below can be
66ac0 20 4f 52 65 64 20 74 6f 67 65 74 68 65 72 2e 0a ORed together..
66ad0 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 */.#define TRIGG
66ae0 45 52 5f 42 45 46 4f 52 45 20 20 31 0a 23 64 65 ER_BEFORE 1.#de
66af0 66 69 6e 65 20 54 52 49 47 47 45 52 5f 41 46 54 fine TRIGGER_AFT
66b00 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e ER 2../*. * An
66b10 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 instance of str
66b20 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
66b30 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 is used to store
66b40 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 a single SQL st
66b50 61 74 65 6d 65 6e 74 0a 20 2a 20 74 68 61 74 20 atement. * that
66b60 69 73 20 61 20 70 61 72 74 20 6f 66 20 61 20 74 is a part of a t
66b70 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 20 rigger-program.
66b80 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 6e 63 65 73 . *. * Instances
66b90 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 of struct Trigg
66ba0 65 72 53 74 65 70 20 61 72 65 20 73 74 6f 72 65 erStep are store
66bb0 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20 6c 69 d in a singly li
66bc0 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e 6b 65 nked list (linke
66bd0 64 0a 20 2a 20 75 73 69 6e 67 20 74 68 65 20 22 d. * using the "
66be0 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 29 20 72 pNext" member) r
66bf0 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 eferenced by the
66c00 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d "step_list" mem
66c10 62 65 72 20 6f 66 20 74 68 65 20 0a 20 2a 20 61 ber of the . * a
66c20 73 73 6f 63 69 61 74 65 64 20 73 74 72 75 63 74 ssociated struct
66c30 20 54 72 69 67 67 65 72 20 69 6e 73 74 61 6e 63 Trigger instanc
66c40 65 2e 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 e. The first ele
66c50 6d 65 6e 74 20 6f 66 20 74 68 65 20 6c 69 6e 6b ment of the link
66c60 65 64 20 6c 69 73 74 20 69 73 0a 20 2a 20 74 68 ed list is. * th
66c70 65 20 66 69 72 73 74 20 73 74 65 70 20 6f 66 20 e first step of
66c80 74 68 65 20 74 72 69 67 67 65 72 2d 70 72 6f 67 the trigger-prog
66c90 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68 65 20 ram.. * . * The
66ca0 22 6f 70 22 20 6d 65 6d 62 65 72 20 69 6e 64 69 "op" member indi
66cb0 63 61 74 65 73 20 77 68 65 74 68 65 72 20 74 68 cates whether th
66cc0 69 73 20 69 73 20 61 20 22 44 45 4c 45 54 45 22 is is a "DELETE"
66cd0 2c 20 22 49 4e 53 45 52 54 22 2c 20 22 55 50 44 , "INSERT", "UPD
66ce0 41 54 45 22 20 6f 72 0a 20 2a 20 22 53 45 4c 45 ATE" or. * "SELE
66cf0 43 54 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 CT" statement. T
66d00 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 he meanings of t
66d10 68 65 20 6f 74 68 65 72 20 6d 65 6d 62 65 72 73 he other members
66d20 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
66d30 79 20 74 68 65 20 0a 20 2a 20 76 61 6c 75 65 20 y the . * value
66d40 6f 66 20 22 6f 70 22 20 61 73 20 66 6f 6c 6c 6f of "op" as follo
66d50 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d ws:. *. * (op ==
66d60 20 54 4b 5f 49 4e 53 45 52 54 29 0a 20 2a 20 6f TK_INSERT). * o
66d70 72 63 6f 6e 66 20 20 20 20 2d 3e 20 73 74 6f 72 rconf -> stor
66d80 65 73 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 es the ON CONFLI
66d90 43 54 20 61 6c 67 6f 72 69 74 68 6d 0a 20 2a 20 CT algorithm. *
66da0 70 53 65 6c 65 63 74 20 20 20 2d 3e 20 49 66 20 pSelect -> If
66db0 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 this is an INSER
66dc0 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 T INTO ... SELEC
66dd0 54 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c T ... statement,
66de0 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 then. *
66df0 20 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 this store
66e00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
66e10 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
66e20 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65 20 4e ent. Otherwise N
66e30 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 65 74 20 20 ULL.. * target
66e40 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c -> A token hol
66e50 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 ding the name of
66e60 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e the table to in
66e70 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 sert into.. * pE
66e80 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68 xprList -> If th
66e90 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 is is an INSERT
66ea0 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20 INTO ... VALUES
66eb0 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 ... statement, t
66ec0 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 hen. *
66ed0 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 this stores
66ee0 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 values to be ins
66ef0 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 erted. Otherwise
66f00 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 NULL.. * pIdLis
66f10 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 t -> If this i
66f20 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
66f30 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 ... (<column-na
66f40 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e mes>) VALUES ...
66f50 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 . *
66f60 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 statement, the
66f70 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 68 n this stores th
66f80 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 e column-names t
66f90 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20 o be. *
66fa0 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e inserted in
66fb0 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d to.. *. * (op ==
66fc0 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74 TK_DELETE). * t
66fd0 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f arget -> A to
66fe0 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ken holding the
66ff0 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
67000 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d e to delete from
67010 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d .. * pWhere -
67020 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 > The WHERE clau
67030 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45 54 45 se of the DELETE
67040 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e statement if on
67050 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a e is specified..
67060 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
67070 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a Otherwise NULL..
67080 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b * . * (op == TK
67090 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61 72 67 _UPDATE). * targ
670a0 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e et -> A token
670b0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d holding the nam
670c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
670d0 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 o update rows of
670e0 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d .. * pWhere -
670f0 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 > The WHERE clau
67100 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 se of the UPDATE
67110 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e statement if on
67120 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a e is specified..
67130 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
67140 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a Otherwise NULL..
67150 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 * pExprList ->
67160 41 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f A list of the co
67170 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 lumns to update
67180 61 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 and the expressi
67190 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a ons to update. *
671a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
671b0 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 em to. See sqlit
671c0 65 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d e3Update() docum
671d0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 entation of "pCh
671e0 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 anges". *
671f0 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e argument.
67200 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 . * . */.struct
67210 54 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 TriggerStep {.
67220 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 int op;
67230 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 /* One of T
67240 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 K_DELETE, TK_UPD
67250 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 ATE, TK_INSERT,
67260 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 TK_SELECT */. i
67270 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 nt orconf;
67280 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 /* OE_Rollba
67290 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 ck etc. */. Tri
672a0 67 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 gger *pTrig;
672b0 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 /* The trigger
672c0 20 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 that this step
672d0 69 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a is a part of */.
672e0 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
672f0 63 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 ct; /* Valid
67300 20 66 6f 72 20 53 45 4c 45 43 54 20 61 6e 64 20 for SELECT and
67310 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 20 sometimes .
67320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67330 20 20 20 20 20 49 4e 53 45 52 54 20 73 74 65 70 INSERT step
67340 73 20 28 77 68 65 6e 20 70 45 78 70 72 4c 69 73 s (when pExprLis
67350 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 20 54 6f 6b t == 0) */. Tok
67360 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 en target;
67370 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 /* Valid for D
67380 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c 20 49 ELETE, UPDATE, I
67390 4e 53 45 52 54 20 73 74 65 70 73 20 2a 2f 0a 20 NSERT steps */.
673a0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 Expr *pWhere;
673b0 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 /* Valid f
673c0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54 or DELETE, UPDAT
673d0 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 E steps */. Exp
673e0 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74 rList *pExprList
673f0 3b 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 55 ; /* Valid for U
67400 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 PDATE statements
67410 20 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a and sometimes .
67420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67430 20 20 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 INSER
67440 54 20 73 74 65 70 73 20 28 77 68 65 6e 20 70 53 T steps (when pS
67450 65 6c 65 63 74 20 3d 3d 20 30 29 20 20 20 20 20 elect == 0)
67460 20 20 20 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 */. IdList
67470 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a *pIdList; /*
67480 20 56 61 6c 69 64 20 66 6f 72 20 49 4e 53 45 52 Valid for INSER
67490 54 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c T statements onl
674a0 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 y */. TriggerSt
674b0 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e ep *pNext; /* N
674c0 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e 6b 2d ext in the link-
674d0 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 list */. Trigge
674e0 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 20 2f rStep *pLast; /
674f0 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69 * Last element i
67500 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c n link-list. Val
67510 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 6d 20 id for 1st elem
67520 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 20 only */.};../*.
67530 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
67540 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 struct TriggerS
67550 74 61 63 6b 20 73 74 6f 72 65 73 20 69 6e 66 6f tack stores info
67560 72 6d 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 rmation required
67570 20 64 75 72 69 6e 67 20 63 6f 64 65 0a 20 2a 20 during code. *
67580 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 61 20 generation of a
67590 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 70 single trigger p
675a0 72 6f 67 72 61 6d 2e 20 57 68 69 6c 65 20 74 68 rogram. While th
675b0 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 e trigger progra
675c0 6d 20 69 73 20 62 65 69 6e 67 0a 20 2a 20 63 6f m is being. * co
675d0 64 65 64 2c 20 69 74 73 20 61 73 73 6f 63 69 61 ded, its associa
675e0 74 65 64 20 54 72 69 67 67 65 72 53 74 61 63 6b ted TriggerStack
675f0 20 69 6e 73 74 61 6e 63 65 20 69 73 20 70 6f 69 instance is poi
67600 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 0a 20 nted to by the.
67610 2a 20 22 70 54 72 69 67 67 65 72 53 74 61 63 6b * "pTriggerStack
67620 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 " member of the
67630 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e Parse structure.
67640 0a 20 2a 0a 20 2a 20 54 68 65 20 70 54 61 62 20 . *. * The pTab
67650 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f member points to
67660 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
67670 74 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 triggers are bei
67680 6e 67 20 63 6f 64 65 64 20 6f 6e 2e 20 54 68 65 ng coded on. The
67690 20 0a 20 2a 20 6e 65 77 49 64 78 20 6d 65 6d 62 . * newIdx memb
676a0 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 er contains the
676b0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 64 62 index of the vdb
676c0 65 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 6f e cursor that po
676d0 69 6e 74 73 20 61 74 20 74 68 65 20 74 65 6d 70 ints at the temp
676e0 0a 20 2a 20 74 61 62 6c 65 20 74 68 61 74 20 73 . * table that s
676f0 74 6f 72 65 73 20 74 68 65 20 6e 65 77 2e 2a 20 tores the new.*
67700 72 65 66 65 72 65 6e 63 65 73 2e 20 49 66 20 6e references. If n
67710 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 20 ew.* references
67720 61 72 65 20 6e 6f 74 20 76 61 6c 69 64 0a 20 2a are not valid. *
67730 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65 72 for the trigger
67740 20 62 65 69 6e 67 20 63 6f 64 65 64 20 28 66 6f being coded (fo
67750 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 4f 4e 20 r example an ON
67760 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 29 2c DELETE trigger),
67770 20 74 68 65 6e 20 6e 65 77 49 64 78 0a 20 2a 20 then newIdx. *
67780 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20 54 68 is set to -1. Th
67790 65 20 6f 6c 64 49 64 78 20 6d 65 6d 62 65 72 20 e oldIdx member
677a0 69 73 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 is analogous to
677b0 6e 65 77 49 64 78 2c 20 66 6f 72 20 6f 6c 64 2e newIdx, for old.
677c0 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 20 2a * references.. *
677d0 0a 20 2a 20 54 68 65 20 4f 4e 20 43 4f 4e 46 4c . * The ON CONFL
677e0 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 62 65 ICT policy to be
677f0 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 74 72 used for the tr
67800 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73 74 igger program st
67810 65 70 73 20 69 73 20 73 74 6f 72 65 64 20 0a 20 eps is stored .
67820 2a 20 61 73 20 74 68 65 20 6f 72 63 6f 6e 66 20 * as the orconf
67830 6d 65 6d 62 65 72 2e 20 49 66 20 74 68 69 73 20 member. If this
67840 69 73 20 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 is OE_Default, t
67850 68 65 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c hen the ON CONFL
67860 49 43 54 20 63 6c 61 75 73 65 20 0a 20 2a 20 73 ICT clause . * s
67870 70 65 63 69 66 69 65 64 20 66 6f 72 20 69 6e 64 pecified for ind
67880 69 76 69 64 75 61 6c 20 74 72 69 67 67 65 72 73 ividual triggers
67890 20 73 74 65 70 73 20 69 73 20 75 73 65 64 2e 0a steps is used..
678a0 20 2a 0a 20 2a 20 73 74 72 75 63 74 20 54 72 69 *. * struct Tri
678b0 67 67 65 72 53 74 61 63 6b 20 68 61 73 20 61 20 ggerStack has a
678c0 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 2c 20 "pNext" member,
678d0 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e 6b 65 64 20 to allow linked
678e0 6c 69 73 74 73 20 74 6f 20 62 65 0a 20 2a 20 63 lists to be. * c
678f0 6f 6e 73 74 72 75 63 74 65 64 2e 20 57 68 65 6e onstructed. When
67900 20 63 6f 64 69 6e 67 20 6e 65 73 74 65 64 20 74 coding nested t
67910 72 69 67 67 65 72 73 20 28 74 72 69 67 67 65 72 riggers (trigger
67920 73 20 66 69 72 65 64 20 62 79 20 6f 74 68 65 72 s fired by other
67930 20 74 72 69 67 67 65 72 73 29 0a 20 2a 20 65 61 triggers). * ea
67940 63 68 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 ch nested trigge
67950 72 20 73 74 6f 72 65 73 20 69 74 73 20 70 61 72 r stores its par
67960 65 6e 74 20 74 72 69 67 67 65 72 27 73 20 54 72 ent trigger's Tr
67970 69 67 67 65 72 53 74 61 63 6b 20 61 73 20 74 68 iggerStack as th
67980 65 20 22 70 4e 65 78 74 22 20 0a 20 2a 20 70 6f e "pNext" . * po
67990 69 6e 74 65 72 2e 20 4f 6e 63 65 20 74 68 65 20 inter. Once the
679a0 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 68 nested trigger h
679b0 61 73 20 62 65 65 6e 20 63 6f 64 65 64 2c 20 74 as been coded, t
679c0 68 65 20 70 4e 65 78 74 20 76 61 6c 75 65 20 69 he pNext value i
679d0 73 20 72 65 73 74 6f 72 65 64 0a 20 2a 20 74 6f s restored. * to
679e0 20 74 68 65 20 70 54 72 69 67 67 65 72 53 74 61 the pTriggerSta
679f0 63 6b 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 ck member of the
67a00 20 50 61 72 73 65 20 73 74 75 63 74 75 72 65 20 Parse stucture
67a10 61 6e 64 20 63 6f 64 69 6e 67 20 6f 66 20 74 68 and coding of th
67a20 65 20 70 61 72 65 6e 74 0a 20 2a 20 74 72 69 67 e parent. * trig
67a30 67 65 72 20 63 6f 6e 74 69 6e 75 65 73 2e 0a 20 ger continues..
67a40 2a 0a 20 2a 20 42 65 66 6f 72 65 20 61 20 6e 65 *. * Before a ne
67a50 73 74 65 64 20 74 72 69 67 67 65 72 20 69 73 20 sted trigger is
67a60 63 6f 64 65 64 2c 20 74 68 65 20 6c 69 6e 6b 65 coded, the linke
67a70 64 20 6c 69 73 74 20 70 6f 69 6e 74 65 64 20 74 d list pointed t
67a80 6f 20 62 79 20 74 68 65 20 0a 20 2a 20 70 54 72 o by the . * pTr
67a90 69 67 67 65 72 53 74 61 63 6b 20 69 73 20 73 63 iggerStack is sc
67aa0 61 6e 6e 65 64 20 74 6f 20 65 6e 73 75 72 65 20 anned to ensure
67ab0 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65 72 that the trigger
67ac0 20 69 73 20 6e 6f 74 20 61 62 6f 75 74 20 74 6f is not about to
67ad0 20 62 65 20 63 6f 64 65 64 0a 20 2a 20 72 65 63 be coded. * rec
67ae0 75 72 73 69 76 65 6c 79 2e 20 49 66 20 74 68 69 ursively. If thi
67af0 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 64 s condition is d
67b00 65 74 65 63 74 65 64 2c 20 74 68 65 20 6e 65 73 etected, the nes
67b10 74 65 64 20 74 72 69 67 67 65 72 20 69 73 20 6e ted trigger is n
67b20 6f 74 20 63 6f 64 65 64 2e 0a 20 2a 2f 0a 73 74 ot coded.. */.st
67b30 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63 ruct TriggerStac
67b40 6b 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 k {. Table *pTa
67b50 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 b; /* Ta
67b60 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 65 72 ble that trigger
67b70 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 s are currently
67b80 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 20 2a being coded on *
67b90 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 3b 20 /. int newIdx;
67ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
67bb0 78 20 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72 x of vdbe cursor
67bc0 20 74 6f 20 22 6e 65 77 22 20 74 65 6d 70 20 74 to "new" temp t
67bd0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c able */. int ol
67be0 64 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f dIdx; /
67bf0 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 * Index of vdbe
67c00 63 75 72 73 6f 72 20 74 6f 20 22 6f 6c 64 22 20 cursor to "old"
67c10 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 temp table */.
67c20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 73 6b 3b 0a u32 newColMask;.
67c30 20 20 75 33 32 20 6f 6c 64 43 6f 6c 4d 61 73 6b u32 oldColMask
67c40 3b 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 ;. int orconf;
67c50 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
67c60 65 6e 74 20 6f 72 63 6f 6e 66 20 70 6f 6c 69 63 ent orconf polic
67c70 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 y */. int ignor
67c80 65 4a 75 6d 70 3b 20 20 20 20 20 20 2f 2a 20 77 eJump; /* w
67c90 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20 here to jump to
67ca0 66 6f 72 20 61 20 52 41 49 53 45 28 49 47 4e 4f for a RAISE(IGNO
67cb0 52 45 29 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 RE) */. Trigger
67cc0 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f 2a *pTrigger; /*
67cd0 20 54 68 65 20 74 72 69 67 67 65 72 20 63 75 72 The trigger cur
67ce0 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 rently being cod
67cf0 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 ed */. TriggerS
67d00 74 61 63 6b 20 2a 70 4e 65 78 74 3b 20 2f 2a 20 tack *pNext; /*
67d10 4e 65 78 74 20 74 72 69 67 67 65 72 20 64 6f 77 Next trigger dow
67d20 6e 20 6f 6e 20 74 68 65 20 74 72 69 67 67 65 72 n on the trigger
67d30 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a stack */.};../*
67d40 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
67d50 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 g structure cont
67d60 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ains information
67d70 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c used by the sql
67d80 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75 iteFix....** rou
67d90 74 69 6e 65 73 20 61 73 20 74 68 65 79 20 77 61 tines as they wa
67da0 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 65 lk the parse tre
67db0 65 20 74 6f 20 6d 61 6b 65 20 64 61 74 61 62 61 e to make databa
67dc0 73 65 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2a se references.**
67dd0 20 65 78 70 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a explicit. .*/.
67de0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 typedef struct D
67df0 62 46 69 78 65 72 20 44 62 46 69 78 65 72 3b 0a bFixer DbFixer;.
67e00 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20 7b struct DbFixer {
67e10 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
67e20 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 ; /* The pa
67e30 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 rsing context.
67e40 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 Error messages w
67e50 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 ritten here */.
67e60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
67e70 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 ; /* Make sur
67e80 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 20 61 72 e all objects ar
67e90 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 e contained in t
67ea0 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a his database */.
67eb0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
67ec0 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 ype; /* Type of
67ed0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d the container -
67ee0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 used for error
67ef0 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 63 6f messages */. co
67f00 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 nst Token *pName
67f10 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 ; /* Name of the
67f20 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 container - use
67f30 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 d for error mess
67f40 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ages */.};../*.*
67f50 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64 20 75 73 * An objected us
67f60 65 64 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 ed to accumulate
67f70 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 the text of a s
67f80 74 72 69 6e 67 20 77 68 65 72 65 20 77 65 0a 2a tring where we.*
67f90 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 * do not necessa
67fa0 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 20 62 69 rily know how bi
67fb0 67 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c g the string wil
67fc0 6c 20 62 65 20 69 6e 20 74 68 65 20 65 6e 64 2e l be in the end.
67fd0 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 72 41 63 .*/.struct StrAc
67fe0 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 cum {. sqlite3
67ff0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
68000 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 Optional databas
68010 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 2e e for lookaside.
68020 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c 20 2a 2f Can be NULL */
68030 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 65 3b 20 . char *zBase;
68040 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 61 73 /* A bas
68050 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e e allocation. N
68060 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 ot from malloc.
68070 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 */. char *zText
68080 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
68090 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65 63 74 65 string collecte
680a0 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e d so far */. in
680b0 74 20 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20 t nChar;
680c0 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
680d0 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20 66 61 the string so fa
680e0 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 41 6c 6c r */. int nAll
680f0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 oc; /* A
68100 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 61 mount of space a
68110 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54 65 78 llocated in zTex
68120 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d 78 41 6c t */. int mxAl
68130 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d loc; /* M
68140 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 aximum allowed s
68150 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a tring length */.
68160 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63 46 61 69 u8 mallocFai
68170 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63 6f 6d 65 led; /* Become
68180 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 6d 65 s true if any me
68190 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
681a0 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38 20 20 20 fails */. u8
681b0 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 useMalloc;
681c0 2f 2a 20 54 72 75 65 20 69 66 20 7a 54 65 78 74 /* True if zText
681d0 20 69 73 20 65 6e 6c 61 72 67 65 61 62 6c 65 20 is enlargeable
681e0 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 20 2a 2f using realloc */
681f0 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 67 3b 20 . u8 tooBig;
68200 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63 6f 6d /* Becom
68210 65 73 20 74 72 75 65 20 69 66 20 73 74 72 69 6e es true if strin
68220 67 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 6c g size exceeds l
68230 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a imits */.};../*.
68240 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** A pointer to
68250 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
68260 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e s used to commun
68270 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f icate informatio
68280 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 n.** from sqlite
68290 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f 50 61 72 3Init and OP_Par
682a0 73 65 53 63 68 65 6d 61 20 69 6e 74 6f 20 74 68 seSchema into th
682b0 65 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c e sqlite3InitCal
682c0 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 lback..*/.typede
682d0 66 20 73 74 72 75 63 74 20 7b 0a 20 20 73 71 6c f struct {. sql
682e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
682f0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
68300 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
68310 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b ed */. int iDb;
68320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 /* 0
68330 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61 for main databa
68340 73 65 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c se. 1 for TEMP,
68350 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65 2.. for ATTACHe
68360 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a d */. char **pz
68370 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a 20 45 72 ErrMsg; /* Er
68380 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 6f 72 ror message stor
68390 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ed here */. int
683a0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
683b0 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
683c0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 7d stored here */.}
683d0 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a InitData;../*.*
683e0 2a 20 53 74 72 75 63 74 75 72 65 20 63 6f 6e 74 * Structure cont
683f0 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c 20 63 6f aining global co
68400 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 61 74 61 nfiguration data
68410 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 for the SQLite
68420 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 library..**.** T
68430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6c his structure al
68440 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 so contains some
68450 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 state informati
68460 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 71 on..*/.struct Sq
68470 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b 0a 20 20 lite3Config {.
68480 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b 20 20 20 int bMemstat;
68490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
684a0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
684b0 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 61 74 75 ble memory statu
684c0 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 6f 72 65 s */. int bCore
684d0 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 Mutex;
684e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
684f0 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f 72 65 20 to enable core
68500 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e mutexing */. in
68510 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b 20 20 20 t bFullMutex;
68520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68530 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c /* True to enabl
68540 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 6e 67 20 e full mutexing
68550 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 72 6c 65 */. int mxStrle
68560 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
68570 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
68580 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 m string length
68590 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f 6f 6b 61 */. int szLooka
685a0 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 side;
685b0 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c /* Defaul
685c0 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 t lookaside buff
685d0 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 er size */. int
685e0 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 nLookaside;
685f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
68600 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 * Default lookas
68610 69 64 65 20 62 75 66 66 65 72 20 63 6f 75 6e 74 ide buffer count
68620 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 */. sqlite3_me
68630 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 20 20 20 m_methods m;
68640 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c /* Low-l
68650 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f evel memory allo
68660 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 cation interface
68670 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
68680 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d 75 74 65 tex_methods mute
68690 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c x; /* Low-l
686a0 65 76 65 6c 20 6d 75 74 65 78 20 69 6e 74 65 72 evel mutex inter
686b0 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 face */. sqlite
686c0 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
686d0 20 70 63 61 63 68 65 3b 20 20 20 20 2f 2a 20 4c pcache; /* L
686e0 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65 2d 63 61 ow-level page-ca
686f0 63 68 65 20 69 6e 74 65 72 66 61 63 65 20 2a 2f che interface */
68700 0a 20 20 76 6f 69 64 20 2a 70 48 65 61 70 3b 20 . void *pHeap;
68710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68720 20 20 20 20 20 2f 2a 20 48 65 61 70 20 73 74 6f /* Heap sto
68730 72 61 67 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 rage space */.
68740 69 6e 74 20 6e 48 65 61 70 3b 20 20 20 20 20 20 int nHeap;
68750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68760 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 48 65 /* Size of pHe
68770 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 6e ap[] */. int mn
68780 52 65 71 2c 20 6d 78 52 65 71 3b 20 20 20 20 20 Req, mxReq;
68790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
687a0 69 6e 20 61 6e 64 20 6d 61 78 20 68 65 61 70 20 in and max heap
687b0 72 65 71 75 65 73 74 73 20 73 69 7a 65 73 20 2a requests sizes *
687c0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 72 61 74 /. void *pScrat
687d0 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ch;
687e0 20 20 20 20 20 20 2f 2a 20 53 63 72 61 74 63 68 /* Scratch
687f0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
68800 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20 szScratch;
68810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
68820 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 73 * Size of each s
68830 63 72 61 74 63 68 20 62 75 66 66 65 72 20 2a 2f cratch buffer */
68840 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 63 68 3b . int nScratch;
68850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68860 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
68870 66 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 f scratch buffer
68880 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 61 s */. void *pPa
68890 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
688a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
688b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f cache memory */
688c0 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 . int szPage;
688d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
688e0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
688f0 65 61 63 68 20 70 61 67 65 20 69 6e 20 70 50 61 each page in pPa
68900 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 ge[] */. int nP
68910 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
68920 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
68930 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
68940 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 n pPage[] */. i
68950 6e 74 20 6d 78 50 61 72 73 65 72 53 74 61 63 6b nt mxParserStack
68960 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
68970 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 /* maximum dept
68980 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 h of the parser
68990 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 stack */. int s
689a0 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 haredCacheEnable
689b0 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
689c0 74 72 75 65 20 69 66 20 73 68 61 72 65 64 2d 63 true if shared-c
689d0 61 63 68 65 20 6d 6f 64 65 20 65 6e 61 62 6c 65 ache mode enable
689e0 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 61 62 d */. /* The ab
689f0 6f 76 65 20 6d 69 67 68 74 20 62 65 20 69 6e 69 ove might be ini
68a00 74 69 61 6c 69 7a 65 64 20 74 6f 20 6e 6f 6e 2d tialized to non-
68a10 7a 65 72 6f 2e 20 20 54 68 65 20 66 6f 6c 6c 6f zero. The follo
68a20 77 69 6e 67 20 6e 65 65 64 20 74 6f 20 61 6c 77 wing need to alw
68a30 61 79 73 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c ays. ** initial
68a40 6c 79 20 62 65 20 7a 65 72 6f 2c 20 68 6f 77 65 ly be zero, howe
68a50 76 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 ver. */. int is
68a60 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
68a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
68a80 72 75 65 20 61 66 74 65 72 20 69 6e 69 74 69 61 rue after initia
68a90 6c 69 7a 61 74 69 6f 6e 20 68 61 73 20 66 69 6e lization has fin
68aa0 69 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ished */. int i
68ab0 6e 50 72 6f 67 72 65 73 73 3b 20 20 20 20 20 20 nProgress;
68ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68ad0 54 72 75 65 20 77 68 69 6c 65 20 69 6e 69 74 69 True while initi
68ae0 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70 72 6f alization in pro
68af0 67 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 69 gress */. int i
68b00 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 20 sMallocInit;
68b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68b20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f True after mallo
68b30 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 c is initialized
68b40 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
68b50 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b tex *pInitMutex;
68b60 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
68b70 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 used by sqlite3
68b80 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f _initialize() */
68b90 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d . int nRefInitM
68ba0 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 utex;
68bb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
68bc0 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 f users of pInit
68bd0 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a Mutex */.};../*.
68be0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 ** Context point
68bf0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74 er passed down t
68c00 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d hrough the tree-
68c10 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 walk..*/.struct
68c20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 Walker {. int (
68c30 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 *xExprCallback)(
68c40 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b Walker*, Expr*);
68c50 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b /* Callback
68c60 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 for expressions
68c70 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c */. int (*xSel
68c80 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c ectCallback)(Wal
68c90 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 ker*,Select*);
68ca0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 /* Callback for
68cb0 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 SELECTs */. Par
68cc0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
68cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68ce0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
68cf0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 context. */.
68d00 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
68d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
68d30 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c ra data for call
68d40 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 back */. Name
68d50 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 Context *pNC;
68d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68d70 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 /* Naming
68d80 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 context */.
68d90 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
68da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68db0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
68dc0 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 teger value */.
68dd0 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 } u;.};../* For
68de0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
68df0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 s */.SQLITE_PRIV
68e00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
68e10 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c alkExpr(Walker*,
68e20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
68e30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
68e40 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 te3WalkExprList(
68e50 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 Walker*, ExprLis
68e60 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
68e70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
68e80 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 alkSelect(Walker
68e90 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
68ea0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
68eb0 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
68ec0 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 tExpr(Walker*, S
68ed0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
68ee0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
68ef0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f te3WalkSelectFro
68f00 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 m(Walker*, Selec
68f10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 t*);../*.** Retu
68f20 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 rn code from the
68f30 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b parse-tree walk
68f40 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 ing primitives a
68f50 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c nd their.** call
68f60 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e backs..*/.#defin
68f70 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 e WRC_Continue
68f80 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 0 /* Continu
68f90 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c e down into chil
68fa0 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 dren */.#define
68fb0 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 WRC_Prune
68fc0 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 1 /* Omit chil
68fd0 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 dren but continu
68fe0 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e e walking siblin
68ff0 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 gs */.#define WR
69000 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20 C_Abort 2
69010 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 /* Abandon the
69020 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f tree walk */../
69030 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 *.** Assuming zI
69040 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 n points to the
69050 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 first byte of a
69060 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c UTF-8 character,
69070 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 .** advance zIn
69080 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
69090 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
690a0 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 e next UTF-8 cha
690b0 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 racter..*/.#defi
690c0 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 ne SQLITE_SKIP_U
690d0 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 TF8(zIn) {
690e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
690f0 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e \. if( (*(zIn
69100 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 ++))>=0xc0 ){
69110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69120 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
69130 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 while( (*zIn &
69140 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 0xc0)==0x80 ){ z
69150 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 In++; }
69160 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
69170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69190 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
691a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
691b0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 TE_CORRUPT_BKPT
691c0 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 macro can be eit
691d0 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 her a constant (
691e0 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a for production.*
691f0 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 * builds) or a f
69200 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f unction call (fo
69210 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 r debugging). I
69220 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 f it is a functi
69230 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 on call,.** it a
69240 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 llows the operat
69250 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 or to set a brea
69260 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 kpoint at the sp
69270 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 ot where databas
69280 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 e.** corruption
69290 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 is first detecte
692a0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
692b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
692c0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
692d0 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f qlite3Corrupt(vo
692e0 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 id);.# define SQ
692f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
69300 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 T sqlite3Corrupt
69310 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ().#else.# defin
69320 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 e SQLITE_CORRUPT
69330 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 _BKPT SQLITE_COR
69340 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RUPT.#endif../*.
69350 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
69360 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68 macros mimic th
69370 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 e standard libra
69380 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75 ry functions tou
69390 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61 pper(),.** isspa
693a0 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c ce(), isalnum(),
693b0 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69 isdigit() and i
693c0 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65 sxdigit(), respe
693d0 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 ctively. The.**
693e0 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20 sqlite versions
693f0 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53 only work for AS
69400 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20 CII characters,
69410 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f regardless of lo
69420 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cale..*/.#ifdef
69430 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64 SQLITE_ASCII.# d
69440 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75 efine sqlite3Tou
69450 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28 pper(x) ((x)&~(
69460 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
69470 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
69480 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66 x)]&0x20)).# def
69490 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 ine sqlite3Isspa
694a0 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ce(x) (sqlite3
694b0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
694c0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
694d0 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 1).# define sqli
694e0 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 te3Isalnum(x)
694f0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
69500 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
69510 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66 (x)]&0x06).# def
69520 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
69530 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ha(x) (sqlite3
69540 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
69550 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
69560 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 2).# define sqli
69570 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20 te3Isdigit(x)
69580 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
69590 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
695a0 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66 (x)]&0x04).# def
695b0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69 ine sqlite3Isxdi
695c0 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33 git(x) (sqlite3
695d0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
695e0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
695f0 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 8).# define sqli
69600 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20 te3Tolower(x)
69610 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c (sqlite3UpperToL
69620 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 ower[(unsigned c
69630 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a har)(x)]).#else.
69640 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 # include <ctype
69650 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c .h>.# define sql
69660 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 ite3Toupper(x)
69670 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e toupper((unsign
69680 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
69690 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 efine sqlite3Iss
696a0 70 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63 pace(x) isspac
696b0 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e((unsigned char
696c0 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 )(x)).# define s
696d0 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 qlite3Isalnum(x)
696e0 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 isalnum((unsi
696f0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
69700 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
69710 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c salpha(x) isal
69720 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68 pha((unsigned ch
69730 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
69740 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
69750 78 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e x) isdigit((un
69760 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
69770 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
69780 33 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73 3Isxdigit(x) is
69790 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64 xdigit((unsigned
697a0 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
697b0 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 ine sqlite3Tolow
697c0 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 er(x) tolower(
697d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
697e0 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a x)).#endif../*.*
697f0 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 * Internal funct
69800 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a ion prototypes.*
69810 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
69820 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 int sqlite3StrI
69830 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
69840 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
69850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69860 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49 int sqlite3StrNI
69870 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
69880 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
69890 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
698a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
698b0 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 IsNumber(const c
698c0 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b har*, int*, u8);
698d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
698e0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 int sqlite3Strle
698f0 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 n30(const char*)
69900 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
69910 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c E int sqlite3Mal
69920 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 locInit(void);.S
69930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69940 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 id sqlite3Malloc
69950 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 End(void);.SQLIT
69960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
69970 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e sqlite3Malloc(in
69980 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
69990 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
699a0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b MallocZero(int);
699b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
699c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d void *sqlite3DbM
699d0 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 allocZero(sqlite
699e0 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 3*, int);.SQLITE
699f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
69a00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
69a10 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 w(sqlite3*, int)
69a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
69a30 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
69a40 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c StrDup(sqlite3*,
69a50 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
69a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
69a70 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e r *sqlite3DbStrN
69a80 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e Dup(sqlite3*,con
69a90 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a st char*, int);.
69aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
69ab0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c oid *sqlite3Real
69ac0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b loc(void*, int);
69ad0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69ae0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
69af0 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c eallocOrFree(sql
69b00 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 ite3 *, void *,
69b10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
69b20 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
69b30 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 e3DbRealloc(sqli
69b40 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 te3 *, void *, i
69b50 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
69b60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
69b70 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c DbFree(sqlite3*,
69b80 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
69b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
69ba0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f te3MallocSize(vo
69bb0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
69bc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
69bd0 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c DbMallocSize(sql
69be0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
69bf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69c00 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74 id *sqlite3Scrat
69c10 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 chMalloc(int);.S
69c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69c30 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 id sqlite3Scratc
69c40 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 hFree(void*);.SQ
69c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
69c60 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 d *sqlite3PageMa
69c70 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
69c80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69c90 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 qlite3PageFree(v
69ca0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
69cb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69cc0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
69cd0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
69ce0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69cf0 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f e3BenignMallocHo
69d00 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 oks(void (*)(voi
69d10 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 d), void (*)(voi
69d20 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d));.SQLITE_PRIV
69d30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
69d40 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20 emoryAlarm(void
69d50 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 (*)(void*, sqlit
69d60 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20 e3_int64, int),
69d70 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
69d80 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65 66 20 53 nt64);..#ifdef S
69d90 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
69da0 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS3.SQLITE_PRIV
69db0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
69dc0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
69dd0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
69de0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys3(void);.#endi
69df0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
69e00 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53 ENABLE_MEMSYS5.S
69e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
69e20 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
69e30 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
69e40 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
69e50 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 id);.#endif...#i
69e60 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 fndef SQLITE_MUT
69e70 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50 EX_OMIT.SQLITE_P
69e80 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 RIVATE sqlite3
69e90 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
69ea0 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 sqlite3DefaultMu
69eb0 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 tex(void);.SQLIT
69ec0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
69ed0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
69ee0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 e3MutexAlloc(int
69ef0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69f00 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
69f10 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a utexInit(void);.
69f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
69f30 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 int sqlite3Mute
69f40 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 xEnd(void);.#end
69f50 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
69f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
69f70 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a atusValue(int);.
69f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
69f90 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
69fa0 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sAdd(int, int);.
69fb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
69fc0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
69fd0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sSet(int, int);.
69fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69ff0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e int sqlite3IsNaN
6a000 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 (double);..SQLIT
6a010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6a020 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53 qlite3VXPrintf(S
6a030 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 trAccum*, int, c
6a040 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c onst char*, va_l
6a050 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ist);.SQLITE_PRI
6a060 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
6a070 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 e3MPrintf(sqlite
6a080 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
6a090 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
6a0a0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
6a0b0 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 e3VMPrintf(sqlit
6a0c0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c e3*,const char*,
6a0d0 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 va_list);.SQLIT
6a0e0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
6a0f0 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 sqlite3MAppendf(
6a100 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 sqlite3*,char*,c
6a110 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
6a120 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
6a130 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 ITE_TEST) || def
6a140 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
6a150 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 G).SQLITE_PRIVAT
6a160 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
6a170 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 DebugPrintf(cons
6a180 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 t char*, ...);.#
6a190 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 endif.#if define
6a1a0 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 d(SQLITE_TEST).S
6a1b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6a1c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 void *sqlite3Tes
6a1d0 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 tTextToPtr(const
6a1e0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
6a1f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a200 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 oid sqlite3SetSt
6a210 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71 ring(char **, sq
6a220 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 lite3*, const ch
6a230 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 ar*, ...);.SQLIT
6a240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6a250 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 qlite3ErrorMsg(P
6a260 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
6a270 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 r*, ...);.SQLITE
6a280 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6a290 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
6a2a0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6a2b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6a2c0 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a te3Dequote(char*
6a2d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 E int sqlite3Key
6a2f0 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 wordCode(const u
6a300 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 nsigned char*, i
6a310 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6a320 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
6a330 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a 2c unParser(Parse*,
6a340 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 const char*, ch
6a350 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ar **);.SQLITE_P
6a360 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6a370 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 te3FinishCoding(
6a380 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6a390 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6a3a0 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61 te3GetTempReg(Pa
6a3b0 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
6a3c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a3d0 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
6a3e0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Parse*,int);.SQ
6a3f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6a400 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
6a410 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 ange(Parse*,int)
6a420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a430 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c void sqlite3Rel
6a440 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 61 easeTempRange(Pa
6a450 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 rse*,int,int);.S
6a460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
6a470 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 pr *sqlite3Expr(
6a480 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45 sqlite3*, int, E
6a490 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e xpr*, Expr*, con
6a4a0 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 st Token*);.SQLI
6a4b0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
6a4c0 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50 61 *sqlite3PExpr(Pa
6a4d0 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a rse*, int, Expr*
6a4e0 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 , Expr*, const T
6a4f0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6a500 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
6a510 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72 ite3RegisterExpr
6a520 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b (Parse*,Token*);
6a530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6a540 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
6a550 72 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 rAnd(sqlite3*,Ex
6a560 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c pr*, Expr*);.SQL
6a570 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6a580 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e sqlite3ExprSpan
6a590 28 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f (Expr*,Token*,To
6a5a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6a5b0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
6a5c0 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 te3ExprFunction(
6a5d0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a Parse*,ExprList*
6a5e0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
6a5f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6a600 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e qlite3ExprAssign
6a610 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a VarNumber(Parse*
6a620 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6a630 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6a640 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 lite3ExprClear(s
6a650 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b qlite3*, Expr*);
6a660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6a670 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6a680 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c Delete(sqlite3*,
6a690 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6a6a0 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 PRIVATE ExprList
6a6b0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 *sqlite3ExprLis
6a6c0 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 tAppend(Parse*,E
6a6d0 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54 xprList*,Expr*,T
6a6e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6a6f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6a700 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
6a710 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 e(sqlite3*, Expr
6a720 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
6a730 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6a740 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c e3Init(sqlite3*,
6a750 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 char**);.SQLITE
6a760 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6a770 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b ite3InitCallback
6a780 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 (void*, int, cha
6a790 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 r**, char**);.SQ
6a7a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6a7b0 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 d sqlite3Pragma(
6a7c0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f Parse*,Token*,To
6a7d0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 ken*,Token*,int)
6a7e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a7f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 void sqlite3Res
6a800 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
6a810 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b (sqlite3*, int);
6a820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6a830 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 void sqlite3Begi
6a840 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c 69 6e nParse(Parse*,in
6a850 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6a860 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
6a870 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 ommitInternalCha
6a880 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a nges(sqlite3*);.
6a890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 SQLITE_PRIVATE T
6a8a0 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 65 73 able *sqlite3Res
6a8b0 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 50 ultSetOfSelect(P
6a8c0 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a arse*,Select*);.
6a8d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a8e0 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d oid sqlite3OpenM
6a8f0 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65 asterTable(Parse
6a900 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
6a910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6a920 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 lite3StartTable(
6a930 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f Parse*,Token*,To
6a940 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ken*,int,int,int
6a950 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6a960 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a970 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 e3AddColumn(Pars
6a980 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 e*,Token*);.SQLI
6a990 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6a9a0 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c sqlite3AddNotNul
6a9b0 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a l(Parse*, int);.
6a9c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a9d0 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50 72 oid sqlite3AddPr
6a9e0 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 2a 2c imaryKey(Parse*,
6a9f0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c ExprList*, int,
6aa00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6aa10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6aa20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 sqlite3AddCheckC
6aa30 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65 2a onstraint(Parse*
6aa40 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6aa50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6aa60 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 lite3AddColumnTy
6aa70 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a pe(Parse*,Token*
6aa80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6aa90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
6aaa0 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61 dDefaultValue(Pa
6aab0 72 73 65 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c rse*,Expr*);.SQL
6aac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6aad0 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 sqlite3AddColla
6aae0 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 teType(Parse*, T
6aaf0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6ab00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6ab10 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73 te3EndTable(Pars
6ab20 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
6ab30 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 ,Select*);..SQLI
6ab40 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 TE_PRIVATE Bitve
6ab50 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 c *sqlite3Bitvec
6ab60 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c Create(u32);.SQL
6ab70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ab80 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
6ab90 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b t(Bitvec*, u32);
6aba0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6abb0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
6abc0 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33 cSet(Bitvec*, u3
6abd0 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 2);.SQLITE_PRIVA
6abe0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
6abf0 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 itvecClear(Bitve
6ac00 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 c*, u32);.SQLITE
6ac10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ac20 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
6ac30 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c oy(Bitvec*);.SQL
6ac40 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
6ac50 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
6ac60 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 e(Bitvec*);.SQLI
6ac70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ac80 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c qlite3BitvecBuil
6ac90 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a tinTest(int,int*
6aca0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
6acb0 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 TE RowSet *sqlit
6acc0 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c e3RowSetInit(sql
6acd0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e ite3*, void*, un
6ace0 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c signed int);.SQL
6acf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6ad00 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c sqlite3RowSetCl
6ad10 65 61 72 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51 ear(RowSet*);.SQ
6ad20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ad30 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 d sqlite3RowSetI
6ad40 6e 73 65 72 74 28 52 6f 77 53 65 74 2a 2c 20 69 nsert(RowSet*, i
6ad50 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
6ad60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
6ad70 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53 65 74 owSetTest(RowSet
6ad80 2a 2c 20 75 38 20 69 42 61 74 63 68 2c 20 69 36 *, u8 iBatch, i6
6ad90 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 4);.SQLITE_PRIVA
6ada0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f TE int sqlite3Ro
6adb0 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 2a wSetNext(RowSet*
6adc0 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 , i64*);..SQLITE
6add0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ade0 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 lite3CreateView(
6adf0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f Parse*,Token*,To
6ae00 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 ken*,Token*,Sele
6ae10 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 ct*,int,int);..#
6ae20 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
6ae30 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c TE_OMIT_VIEW) ||
6ae40 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
6ae50 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
6ae60 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 LE).SQLITE_PRIVA
6ae70 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
6ae80 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
6ae90 65 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a es(Parse*,Table*
6aea0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
6aeb0 65 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 e sqlite3ViewGet
6aec0 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 ColumnNames(A,B)
6aed0 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 0.#endif..SQLIT
6aee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6aef0 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 qlite3DropTable(
6af00 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
6af10 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6af20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6af30 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 sqlite3DeleteTa
6af40 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c ble(Table*);.SQL
6af50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6af60 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 sqlite3Insert(P
6af70 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
6af80 20 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 ExprList*, Sele
6af90 63 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e ct*, IdList*, in
6afa0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6afb0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6afc0 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 ArrayAllocate(sq
6afd0 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 lite3*,void*,int
6afe0 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 ,int,int*,int*,i
6aff0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
6b000 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c VATE IdList *sql
6b010 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 ite3IdListAppend
6b020 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 (sqlite3*, IdLis
6b030 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c t*, Token*);.SQL
6b040 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6b050 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 sqlite3IdListInd
6b060 65 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 ex(IdList*,const
6b070 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6b080 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 PRIVATE SrcList
6b090 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 *sqlite3SrcListE
6b0a0 6e 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c nlarge(sqlite3*,
6b0b0 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 SrcList*, int,
6b0c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6b0d0 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 VATE SrcList *sq
6b0e0 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 lite3SrcListAppe
6b0f0 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 nd(sqlite3*, Src
6b100 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 List*, Token*, T
6b110 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6b120 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
6b130 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 sqlite3SrcListAp
6b140 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 pendFromTerm(Par
6b150 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 se*, SrcList*, T
6b160 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 oken*, Token*,.
6b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b190 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c Token*, Sel
6b1a0 65 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c ect*, Expr*, IdL
6b1b0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6b1c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6b1d0 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 e3SrcListIndexed
6b1e0 42 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c By(Parse *, SrcL
6b1f0 69 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b ist *, Token *);
6b200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6b210 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 int sqlite3Index
6b220 65 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 edByLookup(Parse
6b230 20 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 *, struct SrcLi
6b240 73 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 st_item *);.SQLI
6b250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6b260 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 sqlite3SrcListSh
6b270 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c iftJoinType(SrcL
6b280 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6b290 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6b2a0 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 e3SrcListAssignC
6b2b0 75 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 ursors(Parse*, S
6b2c0 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
6b2d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6b2e0 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 lite3IdListDelet
6b2f0 65 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 e(sqlite3*, IdLi
6b300 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
6b310 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b320 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 3SrcListDelete(s
6b330 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
6b340 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6b350 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
6b360 72 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 reateIndex(Parse
6b370 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
6b380 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 SrcList*,ExprLis
6b390 74 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 t*,int,Token*,.
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b3b0 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 Token*, i
6b3c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
6b3d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6b3e0 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 lite3DropIndex(P
6b3f0 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
6b400 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6b410 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6b420 33 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 3Select(Parse*,
6b430 53 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 Select*, SelectD
6b440 65 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 est*);.SQLITE_PR
6b450 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
6b460 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 lite3SelectNew(P
6b470 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c arse*,ExprList*,
6b480 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 SrcList*,Expr*,E
6b490 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 xprList*,.
6b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b4b0 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 Expr*,ExprLis
6b4c0 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 t*,int,Expr*,Exp
6b4d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6b4e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6b4f0 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c SelectDelete(sql
6b500 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b ite3*, Select*);
6b510 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6b520 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 Table *sqlite3Sr
6b530 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 cListLookup(Pars
6b540 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 e*, SrcList*);.S
6b550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6b560 74 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f t sqlite3IsReadO
6b570 6e 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c nly(Parse*, Tabl
6b580 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
6b590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6b5a0 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 lite3OpenTable(P
6b5b0 61 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c arse*, int iCur,
6b5c0 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a int iDb, Table*
6b5d0 2c 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 , int);.#if defi
6b5e0 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
6b5f0 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f E_UPDATE_DELETE_
6b600 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e LIMIT) && !defin
6b610 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
6b620 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f UBQUERY).SQLITE_
6b630 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
6b640 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 lite3LimitWhere(
6b650 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 Parse *, SrcList
6b660 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
6b670 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 List *, Expr *,
6b680 45 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b Expr *, char *);
6b690 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
6b6a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6b6b0 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 te3DeleteFrom(Pa
6b6c0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
6b6d0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
6b6e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6b6f0 74 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a te3Update(Parse*
6b700 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 , SrcList*, Expr
6b710 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e List*, Expr*, in
6b720 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6b730 54 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 TE WhereInfo *sq
6b740 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 lite3WhereBegin(
6b750 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
6b760 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 , Expr*, ExprLis
6b770 74 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 t**, u16);.SQLIT
6b780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6b790 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 qlite3WhereEnd(W
6b7a0 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 hereInfo*);.SQLI
6b7b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6b7c0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 qlite3ExprCodeGe
6b7d0 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 tColumn(Parse*,
6b7e0 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Table*, int, int
6b7f0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6b800 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6b810 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6b820 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 Move(Parse*, int
6b830 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6b840 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6b850 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6b860 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 Copy(Parse*, int
6b870 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6b880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6b890 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
6b8a0 65 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 eStore(Parse*, i
6b8b0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 nt, int, int);.S
6b8c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6b8d0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
6b8e0 63 68 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b chePush(Parse*);
6b8f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6b900 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6b910 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c CachePop(Parse*,
6b920 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6b930 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6b940 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 e3ExprCacheRemov
6b950 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a e(Parse*, int);.
6b960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6b970 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
6b980 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a acheClear(Parse*
6b990 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b9a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
6b9b0 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
6b9c0 68 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e hange(Parse*, in
6b9d0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6b9e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b9f0 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 ite3ExprHardCopy
6ba00 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 (Parse*,int,int)
6ba10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ba20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
6ba30 43 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 Code(Parse*, Exp
6ba40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
6ba50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6ba60 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
6ba70 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
6ba80 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
6ba90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6baa0 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
6bab0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
6bac0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6bad0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
6bae0 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 xprCodeAndCache(
6baf0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
6bb00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6bb10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6bb20 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 ExprCodeConstant
6bb30 73 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 s(Parse*, Expr*)
6bb40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6bb50 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
6bb60 43 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 CodeExprList(Par
6bb70 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 se*, ExprList*,
6bb80 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
6bb90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6bba0 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 qlite3ExprIfTrue
6bbb0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
6bbc0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
6bbd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6bbe0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
6bbf0 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c e(Parse*, Expr*,
6bc00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6bc10 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
6bc20 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 *sqlite3FindTab
6bc30 6c 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 le(sqlite3*,cons
6bc40 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
6bc50 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
6bc60 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
6bc70 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
6bc80 50 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 Parse*,int isVie
6bc90 77 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 w,const char*, c
6bca0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
6bcb0 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 ITE_PRIVATE Inde
6bcc0 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e x *sqlite3FindIn
6bcd0 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e dex(sqlite3*,con
6bce0 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 st char*, const
6bcf0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
6bd00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6bd10 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
6bd20 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a teTable(sqlite3*
6bd30 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
6bd40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6bd50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e E void sqlite3Un
6bd60 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 linkAndDeleteInd
6bd70 65 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c ex(sqlite3*,int,
6bd80 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
6bd90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6bda0 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 d sqlite3Vacuum(
6bdb0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6bdc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6bdd0 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 te3RunVacuum(cha
6bde0 72 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a r**, sqlite3*);.
6bdf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
6be00 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 har *sqlite3Name
6be10 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 FromToken(sqlite
6be20 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 3*, Token*);.SQL
6be30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6be40 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
6be50 72 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 re(Expr*, Expr*)
6be60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6be70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6be80 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 rAnalyzeAggregat
6be90 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c es(NameContext*,
6bea0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6beb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6bec0 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 ite3ExprAnalyzeA
6bed0 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 ggList(NameConte
6bee0 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a xt*,ExprList*);.
6bef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 SQLITE_PRIVATE V
6bf00 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 dbe *sqlite3GetV
6bf10 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c dbe(Parse*);.SQL
6bf20 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
6bf30 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 *sqlite3CreateI
6bf40 64 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 dExpr(Parse *, c
6bf50 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
6bf60 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6bf70 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 sqlite3PrngSave
6bf80 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
6bf90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6bfa0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 sqlite3PrngRest
6bfb0 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a oreState(void);.
6bfc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6bfd0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 oid sqlite3PrngR
6bfe0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b esetState(void);
6bff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c000 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c void sqlite3Roll
6c010 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a backAll(sqlite3*
6c020 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c030 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
6c040 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 deVerifySchema(P
6c050 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
6c060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6c070 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 sqlite3BeginTra
6c080 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c nsaction(Parse*,
6c090 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6c0a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6c0b0 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 e3CommitTransact
6c0c0 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ion(Parse*);.SQL
6c0d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6c0e0 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
6c0f0 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 Transaction(Pars
6c100 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
6c110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6c120 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a Savepoint(Parse*
6c130 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a , int, Token*);.
6c140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6c150 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 oid sqlite3Close
6c160 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 Savepoints(sqlit
6c170 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e3 *);.SQLITE_PR
6c180 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6c190 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 3ExprIsConstant(
6c1a0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
6c1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6c1c0 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
6c1d0 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a NotJoin(Expr*);.
6c1e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6c1f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 nt sqlite3ExprIs
6c200 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 ConstantOrFuncti
6c210 6f 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 on(Expr*);.SQLIT
6c220 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6c230 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
6c240 65 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b er(Expr*, int*);
6c250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c260 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 int sqlite3IsRow
6c270 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b id(const char*);
6c280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c290 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 void sqlite3Gene
6c2a0 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 rateRowDelete(Pa
6c2b0 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e rse*, Table*, in
6c2c0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
6c2d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6c2e0 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 d sqlite3Generat
6c2f0 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 eRowIndexDelete(
6c300 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6c310 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 int, int*);.SQLI
6c320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6c330 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e qlite3GenerateIn
6c340 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 dexKey(Parse*, I
6c350 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c ndex*, int, int,
6c360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6c370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6c380 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 e3GenerateConstr
6c390 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 aintChecks(Parse
6c3a0 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 *,Table*,int,int
6c3b0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
6c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c3d0 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c int*,int,
6c3e0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a int,int,int,int*
6c3f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c400 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
6c410 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 mpleteInsertion(
6c420 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6c430 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 int, int, int*,
6c440 69 6e 74 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 int, int,int,int
6c450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 E int sqlite3Ope
6c470 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 nTableAndIndices
6c480 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
6c490 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
6c4a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6c4b0 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
6c4c0 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 eOperation(Parse
6c4d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
6c4e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
6c4f0 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 r *sqlite3ExprDu
6c500 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a p(sqlite3*,Expr*
6c510 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6c520 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6c530 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 e3TokenCopy(sqli
6c540 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73 te3*,Token*,cons
6c550 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 t Token*);.SQLIT
6c560 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 E_PRIVATE ExprLi
6c570 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c st *sqlite3ExprL
6c580 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c istDup(sqlite3*,
6c590 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a ExprList*,int);.
6c5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
6c5b0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
6c5c0 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 rcListDup(sqlite
6c5d0 33 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3*,SrcList*,int)
6c5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6c5f0 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
6c600 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 IdListDup(sqlite
6c610 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 3*,IdList*);.SQL
6c620 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
6c630 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
6c640 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 tDup(sqlite3*,Se
6c650 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 lect*,int);.SQLI
6c660 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6c670 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e sqlite3FuncDefIn
6c680 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 sert(FuncDefHash
6c690 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 *, FuncDef*);.SQ
6c6a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e LITE_PRIVATE Fun
6c6b0 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e cDef *sqlite3Fin
6c6c0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 dFunction(sqlite
6c6d0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 3*,const char*,i
6c6e0 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a nt,int,u8,int);.
6c6f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6c700 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
6c710 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 terBuiltinFuncti
6c720 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 ons(sqlite3*);.S
6c730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6c740 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
6c750 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
6c760 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ons(void);.SQLIT
6c770 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6c780 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c qlite3RegisterGl
6c790 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f obalFunctions(vo
6c7a0 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 id);.#ifdef SQLI
6c7b0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
6c7c0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6c7d0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 lite3SafetyOn(sq
6c7e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
6c7f0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6c800 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 lite3SafetyOff(s
6c810 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
6c820 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6c830 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 SafetyOn(A) 0.#
6c840 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 define sqlite3Sa
6c850 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e fetyOff(A) 0.#en
6c860 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
6c870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
6c880 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 fetyCheckOk(sqli
6c890 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
6c8a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6c8b0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 3SafetyCheckSick
6c8c0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a OrOk(sqlite3*);.
6c8d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6c8e0 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 oid sqlite3Chang
6c8f0 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 eCookie(Parse*,
6c900 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
6c910 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
6c920 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 VIEW) && !define
6c930 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
6c940 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 IGGER).SQLITE_PR
6c950 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6c960 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 e3MaterializeVie
6c970 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a w(Parse*, Table*
6c980 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 , Expr*, int);.#
6c990 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
6c9a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
6c9b0 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ER.SQLITE_PRIVAT
6c9c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
6c9d0 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 BeginTrigger(Par
6c9e0 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 se*, Token*,Toke
6c9f0 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 n*,int,int,IdLis
6ca00 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 t*,SrcList*,.
6ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ca20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e Expr*,in
6ca30 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6ca40 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6ca50 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 qlite3FinishTrig
6ca60 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 ger(Parse*, Trig
6ca70 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a gerStep*, Token*
6ca80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ca90 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
6caa0 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 DropTrigger(Pars
6cab0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
6cac0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6cad0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
6cae0 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 3DropTriggerPtr(
6caf0 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a Parse*, Trigger*
6cb00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6cb10 45 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c E Trigger *sql
6cb20 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 ite3TriggersExis
6cb30 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 t(Parse *, Table
6cb40 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 *, int, ExprList
6cb50 2a 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a *, int *pMask);.
6cb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6cb70 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 Trigger *sqlite
6cb80 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 3TriggerList(Par
6cb90 73 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a se *, Table *);.
6cba0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6cbb0 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 int sqlite3Code
6cbc0 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65 RowTrigger(Parse
6cbd0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e *, Trigger *, in
6cbe0 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e t, ExprList*, in
6cbf0 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20 t, Table *,.
6cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cc10 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 int, int
6cc20 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a , int, int, u32*
6cc30 2c 20 75 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20 , u32*);. void
6cc40 73 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 sqliteViewTrigge
6cc50 72 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 rs(Parse*, Table
6cc60 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 *, Expr*, int, E
6cc70 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 xprList*);.SQLIT
6cc80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6cc90 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 sqlite3DeleteTr
6cca0 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 iggerStep(sqlite
6ccb0 33 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 3*, TriggerStep*
6ccc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ccd0 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 E TriggerStep
6cce0 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 *sqlite3TriggerS
6ccf0 65 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 electStep(sqlite
6cd00 33 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 3*,Select*);.SQL
6cd10 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
6cd20 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 iggerStep *sqlit
6cd30 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 e3TriggerInsertS
6cd40 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b tep(sqlite3*,Tok
6cd50 65 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 en*, IdList*,.
6cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd80 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c ExprList*,
6cd90 53 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 Select*,int);.SQ
6cda0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 LITE_PRIVATE T
6cdb0 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 riggerStep *sqli
6cdc0 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65 te3TriggerUpdate
6cdd0 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f Step(sqlite3*,To
6cde0 6b 65 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 ken*,ExprList*,
6cdf0 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Expr*, int);.SQL
6ce00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
6ce10 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 iggerStep *sqlit
6ce20 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 e3TriggerDeleteS
6ce30 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b tep(sqlite3*,Tok
6ce40 65 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c en*, Expr*);.SQL
6ce50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
6ce60 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 id sqlite3Delete
6ce70 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a Trigger(sqlite3*
6ce80 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c , Trigger*);.SQL
6ce90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
6cea0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
6ceb0 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 AndDeleteTrigger
6cec0 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f (sqlite3*,int,co
6ced0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 nst char*);.#els
6cee0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
6cef0 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 e3TriggersExist(
6cf00 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 B,C,D,E,F) 0.# d
6cf10 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c efine sqlite3Del
6cf20 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a eteTrigger(A,B).
6cf30 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6cf40 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 DropTriggerPtr(A
6cf50 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,B).# define sql
6cf60 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c ite3UnlinkAndDel
6cf70 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 eteTrigger(A,B,C
6cf80 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
6cf90 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 e3CodeRowTrigger
6cfa0 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 (A,B,C,D,E,F,G,H
6cfb0 2c 49 2c 4a 2c 4b 2c 4c 29 20 30 0a 23 20 64 65 ,I,J,K,L) 0.# de
6cfc0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 fine sqlite3Trig
6cfd0 67 65 72 4c 69 73 74 28 58 2c 20 59 29 20 30 0a gerList(X, Y) 0.
6cfe0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
6cff0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6d000 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65 e3JoinType(Parse
6d010 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
6d020 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
6d030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6d040 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 sqlite3CreateFor
6d050 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 eignKey(Parse*,
6d060 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e ExprList*, Token
6d070 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
6d080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6d090 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
6d0a0 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 eferForeignKey(P
6d0b0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 arse*, int);.#if
6d0c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
6d0d0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 _AUTHORIZATION.S
6d0e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6d0f0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
6d100 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72 Read(Parse*,Expr
6d110 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73 *,Schema*,SrcLis
6d120 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6d130 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
6d140 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65 3AuthCheck(Parse
6d150 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 *,int, const cha
6d160 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c r*, const char*,
6d170 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 const char*);.S
6d180 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6d190 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
6d1a0 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73 ContextPush(Pars
6d1b0 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a e*, AuthContext*
6d1c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
6d1d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6d1e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 void sqlite3Aut
6d1f0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68 hContextPop(Auth
6d200 43 6f 6e 74 65 78 74 2a 29 3b 0a 23 65 6c 73 65 Context*);.#else
6d210 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6d220 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 3AuthRead(a,b,c,
6d230 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 d).# define sqli
6d240 74 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 te3AuthCheck(a,b
6d250 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 ,c,d,e) SQLIT
6d260 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 E_OK.# define sq
6d270 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
6d280 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 Push(a,b,c).# de
6d290 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
6d2a0 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 ContextPop(a) (
6d2b0 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 (void)(a)).#endi
6d2c0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
6d2d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 void sqlite3Att
6d2e0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ach(Parse*, Expr
6d2f0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 *, Expr*, Expr*)
6d300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6d310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 void sqlite3Det
6d320 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ach(Parse*, Expr
6d330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6d340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
6d350 72 65 65 46 61 63 74 6f 72 79 28 63 6f 6e 73 74 reeFactory(const
6d360 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f sqlite3 *db, co
6d370 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
6d380 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ame,.
6d390 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
6d3a0 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 omitJournal, int
6d3b0 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66 6c 61 nCache, int fla
6d3c0 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70 42 74 gs, Btree **ppBt
6d3d0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ree);.SQLITE_PRI
6d3e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6d3f0 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72 2a FixInit(DbFixer*
6d400 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 , Parse*, int, c
6d410 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
6d420 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 t Token*);.SQLIT
6d430 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d440 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 lite3FixSrcList(
6d450 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c 69 73 DbFixer*, SrcLis
6d460 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6d470 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
6d480 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 65 72 ixSelect(DbFixer
6d490 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
6d4a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6d4b0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 44 sqlite3FixExpr(D
6d4c0 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b bFixer*, Expr*);
6d4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d4e0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 int sqlite3FixEx
6d4f0 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c prList(DbFixer*,
6d500 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c ExprList*);.SQL
6d510 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6d520 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 sqlite3FixTrigge
6d530 72 53 74 65 70 28 44 62 46 69 78 65 72 2a 2c 20 rStep(DbFixer*,
6d540 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 TriggerStep*);.S
6d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6d560 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f t sqlite3AtoF(co
6d570 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 nst char *z, dou
6d580 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
6d590 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6d5a0 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 3GetInt32(const
6d5b0 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 char *, int*);.S
6d5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6d5d0 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 t sqlite3FitsIn6
6d5e0 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 4Bits(const char
6d5f0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
6d600 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6d610 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
6d620 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 (const void *pDa
6d630 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a ta, int nChar);.
6d640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6d650 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 nt sqlite3Utf8Ch
6d660 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 arLen(const char
6d670 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 42 79 *pData, int nBy
6d680 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 te);.SQLITE_PRIV
6d690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
6d6a0 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20 75 38 tf8Read(const u8
6d6b0 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a *, const u8**);.
6d6c0 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 ./*.** Routines
6d6d0 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 to read and writ
6d6e0 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 e variable-lengt
6d6f0 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68 65 h integers. The
6d700 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 se used to.** be
6d710 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 defined locally
6d720 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73 65 , but now we use
6d730 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75 74 the varint rout
6d740 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69 6c ines in the util
6d750 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64 .c.** file. Cod
6d760 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 e should use the
6d770 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c MACRO forms bel
6d780 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72 69 6e ow, as the Varin
6d790 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 t32 versions.**
6d7a0 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61 73 73 are coded to ass
6d7b0 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65 20 62 ume the single b
6d7c0 79 74 65 20 63 61 73 65 20 69 73 20 61 6c 72 65 yte case is alre
6d7d0 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77 68 69 ady handled (whi
6d7e0 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f ch .** the MACRO
6d7f0 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a form does)..*/.
6d800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6d810 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 nt sqlite3PutVar
6d820 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 int(unsigned cha
6d830 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49 54 45 r*, u64);.SQLITE
6d840 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6d850 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 ite3PutVarint32(
6d860 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
6d870 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u32);.SQLITE_PRI
6d880 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 VATE u8 sqlite3G
6d890 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 etVarint(const u
6d8a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
6d8b0 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 u64 *);.SQLITE_P
6d8c0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
6d8d0 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 3GetVarint32(con
6d8e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
6d8f0 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51 4c 49 *, u32 *);.SQLI
6d900 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6d910 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
6d920 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 u64 v);../*.** T
6d930 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 72 he header of a r
6d940 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 20 6f ecord consists o
6d950 66 20 61 20 73 65 71 75 65 6e 63 65 20 76 61 72 f a sequence var
6d960 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
6d970 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 egers..** These
6d980 69 6e 74 65 67 65 72 73 20 61 72 65 20 61 6c 6d integers are alm
6d990 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c ost always small
6d9a0 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 65 64 and are encoded
6d9b0 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 as a single byt
6d9c0 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 e..** The follow
6d9d0 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b 65 20 ing macros take
6d9e0 61 64 76 61 6e 74 61 67 65 20 74 68 69 73 20 66 advantage this f
6d9f0 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65 20 61 act to provide a
6da00 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 fast encode.**
6da10 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20 74 68 and decode of th
6da20 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 61 20 e integers in a
6da30 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e 20 20 record header.
6da40 49 74 20 69 73 20 66 61 73 74 65 72 20 66 6f 72 It is faster for
6da50 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 the common.** c
6da60 61 73 65 20 77 68 65 72 65 20 74 68 65 20 69 6e ase where the in
6da70 74 65 67 65 72 20 69 73 20 61 20 73 69 6e 67 6c teger is a singl
6da80 65 20 62 79 74 65 2e 20 20 49 74 20 69 73 20 61 e byte. It is a
6da90 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 20 77 little slower w
6daa0 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 hen the.** integ
6dab0 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72 er is two or mor
6dac0 65 20 62 79 74 65 73 2e 20 20 42 75 74 20 6f 76 e bytes. But ov
6dad0 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61 73 74 erall it is fast
6dae0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f er..**.** The fo
6daf0 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69 llowing expressi
6db00 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61 6c 65 ons are equivale
6db10 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 nt:.**.** x
6db20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 = sqlite3GetVari
6db30 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b 0a 2a nt32( A, &B );.*
6db40 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 * x = sqlite
6db50 33 50 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 3PutVarint32( A,
6db60 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 B );.**.**
6db70 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 x = getVarint32(
6db80 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20 20 20 A, B );.**
6db90 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 x = putVarint32(
6dba0 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 A, B );.**.*/.#
6dbb0 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 define getVarint
6dbc0 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 2a 32(A,B) (u8)((*
6dbd0 28 41 29 3c 28 75 38 29 30 78 38 30 29 20 3f 20 (A)<(u8)0x80) ?
6dbe0 28 28 42 29 20 3d 20 28 75 33 32 29 2a 28 41 29 ((B) = (u32)*(A)
6dbf0 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 47 65 74 ),1 : sqlite3Get
6dc00 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 75 Varint32((A), (u
6dc10 33 32 20 2a 29 26 28 42 29 29 29 0a 23 64 65 66 32 *)&(B))).#def
6dc20 69 6e 65 20 70 75 74 56 61 72 69 6e 74 33 32 28 ine putVarint32(
6dc30 41 2c 42 29 20 20 28 75 38 29 28 28 28 75 33 32 A,B) (u8)(((u32
6dc40 29 28 42 29 3c 28 75 33 32 29 30 78 38 30 29 20 )(B)<(u32)0x80)
6dc50 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 ? (*(A) = (unsig
6dc60 6e 65 64 20 63 68 61 72 29 28 42 29 29 2c 31 20 ned char)(B)),1
6dc70 3a 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 : sqlite3PutVari
6dc80 6e 74 33 32 28 28 41 29 2c 20 28 42 29 29 29 0a nt32((A), (B))).
6dc90 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e #define getVarin
6dca0 74 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 56 t sqlite3GetV
6dcb0 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 arint.#define pu
6dcc0 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 tVarint sqlit
6dcd0 65 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 e3PutVarint...SQ
6dce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6dcf0 64 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 d sqlite3IndexAf
6dd00 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a finityStr(Vdbe *
6dd10 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 , Index *);.SQLI
6dd20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6dd30 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 sqlite3TableAffi
6dd40 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 nityStr(Vdbe *,
6dd50 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
6dd60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
6dd70 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
6dd80 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
6dd90 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 , char aff2);.SQ
6dda0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ddb0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 sqlite3IndexAff
6ddc0 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 inityOk(Expr *pE
6ddd0 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 xpr, char idx_af
6dde0 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f finity);.SQLITE_
6ddf0 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
6de00 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
6de10 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 (Expr *pExpr);.S
6de20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6de30 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 t sqlite3Atoi64(
6de40 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 const char*, i64
6de50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6de60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6de70 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 rror(sqlite3*, i
6de80 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c nt, const char*,
6de90 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
6dea0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6deb0 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 e3HexToBlob(sqli
6dec0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 te3*, const char
6ded0 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c *z, int n);.SQL
6dee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6def0 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 sqlite3TwoPartNa
6df00 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 me(Parse *, Toke
6df10 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f n *, Token *, To
6df20 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f ken **);.SQLITE_
6df30 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
6df40 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 ar *sqlite3ErrSt
6df50 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 r(int);.SQLITE_P
6df60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6df70 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 e3ReadSchema(Par
6df80 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c se *pParse);.SQL
6df90 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c ITE_PRIVATE Coll
6dfa0 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 Seq *sqlite3Find
6dfb0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a CollSeq(sqlite3*
6dfc0 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 ,u8 enc, const c
6dfd0 68 61 72 20 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a har *,int,int);.
6dfe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
6dff0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c ollSeq *sqlite3L
6e000 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 ocateCollSeq(Par
6e010 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 se *pParse, cons
6e020 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 t char *zName, i
6e030 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 nt nName);.SQLIT
6e040 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
6e050 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f q *sqlite3ExprCo
6e060 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 llSeq(Parse *pPa
6e070 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
6e080 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e090 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
6e0a0 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 xprSetColl(Parse
6e0b0 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
6e0c0 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 , Token *);.SQLI
6e0d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6e0e0 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 qlite3CheckCollS
6e0f0 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c eq(Parse *, Coll
6e100 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Seq *);.SQLITE_P
6e110 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6e120 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d e3CheckObjectNam
6e130 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 e(Parse *, const
6e140 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
6e150 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6e160 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
6e170 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 ges(sqlite3 *, i
6e180 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 nt);..SQLITE_PRI
6e190 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 VATE const void
6e1a0 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 *sqlite3ValueTex
6e1b0 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t(sqlite3_value*
6e1c0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 , u8);.SQLITE_PR
6e1d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6e1e0 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 3ValueBytes(sqli
6e1f0 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b te3_value*, u8);
6e200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e210 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
6e220 65 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f eSetStr(sqlite3_
6e230 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e value*, int, con
6e240 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 st void *,u8, .
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e260 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 void(*)(v
6e270 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
6e280 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e290 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c te3ValueFree(sql
6e2a0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
6e2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
6e2c0 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 ite3_value *sqli
6e2d0 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 te3ValueNew(sqli
6e2e0 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 te3 *);.SQLITE_P
6e2f0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6e300 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c ite3Utf16to8(sql
6e310 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f ite3 *, const vo
6e320 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 id*, int);.SQLIT
6e330 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6e340 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 lite3ValueFromEx
6e350 70 72 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 pr(sqlite3 *, Ex
6e360 70 72 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 pr *, u8, u8, sq
6e370 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b lite3_value **);
6e380 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e390 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
6e3a0 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 eApplyAffinity(s
6e3b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 qlite3_value *,
6e3c0 75 38 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 u8, u8);.#ifndef
6e3d0 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 SQLITE_AMALGAMA
6e3e0 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 TION.SQLITE_PRIV
6e3f0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ATE const unsign
6e400 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 ed char sqlite3U
6e410 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 pperToLower[];.S
6e420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
6e430 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
6e440 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 r sqlite3CtypeMa
6e450 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 p[];.SQLITE_PRIV
6e460 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 ATE SQLITE_WSD s
6e470 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e truct Sqlite3Con
6e480 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 fig sqlite3Confi
6e490 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 g;.SQLITE_PRIVAT
6e4a0 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e E SQLITE_WSD Fun
6e4b0 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 cDefHash sqlite3
6e4c0 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b GlobalFunctions;
6e4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e4e0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 int sqlite3Pendi
6e4f0 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53 ngByte;.#endif.S
6e500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e510 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 id sqlite3RootPa
6e520 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 geMoved(Db*, int
6e530 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6e540 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e550 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 te3Reindex(Parse
6e560 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
6e570 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e580 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
6e590 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 lterFunctions(sq
6e5a0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
6e5b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6e5c0 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 ite3AlterRenameT
6e5d0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 able(Parse*, Src
6e5e0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a List*, Token*);.
6e5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6e600 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b nt sqlite3GetTok
6e610 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 en(const unsigne
6e620 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 d char *, int *)
6e630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 void sqlite3Nes
6e650 74 65 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c tedParse(Parse*,
6e660 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e const char*, ..
6e670 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
6e680 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6e690 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 xpirePreparedSta
6e6a0 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a tements(sqlite3*
6e6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e6c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
6e6d0 64 65 53 75 62 73 65 6c 65 63 74 28 50 61 72 73 deSubselect(Pars
6e6e0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 e *, Expr *, int
6e6f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6e700 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e710 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 50 61 te3SelectPrep(Pa
6e720 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e rse*, Select*, N
6e730 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 ameContext*);.SQ
6e740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6e750 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
6e760 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e xprNames(NameCon
6e770 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 text*, Expr*);.S
6e780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e790 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 id sqlite3Resolv
6e7a0 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 eSelectNames(Par
6e7b0 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 se*, Select*, Na
6e7c0 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c meContext*);.SQL
6e7d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6e7e0 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
6e7f0 64 65 72 47 72 6f 75 70 42 79 28 50 61 72 73 65 derGroupBy(Parse
6e800 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 *, Select*, Expr
6e810 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 List*, const cha
6e820 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6e830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e840 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 ColumnDefault(Vd
6e850 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 be *, Table *, i
6e860 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e870 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e880 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f AlterFinishAddCo
6e890 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f lumn(Parse *, To
6e8a0 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ken *);.SQLITE_P
6e8b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e8c0 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 te3AlterBeginAdd
6e8d0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 Column(Parse *,
6e8e0 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 SrcList *);.SQLI
6e8f0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
6e900 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f eq *sqlite3GetCo
6e910 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 llSeq(sqlite3*,
6e920 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 CollSeq *, const
6e930 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 char *, int);.S
6e940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
6e950 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 ar sqlite3Affini
6e960 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b tyType(const Tok
6e970 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
6e980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6e990 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 3Analyze(Parse*,
6e9a0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
6e9b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e9c0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f int sqlite3Invo
6e9d0 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 keBusyHandler(Bu
6e9e0 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c syHandler*);.SQL
6e9f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ea00 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 sqlite3FindDb(sq
6ea10 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b lite3*, Token*);
6ea20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ea30 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 int sqlite3FindD
6ea40 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c bName(sqlite3 *,
6ea50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
6ea60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6ea70 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 nt sqlite3Analys
6ea80 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c isLoad(sqlite3*,
6ea90 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 int iDB);.SQLITE
6eaa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6eab0 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 lite3DefaultRowE
6eac0 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 st(Index*);.SQLI
6ead0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6eae0 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c sqlite3RegisterL
6eaf0 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c ikeFunctions(sql
6eb00 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
6eb10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6eb20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e sqlite3IsLikeFun
6eb30 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 ction(sqlite3*,E
6eb40 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 xpr*,int*,char*)
6eb50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6eb60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e void sqlite3Min
6eb70 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 imumFileFormat(P
6eb80 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 arse*, int, int)
6eb90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6eba0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 void sqlite3Sch
6ebb0 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b emaFree(void *);
6ebc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ebd0 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 Schema *sqlite3S
6ebe0 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 chemaGet(sqlite3
6ebf0 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 *, Btree *);.SQ
6ec00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ec10 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
6ec20 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 Index(sqlite3 *d
6ec30 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 b, Schema *);.SQ
6ec40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 LITE_PRIVATE Key
6ec50 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 Info *sqlite3Ind
6ec60 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 exKeyinfo(Parse
6ec70 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c *, Index *);.SQL
6ec80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ec90 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e sqlite3CreateFun
6eca0 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e c(sqlite3 *, con
6ecb0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
6ecc0 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 int, void *, .
6ecd0 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 void (*)(sqlite3
6ece0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
6ecf0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c lite3_value **),
6ed00 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 . void (*)(sqli
6ed10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 te3_context*,int
6ed20 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a ,sqlite3_value *
6ed30 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c *), void (*)(sql
6ed40 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b ite3_context*));
6ed50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ed60 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 int sqlite3ApiEx
6ed70 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 it(sqlite3 *db,
6ed80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6ed90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6eda0 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 OpenTempDatabase
6edb0 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 (Parse *);..SQLI
6edc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6edd0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
6ede0 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 nit(StrAccum*, c
6edf0 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b har*, int, int);
6ee00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ee10 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 void sqlite3StrA
6ee20 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 ccumAppend(StrAc
6ee30 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a cum*,const char*
6ee40 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6ee50 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
6ee60 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 te3StrAccumFinis
6ee70 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 h(StrAccum*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ee90 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 d sqlite3StrAccu
6eea0 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a mReset(StrAccum*
6eeb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6eec0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
6eed0 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c lectDestInit(Sel
6eee0 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 ectDest*,int,int
6eef0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
6ef00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
6ef10 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c ackupRestart(sql
6ef20 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a ite3_backup *);.
6ef30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ef40 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
6ef50 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f pUpdate(sqlite3_
6ef60 62 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 backup *, Pgno,
6ef70 63 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a const u8 *);../*
6ef80 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
6ef90 65 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 e to the LEMON-g
6efa0 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a enerated parser.
6efb0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
6efc0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 E void *sqlite3P
6efd0 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a arserAlloc(void*
6efe0 28 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 (*)(size_t));.SQ
6eff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f000 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 d sqlite3ParserF
6f010 72 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 ree(void*, void(
6f020 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
6f030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f040 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f sqlite3Parser(vo
6f050 69 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c id*, int, Token,
6f060 20 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 Parse*);.#ifdef
6f070 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b YYTRACKMAXSTACK
6f080 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 DEPTH.SQLITE_PRI
6f090 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6f0a0 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 e3ParserStackPea
6f0b0 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 k(void*);.#endif
6f0c0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6f0d0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 6f int sqlite3Auto
6f0e0 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 LoadExtensions(s
6f0f0 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 qlite3*);.#ifnde
6f100 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
6f110 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c AD_EXTENSION.SQL
6f120 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
6f130 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 id sqlite3CloseE
6f140 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 xtensions(sqlite
6f150 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 3*);.#else.# def
6f160 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 ine sqlite3Close
6f170 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 Extensions(X).#e
6f180 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
6f190 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
6f1a0 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 _CACHE.SQLITE_PR
6f1b0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6f1c0 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 ite3TableLock(Pa
6f1d0 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c rse *, int, int,
6f1e0 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 u8, const char
6f1f0 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
6f200 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ine sqlite3Table
6f210 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a Lock(v,w,x,y,z).
6f220 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
6f230 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
6f240 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
6f250 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 sqlite3Utf8To8(u
6f260 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a nsigned char*);.
6f270 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
6f280 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
6f290 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e ALTABLE.# defin
6f2a0 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 e sqlite3VtabCle
6f2b0 61 72 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 ar(X).# define
6f2c0 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 sqlite3VtabSync(
6f2d0 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 X,Y) SQLITE_OK.#
6f2e0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
6f2f0 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a VtabRollback(X).
6f300 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
6f310 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 3VtabCommit(X).#
6f320 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
6f330 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30 VtabInSync(db) 0
6f340 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 .#else.SQLITE_PR
6f350 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 IVATE void sq
6f360 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 lite3VtabClear(T
6f370 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
6f380 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 RIVATE int sq
6f390 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 lite3VtabSync(sq
6f3a0 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 lite3 *db, char
6f3b0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6f3c0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 ATE int sqlit
6f3d0 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 e3VtabRollback(s
6f3e0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
6f3f0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
6f400 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f nt sqlite3VtabCo
6f410 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 mmit(sqlite3 *db
6f420 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c );.# define sql
6f430 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 ite3VtabInSync(d
6f440 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e b) ((db)->nVTran
6f450 73 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 s>0 && (db)->aVT
6f460 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a rans==0).#endif.
6f470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f480 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d oid sqlite3VtabM
6f490 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 akeWritable(Pars
6f4a0 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 e*,Table*);.SQLI
6f4b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f4c0 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
6f4d0 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a sqlite3_vtab*);.
6f4e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f4f0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 oid sqlite3VtabU
6f500 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c 20 nlock(sqlite3*,
6f510 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a sqlite3_vtab*);.
6f520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f530 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 oid sqlite3VtabB
6f540 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a eginParse(Parse*
6f550 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a , Token*, Token*
6f560 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
6f570 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f580 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 qlite3VtabFinish
6f590 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f Parse(Parse*, To
6f5a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6f5b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f5c0 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 e3VtabArgInit(Pa
6f5d0 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
6f5e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f5f0 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 e3VtabArgExtend(
6f600 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Parse*, Token*);
6f610 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f620 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 int sqlite3VtabC
6f630 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 allCreate(sqlite
6f640 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 3*, int, const c
6f650 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b har *, char **);
6f660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f670 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 int sqlite3VtabC
6f680 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 allConnect(Parse
6f690 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 *, Table*);.SQLI
6f6a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f6b0 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 qlite3VtabCallDe
6f6c0 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 stroy(sqlite3*,
6f6d0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 int, const char
6f6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f6f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 TE int sqlite3Vt
6f700 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 abBegin(sqlite3
6f710 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 *, sqlite3_vtab
6f720 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f730 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 TE FuncDef *sqli
6f740 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 te3VtabOverloadF
6f750 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 unction(sqlite3
6f760 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 *,FuncDef*, int
6f770 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51 nArg, Expr*);.SQ
6f780 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f790 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 d sqlite3Invalid
6f7a0 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
6f7b0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
6f7c0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a lite3_value**);.
6f7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6f7e0 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 nt sqlite3Transf
6f7f0 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 erBindings(sqlit
6f800 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 e3_stmt *, sqlit
6f810 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 e3_stmt *);.SQLI
6f820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f830 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 qlite3Reprepare(
6f840 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
6f850 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f860 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b te3ExprListCheck
6f870 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 Length(Parse*, E
6f880 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 xprList*, const
6f890 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
6f8a0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
6f8b0 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
6f8c0 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 pareCollSeq(Pars
6f8d0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 e *, Expr *, Exp
6f8e0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r *);.SQLITE_PRI
6f8f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6f900 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e TempInMemory(con
6f910 73 74 20 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 0a st sqlite3*);...
6f920 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65 ./*.** Available
6f930 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 fault injectors
6f940 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d . Should be num
6f950 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 bered beginning
6f960 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 with 0..*/.#defi
6f970 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 ne SQLITE_FAULTI
6f980 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 NJECTOR_MALLOC
6f990 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
6f9a0 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f ITE_FAULTINJECTO
6f9b0 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a R_COUNT 1..
6f9c0 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 /*.** The interf
6f9d0 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 20 ace to the code
6f9e0 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 20 in fault.c used
6f9f0 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 20 for identifying
6fa00 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c "benign".** mall
6fa10 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 69 oc failures. Thi
6fa20 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e s is only presen
6fa30 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 t if SQLITE_OMIT
6fa40 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a _BUILTIN_TEST.**
6fa50 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e is not defined.
6fa60 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
6fa70 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
6fa80 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
6fa90 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
6faa0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
6fab0 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 loc(void);.SQLIT
6fac0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6fad0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
6fae0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 nMalloc(void);.#
6faf0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 else. #define s
6fb00 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
6fb10 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66 nMalloc(). #def
6fb20 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 65 ine sqlite3EndBe
6fb30 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e nignMalloc().#en
6fb40 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f dif..#define IN_
6fb50 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 20 INDEX_ROWID
6fb60 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
6fb70 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 20 IN_INDEX_EPH
6fb80 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 2.#defi
6fb90 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 ne IN_INDEX_INDE
6fba0 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 51 X 3.SQ
6fbb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6fbc0 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e sqlite3FindInIn
6fbd0 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 70 dex(Parse *, Exp
6fbe0 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 r *, int*);..#if
6fbf0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
6fc00 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 E_ATOMIC_WRITE.S
6fc10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6fc20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e int sqlite3Journ
6fc30 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 alOpen(sqlite3_v
6fc40 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 fs *, const char
6fc50 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 *, sqlite3_file
6fc60 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 *, int, int);.S
6fc70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6fc80 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e int sqlite3Journ
6fc90 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 alSize(sqlite3_v
6fca0 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 fs *);.SQLITE_PR
6fcb0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
6fcc0 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 te3JournalCreate
6fcd0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
6fce0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e ;.#else. #defin
6fcf0 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c e sqlite3Journal
6fd00 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66 Size(pVfs) ((pVf
6fd10 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 s)->szOsFile).#e
6fd20 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
6fd30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6fd40 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 3MemJournalOpen(
6fd50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b sqlite3_file *);
6fd60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fd70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f int sqlite3MemJo
6fd80 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b urnalSize(void);
6fd90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fda0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d int sqlite3IsMem
6fdb0 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f Journal(sqlite3_
6fdc0 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51 file *);..#if SQ
6fdd0 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
6fde0 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 PTH>0.SQLITE_PRI
6fdf0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
6fe00 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 te3ExprSetHeight
6fe10 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
6fe20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45 Expr *p);.SQLITE
6fe30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6fe40 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 qlite3SelectExpr
6fe50 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 29 Height(Select *)
6fe60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fe70 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 int sqlite3Ex
6fe80 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 prCheckHeight(Pa
6fe90 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 rse*, int);.#els
6fea0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
6feb0 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 te3ExprSetHeight
6fec0 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20 (x,y). #define
6fed0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
6fee0 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 23 rHeight(x) 0. #
6fef0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 define sqlite3Ex
6ff00 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 2c prCheckHeight(x,
6ff10 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 y).#endif..SQLIT
6ff20 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
6ff30 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f lite3Get4byte(co
6ff40 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 nst u8*);.SQLITE
6ff50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ff60 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38 lite3Put4byte(u8
6ff70 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 *, u32);..#ifdef
6ff80 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 SQLITE_ENABLE_U
6ff90 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c NLOCK_NOTIFY.SQL
6ffa0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
6ffb0 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 id sqlite3Connec
6ffc0 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 tionBlocked(sqli
6ffd0 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a te3 *, sqlite3 *
6ffe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fff0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
70000 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b ConnectionUnlock
70010 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b ed(sqlite3 *db);
70020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70030 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f void sqlite3Co
70040 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 nnectionClosed(s
70050 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c qlite3 *db);.#el
70060 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
70070 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
70080 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 65 ocked(x,y). #de
70090 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e fine sqlite3Conn
700a0 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 ectionUnlocked(x
700b0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
700c0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f te3ConnectionClo
700d0 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a 0a sed(x).#endif...
700e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 #ifdef SQLITE_SS
700f0 45 0a 23 69 6e 63 6c 75 64 65 20 22 73 73 65 49 E.#include "sseI
70100 6e 74 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 23 69 nt.h".#endif..#i
70110 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
70120 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
70130 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 void sqlite3P
70140 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a arserTrace(FILE*
70150 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 , char *);.#endi
70160 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 f../*.** If the
70170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f SQLITE_ENABLE IO
70180 54 52 41 43 45 20 65 78 69 73 74 73 20 74 68 65 TRACE exists the
70190 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 n the global var
701a0 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 iable.** sqlite3
701b0 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 IoTrace is a poi
701c0 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 nter to a printf
701d0 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 -like routine us
701e0 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 ed to.** print I
701f0 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 /O tracing messa
70200 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 ges. .*/.#ifdef
70210 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f SQLITE_ENABLE_IO
70220 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 TRACE.# define I
70230 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 20 73 OTRACE(A) if( s
70240 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b qlite3IoTrace ){
70250 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 sqlite3IoTrace
70260 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 A; }.SQLITE_PRIV
70270 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
70280 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c e3VdbeIOTraceSql
70290 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
702a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
702b0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 qlite3IoTrace)(c
702c0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
702d0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
702e0 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 IOTRACE(A).# def
702f0 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 ine sqlite3VdbeI
70300 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e OTraceSql(X).#en
70310 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a dif..#endif../**
70320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
70330 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
70340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
70370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
70380 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 in file global.c
70390 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
703a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
703b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
703c0 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a ** 2008 June 13.
703d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
703e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
703f0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
70400 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
70410 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
70420 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
70430 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
70440 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
70450 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
70460 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
70470 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
70480 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
70490 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
704a0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
704b0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
704c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
704d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
704e0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
704f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
70530 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
70540 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
70550 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 ons of global va
70560 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 riables and cont
70570 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ants..**.** $Id:
70580 20 67 6c 6f 62 61 6c 2e 63 2c 76 20 31 2e 31 32 global.c,v 1.12
70590 20 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33 2009/02/05 16:3
705a0 31 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 1:46 drh Exp $.*
705b0 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 /.../* An array
705c0 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 to map all upper
705d0 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73 -case characters
705e0 20 69 6e 74 6f 20 74 68 65 69 72 20 63 6f 72 72 into their corr
705f0 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 esponding.** low
70600 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 er-case characte
70610 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 r. .**.** SQLite
70620 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 only considers
70630 55 53 2d 41 53 43 49 49 20 28 6f 72 20 45 42 43 US-ASCII (or EBC
70640 44 49 43 29 20 63 68 61 72 61 63 74 65 72 73 2e DIC) characters.
70650 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 We do not.** h
70660 61 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e 76 65 andle case conve
70670 72 73 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 55 rsions for the U
70680 54 46 20 63 68 61 72 61 63 74 65 72 20 73 65 74 TF character set
70690 20 73 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 since the table
706a0 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72 s.** involved ar
706b0 65 20 6e 65 61 72 6c 79 20 61 73 20 62 69 67 20 e nearly as big
706c0 6f 72 20 62 69 67 67 65 72 20 74 68 61 6e 20 53 or bigger than S
706d0 51 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f QLite itself..*/
706e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
706f0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
70700 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 har sqlite3Upper
70710 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 ToLower[] = {.#i
70720 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 fdef SQLITE_ASCI
70730 49 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 I. 0, 1,
70740 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 2, 3, 4, 5,
70750 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 6, 7, 8, 9, 1
70760 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 0, 11, 12, 13, 1
70770 34 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 4, 15, 16, 17,.
70780 20 20 20 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 18, 19, 20,
70790 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 21, 22, 23, 24,
707a0 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 25, 26, 27, 28,
707b0 32 39 2c 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 29, 30, 31, 32,
707c0 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 33, 34, 35,.
707d0 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 36, 37, 38, 39,
707e0 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 40, 41, 42, 43,
707f0 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 44, 45, 46, 47,
70800 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 48, 49, 50, 51,
70810 20 35 32 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 52, 53,. 54
70820 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 , 55, 56, 57, 58
70830 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 , 59, 60, 61, 62
70840 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 , 63, 64, 97, 98
70850 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 , 99,100,101,102
70860 2c 31 30 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 ,103,. 104,10
70870 35 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 5,106,107,108,10
70880 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 9,110,111,112,11
70890 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 3,114,115,116,11
708a0 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 7,118,119,120,12
708b0 31 2c 0a 20 20 20 20 31 32 32 2c 20 39 31 2c 20 1,. 122, 91,
708c0 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 92, 93, 94, 95,
708d0 39 36 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 96, 97, 98, 99,1
708e0 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 00,101,102,103,1
708f0 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 04,105,106,107,.
70900 20 20 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 108,109,110,
70910 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 111,112,113,114,
70920 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 115,116,117,118,
70930 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 119,120,121,122,
70940 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 123,124,125,.
70950 20 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 126,127,128,129
70960 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 ,130,131,132,133
70970 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 ,134,135,136,137
70980 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 ,138,139,140,141
70990 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 ,142,143,. 14
709a0 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 4,145,146,147,14
709b0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 8,149,150,151,15
709c0 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 2,153,154,155,15
709d0 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 6,157,158,159,16
709e0 30 2c 31 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 0,161,. 162,1
709f0 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 63,164,165,166,1
70a00 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 67,168,169,170,1
70a10 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 71,172,173,174,1
70a20 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 75,176,177,178,1
70a30 37 39 2c 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 79,. 180,181,
70a40 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 182,183,184,185,
70a50 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 186,187,188,189,
70a60 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 190,191,192,193,
70a70 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 194,195,196,197,
70a80 0a 20 20 20 20 31 39 38 2c 31 39 39 2c 32 30 30 . 198,199,200
70a90 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 ,201,202,203,204
70aa0 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 ,205,206,207,208
70ab0 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 ,209,210,211,212
70ac0 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 ,213,214,215,.
70ad0 20 20 32 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 216,217,218,21
70ae0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 9,220,221,222,22
70af0 33 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 3,224,225,226,22
70b00 37 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 7,228,229,230,23
70b10 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 1,232,233,. 2
70b20 33 34 2c 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 34,235,236,237,2
70b30 33 38 2c 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 38,239,240,241,2
70b40 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 42,243,244,245,2
70b50 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 46,247,248,249,2
70b60 35 30 2c 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 50,251,. 252,
70b70 32 35 33 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 253,254,255.#end
70b80 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
70b90 5f 45 42 43 44 49 43 0a 20 20 20 20 20 20 30 2c _EBCDIC. 0,
70ba0 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 1, 2, 3, 4,
70bb0 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 5, 6, 7, 8,
70bc0 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 9, 10, 11, 12,
70bd0 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 13, 14, 15, /*
70be0 30 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 0x */. 16, 1
70bf0 37 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 7, 18, 19, 20, 2
70c00 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 1, 22, 23, 24, 2
70c10 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 5, 26, 27, 28, 2
70c20 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 9, 30, 31, /* 1x
70c30 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33 33 2c */. 32, 33,
70c40 20 33 34 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 34, 35, 36, 37,
70c50 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 38, 39, 40, 41,
70c60 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 42, 43, 44, 45,
70c70 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 46, 47, /* 2x *
70c80 2f 0a 20 20 20 20 20 34 38 2c 20 34 39 2c 20 35 /. 48, 49, 5
70c90 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 0, 51, 52, 53, 5
70ca0 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 4, 55, 56, 57, 5
70cb0 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 8, 59, 60, 61, 6
70cc0 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 2, 63, /* 3x */.
70cd0 20 20 20 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 64, 65, 66,
70ce0 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 67, 68, 69, 70,
70cf0 20 37 31 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 71, 72, 73, 74,
70d00 20 37 35 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 75, 76, 77, 78,
70d10 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 79, /* 4x */.
70d20 20 20 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 80, 81, 82, 8
70d30 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 3, 84, 85, 86, 8
70d40 37 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 7, 88, 89, 90, 9
70d50 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 1, 92, 93, 94, 9
70d60 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 5, /* 5x */.
70d70 20 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 96, 97, 66, 67,
70d80 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 68, 69, 70, 71,
70d90 20 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 72, 73,106,107,
70da0 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 108,109,110,111,
70db0 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 /* 6x */. 11
70dc0 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 2, 81, 82, 83, 8
70dd0 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 4, 85, 86, 87, 8
70de0 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 8, 89,122,123,12
70df0 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 4,125,126,127, /
70e00 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c * 7x */. 128,
70e10 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 129,130,131,132,
70e20 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 133,134,135,136,
70e30 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 137,138,139,140,
70e40 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 141,142,143, /*
70e50 38 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 8x */. 144,14
70e60 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 5,146,147,148,14
70e70 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 9,150,151,152,15
70e80 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 3,154,155,156,15
70e90 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 7,156,159, /* 9x
70ea0 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36 31 2c */. 160,161,
70eb0 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 162,163,164,165,
70ec0 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 166,167,168,169,
70ed0 31 37 30 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 170,171,140,141,
70ee0 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 142,175, /* Ax *
70ef0 2f 0a 20 20 20 20 31 37 36 2c 31 37 37 2c 31 37 /. 176,177,17
70f00 38 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 8,179,180,181,18
70f10 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 2,183,184,185,18
70f20 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 6,187,188,189,19
70f30 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 0,191, /* Bx */.
70f40 20 20 20 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 192,129,130,
70f50 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 131,132,133,134,
70f60 31 33 35 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 135,136,137,202,
70f70 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 203,204,205,206,
70f80 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 207, /* Cx */.
70f90 20 20 32 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 208,145,146,14
70fa0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 7,148,149,150,15
70fb0 31 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 1,152,153,218,21
70fc0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 9,220,221,222,22
70fd0 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 3, /* Dx */.
70fe0 32 32 34 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 224,225,162,163,
70ff0 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 164,165,166,167,
71000 31 36 38 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 168,169,232,203,
71010 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 204,205,206,207,
71020 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 /* Ex */. 23
71030 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 9,240,241,242,24
71040 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 3,244,245,246,24
71050 37 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 7,248,249,219,22
71060 30 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 0,221,222,255, /
71070 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d * Fx */.#endif.}
71080 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
71090 6c 6f 77 69 6e 67 20 32 35 36 20 62 79 74 65 20 lowing 256 byte
710a0 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 lookup table is
710b0 75 73 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 used to support
710c0 53 51 4c 69 74 65 73 20 62 75 69 6c 74 2d 69 6e SQLites built-in
710d0 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 73 20 .** equivalents
710e0 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
710f0 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 standard librar
71100 79 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a y functions:.**.
71110 2a 2a 20 20 20 69 73 73 70 61 63 65 28 29 20 20 ** isspace()
71120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71130 20 20 20 20 20 20 30 78 30 31 0a 2a 2a 20 20 20 0x01.**
71140 69 73 61 6c 70 68 61 28 29 20 20 20 20 20 20 20 isalpha()
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71160 20 30 78 30 32 0a 2a 2a 20 20 20 69 73 64 69 67 0x02.** isdig
71170 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 it()
71180 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 34 0x04
71190 0a 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28 29 20 .** isalnum()
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711b0 20 20 20 20 20 20 20 30 78 30 36 0a 2a 2a 20 20 0x06.**
711c0 20 69 73 78 64 69 67 69 74 28 29 20 20 20 20 20 isxdigit()
711d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711e0 20 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70 0x08.** toup
711f0 70 65 72 28 29 20 20 20 20 20 20 20 20 20 20 20 per()
71200 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 32 0x2
71210 30 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 0.**.** Bit 0x20
71220 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d is set if the m
71230 61 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20 apped character
71240 72 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61 requires transla
71250 74 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a tion to upper.**
71260 20 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 case. i.e. if t
71270 68 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20 he character is
71280 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 a lower-case ASC
71290 49 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a II character..**
712a0 20 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72 If x is a lower
712b0 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 -case ASCII char
712c0 61 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20 acter, then its
712d0 75 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76 upper-case equiv
712e0 61 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d alent.** is (x -
712f0 20 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 0x20). Therefor
71300 65 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 e toupper() can
71310 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 be implemented a
71320 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 s:.**.** (x &
71330 7e 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a ~(map[x]&0x20)).
71340 2a 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 **.** Standard f
71350 75 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 unction tolower(
71360 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 ) is implemented
71370 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
71380 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d e3UpperToLower[]
71390 0a 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 .** array. tolow
713a0 65 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72 er() is used mor
713b0 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 e often than tou
713c0 70 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65 pper() by SQLite
713d0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73 ..**.** SQLite's
713e0 20 76 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64 versions are id
713f0 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 entical to the s
71400 74 61 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73 tandard versions
71410 20 61 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c assuming a.** l
71420 6f 63 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68 ocale of "C". Th
71430 65 79 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 ey are implement
71440 65 64 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20 ed as macros in
71450 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a sqliteInt.h..*/.
71460 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 #ifdef SQLITE_AS
71470 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 CII.SQLITE_PRIVA
71480 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 TE const unsigne
71490 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 d char sqlite3Ct
714a0 79 70 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a ypeMap[256] = {.
714b0 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
714c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
714d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
714e0 2c 20 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20 , /* 00..07
714f0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
71500 30 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 00, 0x01, 0x01,
71510 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 0x01, 0x01, 0x01
71520 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
71530 2a 20 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e * 08..0f ....
71540 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
71550 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71560 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71570 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30 00, 0x00, /* 10
71580 2e 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..17 ........
71590 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
715a0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
715b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
715c0 30 78 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 0x00, /* 18..1f
715d0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
715e0 20 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 0x01, 0x00, 0x
715f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71600 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71610 2c 20 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20 , /* 20..27
71620 20 21 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78 !"#$%&' */. 0x
71630 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71640 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71650 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
71660 2a 20 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b * 28..2f ()*+
71670 2c 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20 ,-./ */. 0x0c,
71680 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 0x0c, 0x0c, 0x0c
71690 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 , 0x0c, 0x0c, 0x
716a0 30 63 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30 0c, 0x0c, /* 30
716b0 2e 2e 33 37 20 20 20 20 30 31 32 33 34 35 36 37 ..37 01234567
716c0 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 */. 0x0c, 0x0c
716d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
716e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
716f0 30 78 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 0x00, /* 38..3f
71700 20 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 89:;<=>? */.
71710 0a 20 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 . 0x00, 0x0a, 0
71720 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c x0a, 0x0a, 0x0a,
71730 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 0x0a, 0x0a, 0x0
71740 32 2c 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 2, /* 40..47
71750 20 40 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30 @ABCDEFG */. 0
71760 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
71770 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
71780 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2, 0x02, 0x02,
71790 2f 2a 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a /* 48..4f HIJ
717a0 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c KLMNO */. 0x02,
717b0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
717c0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
717d0 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35 x02, 0x02, /* 5
717e0 30 2e 2e 35 37 20 20 20 20 50 51 52 53 54 55 56 0..57 PQRSTUV
717f0 57 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 W */. 0x02, 0x0
71800 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 2, 0x02, 0x00, 0
71810 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71820 20 30 78 30 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 0x00, /* 58..5
71830 66 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f f XYZ[\]^_ */
71840 0a 20 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30 . 0x00, 0x2a, 0
71850 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c x2a, 0x2a, 0x2a,
71860 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 0x2a, 0x2a, 0x2
71870 32 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20 2, /* 60..67
71880 20 60 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30 `abcdefg */. 0
71890 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
718a0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
718b0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2, 0x22, 0x22,
718c0 2f 2a 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a /* 68..6f hij
718d0 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c klmno */. 0x22,
718e0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
718f0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
71900 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37 x22, 0x22, /* 7
71910 30 2e 2e 37 37 20 20 20 20 70 71 72 73 74 75 76 0..77 pqrstuv
71920 77 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 w */. 0x22, 0x2
71930 32 2c 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30 2, 0x22, 0x00, 0
71940 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71950 20 30 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 0x00, /* 78..7
71960 66 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f f xyz{|}~. */
71970 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 .. 0x00, 0x00,
71980 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71990 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
719a0 30 30 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20 00, /* 80..87
719b0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
719c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
719d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
719e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
719f0 20 2f 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e /* 88..8f ..
71a00 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
71a10 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71a20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71a30 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
71a40 39 30 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 90..97 ......
71a50 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
71a60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71a70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71a80 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39 38 2e 2e , 0x00, /* 98..
71a90 39 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 9f ........ *
71aa0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
71ab0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71ac0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71ad0 30 30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20 00, /* a0..a7
71ae0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
71af0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71b00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71b10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71b20 20 2f 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e /* a8..af ..
71b30 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
71b40 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71b50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71b60 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
71b70 62 30 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e b0..b7 ......
71b80 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
71b90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71ba0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71bb0 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62 38 2e 2e , 0x00, /* b8..
71bc0 62 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a bf ........ *
71bd0 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c /.. 0x00, 0x00,
71be0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71bf0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71c00 78 30 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20 x00, /* c0..c7
71c10 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
71c20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71c30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71c40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71c50 20 20 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e /* c8..cf .
71c60 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
71c70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71c80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71c90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
71ca0 20 64 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e d0..d7 .....
71cb0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
71cc0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71cd0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71ce0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 38 2e 0, 0x00, /* d8.
71cf0 2e 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .df ........
71d00 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
71d10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71d20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71d30 78 30 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20 x00, /* e0..e7
71d40 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
71d50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71d60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71d70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71d80 20 20 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e /* e8..ef .
71d90 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
71da0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71db0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71dc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
71dd0 20 66 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e f0..f7 .....
71de0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
71df0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71e00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71e10 30 2c 20 30 78 30 30 20 20 20 2f 2a 20 66 38 2e 0, 0x00 /* f8.
71e20 2e 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .ff ........
71e30 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a */.};.#endif....
71e40 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
71e50 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e 20 63 6f ing singleton co
71e60 6e 74 61 69 6e 73 20 74 68 65 20 67 6c 6f 62 61 ntains the globa
71e70 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 l configuration
71e80 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 for.** the SQLit
71e90 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 53 51 e library..*/.SQ
71ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
71eb0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 ITE_WSD struct S
71ec0 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c qlite3Config sql
71ed0 69 74 65 33 43 6f 6e 66 69 67 20 3d 20 7b 0a 20 ite3Config = {.
71ee0 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 SQLITE_DEFAULT
71ef0 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20 2f 2a 20 _MEMSTATUS, /*
71f00 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 20 20 31 bMemstat */. 1
71f10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
71f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 43 6f /* bCo
71f30 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20 20 53 51 reMutex */. SQ
71f40 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
71f50 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62 46 75 6c =1, /* bFul
71f60 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 78 37 lMutex */. 0x7
71f70 66 66 66 66 66 66 65 2c 20 20 20 20 20 20 20 20 ffffffe,
71f80 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 53 74 72 /* mxStr
71f90 6c 65 6e 20 2a 2f 0a 20 20 20 31 30 30 2c 20 20 len */. 100,
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71fb0 20 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 /* szLookas
71fc0 69 64 65 20 2a 2f 0a 20 20 20 35 30 30 2c 20 20 ide */. 500,
71fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71fe0 20 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 /* nLookasi
71ff0 64 65 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c de */. {0,0,0,
72000 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 0,0,0,0,0},
72010 20 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b /* m */. {
72020 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 0,0,0,0,0,0,0,0,
72030 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 6d 75 74 0}, /* mut
72040 65 78 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c ex */. {0,0,0,
72050 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 0,0,0,0,0,0,0,0}
72060 2c 20 20 20 2f 2a 20 70 63 61 63 68 65 20 2a 2f , /* pcache */
72070 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 . (void*)0,
72080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
72090 2a 20 70 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c * pHeap */. 0,
720a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
720b0 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 48 65 61 /* nHea
720c0 70 20 2a 2f 0a 20 20 20 30 2c 20 30 2c 20 20 20 p */. 0, 0,
720d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
720e0 20 20 20 2f 2a 20 6d 6e 48 65 61 70 2c 20 6d 78 /* mnHeap, mx
720f0 48 65 61 70 20 2a 2f 0a 20 20 20 28 76 6f 69 64 Heap */. (void
72100 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 *)0,
72110 20 20 20 20 20 20 2f 2a 20 70 53 63 72 61 74 63 /* pScratc
72120 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 h */. 0,
72130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72140 20 20 20 2f 2a 20 73 7a 53 63 72 61 74 63 68 20 /* szScratch
72150 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
72160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72170 20 2f 2a 20 6e 53 63 72 61 74 63 68 20 2a 2f 0a /* nScratch */.
72180 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 (void*)0,
72190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
721a0 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 pPage */. 0,
721b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
721c0 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 50 61 67 /* szPag
721d0 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 e */. 0,
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
721f0 20 20 20 2f 2a 20 6e 50 61 67 65 20 2a 2f 0a 20 /* nPage */.
72200 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
72210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
72220 6d 78 50 61 72 73 65 72 53 74 61 63 6b 20 2a 2f mxParserStack */
72230 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
72240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
72250 2a 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 * sharedCacheEna
72260 62 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c bled */. /* Al
72270 6c 20 74 68 65 20 72 65 73 74 20 6e 65 65 64 20 l the rest need
72280 74 6f 20 61 6c 77 61 79 73 20 62 65 20 7a 65 72 to always be zer
72290 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 o */. 0,
722a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
722b0 20 20 20 2f 2a 20 69 73 49 6e 69 74 20 2a 2f 0a /* isInit */.
722c0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
722d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
722e0 20 69 6e 50 72 6f 67 72 65 73 73 20 2a 2f 0a 20 inProgress */.
722f0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
72300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
72310 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a isMallocInit */.
72320 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
72330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
72340 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 20 pInitMutex */.
72350 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
72360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
72370 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 20 2a 2f nRefInitMutex */
72380 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 .};.../*.** Hash
72390 20 74 61 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61 table for globa
723a0 6c 20 66 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75 l functions - fu
723b0 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 nctions common t
723c0 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73 o all.** databas
723d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
723e0 41 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 After initializa
723f0 74 69 6f 6e 2c 20 74 68 69 73 20 74 61 62 6c 65 tion, this table
72400 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 is.** read-only
72410 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
72420 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 ATE SQLITE_WSD F
72430 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 uncDefHash sqlit
72440 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e e3GlobalFunction
72450 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 s;../*.** The va
72460 6c 75 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64 lue of the "pend
72470 69 6e 67 22 20 62 79 74 65 20 6d 75 73 74 20 62 ing" byte must b
72480 65 20 30 78 34 30 30 30 30 30 30 30 20 28 31 20 e 0x40000000 (1
72490 62 79 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a byte past the.**
724a0 20 31 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e 1-gibabyte boun
724b0 64 61 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61 dary) in a compa
724c0 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20 tible database.
724d0 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73 SQLite never us
724e0 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 es.** the databa
724f0 73 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e se page that con
72500 74 61 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e tains the pendin
72510 67 20 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65 g byte. It neve
72520 72 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f r attempts.** to
72530 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
72540 68 61 74 20 70 61 67 65 2e 20 20 54 68 65 20 70 hat page. The p
72550 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 ending byte page
72560 20 69 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a is set assign.*
72570 2a 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 * for use by the
72580 20 56 46 53 20 6c 61 79 65 72 73 20 61 73 20 73 VFS layers as s
72590 70 61 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e pace for managin
725a0 67 20 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a g file locks..**
725b0 0a 2a 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 .** During testi
725c0 6e 67 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 ng, it is often
725d0 64 65 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 desirable to mov
725e0 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 e the pending by
725f0 74 65 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65 te to.** a diffe
72600 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e rent position in
72610 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 the file. This
72620 20 61 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 allows code tha
72630 74 20 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c t has to.** deal
72640 20 77 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e with the pendin
72650 67 20 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e g byte to run on
72660 20 66 69 6c 65 73 20 74 68 61 74 20 61 72 65 20 files that are
72670 6d 75 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 much smaller.**
72680 74 68 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65 than 1 GiB. The
72690 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f sqlite3_test_co
726a0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 ntrol() interfac
726b0 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f e can be used to
726c0 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e .** move the pen
726d0 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a ding byte..**.**
726e0 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 IMPORTANT: Cha
726f0 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e nging the pendin
72700 67 20 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61 g byte to any va
72710 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a lue other than.*
72720 2a 20 30 78 34 30 30 30 30 30 30 30 20 72 65 73 * 0x40000000 res
72730 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d ults in an incom
72740 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 patible database
72750 20 66 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a file format!.**
72760 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 Changing the pe
72770 6e 64 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e nding byte durin
72780 67 20 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75 g operating resu
72790 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 lts in undefined
727a0 0a 2a 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69 .** and dileteri
727b0 6f 75 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f ous behavior..*/
727c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
727d0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 int sqlite3Pendi
727e0 6e 67 42 79 74 65 20 3d 20 30 78 34 30 30 30 30 ngByte = 0x40000
727f0 30 30 30 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 000;../*********
72800 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f ***** End of glo
72810 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a bal.c **********
72820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72840 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
72850 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
72860 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a status.c ******
72870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72890 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
728a0 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 June 18.**.** T
728b0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
728c0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
728d0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
728e0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
728f0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
72900 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
72910 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
72920 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
72930 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
72940 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
72950 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
72960 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
72970 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
72980 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
72990 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
729a0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
729b0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72a00 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
72a10 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d is module implem
72a20 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 ents the sqlite3
72a30 5f 73 74 61 74 75 73 28 29 20 69 6e 74 65 72 66 _status() interf
72a40 61 63 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a ace and related.
72a50 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 ** functionality
72a60 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 74 61 ..**.** $Id: sta
72a70 74 75 73 2e 63 2c 76 20 31 2e 39 20 32 30 30 38 tus.c,v 1.9 2008
72a80 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 /09/02 00:52:52
72a90 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
72aa0 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 69 6e .** Variables in
72ab0 20 77 68 69 63 68 20 74 6f 20 72 65 63 6f 72 64 which to record
72ac0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
72ad0 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ion..*/.typedef
72ae0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 struct sqlite3St
72af0 61 74 54 79 70 65 20 73 71 6c 69 74 65 33 53 74 atType sqlite3St
72b00 61 74 54 79 70 65 3b 0a 73 74 61 74 69 63 20 53 atType;.static S
72b10 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
72b20 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 sqlite3StatType
72b30 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56 61 6c 75 {. int nowValu
72b40 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a e[9]; /*
72b50 20 43 75 72 72 65 6e 74 20 76 61 6c 75 65 20 2a Current value *
72b60 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c 75 65 5b /. int mxValue[
72b70 39 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 9]; /*
72b80 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 2a 2f Maximum value */
72b90 0a 7d 20 73 71 6c 69 74 65 33 53 74 61 74 20 3d .} sqlite3Stat =
72ba0 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b { {0,}, {0,} };
72bb0 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 53 74 .../* The "wsdSt
72bc0 61 74 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 at" macro will r
72bd0 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 74 esolve to the st
72be0 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e atus information
72bf0 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 .** state vector
72c00 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 . If writable s
72c10 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e tatic data is un
72c20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 supported on the
72c30 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 target,.** we h
72c40 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 ave to locate th
72c50 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 e state vector a
72c60 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 t run-time. In
72c70 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a the more common.
72c80 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 ** case where wr
72c90 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
72ca0 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c ta is supported,
72cb0 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 wsdStat can ref
72cc0 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 er directly.** t
72cd0 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 53 74 o the "sqlite3St
72ce0 61 74 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 at" state vector
72cf0 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e declared above.
72d00 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
72d10 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 E_OMIT_WSD.# def
72d20 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 20 ine wsdStatInit
72d30 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 sqlite3StatType
72d40 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71 *x = &GLOBAL(sq
72d50 6c 69 74 65 33 53 74 61 74 54 79 70 65 2c 73 71 lite3StatType,sq
72d60 6c 69 74 65 33 53 74 61 74 29 0a 23 20 64 65 66 lite3Stat).# def
72d70 69 6e 65 20 77 73 64 53 74 61 74 20 78 5b 30 5d ine wsdStat x[0]
72d80 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
72d90 77 73 64 53 74 61 74 49 6e 69 74 0a 23 20 64 65 wsdStatInit.# de
72da0 66 69 6e 65 20 77 73 64 53 74 61 74 20 73 71 6c fine wsdStat sql
72db0 69 74 65 33 53 74 61 74 0a 23 65 6e 64 69 66 0a ite3Stat.#endif.
72dc0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
72dd0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
72de0 6f 66 20 61 20 73 74 61 74 75 73 20 70 61 72 61 of a status para
72df0 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 meter..*/.SQLITE
72e00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72e10 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
72e20 69 6e 74 20 6f 70 29 7b 0a 20 20 77 73 64 53 74 int op){. wsdSt
72e30 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 atInit;. assert
72e40 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 ( op>=0 && op<Ar
72e50 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e raySize(wsdStat.
72e60 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 72 nowValue) );. r
72e70 65 74 75 72 6e 20 77 73 64 53 74 61 74 2e 6e 6f eturn wsdStat.no
72e80 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f wValue[op];.}../
72e90 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 6f 20 74 68 *.** Add N to th
72ea0 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 e value of a sta
72eb0 74 75 73 20 72 65 63 6f 72 64 2e 20 20 49 74 20 tus record. It
72ec0 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
72ed0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 68 6f the.** caller ho
72ee0 6c 64 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 lds appropriate
72ef0 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 locks..*/.SQLITE
72f00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72f10 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 69 lite3StatusAdd(i
72f20 6e 74 20 6f 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 nt op, int N){.
72f30 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
72f40 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 assert( op>=0 &&
72f50 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 op<ArraySize(ws
72f60 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 dStat.nowValue)
72f70 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 );. wsdStat.now
72f80 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a Value[op] += N;.
72f90 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f if( wsdStat.no
72fa0 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 wValue[op]>wsdSt
72fb0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 at.mxValue[op] )
72fc0 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 {. wsdStat.mx
72fd0 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 Value[op] = wsdS
72fe0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
72ff0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
73000 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
73010 61 20 73 74 61 74 75 73 20 74 6f 20 58 2e 0a 2a a status to X..*
73020 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
73030 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
73040 74 75 73 53 65 74 28 69 6e 74 20 6f 70 2c 20 69 tusSet(int op, i
73050 6e 74 20 58 29 7b 0a 20 20 77 73 64 53 74 61 74 nt X){. wsdStat
73060 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 Init;. assert(
73070 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 op>=0 && op<Arra
73080 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f ySize(wsdStat.no
73090 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 wValue) );. wsd
730a0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
730b0 5d 20 3d 20 58 3b 0a 20 20 69 66 28 20 77 73 64 ] = X;. if( wsd
730c0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
730d0 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 ]>wsdStat.mxValu
730e0 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 e[op] ){. wsd
730f0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d Stat.mxValue[op]
73100 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 = wsdStat.nowVa
73110 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a lue[op];. }.}..
73120 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 /*.** Query stat
73130 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a us information..
73140 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
73150 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 mentation assume
73160 73 20 74 68 61 74 20 72 65 61 64 69 6e 67 20 6f s that reading o
73170 72 20 77 72 69 74 69 6e 67 20 61 6e 20 61 6c 69 r writing an ali
73180 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 69 74 20 69 gned.** 32-bit i
73190 6e 74 65 67 65 72 20 69 73 20 61 6e 20 61 74 6f nteger is an ato
731a0 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 mic operation.
731b0 49 66 20 74 68 61 74 20 61 73 73 75 6d 70 74 69 If that assumpti
731c0 6f 6e 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 0a on is not true,.
731d0 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 ** then this rou
731e0 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 tine is not thre
731f0 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 adsafe..*/.SQLIT
73200 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
73210 33 5f 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 3_status(int op,
73220 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 int *pCurrent,
73230 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c int *pHighwater,
73240 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b int resetFlag){
73250 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a . wsdStatInit;.
73260 20 20 69 66 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 if( op<0 || op
73270 3e 3d 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 >=ArraySize(wsdS
73280 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 7b tat.nowValue) ){
73290 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
732a0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 TE_MISUSE;. }.
732b0 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 77 73 64 *pCurrent = wsd
732c0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
732d0 5d 3b 0a 20 20 2a 70 48 69 67 68 77 61 74 65 72 ];. *pHighwater
732e0 20 3d 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c = wsdStat.mxVal
732f0 75 65 5b 6f 70 5d 3b 0a 20 20 69 66 28 20 72 65 ue[op];. if( re
73300 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 77 setFlag ){. w
73310 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
73320 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 p] = wsdStat.now
73330 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 Value[op];. }.
73340 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
73350 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 K;.}../*.** Quer
73360 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 y status informa
73370 74 69 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c tion for a singl
73380 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
73390 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ction.*/.SQLITE_
733a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
733b0 64 62 5f 73 74 61 74 75 73 28 0a 20 20 73 71 6c db_status(. sql
733c0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
733d0 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
733e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 se connection wh
733f0 6f 73 65 20 73 74 61 74 75 73 20 69 73 20 64 65 ose status is de
73400 73 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f sired */. int o
73410 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
73420 20 2f 2a 20 53 74 61 74 75 73 20 76 65 72 62 20 /* Status verb
73430 2a 2f 0a 20 20 69 6e 74 20 2a 70 43 75 72 72 65 */. int *pCurre
73440 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 nt, /* Wr
73450 69 74 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 ite current valu
73460 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 e here */. int
73470 2a 70 48 69 67 68 77 61 74 65 72 2c 20 20 20 20 *pHighwater,
73480 20 20 2f 2a 20 57 72 69 74 65 20 68 69 67 68 2d /* Write high-
73490 77 61 74 65 72 20 6d 61 72 6b 20 68 65 72 65 20 water mark here
734a0 2a 2f 0a 20 20 69 6e 74 20 72 65 73 65 74 46 6c */. int resetFl
734b0 61 67 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ag /* Re
734c0 73 65 74 20 68 69 67 68 2d 77 61 74 65 72 20 6d set high-water m
734d0 61 72 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 ark if true */.)
734e0 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 {. switch( op )
734f0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
73500 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 E_DBSTATUS_LOOKA
73510 53 49 44 45 5f 55 53 45 44 3a 20 7b 0a 20 20 20 SIDE_USED: {.
73520 20 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 64 *pCurrent = d
73530 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 b->lookaside.nOu
73540 74 3b 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77 t;. *pHighw
73550 61 74 65 72 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 ater = db->looka
73560 73 69 64 65 2e 6d 78 4f 75 74 3b 0a 20 20 20 20 side.mxOut;.
73570 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 if( resetFlag
73580 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c ){. db->l
73590 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d ookaside.mxOut =
735a0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
735b0 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Out;. }.
735c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
735d0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
735e0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
735f0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a TE_ERROR;. }.
73600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
73610 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a ITE_OK;.}../****
73620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
73630 66 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a f status.c *****
73640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
73670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
73680 20 66 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a file date.c ***
73690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
736a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
736b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
736c0 20 32 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 2003 October 31
736d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
736e0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
736f0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
73700 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
73710 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
73720 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
73730 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
73740 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
73750 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
73760 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
73770 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
73780 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
73790 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
737a0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
737b0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
737c0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
737d0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
737e0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
737f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
73830 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
73840 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
73850 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
73860 6d 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 ment date and ti
73870 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 me.** functions
73880 66 6f 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a for SQLite. .**
73890 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c .** There is onl
738a0 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 y one exported s
738b0 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 ymbol in this fi
738c0 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f le - the functio
738d0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69 n.** sqlite3Regi
738e0 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 sterDateTimeFunc
738f0 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 tions() found at
73900 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 the bottom of t
73910 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 he file..** All
73920 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 other code has f
73930 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a ile scope..**.**
73940 20 24 49 64 3a 20 64 61 74 65 2e 63 2c 76 20 31 $Id: date.c,v 1
73950 2e 31 30 37 20 32 30 30 39 2f 30 35 2f 30 33 20 .107 2009/05/03
73960 32 30 3a 32 33 3a 35 33 20 64 72 68 20 45 78 70 20:23:53 drh Exp
73970 20 24 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 $.**.** SQLite
73980 70 72 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 processes all ti
73990 6d 65 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 mes and dates as
739a0 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 Julian Day numb
739b0 65 72 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 ers. The.** dat
739c0 65 73 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 es and times are
739d0 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e stored as the n
739e0 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 umber of days si
739f0 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 nce noon.** in G
73a00 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 reenwich on Nove
73a10 6d 62 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e mber 24, 4714 B.
73a20 43 2e 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 C. according to
73a30 74 68 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a the Gregorian.**
73a40 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d calendar system
73a50 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 . .**.** 1970-01
73a60 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 -01 00:00:00 is
73a70 4a 44 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 JD 2440587.5.**
73a80 32 30 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 2000-01-01 00:00
73a90 3a 30 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 :00 is JD 245154
73aa0 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 4.5.**.** This i
73ab0 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 mplemention requ
73ac0 69 72 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 ires years to be
73ad0 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20 expressed as a
73ae0 34 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 4-digit number.*
73af0 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 * which means th
73b00 61 74 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 at only dates be
73b10 74 77 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 tween 0000-01-01
73b20 20 61 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 and 9999-12-31
73b30 63 61 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 can.** be repres
73b40 65 6e 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 ented, even thou
73b50 67 68 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 gh julian day nu
73b60 6d 62 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 mbers allow a mu
73b70 63 68 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 ch wider.** rang
73b80 65 20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a e of dates..**.*
73b90 2a 20 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 * The Gregorian
73ba0 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 calendar system
73bb0 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 is used for all
73bc0 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c dates and times,
73bd0 0a 2a 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 .** even those t
73be0 68 61 74 20 70 72 65 64 61 74 65 20 74 68 65 20 hat predate the
73bf0 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 Gregorian calend
73c00 61 72 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 ar. Historians
73c10 75 73 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 usually.** use t
73c20 68 65 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 he Julian calend
73c30 61 72 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 ar for dates pri
73c40 6f 72 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 or to 1582-10-15
73c50 20 61 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a and for some.**
73c60 20 64 61 74 65 73 20 61 66 74 65 72 77 61 72 64 dates afterward
73c70 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 s, depending on
73c80 6c 6f 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 locale. Beware
73c90 6f 66 20 74 68 69 73 20 64 69 66 66 65 72 65 6e of this differen
73ca0 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ce..**.** The co
73cb0 6e 76 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 nversion algorit
73cc0 68 6d 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e hms are implemen
73cd0 74 65 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 ted based on des
73ce0 63 72 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 criptions.** in
73cf0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 the following te
73d00 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a xt:.**.** J
73d10 65 61 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 ean Meeus.**
73d20 20 20 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 Astronomical A
73d30 6c 67 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 lgorithms, 2nd E
73d40 64 69 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 dition, 1998.**
73d50 20 20 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 ISBM 0-9433
73d60 39 36 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 96-61-1.**
73d70 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 Willmann-Bell, I
73d80 6e 63 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d nc.** Richm
73d90 6f 6e 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 ond, Virginia (U
73da0 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 SA).*/.#include
73db0 3c 74 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 <time.h>..#ifnde
73dc0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 f SQLITE_OMIT_DA
73dd0 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a TETIME_FUNCS../*
73de0 0a 2a 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 .** On recent Wi
73df0 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c ndows platforms,
73e00 20 74 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 the localtime_s
73e10 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 () function is a
73e20 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 vailable.** as p
73e30 61 72 74 20 6f 66 20 74 68 65 20 22 53 65 63 75 art of the "Secu
73e40 72 65 20 43 52 54 22 2e 20 49 74 20 69 73 20 65 re CRT". It is e
73e50 73 73 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 ssentially equiv
73e60 61 6c 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 alent to .** loc
73e70 61 6c 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c altime_r() avail
73e80 61 62 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 able under most
73e90 50 4f 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c POSIX platforms,
73ea0 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
73eb0 20 0a 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 .** order of th
73ec0 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 e parameters is
73ed0 72 65 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 reversed..**.**
73ee0 53 65 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e See http://msdn.
73ef0 6d 69 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e microsoft.com/en
73f00 2d 75 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 -us/library/a442
73f10 78 33 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78 x3ye(VS.80).aspx
73f20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 ..**.** If the u
73f30 73 65 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 ser has not indi
73f40 63 61 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 cated to use loc
73f50 61 6c 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f altime_r() or lo
73f60 63 61 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 caltime_s().** a
73f70 6c 72 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f lready, check fo
73f80 72 20 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 r an MSVC build
73f90 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 environment that
73fa0 20 70 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f provides .** lo
73fb0 63 61 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a caltime_s()..*/.
73fc0 23 69 66 20 21 64 65 66 69 6e 65 64 28 48 41 56 #if !defined(HAV
73fd0 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 E_LOCALTIME_R) &
73fe0 26 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f & !defined(HAVE_
73ff0 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 LOCALTIME_S) &&
74000 5c 0a 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f \. defined(_
74010 4d 53 43 5f 56 45 52 29 20 26 26 20 64 65 66 69 MSC_VER) && defi
74020 6e 65 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 ned(_CRT_INSECUR
74030 45 5f 44 45 50 52 45 43 41 54 45 29 0a 23 64 65 E_DEPRECATE).#de
74040 66 69 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 fine HAVE_LOCALT
74050 49 4d 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a IME_S 1.#endif..
74060 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 /*.** A structur
74070 65 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 e for holding a
74080 73 69 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 single date and
74090 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 time..*/.typedef
740a0 20 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 struct DateTime
740b0 20 44 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 DateTime;.struc
740c0 74 20 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 t DateTime {. s
740d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 qlite3_int64 iJD
740e0 3b 20 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 ; /* The julian
740f0 64 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 day number times
74100 20 38 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 86400000 */. i
74110 6e 74 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 nt Y, M, D;
74120 20 20 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 /* Year, month
74130 2c 20 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 , and day */. i
74140 6e 74 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 nt h, m;
74150 20 20 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 /* Hour and mi
74160 6e 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 nutes */. int t
74170 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a z; /*
74180 20 54 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 Timezone offset
74190 20 69 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 in minutes */.
741a0 20 64 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20 double s;
741b0 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a /* Seconds *
741c0 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d /. char validYM
741d0 44 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 D; /* True (
741e0 31 29 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 1) if Y,M,D are
741f0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 valid */. char
74200 76 61 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a validHMS; /*
74210 20 54 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d True (1) if h,m
74220 2c 73 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a ,s are valid */.
74230 20 20 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 char validJD;
74240 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 /* True (1)
74250 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64 if iJD is valid
74260 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 */. char valid
74270 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 TZ; /* True
74280 20 28 31 29 20 69 66 20 74 7a 20 69 73 20 76 61 (1) if tz is va
74290 6c 69 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a lid */.};.../*.*
742a0 2a 20 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 * Convert zDate
742b0 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 into one or more
742c0 20 69 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 integers. Addi
742d0 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 tional arguments
742e0 0a 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 .** come in grou
742f0 70 73 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f ps of 5 as follo
74300 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ws:.**.**
74310 4e 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f N number o
74320 66 20 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 f digits in the
74330 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
74340 20 6d 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d min minimum
74350 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f allowed value o
74360 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a f the integer.**
74370 20 20 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d max m
74380 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 aximum allowed v
74390 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
743a0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 ger.** nex
743b0 74 43 20 20 20 66 69 72 73 74 20 63 68 61 72 61 tC first chara
743c0 63 74 65 72 20 61 66 74 65 72 20 74 68 65 20 69 cter after the i
743d0 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 nteger.**
743e0 70 56 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f pVal where to
743f0 20 77 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 write the integ
74400 65 72 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ers value..**.**
74410 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e Conversions con
74420 74 69 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 tinue until one
74430 77 69 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 with nextC==0 is
74440 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a encountered..**
74450 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 The function re
74460 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
74470 20 6f 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 of successful c
74480 6f 6e 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 onversions..*/.s
74490 74 61 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 tatic int getDig
744a0 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a its(const char *
744b0 7a 44 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 zDate, ...){. v
744c0 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 a_list ap;. int
744d0 20 76 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 val;. int N;.
744e0 20 69 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 int min;. int
744f0 6d 61 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 max;. int nextC
74500 3b 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 ;. int *pVal;.
74510 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
74520 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 va_start(ap, zDa
74530 74 65 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e te);. do{. N
74540 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
74550 74 29 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 t);. min = va
74560 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 _arg(ap, int);.
74570 20 20 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 max = va_arg(
74580 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 ap, int);. ne
74590 78 74 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c xtC = va_arg(ap,
745a0 20 69 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 int);. pVal
745b0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 = va_arg(ap, int
745c0 2a 29 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b *);. val = 0;
745d0 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 . while( N--
745e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 ){. if( !sq
745f0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 lite3Isdigit(*zD
74600 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ate) ){.
74610 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 goto end_getDigi
74620 74 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ts;. }.
74630 20 20 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b val = val*10 +
74640 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 *zDate - '0';.
74650 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 zDate++;.
74660 20 20 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c }. if( val<
74670 6d 69 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c min || val>max |
74680 7c 20 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e | (nextC!=0 && n
74690 65 78 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b extC!=*zDate) ){
746a0 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
746b0 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d getDigits;. }
746c0 0a 20 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c . *pVal = val
746d0 3b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 ;. zDate++;.
746e0 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 cnt++;. }whi
746f0 6c 65 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 le( nextC );.end
74700 5f 67 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61 _getDigits:. va
74710 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 _end(ap);. retu
74720 72 6e 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn cnt;.}../*.**
74730 20 52 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20 Read text from
74740 7a 5b 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 z[] and convert
74750 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 into a floating
74760 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 point number. R
74770 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d eturn.** the num
74780 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f ber of digits co
74790 6e 76 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 nverted..*/.#def
747a0 69 6e 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c ine getValue sql
747b0 69 74 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 ite3AtoF../*.**
747c0 50 61 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 Parse a timezone
747d0 20 65 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 extension on th
747e0 65 20 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d e end of a date-
747f0 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 time..** The ext
74800 65 6e 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 ension is of the
74810 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 form:.**.**
74820 20 20 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a (+/-)HH:MM.*
74830 2a 0a 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c *.** Or the "zul
74840 75 22 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a u" notation:.**.
74850 2a 2a 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a ** Z.**.*
74860 2a 20 49 66 20 74 68 65 20 70 61 72 73 65 20 69 * If the parse i
74870 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72 s successful, wr
74880 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ite the number o
74890 66 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 f minutes.** of
748a0 63 68 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 change in p->tz
748b0 61 6e 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49 and return 0. I
748c0 66 20 61 20 70 61 72 73 65 72 20 65 72 72 6f 72 f a parser error
748d0 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 occurs,.** retu
748e0 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a rn non-zero..**.
748f0 2a 2a 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 ** A missing spe
74900 63 69 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f cifier is not co
74910 6e 73 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f nsidered an erro
74920 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
74930 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 parseTimezone(c
74940 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 onst char *zDate
74950 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a , DateTime *p){.
74960 20 20 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 int sgn = 0;.
74970 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 int nHr, nMn;.
74980 20 69 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 int c;. while(
74990 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
749a0 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 *zDate) ){ zDate
749b0 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 ++; }. p->tz =
749c0 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0;. c = *zDate;
749d0 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b . if( c=='-' ){
749e0 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 . sgn = -1;.
749f0 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b }else if( c=='+
74a00 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b ' ){. sgn = +
74a10 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 1;. }else if( c
74a20 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 =='Z' || c=='z'
74a30 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a ){. zDate++;.
74a40 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 goto zulu_ti
74a50 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 me;. }else{.
74a60 20 72 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 return c!=0;.
74a70 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 }. zDate++;. i
74a80 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 f( getDigits(zDa
74a90 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a te, 2, 0, 14, ':
74aa0 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 ', &nHr, 2, 0, 5
74ab0 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 9, 0, &nMn)!=2 )
74ac0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
74ad0 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 }. zDate += 5
74ae0 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a ;. p->tz = sgn*
74af0 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a (nMn + nHr*60);.
74b00 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 zulu_time:. whi
74b10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 le( sqlite3Isspa
74b20 63 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 ce(*zDate) ){ zD
74b30 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 ate++; }. retur
74b40 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a n *zDate!=0;.}..
74b50 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 /*.** Parse time
74b60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48 s of the form HH
74b70 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 :MM or HH:MM:SS
74b80 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 or HH:MM:SS.FFFF
74b90 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c ..** The HH, MM,
74ba0 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 and SS must eac
74bb0 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 h be exactly 2 d
74bc0 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 igits. The.** f
74bd0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 ractional second
74be0 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e s FFFF can be on
74bf0 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 e or more digits
74c00 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 ..**.** Return 1
74c10 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 if there is a p
74c20 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 arsing error and
74c30 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 0 on success..*
74c40 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 /.static int par
74c50 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 seHhMmSs(const c
74c60 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 har *zDate, Date
74c70 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
74c80 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c h, m, s;. doubl
74c90 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 e ms = 0.0;. if
74ca0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 ( getDigits(zDat
74cb0 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 e, 2, 0, 24, ':'
74cc0 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 , &h, 2, 0, 59,
74cd0 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 0, &m)!=2 ){.
74ce0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
74cf0 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 zDate += 5;. i
74d00 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 f( *zDate==':' )
74d10 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 {. zDate++;.
74d20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 if( getDigits
74d30 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 (zDate, 2, 0, 59
74d40 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 , 0, &s)!=1 ){.
74d50 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
74d60 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b }. zDate +
74d70 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 = 2;. if( *zD
74d80 61 74 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 ate=='.' && sqli
74d90 74 65 33 49 73 64 69 67 69 74 28 7a 44 61 74 65 te3Isdigit(zDate
74da0 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f [1]) ){. do
74db0 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e uble rScale = 1.
74dc0 30 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 0;. zDate++
74dd0 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 ;. while( s
74de0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
74df0 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 Date) ){.
74e00 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 ms = ms*10.0 +
74e10 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 *zDate - '0';.
74e20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 rScale *=
74e30 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 10.0;. zD
74e40 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 ate++;. }.
74e50 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c ms /= rScal
74e60 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 e;. }. }else
74e70 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d {. s = 0;. }
74e80 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
74e90 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 0;. p->validHMS
74ea0 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 = 1;. p->h = h
74eb0 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 ;. p->m = m;.
74ec0 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 p->s = s + ms;.
74ed0 20 69 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f if( parseTimezo
74ee0 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 ne(zDate, p) ) r
74ef0 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 eturn 1;. p->va
74f00 6c 69 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d lidTZ = (p->tz!=
74f10 30 29 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 0)?1:0;. return
74f20 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 0;.}../*.** Con
74f30 76 65 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d vert from YYYY-M
74f40 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f M-DD HH:MM:SS to
74f50 20 6a 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 julian day. We
74f60 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a always assume.*
74f70 2a 20 74 68 61 74 20 74 68 65 20 59 59 59 59 2d * that the YYYY-
74f80 4d 4d 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69 MM-DD is accordi
74f90 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 ng to the Gregor
74fa0 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a ian calendar..**
74fb0 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20 .** Reference:
74fc0 4d 65 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f Meeus page 61.*/
74fd0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d .static void com
74fe0 70 75 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20 puteJD(DateTime
74ff0 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c *p){. int Y, M,
75000 20 44 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 D, A, B, X1, X2
75010 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 ;.. if( p->vali
75020 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 dJD ) return;.
75030 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 if( p->validYMD
75040 29 7b 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b ){. Y = p->Y;
75050 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 . M = p->M;.
75060 20 20 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d D = p->D;. }
75070 65 6c 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 else{. Y = 20
75080 30 30 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 00; /* If no YM
75090 44 20 73 70 65 63 69 66 69 65 64 2c 20 61 73 73 D specified, ass
750a0 75 6d 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 ume 2000-Jan-01
750b0 2a 2f 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 */. M = 1;.
750c0 20 20 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 D = 1;. }. i
750d0 66 28 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 f( M<=2 ){. Y
750e0 2d 2d 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b --;. M += 12;
750f0 0a 20 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 . }. A = Y/100
75100 3b 0a 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 ;. B = 2 - A +
75110 28 41 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 (A/4);. X1 = 36
75120 35 32 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 525*(Y+4716)/100
75130 3b 0a 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a ;. X2 = 306001*
75140 28 4d 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 (M+1)/10000;. p
75150 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 ->iJD = (sqlite3
75160 5f 69 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32 _int64)((X1 + X2
75170 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e + D + B - 1524.
75180 35 20 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b 5 ) * 86400000);
75190 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
751a0 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 1;. if( p->vali
751b0 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 dHMS ){. p->i
751c0 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 JD += p->h*36000
751d0 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 00 + p->m*60000
751e0 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 + (sqlite3_int64
751f0 29 28 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 )(p->s*1000);.
75200 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a if( p->validTZ
75210 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 ){. p->iJD
75220 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b -= p->tz*60000;
75230 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 . p->validY
75240 4d 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d MD = 0;. p-
75250 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 >validHMS = 0;.
75260 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 p->validTZ
75270 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d = 0;. }. }.}
75280 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 ../*.** Parse da
75290 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a tes of the form.
752a0 2a 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d **.** YYYY-M
752b0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 M-DD HH:MM:SS.FF
752c0 46 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d F.** YYYY-MM
752d0 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 -DD HH:MM:SS.**
752e0 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 YYYY-MM-DD H
752f0 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 H:MM.** YYYY
75300 2d 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 -MM-DD.**.** Wri
75310 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e te the result in
75320 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 to the DateTime
75330 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 structure and re
75340 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 turn 0.** on suc
75350 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 cess and 1 if th
75360 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 e input string i
75370 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 s not a well-for
75380 6d 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a med.** date..*/.
75390 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 static int parse
753a0 59 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 YyyyMmDd(const c
753b0 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 har *zDate, Date
753c0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
753d0 59 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 Y, M, D, neg;..
753e0 20 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 if( zDate[0]=='
753f0 2d 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b -' ){. zDate+
75400 2b 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a +;. neg = 1;.
75410 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 }else{. neg
75420 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
75430 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
75440 34 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 4,0,9999,'-',&Y,
75450 32 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 2,1,12,'-',&M,2,
75460 31 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 1,31,0,&D)!=3 ){
75470 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
75480 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 }. zDate += 10
75490 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 ;. while( sqlit
754a0 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 e3Isspace(*zDate
754b0 29 20 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 ) || 'T'==*(u8*)
754c0 7a 44 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b zDate ){ zDate++
754d0 3b 20 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 ; }. if( parseH
754e0 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d hMmSs(zDate, p)=
754f0 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 =0 ){. /* We
75500 67 6f 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a got the time */.
75510 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 }else if( *zDa
75520 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e te==0 ){. p->
75530 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 validHMS = 0;.
75540 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
75550 6e 20 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 n 1;. }. p->va
75560 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e lidJD = 0;. p->
75570 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 validYMD = 1;.
75580 70 2d 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 p->Y = neg ? -Y
75590 3a 20 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b : Y;. p->M = M;
755a0 0a 20 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 . p->D = D;. i
755b0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b f( p->validTZ ){
755c0 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 . computeJD(p
755d0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
755e0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 0;.}../*.** Set
755f0 74 68 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20 the time to the
75600 63 75 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70 current time rep
75610 6f 72 74 65 64 20 62 79 20 74 68 65 20 56 46 53 orted by the VFS
75620 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
75630 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 setDateTimeToCur
75640 72 65 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e rent(sqlite3_con
75650 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 text *context, D
75660 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 ateTime *p){. d
75670 6f 75 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 ouble r;. sqlit
75680 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
75690 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
756a0 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 le(context);. s
756b0 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 qlite3OsCurrentT
756c0 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 ime(db->pVfs, &r
756d0 29 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 );. p->iJD = (s
756e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
756f0 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 86400000.0 + 0.5
75700 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 );. p->validJD
75710 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 = 1;.}../*.** At
75720 74 65 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74 tempt to parse t
75730 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 he given string
75740 69 6e 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 into a Julian Da
75750 79 20 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 y Number. Retur
75760 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 n.** the number
75770 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a of errors..**.**
75780 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
75790 72 65 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f re acceptable fo
757a0 72 6d 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75 rms for the inpu
757b0 74 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 t string:.**.**
757c0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 YYYY-MM-DD
757d0 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f HH:MM:SS.FFF +/
757e0 2d 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 -HH:MM.** D
757f0 44 44 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 DDD.DD .**
75800 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 now.**.** In the
75810 20 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 first form, the
75820 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 +/-HH:MM is alw
75830 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 ays optional. T
75840 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a he fractional.**
75850 20 73 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 seconds extensi
75860 6f 6e 20 28 74 68 65 20 22 2e 46 46 46 22 29 20 on (the ".FFF")
75870 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 is optional. Th
75880 65 20 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f e seconds portio
75890 6e 0a 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 n.** (":SS.FFF")
758a0 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 is option. The
758b0 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 20 63 year and date c
758c0 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 an be omitted as
758d0 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 long.** as ther
758e0 65 20 69 73 20 61 20 74 69 6d 65 20 73 74 72 69 e is a time stri
758f0 6e 67 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74 ng. The time st
75900 72 69 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 ring can be omit
75910 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 ted as long.** a
75920 73 20 74 68 65 72 65 20 69 73 20 61 20 79 65 61 s there is a yea
75930 72 20 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 r and date..*/.s
75940 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 44 tatic int parseD
75950 61 74 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c ateOrTime(. sql
75960 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
75970 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 ntext, . const
75980 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 char *zDate, .
75990 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 DateTime *p.){.
759a0 20 69 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20 int isRealNum;
759b0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f /* Return fro
759c0 6d 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 m sqlite3IsNumbe
759d0 72 28 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a r(). Not used *
759e0 2f 0a 20 20 69 66 28 20 70 61 72 73 65 59 79 79 /. if( parseYyy
759f0 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d yMmDd(zDate,p)==
75a00 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
75a10 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 0;. }else if( p
75a20 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 arseHhMmSs(zDate
75a30 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 , p)==0 ){. r
75a40 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
75a50 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
75a60 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 Cmp(zDate,"now")
75a70 3d 3d 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74 ==0){. setDat
75a80 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 eTimeToCurrent(c
75a90 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20 ontext, p);.
75aa0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
75ab0 65 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e e if( sqlite3IsN
75ac0 75 6d 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73 umber(zDate, &is
75ad0 52 65 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f RealNum, SQLITE_
75ae0 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 UTF8) ){. dou
75af0 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 ble r;. getVa
75b00 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a lue(zDate, &r);.
75b10 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 p->iJD = (sq
75b20 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 lite3_int64)(r*8
75b30 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 6400000.0 + 0.5)
75b40 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 ;. p->validJD
75b50 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
75b60 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
75b70 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 1;.}../*.** Com
75b80 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d pute the Year, M
75b90 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 onth, and Day fr
75ba0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 om the julian da
75bb0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 y number..*/.sta
75bc0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
75bd0 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 YMD(DateTime *p)
75be0 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c {. int Z, A, B,
75bf0 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 C, D, E, X1;.
75c00 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 if( p->validYMD
75c10 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
75c20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 !p->validJD ){.
75c30 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a p->Y = 2000;.
75c40 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 p->M = 1;.
75c50 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 p->D = 1;. }e
75c60 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e lse{. Z = (in
75c70 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 t)((p->iJD + 432
75c80 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 00000)/86400000)
75c90 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 ;. A = (int)(
75ca0 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 (Z - 1867216.25)
75cb0 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 /36524.25);.
75cc0 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 A = Z + 1 + A -
75cd0 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 (A/4);. B = A
75ce0 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d + 1524;. C =
75cf0 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e (int)((B - 122.
75d00 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 1)/365.25);.
75d10 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 D = (36525*C)/10
75d20 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 0;. E = (int)
75d30 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b ((B-D)/30.6001);
75d40 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 . X1 = (int)(
75d50 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 30.6001*E);.
75d60 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 p->D = B - D - X
75d70 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 1;. p->M = E<
75d80 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 14 ? E-1 : E-13;
75d90 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d . p->Y = p->M
75da0 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 >2 ? C - 4716 :
75db0 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 C - 4715;. }.
75dc0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b p->validYMD = 1;
75dd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 .}../*.** Comput
75de0 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 e the Hour, Minu
75df0 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 te, and Seconds
75e00 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 from the julian
75e10 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 day number..*/.s
75e20 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 tatic void compu
75e30 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a teHMS(DateTime *
75e40 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 p){. int s;. i
75e50 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 f( p->validHMS )
75e60 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 return;. compu
75e70 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 teJD(p);. s = (
75e80 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 int)((p->iJD + 4
75e90 33 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30 3200000) % 86400
75ea0 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 000);. p->s = s
75eb0 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 /1000.0;. s = (
75ec0 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 int)p->s;. p->s
75ed0 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 -= s;. p->h =
75ee0 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 s/3600;. s -= p
75ef0 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d ->h*3600;. p->m
75f00 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 = s/60;. p->s
75f10 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a += s - p->m*60;.
75f20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 p->validHMS =
75f30 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 1;.}../*.** Comp
75f40 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 ute both YMD and
75f50 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 HMS.*/.static v
75f60 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 oid computeYMD_H
75f70 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b MS(DateTime *p){
75f80 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 . computeYMD(p)
75f90 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 ;. computeHMS(p
75fa0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 );.}../*.** Clea
75fb0 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d r the YMD and HM
75fc0 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a S and the TZ.*/.
75fd0 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 static void clea
75fe0 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 rYMD_HMS_TZ(Date
75ff0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 Time *p){. p->v
76000 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 alidYMD = 0;. p
76010 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a ->validHMS = 0;.
76020 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 p->validTZ = 0
76030 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
76040 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 ITE_OMIT_LOCALTI
76050 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 ME./*.** Compute
76060 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 the difference
76070 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 (in milliseconds
76080 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 ).** between loc
76090 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 altime and UTC (
760a0 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 a.k.a. GMT).** f
760b0 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 or the time valu
760c0 65 20 70 20 77 68 65 72 65 20 70 20 69 73 20 69 e p where p is i
760d0 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 n UTC..*/.static
760e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c sqlite3_int64 l
760f0 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 ocaltimeOffset(D
76100 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 ateTime *p){. D
76110 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 ateTime x, y;.
76120 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 time_t t;. x =
76130 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 *p;. computeYMD
76140 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 _HMS(&x);. if(
76150 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e x.Y<1971 || x.Y>
76160 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 =2038 ){. x.Y
76170 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d = 2000;. x.M
76180 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 = 1;. x.D =
76190 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 1;. x.h = 0;.
761a0 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 x.m = 0;.
761b0 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 x.s = 0.0;. }
761c0 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 else {. int s
761d0 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 = (int)(x.s + 0
761e0 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 .5);. x.s = s
761f0 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 ;. }. x.tz = 0
76200 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 ;. x.validJD =
76210 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 0;. computeJD(&
76220 78 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44 2f x);. t = x.iJD/
76230 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 1000 - 21086676*
76240 28 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66 64 (i64)10000;.#ifd
76250 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ef HAVE_LOCALTIM
76260 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 E_R. {. stru
76270 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 ct tm sLocal;.
76280 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74 localtime_r(&t
76290 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 , &sLocal);.
762a0 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.Y = sLocal.tm_
762b0 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 year + 1900;.
762c0 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.M = sLocal.tm
762d0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e _mon + 1;. y.
762e0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 D = sLocal.tm_md
762f0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c ay;. y.h = sL
76300 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 ocal.tm_hour;.
76310 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.m = sLocal.t
76320 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d m_min;. y.s =
76330 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a sLocal.tm_sec;.
76340 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 }.#elif define
76350 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 d(HAVE_LOCALTIME
76360 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 _S). {. stru
76370 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 ct tm sLocal;.
76380 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73 localtime_s(&s
76390 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20 Local, &t);.
763a0 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.Y = sLocal.tm_
763b0 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 year + 1900;.
763c0 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.M = sLocal.tm
763d0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e _mon + 1;. y.
763e0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 D = sLocal.tm_md
763f0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c ay;. y.h = sL
76400 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 ocal.tm_hour;.
76410 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.m = sLocal.t
76420 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d m_min;. y.s =
76430 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a sLocal.tm_sec;.
76440 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 }.#else. {.
76450 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d struct tm *pTm
76460 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
76470 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
76480 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
76490 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
764a0 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 MASTER));. pT
764b0 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 m = localtime(&t
764c0 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d );. y.Y = pTm
764d0 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 ->tm_year + 1900
764e0 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d ;. y.M = pTm-
764f0 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 >tm_mon + 1;.
76500 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.D = pTm->tm_m
76510 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 day;. y.h = p
76520 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 Tm->tm_hour;.
76530 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.m = pTm->tm_m
76540 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 in;. y.s = pT
76550 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 m->tm_sec;. s
76560 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
76570 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
76580 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
76590 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
765a0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
765b0 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a y.validYMD = 1;.
765c0 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31 y.validHMS = 1
765d0 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 ;. y.validJD =
765e0 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d 0;. y.validTZ =
765f0 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 0;. computeJD(
76600 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e &y);. return y.
76610 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 iJD - x.iJD;.}.#
76620 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
76630 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a OMIT_LOCALTIME *
76640 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 /../*.** Process
76650 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 a modifier to a
76660 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 date-time stamp
76670 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 . The modifiers
76680 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f are.** as follo
76690 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e ws:.**.** NN
766a0 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e N days.** NN
766b0 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e N hours.** N
766c0 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 NN minutes.**
766d0 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e NNN.NNNN secon
766e0 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f ds.** NNN mo
766f0 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 nths.** NNN
76700 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 years.** sta
76710 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 rt of month.**
76720 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 start of year
76730 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 .** start of
76740 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 week.** sta
76750 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 rt of day.**
76760 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 weekday N.**
76770 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 unixepoch.**
76780 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 localtime.**
76790 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 utc.**.** Re
767a0 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 turn 0 on succes
767b0 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
767c0 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 is any kind of
767d0 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 error..*/.static
767e0 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69 int parseModifi
767f0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a er(const char *z
76800 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 Mod, DateTime *p
76810 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b ){. int rc = 1;
76820 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 . int n;. doub
76830 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c le r;. char *z,
76840 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d zBuf[30];. z =
76850 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 zBuf;. for(n=0
76860 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 ; n<ArraySize(zB
76870 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d uf)-1 && zMod[n]
76880 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d ; n++){. z[n]
76890 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
768a0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 UpperToLower[(u8
768b0 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 )zMod[n]];. }.
768c0 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 z[n] = 0;. swi
768d0 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 tch( z[0] ){.#if
768e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
768f0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 _LOCALTIME. c
76900 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 ase 'l': {.
76910 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 /* localtime
76920 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
76930 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 ** Assuming the
76940 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c current time val
76950 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 ue is UTC (a.k.a
76960 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74 . GMT), shift it
76970 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f to. ** sho
76980 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 w local time..
76990 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
769a0 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 strcmp(z, "loca
769b0 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 ltime")==0 ){.
769c0 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
769d0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
769e0 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f JD += localtimeO
769f0 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 ffset(p);.
76a00 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
76a10 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 Z(p);. rc
76a20 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
76a30 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
76a40 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
76a50 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 'u': {. /*
76a60 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 . ** uni
76a70 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a xepoch. **.
76a80 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 ** Treat t
76a90 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
76aa0 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 of p->iJD as th
76ab0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 e number of.
76ac0 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e ** seconds sin
76ad0 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 ce 1970. Conver
76ae0 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 t to a real juli
76af0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 an day number..
76b00 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
76b10 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 ( strcmp(z, "uni
76b20 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 xepoch")==0 && p
76b30 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 ->validJD ){.
76b40 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70 p->iJD = (p
76b50 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38 ->iJD + 43200)/8
76b60 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a 6400 + 21086676*
76b70 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20 (i64)10000000;.
76b80 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f clearYMD_
76b90 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 HMS_TZ(p);.
76ba0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
76bb0 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
76bc0 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 E_OMIT_LOCALTIME
76bd0 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 . else if(
76be0 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29 strcmp(z, "utc")
76bf0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
76c00 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b qlite3_int64 c1;
76c10 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
76c20 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 JD(p);. c
76c30 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 1 = localtimeOff
76c40 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
76c50 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 p->iJD -= c1;.
76c60 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 clearYMD_H
76c70 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 MS_TZ(p);.
76c80 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d p->iJD += c1 -
76c90 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
76ca0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
76cb0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e = 0;. }.#en
76cc0 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b dif. break;
76cd0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
76ce0 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 'w': {. /*.
76cf0 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b ** week
76d00 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 day N. **.
76d10 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 ** Move the
76d20 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d date to the sam
76d30 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65 e time on the ne
76d40 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 xt occurrence of
76d50 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61 . ** weekda
76d60 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e y N where 0==Sun
76d70 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 day, 1==Monday,
76d80 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 and so forth. I
76d90 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 f the. ** d
76da0 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ate is already o
76db0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
76dc0 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20 e weekday, this
76dd0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
76de0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 */. if( s
76df0 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 trncmp(z, "weekd
76e00 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67 ay ", 8)==0 && g
76e10 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 etValue(&z[8],&r
76e20 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 )>0.
76e30 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29 && (n=(int)
76e40 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 r)==r && n>=0 &&
76e50 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 r<7 ){.
76e60 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b sqlite3_int64 Z;
76e70 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
76e80 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
76e90 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d p->validTZ =
76ea0 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 0;. p->v
76eb0 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
76ec0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
76ed0 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28 ;. Z = ((
76ee0 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30 p->iJD + 1296000
76ef0 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 00)/86400000) %
76f00 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a 7;. if( Z
76f10 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 >n ) Z -= 7;.
76f20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
76f30 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b n - Z)*86400000;
76f40 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
76f50 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
76f60 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
76f70 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
76f80 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
76f90 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 's': {. /*
76fa0 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61 . ** sta
76fb0 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20 rt of TTTTT.
76fc0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f **. ** Mo
76fd0 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b ve the date back
76fe0 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67 wards to the beg
76ff0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75 inning of the cu
77000 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 rrent day,.
77010 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 ** or month or
77020 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 year.. */.
77030 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 if( strncmp
77040 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c (z, "start of ",
77050 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 9)!=0 ) break;.
77060 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 z += 9;.
77070 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 computeYMD(p
77080 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 );. p->vali
77090 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 dHMS = 1;.
770a0 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b p->h = p->m = 0;
770b0 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e . p->s = 0.
770c0 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
770d0 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 dTZ = 0;. p
770e0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
770f0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
77100 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b z,"month")==0 ){
77110 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 . p->D =
77120 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 1;. rc =
77130 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 0;. }else i
77140 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 f( strcmp(z,"yea
77150 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
77160 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b computeYMD(p);
77170 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 . p->M =
77180 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 1;. p->D
77190 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 = 1;. rc
771a0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
771b0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 if( strcmp(z,"d
771c0 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ay")==0 ){.
771d0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
771e0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
771f0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 }. case '
77200 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27 +':. case '-'
77210 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a :. case '0':.
77220 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20 case '1':.
77230 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20 case '2':.
77240 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61 case '3':. ca
77250 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 se '4':. case
77260 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '5':. case '
77270 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27 6':. case '7'
77280 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a :. case '8':.
77290 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a case '9': {.
772a0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f double rRo
772b0 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d under;. n =
772c0 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 getValue(z, &r)
772d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
772e0 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 n>=1 );. if
772f0 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 ( z[n]==':' ){.
77300 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 /* A modi
77310 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d fier of the form
77320 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 (+|-)HH:MM:SS.F
77330 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 FF adds (or subt
77340 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 racts) the.
77350 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 ** specified
77360 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c number of hours,
77370 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 minutes, second
77380 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 s, and fractiona
77390 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 l seconds.
773a0 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 ** to the time
773b0 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 . The ".FFF" ma
773c0 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 y be omitted. T
773d0 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 he ":SS.FFF" may
773e0 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f be. ** o
773f0 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 mitted..
77400 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 */. const
77410 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
77420 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 DateTime
77430 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 tx;. sqli
77440 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20 te3_int64 day;.
77450 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 if( !sqli
77460 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20 te3Isdigit(*z2)
77470 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ) z2++;.
77480 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73 memset(&tx, 0, s
77490 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20 izeof(tx));.
774a0 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d if( parseHhM
774b0 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62 mSs(z2, &tx) ) b
774c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f reak;. co
774d0 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20 mputeJD(&tx);.
774e0 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20 tx.iJD -=
774f0 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20 43200000;.
77500 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38 day = tx.iJD/8
77510 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
77520 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38 tx.iJD -= day*8
77530 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
77540 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 if( z[0]=='-' )
77550 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a tx.iJD = -tx.iJ
77560 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 D;. compu
77570 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
77580 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
77590 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
775a0 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 iJD += tx.iJD;.
775b0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
775c0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
775d0 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d }. z +=
775e0 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 n;. while(
775f0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
77600 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 *z) ) z++;.
77610 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
77620 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69 en30(z);. i
77630 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 f( n>10 || n<3 )
77640 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 break;. if
77650 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b ( z[n-1]=='s' ){
77660 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d z[n-1] = 0; n--
77670 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 ; }. comput
77680 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63 eJD(p);. rc
77690 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75 = 0;. rRou
776a0 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e nder = r<0 ? -0.
776b0 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20 5 : +0.5;.
776c0 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63 if( n==3 && strc
776d0 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
776e0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 {. p->iJD
776f0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
77700 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 64)(r*86400000.0
77710 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 + rRounder);.
77720 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d }else if( n=
77730 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =4 && strcmp(z,"
77740 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 hour")==0 ){.
77750 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
77760 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
77770 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e *(86400000.0/24.
77780 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 0) + rRounder);.
77790 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
777a0 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a n==6 && strcmp(z
777b0 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b ,"minute")==0 ){
777c0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
777d0 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 += (sqlite3_int6
777e0 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30 4)(r*(86400000.0
777f0 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 /(24.0*60.0)) +
77800 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
77810 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 }else if( n==6
77820 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63 && strcmp(z,"sec
77830 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ond")==0 ){.
77840 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
77850 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
77860 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e (86400000.0/(24.
77870 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20 0*60.0*60.0)) +
77880 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
77890 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20 }else if( n==5
778a0 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e && strcmp(z,"mon
778b0 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 th")==0 ){.
778c0 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20 int x, y;.
778d0 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
778e0 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
778f0 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a p->M += (int)r;.
77900 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d x = p->M
77910 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 >0 ? (p->M-1)/12
77920 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b : (p->M-12)/12;
77930 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
77940 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d x;. p->M
77950 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20 -= x*12;.
77960 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 p->validJD = 0
77970 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
77980 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 eJD(p);.
77990 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 y = (int)r;.
779a0 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a if( y!=r ){.
779b0 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 p->iJD
779c0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
779d0 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30 64)((r - y)*30.0
779e0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 *86400000.0 + rR
779f0 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20 ounder);.
77a00 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }. }else i
77a10 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d f( n==4 && strcm
77a20 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 p(z,"year")==0 )
77a30 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20 {. int y
77a40 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 = (int)r;.
77a50 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 computeYMD_HMS
77a60 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
77a70 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20 Y += y;.
77a80 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a p->validJD = 0;.
77a90 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a computeJ
77aa0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 D(p);. if
77ab0 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 ( y!=r ){.
77ac0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
77ad0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 qlite3_int64)((r
77ae0 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30 - y)*365.0*8640
77af0 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 0000.0 + rRounde
77b00 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 r);. }.
77b10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
77b20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 rc = 1;.
77b30 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d }. clearYM
77b40 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
77b50 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
77b60 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
77b70 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
77b80 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
77b90 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 c;.}../*.** Proc
77ba0 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f ess time functio
77bb0 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72 n arguments. ar
77bc0 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d gv[0] is a date-
77bd0 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 time stamp..** a
77be0 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f rgv[1] and follo
77bf0 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65 wing are modifie
77c00 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20 rs. Parse them
77c10 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a all and write.**
77c20 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74 the resulting t
77c30 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74 ime into the Dat
77c40 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20 eTime structure
77c50 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20 p. Return 0.**
77c60 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 on success and 1
77c70 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e if there are an
77c80 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 y errors..**.**
77c90 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72 If there are zer
77ca0 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66 o parameters (if
77cb0 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73 even argv[0] is
77cc0 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74 undefined).** t
77cd0 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66 hen assume a def
77ce0 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e ault value of "n
77cf0 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e ow" for argv[0].
77d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
77d10 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 sDate(. sqlite3
77d20 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
77d30 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 t, . int argc,
77d40 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
77d50 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65 **argv, . Date
77d60 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 Time *p.){. int
77d70 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 i;. const unsi
77d80 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 gned char *z;.
77d90 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d int eType;. mem
77da0 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
77db0 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67 (*p));. if( arg
77dc0 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44 c==0 ){. setD
77dd0 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
77de0 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 (context, p);.
77df0 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65 }else if( (eType
77e00 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
77e10 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d _type(argv[0]))=
77e20 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20 =SQLITE_FLOAT.
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77e40 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
77e50 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 E_INTEGER ){.
77e60 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
77e70 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 e3_int64)(sqlite
77e80 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 3_value_double(a
77e90 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 rgv[0])*86400000
77ea0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 .0 + 0.5);. p
77eb0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
77ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 }else{. z =
77ed0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
77ee0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 xt(argv[0]);.
77ef0 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65 if( !z || parse
77f00 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65 DateOrTime(conte
77f10 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29 xt, (char*)z, p)
77f20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
77f30 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
77f40 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b for(i=1; i<argc;
77f50 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 i++){. if( (
77f60 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
77f70 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29 e_text(argv[i]))
77f80 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69 ==0 || parseModi
77f90 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70 fier((char*)z, p
77fa0 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
77fb0 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
77fc0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f return 0;.}.../
77fd0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
77fe0 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c ng routines impl
77ff0 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 ement the variou
78000 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 s date and time
78010 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 functions.** of
78020 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a SQLite..*/../*.*
78030 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 * julianday(
78040 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
78050 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
78060 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 Return the juli
78070 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 an day number of
78080 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66 the date specif
78090 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d ied in the argum
780a0 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ents.*/.static v
780b0 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e oid juliandayFun
780c0 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
780d0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
780e0 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
780f0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
78100 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 v.){. DateTime
78110 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 x;. if( isDate(
78120 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 context, argc, a
78130 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 rgv, &x)==0 ){.
78140 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 computeJD(&x)
78150 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
78160 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 sult_double(cont
78170 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30 ext, x.iJD/86400
78180 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 000.0);. }.}../
78190 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65 *.** datetime
781a0 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f ( TIMESTRING, MO
781b0 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a D, MOD, ...).**.
781c0 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d ** Return YYYY-M
781d0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f M-DD HH:MM:SS.*/
781e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 .static void dat
781f0 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c etimeFunc(. sql
78200 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
78210 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
78220 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
78230 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
78240 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 ateTime x;. if(
78250 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c isDate(context,
78260 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 argc, argv, &x)
78270 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ==0 ){. char
78280 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 zBuf[100];. c
78290 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
782a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
782b0 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
782c0 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 Buf), zBuf, "%04
782d0 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64 d-%02d-%02d %02d
782e0 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20 :%02d:%02d",.
782f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78300 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c x.Y, x.M, x.D,
78310 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
78320 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 (x.s));. sqli
78330 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
78340 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d context, zBuf, -
78350 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 1, SQLITE_TRANSI
78360 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
78370 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45 ** time( TIME
78380 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
78390 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
783a0 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a urn HH:MM:SS.*/.
783b0 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 static void time
783c0 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
783d0 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
783e0 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
783f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
78400 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
78410 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
78420 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
78430 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
78440 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
78450 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
78460 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 eHMS(&x);. sq
78470 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
78480 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
78490 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30 f, "%02d:%02d:%0
784a0 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 2d", x.h, x.m, (
784b0 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71 int)x.s);. sq
784c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
784d0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
784e0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
784f0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f SIENT);. }.}../
78500 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 *.** date( TI
78510 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
78520 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
78530 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
78540 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
78550 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 dateFunc(. sqli
78560 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
78570 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
78580 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
78590 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
785a0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
785b0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
785c0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
785d0 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a =0 ){. char z
785e0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f Buf[100];. co
785f0 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20 mputeYMD(&x);.
78600 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
78610 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c tf(sizeof(zBuf),
78620 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 zBuf, "%04d-%02
78630 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e d-%02d", x.Y, x.
78640 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c M, x.D);. sql
78650 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
78660 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 (context, zBuf,
78670 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 -1, SQLITE_TRANS
78680 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a IENT);. }.}../*
78690 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28 .** strftime(
786a0 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52 FORMAT, TIMESTR
786b0 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
786c0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
786d0 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69 a string descri
786e0 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20 bed by FORMAT.
786f0 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66 Conversions as f
78700 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
78710 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68 %d day of month
78720 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61 .** %f ** fra
78730 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 ctional seconds
78740 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20 SS.SSS.** %H
78750 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20 hour 00-24.**
78760 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72 %j day of year
78770 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 000-366.** %J
78780 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20 ** Julian day
78790 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20 number.** %m
787a0 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20 month 01-12.**
787b0 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35 %M minute 00-5
787c0 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 9.** %s secon
787d0 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31 ds since 1970-01
787e0 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 -01.** %S sec
787f0 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20 onds 00-59.**
78800 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 %w day of week
78810 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 0-6 sunday==0.*
78820 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20 * %W week of
78830 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20 year 00-53.**
78840 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39 %Y year 0000-99
78850 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f 99.** %% %.*/
78860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 .static void str
78870 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c ftimeFunc(. sql
78880 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
78890 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
788a0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
788b0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
788c0 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34 ateTime x;. u64
788d0 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a n;. size_t i,j
788e0 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 ;. char *z;. s
788f0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f qlite3 *db;. co
78900 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d nst char *zFmt =
78910 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
78920 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
78930 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 (argv[0]);. cha
78940 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 r zBuf[100];. i
78950 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 f( zFmt==0 || is
78960 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 Date(context, ar
78970 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 gc-1, argv+1, &x
78980 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 ) ) return;. db
78990 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
789a0 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
789b0 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 text);. for(i=0
789c0 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 , n=1; zFmt[i];
789d0 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 i++, n++){. i
789e0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 f( zFmt[i]=='%'
789f0 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 ){. switch(
78a00 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 zFmt[i+1] ){.
78a10 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a case 'd':.
78a20 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 case 'H'
78a30 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
78a40 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 m':. case
78a50 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 'M':. ca
78a60 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 se 'S':.
78a70 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 case 'W':.
78a80 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 n++;.
78a90 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 /* fall thru
78aa0 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 */. case
78ab0 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 'w':. cas
78ac0 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 e '%':.
78ad0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
78ae0 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 case 'f':.
78af0 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 n += 8;.
78b00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
78b10 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 case 'j':.
78b20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b n += 3;
78b30 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
78b40 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
78b50 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 Y':. n
78b60 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 += 8;.
78b70 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
78b80 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 ase 's':.
78b90 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 case 'J':.
78ba0 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 n += 50;.
78bb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
78bc0 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a default:.
78bd0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
78be0 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 ; /* ERROR. re
78bf0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 turn a NULL */.
78c00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b }. i++
78c10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65 ;. }. }. te
78c20 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f stcase( n==sizeo
78c30 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74 f(zBuf)-1 );. t
78c40 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 estcase( n==size
78c50 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65 of(zBuf) );. te
78c60 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
78c70 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
78c80 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b E_LIMIT_LENGTH]+
78c90 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 1 );. testcase(
78ca0 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 n==(u64)db->aLi
78cb0 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
78cc0 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66 _LENGTH] );. if
78cd0 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 ( n<sizeof(zBuf)
78ce0 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 ){. z = zBuf
78cf0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e ;. }else if( n>
78d00 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
78d10 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
78d20 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 GTH] ){. sqli
78d30 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
78d40 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 _toobig(context)
78d50 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
78d60 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 }else{. z = s
78d70 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
78d80 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 w(db, (int)n);.
78d90 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 if( z==0 ){.
78da0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
78db0 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 ult_error_nomem(
78dc0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 context);.
78dd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
78de0 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 }. computeJD(&x
78df0 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f );. computeYMD_
78e00 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 HMS(&x);. for(i
78e10 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 =j=0; zFmt[i]; i
78e20 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d ++){. if( zFm
78e30 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 t[i]!='%' ){.
78e40 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 z[j++] = zFmt
78e50 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a [i];. }else{.
78e60 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
78e70 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d switch( zFmt[i]
78e80 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ){. case
78e90 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'd': sqlite3_s
78ea0 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d nprintf(3, &z[j]
78eb0 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b ,"%02d",x.D); j+
78ec0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
78ed0 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 case 'f': {.
78ee0 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 double
78ef0 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 s = x.s;.
78f00 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 if( s>59.999
78f10 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 ) s = 59.999;.
78f20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
78f30 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a snprintf(7, &z[j
78f40 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a ],"%06.3f", s);.
78f50 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 j += s
78f60 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
78f70 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
78f80 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
78f90 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
78fa0 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 H': sqlite3_snp
78fb0 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
78fc0 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 %02d",x.h); j+=2
78fd0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
78fe0 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 case 'W': /* Fa
78ff0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 ll thru */.
79000 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 case 'j': {.
79010 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61 int nDa
79020 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
79030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 * Number of days
79040 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f since 1st day o
79050 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 f year */.
79060 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d DateTime y =
79070 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e x;. y.
79080 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 validJD = 0;.
79090 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a y.M = 1;.
790a0 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20 y.D =
790b0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 1;. com
790c0 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 puteJD(&y);.
790d0 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e nDay = (in
790e0 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b t)((x.iJD-y.iJD+
790f0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
79100 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 00);. i
79110 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 f( zFmt[i]=='W'
79120 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
79130 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f nt wd; /* 0=Mo
79140 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c nday, 1=Tuesday,
79150 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f ... 6=Sunday */
79160 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20 . wd
79170 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b = (int)(((x.iJD+
79180 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
79190 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20 00)%7);.
791a0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
791b0 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(3, &z[j],"%
791c0 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 02d",(nDay+7-wd)
791d0 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 /7);.
791e0 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 j += 2;.
791f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
79200 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
79210 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c printf(4, &z[j],
79220 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a "%03d",nDay+1);.
79230 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d j +=
79240 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 3;. }.
79250 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
79260 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
79270 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 case 'J': {.
79280 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
79290 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a _snprintf(20, &z
792a0 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a [j],"%.16g",x.iJ
792b0 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
792c0 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69 j+=sqli
792d0 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a te3Strlen30(&z[j
792e0 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 ]);. br
792f0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
79300 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a case 'm':
79310 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
79320 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 tf(3, &z[j],"%02
79330 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 d",x.M); j+=2; b
79340 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
79350 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 se 'M': sqlite3
79360 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
79370 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 j],"%02d",x.m);
79380 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
79390 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b case 's': {
793a0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
793b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 e3_snprintf(30,&
793c0 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 z[j],"%lld",.
793d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
793e0 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e (i64)(x.
793f0 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 iJD/1000 - 21086
79400 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 676*(i64)10000))
79410 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d ;. j +=
79420 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
79430 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 (&z[j]);.
79440 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
79450 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 }. case
79460 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'S': sqlite3_s
79470 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c nprintf(3,&z[j],
79480 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 "%02d",(int)x.s)
79490 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 ; j+=2; break;.
794a0 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
794b0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a {. z[j
794c0 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78 ++] = (char)(((x
794d0 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f .iJD+129600000)/
794e0 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b 86400000) % 7) +
794f0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
79500 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
79510 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 . case 'Y
79520 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ': {. s
79530 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
79540 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 5,&z[j],"%04d",x
79550 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 .Y); j+=sqlite3S
79560 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
79570 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
79580 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
79590 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b default: z[
795a0 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 j++] = '%'; brea
795b0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
795c0 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b . }. z[j] = 0;
795d0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
795e0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
795f0 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 z, -1,.
79600 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d z==
79610 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 zBuf ? SQLITE_TR
79620 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 ANSIENT : SQLITE
79630 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a _DYNAMIC);.}../*
79640 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 .** current_time
79650 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ().**.** This fu
79660 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
79670 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 he same value as
79680 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f time('now')..*/
79690 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 .static void cti
796a0 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 meFunc(. sqlite
796b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
796c0 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
796d0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
796e0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
796f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
79700 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
79710 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 tUsed2);. timeF
79720 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
79730 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 0);.}../*.** cur
79740 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a rent_date().**.*
79750 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
79760 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 returns the same
79770 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27 value as date('
79780 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 now')..*/.static
79790 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28 void cdateFunc(
797a0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
797b0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
797c0 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
797d0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
797e0 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 tUsed2.){. UNUS
797f0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
79800 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
79810 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e ;. dateFunc(con
79820 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a text, 0, 0);.}..
79830 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 /*.** current_ti
79840 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 mestamp().**.**
79850 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
79860 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
79870 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 alue as datetime
79880 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 ('now')..*/.stat
79890 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 ic void ctimesta
798a0 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 mpFunc(. sqlite
798b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
798c0 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
798d0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
798e0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
798f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
79900 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
79910 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74 tUsed2);. datet
79920 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c imeFunc(context,
79930 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0, 0);.}.#endif
79940 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c /* !defined(SQL
79950 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d ITE_OMIT_DATETIM
79960 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 E_FUNCS) */..#if
79970 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
79980 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f DATETIME_FUNCS./
79990 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 *.** If the libr
799a0 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ary is compiled
799b0 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c to omit the full
799c0 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 -scale date and
799d0 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 time.** handling
799e0 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c (to get a small
799f0 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 er binary), the
79a00 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 following minima
79a10 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 l version.** of
79a20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 the functions cu
79a30 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 rrent_time(), cu
79a40 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 rrent_date() and
79a50 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 current_timesta
79a60 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c mp().** are incl
79a70 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 uded instead. Th
79a80 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 is is to support
79a90 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 column declarat
79aa0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 ions that.** inc
79ab0 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 lude "DEFAULT CU
79ac0 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e RRENT_TIME" etc.
79ad0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
79ae0 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d tion uses the C-
79af0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e library function
79b00 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 s time(), gmtime
79b10 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 ().** and strfti
79b20 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 me(). The format
79b30 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 string to pass
79b40 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 to strftime() is
79b50 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 supplied.** as
79b60 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f the user-data fo
79b70 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a r the function..
79b80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
79b90 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a urrentTimeFunc(.
79ba0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
79bb0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
79bc0 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
79bd0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
79be0 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 {. time_t t;.
79bf0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 char *zFormat =
79c00 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
79c10 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 user_data(contex
79c20 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 t);. sqlite3 *d
79c30 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a b;. double rT;.
79c40 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b char zBuf[20];
79c50 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
79c60 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e ETER(argc);. UN
79c70 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
79c80 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71 rgv);.. db = sq
79c90 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
79ca0 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
79cb0 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 ;. sqlite3OsCur
79cc0 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
79cd0 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66 s, &rT);.#ifndef
79ce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
79cf0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20 ATING_POINT. t
79d00 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20 = 86400.0*(rT -
79d10 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35 2440587.5) + 0.5
79d20 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74 ;.#else. /* wit
79d30 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f hout floating po
79d40 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20 int support, rT
79d50 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 will have. ** a
79d60 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63 lready lost frac
79d70 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69 tional day preci
79d80 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d sion.. */. t =
79d90 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32 86400 * (rT - 2
79da0 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b 440587) - 43200;
79db0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 .#endif.#ifdef H
79dc0 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b AVE_GMTIME_R. {
79dd0 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 . struct tm s
79de0 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f Now;. gmtime_
79df0 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 r(&t, &sNow);.
79e00 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c strftime(zBuf,
79e10 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 20, zFormat, &s
79e20 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a Now);. }.#else.
79e30 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
79e40 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 m *pTm;. sqli
79e50 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
79e60 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
79e70 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
79e80 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
79e90 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 pTm = gmtime
79ea0 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 (&t);. strfti
79eb0 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f me(zBuf, 20, zFo
79ec0 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 rmat, pTm);.
79ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
79ee0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
79ef0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
79f00 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
79f10 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ));. }.#endif..
79f20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
79f30 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a _text(context, z
79f40 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f Buf, -1, SQLITE_
79f50 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 TRANSIENT);.}.#e
79f60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
79f70 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 function regist
79f80 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 ered all of the
79f90 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e above C function
79fa0 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 s as SQL.** func
79fb0 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f tions. This sho
79fc0 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 uld be the only
79fd0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
79fe0 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 file with.** ext
79ff0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a ernal linkage..*
7a000 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
7a010 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
7a020 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e isterDateTimeFun
7a030 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 ctions(void){.
7a040 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
7a050 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54 D FuncDef aDateT
7a060 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 imeFuncs[] = {.#
7a070 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
7a080 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 IT_DATETIME_FUNC
7a090 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a S. FUNCTION(j
7a0a0 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20 ulianday,
7a0b0 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61 -1, 0, 0, julia
7a0c0 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20 ndayFunc ),.
7a0d0 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20 FUNCTION(date,
7a0e0 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 -1, 0
7a0f0 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20 , 0, dateFunc
7a100 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
7a110 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20 ON(time,
7a120 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74 -1, 0, 0, t
7a130 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a imeFunc ),.
7a140 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 FUNCTION(dat
7a150 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d etime, -
7a160 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d 1, 0, 0, datetim
7a170 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 eFunc ),. FU
7a180 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c NCTION(strftime,
7a190 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 -1, 0,
7a1a0 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20 0, strftimeFunc
7a1b0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
7a1c0 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 (current_time,
7a1d0 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69 0, 0, 0, cti
7a1e0 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 meFunc ),.
7a1f0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 FUNCTION(curre
7a200 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c nt_timestamp, 0,
7a210 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 0, 0, ctimestam
7a220 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 pFunc),. FUNC
7a230 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 TION(current_dat
7a240 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c e, 0, 0, 0,
7a250 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 cdateFunc )
7a260 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f ,.#else. STR_
7a270 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 FUNCTION(current
7a280 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22 _time, 0, "
7a290 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20 %H:%M:%S",
7a2a0 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 0, currentTi
7a2b0 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 meFunc),. STR
7a2c0 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e _FUNCTION(curren
7a2d0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 t_timestamp, 0,
7a2e0 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20 "%Y-%m-%d",
7a2f0 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 0, currentT
7a300 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 imeFunc),. ST
7a310 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 R_FUNCTION(curre
7a320 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c nt_date, 0,
7a330 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d "%Y-%m-%d %H:%M
7a340 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74 :%S", 0, current
7a350 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69 TimeFunc),.#endi
7a360 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a f. };. int i;.
7a370 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 FuncDefHash *p
7a380 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 Hash = &GLOBAL(F
7a390 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 uncDefHash, sqli
7a3a0 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f te3GlobalFunctio
7a3b0 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a ns);. FuncDef *
7a3c0 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 aFunc = (FuncDef
7a3d0 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 *)&GLOBAL(FuncDe
7a3e0 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 f, aDateTimeFunc
7a3f0 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 s);.. for(i=0;
7a400 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74 i<ArraySize(aDat
7a410 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b eTimeFuncs); i++
7a420 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 ){. sqlite3Fu
7a430 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 ncDefInsert(pHas
7a440 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 h, &aFunc[i]);.
7a450 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
7a460 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 ***** End of dat
7a470 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
7a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
7a4b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
7a4c0 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c **********
7a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 ****/./*.** 2005
7a500 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a November 29.**.
7a510 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
7a520 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
7a530 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
7a540 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
7a550 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
7a560 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
7a570 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
7a580 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
7a590 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
7a5a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7a5b0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
7a5c0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
7a5d0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
7a5e0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7a5f0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
7a600 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
7a610 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
7a620 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
7a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a670 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
7a680 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 contains OS int
7a690 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 erface code that
7a6a0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
7a6b0 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 l.** architectur
7a6c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f es..**.** $Id: o
7a6d0 73 2e 63 2c 76 20 31 2e 31 32 36 20 32 30 30 39 s.c,v 1.126 2009
7a6e0 2f 30 33 2f 32 35 20 31 34 3a 32 34 3a 34 32 20 /03/25 14:24:42
7a6f0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 drh Exp $.*/.#de
7a700 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f fine _SQLITE_OS_
7a710 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c C_ 1.#undef _SQL
7a720 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a ITE_OS_C_../*.**
7a730 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c The default SQL
7a740 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 ite sqlite3_vfs
7a750 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
7a760 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a do not allocate.
7a770 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 ** memory (actua
7a780 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 lly, os_unix.c a
7a790 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c llocates a small
7a7a0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
7a7b0 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e y.** from within
7a7c0 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 OsOpen()), but
7a7d0 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 some third-party
7a7e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
7a7f0 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 may..** So we t
7a800 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 est the effects
7a810 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 of a malloc() fa
7a820 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 iling and the sq
7a830 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 lite3OsXXX().**
7a840 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 function returni
7a850 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ng SQLITE_IOERR_
7a860 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 NOMEM using the
7a870 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7a880 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 T macro..**.** T
7a890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e he following fun
7a8a0 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 ctions are instr
7a8b0 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c umented for mall
7a8c0 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a oc() failure .**
7a8d0 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 testing:.**.**
7a8e0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
7a8f0 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 n().** sqlit
7a900 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 e3OsRead().**
7a910 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 sqlite3OsWrite
7a920 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
7a930 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 3OsSync().**
7a940 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
7a950 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e .**.*/.#if defin
7a960 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 ed(SQLITE_TEST)
7a970 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 && (SQLITE_OS_WI
7a980 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 N==0). #define
7a990 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7a9a0 54 20 69 66 20 28 31 29 20 7b 20 20 20 20 20 20 T if (1) {
7a9b0 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 \. void
7a9c0 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 *pTstAlloc = sq
7a9d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b lite3Malloc(10);
7a9e0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 \. if
7a9f0 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 (!pTstAlloc) ret
7aa00 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
7aa10 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20 73 _NOMEM; \. s
7aa20 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 74 qlite3_free(pTst
7aa30 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20 Alloc);
7aa40 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
7aa50 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
7aa60 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
7aa70 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ST.#endif../*.**
7aa80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
7aa90 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 outines are conv
7aaa0 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 enience wrappers
7aab0 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a around methods.
7aac0 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ** of the sqlite
7aad0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 3_file object.
7aae0 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a This is mostly j
7aaf0 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 ust syntactic su
7ab00 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 gar. All.** of t
7ab10 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d his would be com
7ab20 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 pletely automati
7ab30 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 c if SQLite were
7ab40 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 coded using.**
7ab50 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 C++ instead of p
7ab60 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 lain old C..*/.S
7ab70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
7ab80 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 t sqlite3OsClose
7ab90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
7aba0 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 Id){. int rc =
7abb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
7abc0 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 pId->pMethods )
7abd0 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e {. rc = pId->
7abe0 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 pMethods->xClose
7abf0 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e (pId);. pId->
7ac00 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 pMethods = 0;.
7ac10 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
7ac20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7ac30 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 int sqlite3OsRea
7ac40 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a d(sqlite3_file *
7ac50 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 id, void *pBuf,
7ac60 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 int amt, i64 off
7ac70 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 set){. DO_OS_MA
7ac80 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
7ac90 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
7aca0 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 ->xRead(id, pBuf
7acb0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a , amt, offset);.
7acc0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
7acd0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 int sqlite3OsWr
7ace0 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ite(sqlite3_file
7acf0 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 *id, const void
7ad00 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c *pBuf, int amt,
7ad10 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 i64 offset){.
7ad20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7ad30 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e T;. return id->
7ad40 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 65 pMethods->xWrite
7ad50 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 (id, pBuf, amt,
7ad60 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 offset);.}.SQLIT
7ad70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7ad80 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
7ad90 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
7ada0 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20 72 , i64 size){. r
7adb0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
7adc0 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69 64 ds->xTruncate(id
7add0 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 , size);.}.SQLIT
7ade0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7adf0 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 lite3OsSync(sqli
7ae00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
7ae10 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f t flags){. DO_O
7ae20 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
7ae30 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
7ae40 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 hods->xSync(id,
7ae50 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 flags);.}.SQLITE
7ae60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7ae70 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 ite3OsFileSize(s
7ae80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
7ae90 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 i64 *pSize){.
7aea0 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
7aeb0 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e T;. return id->
7aec0 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 pMethods->xFileS
7aed0 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a ize(id, pSize);.
7aee0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
7aef0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f int sqlite3OsLo
7af00 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
7af10 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 *id, int lockTyp
7af20 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c e){. DO_OS_MALL
7af30 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 OC_TEST;. retur
7af40 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
7af50 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 xLock(id, lockTy
7af60 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 pe);.}.SQLITE_PR
7af70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7af80 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 3OsUnlock(sqlite
7af90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
7afa0 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 lockType){. ret
7afb0 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
7afc0 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f ->xUnlock(id, lo
7afd0 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 ckType);.}.SQLIT
7afe0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7aff0 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 lite3OsCheckRese
7b000 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
7b010 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
7b020 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f pResOut){. DO_O
7b030 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
7b040 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
7b050 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73 65 hods->xCheckRese
7b060 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65 rvedLock(id, pRe
7b070 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f sOut);.}.SQLITE_
7b080 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7b090 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
7b0a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
7b0b0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 d, int op, void
7b0c0 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72 6e *pArg){. return
7b0d0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
7b0e0 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 FileControl(id,
7b0f0 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c op, pArg);.}.SQL
7b100 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7b110 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 sqlite3OsSectorS
7b120 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
7b130 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78 *id){. int (*x
7b140 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 SectorSize)(sqli
7b150 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d te3_file*) = id-
7b160 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63 74 >pMethods->xSect
7b170 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e orSize;. return
7b180 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f 20 (xSectorSize ?
7b190 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29 20 xSectorSize(id)
7b1a0 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 : SQLITE_DEFAULT
7b1b0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d _SECTOR_SIZE);.}
7b1c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b1d0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 int sqlite3OsDev
7b1e0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
7b1f0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
7b200 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 *id){. return i
7b210 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 d->pMethods->xDe
7b220 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
7b230 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ics(id);.}../*.*
7b240 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 * The next group
7b250 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 of routines are
7b260 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 convenience wra
7b270 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 ppers around the
7b280 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 2e .** VFS methods.
7b290 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
7b2a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
7b2b0 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
7b2c0 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f vfs *pVfs, . co
7b2d0 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
7b2e0 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 . sqlite3_file
7b2f0 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 *pFile, . int
7b300 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 flags, . int *p
7b310 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 69 6e FlagsOut.){. in
7b320 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 t rc;. DO_OS_MA
7b330 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 63 20 LLOC_TEST;. rc
7b340 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 = pVfs->xOpen(pV
7b350 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 fs, zPath, pFile
7b360 2c 20 66 6c 61 67 73 2c 20 70 46 6c 61 67 73 4f , flags, pFlagsO
7b370 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 ut);. assert( r
7b380 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
7b390 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d pFile->pMethods=
7b3a0 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 =0 );. return r
7b3b0 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 c;.}.SQLITE_PRIV
7b3c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7b3d0 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f sDelete(sqlite3_
7b3e0 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
7b3f0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e char *zPath, in
7b400 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 t dirSync){. re
7b410 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 turn pVfs->xDele
7b420 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 te(pVfs, zPath,
7b430 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 dirSync);.}.SQLI
7b440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
7b450 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a qlite3OsAccess(.
7b460 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
7b470 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Vfs, . const ch
7b480 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e ar *zPath, . in
7b490 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
7b4a0 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f *pResOut.){. DO
7b4b0 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b _OS_MALLOC_TEST;
7b4c0 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
7b4d0 78 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 50 xAccess(pVfs, zP
7b4e0 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52 65 73 ath, flags, pRes
7b4f0 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Out);.}.SQLITE_P
7b500 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
7b510 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e3OsFullPathname
7b520 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
7b530 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 *pVfs, . const
7b540 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 char *zPath, .
7b550 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20 0a 20 int nPathOut, .
7b560 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75 74 0a char *zPathOut.
7b570 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 ){. return pVfs
7b580 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 ->xFullPathname(
7b590 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61 pVfs, zPath, nPa
7b5a0 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29 thOut, zPathOut)
7b5b0 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
7b5c0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
7b5d0 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 ENSION.SQLITE_PR
7b5e0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
7b5f0 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 te3OsDlOpen(sqli
7b600 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
7b610 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
7b620 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 ){. return pVfs
7b630 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20 ->xDlOpen(pVfs,
7b640 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 zPath);.}.SQLITE
7b650 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
7b660 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 lite3OsDlError(s
7b670 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
7b680 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 , int nByte, cha
7b690 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 70 r *zBufOut){. p
7b6a0 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 70 56 Vfs->xDlError(pV
7b6b0 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f fs, nByte, zBufO
7b6c0 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ut);.}.SQLITE_PR
7b6d0 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c IVATE void (*sql
7b6e0 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69 ite3OsDlSym(sqli
7b6f0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 te3_vfs *pVfs, v
7b700 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 oid *pHdle, cons
7b710 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 28 76 t char *zSym))(v
7b720 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 oid){. return p
7b730 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 66 73 Vfs->xDlSym(pVfs
7b740 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a , pHdle, zSym);.
7b750 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
7b760 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 void sqlite3OsD
7b770 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 lClose(sqlite3_v
7b780 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
7b790 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73 pHandle){. pVfs
7b7a0 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c ->xDlClose(pVfs,
7b7b0 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e pHandle);.}.#en
7b7c0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
7b7d0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
7b7e0 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 N */.SQLITE_PRIV
7b7f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7b800 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 sRandomness(sqli
7b810 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
7b820 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a nt nByte, char *
7b830 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 zBufOut){. retu
7b840 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d rn pVfs->xRandom
7b850 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65 ness(pVfs, nByte
7b860 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 , zBufOut);.}.SQ
7b870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7b880 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 sqlite3OsSleep(
7b890 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
7b8a0 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a s, int nMicro){.
7b8b0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
7b8c0 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69 63 Sleep(pVfs, nMic
7b8d0 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ro);.}.SQLITE_PR
7b8e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7b8f0 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 3OsCurrentTime(s
7b900 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
7b910 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f , double *pTimeO
7b920 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 ut){. return pV
7b930 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 fs->xCurrentTime
7b940 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 (pVfs, pTimeOut)
7b950 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
7b960 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7b970 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 sOpenMalloc(. s
7b980 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
7b990 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
7b9a0 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 *zFile, . sqlit
7b9b0 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 e3_file **ppFile
7b9c0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a , . int flags,.
7b9d0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 int *pOutFlags
7b9e0 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
7b9f0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 QLITE_NOMEM;. s
7ba00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
7ba10 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 le;. pFile = (s
7ba20 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 qlite3_file *)sq
7ba30 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73 lite3Malloc(pVfs
7ba40 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 ->szOsFile);. i
7ba50 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 f( pFile ){.
7ba60 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
7ba70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 en(pVfs, zFile,
7ba80 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f pFile, flags, pO
7ba90 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 utFlags);. if
7baa0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
7bab0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
7bac0 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 _free(pFile);.
7bad0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a }else{. *
7bae0 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a ppFile = pFile;.
7baf0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
7bb00 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
7bb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7bb20 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 te3OsCloseFree(s
7bb30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
7bb40 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 le){. int rc =
7bb50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
7bb60 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
7bb70 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c rc = sqlite3OsCl
7bb80 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 71 ose(pFile);. sq
7bb90 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
7bba0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
7bbb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 }../*.** The lis
7bbc0 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 74 65 t of all registe
7bbd0 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e red VFS implemen
7bbe0 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 tations..*/.stat
7bbf0 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ic sqlite3_vfs *
7bc00 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 73 4c SQLITE_WSD vfsL
7bc10 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ist = 0;.#define
7bc20 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28 vfsList GLOBAL(
7bc30 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 sqlite3_vfs *, v
7bc40 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c fsList)../*.** L
7bc50 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 20 6e ocate a VFS by n
7bc60 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65 ame. If no name
7bc70 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c is given, simpl
7bc80 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 y return the.**
7bc90 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 68 65 first VFS on the
7bca0 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 list..*/.SQLITE
7bcb0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 73 _API sqlite3_vfs
7bcc0 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 *sqlite3_vfs_fi
7bcd0 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a nd(const char *z
7bce0 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f Vfs){. sqlite3_
7bcf0 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 vfs *pVfs = 0;.#
7bd00 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
7bd10 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d SAFE. sqlite3_m
7bd20 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e utex *mutex;.#en
7bd30 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
7bd40 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
7bd50 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 . int rc = sqli
7bd60 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
7bd70 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
7bd80 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 urn 0;.#endif.#i
7bd90 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
7bda0 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 AFE. mutex = sq
7bdb0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
7bdc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
7bdd0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e TIC_MASTER);.#en
7bde0 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 dif. sqlite3_mu
7bdf0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
7be00 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d 20 76 ;. for(pVfs = v
7be10 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 70 56 fsList; pVfs; pV
7be20 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b fs=pVfs->pNext){
7be30 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 . if( zVfs==0
7be40 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
7be50 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 ( strcmp(zVfs, p
7be60 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 Vfs->zName)==0 )
7be70 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 break;. }. sq
7be80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
7be90 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
7bea0 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pVfs;.}../*.*
7beb0 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 * Unlink a VFS f
7bec0 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c rom the linked l
7bed0 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ist.*/.static vo
7bee0 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c id vfsUnlink(sql
7bef0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b ite3_vfs *pVfs){
7bf00 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
7bf10 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 e3_mutex_held(sq
7bf20 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
7bf30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
7bf40 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a TIC_MASTER)) );.
7bf50 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 7b if( pVfs==0 ){
7bf60 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f . /* No-op */
7bf70 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73 . }else if( vfs
7bf80 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 List==pVfs ){.
7bf90 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73 vfsList = pVfs
7bfa0 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 ->pNext;. }else
7bfb0 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b 0a if( vfsList ){.
7bfc0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 sqlite3_vfs
7bfd0 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 *p = vfsList;.
7bfe0 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 while( p->pNex
7bff0 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 t && p->pNext!=p
7c000 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20 3d Vfs ){. p =
7c010 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d p->pNext;. }
7c020 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 . if( p->pNex
7c030 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 t==pVfs ){.
7c040 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 p->pNext = pVfs
7c050 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
7c060 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 }.}../*.** Regi
7c070 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 ster a VFS with
7c080 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74 20 the system. It
7c090 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 is harmless to r
7c0a0 65 67 69 73 74 65 72 20 74 68 65 20 73 61 6d 65 egister the same
7c0b0 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 .** VFS multiple
7c0c0 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 times. The new
7c0d0 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65 VFS becomes the
7c0e0 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 default if make
7c0f0 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e Dflt is.** true.
7c100 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
7c110 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 nt sqlite3_vfs_r
7c120 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f egister(sqlite3_
7c130 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d vfs *pVfs, int m
7c140 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69 akeDflt){. sqli
7c150 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
7c160 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 = 0;.#ifndef SQ
7c170 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
7c180 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 IT. int rc = sq
7c190 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
7c1a0 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 ();. if( rc ) r
7c1b0 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 eturn rc;.#endif
7c1c0 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 . mutex = sqlit
7c1d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
7c1e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
7c1f0 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 _MASTER);. sqli
7c200 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
7c210 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c mutex);. vfsUnl
7c220 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 66 28 ink(pVfs);. if(
7c230 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73 makeDflt || vfs
7c240 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 List==0 ){. p
7c250 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 Vfs->pNext = vfs
7c260 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 List;. vfsLis
7c270 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73 t = pVfs;. }els
7c280 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 e{. pVfs->pNe
7c290 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e xt = vfsList->pN
7c2a0 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 ext;. vfsList
7c2b0 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a ->pNext = pVfs;.
7c2c0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 66 73 }. assert(vfs
7c2d0 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 List);. sqlite3
7c2e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
7c2f0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ex);. return SQ
7c300 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
7c310 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 56 * Unregister a V
7c320 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 FS so that it is
7c330 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 65 73 no longer acces
7c340 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sible..*/.SQLITE
7c350 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
7c360 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 _vfs_unregister(
7c370 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
7c380 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 s){.#if SQLITE_T
7c390 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 HREADSAFE. sqli
7c3a0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
7c3b0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
7c3c0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
7c3d0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
7c3e0 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
7c3f0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
7c400 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 utex);. vfsUnli
7c410 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 6c 69 nk(pVfs);. sqli
7c420 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
7c430 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
7c440 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
7c450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
7c460 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a nd of os.c *****
7c470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
7c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
7c4b0 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e egin file fault.
7c4c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
7c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
7c4f0 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 32 32 *.** 2008 Jan 22
7c500 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
7c510 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
7c520 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
7c530 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
7c540 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
7c550 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
7c560 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
7c570 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
7c580 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
7c590 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
7c5a0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
7c5b0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
7c5c0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
7c5d0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
7c5e0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
7c5f0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
7c600 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
7c610 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
7c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
7c660 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 75 6c 74 **.** $Id: fault
7c670 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 30 .c,v 1.11 2008/0
7c680 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72 9/02 00:52:52 dr
7c690 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
7c6a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
7c6b0 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70 ains code to sup
7c6c0 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74 port the concept
7c6d0 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a of "benign" .**
7c6e0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
7c6f0 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c (when the xMall
7c700 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 oc() or xRealloc
7c710 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 () method of the
7c720 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f .** sqlite3_mem_
7c730 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 methods structur
7c740 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 e fails to alloc
7c750 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d ate a block of m
7c760 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 emory.** and ret
7c770 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 urns 0). .**.**
7c780 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c Most malloc fail
7c790 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e ures are non-ben
7c7a0 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20 ign. After they
7c7b0 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a occur, SQLite.**
7c7c0 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 abandons the cu
7c7d0 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 rrent operation
7c7e0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 and returns an e
7c7f0 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c rror code (usual
7c800 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d ly.** SQLITE_NOM
7c810 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e EM) to the user.
7c820 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 However, someti
7c830 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e mes a fault is n
7c840 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a ot necessarily.*
7c850 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 * fatal. For exa
7c860 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f mple, if a mallo
7c870 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65 c fails while re
7c880 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61 sizing a hash ta
7c890 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 ble, this .** is
7c8a0 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f completely reco
7c8b0 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 verable simply b
7c8c0 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f y not carrying o
7c8d0 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 ut the resize. T
7c8e0 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c he .** hash tabl
7c8f0 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 e will continue
7c900 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d to function norm
7c910 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c ally. So a mall
7c920 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 oc failure .** d
7c930 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 uring a hash tab
7c940 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 62 le resize is a b
7c950 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a enign fault..*/.
7c960 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
7c970 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 _OMIT_BUILTIN_TE
7c980 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c ST../*.** Global
7c990 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 variables..*/.t
7c9a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 65 ypedef struct Be
7c9b0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 nignMallocHooks
7c9c0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
7c9d0 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 s;.static SQLITE
7c9e0 5f 57 53 44 20 73 74 72 75 63 74 20 42 65 6e 69 _WSD struct Beni
7c9f0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a gnMallocHooks {.
7ca00 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e void (*xBenign
7ca10 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 Begin)(void);.
7ca20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e void (*xBenignEn
7ca30 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 d)(void);.} sqli
7ca40 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 te3Hooks = { 0,
7ca50 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 0 };../* The "ws
7ca60 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 dHooks" macro wi
7ca70 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 ll resolve to th
7ca80 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 e appropriate Be
7ca90 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a nignMallocHooks.
7caa0 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 ** structure. I
7cab0 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 f writable stati
7cac0 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 c data is unsupp
7cad0 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 orted on the tar
7cae0 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 get,.** we have
7caf0 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 to locate the st
7cb00 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 ate vector at ru
7cb10 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 n-time. In the
7cb20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 more common.** c
7cb30 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 ase where writab
7cb40 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
7cb50 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 s supported, wsd
7cb60 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20 Hooks can refer
7cb70 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 directly.** to t
7cb80 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 he "sqlite3Hooks
7cb90 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 " state vector d
7cba0 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a eclared above..*
7cbb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
7cbc0 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e OMIT_WSD.# defin
7cbd0 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c e wsdHooksInit \
7cbe0 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 . BenignMallocH
7cbf0 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 ooks *x = &GLOBA
7cc00 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f L(BenignMallocHo
7cc10 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 oks,sqlite3Hooks
7cc20 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f ).# define wsdHo
7cc30 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 oks x[0].#else.#
7cc40 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 define wsdHooks
7cc50 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 Init.# define ws
7cc60 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f dHooks sqlite3Ho
7cc70 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a oks.#endif.../*.
7cc80 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b ** Register hook
7cc90 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 s to call when s
7cca0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
7ccb0 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a nMalloc() and.**
7ccc0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
7ccd0 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 nMalloc() are ca
7cce0 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76 65 lled, respective
7ccf0 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
7cd00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7cd10 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f e3BenignMallocHo
7cd20 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 oks(. void (*xB
7cd30 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 enignBegin)(void
7cd40 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e ),. void (*xBen
7cd50 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b ignEnd)(void).){
7cd60 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b . wsdHooksInit;
7cd70 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e . wsdHooks.xBen
7cd80 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 ignBegin = xBeni
7cd90 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f gnBegin;. wsdHo
7cda0 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d oks.xBenignEnd =
7cdb0 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a xBenignEnd;.}..
7cdc0 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 /*.** This (sqli
7cdd0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
7cde0 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20 oc()) is called
7cdf0 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74 by SQLite code t
7ce00 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a o indicate that.
7ce10 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 ** subsequent ma
7ce20 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 lloc failures ar
7ce30 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c e benign. A call
7ce40 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 to sqlite3EndBe
7ce50 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 nignMalloc().**
7ce60 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 indicates that s
7ce70 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 ubsequent malloc
7ce80 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f failures are no
7ce90 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c n-benign..*/.SQL
7cea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7ceb0 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
7cec0 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b ignMalloc(void){
7ced0 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b . wsdHooksInit;
7cee0 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e . if( wsdHooks.
7cef0 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a xBenignBegin ){.
7cf00 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 wsdHooks.xBe
7cf10 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d nignBegin();. }
7cf20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
7cf30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e E void sqlite3En
7cf40 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f dBenignMalloc(vo
7cf50 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 id){. wsdHooksI
7cf60 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f nit;. if( wsdHo
7cf70 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 oks.xBenignEnd )
7cf80 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 {. wsdHooks.x
7cf90 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d BenignEnd();. }
7cfa0 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 .}..#endif /*
7cfb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7cfc0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
7cfd0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
7cfe0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c **** End of faul
7cff0 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.c ************
7d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d020 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
7d030 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
7d040 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem0.c *********
7d050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d070 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
7d080 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a October 28.**.**
7d090 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
7d0a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
7d0b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
7d0c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
7d0d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
7d0e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
7d0f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
7d100 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
7d110 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
7d120 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
7d130 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
7d140 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
7d150 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
7d160 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
7d170 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
7d180 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
7d190 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
7d1f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
7d200 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 ns a no-op memor
7d210 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 y allocation dri
7d220 76 65 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 vers for use whe
7d230 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f n.** SQLITE_ZERO
7d240 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e _MALLOC is defin
7d250 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 ed. The allocat
7d260 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c ion drivers impl
7d270 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 emented.** here
7d280 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51 always fail. SQ
7d290 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 Lite will not op
7d2a0 65 72 61 74 65 20 77 69 74 68 20 74 68 65 73 65 erate with these
7d2b0 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 65 drivers. These
7d2c0 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70 .** are merely p
7d2d0 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 laceholders. Re
7d2e0 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74 20 al drivers must
7d2f0 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 75 be substituted u
7d300 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f sing.** sqlite3_
7d310 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20 config() before
7d320 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 SQLite will oper
7d330 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ate..**.** $Id:
7d340 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 32 30 30 mem0.c,v 1.1 200
7d350 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 8/10/28 18:58:20
7d360 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f drh Exp $.*/../
7d370 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
7d380 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
7d390 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 allocator is the
7d3a0 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 default. It is
7d3b0 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f .** used when no
7d3c0 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c other memory al
7d3d0 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 locator is speci
7d3e0 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 fied using compi
7d3f0 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f le-time.** macro
7d400 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
7d410 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a ITE_ZERO_MALLOC.
7d420 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 ./*.** No-op ver
7d430 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d sions of all mem
7d440 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
7d450 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 outines.*/.stati
7d460 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d c void *sqlite3M
7d470 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 emMalloc(int nBy
7d480 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d te){ return 0; }
7d490 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
7d4a0 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 ite3MemFree(void
7d4b0 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 *pPrior){ retur
7d4c0 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 n; }.static void
7d4d0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c *sqlite3MemReal
7d4e0 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
7d4f0 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 , int nByte){ re
7d500 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 turn 0; }.static
7d510 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 int sqlite3MemS
7d520 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ize(void *pPrior
7d530 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 ){ return 0; }.s
7d540 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
7d550 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 3MemRoundup(int
7d560 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a n){ return n; }.
7d570 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
7d580 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a e3MemInit(void *
7d590 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e NotUsed){ return
7d5a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
7d5b0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
7d5c0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 3MemShutdown(voi
7d5d0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 d *NotUsed){ ret
7d5e0 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 urn; }../*.** Th
7d5f0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
7d600 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 e only routine i
7d610 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 n this file with
7d620 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 external linkag
7d630 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 e..**.** Populat
7d640 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 e the low-level
7d650 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
7d660 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 n function point
7d670 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 ers in.** sqlite
7d680 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3GlobalConfig.m
7d690 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f with pointers to
7d6a0 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e the routines in
7d6b0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 this file..*/.S
7d6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7d6d0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 id sqlite3MemSet
7d6e0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 Default(void){.
7d6f0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
7d700 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
7d710 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 s defaultMethods
7d720 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 = {. sqlite
7d730 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 3MemMalloc,.
7d740 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c sqlite3MemFree,
7d750 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
7d760 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 Realloc,. sq
7d770 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 lite3MemSize,.
7d780 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 sqlite3MemRou
7d790 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 ndup,. sqlit
7d7a0 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 e3MemInit,.
7d7b0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f sqlite3MemShutdo
7d7c0 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
7d7d0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 sqlite3_config
7d7e0 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d (SQLITE_CONFIG_M
7d7f0 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d ALLOC, &defaultM
7d800 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 ethods);.}..#end
7d810 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52 if /* SQLITE_ZER
7d820 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a O_MALLOC */../**
7d830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
7d840 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a of mem0.c *****
7d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
7d880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
7d890 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a in file mem1.c *
7d8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
7d8d0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
7d8e0 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
7d8f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
7d900 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
7d910 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
7d920 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
7d930 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
7d940 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
7d950 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
7d960 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
7d970 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
7d980 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
7d990 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
7d9a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
7d9b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
7d9c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
7d9d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
7d9e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
7d9f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
7da00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
7da50 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 contains low-le
7da60 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
7da70 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f ation drivers fo
7da80 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 r when.** SQLite
7da90 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74 will use the st
7daa0 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79 andard C-library
7dab0 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f malloc/realloc/
7dac0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a free interface.*
7dad0 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 * to obtain the
7dae0 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e memory it needs.
7daf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
7db00 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d contains implem
7db10 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
7db20 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
7db30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
7db40 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 routines specifi
7db50 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ed in the sqlite
7db60 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 3_mem_methods ob
7db70 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ject..**.** $Id:
7db80 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 30 20 32 mem1.c,v 1.30 2
7db90 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 009/03/23 04:33:
7dba0 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 33 danielk1977 E
7dbb0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 xp $.*/../*.** T
7dbc0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
7dbd0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
7dbe0 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 tor is the defau
7dbf0 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 lt. It is.** us
7dc00 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 ed when no other
7dc10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
7dc20 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 r is specified u
7dc30 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d sing compile-tim
7dc40 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a e.** macros..*/.
7dc50 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 59 #ifdef SQLITE_SY
7dc60 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a STEM_MALLOC../*.
7dc70 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 ** Like malloc()
7dc80 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 20 74 , but remember t
7dc90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 he size of the a
7dca0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 llocation.** so
7dcb0 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 6e 64 that we can find
7dcc0 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e 67 20 it later using
7dcd0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 29 sqlite3MemSize()
7dce0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ..**.** For this
7dcf0 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 low-level routi
7dd00 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 72 61 ne, we are guara
7dd10 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 74 65 nteed that nByte
7dd20 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 63 61 >0 because.** ca
7dd30 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d 30 20 ses of nByte<=0
7dd40 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 65 70 will be intercep
7dd50 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 ted and dealt wi
7dd60 74 68 20 62 79 20 68 69 67 68 65 72 20 6c 65 76 th by higher lev
7dd70 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a el.** routines..
7dd80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
7dd90 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
7dda0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 (int nByte){. s
7ddb0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b qlite3_int64 *p;
7ddc0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
7ddd0 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 >0 );. nByte =
7dde0 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 ROUND8(nByte);.
7ddf0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 p = malloc( nBy
7de00 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20 te+8 );. if( p
7de10 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 ){. p[0] = nB
7de20 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 yte;. p++;.
7de30 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
7de40 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c *)p;.}../*.** L
7de50 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20 77 ike free() but w
7de60 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 orks for allocat
7de70 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66 72 ions obtained fr
7de80 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c om sqlite3MemMal
7de90 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 loc().** or sqli
7dea0 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 2e te3MemRealloc().
7deb0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 .**.** For this
7dec0 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e low-level routin
7ded0 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e e, we already kn
7dee0 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d ow that pPrior!=
7def0 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65 73 0 since.** cases
7df00 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d 30 where pPrior==0
7df10 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 will have been
7df20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 64 intecepted and d
7df30 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79 20 ealt with.** by
7df40 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 higher-level rou
7df50 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tines..*/.static
7df60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
7df70 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
7df80 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e r){. sqlite3_in
7df90 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 t64 *p = (sqlite
7dfa0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
7dfb0 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f . assert( pPrio
7dfc0 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 r!=0 );. p--;.
7dfd0 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a free(p);.}../*.
7dfe0 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 ** Like realloc(
7dff0 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61 6c ). Resize an al
7e000 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 location previou
7e010 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f sly obtained fro
7e020 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 4d m.** sqlite3MemM
7e030 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 alloc()..**.** F
7e040 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 or this low-leve
7e050 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 20 l interface, we
7e060 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 know that pPrior
7e070 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65 72 !=0. Cases wher
7e080 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 77 e.** pPrior==0 w
7e090 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20 69 hile have been i
7e0a0 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 ntercepted by hi
7e0b0 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 gher-level routi
7e0c0 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72 65 ne and.** redire
7e0d0 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e cted to xMalloc.
7e0e0 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 20 Similarly, we
7e0f0 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 3e know that nByte>
7e100 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63 61 0 becauses.** ca
7e110 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 3c ses where nByte<
7e120 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 =0 will have bee
7e130 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 n intercepted by
7e140 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a higher-level.**
7e150 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72 65 routines and re
7e160 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72 65 directed to xFre
7e170 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
7e180 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 d *sqlite3MemRea
7e190 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f lloc(void *pPrio
7e1a0 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 r, int nByte){.
7e1b0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
7e1c0 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 p = (sqlite3_int
7e1d0 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 64*)pPrior;. as
7e1e0 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 sert( pPrior!=0
7e1f0 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 && nByte>0 );.
7e200 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e nByte = ROUND8(n
7e210 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28 73 71 Byte);. p = (sq
7e220 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
7e230 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 ior;. p--;. p
7e240 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 = realloc(p, nBy
7e250 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20 te+8 );. if( p
7e260 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 ){. p[0] = nB
7e270 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 yte;. p++;.
7e280 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
7e290 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 *)p;.}../*.** Re
7e2a0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 port the allocat
7e2b0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69 ed size of a pri
7e2c0 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78 or return from x
7e2d0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 Malloc().** or x
7e2e0 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 Realloc()..*/.st
7e2f0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
7e300 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 MemSize(void *pP
7e310 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 rior){. sqlite3
7e320 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28 _int64 *p;. if(
7e330 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74 pPrior==0 ) ret
7e340 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71 urn 0;. p = (sq
7e350 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
7e360 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 ior;. p--;. re
7e370 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a turn (int)p[0];.
7e380 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 }../*.** Round u
7e390 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 p a request size
7e3a0 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c to the next val
7e3b0 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 id allocation si
7e3c0 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ze..*/.static in
7e3d0 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
7e3e0 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 dup(int n){. re
7e3f0 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a turn ROUND8(n);.
7e400 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
7e410 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e ize this module.
7e420 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
7e430 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f qlite3MemInit(vo
7e440 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
7e450 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
7e460 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
7e470 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
7e480 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 ../*.** Deinitia
7e490 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
7e4a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
7e4b0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
7e4c0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 own(void *NotUse
7e4d0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
7e4e0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
7e4f0 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a . return;.}../*
7e500 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
7e510 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 is the only rou
7e520 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c tine in this fil
7e530 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 e with external
7e540 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 linkage..**.** P
7e550 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d opulate the low-
7e560 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
7e570 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
7e580 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 pointers in.**
7e590 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
7e5a0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 fig.m with point
7e5b0 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 ers to the routi
7e5c0 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nes in this file
7e5d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
7e5e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7e5f0 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f MemSetDefault(vo
7e600 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
7e610 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
7e620 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d methods defaultM
7e630 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
7e640 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
7e650 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
7e660 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 mFree,. sqli
7e670 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 te3MemRealloc,.
7e680 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 sqlite3MemSi
7e690 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 ze,. sqlite3
7e6a0 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 MemRoundup,.
7e6b0 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c sqlite3MemInit,
7e6c0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
7e6d0 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 Shutdown,. 0
7e6e0 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f . };. sqlite3_
7e6f0 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
7e700 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 NFIG_MALLOC, &de
7e710 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d faultMethods);.}
7e720 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
7e730 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 TE_SYSTEM_MALLOC
7e740 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
7e750 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 31 **** End of mem1
7e760 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
7e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e790 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
7e7a0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
7e7b0 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem2.c *********
7e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e7e0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
7e7f0 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 August 15.**.**
7e800 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
7e810 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
7e820 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
7e830 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
7e840 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
7e850 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
7e860 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
7e870 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
7e880 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
7e890 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
7e8a0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
7e8b0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
7e8c0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
7e8d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
7e8e0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
7e8f0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
7e900 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e950 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
7e960 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
7e970 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f s low-level memo
7e980 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
7e990 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a ivers for when.*
7e9a0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
7e9b0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 e the standard C
7e9c0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f -library malloc/
7e9d0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 realloc/free int
7e9e0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 erface.** to obt
7e9f0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 ain the memory i
7ea00 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61 64 t needs while ad
7ea10 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64 64 ding lots of add
7ea20 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69 6e itional debuggin
7ea30 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e g.** information
7ea40 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61 74 to each allocat
7ea50 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ion in order to
7ea60 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64 20 help detect and
7ea70 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 fix memory.** le
7ea80 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20 75 aks and memory u
7ea90 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a sage errors..**.
7eaa0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
7eab0 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 tains implementa
7eac0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 tions of the low
7ead0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
7eae0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 location.** rout
7eaf0 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 ines specified i
7eb00 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 n the sqlite3_me
7eb10 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 m_methods object
7eb20 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d ..**.** $Id: mem
7eb30 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 30 39 2f 2.c,v 1.45 2009/
7eb40 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64 03/23 04:33:33 d
7eb50 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
7eb60 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .*/../*.** This
7eb70 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
7eb80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
7eb90 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 is used only if
7eba0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 the.** SQLITE_ME
7ebb0 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 MDEBUG macro is
7ebc0 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 defined.*/.#ifde
7ebd0 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 f SQLITE_MEMDEBU
7ebe0 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63 G../*.** The bac
7ebf0 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 61 ktrace functiona
7ec00 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61 76 61 lity is only ava
7ec10 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c 49 42 ilable with GLIB
7ec20 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c C.*/.#ifdef __GL
7ec30 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69 IBC__. extern i
7ec40 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76 6f 69 nt backtrace(voi
7ec50 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65 d**,int);. exte
7ec60 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 61 63 rn void backtrac
7ec70 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 e_symbols_fd(voi
7ec80 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 d*const*,int,int
7ec90 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
7eca0 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c 42 29 e backtrace(A,B)
7ecb0 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 1.# define back
7ecc0 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 trace_symbols_fd
7ecd0 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a (A,B,C).#endif..
7ece0 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72 /*.** Each memor
7ecf0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f y allocation loo
7ed00 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a ks like this:.**
7ed10 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** -----------
7ed20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
7ed60 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 61 63 | Title | bac
7ed70 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 20 ktrace pointers
7ed80 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c | MemBlockHdr |
7ed90 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 allocation |
7eda0 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d EndGuard |.** -
7edb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ede0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edf0 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 -------.**.** Th
7ee00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f e application co
7ee10 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 20 70 de sees only a p
7ee20 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c ointer to the al
7ee30 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 68 61 location. We ha
7ee40 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70 ve.** to back up
7ee50 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61 from the alloca
7ee60 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 tion pointer to
7ee70 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 find the MemBloc
7ee80 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 kHdr. The.** Me
7ee90 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20 mBlockHdr tells
7eea0 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 us the size of t
7eeb0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e he allocation an
7eec0 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a d the number of.
7eed0 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 6f 69 ** backtrace poi
7eee0 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 69 73 nters. There is
7eef0 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 77 6f also a guard wo
7ef00 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 rd at the end of
7ef10 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b the.** MemBlock
7ef20 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d Hdr..*/.struct M
7ef30 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69 emBlockHdr {. i
7ef40 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20 64 iSize;
7ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ef60 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
7ef70 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f is allocation */
7ef80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
7ef90 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 ckHdr *pNext, *p
7efa0 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 Prev; /* Linked
7efb0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 list of all unf
7efc0 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 reed memory */.
7efd0 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 char nBacktrace
7efe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7eff0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
7f000 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 f backtraces on
7f010 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 this alloc */.
7f020 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 53 char nBacktraceS
7f030 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 20 20 lots;
7f040 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 /* Available
7f050 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f 74 73 backtrace slots
7f060 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54 69 74 */. short nTit
7f070 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
7f080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
7f090 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63 es of title; inc
7f0a0 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20 ludes '\0' */.
7f0b0 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64 3b 20 int iForeGuard;
7f0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f0d0 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 /* Guard wor
7f0e0 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a d for sanity */.
7f0f0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 };../*.** Guard
7f100 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 words.*/.#define
7f110 20 46 4f 52 45 47 55 41 52 44 20 30 78 38 30 46 FOREGUARD 0x80F
7f120 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20 52 45 5E153.#define RE
7f130 41 52 47 55 41 52 44 20 30 78 45 34 36 37 36 42 ARGUARD 0xE4676B
7f140 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 53../*.** Number
7f150 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 of malloc size
7f160 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 increments to tr
7f170 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ack..*/.#define
7f180 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a NCSIZE 1000../*
7f190 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 .** All of the s
7f1a0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
7f1b0 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 used by this mod
7f1c0 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 ule are collecte
7f1d0 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 d.** into a sing
7f1e0 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d le structure nam
7f1f0 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 ed "mem". This
7f200 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a is to keep the.*
7f210 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c * static variabl
7f220 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 es organized and
7f230 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 to reduce names
7f240 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a pace pollution.*
7f250 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 * when this modu
7f260 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 le is combined w
7f270 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 ith other in the
7f280 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a amalgamation..*
7f290 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
7f2a0 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d {. . /*. ** M
7f2b0 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 utex to control
7f2c0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 access to the me
7f2d0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
7f2e0 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a subsystem.. */.
7f2f0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
7f300 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 *mutex;.. /*.
7f310 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 69 6c ** Head and tail
7f320 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 of a linked lis
7f330 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e t of all outstan
7f340 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 ding allocations
7f350 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d . */. struct M
7f360 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72 emBlockHdr *pFir
7f370 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d st;. struct Mem
7f380 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b BlockHdr *pLast;
7f390 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 . . /*. ** Th
7f3a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 e number of leve
7f3b0 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 ls of backtrace
7f3c0 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77 20 61 to save in new a
7f3d0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f llocations.. */
7f3e0 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72 61 63 . int nBacktrac
7f3f0 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 61 63 e;. void (*xBac
7f400 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 ktrace)(int, int
7f410 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f , void **);.. /
7f420 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74 65 78 *. ** Title tex
7f430 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 20 66 t to insert in f
7f440 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62 6c 6f ront of each blo
7f450 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 ck. */. int nT
7f460 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 itle; /*
7f470 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 Bytes of zTitle
7f480 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 to save. Includ
7f490 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61 64 64 es '\0' and padd
7f4a0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 ing */. char zT
7f4b0 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 itle[100]; /* T
7f4c0 68 65 20 74 69 74 6c 65 20 74 65 78 74 20 2a 2f he title text */
7f4d0 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c .. /* . ** sql
7f4e0 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c ite3MallocDisall
7f4f0 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 ow() increments
7f500 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
7f510 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 unter.. ** sqli
7f520 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 te3MallocAllow()
7f530 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a decrements it..
7f540 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c */. int disal
7f550 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 low; /* Do not a
7f560 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f llow memory allo
7f570 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a cation */.. /*.
7f580 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74 61 74 ** Gather stat
7f590 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20 73 69 istics on the si
7f5a0 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c zes of memory al
7f5b0 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 locations.. **
7f5c0 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65 nAlloc[i] is the
7f5d0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
7f5e0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f ation attempts o
7f5f0 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73 f i*8. ** bytes
7f600 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20 . i==NCSIZE is
7f610 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c the number of al
7f620 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 location attempt
7f630 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73 s for. ** sizes
7f640 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53 49 5a more than NCSIZ
7f650 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a E*8 bytes.. */.
7f660 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 int nAlloc[NCS
7f670 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f IZE]; /* To
7f680 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c tal number of al
7f690 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 locations */. i
7f6a0 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 nt nCurrent[NCSI
7f6b0 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 ZE]; /* Curre
7f6c0 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c nt number of all
7f6d0 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e ocations */. in
7f6e0 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 t mxCurrent[NCSI
7f6f0 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 ZE]; /* Highwa
7f700 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 ter mark for nCu
7f710 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b rrent */..} mem;
7f720 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 .../*.** Adjust
7f730 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73 74 61 memory usage sta
7f740 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69 tistics.*/.stati
7f750 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53 74 61 c void adjustSta
7f760 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e ts(int iSize, in
7f770 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20 t increment){.
7f780 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38 28 69 int i = ROUND8(i
7f790 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28 20 69 Size)/8;. if( i
7f7a0 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 >NCSIZE-1 ){.
7f7b0 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b i = NCSIZE - 1;
7f7c0 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63 72 65 . }. if( incre
7f7d0 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65 ment>0 ){. me
7f7e0 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 m.nAlloc[i]++;.
7f7f0 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b mem.nCurrent[
7f800 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65 i]++;. if( me
7f810 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 m.nCurrent[i]>me
7f820 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 m.mxCurrent[i] )
7f830 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75 {. mem.mxCu
7f840 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e rrent[i] = mem.n
7f850 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 Current[i];.
7f860 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d }. }else{. m
7f870 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d em.nCurrent[i]--
7f880 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 ;. assert( me
7f890 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 m.nCurrent[i]>=0
7f8a0 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a );. }.}../*.**
7f8b0 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 Given an alloca
7f8c0 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d tion, find the M
7f8d0 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 emBlockHdr for t
7f8e0 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a hat allocation..
7f8f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
7f900 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20 67 75 ne checks the gu
7f910 61 72 64 73 20 61 74 20 65 69 74 68 65 72 20 65 ards at either e
7f920 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 nd of the alloca
7f930 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 tion and.** if t
7f940 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72 65 63 hey are incorrec
7f950 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f t it asserts..*/
7f960 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 4d .static struct M
7f970 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 emBlockHdr *sqli
7f980 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 te3MemsysGetHead
7f990 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 er(void *pAlloca
7f9a0 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 tion){. struct
7f9b0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a MemBlockHdr *p;.
7f9c0 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 int *pInt;. u
7f9d0 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 8 *pU8;. int nR
7f9e0 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 eserve;.. p = (
7f9f0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
7fa00 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b dr*)pAllocation;
7fa10 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 . p--;. assert
7fa20 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d ( p->iForeGuard=
7fa30 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52 44 20 =(int)FOREGUARD
7fa40 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 );. nReserve =
7fa50 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a 65 29 ROUND8(p->iSize)
7fa60 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a ;. pInt = (int*
7fa70 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 )pAllocation;.
7fa80 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c 6c 6f pU8 = (u8*)pAllo
7fa90 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65 72 74 cation;. assert
7faa0 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f ( pInt[nReserve/
7fab0 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 sizeof(int)]==(i
7fac0 6e 74 29 52 45 41 52 47 55 41 52 44 20 29 3b 0a nt)REARGUARD );.
7fad0 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63 6b 73 /* This checks
7fae0 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65 78 74 any of the "ext
7faf0 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 ra" bytes alloca
7fb00 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74 6f 20 ted due. ** to
7fb10 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f 20 61 rounding up to a
7fb20 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72 n 8 byte boundar
7fb30 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20 20 2a y to ensure . *
7fb40 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74 20 62 * they haven't b
7fb50 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 2e een overwritten.
7fb60 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e . */. while( n
7fb70 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 Reserve-- > p->i
7fb80 53 69 7a 65 20 29 20 61 73 73 65 72 74 28 20 70 Size ) assert( p
7fb90 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d 30 78 U8[nReserve]==0x
7fba0 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 65 );. return p
7fbb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
7fbc0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
7fbd0 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79 20 bytes currently
7fbe0 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64 64 allocated at add
7fbf0 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 ress p..*/.stati
7fc00 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
7fc10 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Size(void *p){.
7fc20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
7fc30 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66 28 Hdr *pHdr;. if(
7fc40 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !p ){. retur
7fc50 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72 20 n 0;. }. pHdr
7fc60 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 = sqlite3MemsysG
7fc70 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20 72 etHeader(p);. r
7fc80 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69 7a eturn pHdr->iSiz
7fc90 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 e;.}../*.** Init
7fca0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
7fcb0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
7fcc0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
7fcd0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
7fce0 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 Init(void *NotUs
7fcf0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
7fd00 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
7fd10 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 69 7a ;. assert( (siz
7fd20 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d 42 6c eof(struct MemBl
7fd30 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20 30 20 ockHdr)&7) == 0
7fd40 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 );. if( !sqlite
7fd50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
7fd60 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 2f 2a emstat ){. /*
7fd70 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61 74 75 If memory statu
7fd80 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 s is enabled, th
7fd90 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 en the malloc.c
7fda0 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61 6c 72 wrapper will alr
7fdb0 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 eady. ** hold
7fdc0 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
7fdd0 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65 20 72 mutex when the r
7fde0 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61 72 65 outines here are
7fdf0 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 invoked. */.
7fe00 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c mem.mutex = sql
7fe10 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
7fe20 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
7fe30 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 IC_MEM);. }. r
7fe40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
7fe50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
7fe60 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
7fe70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
7fe80 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
7fe90 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 c void sqlite3Me
7fea0 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a mShutdown(void *
7feb0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
7fec0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
7fed0 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 Used);. mem.mut
7fee0 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ex = 0;.}../*.**
7fef0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 Round up a requ
7ff00 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 est size to the
7ff10 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 next valid alloc
7ff20 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 ation size..*/.s
7ff30 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
7ff40 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 3MemRoundup(int
7ff50 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 n){. return ROU
7ff60 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ND8(n);.}../*.**
7ff70 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 Allocate nByte
7ff80 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e bytes of memory.
7ff90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7ffa0 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f *sqlite3MemMallo
7ffb0 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 c(int nByte){.
7ffc0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
7ffd0 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 dr *pHdr;. void
7ffe0 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20 2a **pBt;. char *
7fff0 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a z;. int *pInt;.
80000 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 void *p = 0;.
80010 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b 0a int totalSize;.
80020 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a int nReserve;.
80030 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
80040 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 enter(mem.mutex)
80050 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e ;. assert( mem.
80060 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 disallow==0 );.
80070 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e nReserve = ROUN
80080 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 6f 74 D8(nByte);. tot
80090 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 72 76 alSize = nReserv
800a0 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 e + sizeof(*pHdr
800b0 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 ) + sizeof(int)
800c0 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
800d0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2a mem.nBacktrace*
800e0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 sizeof(void*) +
800f0 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 70 20 mem.nTitle;. p
80100 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 = malloc(totalSi
80110 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a ze);. if( p ){.
80120 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20 70 z = p;. p
80130 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 7a 5b Bt = (void**)&z[
80140 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 mem.nTitle];.
80150 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 74 20 pHdr = (struct
80160 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 70 42 MemBlockHdr*)&pB
80170 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 t[mem.nBacktrace
80180 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 ];. pHdr->pNe
80190 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 64 72 xt = 0;. pHdr
801a0 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e 70 4c ->pPrev = mem.pL
801b0 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d ast;. if( mem
801c0 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 .pLast ){.
801d0 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 mem.pLast->pNext
801e0 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 65 6c = pHdr;. }el
801f0 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 46 se{. mem.pF
80200 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 irst = pHdr;.
80210 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 }. mem.pLast
80220 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 48 64 = pHdr;. pHd
80230 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 3d 20 r->iForeGuard =
80240 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 20 70 FOREGUARD;. p
80250 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 Hdr->nBacktraceS
80260 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b lots = mem.nBack
80270 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 72 2d trace;. pHdr-
80280 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 >nTitle = mem.nT
80290 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 6d 65 itle;. if( me
802a0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a m.nBacktrace ){.
802b0 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 64 64 void *aAdd
802c0 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 48 64 r[40];. pHd
802d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 r->nBacktrace =
802e0 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 72 2c backtrace(aAddr,
802f0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b mem.nBacktrace+
80300 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 6d 63 1)-1;. memc
80310 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 5b 31 py(pBt, &aAddr[1
80320 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 ], pHdr->nBacktr
80330 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a ace*sizeof(void*
80340 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ));. assert
80350 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 (pBt[0]);.
80360 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 if( mem.xBacktra
80370 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ce ){. me
80380 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42 79 m.xBacktrace(nBy
80390 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 te, pHdr->nBackt
803a0 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b 31 race-1, &aAddr[1
803b0 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
803c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48 64 }else{. pHd
803d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 r->nBacktrace =
803e0 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 0;. }. if(
803f0 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 mem.nTitle ){.
80400 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d memcpy(z, m
80410 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e em.zTitle, mem.n
80420 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 Title);. }.
80430 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20 pHdr->iSize =
80440 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75 73 nByte;. adjus
80450 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b 31 tStats(nByte, +1
80460 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28 69 );. pInt = (i
80470 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20 nt*)&pHdr[1];.
80480 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f pInt[nReserve/
80490 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52 sizeof(int)] = R
804a0 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d 65 EARGUARD;. me
804b0 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 2c mset(pInt, 0x65,
804c0 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20 nReserve);.
804d0 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b p = (void*)pInt;
804e0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
804f0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d utex_leave(mem.m
80500 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
80510 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p; .}../*.** Fre
80520 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 e memory..*/.sta
80530 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
80540 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
80550 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74 20 rior){. struct
80560 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 MemBlockHdr *pHd
80570 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b r;. void **pBt;
80580 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73 . char *z;. as
80590 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f sert( sqlite3Glo
805a0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
805b0 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 at || mem.mutex!
805c0 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 73 =0 );. pHdr = s
805d0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
805e0 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 eader(pPrior);.
805f0 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 pBt = (void**)p
80600 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48 Hdr;. pBt -= pH
80610 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
80620 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ots;. sqlite3_m
80630 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d utex_enter(mem.m
80640 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48 64 utex);. if( pHd
80650 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 r->pPrev ){.
80660 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 50 assert( pHdr->pP
80670 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72 rev->pNext==pHdr
80680 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 );. pHdr->pP
80690 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 rev->pNext = pHd
806a0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 r->pNext;. }els
806b0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d e{. assert( m
806c0 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72 20 em.pFirst==pHdr
806d0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 );. mem.pFirs
806e0 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b t = pHdr->pNext;
806f0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72 2d . }. if( pHdr-
80700 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 >pNext ){. as
80710 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65 78 sert( pHdr->pNex
80720 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20 29 t->pPrev==pHdr )
80730 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 ;. pHdr->pNex
80740 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72 2d t->pPrev = pHdr-
80750 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b >pPrev;. }else{
80760 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d . assert( mem
80770 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a .pLast==pHdr );.
80780 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 mem.pLast =
80790 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d pHdr->pPrev;. }
807a0 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 42 . z = (char*)pB
807b0 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e t;. z -= pHdr->
807c0 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74 nTitle;. adjust
807d0 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69 7a Stats(pHdr->iSiz
807e0 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74 e, -1);. memset
807f0 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f 66 (z, 0x2b, sizeof
80800 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 (void*)*pHdr->nB
80810 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b 20 acktraceSlots +
80820 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a sizeof(*pHdr) +.
80830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80840 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20 pHdr->iSize +
80850 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 48 sizeof(int) + pH
80860 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66 dr->nTitle);. f
80870 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65 ree(z);. sqlite
80880 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
80890 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f m.mutex); .}../
808a0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
808b0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
808c0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
808d0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ation..**.** For
808e0 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67 20 this debugging
808f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
80900 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65 we *always* make
80910 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
80920 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 * allocation int
80930 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69 6e o a new place in
80940 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69 memory. In thi
80950 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a 2a s way, if the .*
80960 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 63 * higher level c
80970 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f 69 ode is using poi
80980 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64 20 nter to the old
80990 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69 allocation, it i
809a0 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 s .** much more
809b0 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b 20 likely to break
809c0 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68 20 and we are much
809d0 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66 more liking to f
809e0 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 ind.** the error
809f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
80a00 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c *sqlite3MemReal
80a10 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
80a20 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
80a30 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
80a40 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76 dr *pOldHdr;. v
80a50 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73 oid *pNew;. ass
80a60 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f ert( mem.disallo
80a70 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64 w==0 );. pOldHd
80a80 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 r = sqlite3Memsy
80a90 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f sGetHeader(pPrio
80aa0 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c r);. pNew = sql
80ab0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 ite3MemMalloc(nB
80ac0 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 yte);. if( pNew
80ad0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ){. memcpy(p
80ae0 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 New, pPrior, nBy
80af0 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a te<pOldHdr->iSiz
80b00 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64 e ? nByte : pOld
80b10 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 Hdr->iSize);.
80b20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48 if( nByte>pOldH
80b30 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 dr->iSize ){.
80b40 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 memset(&((cha
80b50 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72 r*)pNew)[pOldHdr
80b60 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20 ->iSize], 0x2b,
80b70 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d nByte - pOldHdr-
80b80 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 >iSize);. }.
80b90 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
80ba0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 e(pPrior);. }.
80bb0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
80bc0 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 ./*.** Populate
80bd0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
80be0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
80bf0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
80c00 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 s in.** sqlite3G
80c10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 lobalConfig.m wi
80c20 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
80c30 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 he routines in t
80c40 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c his file..*/.SQL
80c50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
80c60 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
80c70 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 fault(void){. s
80c80 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
80c90 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
80ca0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
80cb0 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
80cc0 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 emMalloc,. s
80cd0 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 qlite3MemFree,.
80ce0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 sqlite3MemRe
80cf0 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
80d00 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 te3MemSize,.
80d10 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 sqlite3MemRound
80d20 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 up,. sqlite3
80d30 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 MemInit,. sq
80d40 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e lite3MemShutdown
80d50 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
80d60 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
80d70 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
80d80 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 LOC, &defaultMet
80d90 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 hods);.}../*.**
80da0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f Set the number o
80db0 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 65 f backtrace leve
80dc0 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 68 ls kept for each
80dd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 allocation..**
80de0 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20 A value of zero
80df0 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 72 turns off backtr
80e00 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62 acing. The numb
80e10 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f 75 er is always rou
80e20 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20 nded.** up to a
80e30 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a multiple of 2..*
80e40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
80e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
80e60 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 69 debugBacktrace(i
80e70 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 28 nt depth){. if(
80e80 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 74 depth<0 ){ dept
80e90 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 64 h = 0; }. if( d
80ea0 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 68 epth>20 ){ depth
80eb0 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 68 = 20; }. depth
80ec0 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 66 = (depth+1)&0xf
80ed0 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 e;. mem.nBacktr
80ee0 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a ace = depth;.}..
80ef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
80f00 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 oid sqlite3Memde
80f10 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c 6c bugBacktraceCall
80f20 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 63 back(void (*xBac
80f30 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 ktrace)(int, int
80f40 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d , void **)){. m
80f50 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d 20 em.xBacktrace =
80f60 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f xBacktrace;.}../
80f70 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 74 *.** Set the tit
80f80 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 75 le string for su
80f90 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 bsequent allocat
80fa0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
80fb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
80fc0 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 74 ite3MemdebugSett
80fd0 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 itle(const char
80fe0 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73 69 *zTitle){. unsi
80ff0 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71 6c gned int n = sql
81000 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 69 ite3Strlen30(zTi
81010 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 tle) + 1;. sqli
81020 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
81030 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 mem.mutex);. if
81040 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e ( n>=sizeof(mem.
81050 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 zTitle) ) n = si
81060 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 zeof(mem.zTitle)
81070 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d -1;. memcpy(mem
81080 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c .zTitle, zTitle,
81090 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c n);. mem.zTitl
810a0 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e e[n] = 0;. mem.
810b0 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38 28 nTitle = ROUND8(
810c0 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 n);. sqlite3_mu
810d0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 tex_leave(mem.mu
810e0 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f tex);.}..SQLITE_
810f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
81100 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 ite3MemdebugSync
81110 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d (){. struct Mem
81120 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a BlockHdr *pHdr;.
81130 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 for(pHdr=mem.p
81140 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64 First; pHdr; pHd
81150 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a r=pHdr->pNext){.
81160 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d void **pBt =
81170 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 (void**)pHdr;.
81180 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e pBt -= pHdr->
81190 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
811a0 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 . mem.xBacktr
811b0 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c ace(pHdr->iSize,
811c0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
811d0 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 e-1, &pBt[1]);.
811e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e }.}../*.** Open
811f0 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 the file indica
81200 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 ted and write a
81210 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 log of all unfre
81220 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c ed memory .** al
81230 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 locations into t
81240 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 hat log..*/.SQLI
81250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
81260 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 44 sqlite3MemdebugD
81270 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ump(const char *
81280 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 zFilename){. FI
81290 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 LE *out;. struc
812a0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
812b0 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 Hdr;. void **pB
812c0 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 t;. int i;. ou
812d0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e t = fopen(zFilen
812e0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 ame, "w");. if(
812f0 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 out==0 ){. f
81300 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
81310 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 ** Unable to out
81320 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 put memory debug
81330 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 output log: %s
81340 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 **\n",.
81350 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 zFile
81360 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 name);. retur
81370 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 n;. }. for(pHd
81380 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 r=mem.pFirst; pH
81390 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 dr; pHdr=pHdr->p
813a0 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 20 Next){. char
813b0 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 72 *z = (char*)pHdr
813c0 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d ;. z -= pHdr-
813d0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
813e0 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b *sizeof(void*) +
813f0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 pHdr->nTitle;.
81400 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
81410 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 "**** %lld bytes
81420 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 2a at %p from %s *
81430 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 ***\n", .
81440 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
81450 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 , &pHdr[1], pHdr
81460 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 ->nTitle ? z : "
81470 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 70 ???");. if( p
81480 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
81490 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 28 ){. fflush(
814a0 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 20 out);. pBt
814b0 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a = (void**)pHdr;.
814c0 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 pBt -= pHd
814d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
814e0 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 72 ts;. backtr
814f0 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 ace_symbols_fd(p
81500 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 Bt, pHdr->nBackt
81510 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 race, fileno(out
81520 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ));. fprint
81530 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 f(out, "\n");.
81540 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 }. }. fprint
81550 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c f(out, "COUNTS:\
81560 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 n");. for(i=0;
81570 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 i<NCSIZE-1; i++)
81580 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 {. if( mem.nA
81590 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 lloc[i] ){.
815a0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
815b0 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 64 %5d: %10d %10d
815c0 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 %10d\n", .
815d0 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e i*8, mem.
815e0 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e nAlloc[i], mem.n
815f0 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e Current[i], mem.
81600 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 mxCurrent[i]);.
81610 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d }. }. if( m
81620 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 em.nAlloc[NCSIZE
81630 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 6e -1] ){. fprin
81640 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a tf(out, " %5d:
81650 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 5c %10d %10d %10d\
81660 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
81670 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d NCSIZE*8-8, mem
81680 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 .nAlloc[NCSIZE-1
81690 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ],.
816a0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 mem.nCurrent[NCS
816b0 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 IZE-1], mem.mxCu
816c0 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 rrent[NCSIZE-1])
816d0 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f ;. }. fclose(o
816e0 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ut);.}../*.** Re
816f0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
81700 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 33 of times sqlite3
81710 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 MemMalloc() has
81720 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a been called..*/.
81730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81740 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 nt sqlite3Memdeb
81750 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b ugMallocCount(){
81760 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
81770 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f nTotal = 0;. fo
81780 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b r(i=0; i<NCSIZE;
81790 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 i++){. nTota
817a0 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b l += mem.nAlloc[
817b0 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e i];. }. return
817c0 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e nTotal;.}...#en
817d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 dif /* SQLITE_ME
817e0 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a MDEBUG */../****
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
81800 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a f mem2.c *******
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
81840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
81850 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a file mem3.c ***
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
81890 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 2007 October 14
818a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
818b0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
818c0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
818d0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
818e0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
818f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
81900 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
81910 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
81920 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
81930 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
81940 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
81950 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
81960 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
81970 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
81980 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
81990 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
819a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
819b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
81a00 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
81a10 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
81a20 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
81a30 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a ment a memory.**
81a40 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
81a50 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 ystem for use by
81a60 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 SQLite. .**.**
81a70 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
81a80 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
81a90 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
81aa0 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 omits all.** use
81ab0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 of malloc(). Th
81ac0 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 e SQLite user su
81ad0 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f pplies a block o
81ae0 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f f memory.** befo
81af0 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 re calling sqlit
81b00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
81b10 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 from which alloc
81b20 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 ations.** are ma
81b30 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 de and returned
81b40 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 by the xMalloc()
81b50 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 and xRealloc()
81b60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
81b70 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 ons. Once sqlite
81b80 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 3_initialize() h
81b90 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a as been called,.
81ba0 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ** the amount of
81bb0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c memory availabl
81bc0 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 e to SQLite is f
81bd0 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a ixed and cannot.
81be0 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a ** be changed..*
81bf0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
81c00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
81c10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
81c20 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 stem is included
81c30 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 .** in the build
81c40 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f only if SQLITE_
81c50 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 ENABLE_MEMSYS3 i
81c60 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a s defined..**.**
81c70 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 $Id: mem3.c,v 1
81c80 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39 20 31 .25 2008/11/19 1
81c90 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 6:52:44 danielk1
81ca0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 977 Exp $.*/../*
81cb0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
81cc0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
81cd0 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 llocator is only
81ce0 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 built into the
81cf0 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 library.** SQLIT
81d00 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 E_ENABLE_MEMSYS3
81d10 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66 is defined. Def
81d20 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f ining this symbo
81d30 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 l does not.** me
81d40 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 an that the libr
81d50 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d ary will use a m
81d60 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 emory-pool by de
81d70 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 fault, just that
81d80 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61 .** it is availa
81d90 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c ble. The mempool
81da0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 allocator is ac
81db0 74 69 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69 tivated by calli
81dc0 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ng.** sqlite3_co
81dd0 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 nfig()..*/.#ifde
81de0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
81df0 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d MEMSYS3../*.** M
81e00 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 aximum size (in
81e10 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 Mem3Blocks) of a
81e20 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a "small" chunk..
81e30 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d */.#define MX_SM
81e40 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e ALL 10.../*.** N
81e50 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 umber of freelis
81e60 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a t hash slots.*/.
81e70 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 #define N_HASH
81e80 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 61../*.** A memo
81e90 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 ry allocation (a
81ea0 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68 lso called a "ch
81eb0 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f unk") consists o
81ec0 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 f two or .** mor
81ed0 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 e blocks where e
81ee0 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 ach block is 8 b
81ef0 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 ytes. The first
81f00 20 38 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 8 bytes are .**
81f10 20 61 20 68 65 61 64 65 72 20 74 68 61 74 20 69 a header that i
81f20 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74 s not returned t
81f30 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a o the user..**.*
81f40 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f * A chunk is two
81f50 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 or more blocks
81f60 74 68 61 74 20 69 73 20 65 69 74 68 65 72 20 63 that is either c
81f70 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a hecked out or.**
81f80 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 72 73 free. The firs
81f90 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d t block has form
81fa0 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 at u.hdr. u.hdr
81fb0 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d .size4x is 4 tim
81fc0 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f es the.** size o
81fd0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
81fe0 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 in blocks if th
81ff0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 e allocation is
82000 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 free..** The u.h
82010 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20 dr.size4x&1 bit
82020 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63 is true if the c
82030 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 hunk is checked
82040 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 out and.** false
82050 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 if the chunk is
82060 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
82070 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a . The u.hdr.siz
82080 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 e4x&2 bit.** is
82090 74 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76 true if the prev
820a0 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 ious chunk is ch
820b0 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61 ecked out and fa
820c0 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 lse if the.** pr
820d0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
820e0 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72 free. The u.hdr
820f0 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20 .prevSize field
82100 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a is the size of.*
82110 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 * the previous c
82120 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 hunk in blocks i
82130 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
82140 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a hunk is on the.*
82150 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74 * freelist. If t
82160 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e he previous chun
82170 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 k is checked out
82180 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e , then.** u.hdr.
82190 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20 prevSize can be
821a0 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
821b0 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20 for that chunk
821c0 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f and should.** no
821d0 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 t be read or wri
821e0 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f tten..**.** We o
821f0 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20 ften identify a
82200 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 chunk by its ind
82210 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
82220 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 []. When.** thi
82230 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 s is done, the c
82240 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72 hunk index refer
82250 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 s to the second
82260 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 block of.** the
82270 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 chunk. In this
82280 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 20 63 way, the first c
82290 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 hunk has an inde
822a0 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 x of 1..** A chu
822b0 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 nk index of 0 me
822c0 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75 ans "no such chu
822d0 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 20 65 nk" and is the e
822e0 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 quivalent.** of
822f0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
82300 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
82310 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 block of free c
82320 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20 hunks is of the
82330 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 form u.list. Th
82340 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 e.** two fields
82350 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 form a double-li
82360 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75 nked list of chu
82370 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73 nks of related s
82380 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 izes..** Pointer
82390 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 s to the head of
823a0 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 73 74 the list are st
823b0 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 ored in mem3.aiS
823c0 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 mall[] .** for s
823d0 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e maller chunks an
823e0 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 d mem3.aiHash[]
823f0 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b for larger chunk
82400 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 s..**.** The sec
82410 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 ond block of a c
82420 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74 hunk is user dat
82430 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 a if the chunk i
82440 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 s checked .** ou
82450 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69 t. If a chunk i
82460 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 s checked out, t
82470 68 65 20 75 73 65 72 20 64 61 74 61 20 6d 61 79 he user data may
82480 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 extend into.**
82490 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 the u.hdr.prevSi
824a0 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 ze value of the
824b0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e following chunk.
824c0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
824d0 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d ct Mem3Block Mem
824e0 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 3Block;.struct M
824f0 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 em3Block {. uni
82500 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 on {. struct
82510 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 76 {. u32 prev
82520 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 Size; /* Size
82530 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e of previous chun
82540 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 k in Mem3Block e
82550 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 lements */.
82560 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20 u32 size4x;
82570 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20 /* 4x the size
82580 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b of current chunk
82590 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c in Mem3Block el
825a0 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 ements */. }
825b0 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 hdr;. struct
825c0 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 78 74 {. u32 next
825d0 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 ; /* Index
825e0 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d in mem3.aPool[]
825f0 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 of next free ch
82600 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 unk */. u32
82610 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 prev; /*
82620 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 Index in mem3.aP
82630 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 ool[] of previou
82640 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a s free chunk */.
82650 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 } list;. }
82660 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c u;.};../*.** All
82670 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76 of the static v
82680 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 ariables used by
82690 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 this module are
826a0 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e collected.** in
826b0 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 to a single stru
826c0 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d cture named "mem
826d0 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 3". This is to
826e0 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 keep the.** stat
826f0 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 ic variables org
82700 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 anized and to re
82710 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 duce namespace p
82720 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e ollution.** when
82730 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 this module is
82740 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 combined with ot
82750 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 her in the amalg
82760 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 amation..*/.stat
82770 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 ic SQLITE_WSD st
82780 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 ruct Mem3Global
82790 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f {. /*. ** Memo
827a0 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ry available for
827b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f allocation. nPo
827c0 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ol is the size o
827d0 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a f the array. **
827e0 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 (in Mem3Blocks)
827f0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 pointed to by a
82800 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a Pool less 2.. *
82810 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 /. u32 nPool;.
82820 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f Mem3Block *aPoo
82830 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 l;.. /*. ** Tr
82840 75 65 20 69 66 20 77 65 20 61 72 65 20 65 76 61 ue if we are eva
82850 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f luating an out-o
82860 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 f-memory callbac
82870 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c k.. */. int al
82880 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a armBusy;. . /*
82890 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 . ** Mutex to c
828a0 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f ontrol access to
828b0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
828c0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
828d0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
828e0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 _mutex *mutex;.
828f0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 . /*. ** The
82900 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f minimum amount o
82910 66 20 66 72 65 65 20 73 70 61 63 65 20 74 68 61 f free space tha
82920 74 20 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a t we have seen..
82930 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 */. u32 mnMas
82940 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ter;.. /*. **
82950 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 69 iMaster is the i
82960 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74 ndex of the mast
82970 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 er chunk. Most
82980 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a new allocations.
82990 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f ** occur off o
829a0 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 f this chunk. s
829b0 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 73 zMaster is the s
829c0 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 ize (in Mem3Bloc
829d0 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ks). ** of the
829e0 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 current master.
829f0 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 69 66 iMaster is 0 if
82a00 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 there is not ma
82a10 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a ster chunk.. **
82a20 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e The master chun
82a30 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 k is not in eith
82a40 65 72 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20 er the aiHash[]
82a50 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 or aiSmall[]..
82a60 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72 */. u32 iMaster
82a70 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72 ;. u32 szMaster
82a80 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 ;.. /*. ** Arr
82a90 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 ay of lists of f
82aa0 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 ree blocks accor
82ab0 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 ding to the bloc
82ac0 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 k size . ** for
82ad0 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c smaller chunks,
82ae0 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68 or a hash on th
82af0 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 e block size for
82b00 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 larger. ** chu
82b10 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 nks.. */. u32
82b20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c aiSmall[MX_SMALL
82b30 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 -1]; /* For si
82b40 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58 zes 2 through MX
82b50 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 _SMALL, inclusiv
82b60 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73 e */. u32 aiHas
82b70 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 h[N_HASH];
82b80 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d /* For sizes M
82b90 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 X_SMALL+1 and la
82ba0 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d rger */.} mem3 =
82bb0 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a { 97535575 };..
82bc0 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f #define mem3 GLO
82bd0 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47 BAL(struct Mem3G
82be0 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a lobal, mem3)../*
82bf0 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 .** Unlink the c
82c00 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f hunk at mem3.aPo
82c10 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 ol[i] from list
82c20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a it is currently.
82c30 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 ** on. *pRoot i
82c40 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 s the list that
82c50 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 i is a member of
82c60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
82c70 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
82c80 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 omList(u32 i, u3
82c90 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 2 *pRoot){. u32
82ca0 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f next = mem3.aPo
82cb0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
82cc0 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20 t;. u32 prev =
82cd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
82ce0 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 list.prev;. ass
82cf0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
82d00 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
82d10 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65 ex) );. if( pre
82d20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f v==0 ){. *pRo
82d30 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c ot = next;. }el
82d40 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f se{. mem3.aPo
82d50 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e ol[prev].u.list.
82d60 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d next = next;. }
82d70 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 . if( next ){.
82d80 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 mem3.aPool[ne
82d90 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 xt].u.list.prev
82da0 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 = prev;. }. me
82db0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
82dc0 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d st.next = 0;. m
82dd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
82de0 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a ist.prev = 0;.}.
82df0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
82e00 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 e chunk at index
82e10 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 i from .** what
82e20 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 75 72 ever list is cur
82e30 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 rently a member
82e40 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
82e50 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
82e60 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 (u32 i){. u32 s
82e70 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 ize, hash;. ass
82e80 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
82e90 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
82ea0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
82eb0 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 (mem3.aPool[i-1
82ec0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 ].u.hdr.size4x &
82ed0 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 1)==0 );. asse
82ee0 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 rt( i>=1 );. si
82ef0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
82f00 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
82f10 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 x/4;. assert( s
82f20 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ize==mem3.aPool[
82f30 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
82f40 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 prevSize );. as
82f50 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b sert( size>=2 );
82f60 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d . if( size <= M
82f70 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d X_SMALL ){. m
82f80 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
82f90 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
82fa0 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a Small[size-2]);.
82fb0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 }else{. has
82fc0 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 h = size % N_HAS
82fd0 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e H;. memsys3Un
82fe0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
82ff0 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 &mem3.aiHash[has
83000 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a h]);. }.}../*.*
83010 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b * Link the chunk
83020 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 at mem3.aPool[i
83030 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 ] so that is on
83040 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a the list rooted.
83050 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f ** at *pRoot..*/
83060 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
83070 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 sys3LinkIntoList
83080 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f (u32 i, u32 *pRo
83090 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ot){. assert( s
830a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
830b0 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
830c0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d . mem3.aPool[i]
830d0 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a .u.list.next = *
830e0 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 pRoot;. mem3.aP
830f0 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 ool[i].u.list.pr
83100 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 ev = 0;. if( *p
83110 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 Root ){. mem3
83120 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 .aPool[*pRoot].u
83130 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a .list.prev = i;.
83140 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 }. *pRoot = i
83150 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 ;.}../*.** Link
83160 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
83170 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72 ex i into either
83180 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
83190 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 .** small chunk
831a0 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 list, or into th
831b0 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 e large chunk ha
831c0 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 sh table..*/.sta
831d0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
831e0 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 Link(u32 i){. u
831f0 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 32 size, hash;.
83200 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
83210 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 _mutex_held(mem3
83220 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
83230 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 ert( i>=1 );. a
83240 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f ssert( (mem3.aPo
83250 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
83260 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a ze4x & 1)==0 );.
83270 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
83280 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
83290 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
832a0 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 t( size==mem3.aP
832b0 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e ool[i+size-1].u.
832c0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a hdr.prevSize );.
832d0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d assert( size>=
832e0 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 2 );. if( size
832f0 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 <= MX_SMALL ){.
83300 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e memsys3LinkIn
83310 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e toList(i, &mem3.
83320 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
83330 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
83340 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
83350 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
83360 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 LinkIntoList(i,
83370 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 &mem3.aiHash[has
83380 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a h]);. }.}../*.*
83390 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f * If the STATIC_
833a0 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 MEM mutex is not
833b0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f already held, o
833c0 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 btain it now. Th
833d0 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 e mutex.** will
833e0 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 already be held
833f0 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 (obtained by cod
83400 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 e in malloc.c) i
83410 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 f.** sqlite3Glob
83420 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 alConfig.bMemSta
83430 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 t is true..*/.st
83440 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
83450 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 3Enter(void){.
83460 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
83470 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
83480 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 ==0 && mem3.mute
83490 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 x==0 ){. mem3
834a0 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 .mutex = sqlite3
834b0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
834c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
834d0 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 EM);. }. sqlit
834e0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
834f0 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 em3.mutex);.}.st
83500 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
83510 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 3Leave(void){.
83520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
83530 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b ave(mem3.mutex);
83540 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 .}../*.** Called
83550 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61 when we are una
83560 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61 ble to satisfy a
83570 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 n allocation of
83580 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 nBytes..*/.stati
83590 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 c void memsys3Ou
835a0 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 tOfMemory(int nB
835b0 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d yte){. if( !mem
835c0 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 3.alarmBusy ){.
835d0 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 mem3.alarmBus
835e0 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 y = 1;. asser
835f0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
83600 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
83610 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
83620 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
83630 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 3.mutex);. sq
83640 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
83650 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 mory(nByte);.
83660 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
83670 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 nter(mem3.mutex)
83680 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d ;. mem3.alarm
83690 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a Busy = 0;. }.}.
836a0 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 ../*.** Chunk i
836b0 69 73 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20 is a free chunk
836c0 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e that has been un
836d0 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 linked. Adjust
836e0 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 its .** size par
836f0 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 65 63 ameters for chec
83700 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e k-out and return
83710 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
83720 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 e .** user porti
83730 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e on of the chunk.
83740 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
83750 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 *memsys3Checkout
83760 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f (u32 i, u32 nBlo
83770 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 ck){. u32 x;.
83780 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
83790 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
837a0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
837b0 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 rt( i>=1 );. as
837c0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c sert( mem3.aPool
837d0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
837e0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 4x/4==nBlock );.
837f0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 assert( mem3.a
83800 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
83810 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d .u.hdr.prevSize=
83820 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d =nBlock );. x =
83830 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
83840 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 .u.hdr.size4x;.
83850 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
83860 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 .u.hdr.size4x =
83870 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 nBlock*4 | 1 | (
83880 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f x&2);. mem3.aPo
83890 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 ol[i+nBlock-1].u
838a0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 .hdr.prevSize =
838b0 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 nBlock;. mem3.a
838c0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
838d0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d .u.hdr.size4x |=
838e0 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 2;. return &me
838f0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a m3.aPool[i];.}..
83900 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69 /*.** Carve a pi
83910 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 ece off of the e
83920 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 nd of the mem3.i
83930 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 75 6e Master free chun
83940 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 k..** Return a p
83950 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
83960 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f w allocation. O
83970 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 65 72 r, if the master
83980 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 chunk.** is not
83990 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 large enough, r
839a0 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 eturn 0..*/.stat
839b0 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 ic void *memsys3
839c0 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 20 6e FromMaster(u32 n
839d0 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 Block){. assert
839e0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
839f0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
83a00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 );. assert( me
83a10 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c m3.szMaster>=nBl
83a20 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c ock );. if( nBl
83a30 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 ock>=mem3.szMast
83a40 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 er-1 ){. /* U
83a50 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 se the entire ma
83a60 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 ster */. void
83a70 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 *p = memsys3Che
83a80 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 ckout(mem3.iMast
83a90 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 er, mem3.szMaste
83aa0 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 r);. mem3.iMa
83ab0 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 ster = 0;. me
83ac0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b m3.szMaster = 0;
83ad0 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 . mem3.mnMast
83ae0 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 er = 0;. retu
83af0 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn p;. }else{.
83b00 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 /* Split the
83b10 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 master block. R
83b20 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 eturn the tail.
83b30 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c */. u32 newi,
83b40 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d x;. newi = m
83b50 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 em3.iMaster + me
83b60 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 m3.szMaster - nB
83b70 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 lock;. assert
83b80 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d ( newi > mem3.iM
83b90 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d aster+1 );. m
83ba0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
83bb0 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
83bc0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ster-1].u.hdr.pr
83bd0 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b evSize = nBlock;
83be0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
83bf0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
83c00 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
83c10 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b hdr.size4x |= 2;
83c20 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
83c30 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 newi-1].u.hdr.si
83c40 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 ze4x = nBlock*4
83c50 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a + 1;. mem3.sz
83c60 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b Master -= nBlock
83c70 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ;. mem3.aPool
83c80 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 [newi-1].u.hdr.p
83c90 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 revSize = mem3.s
83ca0 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d zMaster;. x =
83cb0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
83cc0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 .iMaster-1].u.hd
83cd0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 r.size4x & 2;.
83ce0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
83cf0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 3.iMaster-1].u.h
83d00 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 dr.size4x = mem3
83d10 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b .szMaster*4 | x;
83d20 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a . if( mem3.sz
83d30 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e Master < mem3.mn
83d40 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 Master ){.
83d50 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 mem3.mnMaster =
83d60 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 mem3.szMaster;.
83d70 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
83d80 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f (void*)&mem3.aPo
83d90 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a ol[newi];. }.}.
83da0 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 ./*.** *pRoot is
83db0 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c the head of a l
83dc0 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e ist of free chun
83dd0 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 ks of the same s
83de0 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 ize.** or same s
83df0 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 ize hash. In ot
83e00 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f her words, *pRoo
83e10 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e t is an entry in
83e20 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e either.** mem3.
83e30 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d aiSmall[] or mem
83e40 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 3.aiHash[]. .**
83e50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
83e60 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e examines all en
83e70 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76 tries on the giv
83e80 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65 en list and trie
83e90 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 s.** to coalesce
83ea0 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 77 69 each entries wi
83eb0 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 th adjacent free
83ec0 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a chunks. .**.**
83ed0 20 49 66 20 69 74 20 73 65 65 73 20 61 20 63 68 If it sees a ch
83ee0 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67 unk that is larg
83ef0 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 er than mem3.iMa
83f00 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65 ster, it replace
83f10 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e s .** the curren
83f20 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 t mem3.iMaster w
83f30 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67 ith the new larg
83f40 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 er chunk. In or
83f50 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 der for.** this
83f60 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70 mem3.iMaster rep
83f70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b lacement to work
83f80 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 , the master chu
83f90 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 nk must be.** li
83fa0 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 nked into the ha
83fb0 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74 sh tables. That
83fc0 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d is not the norm
83fd0 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 al state of.** a
83fe0 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 ffairs, of cours
83ff0 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 e. The calling
84000 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e routine must lin
84010 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 k the master.**
84020 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 chunk before inv
84030 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 oking this routi
84040 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e ne, then must un
84050 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62 link the (possib
84060 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d ly.** changed) m
84070 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 aster chunk once
84080 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 this routine ha
84090 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 s finished..*/.s
840a0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
840b0 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f s3Merge(u32 *pRo
840c0 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 ot){. u32 iNext
840d0 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c , prev, size, i,
840e0 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 x;.. assert( s
840f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
84100 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
84110 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b . for(i=*pRoot;
84120 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a i>0; i=iNext){.
84130 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 iNext = mem3
84140 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
84150 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 .next;. size
84160 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 = mem3.aPool[i-1
84170 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a ].u.hdr.size4x;.
84180 20 20 20 20 61 73 73 65 72 74 28 20 28 73 69 7a assert( (siz
84190 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 e&1)==0 );. i
841a0 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 f( (size&2)==0 )
841b0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
841c0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
841d0 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 pRoot);. a
841e0 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e ssert( i > mem3.
841f0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
84200 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 .prevSize );.
84210 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 prev = i - me
84220 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
84230 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 hdr.prevSize;.
84240 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e if( prev==iN
84250 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ext ){. i
84260 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f Next = mem3.aPoo
84270 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e l[prev].u.list.n
84280 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ext;. }.
84290 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
842a0 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69 (prev);. si
842b0 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 ze = i + size/4
842c0 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20 - prev;. x
842d0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 = mem3.aPool[pre
842e0 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 v-1].u.hdr.size4
842f0 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d x & 2;. mem
84300 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 3.aPool[prev-1].
84310 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 u.hdr.size4x = s
84320 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 ize*4 | x;.
84330 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 mem3.aPool[prev
84340 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 +size-1].u.hdr.p
84350 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a revSize = size;.
84360 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e memsys3Lin
84370 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69 k(prev);. i
84380 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c = prev;. }el
84390 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f se{. size /
843a0 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 4;. }. i
843b0 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d f( size>mem3.szM
843c0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
843d0 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b em3.iMaster = i;
843e0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
843f0 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 ster = size;.
84400 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
84410 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f Return a block o
84420 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c f memory of at l
84430 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 east nBytes in s
84440 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e ize..** Return N
84450 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a ULL if unable..*
84460 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
84470 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
84480 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 the necessary mu
84490 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 texes, if any, a
844a0 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 re.** already he
844b0 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ld by the caller
844c0 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 . Hence "Unsafe"
844d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
844e0 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 *memsys3MallocU
844f0 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 nsafe(int nByte)
84500 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 33 32 {. u32 i;. u32
84510 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74 nBlock;. u32 t
84520 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 oFree;.. assert
84530 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
84540 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
84550 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 );. assert( si
84560 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d zeof(Mem3Block)=
84570 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 =8 );. if( nByt
84580 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c e<=12 ){. nBl
84590 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 ock = 2;. }else
845a0 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 {. nBlock = (
845b0 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 nByte + 11)/8;.
845c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c }. assert( nBl
845d0 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 ock>=2 );.. /*
845e0 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f STEP 1:. ** Loo
845f0 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f k for an entry o
84600 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 f the correct si
84610 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 ze in either the
84620 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e small. ** chun
84630 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 k table or in th
84640 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 e large chunk ha
84650 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 sh table. This
84660 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 is. ** successf
84670 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 ul most of the t
84680 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d ime (about 9 tim
84690 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 es out of 10)..
846a0 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b */. if( nBlock
846b0 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a <= MX_SMALL ){.
846c0 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 i = mem3.aiS
846d0 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a mall[nBlock-2];.
846e0 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 if( i>0 ){.
846f0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
84700 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d nkFromList(i, &m
84710 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f em3.aiSmall[nBlo
84720 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 ck-2]);. re
84730 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 turn memsys3Chec
84740 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b kout(i, nBlock);
84750 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
84760 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e int hash = n
84770 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a Block % N_HASH;.
84780 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 for(i=mem3.a
84790 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 iHash[hash]; i>0
847a0 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ; i=mem3.aPool[i
847b0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a ].u.list.next){.
847c0 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 if( mem3.a
847d0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
847e0 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b size4x/4==nBlock
847f0 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ){. mems
84800 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 ys3UnlinkFromLis
84810 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 t(i, &mem3.aiHas
84820 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 h[hash]);.
84830 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
84840 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f Checkout(i, nBlo
84850 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ck);. }.
84860 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 }. }.. /* STE
84870 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f P 2:. ** Try to
84880 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c satisfy the all
84890 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69 ocation by carvi
848a0 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f ng a piece off o
848b0 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f f the end. ** o
848c0 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 f the master chu
848d0 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 75 nk. This step u
848e0 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 sually works if
848f0 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 step 1 fails..
84900 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a */. if( mem3.sz
84910 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 Master>=nBlock )
84920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d {. return mem
84930 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e sys3FromMaster(n
84940 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 Block);. }...
84950 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a /* STEP 3: . *
84960 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 * Loop through t
84970 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 he entire memory
84980 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 pool. Coalesce
84990 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 adjacent free.
849a0 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 ** chunks. Rec
849b0 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 74 65 ompute the maste
849c0 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c r chunk as the l
849d0 61 72 67 65 73 74 20 66 72 65 65 20 63 68 75 6e argest free chun
849e0 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 k.. ** Then try
849f0 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66 again to satisf
84a00 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e y the allocation
84a10 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 by carving a pi
84a20 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 ece off. ** of
84a30 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d the end of the m
84a40 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 aster chunk. Th
84a50 69 73 20 73 74 65 70 20 68 61 70 70 65 6e 73 20 is step happens
84a60 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 very. ** rarely
84a70 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f (we hope!). */
84a80 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 . for(toFree=nB
84a90 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c lock*16; toFree<
84aa0 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b (mem3.nPool*16);
84ab0 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 toFree *= 2){.
84ac0 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d memsys3OutOfM
84ad0 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 emory(toFree);.
84ae0 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 if( mem3.iMas
84af0 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ter ){. mem
84b00 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d sys3Link(mem3.iM
84b10 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 aster);. me
84b20 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a m3.iMaster = 0;.
84b30 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 mem3.szMas
84b40 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ter = 0;. }.
84b50 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f for(i=0; i<N_
84b60 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 HASH; i++){.
84b70 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 memsys3Merge(&
84b80 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b mem3.aiHash[i]);
84b90 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
84ba0 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 =0; i<MX_SMALL-1
84bb0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 ; i++){. me
84bc0 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 msys3Merge(&mem3
84bd0 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 .aiSmall[i]);.
84be0 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 }. if( mem3
84bf0 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 .szMaster ){.
84c00 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
84c10 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a (mem3.iMaster);.
84c20 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 if( mem3.s
84c30 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 zMaster>=nBlock
84c40 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
84c50 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 n memsys3FromMas
84c60 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 ter(nBlock);.
84c70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
84c80 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 /* If none of
84c90 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 the above worked
84ca0 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 , then we fail.
84cb0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
84cc0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 ../*.** Free an
84cd0 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f outstanding memo
84ce0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a ry allocation..*
84cf0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
84d00 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
84d10 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 the necessary mu
84d20 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 texes, if any, a
84d30 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 re.** already he
84d40 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ld by the caller
84d50 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 . Hence "Unsafe"
84d60 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 ..*/.void memsys
84d70 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 3FreeUnsafe(void
84d80 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 *pOld){. Mem3B
84d90 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 lock *p = (Mem3B
84da0 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e lock*)pOld;. in
84db0 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c t i;. u32 size,
84dc0 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 x;. assert( sq
84dd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
84de0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
84df0 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 assert( p>mem3
84e00 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d .aPool && p<&mem
84e10 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 3.aPool[mem3.nPo
84e20 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d ol] );. i = p -
84e30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 mem3.aPool;. a
84e40 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f ssert( (mem3.aPo
84e50 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
84e60 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 ze4x&1)==1 );.
84e70 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
84e80 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
84e90 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
84ea0 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 i+size<=mem3.nP
84eb0 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e ool+1 );. mem3.
84ec0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
84ed0 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 .size4x &= ~1;.
84ee0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 mem3.aPool[i+si
84ef0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
84f00 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d Size = size;. m
84f10 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 em3.aPool[i+size
84f20 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
84f30 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 &= ~2;. memsys
84f40 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 3Link(i);.. /*
84f50 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68 Try to expand th
84f60 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74 e master using t
84f70 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63 he newly freed c
84f80 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 hunk */. if( me
84f90 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 m3.iMaster ){.
84fa0 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 while( (mem3.a
84fb0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
84fc0 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
84fd0 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 x&2)==0 ){.
84fe0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
84ff0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
85000 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
85010 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d e;. mem3.iM
85020 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 aster -= size;.
85030 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 mem3.szMast
85040 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 er += size;.
85050 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 memsys3Unlink(
85060 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 mem3.iMaster);.
85070 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 x = mem3.aP
85080 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
85090 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
850a0 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 & 2;. mem3
850b0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
850c0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
850d0 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 e4x = mem3.szMas
850e0 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 ter*4 | x;.
850f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
85100 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
85110 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
85120 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e prevSize = mem3.
85130 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a szMaster;. }.
85140 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f x = mem3.aPo
85150 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
85160 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
85170 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 & 2;. while(
85180 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 (mem3.aPool[mem3
85190 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
851a0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
851b0 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a size4x&1)==0 ){.
851c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
851d0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ink(mem3.iMaster
851e0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b +mem3.szMaster);
851f0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
85200 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f ster += mem3.aPo
85210 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
85220 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
85230 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
85240 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f . mem3.aPoo
85250 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 l[mem3.iMaster-1
85260 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
85270 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 mem3.szMaster*4
85280 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 | x;. mem3
85290 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
852a0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
852b0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
852c0 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ize = mem3.szMas
852d0 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ter;. }. }.}
852e0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
852f0 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 he size of an ou
85300 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 tstanding alloca
85310 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 tion, in bytes.
85320 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 The.** size ret
85330 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 urned omits the
85340 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 8-byte header ov
85350 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e erhead. This on
85360 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 ly.** works for
85370 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 chunks that are
85380 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
85390 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 d out..*/.static
853a0 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 int memsys3Size
853b0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d (void *p){. Mem
853c0 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 3Block *pBlock;.
853d0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
853e0 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 urn 0;. pBlock
853f0 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b = (Mem3Block*)p;
85400 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f . assert( (pBlo
85410 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ck[-1].u.hdr.siz
85420 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 e4x&1)!=0 );. r
85430 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 eturn (pBlock[-1
85440 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e ].u.hdr.size4x&~
85450 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 3)*2 - 4;.}../*.
85460 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
85470 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
85480 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
85490 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f ocation size..*/
854a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
854b0 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e ys3Roundup(int n
854c0 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 ){. if( n<=12 )
854d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b {. return 12;
854e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
854f0 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 turn ((n+11)&~7)
85500 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a - 4;. }.}../*.
85510 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
85520 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f es of memory..*/
85530 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
85540 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 msys3Malloc(int
85550 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 nBytes){. sqlit
85560 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 e3_int64 *p;. a
85570 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20 ssert( nBytes>0
85580 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d ); /* m
85590 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 alloc.c filters
855a0 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 75 65 out 0 byte reque
855b0 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 sts */. memsys3
855c0 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d Enter();. p = m
855d0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 emsys3MallocUnsa
855e0 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 fe(nBytes);. me
855f0 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 msys3Leave();.
85600 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
85610 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 .}../*.** Free
85620 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 memory..*/.void
85630 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64 memsys3Free(void
85640 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 *pPrior){. ass
85650 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 ert( pPrior );.
85660 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
85670 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e . memsys3FreeUn
85680 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 safe(pPrior);.
85690 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
856a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
856b0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
856c0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
856d0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 llocation.*/.voi
856e0 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f d *memsys3Reallo
856f0 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 c(void *pPrior,
85700 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 int nBytes){. i
85710 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 nt nOld;. void
85720 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 *p;. if( pPrior
85730 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
85740 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 n sqlite3_malloc
85750 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 (nBytes);. }.
85760 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b if( nBytes<=0 ){
85770 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
85780 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 e(pPrior);. r
85790 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
857a0 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a Old = memsys3Siz
857b0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 e(pPrior);. if(
857c0 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 nBytes<=nOld &&
857d0 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 nBytes>=nOld-12
857e0 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 8 ){. return
857f0 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 pPrior;. }. me
85800 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 msys3Enter();.
85810 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f p = memsys3Mallo
85820 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
85830 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
85840 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 if( nOld<nBytes
85850 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
85860 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 p, pPrior, nOld)
85870 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
85880 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 memcpy(p, pPr
85890 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 ior, nBytes);.
858a0 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 }. memsys3F
858b0 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 reeUnsafe(pPrior
858c0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 );. }. memsys3
858d0 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 Leave();. retur
858e0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e n p;.}../*.** In
858f0 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
85900 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
85910 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 int memsys3Init(
85920 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
85930 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
85940 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 ER(NotUsed);. i
85950 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 f( !sqlite3Globa
85960 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 29 7b lConfig.pHeap ){
85970 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
85980 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 TE_ERROR;. }..
85990 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f 69 6e /* Store a poin
859a0 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 ter to the memor
859b0 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 y block in globa
859c0 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65 6d 33 l structure mem3
859d0 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 . */. assert( s
859e0 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 izeof(Mem3Block)
859f0 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 ==8 );. mem3.aP
85a00 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b ool = (Mem3Block
85a10 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c *)sqlite3Global
85a20 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 Config.pHeap;.
85a30 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 mem3.nPool = (sq
85a40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
85a50 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 g.nHeap / sizeof
85a60 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 (Mem3Block)) - 2
85a70 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.. /* Initiali
85a80 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c ze the master bl
85a90 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 ock. */. mem3.s
85aa0 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e zMaster = mem3.n
85ab0 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d Pool;. mem3.mnM
85ac0 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d aster = mem3.szM
85ad0 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d aster;. mem3.iM
85ae0 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d aster = 1;. mem
85af0 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 3.aPool[0].u.hdr
85b00 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e .size4x = (mem3.
85b10 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b 20 32 szMaster<<2) + 2
85b20 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d ;. mem3.aPool[m
85b30 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 em3.nPool].u.hdr
85b40 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 .prevSize = mem3
85b50 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 .nPool;. mem3.a
85b60 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d Pool[mem3.nPool]
85b70 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 .u.hdr.size4x =
85b80 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 1;.. return SQL
85b90 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
85ba0 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 Deinitialize th
85bb0 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 is module..*/.st
85bc0 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
85bd0 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 3Shutdown(void *
85be0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
85bf0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
85c00 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b Used);. return;
85c10 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e .}..../*.** Open
85c20 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 the file indica
85c30 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 ted and write a
85c40 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 log of all unfre
85c50 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c ed memory .** al
85c60 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 locations into t
85c70 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 hat log..*/.SQLI
85c80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
85c90 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75 sqlite3Memsys3Du
85ca0 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a mp(const char *z
85cb0 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 Filename){.#ifde
85cc0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
85cd0 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 FILE *out;. u3
85ce0 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 2 i, j;. u32 si
85cf0 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e ze;. if( zFilen
85d00 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
85d10 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
85d20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
85d30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
85d40 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
85d50 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
85d60 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
85d70 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
85d80 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
85d90 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
85da0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
85db0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
85dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
85dd0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
85de0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
85df0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 }. memsys3Ente
85e00 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f r();. fprintf(o
85e10 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 ut, "CHUNKS:\n")
85e20 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d ;. for(i=1; i<=
85e30 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 mem3.nPool; i+=s
85e40 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 ize/4){. size
85e50 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
85e60 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
85e70 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c . if( size/4<
85e80 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 =1 ){. fpri
85e90 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a ntf(out, "%p siz
85ea0 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d e error\n", &mem
85eb0 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 3.aPool[i]);.
85ec0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
85ed0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
85ee0 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 }. if( (size
85ef0 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 &1)==0 && mem3.a
85f00 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d Pool[i+size/4-1]
85f10 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21 .u.hdr.prevSize!
85f20 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 =size/4 ){.
85f30 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
85f40 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 p tail size does
85f50 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 not match\n", &
85f60 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a mem3.aPool[i]);.
85f70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 assert( 0
85f80 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
85f90 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28 }. if( ((
85fa0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a mem3.aPool[i+siz
85fb0 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a e/4-1].u.hdr.siz
85fc0 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a e4x&2)>>1)!=(siz
85fd0 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70 e&1) ){. fp
85fe0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 rintf(out, "%p t
85ff0 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74 ail checkout bit
86000 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 is incorrect\n"
86010 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
86020 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
86030 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 0 );. brea
86040 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 k;. }. if(
86050 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 size&1 ){.
86060 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
86070 70 20 25 36 64 20 62 79 74 65 73 20 63 68 65 63 p %6d bytes chec
86080 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d ked out\n", &mem
86090 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 3.aPool[i], (siz
860a0 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d e/4)*8-8);. }
860b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69 else{. fpri
860c0 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 ntf(out, "%p %6d
860d0 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22 bytes free%s\n"
860e0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
860f0 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a , (size/4)*8-8,.
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
86110 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 i==mem3.iMaste
86120 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a r ? " **master**
86130 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 " : "");. }.
86140 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
86150 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 MX_SMALL-1; i++)
86160 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 {. if( mem3.a
86170 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 iSmall[i]==0 ) c
86180 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 ontinue;. fpr
86190 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c intf(out, "small
861a0 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 (%2d):", i);.
861b0 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 for(j = mem3.ai
861c0 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a Small[i]; j>0; j
861d0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 =mem3.aPool[j].u
861e0 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 .list.next){.
861f0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
86200 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 " %p(%d)", &mem3
86210 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 .aPool[j],.
86220 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 (mem3.a
86230 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e Pool[j-1].u.hdr.
86240 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a size4x/4)*8-8);.
86250 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 }. fprint
86260 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 f(out, "\n"); .
86270 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
86280 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
86290 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 if( mem3.aiHas
862a0 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e h[i]==0 ) contin
862b0 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ue;. fprintf(
862c0 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a out, "hash(%2d):
862d0 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a ", i);. for(j
862e0 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 = mem3.aiHash[i
862f0 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 ]; j>0; j=mem3.a
86300 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[j].u.list.n
86310 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69 ext){. fpri
86320 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 ntf(out, " %p(%d
86330 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b )", &mem3.aPool[
86340 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 j],.
86350 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d (mem3.aPool[j-
86360 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
86370 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 4)*8-8);. }.
86380 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
86390 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 "\n"); . }. fp
863a0 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 rintf(out, "mast
863b0 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 er=%d\n", mem3.i
863c0 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e Master);. fprin
863d0 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 tf(out, "nowUsed
863e0 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f =%d\n", mem3.nPo
863f0 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 ol*8 - mem3.szMa
86400 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e ster*8);. fprin
86410 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d tf(out, "mxUsed=
86420 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f %d\n", mem3.nPoo
86430 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 l*8 - mem3.mnMas
86440 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 ter*8);. sqlite
86450 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
86460 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 m3.mutex);. if(
86470 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a out==stdout ){.
86480 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 fflush(stdou
86490 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
864a0 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 fclose(out);.
864b0 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 }.#else. UNUSED
864c0 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 _PARAMETER(zFile
864d0 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a name);.#endif.}.
864e0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
864f0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ine is the only
86500 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
86510 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e file with extern
86520 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a al .** linkage..
86530 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 **.** Populate t
86540 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d he low-level mem
86550 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
86560 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 unction pointers
86570 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c in.** sqlite3Gl
86580 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 obalConfig.m wit
86590 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 h pointers to th
865a0 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 e routines in th
865b0 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 is file. The.**
865c0 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 arguments specif
865d0 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d y the block of m
865e0 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e emory to manage.
865f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
86600 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ine is only call
86610 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
86620 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72 nfig(), and ther
86630 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 efore.** is not
86640 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 74 required to be t
86650 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69 73 hreadsafe (it is
86660 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 not)..*/.SQLITE
86670 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
86680 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
86690 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
866a0 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a tMemsys3(void){.
866b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
866c0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
866d0 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 ds mempoolMethod
866e0 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 s = {. memsy
866f0 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d s3Malloc,. m
86700 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20 emsys3Free,.
86710 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c memsys3Realloc,
86720 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a . memsys3Siz
86730 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 e,. memsys3R
86740 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d oundup,. mem
86750 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d sys3Init,. m
86760 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a emsys3Shutdown,.
86770 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 0. };. re
86780 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 turn &mempoolMet
86790 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 hods;.}..#endif
867a0 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
867b0 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a _MEMSYS3 */../**
867c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
867d0 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a of mem3.c *****
867e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
867f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
86810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
86820 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a in file mem5.c *
86830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
86860 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 ** 2007 October
86870 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
86880 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
86890 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
868a0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
868b0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
868c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
868d0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
868e0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
868f0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
86900 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
86910 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
86920 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
86930 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
86940 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
86950 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
86960 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
86970 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
86980 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
86990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869d0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
869e0 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
869f0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
86a00 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a lement a memory.
86a10 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ** allocation su
86a20 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 bsystem for use
86a30 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a by SQLite. .**.*
86a40 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
86a50 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
86a60 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
86a70 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 m omits all.** u
86a80 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 se of malloc().
86a90 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 The SQLite user
86aa0 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b supplies a block
86ab0 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 of memory.** be
86ac0 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c fore calling sql
86ad0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
86ae0 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c ) from which all
86af0 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 ocations.** are
86b00 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 made and returne
86b10 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 d by the xMalloc
86b20 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 () and xRealloc(
86b30 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ) .** implementa
86b40 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 tions. Once sqli
86b50 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
86b60 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
86b70 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 ,.** the amount
86b80 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 of memory availa
86b90 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 ble to SQLite is
86ba0 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f fixed and canno
86bb0 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e t.** be changed.
86bc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
86bd0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
86be0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
86bf0 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 system is includ
86c00 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 ed.** in the bui
86c10 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 ld only if SQLIT
86c20 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 E_ENABLE_MEMSYS5
86c30 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a is defined..**.
86c40 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 ** $Id: mem5.c,v
86c50 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f 31 39 1.19 2008/11/19
86c60 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 16:52:44 daniel
86c70 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
86c80 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
86c90 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
86ca0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 allocator is us
86cb0 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a ed only when .**
86cc0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
86cd0 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 EMSYS5 is define
86ce0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
86cf0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
86d00 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 S5../*.** A mini
86d10 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 mum allocation i
86d20 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
86d30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
86d40 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 tructure..** Lar
86d50 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ger allocations
86d60 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 are an array of
86d70 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 these structures
86d80 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 where the.** si
86d90 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 ze of the array
86da0 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e is a power of 2.
86db0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
86dc0 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 ct Mem5Link Mem5
86dd0 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d Link;.struct Mem
86de0 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 5Link {. int ne
86df0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
86e00 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 ex of next free
86e10 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 chunk */. int p
86e20 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e rev; /* In
86e30 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20 dex of previous
86e40 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b free chunk */.};
86e50 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
86e60 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f size of any allo
86e70 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c cation is ((1<<L
86e80 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f OGMAX)*mem5.nAto
86e90 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d m). Since.** mem
86ea0 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 5.nAtom is alway
86eb0 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20 74 68 s at least 8, th
86ec0 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 is is not really
86ed0 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a 2a 20 a practical.**
86ee0 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 limitation..*/.#
86ef0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 define LOGMAX 30
86f00 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 ../*.** Masks us
86f10 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 ed for mem5.aCtr
86f20 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f l[] elements..*/
86f30 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f .#define CTRL_LO
86f40 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f GSIZE 0x1f /
86f50 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 * Log2 Size of t
86f60 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61 74 69 his block relati
86f70 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a ve to POW2_MIN *
86f80 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 /.#define CTRL_F
86f90 52 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20 REE 0x20
86fa0 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63 /* True if not c
86fb0 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f hecked out */../
86fc0 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
86fd0 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
86fe0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f used by this mo
86ff0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 dule are collect
87000 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e ed.** into a sin
87010 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 gle structure na
87020 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 med "mem5". Thi
87030 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 s is to keep the
87040 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 .** static varia
87050 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 bles organized a
87060 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d nd to reduce nam
87070 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e espace pollution
87080 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f .** when this mo
87090 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 dule is combined
870a0 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 with other in t
870b0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e he amalgamation.
870c0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
870d0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d E_WSD struct Mem
870e0 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 5Global {. /*.
870f0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c ** Memory avail
87100 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
87110 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e ion. */. int n
87120 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a 20 53 Atom; /* S
87130 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 mallest possible
87140 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 allocation in b
87150 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ytes */. int nB
87160 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 lock; /* Nu
87170 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20 73 69 mber of nAtom si
87180 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 zed blocks in zP
87190 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f ool */. u8 *zPo
871a0 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a ol;. . /*. **
871b0 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f Mutex to contro
871c0 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 l access to the
871d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
871e0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a n subsystem.. *
871f0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
87200 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a x *mutex;.. /*.
87210 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 ** Performance
87220 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f statistics. */
87230 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 . u64 nAlloc;
87240 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
87250 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 number of calls
87260 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 to malloc */. u
87270 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 64 totalAlloc;
87280 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 /* Total of a
87290 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 ll malloc calls
872a0 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 - includes inter
872b0 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 nal frag */. u6
872c0 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 4 totalExcess;
872d0 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 /* Total inter
872e0 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f nal fragmentatio
872f0 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 n */. u32 curre
87300 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 ntOut; /* Cu
87310 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 rrent checkout,
87320 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e including intern
87330 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e al fragmentation
87340 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e */. u32 curren
87350 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 tCount; /* Cur
87360 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 rent number of d
87370 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 istinct checkout
87380 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 s */. u32 maxOu
87390 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 t; /* Ma
873a0 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 ximum instantane
873b0 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a ous currentOut *
873c0 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 /. u32 maxCount
873d0 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d ; /* Maxim
873e0 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 um instantaneous
873f0 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f currentCount */
87400 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73 . u32 maxReques
87410 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 t; /* Larges
87420 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 t allocation (ex
87430 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72 clusive of inter
87440 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a nal frag) */. .
87450 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 /*. ** Lists
87460 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f of free blocks o
87470 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65 73 2e f various sizes.
87480 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 . */. int aiFr
87490 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d eelist[LOGMAX+1]
874a0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 ;.. /*. ** Spa
874b0 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 ce for tracking
874c0 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 which blocks are
874d0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 checked out and
874e0 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f the size. ** o
874f0 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f f each block. O
87500 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63 ne byte per bloc
87510 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 k.. */. u8 *aC
87520 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b trl;..} mem5 = {
87530 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a 23 64 19804167 };..#d
87540 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f 42 41 efine mem5 GLOBA
87550 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f L(struct Mem5Glo
87560 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64 65 66 bal, mem5)..#def
87570 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 ine MEM5LINK(idx
87580 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 ) ((Mem5Link *)(
87590 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 &mem5.zPool[(idx
875a0 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a )*mem5.nAtom])).
875b0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
875c0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e e chunk at mem5.
875d0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 aPool[i] from li
875e0 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 st it is current
875f0 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 ly.** on. It sh
87600 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e ould be found on
87610 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
87620 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 [iLogsize]..*/.s
87630 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
87640 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 s5Unlink(int i,
87650 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 int iLogsize){.
87660 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b int next, prev;
87670 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
87680 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b && i<mem5.nBlock
87690 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c );. assert( iL
876a0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f ogsize>=0 && iLo
876b0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b gsize<=LOGMAX );
876c0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 . assert( (mem5
876d0 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c .aCtrl[i] & CTRL
876e0 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 _LOGSIZE)==iLogs
876f0 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d ize );.. next =
87700 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 MEM5LINK(i)->ne
87710 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d xt;. prev = MEM
87720 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 5LINK(i)->prev;.
87730 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b 0a if( prev<0 ){.
87740 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c mem5.aiFreel
87750 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 ist[iLogsize] =
87760 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 next;. }else{.
87770 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76 MEM5LINK(prev
87780 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a )->next = next;.
87790 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d }. if( next>=
877a0 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 0 ){. MEM5LIN
877b0 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 K(next)->prev =
877c0 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a prev;. }.}../*.
877d0 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e ** Link the chun
877e0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b k at mem5.aPool[
877f0 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e i] so that is on
87800 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a the iLogsize.**
87810 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 free list..*/.s
87820 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
87830 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e s5Link(int i, in
87840 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 t iLogsize){. i
87850 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 nt x;. assert(
87860 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
87870 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29 ld(mem5.mutex) )
87880 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 ;. assert( i>=0
87890 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 && i<mem5.nBloc
878a0 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 k );. assert( i
878b0 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c Logsize>=0 && iL
878c0 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 ogsize<=LOGMAX )
878d0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d ;. assert( (mem
878e0 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 5.aCtrl[i] & CTR
878f0 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 L_LOGSIZE)==iLog
87900 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d size );.. x = M
87910 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 EM5LINK(i)->next
87920 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 = mem5.aiFreeli
87930 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 st[iLogsize];.
87940 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 MEM5LINK(i)->pre
87950 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e v = -1;. if( x>
87960 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
87970 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 ( x<mem5.nBlock
87980 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 );. MEM5LINK(
87990 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20 x)->prev = i;.
879a0 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c }. mem5.aiFreel
879b0 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 ist[iLogsize] =
879c0 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 i;.}../*.** If t
879d0 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 he STATIC_MEM mu
879e0 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 tex is not alrea
879f0 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 dy held, obtain
87a00 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 it now. The mute
87a10 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 x.** will alread
87a20 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69 y be held (obtai
87a30 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d ned by code in m
87a40 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 alloc.c) if.** s
87a50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
87a60 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74 ig.bMemStat is t
87a70 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 rue..*/.static v
87a80 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 oid memsys5Enter
87a90 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 (void){. if( sq
87aa0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
87ab0 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26 g.bMemstat==0 &&
87ac0 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30 20 29 mem5.mutex==0 )
87ad0 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 {. mem5.mutex
87ae0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
87af0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
87b00 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 X_STATIC_MEM);.
87b10 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
87b20 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 ex_enter(mem5.mu
87b30 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 tex);.}.static v
87b40 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 oid memsys5Leave
87b50 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 (void){. sqlite
87b60 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
87b70 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a m5.mutex);.}../*
87b80 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
87b90 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 ize of an outsta
87ba0 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e nding allocation
87bb0 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 , in bytes. The
87bc0 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 .** size returne
87bd0 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 d omits the 8-by
87be0 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 te header overhe
87bf0 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a ad. This only.*
87c00 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e * works for chun
87c10 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 ks that are curr
87c20 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 ently checked ou
87c30 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
87c40 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 memsys5Size(voi
87c50 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 d *p){. int iSi
87c60 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 ze = 0;. if( p
87c70 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 ){. int i = (
87c80 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f (u8 *)p-mem5.zPo
87c90 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a ol)/mem5.nAtom;.
87ca0 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 assert( i>=0
87cb0 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 && i<mem5.nBloc
87cc0 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d k );. iSize =
87cd0 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20 28 31 mem5.nAtom * (1
87ce0 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b << (mem5.aCtrl[
87cf0 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 i]&CTRL_LOGSIZE)
87d00 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
87d10 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iSize;.}../*.**
87d20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65 Find the first e
87d30 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65 ntry on the free
87d40 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 list iLogsize.
87d50 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 Unlink that.** e
87d60 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 ntry and return
87d70 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 its index. .*/.s
87d80 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
87d90 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 5UnlinkFirst(int
87da0 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e iLogsize){. in
87db0 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 t i;. int iFirs
87dc0 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c t;.. assert( iL
87dd0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f ogsize>=0 && iLo
87de0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b gsize<=LOGMAX );
87df0 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20 . i = iFirst =
87e00 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
87e10 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 iLogsize];. ass
87e20 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29 ert( iFirst>=0 )
87e30 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 ;. while( i>0 )
87e40 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72 {. if( i<iFir
87e50 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b st ) iFirst = i;
87e60 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e . i = MEM5LIN
87e70 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a K(i)->next;. }.
87e80 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 memsys5Unlink(
87e90 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 iFirst, iLogsize
87ea0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72 );. return iFir
87eb0 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 st;.}../*.** Ret
87ec0 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d urn a block of m
87ed0 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 emory of at leas
87ee0 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 t nBytes in size
87ef0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
87f00 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 if unable..*/.s
87f10 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
87f20 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 ys5MallocUnsafe(
87f30 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e int nByte){. in
87f40 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f t i; /
87f50 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d * Index of a mem
87f60 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 5.aPool[] slot *
87f70 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 /. int iBin;
87f80 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
87f90 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 to mem5.aiFreeli
87fa0 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 st[] */. int iF
87fb0 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 ullSz; /* Si
87fc0 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e ze of allocation
87fd0 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70 rounded up to p
87fe0 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 ower of 2 */. i
87ff0 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 nt iLogsize;
88000 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c /* Log2 of iFull
88010 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a Sz/POW2_MIN */..
88020 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 /* Keep track
88030 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 of the maximum a
88040 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
88050 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 t. Even unfulfi
88060 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 lled. ** reques
88070 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a ts are counted *
88080 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e 42 79 /. if( (u32)nBy
88090 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 te>mem5.maxReque
880a0 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d st ){. mem5.m
880b0 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74 axRequest = nByt
880c0 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 e;. }.. /* Rou
880d0 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 nd nByte up to t
880e0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f he next valid po
880f0 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 wer of two */.
88100 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 for(iFullSz=mem5
88110 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 .nAtom, iLogsize
88120 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 =0; iFullSz<nByt
88130 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c e; iFullSz *= 2,
88140 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a iLogsize++){}..
88150 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d /* Make sure m
88160 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 em5.aiFreelist[i
88170 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e Logsize] contain
88180 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 s at least one f
88190 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 ree. ** block.
881a0 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 If not, then sp
881b0 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 lit a block of t
881c0 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 he next larger p
881d0 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f ower of. ** two
881e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 in order to cre
881f0 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 ate a new free b
88200 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f lock of size iLo
88210 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f gsize.. */. fo
88220 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b r(iBin=iLogsize;
88230 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
88240 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e [iBin]<0 && iBin
88250 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b <=LOGMAX; iBin++
88260 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c ){}. if( iBin>L
88270 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 OGMAX ) return 0
88280 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 ;. i = memsys5U
88290 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 nlinkFirst(iBin)
882a0 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e ;. while( iBin>
882b0 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 iLogsize ){.
882c0 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 int newSize;..
882d0 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 iBin--;. ne
882e0 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 wSize = 1 << iBi
882f0 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 n;. mem5.aCtr
88300 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 l[i+newSize] = C
88310 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b TRL_FREE | iBin;
88320 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b . memsys5Link
88330 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e (i+newSize, iBin
88340 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 );. }. mem5.aC
88350 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a trl[i] = iLogsiz
88360 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 e;.. /* Update
88370 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 allocator perfor
88380 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 mance statistics
88390 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c . */. mem5.nAll
883a0 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 oc++;. mem5.tot
883b0 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c alAlloc += iFull
883c0 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c Sz;. mem5.total
883d0 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 Excess += iFullS
883e0 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d z - nByte;. mem
883f0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 5.currentCount++
88400 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
88410 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a Out += iFullSz;.
88420 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f if( mem5.maxCo
88430 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 unt<mem5.current
88440 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 Count ) mem5.max
88450 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 Count = mem5.cur
88460 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 rentCount;. if(
88470 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d mem5.maxOut<mem
88480 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 5.currentOut ) m
88490 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d em5.maxOut = mem
884a0 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 5.currentOut;..
884b0 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 /* Return a poi
884c0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
884d0 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f cated memory. */
884e0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
884f0 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d )&mem5.zPool[i*m
88500 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f em5.nAtom];.}../
88510 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 *.** Free an out
88520 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 standing memory
88530 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 allocation..*/.s
88540 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
88550 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 s5FreeUnsafe(voi
88560 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 d *pOld){. u32
88570 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a size, iLogsize;.
88580 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20 20 20 int iBlock;
88590 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a .. /*
885a0 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 Set iBlock to t
885b0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
885c0 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f block pointed to
885d0 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a by pOld in . *
885e0 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6d * the array of m
885f0 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65 20 62 em5.nAtom byte b
88600 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f locks pointed to
88610 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a by mem5.zPool..
88620 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 */. iBlock =
88630 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 ((u8 *)pOld-mem5
88640 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 .zPool)/mem5.nAt
88650 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 om;.. /* Check
88660 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 that the pointer
88670 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 pOld points to
88680 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 a valid, non-fre
88690 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 e block. */. as
886a0 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 sert( iBlock>=0
886b0 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e && iBlock<mem5.n
886c0 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 Block );. asser
886d0 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d t( ((u8 *)pOld-m
886e0 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e em5.zPool)%mem5.
886f0 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 nAtom==0 );. as
88700 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 sert( (mem5.aCtr
88710 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c l[iBlock] & CTRL
88720 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 _FREE)==0 );..
88730 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e iLogsize = mem5.
88740 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 aCtrl[iBlock] &
88750 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 CTRL_LOGSIZE;.
88760 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 size = 1<<iLogsi
88770 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 ze;. assert( iB
88780 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 lock+size-1<(u32
88790 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a )mem5.nBlock );.
887a0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 . mem5.aCtrl[iB
887b0 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 lock] |= CTRL_FR
887c0 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c EE;. mem5.aCtrl
887d0 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 [iBlock+size-1]
887e0 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 |= CTRL_FREE;.
887f0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
88800 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 rentCount>0 );.
88810 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 assert( mem5.cu
88820 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a rrentOut>=(size*
88830 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20 mem5.nAtom) );.
88840 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 mem5.currentCou
88850 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 nt--;. mem5.cur
88860 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a rentOut -= size*
88870 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 mem5.nAtom;. as
88880 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
88890 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e ntOut>0 || mem5.
888a0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 currentCount==0
888b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
888c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 5.currentCount>0
888d0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
888e0 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d Out==0 );.. mem
888f0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
88900 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
88910 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 ogsize;. while(
88920 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 iLogsize<LOGMAX
88930 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64 ){. int iBud
88940 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c dy;. if( (iBl
88950 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 ock>>iLogsize) &
88960 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64 1 ){. iBud
88970 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 dy = iBlock - si
88980 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ze;. }else{.
88990 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 iBuddy = iB
889a0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 lock + size;.
889b0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 }. assert( i
889c0 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 Buddy>=0 );.
889d0 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c if( (iBuddy+(1<<
889e0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e iLogsize))>mem5.
889f0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a nBlock ) break;.
88a00 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 if( mem5.aCt
88a10 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52 rl[iBuddy]!=(CTR
88a20 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
88a30 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 e) ) break;.
88a40 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 memsys5Unlink(iB
88a50 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b uddy, iLogsize);
88a60 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b . iLogsize++;
88a70 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c . if( iBuddy<
88a80 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 iBlock ){.
88a90 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
88aa0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c y] = CTRL_FREE |
88ab0 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 iLogsize;.
88ac0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
88ad0 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ck] = 0;. i
88ae0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a Block = iBuddy;.
88af0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
88b00 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
88b10 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ck] = CTRL_FREE
88b20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
88b30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
88b40 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ddy] = 0;. }.
88b50 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 size *= 2;.
88b60 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b }. memsys5Link
88b70 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a (iBlock, iLogsiz
88b80 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c e);.}../*.** All
88b90 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 ocate nBytes of
88ba0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 memory.*/.static
88bb0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 void *memsys5Ma
88bc0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 lloc(int nBytes)
88bd0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
88be0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 4 *p = 0;. if(
88bf0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 nBytes>0 ){.
88c00 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a memsys5Enter();.
88c10 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d p = memsys5M
88c20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
88c30 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 es);. memsys5
88c40 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 Leave();. }. r
88c50 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 eturn (void*)p;
88c60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
88c70 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
88c80 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 void memsys5Fre
88c90 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b e(void *pPrior){
88ca0 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 . if( pPrior==0
88cb0 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b 0a 20 ){.assert(0);.
88cc0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
88cd0 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
88ce0 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e . memsys5FreeUn
88cf0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 safe(pPrior);.
88d00 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 20 memsys5Leave();
88d10 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 .}../*.** Chang
88d20 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
88d30 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
88d40 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 allocation.*/.s
88d50 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
88d60 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 ys5Realloc(void
88d70 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 *pPrior, int nBy
88d80 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 tes){. int nOld
88d90 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 ;. void *p;. i
88da0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a f( pPrior==0 ){.
88db0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
88dc0 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 s5Malloc(nBytes)
88dd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 ;. }. if( nByt
88de0 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d es<=0 ){. mem
88df0 73 79 73 35 46 72 65 65 28 70 50 72 69 6f 72 29 sys5Free(pPrior)
88e00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
88e10 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d }. nOld = mem
88e20 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 29 sys5Size(pPrior)
88e30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d ;. if( nBytes<=
88e40 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 75 nOld ){. retu
88e50 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 rn pPrior;. }.
88e60 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
88e70 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 . p = memsys5Ma
88e80 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 llocUnsafe(nByte
88e90 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 s);. if( p ){.
88ea0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 memcpy(p, pPr
88eb0 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 ior, nOld);.
88ec0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 memsys5FreeUnsaf
88ed0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 e(pPrior);. }.
88ee0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b memsys5Leave();
88ef0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
88f00 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 /*.** Round up a
88f10 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
88f20 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
88f30 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e allocation size.
88f40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
88f50 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e emsys5Roundup(in
88f60 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c t n){. int iFul
88f70 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c lSz;. for(iFull
88f80 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 Sz=mem5.nAtom; i
88f90 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 FullSz<n; iFullS
88fa0 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 z *= 2);. retur
88fb0 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 n iFullSz;.}..st
88fc0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
88fd0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b Log(int iValue){
88fe0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 . int iLog;. f
88ff0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 or(iLog=0; (1<<i
89000 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f Log)<iValue; iLo
89010 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 g++);. return i
89020 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e Log;.}../*.** In
89030 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
89040 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
89050 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 int memsys5Init(
89060 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
89070 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 int ii;. int
89080 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47 nByte = sqlite3G
89090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 lobalConfig.nHea
890a0 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 20 3d p;. u8 *zByte =
890b0 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47 6c (u8 *)sqlite3Gl
890c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
890d0 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b ;. int nMinLog;
890e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
890f0 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d /* Log of minim
89100 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 um allocation si
89110 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20 ze in bytes*/.
89120 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20 int iOffset;..
89130 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
89140 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 (NotUsed);.. if
89150 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20 20 20 ( !zByte ){.
89160 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
89170 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e ROR;. }.. nMin
89180 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 Log = memsys5Log
89190 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
891a0 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d nfig.mnReq);. m
891b0 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c em5.nAtom = (1<<
891c0 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c nMinLog);. whil
891d0 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d e( (int)sizeof(M
891e0 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 em5Link)>mem5.nA
891f0 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e tom ){. mem5.
89200 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 nAtom = mem5.nAt
89210 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 om << 1;. }..
89220 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e mem5.nBlock = (n
89230 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 Byte / (mem5.nAt
89240 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b om+sizeof(u8)));
89250 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 . mem5.zPool =
89260 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 zByte;. mem5.aC
89270 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d trl = (u8 *)&mem
89280 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 5.zPool[mem5.nBl
89290 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b ock*mem5.nAtom];
892a0 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 .. for(ii=0; ii
892b0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b <=LOGMAX; ii++){
892c0 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 . mem5.aiFree
892d0 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 list[ii] = -1;.
892e0 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 }.. iOffset =
892f0 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 0;. for(ii=LOGM
89300 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 AX; ii>=0; ii--)
89310 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 {. int nAlloc
89320 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 = (1<<ii);.
89330 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c if( (iOffset+nAl
89340 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 loc)<=mem5.nBloc
89350 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e k ){. mem5.
89360 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d aCtrl[iOffset] =
89370 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b ii | CTRL_FREE;
89380 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 . memsys5Li
89390 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29 3b nk(iOffset, ii);
893a0 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 2b . iOffset +
893b0 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a = nAlloc;. }.
893c0 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66 66 assert((iOff
893d0 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 set+nAlloc)>mem5
893e0 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 .nBlock);. }..
893f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
89400 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e K;.}../*.** Dein
89410 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f itialize this mo
89420 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dule..*/.static
89430 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 void memsys5Shut
89440 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
89450 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
89460 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
89470 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f ;. return;.}../
89480 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 *.** Open the fi
89490 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 le indicated and
894a0 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 write a log of
894b0 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f all unfreed memo
894c0 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ry .** allocatio
894d0 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 ns into that log
894e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
894f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
89500 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 Memsys5Dump(cons
89510 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
89520 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 e){.#ifdef SQLIT
89530 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a E_DEBUG. FILE *
89540 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c out;. int i, j,
89550 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f n;. int nMinLo
89560 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e g;.. if( zFilen
89570 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
89580 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
89590 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
895a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
895b0 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
895c0 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
895d0 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
895e0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
895f0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
89600 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
89610 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
89620 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
89630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
89640 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
89650 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
89660 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 }. memsys5Ente
89670 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d r();. nMinLog =
89680 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 memsys5Log(mem5
89690 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 .nAtom);. for(i
896a0 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 =0; i<=LOGMAX &&
896b0 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 i+nMinLog<32; i
896c0 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 ++){. for(n=0
896d0 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c , j=mem5.aiFreel
896e0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 ist[i]; j>=0; j
896f0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e = MEM5LINK(j)->n
89700 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 ext, n++){}.
89710 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 fprintf(out, "fr
89720 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20 eelist items of
89730 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 size %d: %d\n",
89740 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c mem5.nAtom << i,
89750 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e n);. }. fprin
89760 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 tf(out, "mem5.nA
89770 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c 6c lloc = %ll
89780 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f u\n", mem5.nAllo
89790 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 c);. fprintf(ou
897a0 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c t, "mem5.totalAl
897b0 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c loc = %llu\n",
897c0 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 mem5.totalAlloc
897d0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
897e0 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 , "mem5.totalExc
897f0 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 ess = %llu\n",
89800 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 mem5.totalExcess
89810 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
89820 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f , "mem5.currentO
89830 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 ut = %u\n", me
89840 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a m5.currentOut);.
89850 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
89860 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e mem5.currentCoun
89870 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e t = %u\n", mem5.
89880 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 currentCount);.
89890 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
898a0 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 em5.maxOut
898b0 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d = %u\n", mem5.m
898c0 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 axOut);. fprint
898d0 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 f(out, "mem5.max
898e0 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e Count = %u\n
898f0 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 ", mem5.maxCount
89900 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
89910 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 , "mem5.maxReque
89920 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 st = %u\n", me
89930 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b 0a m5.maxRequest);.
89940 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
89950 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 ;. if( out==std
89960 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 out ){. fflus
89970 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c h(stdout);. }el
89980 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f se{. fclose(o
89990 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 ut);. }.#else.
899a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
899b0 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 R(zFilename);.#e
899c0 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 ndif.}../*.** Th
899d0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
899e0 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 e only routine i
899f0 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 n this file with
89a00 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 external .** li
89a10 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 72 6e nkage. It return
89a20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
89a30 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
89a40 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 mem_methods.** s
89a50 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 64 20 truct populated
89a60 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 73 35 with the memsys5
89a70 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c methods..*/.SQL
89a80 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
89a90 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 t sqlite3_mem_me
89aa0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 thods *sqlite3Me
89ab0 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 mGetMemsys5(void
89ac0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
89ad0 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 t sqlite3_mem_me
89ae0 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 thods memsys5Met
89af0 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 hods = {. me
89b00 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 msys5Malloc,.
89b10 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 memsys5Free,.
89b20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c memsys5Reall
89b30 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 oc,. memsys5
89b40 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 Size,. memsy
89b50 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 s5Roundup,.
89b60 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 memsys5Init,.
89b70 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77 memsys5Shutdow
89b80 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 n,. 0. };.
89b90 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 return &memsys5
89ba0 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 Methods;.}..#end
89bb0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 if /* SQLITE_ENA
89bc0 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a BLE_MEMSYS5 */..
89bd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
89be0 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a End of mem5.c **
89bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
89c20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
89c30 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
89c40 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
89c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
89c70 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
89c80 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
89c90 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
89ca0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
89cb0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
89cc0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
89cd0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
89ce0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
89cf0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
89d00 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
89d10 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
89d20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
89d30 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
89d40 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
89d50 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
89d60 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
89d70 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
89d80 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
89d90 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
89da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89de0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
89df0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
89e00 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
89e10 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
89e20 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
89e30 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
89e40 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 that is common a
89e50 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 cross all mutex
89e60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
89e70 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 ..**.** $Id: mut
89e80 65 78 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 ex.c,v 1.30 2009
89e90 2f 30 32 2f 31 37 20 31 36 3a 32 39 3a 31 31 20 /02/17 16:29:11
89ea0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
89eb0 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 $.*/..#ifndef SQ
89ec0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a LITE_MUTEX_OMIT.
89ed0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
89ee0 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 the mutex syste
89ef0 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 m..*/.SQLITE_PRI
89f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
89f10 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b MutexInit(void){
89f20 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c . int rc = SQL
89f30 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 ITE_OK;. if( sq
89f40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
89f50 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
89f60 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
89f70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
89f80 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 ex.xMutexAlloc )
89f90 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
89fa0 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 e xMutexAlloc me
89fb0 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 thod has not bee
89fc0 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 n set, then the
89fd0 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 user did not.
89fe0 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 ** install a
89ff0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
8a000 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 tion via sqlite3
8a010 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 _config() prior
8a020 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c to . ** sql
8a030 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
8a040 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 ) being called.
8a050 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 This block copie
8a060 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 s pointers to.
8a070 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 ** the defau
8a080 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f lt implementatio
8a090 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 n into the sqlit
8a0a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 e3GlobalConfig s
8a0b0 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 tructure..
8a0c0 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
8a0d0 64 61 6e 67 65 72 20 69 73 20 74 68 61 74 20 61 danger is that a
8a0e0 6c 74 68 6f 75 67 68 20 73 71 6c 69 74 65 33 5f lthough sqlite3_
8a0f0 63 6f 6e 66 69 67 28 29 20 69 73 20 6e 6f 74 20 config() is not
8a100 61 20 74 68 72 65 61 64 73 61 66 65 0a 20 20 20 a threadsafe.
8a110 20 20 20 2a 2a 20 41 50 49 2c 20 73 71 6c 69 74 ** API, sqlit
8a120 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
8a130 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75 6c 74 69 is, and so multi
8a140 70 6c 65 20 74 68 72 65 61 64 73 20 6d 61 79 20 ple threads may
8a150 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 74 74 65 be. ** atte
8a160 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e 20 74 68 mpting to run th
8a170 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 69 6d 75 is function simu
8a180 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54 6f 20 67 ltaneously. To g
8a190 75 61 72 64 20 77 72 69 74 65 0a 20 20 20 20 20 uard write.
8a1a0 20 2a 2a 20 61 63 63 65 73 73 20 74 6f 20 74 68 ** access to th
8a1b0 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e sqlite3GlobalC
8a1c0 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 2c onfig structure,
8a1d0 20 74 68 65 20 27 4d 41 53 54 45 52 27 20 73 74 the 'MASTER' st
8a1e0 61 74 69 63 20 6d 75 74 65 78 0a 20 20 20 20 20 atic mutex.
8a1f0 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ** is obtained
8a200 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 before modifying
8a210 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 it.. */.
8a220 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
8a230 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20 3d 20 73 x_methods *p = s
8a240 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
8a250 65 78 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ex();. sqli
8a260 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 te3_mutex *pMast
8a270 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 20 er = 0;. .
8a280 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74 65 78 49 rc = p->xMutexI
8a290 6e 69 74 28 29 3b 0a 20 20 20 20 20 20 69 66 28 nit();. if(
8a2a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
8a2b0 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 73 74 65 {. pMaste
8a2c0 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78 41 6c 6c r = p->xMutexAll
8a2d0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
8a2e0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
8a2f0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 assert(p
8a300 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 Master);.
8a310 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74 65 72 28 p->xMutexEnter(
8a320 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 pMaster);.
8a330 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8a340 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
8a350 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d tex.xMutexAlloc=
8a360 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 =0 .
8a370 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 || sqlite3Globa
8a380 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
8a390 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d 3e 78 4d utexAlloc==p->xM
8a3a0 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20 20 20 20 utexAlloc.
8a3b0 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
8a3c0 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 !sqlite3GlobalC
8a3d0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
8a3e0 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 exAlloc ){.
8a3f0 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 sqlite3Glob
8a400 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d alConfig.mutex =
8a410 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 *p;. }.
8a420 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 65 78 p->xMutex
8a430 4c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a Leave(pMaster);.
8a440 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
8a450 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
8a460 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
8a470 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49 6e g.mutex.xMutexIn
8a480 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a it();. }. }.
8a490 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
8a4a0 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
8a4b0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d the mutex system
8a4c0 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65 . This call free
8a4d0 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f s resources allo
8a4e0 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 cated by.** sqli
8a4f0 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a te3MutexInit()..
8a500 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
8a510 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 E int sqlite3Mut
8a520 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 exEnd(void){. i
8a530 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
8a540 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 K;. if( sqlite3
8a550 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
8a560 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a ex.xMutexEnd ){.
8a570 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
8a580 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
8a590 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a ex.xMutexEnd();.
8a5a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
8a5b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 .}../*.** Retrie
8a5c0 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ve a pointer to
8a5d0 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f a static mutex o
8a5e0 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 r allocate a new
8a5f0 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f dynamic one..*/
8a600 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
8a610 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
8a620 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 e3_mutex_alloc(i
8a630 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 nt id){.#ifndef
8a640 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
8a650 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
8a660 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
8a670 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
8a680 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 if. return sqli
8a690 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8a6a0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f mutex.xMutexAllo
8a6b0 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 c(id);.}..SQLITE
8a6c0 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
8a6d0 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d _mutex *sqlite3M
8a6e0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 utexAlloc(int id
8a6f0 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 ){. if( !sqlite
8a700 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
8a710 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
8a720 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
8a730 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
8a740 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
8a750 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 .xMutexAlloc(id)
8a760 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
8a770 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e a dynamic mutex.
8a780 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
8a790 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
8a7a0 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d x_free(sqlite3_m
8a7b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
8a7c0 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
8a7d0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
8a7e0 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 ex.xMutexFree(p)
8a7f0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
8a800 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 btain the mutex
8a810 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 p. If some other
8a820 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 thread already
8a830 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 has the mutex, b
8a840 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 lock.** until it
8a850 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
8a860 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
8a870 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
8a880 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
8a890 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
8a8a0 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
8a8b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
8a8c0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 utex.xMutexEnter
8a8d0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
8a8e0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 * Obtain the mut
8a8f0 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 ex p. If success
8a900 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ful, return SQLI
8a910 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 TE_OK. Otherwise
8a920 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 , if another.**
8a930 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 thread holds the
8a940 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 mutex and it ca
8a950 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
8a960 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
8a970 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f BUSY..*/.SQLITE_
8a980 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
8a990 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 mutex_try(sqlite
8a9a0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
8a9b0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
8a9c0 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 K;. if( p ){.
8a9d0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
8a9e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
8a9f0 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b ex.xMutexTry(p);
8aa00 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
8aa10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
8aa20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
8aa30 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
8aa40 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
8aa50 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
8aa60 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 * entered by the
8aa70 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 same thread. T
8aa80 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
8aa90 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
8aaa0 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 mutex .** is not
8aab0 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
8aac0 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f ed. If a NULL po
8aad0 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 inter is passed
8aae0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a as an argument.*
8aaf0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
8ab00 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
8ab10 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
8ab20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
8ab30 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
8ab40 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
8ab50 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
8ab60 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8ab70 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 MutexLeave(p);.
8ab80 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
8ab90 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
8aba0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8abb0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
8abc0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
8abd0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
8abe0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
8abf0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
8ac00 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 statements..*/.S
8ac10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
8ac20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
8ac30 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8ac40 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
8ac50 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
8ac60 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8ac70 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a MutexHeld(p);.}.
8ac80 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
8ac90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
8aca0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
8acb0 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
8acc0 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 p==0 || sqlite3
8acd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
8ace0 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 ex.xMutexNotheld
8acf0 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 (p);.}.#endif..#
8ad00 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
8ad10 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f OMIT_MUTEX */../
8ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
8ad30 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a nd of mutex.c **
8ad40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
8ad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
8ad80 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
8ad90 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a noop.c *********
8ada0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8adb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
8adc0 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 *.** 2008 Octobe
8add0 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 07.**.** The a
8ade0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
8adf0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
8ae00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
8ae10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
8ae20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
8ae30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
8ae40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
8ae50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
8ae60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
8ae70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
8ae80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
8ae90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
8aea0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
8aeb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
8aec0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
8aed0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
8aee0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
8aef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af30 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
8af40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
8af50 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
8af60 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
8af70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 ..**.** This imp
8af80 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 lementation in t
8af90 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f his file does no
8afa0 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 t provide any mu
8afb0 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f tual.** exclusio
8afc0 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 n and is thus su
8afd0 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f itable for use o
8afe0 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 nly in applicati
8aff0 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 ons.** that use
8b000 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 SQLite in a sing
8b010 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
8b020 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 routines defined
8b030 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 .** here are pla
8b040 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 ce-holders. App
8b050 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 lications can su
8b060 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 bstitute working
8b070 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e .** mutex routin
8b080 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 es at start-time
8b090 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a using the.**.**
8b0a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e sqlite3_con
8b0b0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
8b0c0 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a G_MUTEX,...).**.
8b0d0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a ** interface..**
8b0e0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 .** If compiled
8b0f0 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 with SQLITE_DEBU
8b100 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e G, then addition
8b110 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 al logic is inse
8b120 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 rted.** that doe
8b130 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 s error checking
8b140 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d on mutexes to m
8b150 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 ake sure they ar
8b160 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 e being.** calle
8b170 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a d correctly..**.
8b180 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f ** $Id: mutex_no
8b190 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f op.c,v 1.3 2008/
8b1a0 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64 12/05 17:17:08 d
8b1b0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 rh Exp $.*/...#i
8b1c0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
8b1d0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
8b1e0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
8b1f0 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 DEBUG)./*.** Stu
8b200 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 b routines for a
8b210 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 ll mutex methods
8b220 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
8b230 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f tines provide no
8b240 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
8b250 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b n or error check
8b260 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
8b270 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 nt noopMutexHeld
8b280 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8b290 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a p){ return 1; }.
8b2a0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d static int noopM
8b2b0 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
8b2c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
8b2d0 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 eturn 1; }.stati
8b2e0 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 c int noopMutexI
8b2f0 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
8b300 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
8b310 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
8b320 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
8b330 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
8b340 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 }.static sqlite3
8b350 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 _mutex *noopMute
8b360 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 xAlloc(int id){
8b370 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
8b380 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 mutex*)8; }.stat
8b390 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
8b3a0 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
8b3b0 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b tex *p){ return;
8b3c0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e }.static void n
8b3d0 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 oopMutexEnter(sq
8b3e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8b3f0 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 return; }.stati
8b400 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 c int noopMutexT
8b410 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
8b420 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c *p){ return SQL
8b430 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
8b440 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c void noopMutexL
8b450 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
8b460 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
8b470 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
8b480 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
8b490 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
8b4a0 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
8b4b0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
8b4c0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
8b4d0 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
8b4e0 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c noopMutexInit,
8b4f0 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e . noopMutexEn
8b500 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 d,. noopMutex
8b510 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d Alloc,. noopM
8b520 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f utexFree,. no
8b530 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 opMutexEnter,.
8b540 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a noopMutexTry,.
8b550 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 noopMutexLea
8b560 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ve,.. noopMut
8b570 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 exHeld,. noop
8b580 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d MutexNotheld. }
8b590 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
8b5a0 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
8b5b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
8b5c0 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 MUTEX_NOOP) && !
8b5d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
8b5e0 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 EBUG) */..#if de
8b5f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
8b600 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 EX_NOOP) && defi
8b610 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
8b620 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 )./*.** In this
8b630 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
8b640 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 error checking i
8b650 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 s provided for t
8b660 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 esting.** and de
8b670 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
8b680 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 . The mutexes s
8b690 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 till do not prov
8b6a0 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 ide any.** mutua
8b6b0 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a l exclusion..*/.
8b6c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ./*.** The mutex
8b6d0 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 object.*/.struc
8b6e0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
8b6f0 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 {. int id;
8b700 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 /* The mutex typ
8b710 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 e */. int cnt;
8b720 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
8b730 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 entries without
8b740 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 a matching leave
8b750 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
8b760 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
8b770 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
8b780 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
8b790 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
8b7a0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
8b7b0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
8b7c0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
8b7d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 /.static int deb
8b7e0 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 ugMutexHeld(sqli
8b7f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
8b800 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
8b810 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 p->cnt>0;.}.stat
8b820 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
8b830 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
8b840 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
8b850 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e turn p==0 || p->
8b860 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cnt==0;.}../*.**
8b870 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
8b880 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
8b890 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
8b8a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8b8b0 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 debugMutexInit(v
8b8c0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
8b8d0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
8b8e0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 int debugMutexE
8b8f0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
8b900 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
8b910 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
8b920 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
8b930 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
8b940 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
8b950 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
8b960 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
8b970 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
8b980 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
8b990 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
8b9a0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
8b9b0 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ed. .*/.static s
8b9c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 qlite3_mutex *de
8b9d0 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e bugMutexAlloc(in
8b9e0 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 t id){. static
8b9f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 sqlite3_mutex aS
8ba00 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 tatic[6];. sqli
8ba10 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 te3_mutex *pNew
8ba20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 = 0;. switch( i
8ba30 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 d ){. case SQ
8ba40 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a LITE_MUTEX_FAST:
8ba50 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
8ba60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
8ba70 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d : {. pNew =
8ba80 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
8ba90 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
8baa0 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
8bab0 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
8bac0 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 d = id;.
8bad0 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 pNew->cnt = 0;.
8bae0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
8baf0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
8bb00 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
8bb10 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 ssert( id-2 >= 0
8bb20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
8bb30 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 ( id-2 < (int)(s
8bb40 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 izeof(aStatic)/s
8bb50 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d izeof(aStatic[0]
8bb60 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 )) );. pNew
8bb70 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 = &aStatic[id-2
8bb80 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 ];. pNew->i
8bb90 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 d = id;. br
8bba0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
8bbb0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
8bbc0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
8bbd0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
8bbe0 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c a previously all
8bbf0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f ocated mutex..*/
8bc00 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
8bc10 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 ugMutexFree(sqli
8bc20 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
8bc30 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d assert( p->cnt=
8bc40 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
8bc50 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
8bc60 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 TEX_FAST || p->i
8bc70 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
8bc80 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 RECURSIVE );. s
8bc90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
8bca0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
8bcb0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
8bcc0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
8bcd0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
8bce0 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
8bcf0 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
8bd00 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
8bd10 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
8bd20 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
8bd30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
8bd40 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
8bd50 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
8bd60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
8bd70 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
8bd80 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
8bd90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
8bda0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
8bdb0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
8bdc0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
8bdd0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
8bde0 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
8bdf0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
8be00 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
8be10 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
8be20 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
8be30 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
8be40 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
8be50 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
8be60 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
8be70 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
8be80 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
8be90 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
8bea0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
8beb0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
8bec0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
8bed0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
8bee0 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
8bef0 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
8bf00 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
8bf10 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 static void debu
8bf20 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 gMutexEnter(sqli
8bf30 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
8bf40 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
8bf50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8bf60 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d URSIVE || debugM
8bf70 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
8bf80 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a ;. p->cnt++;.}.
8bf90 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 static int debug
8bfa0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
8bfb0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
8bfc0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
8bfd0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8bfe0 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
8bff0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 xNotheld(p) );.
8c000 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 p->cnt++;. ret
8c010 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
8c020 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
8c030 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
8c040 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 ) routine exits
8c050 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 a mutex that was
8c060 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 .** previously e
8c070 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 ntered by the sa
8c080 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 me thread. The
8c090 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 behavior.** is u
8c0a0 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
8c0b0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 mutex is not cur
8c0c0 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f rently entered o
8c0d0 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 r.** is not curr
8c0e0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e ently allocated.
8c0f0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 SQLite will ne
8c100 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a ver do either..*
8c110 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 /.static void de
8c120 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 bugMutexLeave(sq
8c130 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
8c140 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 . assert( debug
8c150 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
8c160 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 p->cnt--;. as
8c170 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
8c180 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8c190 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
8c1a0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d xNotheld(p) );.}
8c1b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
8c1c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
8c1d0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 ethods *sqlite3D
8c1e0 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
8c1f0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
8c200 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
8c210 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
8c220 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c debugMutexInit,
8c230 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 . debugMutexE
8c240 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 nd,. debugMut
8c250 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 exAlloc,. deb
8c260 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 ugMutexFree,.
8c270 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 debugMutexEnter
8c280 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
8c290 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 Try,. debugMu
8c2a0 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 texLeave,.. d
8c2b0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 ebugMutexHeld,.
8c2c0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 debugMutexNot
8c2d0 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 held. };.. ret
8c2e0 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 urn &sMutex;.}.#
8c2f0 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
8c300 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f (SQLITE_MUTEX_NO
8c310 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 OP) && defined(S
8c320 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a QLITE_DEBUG) */.
8c330 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8c340 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f End of mutex_no
8c350 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a op.c ***********
8c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8c380 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8c390 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
8c3a0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
8c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8c3d0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
8c3e0 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 28.**.** The
8c3f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
8c400 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
8c410 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
8c420 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
8c430 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
8c440 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
8c450 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
8c460 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
8c470 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
8c480 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
8c490 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
8c4a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
8c4b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
8c4c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
8c4d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
8c4e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
8c4f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c540 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
8c550 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
8c560 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
8c570 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
8c580 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a s for OS/2.**.**
8c590 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e $Id: mutex_os2.
8c5a0 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31 c,v 1.11 2008/11
8c5b0 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65 /22 19:50:54 pwe
8c5c0 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a ilbacher Exp $.*
8c5d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 /../*.** The cod
8c5e0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
8c5f0 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53 s only used if S
8c600 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 QLITE_MUTEX_OS2
8c610 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 is defined..** S
8c620 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66 ee the mutex.h f
8c630 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e ile for details.
8c640 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
8c650 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a E_MUTEX_OS2../**
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c670 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20 **** OS/2 Mutex
8c680 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a Implementation *
8c690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c6a0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
8c6b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
8c6c0 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75 of mutexes is bu
8c6d0 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53 ilt using the OS
8c6e0 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a /2 API..*/../*.*
8c6f0 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 * The mutex obje
8c700 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 ct.** Each recur
8c710 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e sive mutex is an
8c720 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
8c730 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
8c740 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
8c750 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
8c760 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20 HMTX mutex;
8c770 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e /* Mutex con
8c780 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 trolling the loc
8c790 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20 k */. int id;
8c7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
8c7b0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
8c7c0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a nRef; /*
8c7d0 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
8c7e0 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20 ences */. TID
8c7f0 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 owner; /*
8c800 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 Thread holding t
8c810 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a his mutex */.};.
8c820 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54 .#define OS2_MUT
8c830 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20 EX_INITIALIZER
8c840 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 0,0,0,0../*.**
8c850 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 Initialize and d
8c860 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
8c870 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e mutex subsystem.
8c880 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
8c890 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 s2MutexInit(void
8c8a0 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
8c8b0 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e _OK; }.static in
8c8c0 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f t os2MutexEnd(vo
8c8d0 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
8c8e0 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK; }../*.**
8c8f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
8c900 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
8c910 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
8c920 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
8c930 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
8c940 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 to it. If it r
8c950 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
8c960 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
8c970 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
8c980 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a be allocated. .
8c990 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
8c9a0 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
8c9b0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
8c9c0 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ror. The argume
8c9d0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 nt.** to sqlite3
8c9e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 _mutex_alloc() i
8c9f0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
8ca00 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
8ca10 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
8ca20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
8ca30 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 EX_FAST
8ca40 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 0.** <li>
8ca50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
8ca60 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 CURSIVE
8ca70 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 1.** <li> SQLI
8ca80 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
8ca90 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a MASTER 2.**
8caa0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
8cab0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 TEX_STATIC_MEM
8cac0 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 3.** <li>
8cad0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
8cae0 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 TATIC_PRNG
8caf0 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 4.** </ul>.**.
8cb00 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
8cb10 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
8cb20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
8cb30 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
8cb40 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
8cb50 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
8cb60 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
8cb70 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
8cb80 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
8cb90 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
8cba0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
8cbb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
8cbc0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
8cbd0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
8cbe0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
8cbf0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
8cc00 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
8cc10 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
8cc20 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
8cc30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
8cc40 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
8cc50 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
8cc60 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
8cc70 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
8cc80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
8cc90 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
8cca0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
8ccb0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
8ccc0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
8ccd0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
8cce0 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
8ccf0 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
8cd00 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
8cd10 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
8cd20 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
8cd30 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
8cd40 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
8cd50 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
8cd60 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
8cd70 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
8cd80 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
8cd90 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
8cda0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
8cdb0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
8cdc0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
8cdd0 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 . Three static
8cde0 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 mutexes are.** u
8cdf0 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 sed by the curre
8ce00 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 nt version of SQ
8ce10 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 Lite. Future ve
8ce20 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
8ce30 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 .** may add addi
8ce40 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 tional static mu
8ce50 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d texes. Static m
8ce60 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 utexes are for i
8ce70 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 nternal.** use b
8ce80 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 y SQLite only.
8ce90 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 Applications tha
8cea0 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 t use SQLite mut
8ceb0 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 exes should.** u
8cec0 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 se only the dyna
8ced0 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 mic mutexes retu
8cee0 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d rned by SQLITE_M
8cef0 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 UTEX_FAST or.**
8cf00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8cf10 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f URSIVE..**.** No
8cf20 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
8cf30 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
8cf40 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
8cf50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
8cf60 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
8cf70 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
8cf80 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
8cf90 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
8cfa0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
8cfb0 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
8cfc0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
8cfd0 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 But for the stat
8cfe0 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 ic.** mutex type
8cff0 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 s, the same mute
8d000 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e x is returned on
8d010 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 every call that
8d020 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 has.** the same
8d030 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f type number..*/
8d040 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
8d050 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41 mutex *os2MutexA
8d060 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b lloc(int iType){
8d070 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
8d080 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 *p = NULL;. sw
8d090 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
8d0a0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
8d0b0 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
8d0c0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
8d0d0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
8d0e0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
8d0f0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
8d100 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
8d110 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 f( p ){.
8d120 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
8d130 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72 if( DosCr
8d140 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c eateMutexSem( 0,
8d150 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 &p->mutex, 0, F
8d160 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 ALSE ) != NO_ERR
8d170 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OR ){.
8d180 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 sqlite3_free( p
8d190 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d );. p =
8d1a0 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d NULL;. }
8d1b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
8d1c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
8d1d0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
8d1e0 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 static volatile
8d1f0 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b int isInit = 0;
8d200 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 . static sq
8d210 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 lite3_mutex stat
8d220 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a icMutexes[] = {.
8d230 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
8d240 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8d250 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
8d260 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
8d270 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 ZER, },.
8d280 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 { OS2_MUTEX_INIT
8d290 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 IALIZER, },.
8d2a0 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
8d2b0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
8d2c0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
8d2d0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8d2e0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
8d2f0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
8d300 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b ZER, },. };
8d310 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49 . if ( !isI
8d320 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41 nit ){. A
8d330 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20 PIRET rc;.
8d340 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 PTIB ptib;.
8d350 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a PPIB ppib;.
8d360 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74 HMTX mut
8d370 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ex;. char
8d380 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20 name[32];.
8d390 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
8d3a0 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 cks( &ptib, &ppi
8d3b0 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c b );. sql
8d3c0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73 ite3_snprintf( s
8d3d0 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d izeof(name), nam
8d3e0 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c e, "\\SEM32\\SQL
8d3f0 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20 ITE%04x",.
8d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8d410 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c ppib->pib_ul
8d420 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77 pid );. w
8d430 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b hile( !isInit ){
8d440 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 . mutex
8d450 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
8d460 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75 rc = DosCreateMu
8d470 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d texSem( name, &m
8d480 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b utex, 0, FALSE);
8d490 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
8d4a0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b c == NO_ERROR ){
8d4b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 . uns
8d4c0 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 igned int i;.
8d4d0 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 if( !is
8d4e0 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
8d4f0 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 for( i = 0
8d500 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 ; i < sizeof(sta
8d510 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
8d520 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
8d530 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 [0]); i++ ){.
8d540 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 Dos
8d550 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 CreateMutexSem(
8d560 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 0, &staticMutexe
8d570 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 s[i].mutex, 0, F
8d580 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 ALSE );.
8d590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
8d5a0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
8d5b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
8d5c0 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 DosC
8d5d0 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 loseMutexSem( mu
8d5e0 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 tex );.
8d5f0 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d }else if( rc ==
8d600 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 ERROR_DUPLICATE
8d610 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 _NAME ){.
8d620 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 DosSleep( 1
8d630 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 );. }e
8d640 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
8d650 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 return p;.
8d660 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
8d670 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
8d680 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e ssert( iType-2 >
8d690 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 = 0 );. ass
8d6a0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 ert( iType-2 < s
8d6b0 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
8d6c0 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 xes)/sizeof(stat
8d6d0 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b icMutexes[0]) );
8d6e0 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 . p = &stat
8d6f0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
8d700 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
8d710 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
8d720 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
8d730 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
8d740 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
8d750 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
8d760 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
8d770 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 cated mutex..**
8d780 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
8d790 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
8d7a0 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 every mutex that
8d7b0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
8d7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
8d7d0 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 2MutexFree(sqlit
8d7e0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
8d7f0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
8d800 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
8d810 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 nRef==0 );. ass
8d820 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
8d830 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c TE_MUTEX_FAST ||
8d840 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
8d850 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 UTEX_RECURSIVE )
8d860 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 ;. DosCloseMute
8d870 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 xSem( p->mutex )
8d880 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
8d890 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ( p );.}../*.**
8d8a0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
8d8b0 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
8d8c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
8d8d0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
8d8e0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
8d8f0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
8d900 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
8d910 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
8d920 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
8d930 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
8d940 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
8d950 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
8d960 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
8d970 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
8d980 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
8d990 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
8d9a0 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
8d9b0 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
8d9c0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
8d9d0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
8d9e0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
8d9f0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
8da00 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
8da10 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
8da20 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
8da30 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
8da40 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
8da50 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
8da60 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
8da70 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
8da80 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
8da90 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
8daa0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
8dab0 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
8dac0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
8dad0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
8dae0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
8daf0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
8db00 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
8db10 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 id os2MutexEnter
8db20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8db30 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
8db40 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 PID holder1;.
8db50 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 ULONG holder2;.
8db60 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
8db70 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
8db80 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
8db90 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f X_RECURSIVE || o
8dba0 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 s2MutexNotheld(p
8dbb0 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 ) );. DosReques
8dbc0 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 tMutexSem(p->mut
8dbd0 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 ex, SEM_INDEFINI
8dbe0 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 TE_WAIT);. DosQ
8dbf0 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e ueryMutexSem(p->
8dc00 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c mutex, &holder1,
8dc10 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 &tid, &holder2)
8dc20 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 ;. p->owner = t
8dc30 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b id;. p->nRef++;
8dc40 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 .}.static int os
8dc50 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 2MutexTry(sqlite
8dc60 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
8dc70 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 nt rc;. TID tid
8dc80 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b ;. PID holder1;
8dc90 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 . ULONG holder2
8dca0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
8dcb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
8dcc0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
8dcd0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
8dce0 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d ECURSIVE || os2M
8dcf0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
8dd00 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 ;. if( DosReque
8dd10 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 stMutexSem(p->mu
8dd20 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 tex, SEM_IMMEDIA
8dd30 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f TE_RETURN) == NO
8dd40 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f _ERROR) {. Do
8dd50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
8dd60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
8dd70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
8dd80 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 2);. p->owner
8dd90 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e = tid;. p->n
8dda0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
8ddb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 SQLITE_OK;. } e
8ddc0 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 lse {. rc = S
8ddd0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
8dde0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
8ddf0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8de00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
8de10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
8de20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
8de30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
8de40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
8de50 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
8de60 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
8de70 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
8de80 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
8de90 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
8dea0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
8deb0 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
8dec0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
8ded0 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
8dee0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
8def0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 MutexLeave(sqlit
8df00 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
8df10 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
8df20 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
8df30 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
8df40 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
8df50 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
8df60 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 0 );. DosQueryM
8df70 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 utexSem(p->mutex
8df80 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 , &holder1, &tid
8df90 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 , &holder2);. a
8dfa0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
8dfb0 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 =tid );. p->nRe
8dfc0 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
8dfd0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
8dfe0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
8dff0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
8e000 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 DosReleaseMutexS
8e010 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a em(p->mutex);.}.
8e020 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
8e030 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
8e040 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8e050 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
8e060 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
8e070 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
8e080 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
8e090 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
8e0a0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 statements..*/.s
8e0b0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
8e0c0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
8e0d0 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
8e0e0 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a tid;. PID pid;.
8e0f0 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b ULONG ulCount;
8e100 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 . PTIB ptib;.
8e110 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20 if( p!=0 ) {.
8e120 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 DosQueryMutexSe
8e130 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 m(p->mutex, &pid
8e140 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 , &tid, &ulCount
8e150 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 );. } else {.
8e160 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
8e170 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b ks(&ptib, NULL);
8e180 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d . tid = ptib-
8e190 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 >tib_ptib2->tib2
8e1a0 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 _ultid;. }. re
8e1b0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d turn p==0 || (p-
8e1c0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f >nRef!=0 && p->o
8e1d0 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 wner==tid);.}.st
8e1e0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
8e1f0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
8e200 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 _mutex *p){. TI
8e210 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 D tid;. PID pid
8e220 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e ;. ULONG ulCoun
8e230 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a t;. PTIB ptib;.
8e240 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a if( p!= 0 ) {.
8e250 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 DosQueryMute
8e260 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 xSem(p->mutex, &
8e270 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f pid, &tid, &ulCo
8e280 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b unt);. } else {
8e290 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 . DosGetInfoB
8e2a0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c locks(&ptib, NUL
8e2b0 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 L);. tid = pt
8e2c0 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 ib->tib_ptib2->t
8e2d0 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 ib2_ultid;. }.
8e2e0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
8e2f0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
8e300 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 >owner!=tid;.}.#
8e310 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
8e320 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
8e330 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
8e340 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
8e350 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
8e360 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
8e370 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
8e380 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e {. os2MutexIn
8e390 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 it,. os2Mutex
8e3a0 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 End,. os2Mute
8e3b0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d xAlloc,. os2M
8e3c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 utexFree,. os
8e3d0 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 2MutexEnter,.
8e3e0 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 os2MutexTry,.
8e3f0 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c os2MutexLeave,
8e400 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
8e410 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 EBUG. os2Mute
8e420 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 xHeld,. os2Mu
8e430 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
8e440 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
8e450 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
8e460 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
8e470 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_OS2 */../****
8e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
8e490 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a f mutex_os2.c **
8e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
8e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
8e4e0 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 file mutex_unix
8e4f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
8e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
8e520 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
8e530 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
8e540 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
8e550 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
8e560 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
8e570 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
8e580 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
8e590 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
8e5a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
8e5b0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
8e5c0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
8e5d0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
8e5e0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
8e5f0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
8e600 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
8e610 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
8e620 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
8e630 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
8e640 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
8e650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
8e690 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
8e6a0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
8e6b0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
8e6c0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
8e6d0 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 pthreads.**.** $
8e6e0 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 Id: mutex_unix.c
8e6f0 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f ,v 1.16 2008/12/
8e700 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20 08 18:19:18 drh
8e710 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
8e720 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
8e730 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 file is only us
8e740 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d ed if we are com
8e750 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 piling threadsaf
8e760 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 e.** under unix
8e770 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a with pthreads..*
8e780 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 *.** Note that t
8e790 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
8e7a0 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 on requires a ve
8e7b0 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 rsion of pthread
8e7c0 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 s that.** suppor
8e7d0 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ts recursive mut
8e7e0 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 exes..*/.#ifdef
8e7f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
8e800 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 READS..#include
8e810 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a <pthread.h>.../*
8e820 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 .** Each recursi
8e830 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 ve mutex is an i
8e840 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
8e850 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
8e860 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 re..*/.struct sq
8e870 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
8e880 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 pthread_mutex_t
8e890 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 mutex; /* Mu
8e8a0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
8e8b0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
8e8c0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 t id;
8e8d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
8e8e0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
8e8f0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
8e900 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
8e910 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f of entrances */
8e920 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e . pthread_t own
8e930 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a er; /*
8e940 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 Thread that is
8e950 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 within this mute
8e960 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 x */.#ifdef SQLI
8e970 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 TE_DEBUG. int t
8e980 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 race;
8e990 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
8e9a0 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a trace changes *
8e9b0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 /.#endif.};.#ifd
8e9c0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
8e9d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
8e9e0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
8e9f0 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 R { PTHREAD_MUTE
8ea00 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 X_INITIALIZER, 0
8ea10 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 , 0, (pthread_t)
8ea20 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 0, 0 }.#else.#de
8ea30 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
8ea40 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
8ea50 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 PTHREAD_MUTEX_I
8ea60 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 NITIALIZER, 0, 0
8ea70 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d , (pthread_t)0 }
8ea80 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
8ea90 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
8eaa0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
8eab0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
8eac0 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
8ead0 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
8eae0 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 use only inside
8eaf0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
8eb00 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c nts. On some pl
8eb10 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 atforms,.** ther
8eb20 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 e might be race
8eb30 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 conditions that
8eb40 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 can cause these
8eb50 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 routines to.** d
8eb60 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 eliver incorrect
8eb70 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 results. In pa
8eb80 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 rticular, if pth
8eb90 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a read_equal() is.
8eba0 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 ** not an atomic
8ebb0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
8ebc0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
8ebd0 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a might delivery.*
8ebe0 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 * incorrect resu
8ebf0 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c lts. On most pl
8ec00 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 atforms, pthread
8ec10 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a _equal() is a .*
8ec20 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 * comparison of
8ec30 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 two integers and
8ec40 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 is therefore at
8ec50 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 omic. But we ar
8ec60 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 e.** told that H
8ec70 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 PUX is not such
8ec80 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 a platform. If
8ec90 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 so, then these r
8eca0 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 outines.** will
8ecb0 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 not always work
8ecc0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 correctly on HPU
8ecd0 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 X..**.** On thos
8ece0 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 e platforms wher
8ecf0 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 e pthread_equal(
8ed00 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c ) is not atomic,
8ed10 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c SQLite.** shoul
8ed20 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 d be compiled wi
8ed30 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 thout -DSQLITE_D
8ed40 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 EBUG and with -D
8ed50 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b NDEBUG to.** mak
8ed60 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 e sure no assert
8ed70 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 () statements ar
8ed80 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 e evaluated and
8ed90 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 hence these.** r
8eda0 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 outines are neve
8edb0 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 r called..*/.#if
8edc0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
8edd0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
8ede0 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 ITE_DEBUG).stati
8edf0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 c int pthreadMut
8ee00 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
8ee10 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
8ee20 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 rn (p->nRef!=0 &
8ee30 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
8ee40 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 p->owner, pthrea
8ee50 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 d_self()));.}.st
8ee60 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
8ee70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c MutexNotheld(sql
8ee80 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
8ee90 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 return p->nRef
8eea0 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 ==0 || pthread_e
8eeb0 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 qual(p->owner, p
8eec0 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d thread_self())==
8eed0 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0;.}.#endif../*.
8eee0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e ** Initialize an
8eef0 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 d deinitialize t
8ef00 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
8ef10 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
8ef20 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e t pthreadMutexIn
8ef30 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
8ef40 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
8ef50 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
8ef60 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
8ef70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
8ef80 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ; }../*.** The s
8ef90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
8efa0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c oc() routine all
8efb0 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 ocates a new.**
8efc0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e mutex and return
8efd0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
8efe0 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e t. If it return
8eff0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d s NULL.** that m
8f000 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 eans that a mute
8f010 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 x could not be a
8f020 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
8f030 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 e.** will unwind
8f040 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 its stack and r
8f050 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 eturn an error.
8f060 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a The argument.**
8f070 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
8f080 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 x_alloc() is one
8f090 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
8f0a0 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a r constants:.**.
8f0b0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
8f0c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
8f0d0 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ST.** <li> SQLI
8f0e0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8f0f0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 VE.** <li> SQLI
8f100 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
8f110 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 MASTER.** <li>
8f120 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
8f130 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 TIC_MEM.** <li>
8f140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
8f150 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 ATIC_MEM2.** <li
8f160 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
8f170 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c STATIC_PRNG.** <
8f180 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
8f190 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 X_STATIC_LRU.**
8f1a0 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
8f1b0 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 first two consta
8f1c0 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 nts cause sqlite
8f1d0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
8f1e0 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e to create.** a n
8f1f0 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e ew mutex. The n
8f200 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 ew mutex is recu
8f210 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 rsive when SQLIT
8f220 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
8f230 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 E.** is used but
8f240 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
8f250 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f so when SQLITE_
8f260 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 MUTEX_FAST is us
8f270 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ed..** The mutex
8f280 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
8f290 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
8f2a0 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 make a distinct
8f2b0 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 ion.** between S
8f2c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
8f2d0 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 RSIVE and SQLITE
8f2e0 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 _MUTEX_FAST if i
8f2f0 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 t does.** not wa
8f300 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 nt to. But SQLi
8f310 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 te will only req
8f320 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 uest a recursive
8f330 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 mutex in.** cas
8f340 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c es where it real
8f350 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 ly needs one. I
8f360 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 f a faster non-r
8f370 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a ecursive mutex.*
8f380 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
8f390 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e is available on
8f3a0 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f the host platfo
8f3b0 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 rm, the mutex su
8f3c0 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 bsystem.** might
8f3d0 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d return such a m
8f3e0 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 utex in response
8f3f0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 to SQLITE_MUTEX
8f400 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 _FAST..**.** The
8f410 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 other allowed p
8f420 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c arameters to sql
8f430 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
8f440 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a () each return.*
8f450 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
8f460 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 static preexist
8f470 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65 ing mutex. Thre
8f480 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 e static mutexes
8f490 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
8f4a0 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
8f4b0 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
8f4c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
8f4d0 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
8f4e0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
8f4f0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
8f500 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
8f510 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
8f520 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
8f530 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
8f540 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
8f550 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
8f560 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
8f570 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
8f580 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
8f590 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
8f5a0 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
8f5b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
8f5c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
8f5d0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 if one of the d
8f5e0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 ynamic mutex par
8f5f0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f ameters (SQLITE_
8f600 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 MUTEX_FAST.** or
8f610 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
8f620 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 CURSIVE) is used
8f630 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 then sqlite3_mu
8f640 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 tex_alloc().** r
8f650 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 eturns a differe
8f660 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 nt mutex on ever
8f670 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 y call. But for
8f680 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 the static .**
8f690 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
8f6a0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
8f6b0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
8f6c0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
8f6d0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
8f6e0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
8f6f0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
8f700 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c *pthreadMutexAll
8f710 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 oc(int iType){.
8f720 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
8f730 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
8f740 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 xes[] = {. SQ
8f750 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
8f760 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
8f770 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
8f780 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
8f790 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
8f7a0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
8f7b0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
8f7c0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
8f7d0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
8f7e0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
8f7f0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
8f800 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 ER. };. sqlite
8f810 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 3_mutex *p;. sw
8f820 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
8f830 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
8f840 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 UTEX_RECURSIVE:
8f850 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 {. p = sqli
8f860 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
8f870 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 izeof(*p) );.
8f880 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 if( p ){.#ifd
8f890 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 ef SQLITE_HOMEGR
8f8a0 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
8f8b0 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 TEX. /* I
8f8c0 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
8f8d0 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
8f8e0 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 lable, we will h
8f8f0 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a ave to. *
8f900 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e * build our own.
8f910 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a See below. */.
8f920 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
8f930 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d mutex_init(&p->m
8f940 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a utex, 0);.#else.
8f950 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 /* Use a
8f960 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
8f970 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 if it is availa
8f980 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 ble */. p
8f990 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 thread_mutexattr
8f9a0 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 _t recursiveAttr
8f9b0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
8f9c0 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 d_mutexattr_init
8f9d0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 (&recursiveAttr)
8f9e0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
8f9f0 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 d_mutexattr_sett
8fa00 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 ype(&recursiveAt
8fa10 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 tr, PTHREAD_MUTE
8fa20 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 X_RECURSIVE);.
8fa30 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
8fa40 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 tex_init(&p->mut
8fa50 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 ex, &recursiveAt
8fa60 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 tr);. pth
8fa70 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 read_mutexattr_d
8fa80 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 estroy(&recursiv
8fa90 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 eAttr);.#endif.
8faa0 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 p->id = i
8fab0 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Type;. }.
8fac0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
8fad0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
8fae0 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 _MUTEX_FAST: {.
8faf0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p = sqlite3
8fb00 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
8fb10 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 of(*p) );.
8fb20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 if( p ){.
8fb30 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
8fb40 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
8fb50 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d mutex_init(&p->m
8fb60 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 utex, 0);.
8fb70 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
8fb80 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 }. default
8fb90 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
8fba0 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 ( iType-2 >= 0 )
8fbb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
8fbc0 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 iType-2 < ArrayS
8fbd0 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 ize(staticMutexe
8fbe0 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 s) );. p =
8fbf0 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 &staticMutexes[i
8fc00 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 Type-2];. p
8fc10 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 ->id = iType;.
8fc20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
8fc30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
8fc40 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .}.../*.** This
8fc50 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
8fc60 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
8fc70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 .** allocated mu
8fc80 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 tex. SQLite is
8fc90 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
8fca0 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d ocate every.** m
8fcb0 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c utex that it all
8fcc0 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ocates..*/.stati
8fcd0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
8fce0 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
8fcf0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
8fd00 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
8fd10 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
8fd20 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
8fd30 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d _FAST || p->id==
8fd40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8fd50 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 URSIVE );. pthr
8fd60 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f ead_mutex_destro
8fd70 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 y(&p->mutex);.
8fd80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
8fd90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
8fda0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
8fdb0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
8fdc0 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
8fdd0 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
8fde0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
8fdf0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
8fe00 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
8fe10 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
8fe20 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
8fe30 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
8fe40 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
8fe50 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
8fe60 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
8fe70 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
8fe80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
8fe90 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
8fea0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
8feb0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
8fec0 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
8fed0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
8fee0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8fef0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
8ff00 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
8ff10 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
8ff20 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
8ff30 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
8ff40 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
8ff50 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
8ff60 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
8ff70 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
8ff80 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
8ff90 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
8ffa0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
8ffb0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
8ffc0 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
8ffd0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
8ffe0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
8fff0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
90000 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 .static void pth
90010 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 readMutexEnter(s
90020 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
90030 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
90040 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
90050 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 RECURSIVE || pth
90060 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
90070 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 (p) );..#ifdef S
90080 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f QLITE_HOMEGROWN_
90090 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a RECURSIVE_MUTEX.
900a0 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 /* If recursiv
900b0 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f e mutexes are no
900c0 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 t available, the
900d0 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f n we have to gro
900e0 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 w. ** our own.
900f0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
90100 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
90110 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 t pthread_equal(
90120 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 ). ** is atomic
90130 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f - that it canno
90140 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e t be deceived in
90150 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 to thinking self
90160 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e . ** and p->own
90170 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 er are equal if
90180 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 p->owner changes
90190 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c between two val
901a0 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 ues. ** that ar
901b0 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 e not equal to s
901c0 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f elf while the co
901d0 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 mparison is taki
901e0 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 ng place.. ** T
901f0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
90200 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 on also assumes
90210 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 a coherent cache
90220 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 - that . ** se
90230 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 parate processes
90240 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 cannot read dif
90250 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 ferent values fr
90260 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a om the same. **
90270 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 address at the
90280 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 same time. If e
90290 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 ither of these t
902a0 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 wo conditions.
902b0 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 ** are not met,
902c0 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 then the mutexes
902d0 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 will fail and p
902e0 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 roblems will res
902f0 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 ult.. */. {.
90300 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 pthread_t self
90310 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
90320 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 );. if( p->nR
90330 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f ef>0 && pthread_
90340 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 equal(p->owner,
90350 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 self) ){. p
90360 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 ->nRef++;. }e
90370 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 lse{. pthre
90380 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 ad_mutex_lock(&p
90390 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
903a0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
903b0 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f =0 );. p->o
903c0 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 wner = self;.
903d0 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a p->nRef = 1;.
903e0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a }. }.#else.
903f0 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 /* Use the bui
90400 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 lt-in recursive
90410 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 mutexes if they
90420 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 are available..
90430 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 */. pthread_mu
90440 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 tex_lock(&p->mut
90450 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 ex);. p->owner
90460 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
90470 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 ;. p->nRef++;.#
90480 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
90490 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 LITE_DEBUG. if(
904a0 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 p->trace ){.
904b0 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d printf("enter m
904c0 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 utex %p (%d) wit
904d0 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c h nRef=%d\n", p,
904e0 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 p->trace, p->nR
904f0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ef);. }.#endif.
90500 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 }.static int pth
90510 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c readMutexTry(sql
90520 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
90530 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
90540 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
90550 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
90560 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 E || pthreadMute
90570 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a xNotheld(p) );..
90580 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f #ifdef SQLITE_HO
90590 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 MEGROWN_RECURSIV
905a0 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 E_MUTEX. /* If
905b0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
905c0 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 s are not availa
905d0 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 ble, then we hav
905e0 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f e to grow. ** o
905f0 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d ur own. This im
90600 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 plementation ass
90610 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 umes that pthrea
90620 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 d_equal(). ** i
90630 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 s atomic - that
90640 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 it cannot be dec
90650 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b eived into think
90660 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e ing self. ** an
90670 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 d p->owner are e
90680 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 qual if p->owner
90690 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e changes between
906a0 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a two values. **
906b0 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 that are not eq
906c0 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c ual to self whil
906d0 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e e the comparison
906e0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 is taking place
906f0 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c .. ** This impl
90700 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 ementation also
90710 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 assumes a cohere
90720 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 nt cache - that
90730 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 . ** separate p
90740 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 rocesses cannot
90750 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 read different v
90760 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 alues from the s
90770 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 ame. ** address
90780 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
90790 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 e. If either of
907a0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 these two condi
907b0 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e tions. ** are n
907c0 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 ot met, then the
907d0 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 mutexes will fa
907e0 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 il and problems
907f0 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a will result.. *
90800 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 /. {. pthrea
90810 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 d_t self = pthre
90820 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 ad_self();. i
90830 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 f( p->nRef>0 &&
90840 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d pthread_equal(p-
90850 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b >owner, self) ){
90860 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b . p->nRef++
90870 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
90880 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 ITE_OK;. }els
90890 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 e if( pthread_mu
908a0 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e tex_trylock(&p->
908b0 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 mutex)==0 ){.
908c0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 assert( p->nR
908d0 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 ef==0 );. p
908e0 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a ->owner = self;.
908f0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 p->nRef =
90900 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 1;. rc = SQ
90910 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
90920 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
90930 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
90940 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a }. }.#else. /*
90950 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 Use the built-i
90960 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n recursive mute
90970 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 xes if they are
90980 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a available.. */.
90990 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 if( pthread_mu
909a0 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e tex_trylock(&p->
909b0 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 mutex)==0 ){.
909c0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 p->owner = pthr
909d0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 ead_self();.
909e0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 p->nRef++;. r
909f0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
90a00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
90a10 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
90a20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
90a30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
90a40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
90a50 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b K && p->trace ){
90a60 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 . printf("ent
90a70 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 er mutex %p (%d)
90a80 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 with nRef=%d\n"
90a90 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 , p, p->trace, p
90aa0 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e ->nRef);. }.#en
90ab0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b dif. return rc;
90ac0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
90ad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
90ae0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
90af0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
90b00 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
90b10 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
90b20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 same thread. Th
90b30 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
90b40 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
90b50 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
90b60 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
90b70 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 or.** is not cu
90b80 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
90b90 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 d. SQLite will
90ba0 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e never do either.
90bb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
90bc0 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 pthreadMutexLeav
90bd0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
90be0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
90bf0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 threadMutexHeld(
90c00 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d p) );. p->nRef-
90c10 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e -;. assert( p->
90c20 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 nRef==0 || p->id
90c30 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
90c40 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 ECURSIVE );..#if
90c50 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 def SQLITE_HOMEG
90c60 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d ROWN_RECURSIVE_M
90c70 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 UTEX. if( p->nR
90c80 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 ef==0 ){. pth
90c90 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 read_mutex_unloc
90ca0 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 k(&p->mutex);.
90cb0 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 }.#else. pthrea
90cc0 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
90cd0 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 p->mutex);.#endi
90ce0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
90cf0 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e _DEBUG. if( p->
90d00 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 trace ){. pri
90d10 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 ntf("leave mutex
90d20 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 %p (%d) with nR
90d30 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e ef=%d\n", p, p->
90d40 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b trace, p->nRef);
90d50 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 . }.#endif.}..S
90d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
90d70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
90d80 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
90d90 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
90da0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
90db0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
90dc0 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 Mutex = {. pt
90dd0 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a hreadMutexInit,.
90de0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
90df0 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 End,. pthread
90e00 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 MutexAlloc,.
90e10 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 pthreadMutexFree
90e20 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 ,. pthreadMut
90e30 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 exEnter,. pth
90e40 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 readMutexTry,.
90e50 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 pthreadMutexLe
90e60 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 ave,.#ifdef SQLI
90e70 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 TE_DEBUG. pth
90e80 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 readMutexHeld,.
90e90 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e pthreadMutexN
90ea0 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 otheld.#else.
90eb0 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0,. 0.#endif
90ec0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 . };.. return
90ed0 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 &sMutex;.}..#end
90ee0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
90ef0 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f EX_PTHREAD */../
90f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
90f10 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 nd of mutex_unix
90f20 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
90f60 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
90f70 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a w32.c **********
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
90fa0 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
90fb0 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
90fc0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
90fd0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
90fe0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
90ff0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
91000 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
91010 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
91020 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
91030 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
91040 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
91050 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
91060 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
91070 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
91080 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
91090 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
910a0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
910b0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
910c0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
910d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91110 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
91120 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
91130 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
91140 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 plement mutexes
91150 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 for win32.**.**
91160 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63 $Id: mutex_w32.c
91170 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30 31 2f ,v 1.15 2009/01/
91180 33 30 20 31 36 3a 30 39 3a 32 33 20 73 68 61 6e 30 16:09:23 shan
91190 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a e Exp $.*/../*.*
911a0 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 * The code in th
911b0 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 is file is only
911c0 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 used if we are c
911d0 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 ompiling multith
911e0 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 readed.** on a w
911f0 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a in32 system..*/.
91200 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
91210 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 TEX_W32../*.** E
91220 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 ach recursive mu
91230 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e tex is an instan
91240 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
91250 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
91260 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
91270 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 _mutex {. CRITI
91280 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 CAL_SECTION mute
91290 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 x; /* Mutex c
912a0 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c ontrolling the l
912b0 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b ock */. int id;
912c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
912d0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 /* Mutex typ
912e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b e */. int nRef;
912f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
91300 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
91310 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 nterances */. D
91320 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 WORD owner;
91330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 /* Thr
91340 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 ead holding this
91350 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a mutex */.};../*
91360 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
91370 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 (non-zero) if we
91380 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 are running und
91390 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c er WinNT, Win2K,
913a0 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 WinXP,.** or Wi
913b0 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c nCE. Return fal
913c0 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 se (zero) for Wi
913d0 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 n95, Win98, or W
913e0 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 inME..**.** Here
913f0 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 is an interesti
91400 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 ng observation:
91410 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 Win95, Win98, a
91420 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a nd WinME lack.**
91430 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 the LockFileEx(
91440 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 ) API. But we c
91450 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 an still statica
91460 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 lly link against
91470 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 that.** API as
91480 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 long as we don't
91490 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e call it win run
914a0 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 ning Win95/98/ME
914b0 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 . A call to.**
914c0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
914d0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
914e0 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 e if the host is
914f0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a Win95/98/ME or.
91500 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 ** WinNT/2K/XP s
91510 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b o that we will k
91520 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e now whether or n
91530 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 ot we can safely
91540 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 call.** the Loc
91550 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a kFileEx() API..*
91560 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 *.** mutexIsNT()
91570 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f is only used fo
91580 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 r the TryEnterCr
91590 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
915a0 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 API call,.** whi
915b0 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ch is only avail
915c0 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 able if your app
915d0 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d lication was com
915e0 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f piled with .** _
915f0 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 WIN32_WINNT defi
91600 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e ned to a value >
91610 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 = 0x0400. Curre
91620 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a ntly, the only.*
91630 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 * call to TryEnt
91640 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f erCriticalSectio
91650 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 n() is #ifdef'ed
91660 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 out, so #ifdef
91670 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 .** this out as
91680 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 well..*/.#if 0.#
91690 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
916a0 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 CE.# define mute
916b0 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c xIsNT() (1).#el
916c0 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
916d0 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b mutexIsNT(void){
916e0 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 . static int
916f0 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 osType = 0;.
91700 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b if( osType==0 ){
91710 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e . OSVERSION
91720 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 INFO sInfo;.
91730 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 sInfo.dwOSVers
91740 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 ionInfoSize = si
91750 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 zeof(sInfo);.
91760 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 GetVersionEx(
91770 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f &sInfo);. o
91780 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 sType = sInfo.dw
91790 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f PlatformId==VER_
917a0 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e PLATFORM_WIN32_N
917b0 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d T ? 2 : 1;. }
917c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 . return osTy
917d0 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 pe==2;. }.#endi
917e0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
917f0 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a INCE */.#endif..
91800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
91810 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 BUG./*.** The sq
91820 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
91830 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
91840 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 utex_notheld() r
91850 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e outine are.** in
91860 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f tended for use o
91870 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 nly inside asser
91880 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a t() statements..
91890 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
918a0 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 nMutexHeld(sqlit
918b0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
918c0 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d return p->nRef!=
918d0 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 0 && p->owner==G
918e0 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 etCurrentThreadI
918f0 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e d();.}.static in
91900 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c t winMutexNothel
91910 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
91920 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
91930 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f >nRef==0 || p->o
91940 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 wner!=GetCurrent
91950 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 ThreadId();.}.#e
91960 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ndif.../*.** Ini
91970 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
91980 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
91990 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
919a0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
919b0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
919c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
919d0 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 ; }.static int w
919e0 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 inMutexEnd(void)
919f0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
91a00 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OK; }../*.** The
91a10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
91a20 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 lloc() routine a
91a30 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a llocates a new.*
91a40 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 * mutex and retu
91a50 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
91a60 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 it. If it retu
91a70 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 rns NULL.** that
91a80 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 means that a mu
91a90 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 tex could not be
91aa0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
91ab0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 ite.** will unwi
91ac0 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 nd its stack and
91ad0 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
91ae0 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a . The argument.
91af0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 ** to sqlite3_mu
91b00 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f tex_alloc() is o
91b10 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
91b20 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a ger constants:.*
91b30 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
91b40 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
91b50 46 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 FAST
91b60 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 0.** <li> SQ
91b70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
91b80 53 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a SIVE 1.
91b90 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
91ba0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
91bb0 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c TER 2.** <l
91bc0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
91bd0 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 _STATIC_MEM
91be0 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 3.** <li> S
91bf0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
91c00 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 IC_PRNG 4
91c10 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ul>.**.**
91c20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f The first two co
91c30 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 nstants cause sq
91c40 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
91c50 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a c() to create.**
91c60 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 a new mutex. T
91c70 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 he new mutex is
91c80 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 recursive when S
91c90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91ca0 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 RSIVE.** is used
91cb0 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 but not necessa
91cc0 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c rily so when SQL
91cd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
91ce0 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d s used..** The m
91cf0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
91d00 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 ion does not nee
91d10 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 d to make a dist
91d20 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 inction.** betwe
91d30 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f en SQLITE_MUTEX_
91d40 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 RECURSIVE and SQ
91d50 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
91d60 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f if it does.** no
91d70 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 t want to. But
91d80 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 SQLite will only
91d90 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 request a recur
91da0 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a sive mutex in.**
91db0 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 cases where it
91dc0 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 really needs one
91dd0 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e . If a faster n
91de0 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 on-recursive mut
91df0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ex.** implementa
91e00 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
91e10 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c e on the host pl
91e20 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 atform, the mute
91e30 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d x subsystem.** m
91e40 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 ight return such
91e50 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 a mutex in resp
91e60 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d onse to SQLITE_M
91e70 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a UTEX_FAST..**.**
91e80 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 The other allow
91e90 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f ed parameters to
91ea0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
91eb0 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 lloc() each retu
91ec0 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 rn.** a pointer
91ed0 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 to a static pree
91ee0 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 xisting mutex.
91ef0 54 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 Three static mut
91f00 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 exes are.** used
91f10 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 by the current
91f20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 version of SQLit
91f30 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 e. Future versi
91f40 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a ons of SQLite.**
91f50 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f may add additio
91f60 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 nal static mutex
91f70 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 es. Static mute
91f80 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 xes are for inte
91f90 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 rnal.** use by S
91fa0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 QLite only. App
91fb0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 lications that u
91fc0 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 se SQLite mutexe
91fd0 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 s should.** use
91fe0 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 only the dynamic
91ff0 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 mutexes returne
92000 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
92010 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c X_FAST or.** SQL
92020 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
92030 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 IVE..**.** Note
92040 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 that if one of t
92050 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
92060 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c parameters (SQL
92070 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
92080 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 * or SQLITE_MUTE
92090 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 X_RECURSIVE) is
920a0 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 used then sqlite
920b0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 3_mutex_alloc().
920c0 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 ** returns a dif
920d0 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 ferent mutex on
920e0 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 every call. But
920f0 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 for the static
92100 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c .** mutex types,
92110 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 the same mutex
92120 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 is returned on e
92130 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 very call that h
92140 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 as.** the same t
92150 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 ype number..*/.s
92160 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 tatic sqlite3_mu
92170 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c 6c tex *winMutexAll
92180 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 oc(int iType){.
92190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
921a0 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69 54 p;.. switch( iT
921b0 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ype ){. case
921c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
921d0 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 T:. case SQLI
921e0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
921f0 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 VE: {. p =
92200 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
92210 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b o( sizeof(*p) );
92220 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a . if( p ){.
92230 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 p->id =
92240 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 49 iType;. I
92250 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 nitializeCritica
92260 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 lSection(&p->mut
92270 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ex);. }.
92280 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
92290 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
922a0 20 20 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 static sqli
922b0 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 te3_mutex static
922c0 4d 75 74 65 78 65 73 5b 36 5d 3b 0a 20 20 20 20 Mutexes[6];.
922d0 20 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 49 static int isI
922e0 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 nit = 0;. w
922f0 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b hile( !isInit ){
92300 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 . static
92310 6c 6f 6e 67 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 long lock = 0;.
92320 20 20 20 20 20 20 20 69 66 28 20 49 6e 74 65 72 if( Inter
92330 6c 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e 74 28 lockedIncrement(
92340 26 6c 6f 63 6b 29 3d 3d 31 20 29 7b 0a 20 20 20 &lock)==1 ){.
92350 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 int i;.
92360 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
92370 20 69 3c 73 69 7a 65 6f 66 28 73 74 61 74 69 63 i<sizeof(static
92380 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 Mutexes)/sizeof(
92390 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d staticMutexes[0]
923a0 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ); i++){.
923b0 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 InitializeC
923c0 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 riticalSection(&
923d0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d staticMutexes[i]
923e0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 .mutex);.
923f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
92400 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 sInit = 1;.
92410 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
92420 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 20 Sleep(1);.
92430 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
92440 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 assert( iT
92450 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 ype-2 >= 0 );.
92460 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
92470 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 e-2 < sizeof(sta
92480 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
92490 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
924a0 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20 [0]) );. p
924b0 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 = &staticMutexes
924c0 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 [iType-2];.
924d0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
924e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
924f0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
92500 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 p;.}.../*.** Thi
92510 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f s routine deallo
92520 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 cates a previous
92530 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 ly.** allocated
92540 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 mutex. SQLite i
92550 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 s careful to dea
92560 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a llocate every.**
92570 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 mutex that it a
92580 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 llocates..*/.sta
92590 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 tic void winMute
925a0 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
925b0 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
925c0 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 t( p );. assert
925d0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
925e0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
925f0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 =SQLITE_MUTEX_FA
92600 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c ST || p->id==SQL
92610 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
92620 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74 65 43 IVE );. DeleteC
92630 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 riticalSection(&
92640 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c p->mutex);. sql
92650 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a ite3_free(p);.}.
92660 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
92670 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
92680 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
92690 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
926a0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 s attempt.** to
926b0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 enter a mutex.
926c0 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 If another threa
926d0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 d is already wit
926e0 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a hin the mutex,.*
926f0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
92700 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f enter() will blo
92710 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d ck and sqlite3_m
92720 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 utex_try() will
92730 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
92740 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 _BUSY. The sqli
92750 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
92760 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
92770 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 s SQLITE_OK.** u
92780 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 pon successful e
92790 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 ntry. Mutexes c
927a0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c reated using SQL
927b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
927c0 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e IVE can.** be en
927d0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 tered multiple t
927e0 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 imes by the same
927f0 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 thread. In suc
92800 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 h cases the,.**
92810 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 mutex must be ex
92820 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 ited an equal nu
92830 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 mber of times be
92840 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 fore another thr
92850 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 ead.** can enter
92860 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 . If the same t
92870 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 hread tries to e
92880 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b nter any other k
92890 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 ind of mutex.**
928a0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
928b0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
928c0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
928d0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 atic void winMut
928e0 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f exEnter(sqlite3_
928f0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
92900 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
92910 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
92920 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f VE || winMutexNo
92930 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 45 6e theld(p) );. En
92940 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
92950 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 on(&p->mutex);.
92960 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 p->owner = GetC
92970 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 urrentThreadId()
92980 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a ; . p->nRef++;.
92990 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e }.static int win
929a0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
929b0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e _mutex *p){. in
929c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 t rc = SQLITE_BU
929d0 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d SY;. assert( p-
929e0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
929f0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 X_RECURSIVE || w
92a00 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 inMutexNotheld(p
92a10 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 ) );. /*. ** T
92a20 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
92a30 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 20 69 _try() routine i
92a40 73 20 76 65 72 79 20 72 61 72 65 6c 79 20 75 73 s very rarely us
92a50 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 0a ed, and when it.
92a60 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69 74 20 ** is used it
92a70 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74 is merely an opt
92a80 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69 imization. So i
92a90 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 20 74 t is OK for it t
92aa0 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61 o always. ** fa
92ab0 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 il. . **. **
92ac0 54 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 74 The TryEnterCrit
92ad0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e icalSection() in
92ae0 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 terface is only
92af0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e available on Win
92b00 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d NT.. ** And som
92b10 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c e windows compil
92b20 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 ers complain if
92b30 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65 20 69 you try to use i
92b40 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66 t without. ** f
92b50 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 irst doing some
92b60 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 70 72 #defines that pr
92b70 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66 72 6f event SQLite fro
92b80 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 m building on Wi
92b90 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 n98.. ** For th
92ba0 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 77 69 at reason, we wi
92bb0 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f 70 74 ll omit this opt
92bc0 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f imization for no
92bd0 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 69 63 w. See. ** tic
92be0 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a ket #2685.. */.
92bf0 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75 74 65 #if 0. if( mute
92c00 78 49 73 4e 54 28 29 20 26 26 20 54 72 79 45 6e xIsNT() && TryEn
92c10 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
92c20 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b on(&p->mutex) ){
92c30 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 . p->owner =
92c40 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
92c50 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 Id();. p->nRe
92c60 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 f++;. rc = SQ
92c70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c LITE_OK;. }.#el
92c80 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
92c90 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 METER(p);.#endif
92ca0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
92cb0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
92cc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
92cd0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
92ce0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
92cf0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
92d00 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
92d10 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
92d20 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
92d30 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
92d40 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
92d50 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
92d60 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
92d70 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
92d80 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
92d90 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
92da0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
92db0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 MutexLeave(sqlit
92dc0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
92dd0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
92de0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
92df0 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 ->owner==GetCurr
92e00 65 6e 74 54 68 72 65 61 64 49 64 28 29 20 29 3b entThreadId() );
92e10 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 . p->nRef--;.
92e20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
92e30 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c =0 || p->id==SQL
92e40 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
92e50 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65 43 72 IVE );. LeaveCr
92e60 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 iticalSection(&p
92e70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c ->mutex);.}..SQL
92e80 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
92e90 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
92ea0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
92eb0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 tMutex(void){.
92ec0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
92ed0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 utex_methods sMu
92ee0 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69 6e 4d tex = {. winM
92ef0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 77 69 utexInit,. wi
92f00 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 77 nMutexEnd,. w
92f10 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 inMutexAlloc,.
92f20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65 2c 0a winMutexFree,.
92f30 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 winMutexEnte
92f40 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 54 r,. winMutexT
92f50 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 ry,. winMutex
92f60 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 Leave,.#ifdef SQ
92f70 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 77 LITE_DEBUG. w
92f80 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 inMutexHeld,.
92f90 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
92fa0 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 .#else. 0,.
92fb0 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0.#endif. };.
92fc0 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 . return &sMute
92fd0 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 x;.}.#endif /* S
92fe0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 QLITE_MUTEX_W32
92ff0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
93000 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 *** End of mutex
93010 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _w32.c *********
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93040 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
93050 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d *** Begin file m
93060 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a alloc.c ********
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93090 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
930a0 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
930b0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
930c0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
930d0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
930e0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
930f0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
93100 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
93110 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
93120 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
93130 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
93140 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
93150 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
93160 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
93170 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
93180 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
93190 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
931a0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
931b0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
931c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
931d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
93210 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 Memory allocati
93220 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 on functions use
93230 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c d throughout sql
93240 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ite..**.** $Id:
93250 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36 32 20 malloc.c,v 1.62
93260 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 2009/05/03 20:23
93270 3a 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :54 drh Exp $.*/
93280 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
93290 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 tine runs when t
932a0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
932b0 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68 tor sees that th
932c0 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 e.** total memor
932d0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 y allocation is
932e0 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 about to exceed
932f0 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a the soft heap.**
93300 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 limit..*/.stati
93310 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c c void softHeapL
93320 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 imitEnforcer(.
93330 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a void *NotUsed, .
93340 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
93350 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 NotUsed2,. int
93360 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 allocSize.){. U
93370 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
93380 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
93390 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 d2);. sqlite3_r
933a0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c elease_memory(al
933b0 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a locSize);.}../*.
933c0 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 ** Set the soft
933d0 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 heap-size limit
933e0 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e for the library.
933f0 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 Passing a zero
93400 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or .** negative
93410 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 value indicates
93420 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c no limit..*/.SQL
93430 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
93440 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
93450 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 imit(int n){. s
93460 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c qlite3_uint64 iL
93470 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 imit;. int over
93480 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 age;. if( n<0 )
93490 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 {. iLimit = 0
934a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
934b0 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 Limit = n;. }.
934c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
934d0 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 ize();. if( iLi
934e0 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c mit>0 ){. sql
934f0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
93500 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 softHeapLimitEnf
93510 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 orcer, 0, iLimit
93520 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
93530 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 sqlite3MemoryAla
93540 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d rm(0, 0, 0);. }
93550 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28 69 6e . overage = (in
93560 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 t)(sqlite3_memor
93570 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36 34 29 y_used() - (i64)
93580 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72 61 67 n);. if( overag
93590 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 e>0 ){. sqlit
935a0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
935b0 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20 7d 0a y(overage);. }.
935c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
935d0 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70 20 74 to release up t
935e0 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e 6f 6e o n bytes of non
935f0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 -essential memor
93600 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 y currently.** h
93610 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e 20 41 eld by SQLite. A
93620 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e n example of non
93630 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 -essential memor
93640 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73 65 64 y is memory used
93650 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64 61 74 to.** cache dat
93660 61 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74 abase pages that
93670 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e 74 are not current
93680 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 ly in use..*/.SQ
93690 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
936a0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
936b0 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69 66 64 ory(int n){.#ifd
936c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
936d0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
936e0 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20 3d 20 NT. int nRet =
936f0 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65 74 20 0;.#if 0. nRet
93700 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 += sqlite3VdbeRe
93710 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a leaseMemory(n);.
93720 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20 2b 3d #endif. nRet +=
93730 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
93740 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 leaseMemory(n-nR
93750 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 et);. return nR
93760 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 et;.#else. UNUS
93770 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b ED_PARAMETER(n);
93780 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
93790 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f _OK;.#endif.}../
937a0 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 *.** State infor
937b0 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 mation local to
937c0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
937d0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
937e0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
937f0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d E_WSD struct Mem
93800 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 0Global {. /* N
93810 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 umber of free pa
93820 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 68 20 ges for scratch
93830 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d and page-cache m
93840 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e emory */. u32 n
93850 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 ScratchFree;. u
93860 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 32 nPageFree;..
93870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
93880 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f mutex; /
93890 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 * Mutex to seria
938a0 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a lize access */..
938b0 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c /*. ** The al
938c0 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 arm callback and
938d0 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 its arguments.
938e0 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 The mem0.mutex
938f0 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 lock will. ** b
93900 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 e held while the
93910 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e callback is run
93920 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 ning. Recursive
93930 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a calls into. **
93940 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 the memory subs
93950 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 ystem are allowe
93960 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 d, but no new ca
93970 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a llbacks will be.
93980 20 20 2a 2a 20 69 73 73 75 65 64 2e 20 20 54 68 ** issued. Th
93990 65 20 61 6c 61 72 6d 42 75 73 79 20 76 61 72 69 e alarmBusy vari
939a0 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 70 able is set to p
939b0 72 65 76 65 6e 74 20 72 65 63 75 72 73 69 76 65 revent recursive
939c0 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e . ** callbacks.
939d0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f . */. sqlite3_
939e0 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73 int64 alarmThres
939f0 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 hold;. void (*a
93a00 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f larmCallback)(vo
93a10 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 id*, sqlite3_int
93a20 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 64,int);. void
93a30 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20 69 6e 74 *alarmArg;. int
93a40 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a 20 20 2f alarmBusy;.. /
93a50 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20 *. ** Pointers
93a60 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 to the end of sq
93a70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
93a80 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a 20 g.pScratch and.
93a90 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 ** sqlite3Globa
93aa0 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74 6f lConfig.pPage to
93ab0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
93ac0 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a ry that records.
93ad0 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65 73 ** which pages
93ae0 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a are available..
93af0 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63 72 */. u32 *aScr
93b00 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 atchFree;. u32
93b10 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65 *aPageFree;.} me
93b20 6d 30 20 3d 20 7b 20 36 32 35 36 30 39 35 35 2c m0 = { 62560955,
93b30 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
93b40 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 0, 0, 0 };..#def
93b50 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 ine mem0 GLOBAL(
93b60 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 struct Mem0Globa
93b70 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 l, mem0)../*.**
93b80 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
93b90 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
93ba0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 subsystem..*/.S
93bb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
93bc0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 t sqlite3MallocI
93bd0 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 nit(void){. if(
93be0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
93bf0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d nfig.m.xMalloc==
93c00 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
93c10 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b MemSetDefault();
93c20 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d . }. memset(&m
93c30 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d em0, 0, sizeof(m
93c40 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c em0));. if( sql
93c50 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93c60 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 .bCoreMutex ){.
93c70 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 mem0.mutex =
93c80 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
93c90 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
93ca0 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
93cb0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
93cc0 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
93cd0 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f ch && sqlite3Glo
93ce0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
93cf0 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 tch>=100. &
93d00 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 & sqlite3GlobalC
93d10 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d onfig.nScratch>=
93d20 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0 ){. int i;.
93d30 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
93d40 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
93d50 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 h = ROUNDDOWN8(s
93d60 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
93d70 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b ig.szScratch-4);
93d80 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 . mem0.aScrat
93d90 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 chFree = (u32*)&
93da0 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 ((char*)sqlite3G
93db0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
93dc0 61 74 63 68 29 0a 20 20 20 20 20 20 20 20 20 20 atch).
93dd0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
93de0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
93df0 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c cratch*sqlite3Gl
93e00 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
93e10 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d tch];. for(i=
93e20 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 0; i<sqlite3Glob
93e30 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
93e40 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 h; i++){ mem0.aS
93e50 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 cratchFree[i] =
93e60 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 i; }. mem0.nS
93e70 63 72 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c cratchFree = sql
93e80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93e90 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c .nScratch;. }el
93ea0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 se{. sqlite3G
93eb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
93ec0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 atch = 0;. sq
93ed0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
93ee0 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b g.szScratch = 0;
93ef0 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
93f00 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
93f10 50 61 67 65 20 26 26 20 73 71 6c 69 74 65 33 47 Page && sqlite3G
93f20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 lobalConfig.szPa
93f30 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26 ge>=512. &&
93f40 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
93f50 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b nfig.nPage>=1 ){
93f60 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
93f70 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 int overhead;.
93f80 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 int sz = ROUND
93f90 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f DOWN8(sqlite3Glo
93fa0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
93fb0 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 );. int n = s
93fc0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
93fd0 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 ig.nPage;. ov
93fe0 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 erhead = (4*n +
93ff0 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 sz - 1)/sz;.
94000 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
94010 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 fig.nPage -= ove
94020 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e rhead;. mem0.
94030 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 aPageFree = (u32
94040 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 *)&((char*)sqlit
94050 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
94060 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 Page).
94070 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
94080 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
94090 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 age*sqlite3Globa
940a0 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a lConfig.nPage];.
940b0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
940c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
940d0 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 ig.nPage; i++){
940e0 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 mem0.aPageFree[i
940f0 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d ] = i; }. mem
94100 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 0.nPageFree = sq
94110 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
94120 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 g.nPage;. }else
94130 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
94140 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 balConfig.pPage
94150 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
94160 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
94170 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 age = 0;. }. r
94180 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
94190 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 balConfig.m.xIni
941a0 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 t(sqlite3GlobalC
941b0 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 onfig.m.pAppData
941c0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e );.}../*.** Dein
941d0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
941e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
941f0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c ubsystem..*/.SQL
94200 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
94210 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e sqlite3MallocEn
94220 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 d(void){. if( s
94230 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
94240 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 ig.m.xShutdown )
94250 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
94260 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 balConfig.m.xShu
94270 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f tdown(sqlite3Glo
94280 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 balConfig.m.pApp
94290 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d Data);. }. mem
942a0 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 set(&mem0, 0, si
942b0 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a zeof(mem0));.}..
942c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
942d0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
942e0 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 y currently chec
942f0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 ked out..*/.SQLI
94300 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 TE_API sqlite3_i
94310 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d nt64 sqlite3_mem
94320 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a ory_used(void){.
94330 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 int n, mx;. s
94340 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 qlite3_int64 res
94350 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 ;. sqlite3_stat
94360 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 us(SQLITE_STATUS
94370 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e _MEMORY_USED, &n
94380 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 , &mx, 0);. res
94390 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
943a0 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 4)n; /* Work ar
943b0 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c ound bug in Borl
943c0 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 and C. Ticket #3
943d0 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 216 */. return
943e0 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 res;.}../*.** Re
943f0 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d turn the maximum
94400 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
94410 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 y that has ever
94420 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 been.** checked
94430 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 out since either
94440 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
94450 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a f this process.*
94460 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d * or since the m
94470 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 ost recent reset
94480 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
94490 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
944a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 lite3_memory_hig
944b0 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 hwater(int reset
944c0 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 Flag){. int n,
944d0 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e mx;. sqlite3_in
944e0 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 t64 res;. sqlit
944f0 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 e3_status(SQLITE
94500 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
94510 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 SED, &n, &mx, re
94520 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 setFlag);. res
94530 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
94540 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 )mx; /* Work ar
94550 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c ound bug in Borl
94560 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 and C. Ticket #3
94570 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 216 */. return
94580 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 res;.}../*.** Ch
94590 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 ange the alarm c
945a0 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 allback.*/.SQLIT
945b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
945c0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
945d0 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 (. void(*xCallb
945e0 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c ack)(void *pArg,
945f0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 sqlite3_int64 u
94600 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f sed,int N),. vo
94610 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 id *pArg,. sqli
94620 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 te3_int64 iThres
94630 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 hold.){. sqlite
94640 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
94650 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d m0.mutex);. mem
94660 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 0.alarmCallback
94670 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d = xCallback;. m
94680 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 em0.alarmArg = p
94690 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 Arg;. mem0.alar
946a0 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 mThreshold = iTh
946b0 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 reshold;. sqlit
946c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
946d0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 em0.mutex);. re
946e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
946f0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
94700 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 E_OMIT_DEPRECATE
94710 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 D./*.** Deprecat
94720 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 ed external inte
94730 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c rface. Internal
94740 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 /core SQLite cod
94750 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c e.** should call
94760 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c sqlite3MemoryAl
94770 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 arm..*/.SQLITE_A
94780 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d PI int sqlite3_m
94790 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 emory_alarm(. v
947a0 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 oid(*xCallback)(
947b0 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 void *pArg, sqli
947c0 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 te3_int64 used,i
947d0 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 nt N),. void *p
947e0 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 Arg,. sqlite3_i
947f0 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a nt64 iThreshold.
94800 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
94810 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 te3MemoryAlarm(x
94820 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 Callback, pArg,
94830 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 iThreshold);.}.#
94840 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 endif../*.** Tri
94850 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a gger the alarm .
94860 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
94870 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
94880 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 m(int nByte){.
94890 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b void (*xCallback
948a0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f )(void*,sqlite3_
948b0 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 int64,int);. sq
948c0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 lite3_int64 nowU
948d0 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 sed;. void *pAr
948e0 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c g;. if( mem0.al
948f0 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c armCallback==0 |
94900 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 | mem0.alarmBusy
94910 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 ) return;. me
94920 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 m0.alarmBusy = 1
94930 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 ;. xCallback =
94940 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
94950 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 ck;. nowUsed =
94960 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c sqlite3StatusVal
94970 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ue(SQLITE_STATUS
94980 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 _MEMORY_USED);.
94990 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 pArg = mem0.ala
949a0 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 rmArg;. sqlite3
949b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
949c0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 0.mutex);. xCal
949d0 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 lback(pArg, nowU
949e0 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 sed, nByte);. s
949f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
94a00 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
94a10 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 mem0.alarmBusy
94a20 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 = 0;.}../*.** D
94a30 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 o a memory alloc
94a40 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69 ation with stati
94a50 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 stics and alarms
94a60 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a . Assume the.**
94a70 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 lock is already
94a80 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 held..*/.static
94a90 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 int mallocWithA
94aa0 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 larm(int n, void
94ab0 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 **pp){. int nF
94ac0 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a ull;. void *p;.
94ad0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
94ae0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
94af0 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 0.mutex) );. nF
94b00 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f ull = sqlite3Glo
94b10 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 balConfig.m.xRou
94b20 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 ndup(n);. sqlit
94b30 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 e3StatusSet(SQLI
94b40 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 TE_STATUS_MALLOC
94b50 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 _SIZE, n);. if(
94b60 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
94b70 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e ack!=0 ){. in
94b80 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 t nUsed = sqlite
94b90 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 3StatusValue(SQL
94ba0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
94bb0 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28 Y_USED);. if(
94bc0 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 nUsed+nFull >=
94bd0 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 mem0.alarmThresh
94be0 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c old ){. sql
94bf0 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 ite3MallocAlarm(
94c00 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 nFull);. }.
94c10 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 }. p = sqlite3G
94c20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d lobalConfig.m.xM
94c30 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 alloc(nFull);.
94c40 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 if( p==0 && mem0
94c50 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 .alarmCallback )
94c60 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c {. sqlite3Mal
94c70 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b locAlarm(nFull);
94c80 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
94c90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
94ca0 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 Malloc(nFull);.
94cb0 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 }. if( p ){.
94cc0 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 nFull = sqlite
94cd0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
94ce0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
94cf0 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
94d00 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 US_MEMORY_USED,
94d10 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 nFull);. }. *p
94d20 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 p = p;. return
94d30 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 nFull;.}../*.**
94d40 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e Allocate memory.
94d50 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 This routine i
94d60 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d s like sqlite3_m
94d70 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 alloc() except t
94d80 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 hat it.** assume
94d90 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 s the memory sub
94da0 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61 system has alrea
94db0 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 dy been initiali
94dc0 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 zed..*/.SQLITE_P
94dd0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
94de0 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e ite3Malloc(int n
94df0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 ){. void *p;.
94e00 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 4e 45 56 45 if( n<=0 || NEVE
94e10 52 28 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 29 R(n>=0x7fffff00)
94e20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 4e ){. /* The N
94e30 45 56 45 52 28 6e 3e 3d 30 78 37 66 66 66 66 66 EVER(n>=0x7fffff
94e40 30 30 29 20 74 65 72 6d 20 69 73 20 61 64 64 65 00) term is adde
94e50 64 20 6f 75 74 20 6f 66 20 70 61 72 61 6e 6f 69 d out of paranoi
94e60 61 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 6d a. We want to m
94e70 61 6b 65 0a 20 20 20 20 2a 2a 20 61 62 73 6f 6c ake. ** absol
94e80 75 74 65 6c 79 20 73 75 72 65 20 74 68 61 74 20 utely sure that
94e90 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 there is nothing
94ea0 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20 74 within SQLite t
94eb0 68 61 74 20 63 61 6e 20 63 61 75 73 65 20 61 0a hat can cause a.
94ec0 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c ** memory al
94ed0 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 6e 75 location of a nu
94ee0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 68 mber of bytes wh
94ef0 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 65 20 ich is near the
94f00 6d 61 78 69 6d 75 6d 20 73 69 67 6e 65 64 0a 20 maximum signed.
94f10 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 ** integer va
94f20 6c 75 65 20 61 6e 64 20 74 68 75 73 20 63 61 75 lue and thus cau
94f30 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 se an integer ov
94f40 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 erflow inside of
94f50 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 0a 20 the xMalloc().
94f60 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ** implementa
94f70 74 69 6f 6e 2e 20 20 54 68 65 20 6e 3e 3d 30 78 tion. The n>=0x
94f80 37 66 66 66 66 66 30 30 20 67 69 76 65 73 20 75 7fffff00 gives u
94f90 73 20 32 35 35 20 62 79 74 65 73 20 6f 66 20 68 s 255 bytes of h
94fa0 65 61 64 72 6f 6f 6d 2e 20 20 54 68 65 0a 20 20 eadroom. The.
94fb0 20 20 2a 2a 20 74 65 73 74 20 73 68 6f 75 6c 64 ** test should
94fc0 20 6e 65 76 65 72 20 62 65 20 74 72 75 65 20 62 never be true b
94fd0 65 63 61 75 73 65 20 53 51 4c 49 54 45 5f 4d 41 ecause SQLITE_MA
94fe0 58 5f 4c 45 4e 47 54 48 20 73 68 6f 75 6c 64 20 X_LENGTH should
94ff0 62 65 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20 6c be much. ** l
95000 65 73 73 20 74 68 61 6e 20 30 78 37 66 66 66 66 ess than 0x7ffff
95010 66 30 30 20 61 6e 64 20 69 74 20 73 68 6f 75 6c f00 and it shoul
95020 64 20 63 61 74 63 68 20 6c 61 72 67 65 20 6d 65 d catch large me
95030 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
95040 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 . ** before t
95050 68 65 79 20 72 65 61 63 68 20 74 68 69 73 20 70 hey reach this p
95060 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 70 20 3d oint. */. p =
95070 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
95080 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95090 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
950a0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
950b0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
950c0 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 ex);. mallocW
950d0 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b ithAlarm(n, &p);
950e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
950f0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
95100 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 tex);. }else{.
95110 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c p = sqlite3Gl
95120 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
95130 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 lloc(n);. }. r
95140 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
95150 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
95160 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
95170 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 ocation is for u
95180 73 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 se by the applic
95190 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 ation..** First
951a0 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 make sure the me
951b0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 mory subsystem i
951c0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 s initialized, t
951d0 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c hen do the.** al
951e0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c location..*/.SQL
951f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
95200 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 lite3_malloc(int
95210 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c n){.#ifndef SQL
95220 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
95230 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
95240 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 initialize() ) r
95250 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
95260 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
95270 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a Malloc(n);.}../*
95280 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 .** Each thread
95290 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 may only have a
952a0 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 single outstandi
952b0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 ng allocation fr
952c0 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 om.** xScratchMa
952d0 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 lloc(). We veri
952e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 fy this constrai
952f0 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 nt in the single
95300 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 -threaded.** cas
95310 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 e by setting scr
95320 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 atchAllocOut to
95330 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 1 when an alloca
95340 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 tion.** is outst
95350 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 anding clearing
95360 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f it when the allo
95370 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e cation is freed.
95380 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
95390 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 HREADSAFE==0 &&
953a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
953b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 .static int scra
953c0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b tchAllocOut = 0;
953d0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
953e0 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 Allocate memory
953f0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 that is to be us
95400 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 ed and released
95410 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 right away..** T
95420 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 his routine is s
95430 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 imilar to alloca
95440 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 () in that it is
95450 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a not intended.**
95460 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 for situations
95470 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 where the memory
95480 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c might be held l
95490 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a ong-term. This.
954a0 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e ** routine is in
954b0 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 tended to get me
954c0 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 mory to old larg
954d0 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 e transient data
954e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 .** structures t
954f0 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f hat would not no
95500 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 rmally fit on th
95510 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a e stack of an.**
95520 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 embedded proces
95530 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sor..*/.SQLITE_P
95540 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
95550 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f ite3ScratchMallo
95560 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 c(int n){. void
95570 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e *p;. assert( n
95580 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 >0 );..#if SQLIT
95590 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 E_THREADSAFE==0
955a0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 && !defined(NDEB
955b0 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 UG). /* Verify
955c0 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 that no more tha
955d0 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c n one scratch al
955e0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 location per thr
955f0 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 ead. ** is outs
95600 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 tanding at one t
95610 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f ime. (This is o
95620 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 nly checked in t
95630 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 he. ** single-t
95640 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e hreaded case sin
95650 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 ce checking in t
95660 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 he multi-threade
95670 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c d case. ** woul
95680 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 d be much more c
95690 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a omplicated.) */.
956a0 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 assert( scratc
956b0 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a hAllocOut==0 );.
956c0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 #endif.. if( sq
956d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
956e0 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b g.szScratch<n ){
956f0 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 . goto scratc
95700 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 h_overflow;. }e
95710 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 lse{ . sqlit
95720 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
95730 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
95740 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 if( mem0.nScratc
95750 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 hFree==0 ){.
95760 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
95770 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
95780 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 );. goto sc
95790 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a ratch_overflow;.
957a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
957b0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 int i;. i
957c0 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 = mem0.aScratchF
957d0 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 ree[--mem0.nScra
957e0 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 tchFree];.
957f0 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 i *= sqlite3Glob
95800 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
95810 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ch;. sqlite
95820 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
95830 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
95840 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 _USED, 1);.
95850 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
95860 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t(SQLITE_STATUS_
95870 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 SCRATCH_SIZE, n)
95880 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
95890 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
958a0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 .mutex);. p
958b0 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 = (void*)&((cha
958c0 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c r*)sqlite3Global
958d0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 Config.pScratch)
958e0 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 [i];. asser
958f0 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 t( (((u8*)p - (
95900 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 u8*)0) & 7)==0 )
95910 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 ;. }. }.#if
95920 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
95930 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
95940 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 (NDEBUG). scrat
95950 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d chAllocOut = p!=
95960 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 0;.#endif.. ret
95970 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f urn p;..scratch_
95980 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 overflow:. if(
95990 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
959a0 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
959b0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
959c0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
959d0 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ex);. sqlite3
959e0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
959f0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
95a00 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 SIZE, n);. n
95a10 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 = mallocWithAlar
95a20 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 m(n, &p);. if
95a30 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 ( p ) sqlite3Sta
95a40 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
95a50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
95a60 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 RFLOW, n);. s
95a70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
95a80 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
95a90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d }else{. p =
95aa0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
95ab0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e nfig.m.xMalloc(n
95ac0 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 );. }.#if SQLIT
95ad0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 E_THREADSAFE==0
95ae0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 && !defined(NDEB
95af0 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c UG). scratchAll
95b00 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 ocOut = p!=0;.#e
95b10 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b ndif. return p;
95b20 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 .}.SQLITE_PR
95b30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
95b40 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f e3ScratchFree(vo
95b50 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 id *p){. if( p
95b60 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 ){..#if SQLITE_T
95b70 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 HREADSAFE==0 &&
95b80 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
95b90 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 . /* Verify t
95ba0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e hat no more than
95bb0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c one scratch all
95bc0 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 ocation per thre
95bd0 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 ad. ** is out
95be0 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 standing at one
95bf0 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 time. (This is
95c00 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 only checked in
95c10 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c the. ** singl
95c20 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 e-threaded case
95c30 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 since checking i
95c40 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 n the multi-thre
95c50 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a aded case. **
95c60 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d would be much m
95c70 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e ore complicated.
95c80 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 ) */. assert(
95c90 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
95ca0 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 ==1 );. scrat
95cb0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a chAllocOut = 0;.
95cc0 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
95cd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95ce0 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a fig.pScratch==0.
95cf0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c || p<
95d00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95d10 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 fig.pScratch.
95d20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 || p>=(v
95d30 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 oid*)mem0.aScrat
95d40 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 chFree ){.
95d50 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
95d60 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
95d70 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
95d80 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d iSize = sqlite3M
95d90 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 allocSize(p);.
95da0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
95db0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
95dc0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 utex);. s
95dd0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
95de0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
95df0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 RATCH_OVERFLOW,
95e00 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 -iSize);.
95e10 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
95e20 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
95e30 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 MEMORY_USED, -iS
95e40 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ize);. sq
95e50 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
95e60 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
95e70 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
95e80 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
95e90 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c utex);. }el
95ea0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
95eb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95ec0 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 m.xFree(p);.
95ed0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
95ee0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
95ef0 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a i = (int)((u8*
95f00 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 )p - (u8*)sqlite
95f10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 3GlobalConfig.pS
95f20 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 cratch);. i
95f30 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 /= sqlite3Globa
95f40 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
95f50 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 h;. assert(
95f60 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 i>=0 && i<sqlit
95f70 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
95f80 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
95f90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
95fa0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
95fb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
95fc0 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
95fd0 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c e<(u32)sqlite3Gl
95fe0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
95ff0 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d tch );. mem
96000 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 0.aScratchFree[m
96010 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
96020 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 ++] = i;. s
96030 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
96040 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
96050 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b RATCH_USED, -1);
96060 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
96070 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
96080 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 mutex);. }.
96090 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 }.}../*.** TRUE
960a0 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 if p is a lookas
960b0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
960c0 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f ation from db.*/
960d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
960e0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 OMIT_LOOKASIDE.s
960f0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b tatic int isLook
96100 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 aside(sqlite3 *d
96110 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 b, void *p){. r
96120 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 eturn db && p &&
96130 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 p>=db->lookasid
96140 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 e.pStart && p<db
96150 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 ->lookaside.pEnd
96160 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ;.}.#else.#defin
96170 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c e isLookaside(A,
96180 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a B) 0.#endif../*.
96190 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 ** Return the si
961a0 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 ze of a memory a
961b0 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f llocation previo
961c0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 usly obtained fr
961d0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c om.** sqlite3Mal
961e0 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 loc() or sqlite3
961f0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 _malloc()..*/.SQ
96200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
96210 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
96220 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 ze(void *p){. r
96230 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
96240 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a balConfig.m.xSiz
96250 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 e(p);.}.SQLITE_P
96260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
96270 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 e3DbMallocSize(s
96280 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 qlite3 *db, void
96290 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
962a0 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
962b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
962c0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
962d0 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 p==0 ){. retu
962e0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rn 0;. }else if
962f0 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 ( isLookaside(db
96300 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , p) ){. retu
96310 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 rn db->lookaside
96320 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .sz;. }else{.
96330 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
96340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
96350 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a Size(p);. }.}..
96360 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 /*.** Free memor
96370 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 y previously obt
96380 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
96390 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 e3Malloc()..*/.S
963a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
963b0 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 qlite3_free(void
963c0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 *p){. if( p==0
963d0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
963e0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
963f0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b nfig.bMemstat ){
96400 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
96410 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
96420 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
96430 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
96440 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
96450 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 USED, -sqlite3Ma
96460 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 llocSize(p));.
96470 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
96480 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
96490 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
964a0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
964b0 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a utex);. }else{.
964c0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
964d0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
964e0 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
964f0 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 Free memory tha
96500 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 t might be assoc
96510 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 iated with a par
96520 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
96530 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a .** connection..
96540 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
96550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 E void sqlite3Db
96560 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 Free(sqlite3 *db
96570 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 , void *p){. as
96580 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 sert( db==0 || s
96590 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
965a0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
965b0 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 if( isLookasid
965c0 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 e(db, p) ){.
965d0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 LookasideSlot *p
965e0 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 Buf = (Lookaside
965f0 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 Slot*)p;. pBu
96600 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c f->pNext = db->l
96610 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a ookaside.pFree;.
96620 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
96630 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a e.pFree = pBuf;.
96640 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
96650 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 e.nOut--;. }els
96660 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 e{. sqlite3_f
96670 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ree(p);. }.}../
96680 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
96690 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
966a0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
966b0 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ation.*/.SQLITE_
966c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
966d0 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 lite3Realloc(voi
966e0 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 d *pOld, int nBy
966f0 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 tes){. int nOld
96700 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a , nNew;. void *
96710 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 pNew;. if( pOld
96720 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
96730 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 n sqlite3Malloc(
96740 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 nBytes);. }. i
96750 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 7c 7c 20 f( nBytes<=0 ||
96760 4e 45 56 45 52 28 6e 42 79 74 65 73 3e 3d 30 78 NEVER(nBytes>=0x
96770 37 66 66 66 66 66 30 30 29 20 29 7b 0a 20 20 20 7fffff00) ){.
96780 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 28 2e 2e /* The NEVER(..
96790 2e 29 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61 .) term is expla
967a0 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 ined in comments
967b0 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f on sqlite3Mallo
967c0 63 28 29 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 c() */. sqlit
967d0 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 e3_free(pOld);.
967e0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
967f0 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65 . nOld = sqlite
96800 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 3MallocSize(pOld
96810 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
96820 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
96830 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
96840 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
96850 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
96860 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
96870 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
96880 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 _MALLOC_SIZE, nB
96890 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65 77 20 ytes);. nNew
968a0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
968b0 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 onfig.m.xRoundup
968c0 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 (nBytes);. if
968d0 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a ( nOld==nNew ){.
968e0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c pNew = pOl
968f0 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
96900 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
96910 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 tatusValue(SQLIT
96920 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
96930 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 USED)+nNew-nOld
96940 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 >= .
96950 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 mem0.alarmThresh
96960 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 old ){. s
96970 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
96980 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 m(nNew-nOld);.
96990 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 }. pNew
969a0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
969b0 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f Config.m.xReallo
969c0 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 c(pOld, nNew);.
969d0 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 if( pNew==0
969e0 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 && mem0.alarmCa
969f0 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 llback ){.
96a00 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 sqlite3MallocA
96a10 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 larm(nBytes);.
96a20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c pNew = sql
96a30 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
96a40 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 .m.xRealloc(pOld
96a50 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 7d , nNew);. }
96a60 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 . if( pNew
96a70 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 20 ){. nNew
96a80 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
96a90 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 ize(pNew);.
96aa0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
96ab0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
96ac0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e S_MEMORY_USED, n
96ad0 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 New-nOld);.
96ae0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
96af0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
96b00 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
96b10 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 }else{. pNew
96b20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
96b30 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 onfig.m.xRealloc
96b40 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29 3b 0a (pOld, nBytes);.
96b50 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
96b60 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 w;.}../*.** The
96b70 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 public interface
96b80 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c to sqlite3Reall
96b90 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 oc. Make sure t
96ba0 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a hat the memory.*
96bb0 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 * subsystem is i
96bc0 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 nitialized prior
96bd0 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c to invoking sql
96be0 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 iteRealloc..*/.S
96bf0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
96c00 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
96c10 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 void *pOld, int
96c20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n){.#ifndef SQLI
96c30 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
96c40 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
96c50 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
96c60 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
96c70 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 return sqlite3R
96c80 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b ealloc(pOld, n);
96c90 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 .}.../*.** Alloc
96ca0 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d ate and zero mem
96cb0 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f ory..*/ .SQLITE_
96cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
96cd0 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
96ce0 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
96cf0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
96d00 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b c(n);. if( p ){
96d10 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 . memset(p, 0
96d20 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , n);. }. retu
96d30 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 rn p;.}../*.** A
96d40 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f llocate and zero
96d50 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 memory. If the
96d60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
96d70 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d s, make.** the m
96d80 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
96d90 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 in the connecti
96da0 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 on pointer..*/.S
96db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
96dc0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c id *sqlite3DbMal
96dd0 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 locZero(sqlite3
96de0 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 *db, int n){. v
96df0 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 oid *p = sqlite3
96e00 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
96e10 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 n);. if( p ){.
96e20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 memset(p, 0,
96e30 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
96e40 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c p;.}../*.** All
96e50 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d ocate and zero m
96e60 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 emory. If the a
96e70 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
96e80 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c make.** the mal
96e90 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 locFailed flag i
96ea0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
96eb0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
96ec0 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d If db!=0 and db-
96ed0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 >mallocFailed is
96ee0 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e true (indicatin
96ef0 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 g a prior malloc
96f00 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 .** failure on t
96f10 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
96f20 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 connection) the
96f30 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 n always return
96f40 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 0..** Hence for
96f50 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
96f60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
96f70 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 , once malloc st
96f80 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c arts.** failing,
96f90 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 it fails consis
96fa0 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c tently until mal
96fb0 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 locFailed is res
96fc0 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 et..** This is a
96fd0 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 n important assu
96fe0 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 mption. There a
96ff0 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 re many places i
97000 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 n the.** code th
97010 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b at do things lik
97020 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
97030 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 int *a = (
97040 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 int*)sqlite3DbMa
97050 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 llocRaw(db, 100)
97060 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 ;.** int
97070 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 *b = (int*)sqli
97080 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
97090 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 b, 200);.**
970a0 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 if( b ) a[10
970b0 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 ] = 9;.**.** In
970c0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
970d0 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c a subsequent mal
970e0 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f loc (ex: "b") wo
970f0 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 rked, it is assu
97100 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 med.** that all
97110 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 prior mallocs (e
97120 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 x: "a") worked t
97130 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 oo..*/.SQLITE_PR
97140 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
97150 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
97160 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
97170 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 n){. void *p;.
97180 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c assert( db==0 |
97190 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
971a0 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
971b0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
971c0 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 E_OMIT_LOOKASIDE
971d0 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20 . if( db ){.
971e0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
971f0 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62 pBuf;. if( db
97200 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
97210 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
97220 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
97230 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 db->lookaside.bE
97240 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d nabled && n<=db-
97250 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 >lookaside.sz.
97260 20 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20 && (pBuf
97270 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
97280 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 pFree)!=0 ){.
97290 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
972a0 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 .pFree = pBuf->p
972b0 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e Next;. db->
972c0 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b lookaside.nOut++
972d0 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e ;. if( db->
972e0 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 lookaside.nOut>d
972f0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f b->lookaside.mxO
97300 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 ut ){. db
97310 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 ->lookaside.mxOu
97320 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 t = db->lookasid
97330 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a e.nOut;. }.
97340 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f return (vo
97350 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a id*)pBuf;. }.
97360 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 }.#else. if(
97370 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db && db->malloc
97380 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
97390 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 turn 0;. }.#end
973a0 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 if. p = sqlite3
973b0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 Malloc(n);. if(
973c0 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20 !p && db ){.
973d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
973e0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 d = 1;. }. ret
973f0 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
97400 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b Resize the block
97410 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 of memory point
97420 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 ed to by p to n
97430 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a bytes. If the.**
97440 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 resize fails, s
97450 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 et the mallocFai
97460 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 led flag in the
97470 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 connection objec
97480 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
97490 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
974a0 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 e3DbRealloc(sqli
974b0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 te3 *db, void *p
974c0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
974d0 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 *pNew = 0;. as
974e0 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 sert( db!=0 );.
974f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
97500 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
97510 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
97520 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
97530 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
97540 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
97550 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c urn sqlite3DbMal
97560 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
97570 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c }. if( isL
97580 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 ookaside(db, p)
97590 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d ){. if( n<=
975a0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a db->lookaside.sz
975b0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
975c0 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn p;. }.
975d0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
975e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
975f0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , n);. if(
97600 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
97610 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 memcpy(pNew, p,
97620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a db->lookaside.sz
97630 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
97640 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
97650 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
97660 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d se{. pNew =
97670 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
97680 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 (p, n);. if
97690 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 ( !pNew ){.
976a0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
976b0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d led = 1;. }
976c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
976d0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
976e0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 ** Attempt to re
976f0 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 allocate p. If
97700 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e the reallocation
97710 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 fails, then fre
97720 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 e p.** and set t
97730 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
97740 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 flag in the data
97750 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
97760 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
97770 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
97780 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
97790 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
977a0 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 d *p, int n){.
977b0 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e void *pNew;. pN
977c0 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 ew = sqlite3DbRe
977d0 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b alloc(db, p, n);
977e0 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a . if( !pNew ){.
977f0 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
97800 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 e(db, p);. }.
97810 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
97820 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 /*.** Make a cop
97830 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e y of a string in
97840 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
97850 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
97860 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 oc(). These .**
97870 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 functions call s
97880 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 qlite3MallocRaw(
97890 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 ) directly inste
978a0 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c ad of sqliteMall
978b0 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 oc(). This.** is
978c0 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 because when me
978d0 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 mory debugging i
978e0 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 s turned on, the
978f0 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 se two functions
97900 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 are .** called
97910 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 via macros that
97920 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 record the curre
97930 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 nt file and line
97940 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a number in the.*
97950 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72 * ThreadData str
97960 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ucture..*/.SQLIT
97970 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
97980 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
97990 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
979a0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 st char *z){. c
979b0 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a har *zNew;. siz
979c0 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d e_t n;. if( z==
979d0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
979e0 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 0;. }. n = sql
979f0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 ite3Strlen30(z)
97a00 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 + 1;. assert( (
97a10 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e n&0x7fffffff)==n
97a20 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c );. zNew = sql
97a30 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
97a40 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 db, (int)n);. i
97a50 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d f( zNew ){. m
97a60 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e emcpy(zNew, z, n
97a70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
97a80 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 zNew;.}.SQLITE_P
97a90 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
97aa0 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 ite3DbStrNDup(sq
97ab0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
97ac0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 char *z, int n)
97ad0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a {. char *zNew;.
97ae0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 if( z==0 ){.
97af0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
97b00 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 assert( (n&0x7
97b10 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 fffffff)==n );.
97b20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
97b30 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
97b40 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 +1);. if( zNew
97b50 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e ){. memcpy(zN
97b60 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a ew, z, n);. z
97b70 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a New[n] = 0;. }.
97b80 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d return zNew;.}
97b90 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
97ba0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 string from the
97bb0 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e zFromat argumen
97bc0 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 t and the va_lis
97bd0 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a t that follows..
97be0 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 ** Store the str
97bf0 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 ing in memory ob
97c00 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
97c10 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d teMalloc() and m
97c20 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 ake *pz.** point
97c30 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e to that string.
97c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
97c50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
97c60 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a etString(char **
97c70 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c pz, sqlite3 *db,
97c80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
97c90 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
97ca0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
97cb0 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 *z;.. va_start
97cc0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
97cd0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 z = sqlite3VMPr
97ce0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
97cf0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
97d00 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ap);. sqlite3Db
97d10 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 Free(db, *pz);.
97d20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a *pz = z;.}.../*
97d30 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
97d40 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 n must be called
97d50 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
97d60 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e any API function
97d70 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 (i.e. .** retur
97d80 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 ning control to
97d90 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68 the user) that h
97da0 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 as called sqlite
97db0 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 3_malloc or.** s
97dc0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a qlite3_realloc..
97dd0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
97de0 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d ed value is norm
97df0 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 ally a copy of t
97e00 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
97e10 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 nt to this.** fu
97e20 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c nction. However,
97e30 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 if a malloc() f
97e40 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 ailure has occur
97e50 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 red since the pr
97e60 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 evious.** invoca
97e70 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 tion SQLITE_NOME
97e80 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e M is returned in
97e90 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 stead. .**.** If
97ea0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
97eb0 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 ent, db, is not
97ec0 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f NULL and a mallo
97ed0 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 c() error has oc
97ee0 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 curred,.** then
97ef0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 the connection e
97f00 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 rror-code (the v
97f10 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
97f20 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
97f30 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f ()).** is set to
97f40 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a SQLITE_NOMEM..*
97f50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
97f60 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 int sqlite3ApiE
97f70 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c xit(sqlite3* db,
97f80 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 int rc){. /* I
97f90 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 f the db handle
97fa0 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 is not NULL, the
97fb0 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 n we must hold t
97fc0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 he connection ha
97fd0 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 ndle. ** mutex
97fe0 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 here. Otherwise
97ff0 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f the read (and po
98000 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 ssible write) of
98010 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
98020 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 d . ** is unsaf
98030 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c e, as is the cal
98040 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f l to sqlite3Erro
98050 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 r().. */. asse
98060 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 rt( !db || sqlit
98070 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
98080 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
98090 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c ( db && (db->mal
980a0 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d locFailed || rc=
980b0 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f =SQLITE_IOERR_NO
980c0 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 MEM) ){. sqli
980d0 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c te3Error(db, SQL
980e0 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 ITE_NOMEM, 0);.
980f0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
98100 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 led = 0;. rc
98110 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
98120 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
98130 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d & (db ? db->errM
98140 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a ask : 0xff);.}..
98150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
98160 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 End of malloc.c
98170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
981a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
981b0 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 Begin file print
981c0 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
981d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
981e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
981f0 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 /*.** The "print
98200 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c f" code that fol
98210 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 lows dates from
98220 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 the 1980's. It
98230 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 is in.** the pub
98240 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 lic domain. The
98250 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e original commen
98260 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 ts are included
98270 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 here for.** comp
98280 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 leteness. They
98290 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d are very out-of-
982a0 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 date but might b
982b0 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 e useful as.** a
982c0 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 n historical ref
982d0 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 erence. Most of
982e0 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e the "enhancemen
982f0 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 ts" have been ba
98300 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 cked.** out so t
98310 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e hat the function
98320 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 ality is now the
98330 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 same as standar
98340 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a d printf()..**.*
98350 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c * $Id: printf.c,
98360 76 20 31 2e 31 30 33 20 32 30 30 39 2f 30 35 2f v 1.103 2009/05/
98370 30 34 20 32 30 3a 32 30 3a 31 36 20 64 72 68 20 04 20:20:16 drh
98380 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a Exp $.**.*******
98390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983d0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ***.**.** The fo
983e0 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 llowing modules
983f0 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 is an enhanced r
98400 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 eplacement for t
98410 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 he "printf" subr
98420 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 outines.** found
98430 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 in the standard
98440 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 C library. The
98450 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e following enhan
98460 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 cements are.** s
98470 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 upported:.**.**
98480 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e + Addition
98490 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 al functions. T
984a0 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 he standard set
984b0 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 of "printf" func
984c0 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tions.**
984d0 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 includes printf
984e0 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e , fprintf, sprin
984f0 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 tf, vprintf, vfp
98500 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 rintf, and.**
98510 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 vsprintf.
98520 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 This module add
98530 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
98540 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
98550 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 * snprintf --
98560 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e Works like sprin
98570 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 tf, but has an e
98580 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a xtra argument.**
98590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
985a0 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 which
985b0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
985c0 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 he buffer writte
985d0 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 n to..**.**
985e0 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 * mprintf
985f0 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 -- Similar to
98600 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 sprintf. Writes
98610 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 output to memor
98620 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 y.**
98630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 ob
98640 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
98650 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 oc..**.**
98660 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d * xprintf -
98670 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 - Calls a funct
98680 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f ion to dispose o
98690 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 f output..**.**
986a0 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 * npr
986b0 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 intf -- No outp
986c0 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 ut, but returns
986d0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
986e0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 aracters.**
986f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98700 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 that would
98710 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 have been output
98720 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a by printf..**.*
98730 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 * * A
98740 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a v- version (ex:
98750 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 vsnprintf) of e
98760 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 very function is
98770 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 also.**
98780 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a supplied..
98790 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 **.** + A
987a0 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 few extensions t
987b0 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 o the formatting
987c0 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 notation are su
987d0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 pported:.**.**
987e0 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 * The
987f0 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 "=" flag (simila
98800 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 r to "-") causes
98810 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 the output to b
98820 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
98830 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e be centered in
98840 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
98850 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a ly sized field..
98860 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
98870 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 * The %b field
98880 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 outputs an integ
98890 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 er in binary not
988a0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ation..**.**
988b0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 * The %c
988c0 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 field now accep
988d0 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 ts a precision.
988e0 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f The character o
988f0 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 utput.**
98900 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 is repeate
98910 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 d by the number
98920 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 of times the pre
98930 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 cision specifies
98940 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
98950 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c * The %' fiel
98960 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c d works like %c,
98970 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 but takes as it
98980 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a s character the.
98990 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
989a0 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f next character o
989b0 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 f the format str
989c0 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 ing, instead of
989d0 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 the next.**
989e0 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e argumen
989f0 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c t. For example,
98a00 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d printf("%.78'-
98a10 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 ") prints 78 mi
98a20 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nus.**
98a30 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 signs, the s
98a40 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 ame as printf("
98a50 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a %.78c",'-')..**.
98a60 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 ** + When
98a70 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 compiled using G
98a80 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 CC on a SPARC, t
98a90 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 his version of p
98aa0 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 rintf is.**
98ab0 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 faster than
98ac0 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e the library prin
98ad0 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e tf for SUN OS 4.
98ae0 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 1..**.** +
98af0 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 All functions a
98b00 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 re fully reentra
98b10 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a nt..**.*/../*.**
98b20 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 Conversion type
98b30 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 s fall into vari
98b40 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 ous categories a
98b50 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 s defined by the
98b60 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e .** following en
98b70 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 umeration..*/.#d
98b80 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 efine etRADIX
98b90 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 1 /* Integer
98ba0 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c types. %d, %x,
98bb0 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 %o, and so fort
98bc0 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 h */.#define etF
98bd0 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 LOAT 2 /*
98be0 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 Floating point.
98bf0 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %f */.#define e
98c00 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f tEXP 3 /
98c10 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 * Exponentional
98c20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 notation. %e and
98c30 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %E */.#define e
98c40 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f tGENERIC 4 /
98c50 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 * Floating or ex
98c60 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e ponential, depen
98c70 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 ding on exponent
98c80 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 . %g */.#define
98c90 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 etSIZE 5
98ca0 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 /* Return number
98cb0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 of characters p
98cc0 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e rocessed so far.
98cd0 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %n */.#define e
98ce0 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f tSTRING 6 /
98cf0 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f * Strings. %s */
98d00 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 .#define etDYNST
98d10 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 RING 7 /* Dyna
98d20 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
98d30 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f d strings. %z */
98d40 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 .#define etPERCE
98d50 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 NT 8 /* Perc
98d60 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a ent symbol. %% *
98d70 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 /.#define etCHAR
98d80 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 X 9 /* Cha
98d90 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f racters. %c */./
98da0 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 * The rest are e
98db0 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e xtensions, not n
98dc0 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e ormally found in
98dd0 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 printf() */.#de
98de0 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 fine etSQLESCAPE
98df0 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 10 /* Strings
98e00 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 with '\'' double
98e10 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e d. %q */.#defin
98e20 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 e etSQLESCAPE2 1
98e30 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 1 /* Strings wit
98e40 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 h '\'' doubled a
98e50 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 nd enclosed in '
98e60 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ',.
98e70 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
98e80 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 L pointers repla
98e90 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e ced by SQL NULL.
98ea0 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 %Q */.#define
98eb0 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 etTOKEN 12
98ec0 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 /* a pointer to
98ed0 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 a Token structur
98ee0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 e */.#define etS
98ef0 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 RCLIST 13 /*
98f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 a pointer to a S
98f10 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e rcList */.#defin
98f20 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 e etPOINTER 1
98f30 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 4 /* The %p conv
98f40 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e ersion */.#defin
98f50 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 e etSQLESCAPE3 1
98f60 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 5 /* %w -> Strin
98f70 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 gs with '\"' dou
98f80 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 bled */.#define
98f90 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 etORDINAL 16
98fa0 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e /* %r -> 1st, 2n
98fb0 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 d, 3rd, 4th, etc
98fc0 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 . English only
98fd0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e */..#define etIN
98fe0 56 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 VALID 0 /* A
98ff0 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 ny unrecognized
99000 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 conversion type
99010 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 */.../*.** An "e
99020 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 tByte" is an 8-b
99030 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 it unsigned valu
99040 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e e..*/.typedef un
99050 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 signed char etBy
99060 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 te;../*.** Each
99070 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 builtin conversi
99080 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 on character (ex
99090 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 : the 'd' in "%d
990a0 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a ") is described.
990b0 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ** by an instanc
990c0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
990d0 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a ng structure.*/.
990e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 typedef struct e
990f0 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e t_info { /* In
99100 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
99110 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c each format fiel
99120 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 d */. char fmtt
99130 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ype;
99140 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 /* The format fi
99150 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 eld code letter
99160 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 */. etByte base
99170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
99180 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 The base for ra
99190 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a dix conversion *
991a0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 /. etByte flags
991b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
991c0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 One or more of F
991d0 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 LAG_ constants b
991e0 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 elow */. etByte
991f0 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 type;
99200 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e /* Conversion
99210 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 paradigm */. e
99220 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 tByte charset;
99230 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
99240 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d t into aDigits[]
99250 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 of the digits s
99260 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 tring */. etByt
99270 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 e prefix;
99280 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
99290 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 to aPrefix[] of
992a0 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e the prefix strin
992b0 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a g */.} et_info;.
992c0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
992d0 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 alues for et_inf
992e0 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 o.flags.*/.#defi
992f0 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 ne FLAG_SIGNED
99300 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 1 /* True if
99310 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f the value to co
99320 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 nvert is signed
99330 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f */.#define FLAG_
99340 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a INTERN 2 /*
99350 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 True if for int
99360 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a ernal use only *
99370 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 /.#define FLAG_S
99380 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 TRING 4 /*
99390 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 Allow infinity p
993a0 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a recision */.../*
993b0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
993c0 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 g table is searc
993d0 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f hed linearly, so
993e0 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 it is good to p
993f0 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 ut the.** most f
99400 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 requently used c
99410 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 onversion types
99420 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 first..*/.static
99430 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 const char aDig
99440 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 its[] = "0123456
99450 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 789ABCDEF0123456
99460 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 789abcdef";.stat
99470 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 ic const char aP
99480 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 refix[] = "-x0\0
99490 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 00X0";.static co
994a0 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 nst et_info fmti
994b0 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 nfo[] = {. { '
994c0 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 d', 10, 1, etRAD
994d0 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d IX, 0, 0 }
994e0 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 ,. { 's', 0,
994f0 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 4, etSTRING,
99500 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
99510 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e g', 0, 1, etGEN
99520 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d ERIC, 30, 0 }
99530 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 ,. { 'z', 0,
99540 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 4, etDYNSTRING,
99550 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
99560 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c q', 0, 4, etSQL
99570 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d ESCAPE, 0, 0 }
99580 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 ,. { 'Q', 0,
99590 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 4, etSQLESCAPE2,
995a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
995b0 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c w', 0, 4, etSQL
995c0 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d ESCAPE3, 0, 0 }
995d0 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 ,. { 'c', 0,
995e0 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 0, etCHARX,
995f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
99600 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 o', 8, 0, etRAD
99610 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d IX, 0, 2 }
99620 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 ,. { 'u', 10,
99630 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
99640 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
99650 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 x', 16, 0, etRAD
99660 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d IX, 16, 1 }
99670 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 ,. { 'X', 16,
99680 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
99690 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 0, 4 },.#ifnde
996a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
996b0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b OATING_POINT. {
996c0 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 'f', 0, 1, et
996d0 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 FLOAT, 0,
996e0 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 0 },. { 'e',
996f0 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 0, 1, etEXP,
99700 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 30, 0 },. {
99710 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 'E', 0, 1, et
99720 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 EXP, 14,
99730 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 0 },. { 'G',
99740 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 0, 1, etGENERIC,
99750 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 14, 0 },.#en
99760 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 dif. { 'i', 10
99770 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 , 1, etRADIX,
99780 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
99790 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 'n', 0, 0, etS
997a0 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 IZE, 0, 0
997b0 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 },. { '%', 0
997c0 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 , 0, etPERCENT,
997d0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
997e0 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 'p', 16, 0, etP
997f0 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 OINTER, 0, 1
99800 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 },../* All the
99810 72 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c rest have the FL
99820 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 AG_INTERN bit se
99830 74 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 t and are thus f
99840 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
99850 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 se only */. {
99860 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 'T', 0, 2, etTO
99870 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 KEN, 0, 0
99880 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c },. { 'S', 0,
99890 20 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 2, etSRCLIST,
998a0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
998b0 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 'r', 10, 3, etOR
998c0 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 DINAL, 0, 0
998d0 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 },.};../*.** If
998e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
998f0 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 TING_POINT is de
99900 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 fined, then none
99910 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 of the floating
99920 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 point.** conver
99930 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e sions will work.
99940 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
99950 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
99960 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 _POINT./*.** "*v
99970 61 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 al" is a double
99980 73 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d such that 0.1 <=
99990 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 *val < 10.0.**
999a0 52 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 Return the ascii
999b0 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 code for the le
999c0 61 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a ading digit of *
999d0 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c val, then.** mul
999e0 74 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 tiply "*val" by
999f0 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 10.0 to renormal
99a00 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 ize..**.** Examp
99a10 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 le:.** input
99a20 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 : *val = 3.1
99a30 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 4159.** outp
99a40 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e ut: *val = 1.
99a50 34 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 4159 function
99a60 20 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a return = '3'.**
99a70 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 .** The counter
99a80 2a 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e *cnt is incremen
99a90 74 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 ted each time.
99aa0 41 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 After counter ex
99ab0 63 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 ceeds.** 16 (the
99ac0 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 number of signi
99ad0 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e ficant digits in
99ae0 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 a 64-bit float)
99af0 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 '0' is.** alway
99b00 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 s returned..*/.s
99b10 74 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 tatic char et_ge
99b20 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c tdigit(LONGDOUBL
99b30 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 E_TYPE *val, int
99b40 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 *cnt){. int di
99b50 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c git;. LONGDOUBL
99b60 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 E_TYPE d;. if(
99b70 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 (*cnt)++ >= 16 )
99b80 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 return '0';. d
99b90 69 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c igit = (int)*val
99ba0 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 ;. d = digit;.
99bb0 20 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 digit += '0';.
99bc0 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 *val = (*val -
99bd0 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 d)*10.0;. retur
99be0 6e 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d n (char)digit;.}
99bf0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
99c00 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
99c10 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 POINT */../*.**
99c20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 Append N space c
99c30 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 haracters to the
99c40 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 given string bu
99c50 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ffer..*/.static
99c60 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 void appendSpace
99c70 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 (StrAccum *pAccu
99c80 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 m, int N){. sta
99c90 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a tic const char z
99ca0 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 Spaces[] = "
99cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99cc0 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 ";. wh
99cd0 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a ile( N>=(int)siz
99ce0 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 eof(zSpaces)-1 )
99cf0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 {. sqlite3Str
99d00 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
99d10 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a um, zSpaces, siz
99d20 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b eof(zSpaces)-1);
99d30 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 . N -= sizeof
99d40 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d (zSpaces)-1;. }
99d50 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 . if( N>0 ){.
99d60 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
99d70 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
99d80 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d zSpaces, N);. }
99d90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 .}../*.** On mac
99da0 68 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 hines with a sma
99db0 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 ll stack size, y
99dc0 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 ou can redefine
99dd0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 the.** SQLITE_PR
99de0 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 INT_BUF_SIZE to
99df0 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 be less than 350
99e00 2e 20 20 42 75 74 20 62 65 77 61 72 65 20 2d 20 . But beware -
99e10 66 6f 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72 20 76 for.** smaller v
99e20 61 6c 75 65 73 20 73 6f 6d 65 20 25 66 20 63 6f alues some %f co
99e30 6e 76 65 72 73 69 6f 6e 73 20 6d 61 79 20 67 6f nversions may go
99e40 20 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e 69 74 into an infinit
99e50 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 e loop..*/.#ifnd
99e60 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f ef SQLITE_PRINT_
99e70 42 55 46 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e BUF_SIZE.# defin
99e80 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 e SQLITE_PRINT_B
99e90 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 65 6e 64 UF_SIZE 350.#end
99ea0 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 if.#define etBUF
99eb0 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e SIZE SQLITE_PRIN
99ec0 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 T_BUF_SIZE /* S
99ed0 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ize of the outpu
99ee0 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a t buffer */../*.
99ef0 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 ** The root prog
99f00 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 ram. All variat
99f10 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 ions call this c
99f20 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 ore..**.** INPUT
99f30 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 S:.** func T
99f40 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 his is a pointer
99f50 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 to a function t
99f60 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 aking three argu
99f70 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 ments.**
99f80 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 1. A pointer
99f90 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 to anything. S
99fa0 61 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 ame as the "arg"
99fb0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 parameter..**
99fc0 20 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 2. A p
99fd0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 ointer to the li
99fe0 73 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 st of characters
99ff0 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a to be output.**
9a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
9a010 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 Note, this list
9a020 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d is NOT null term
9a030 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 inated.).**
9a040 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 3. An int
9a050 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 eger number of c
9a060 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 haracters to be
9a070 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 output..**
9a080 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 (Note:
9a090 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 This number migh
9a0a0 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a t be zero.).**.*
9a0b0 2a 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 * arg This
9a0c0 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 is the pointer t
9a0d0 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 o anything which
9a0e0 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 will be passed
9a0f0 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 as the.**
9a100 20 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e first argumen
9a110 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 t to "func". Us
9a120 65 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 e it for whateve
9a130 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a r you like..**.*
9a140 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 * fmt This
9a150 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 is the format st
9a160 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 ring, as in the
9a170 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a usual print..**.
9a180 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 ** ap This
9a190 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
9a1a0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d a list of argum
9a1b0 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 ents. Same as i
9a1c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 n.** vf
9a1d0 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 print..**.** OUT
9a1e0 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 PUTS:.**
9a1f0 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
9a200 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 ue is the total
9a210 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
9a220 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 ters sent to.**
9a230 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e the fun
9a240 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 ction "func". R
9a250 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 eturns -1 on a e
9a260 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 rror..**.** Note
9a270 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 that the order
9a280 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 in which automat
9a290 69 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 ic variables are
9a2a0 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a declared below.
9a2b0 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 ** seems to make
9a2c0 20 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 a big differenc
9a2d0 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 e in determining
9a2e0 20 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 how fast this b
9a2f0 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e east.** will run
9a300 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9a310 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9a320 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 VXPrintf(. StrA
9a330 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 ccum *pAccum,
9a340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9a350 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 * Accumulate res
9a360 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 ults here */. i
9a370 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 nt useExtended,
9a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a390 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e /* Allow exten
9a3a0 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e ded %-conversion
9a3b0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
9a3c0 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 r *fmt,
9a3d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
9a3e0 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 mat string */.
9a3f0 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 va_list ap
9a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a410 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 /* arguments
9a420 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 */.){. int c;
9a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a440 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 /* Next chara
9a450 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d cter in the form
9a460 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 at string */. c
9a470 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 har *bufpt;
9a480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
9a490 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 nter to the conv
9a4a0 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f ersion buffer */
9a4b0 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e . int precision
9a4c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
9a4d0 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 Precision of th
9a4e0 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 e current field
9a4f0 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b */. int length;
9a500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a510 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 /* Length of the
9a520 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 field */. int
9a530 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 idx;
9a540 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 /* A gene
9a550 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 ral purpose loop
9a560 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
9a570 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 t width;
9a580 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 /* Widt
9a590 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 h of the current
9a5a0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 field */. etBy
9a5b0 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 te flag_leftjust
9a5c0 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 ify; /* True i
9a5d0 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 f "-" flag is pr
9a5e0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
9a5f0 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b e flag_plussign;
9a600 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
9a610 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 "+" flag is pre
9a620 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
9a630 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b flag_blanksign;
9a640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
9a650 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 " " flag is pres
9a660 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
9a670 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
9a680 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 rm; /* True if "
9a690 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 #" flag is prese
9a6a0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
9a6b0 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 lag_altform2;
9a6c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 /* True if "!
9a6d0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
9a6e0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
9a6f0 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 ag_zeropad;
9a700 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 /* True if fie
9a710 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e ld width constan
9a720 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 t starts with ze
9a730 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 ro */. etByte f
9a740 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 lag_long;
9a750 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c /* True if "l
9a760 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
9a770 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
9a780 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 ag_longlong;
9a790 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
9a7a0 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 "ll" flag is pr
9a7b0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
9a7c0 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 e done;
9a7d0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 /* Loop te
9a7e0 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a rmination flag *
9a7f0 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 /. sqlite_uint6
9a800 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 4 longvalue; /
9a810 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 * Value for inte
9a820 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c ger types */. L
9a830 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 ONGDOUBLE_TYPE r
9a840 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c ealvalue; /* Val
9a850 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 ue for real type
9a860 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f s */. const et_
9a870 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 info *infop;
9a880 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
9a890 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
9a8a0 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a info structure *
9a8b0 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 /. char buf[etB
9a8c0 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f UFSIZE]; /
9a8d0 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 * Conversion buf
9a8e0 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 fer */. char pr
9a8f0 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 efix;
9a900 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 /* Prefix ch
9a910 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 aracter. "+" or
9a920 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 "-" or " " or '
9a930 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 \0'. */. etByte
9a940 20 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 xtype = 0;
9a950 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 /* Conversi
9a960 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 on paradigm */.
9a970 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 char *zExtra;
9a980 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
9a990 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 xtra memory used
9a9a0 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 for etTCLESCAPE
9a9b0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a conversions */.
9a9c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9a9d0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
9a9e0 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 NT. int exp, e
9a9f0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2;
9aa00 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 /* exponent of r
9aa10 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 eal numbers */.
9aa20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b double rounder;
9aa30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
9aa40 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 sed for rounding
9aa50 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
9aa60 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 values */. etBy
9aa70 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 te flag_dp;
9aa80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
9aa90 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 f decimal point
9aaa0 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 should be shown
9aab0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
9aac0 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 _rtz;
9aad0 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c /* True if trail
9aae0 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 ing zeros should
9aaf0 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 be removed */.
9ab00 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 etByte flag_exp
9ab10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
9ab20 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 rue to force dis
9ab30 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f play of the expo
9ab40 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 nent */. int ns
9ab50 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
9ab60 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
9ab70 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 f significant di
9ab80 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f gits returned */
9ab90 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 .#endif.. lengt
9aba0 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d h = 0;. bufpt =
9abb0 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 0;. for(; (c=(
9abc0 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 *fmt))!=0; ++fmt
9abd0 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 ){. if( c!='%
9abe0 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 ' ){. int a
9abf0 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 mt;. bufpt
9ac00 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 = (char *)fmt;.
9ac10 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 amt = 1;.
9ac20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a while( (c=(*
9ac30 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 ++fmt))!='%' &&
9ac40 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 c!=0 ) amt++;.
9ac50 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
9ac60 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
9ac70 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 , bufpt, amt);.
9ac80 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 if( c==0 )
9ac90 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
9aca0 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 if( (c=(*++fmt)
9acb0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )==0 ){. sq
9acc0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
9acd0 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c end(pAccum, "%",
9ace0 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 1);. break
9acf0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 ;. }. /* F
9ad00 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 ind out what fla
9ad10 67 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a gs are present *
9ad20 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a /. flag_leftj
9ad30 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c ustify = flag_pl
9ad40 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c ussign = flag_bl
9ad50 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 anksign = .
9ad60 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
9ad70 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 rm = flag_altfor
9ad80 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 m2 = flag_zeropa
9ad90 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 d = 0;. done
9ada0 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 = 0;. do{.
9adb0 20 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a switch( c ){.
9adc0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 case '-'
9add0 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 : flag_leftjus
9ade0 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 tify = 1; br
9adf0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
9ae00 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c e '+': flag_pl
9ae10 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 ussign = 1;
9ae20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
9ae30 20 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c case ' ': fl
9ae40 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 ag_blanksign = 1
9ae50 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
9ae60 20 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a case '#':
9ae70 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 flag_alternat
9ae80 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 eform = 1; bre
9ae90 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
9aea0 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 '!': flag_alt
9aeb0 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 form2 = 1;
9aec0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
9aed0 20 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 case '0': fla
9aee0 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 g_zeropad = 1;
9aef0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
9af00 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 default:
9af10 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 done = 1;
9af20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
9af30 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
9af40 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 while( !done &&
9af50 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 (c=(*++fmt))!=0
9af60 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 );. /* Get th
9af70 65 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f e field width */
9af80 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a . width = 0;.
9af90 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 if( c=='*' )
9afa0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 {. width =
9afb0 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a va_arg(ap,int);.
9afc0 20 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c if( width<
9afd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 0 ){. fla
9afe0 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 g_leftjustify =
9aff0 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 1;. width
9b000 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 = -width;.
9b010 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b }. c = *++
9b020 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a fmt;. }else{.
9b030 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d while( c>=
9b040 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b '0' && c<='9' ){
9b050 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d . width =
9b060 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 width*10 + c -
9b070 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d '0';. c =
9b080 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d *++fmt;. }
9b090 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 . }. if( w
9b0a0 69 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 idth > etBUFSIZE
9b0b0 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 -10 ){. wid
9b0c0 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 th = etBUFSIZE-1
9b0d0 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 0;. }. /*
9b0e0 47 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f Get the precisio
9b0f0 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d n */. if( c==
9b100 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 '.' ){. pre
9b110 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 cision = 0;.
9b120 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
9b130 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 if( c=='*' )
9b140 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 {. precis
9b150 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c ion = va_arg(ap,
9b160 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 int);. if
9b170 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 ( precision<0 )
9b180 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 precision = -pre
9b190 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 cision;.
9b1a0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
9b1b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9b1c0 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 while( c>='0' &
9b1d0 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 & c<='9' ){.
9b1e0 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
9b1f0 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b = precision*10 +
9b200 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 c - '0';.
9b210 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
9b220 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9b230 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
9b240 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d precision = -
9b250 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 1;. }. /*
9b260 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 Get the conversi
9b270 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 on type modifier
9b280 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 */. if( c=='
9b290 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 l' ){. flag
9b2a0 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 _long = 1;.
9b2b0 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
9b2c0 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b if( c=='l' ){
9b2d0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f . flag_lo
9b2e0 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 nglong = 1;.
9b2f0 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
9b300 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
9b310 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f flag_longlo
9b320 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ng = 0;. }.
9b330 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9b340 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 flag_long = fla
9b350 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a g_longlong = 0;.
9b360 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 }. /* Fet
9b370 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 ch the info entr
9b380 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 y for the field
9b390 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 */. infop = &
9b3a0 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 fmtinfo[0];.
9b3b0 78 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 xtype = etINVALI
9b3c0 44 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 D;. for(idx=0
9b3d0 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 ; idx<ArraySize(
9b3e0 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 fmtinfo); idx++)
9b3f0 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 {. if( c==f
9b400 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 mtinfo[idx].fmtt
9b410 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ype ){. i
9b420 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b nfop = &fmtinfo[
9b430 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 idx];. if
9b440 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c ( useExtended ||
9b450 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 (infop->flags &
9b460 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 FLAG_INTERN)==0
9b470 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 ){. xt
9b480 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 ype = infop->typ
9b490 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 e;. }else
9b4a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
9b4b0 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rn;. }.
9b4c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
9b4d0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a }. }. z
9b4e0 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 Extra = 0;...
9b4f0 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 /* Limit the pr
9b500 65 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 ecision to preve
9b510 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 nt overflowing b
9b520 75 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 uf[] during conv
9b530 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 ersion */. if
9b540 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 ( precision>etBU
9b550 46 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 FSIZE-40 && (inf
9b560 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 op->flags & FLAG
9b570 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 _STRING)==0 ){.
9b580 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
9b590 20 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 etBUFSIZE-40;.
9b5a0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
9b5b0 20 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e ** At this poin
9b5c0 74 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 t, variables are
9b5d0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 initialized as
9b5e0 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a follows:. **.
9b5f0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c ** flag_al
9b600 74 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 ternateform
9b610 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 TRUE if a '
9b620 23 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 #' is present..
9b630 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 ** flag_alt
9b640 66 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 form2
9b650 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 TRUE if a '!
9b660 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
9b670 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 ** flag_plus
9b680 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 sign
9b690 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 TRUE if a '+'
9b6a0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
9b6b0 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a ** flag_leftj
9b6c0 75 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 ustify
9b6d0 20 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 TRUE if a '-'
9b6e0 69 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 is present or if
9b6f0 20 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 the. **
9b700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b710 20 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 field
9b720 77 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 width was negati
9b730 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 ve.. ** fla
9b740 67 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 g_zeropad
9b750 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 TRUE if
9b760 20 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e the width began
9b770 20 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 with 0.. **
9b780 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 flag_long
9b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
9b7a0 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 UE if the letter
9b7b0 20 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 'l' (ell) prefi
9b7c0 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 xed. **
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b7e0 20 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e the con
9b7f0 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 version characte
9b800 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 r.. ** flag
9b810 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 _longlong
9b820 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
9b830 74 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 the letter 'll'
9b840 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 (ell ell) prefix
9b850 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 ed. **
9b860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b870 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 the conv
9b880 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 ersion character
9b890 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
9b8a0 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 blanksign
9b8b0 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 TRUE if a
9b8c0 20 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e ' ' is present.
9b8d0 0a 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 . ** width
9b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b8f0 20 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 The specif
9b900 69 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e ied field width.
9b910 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a This is. **
9b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
9b940 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 lways non-negati
9b950 76 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 ve. Zero is the
9b960 20 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a default.. **
9b970 20 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 precision
9b980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
9b990 68 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 he specified pre
9b9a0 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 cision. The def
9b9b0 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 ault. **
9b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b9d0 20 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e is -1.
9b9e0 0a 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 . ** xtype
9b9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ba00 20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 The class
9ba10 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f of the conversio
9ba20 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f n.. ** info
9ba30 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
9ba40 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 Pointer
9ba50 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 to the appropria
9ba60 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a te info struct..
9ba70 20 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 */. switc
9ba80 68 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 h( xtype ){.
9ba90 20 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 case etPOINTER
9baa0 3a 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c :. flag_l
9bab0 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 onglong = sizeof
9bac0 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 (char*)==sizeof(
9bad0 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c i64);. fl
9bae0 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 ag_long = sizeof
9baf0 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 (char*)==sizeof(
9bb00 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 long int);.
9bb10 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 /* Fall throu
9bb20 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 gh into the next
9bb30 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 case */. c
9bb40 61 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 ase etORDINAL:.
9bb50 20 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 case etRADI
9bb60 58 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 X:. if( i
9bb70 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c nfop->flags & FL
9bb80 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 AG_SIGNED ){.
9bb90 20 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 i64 v;.
9bba0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
9bbb0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 _longlong ){.
9bbc0 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f v = va_
9bbd0 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 arg(ap,i64);.
9bbe0 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
9bbf0 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 flag_long ){.
9bc00 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 v = va
9bc10 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 _arg(ap,long int
9bc20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
9bc30 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
9bc40 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e v = va_arg(ap,in
9bc50 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a t);. }.
9bc60 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c if( v<
9bc70 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
9bc80 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b longvalue = -v;
9bc90 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 . pre
9bca0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 fix = '-';.
9bcb0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9bcc0 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 longvalu
9bcd0 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 e = v;.
9bce0 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 if( flag_plus
9bcf0 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 sign ) pr
9bd00 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 efix = '+';.
9bd10 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 else if(
9bd20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 flag_blanksign
9bd30 29 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b ) prefix = ' ';
9bd40 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 . els
9bd50 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
9bd60 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
9bd70 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0;. }.
9bd80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
9bd90 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 if( fla
9bda0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 g_longlong ){.
9bdb0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
9bdc0 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c lue = va_arg(ap,
9bdd0 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 u64);.
9bde0 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c }else if( flag_l
9bdf0 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ong ){.
9be00 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 longvalue = v
9be10 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 a_arg(ap,unsigne
9be20 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 d long int);.
9be30 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
9be40 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
9be50 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c lue = va_arg(ap,
9be60 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 unsigned int);.
9be70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
9be80 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b prefix = 0;
9be90 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9bea0 20 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 if( longvalue
9beb0 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 ==0 ) flag_alter
9bec0 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 nateform = 0;.
9bed0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a if( flag_z
9bee0 65 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 eropad && precis
9bef0 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 ion<width-(prefi
9bf00 78 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 x!=0) ){.
9bf10 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 precision = w
9bf20 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 idth-(prefix!=0)
9bf30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9bf40 20 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 bufpt = &buf
9bf50 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 [etBUFSIZE-1];.
9bf60 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
9bf70 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 ==etORDINAL ){.
9bf80 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 static
9bf90 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b const char zOrd[
9bfa0 5d 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a ] = "thstndrd";.
9bfb0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 int x
9bfc0 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 = (int)(longvalu
9bfd0 65 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 e % 10);.
9bfe0 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 if( x>=4 || (
9bff0 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 longvalue/10)%10
9c000 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==1 ){.
9c010 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 x = 0;.
9c020 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
9c030 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d buf[etBUFSIZE-3]
9c040 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 = zOrd[x*2];.
9c050 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 buf[etBU
9c060 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b FSIZE-2] = zOrd[
9c070 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 x*2+1];.
9c080 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 bufpt -= 2;.
9c090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9c0a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 {. regi
9c0b0 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 ster const char
9c0c0 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 *cset; /* U
9c0d0 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 se registers for
9c0e0 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 speed */.
9c0f0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
9c100 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 base;.
9c110 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 cset = &aDigits
9c120 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d [infop->charset]
9c130 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 ;. base
9c140 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a = infop->base;.
9c150 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 do{
9c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c180 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 /* Conve
9c190 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 rt to ascii */.
9c1a0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 *(--b
9c1b0 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e ufpt) = cset[lon
9c1c0 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 gvalue%base];.
9c1d0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
9c1e0 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f lue = longvalue/
9c1f0 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
9c200 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 }while( longvalu
9c210 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d e>0 );. }
9c220 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
9c230 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 = (int)(&buf[etB
9c240 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 UFSIZE-1]-bufpt)
9c250 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 ;. for(id
9c260 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 x=precision-leng
9c270 74 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d th; idx>0; idx--
9c280 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d ){. *(-
9c290 2d 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 -bufpt) = '0';
9c2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c2b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 /* Ze
9c2c0 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 ro pad */.
9c2d0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
9c2e0 70 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 prefix ) *(--buf
9c2f0 70 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 pt) = prefix;
9c300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
9c310 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 dd sign */.
9c320 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 if( flag_alte
9c330 72 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 rnateform && inf
9c340 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 op->prefix ){
9c350 20 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 /* Add "0" or
9c360 20 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 "0x" */.
9c370 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 const char *p
9c380 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 re;. ch
9c390 61 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 ar x;.
9c3a0 70 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 pre = &aPrefix[i
9c3b0 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 nfop->prefix];.
9c3c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 for(; (
9c3d0 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 x=(*pre))!=0; pr
9c3e0 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 e++) *(--bufpt)
9c3f0 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = x;. }.
9c400 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
9c410 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 (int)(&buf[etBUF
9c420 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a SIZE-1]-bufpt);.
9c430 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9c440 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 case etFLOA
9c450 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 T:. case et
9c460 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 EXP:. case
9c470 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 etGENERIC:.
9c480 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 realvalue = v
9c490 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 a_arg(ap,double)
9c4a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
9c4b0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
9c4c0 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 OINT. if(
9c4d0 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 precision<0 ) p
9c4e0 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 recision = 6;
9c4f0 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 /* Set def
9c500 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a ault precision *
9c510 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 /. if( pr
9c520 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a ecision>etBUFSIZ
9c530 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 E/2-10 ) precisi
9c540 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 on = etBUFSIZE/2
9c550 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 -10;. if(
9c560 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 realvalue<0.0 )
9c570 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c {. real
9c580 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c value = -realval
9c590 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ue;. pr
9c5a0 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 efix = '-';.
9c5b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9c5c0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c if( flag_pl
9c5d0 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 ussign )
9c5e0 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a prefix = '+';.
9c5f0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 else i
9c600 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 f( flag_blanksig
9c610 6e 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 n ) prefix =
9c620 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 ' ';. e
9c630 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 lse
9c640 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 pref
9c650 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ix = 0;.
9c660 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 }. if( xt
9c670 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 ype==etGENERIC &
9c680 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 & precision>0 )
9c690 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 precision--;.#if
9c6a0 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 0. /* Ro
9c6b0 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b unding works lik
9c6c0 65 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 e BSD when the c
9c6d0 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 onstant 0.4999 i
9c6e0 73 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 s used. Wierd!
9c6f0 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 */. for(i
9c700 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f dx=precision, ro
9c710 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 under=0.4999; id
9c720 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e x>0; idx--, roun
9c730 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 der*=0.1);.#else
9c740 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d . /* It m
9c750 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 akes more sense
9c760 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 to use 0.5 */.
9c770 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
9c780 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 ecision, rounder
9c790 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 =0.5; idx>0; idx
9c7a0 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 --, rounder*=0.1
9c7b0 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ){}.#endif.
9c7c0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 if( xtype==et
9c7d0 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 FLOAT ) realvalu
9c7e0 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 e += rounder;.
9c7f0 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 /* Normali
9c800 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 ze realvalue to
9c810 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 within 10.0 > re
9c820 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a alvalue >= 1.0 *
9c830 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 /. exp =
9c840 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 0;. if( s
9c850 71 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 qlite3IsNaN((dou
9c860 62 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 ble)realvalue) )
9c870 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 {. bufp
9c880 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 t = "NaN";.
9c890 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b length = 3;
9c8a0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
9c8b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9c8c0 20 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 if( realvalu
9c8d0 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 e>0.0 ){.
9c8e0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
9c8f0 6c 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 lue>=1e32 && exp
9c900 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c <=350 ){ realval
9c910 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 ue *= 1e-32; exp
9c920 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 +=32; }.
9c930 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c while( realval
9c940 75 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d ue>=1e8 && exp<=
9c950 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 350 ){ realvalue
9c960 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 *= 1e-8; exp+=8
9c970 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
9c980 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
9c990 31 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 10.0 && exp<=350
9c9a0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
9c9b0 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 0.1; exp++; }.
9c9c0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
9c9d0 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 realvalue<1e-8 )
9c9e0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
9c9f0 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 e8; exp-=8; }.
9ca00 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
9ca10 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 ealvalue<1.0 ){
9ca20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e realvalue *= 10.
9ca30 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 0; exp--; }.
9ca40 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 if( exp>35
9ca50 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
9ca60 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 if( prefix=='-'
9ca70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
9ca80 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 bufpt = "-Inf"
9ca90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
9caa0 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d lse if( prefix==
9cab0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 '+' ){.
9cac0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 bufpt = "+I
9cad0 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nf";.
9cae0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9caf0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 bufpt = "I
9cb00 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nf";.
9cb10 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c }. l
9cb20 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 ength = sqlite3S
9cb30 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a trlen30(bufpt);.
9cb40 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
9cb50 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 k;. }.
9cb60 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9cb70 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 bufpt = buf;.
9cb80 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 /*.
9cb90 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 ** If the field
9cba0 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 type is etGENER
9cbb0 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 IC, then convert
9cbc0 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 to either etEXP
9cbd0 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 . ** or e
9cbe0 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f tFLOAT, as appro
9cbf0 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 priate..
9cc00 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f */. flag_
9cc10 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 exp = xtype==etE
9cc20 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 XP;. if(
9cc30 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 xtype!=etFLOAT )
9cc40 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c {. real
9cc50 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 value += rounder
9cc60 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
9cc70 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
9cc80 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
9cc90 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 0.1; exp++; }.
9cca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9ccb0 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e if( xtype==etGEN
9ccc0 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 ERIC ){.
9ccd0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c flag_rtz = !fl
9cce0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
9ccf0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
9cd00 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 exp<-4 || exp>pr
9cd10 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 ecision ){.
9cd20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
9cd30 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 tEXP;.
9cd40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9cd50 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 precision = p
9cd60 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a recision - exp;.
9cd70 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 xtyp
9cd80 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 e = etFLOAT;.
9cd90 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9cda0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9cdb0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a flag_rtz = 0;.
9cdc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9cdd0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 if( xtype==etE
9cde0 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 XP ){.
9cdf0 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 e2 = 0;.
9ce00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9ce10 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 e2 = exp;.
9ce20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 }. nsd
9ce30 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c = 0;. fl
9ce40 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 ag_dp = (precisi
9ce50 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 on>0 ?1:0) | fla
9ce60 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 g_alternateform
9ce70 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b | flag_altform2;
9ce80 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
9ce90 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 sign in front of
9cea0 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 the number */.
9ceb0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
9cec0 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a x ){. *
9ced0 28 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 (bufpt++) = pref
9cee0 69 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ix;. }.
9cef0 20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 /* Digits
9cf00 70 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 prior to the dec
9cf10 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
9cf20 20 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 if( e2<0 )
9cf30 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
9cf40 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 fpt++) = '0';.
9cf50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
9cf60 20 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e for(; e2>
9cf70 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 =0; e2--){.
9cf80 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
9cf90 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 ) = et_getdigit(
9cfa0 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 &realvalue,&nsd)
9cfb0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
9cfc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9cfd0 2f 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 /* The decimal p
9cfe0 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 oint */.
9cff0 69 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 if( flag_dp ){.
9d000 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
9d010 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 ++) = '.';.
9d020 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
9d030 22 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 "0" digits after
9d040 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 the decimal poi
9d050 6e 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 nt but before th
9d060 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 e first.
9d070 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 ** significant d
9d080 69 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 igit of the numb
9d090 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f er */. fo
9d0a0 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 r(e2++; e2<0; pr
9d0b0 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 ecision--, e2++)
9d0c0 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
9d0d0 72 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 rt( precision>0
9d0e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 );. *(b
9d0f0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 ufpt++) = '0';.
9d100 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9d110 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 /* Significant
9d120 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 digits after the
9d130 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a decimal point *
9d140 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 /. while(
9d150 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 (precision--)>0
9d160 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
9d170 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 bufpt++) = et_ge
9d180 74 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 tdigit(&realvalu
9d190 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 e,&nsd);.
9d1a0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 }. /* Re
9d1b0 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 move trailing ze
9d1c0 72 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 ros and the "."
9d1d0 69 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c if no digits fol
9d1e0 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 low the "." */.
9d1f0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
9d200 72 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 rtz && flag_dp )
9d210 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c {. whil
9d220 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 e( bufpt[-1]=='0
9d230 27 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d ' ) *(--bufpt) =
9d240 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 0;. as
9d250 73 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 sert( bufpt>buf
9d260 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
9d270 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 bufpt[-1]=='.'
9d280 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
9d290 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 f( flag_altform2
9d2a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
9d2b0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
9d2c0 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0';.
9d2d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9d2e0 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 *(--bufpt)
9d2f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
9d300 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
9d310 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9d320 20 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e /* Add the "eNN
9d330 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 N" suffix */.
9d340 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 if( flag_ex
9d350 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 p || xtype==etEX
9d360 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a P ){. *
9d370 28 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 (bufpt++) = aDig
9d380 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 its[infop->chars
9d390 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 et];. i
9d3a0 66 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 f( exp<0 ){.
9d3b0 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
9d3c0 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 +) = '-'; exp =
9d3d0 2d 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 -exp;.
9d3e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9d3f0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
9d400 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d '+';. }
9d410 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
9d420 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 xp>=100 ){.
9d430 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
9d440 29 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f ) = (char)((exp/
9d450 31 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 100)+'0');
9d460 20 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 /* 100's digit
9d470 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
9d480 65 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 exp %= 100;.
9d490 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9d4a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
9d4b0 63 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 char)(exp/10+'0'
9d4c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ); /
9d4d0 2a 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a * 10's digit */.
9d4e0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
9d4f0 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 t++) = (char)(ex
9d500 70 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 p%10+'0');
9d510 20 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 /* 1's di
9d520 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d git */. }
9d530 0a 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 . *bufpt
9d540 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a = 0;.. /*
9d550 20 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e The converted n
9d560 75 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b umber is in buf[
9d570 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 ] and zero termi
9d580 6e 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 nated. Output it
9d590 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 .. ** Not
9d5a0 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 e that the numbe
9d5b0 72 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 r is in the usua
9d5c0 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 l order, not rev
9d5d0 65 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 ersed as with.
9d5e0 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 ** integer
9d5f0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f conversions. */
9d600 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
9d610 3d 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 = (int)(bufpt-bu
9d620 66 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 f);. bufp
9d630 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 t = buf;..
9d640 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 /* Special cas
9d650 65 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 e: Add leading
9d660 7a 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 zeros if the fla
9d670 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 g_zeropad flag i
9d680 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 s. ** set
9d690 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 and we are not
9d6a0 6c 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a left justified *
9d6b0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c /. if( fl
9d6c0 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 ag_zeropad && !f
9d6d0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
9d6e0 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 && length < widt
9d6f0 68 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e h){. in
9d700 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 t i;. i
9d710 6e 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 nt nPad = width
9d720 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 - length;.
9d730 20 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b for(i=width;
9d740 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a i>=nPad; i--){.
9d750 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 bufp
9d760 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e t[i] = bufpt[i-n
9d770 50 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 Pad];.
9d780 7d 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 }. i =
9d790 70 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 prefix!=0;.
9d7a0 20 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 while( nPad
9d7b0 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 -- ) bufpt[i++]
9d7c0 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 = '0';.
9d7d0 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b length = width;
9d7e0 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 . }.#endi
9d7f0 66 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b f. break;
9d800 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 . case etSI
9d810 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 ZE:. *(va
9d820 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d _arg(ap,int*)) =
9d830 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a pAccum->nChar;.
9d840 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
9d850 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
9d860 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9d870 20 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a case etPERCENT:
9d880 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 . buf[0]
9d890 3d 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 = '%';. b
9d8a0 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 ufpt = buf;.
9d8b0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a length = 1;.
9d8c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9d8d0 20 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 case etCHAR
9d8e0 58 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 X:. c = v
9d8f0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 a_arg(ap,int);.
9d900 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 buf[0] =
9d910 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 (char)c;.
9d920 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d if( precision>=
9d930 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 0 ){. f
9d940 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 or(idx=1; idx<pr
9d950 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 ecision; idx++)
9d960 62 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 buf[idx] = (char
9d970 29 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 )c;. le
9d980 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e ngth = precision
9d990 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
9d9a0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 . lengt
9d9b0 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a h =1;. }.
9d9c0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
9d9d0 62 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 buf;. bre
9d9e0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
9d9f0 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 tSTRING:. c
9da00 61 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a ase etDYNSTRING:
9da10 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d . bufpt =
9da20 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
9da30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 );. if( b
9da40 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ufpt==0 ){.
9da50 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b bufpt = "";
9da60 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
9da70 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 f( xtype==etDYNS
9da80 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 TRING ){.
9da90 20 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 zExtra = bufp
9daa0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
9dab0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
9dac0 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 on>=0 ){.
9dad0 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b for(length=0;
9dae0 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f length<precisio
9daf0 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 n && bufpt[lengt
9db00 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a h]; length++){}.
9db10 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
9db20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
9db30 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
9db40 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 0(bufpt);.
9db50 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
9db60 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
9db70 53 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 SQLESCAPE:.
9db80 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 case etSQLESCAP
9db90 45 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 E2:. case e
9dba0 74 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 tSQLESCAPE3: {.
9dbb0 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c int i, j,
9dbc0 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 n, isnull;.
9dbd0 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 int needQuot
9dbe0 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 e;. char
9dbf0 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ch;. char
9dc00 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 q = ((xtype==et
9dc10 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a SQLESCAPE3)?'"':
9dc20 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 '\''); /* Quot
9dc30 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 e character */.
9dc40 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 char *esc
9dc50 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c arg = va_arg(ap,
9dc60 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 char*);.
9dc70 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d isnull = escarg=
9dc80 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 =0;. if(
9dc90 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 isnull ) escarg
9dca0 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 = (xtype==etSQLE
9dcb0 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 SCAPE2 ? "NULL"
9dcc0 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 : "(NULL)");.
9dcd0 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 for(i=n=0;
9dce0 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d (ch=escarg[i])!=
9dcf0 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 0; i++){.
9dd00 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 if( ch==q )
9dd10 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n++;. }.
9dd20 20 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 needQuote
9dd30 20 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 = !isnull && xt
9dd40 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
9dd50 32 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 2;. n +=
9dd60 69 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 i + 1 + needQuot
9dd70 65 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e*2;. if(
9dd80 20 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a n>etBUFSIZE ){.
9dd90 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 bufpt
9dda0 3d 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 = zExtra = sqlit
9ddb0 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 e3Malloc( n );.
9ddc0 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 if( buf
9ddd0 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pt==0 ){.
9dde0 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c pAccum->mal
9ddf0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
9de00 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
9de10 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 n;. }.
9de20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
9de30 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
9de40 62 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 buf;. }.
9de50 20 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 j = 0;.
9de60 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 if( needQu
9de70 6f 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d ote ) bufpt[j++]
9de80 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f = q;. fo
9de90 72 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 r(i=0; (ch=escar
9dea0 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a g[i])!=0; i++){.
9deb0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b bufpt[
9dec0 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 j++] = ch;.
9ded0 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 if( ch==q )
9dee0 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 bufpt[j++] = ch
9def0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9df00 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 if( needQuot
9df10 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d e ) bufpt[j++] =
9df20 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 q;. bufp
9df30 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 t[j] = 0;.
9df40 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 length = j;.
9df50 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 /* The pre
9df60 63 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 cision is ignore
9df70 64 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a d on %q and %Q *
9df80 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 /. /* if(
9df90 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 precision>=0 &&
9dfa0 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 precision<lengt
9dfb0 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 h ) length = pre
9dfc0 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 cision; */.
9dfd0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
9dfe0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 }. case etT
9dff0 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 OKEN: {.
9e000 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 Token *pToken =
9e010 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e va_arg(ap, Token
9e020 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *);. if(
9e030 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 pToken ){.
9e040 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
9e050 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
9e060 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 , (const char*)p
9e070 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e Token->z, pToken
9e080 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ->n);. }.
9e090 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
9e0a0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
9e0b0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9e0c0 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 }. case et
9e0d0 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 SRCLIST: {.
9e0e0 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 SrcList *pSrc
9e0f0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 = va_arg(ap, Sr
9e100 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 cList*);.
9e110 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 int k = va_arg(
9e120 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ap, int);.
9e130 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
9e140 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 _item *pItem = &
9e150 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 pSrc->a[k];.
9e160 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 assert( k>=0
9e170 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 && k<pSrc->nSrc
9e180 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
9e190 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 pItem->zDatabase
9e1a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
9e1b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
9e1c0 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 end(pAccum, pIte
9e1d0 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 m->zDatabase, -1
9e1e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
9e1f0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
9e200 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 nd(pAccum, ".",
9e210 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1);. }.
9e220 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
9e230 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
9e240 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 um, pItem->zName
9e250 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c , -1);. l
9e260 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 ength = width =
9e270 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 0;. break
9e280 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
9e290 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
9e2a0 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 assert( xtype
9e2b0 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 ==etINVALID );.
9e2c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
9e2d0 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 }. }/* E
9e2e0 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 nd switch over t
9e2f0 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a he format type *
9e300 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 /. /*. **
9e310 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 The text of the
9e320 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
9e330 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 inted to by "buf
9e340 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a pt" and is. *
9e350 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 * "length" chara
9e360 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 cters long. The
9e370 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 field width is
9e380 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 "width". Do.
9e390 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a ** the output..
9e3a0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
9e3b0 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
9e3c0 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 ){. regist
9e3d0 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 er int nspace;.
9e3e0 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 nspace = wi
9e3f0 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 dth-length;.
9e400 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 if( nspace>0 )
9e410 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 {. append
9e420 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 Space(pAccum, ns
9e430 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pace);. }.
9e440 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e }. if( len
9e450 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 gth>0 ){. s
9e460 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
9e470 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 pend(pAccum, buf
9e480 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 pt, length);.
9e490 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f }. if( flag_
9e4a0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 leftjustify ){.
9e4b0 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
9e4c0 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 t nspace;.
9e4d0 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c nspace = width-l
9e4e0 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 ength;. if(
9e4f0 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 nspace>0 ){.
9e500 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 appendSpace
9e510 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 (pAccum, nspace)
9e520 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
9e530 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 if( zExtra )
9e540 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
9e550 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 free(zExtra);.
9e560 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f }. }/* End fo
9e570 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 r loop over the
9e580 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f format string */
9e590 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e .} /* End of fun
9e5a0 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ction */../*.**
9e5b0 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f Append N bytes o
9e5c0 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f f text from z to
9e5d0 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 the StrAccum ob
9e5e0 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
9e5f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9e600 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
9e610 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 nd(StrAccum *p,
9e620 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
9e630 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt N){. assert(
9e640 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b z!=0 || N==0 );
9e650 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 . if( p->tooBig
9e660 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | p->mallocFail
9e670 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 ed ){. testca
9e680 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 se(p->tooBig);.
9e690 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d testcase(p->m
9e6a0 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 allocFailed);.
9e6b0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
9e6c0 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e if( N<0 ){. N
9e6d0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
9e6e0 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 30(z);. }. if(
9e6f0 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a N==0 || NEVER(z
9e700 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ==0) ){. retu
9e710 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d rn;. }. if( p-
9e720 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e >nChar+N >= p->n
9e730 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 Alloc ){. cha
9e740 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 r *zNew;. if(
9e750 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 !p->useMalloc )
9e760 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 {. p->tooBi
9e770 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d g = 1;. N =
9e780 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e p->nAlloc - p->
9e790 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 nChar - 1;.
9e7a0 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 if( N<=0 ){.
9e7b0 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
9e7c0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
9e7d0 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 i64 szNew
9e7e0 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 = p->nChar;.
9e7f0 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 szNew += N + 1
9e800 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 ;. if( szNe
9e810 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 w > p->mxAlloc )
9e820 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
9e830 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 3StrAccumReset(p
9e840 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f );. p->to
9e850 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 oBig = 1;.
9e860 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
9e870 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
9e880 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 ->nAlloc = (int)
9e890 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 szNew;. }.
9e8a0 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 zNew = sqli
9e8b0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
9e8c0 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 ->db, p->nAlloc
9e8d0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 );. if( zNe
9e8e0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d w ){. mem
9e8f0 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 cpy(zNew, p->zTe
9e900 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 xt, p->nChar);.
9e910 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
9e920 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
9e930 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 p->zText
9e940 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d = zNew;. }
9e950 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
9e960 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
9e970 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 1;. sqlit
9e980 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
9e990 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
9e9a0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
9e9b0 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 }. }. memcpy(&
9e9c0 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 p->zText[p->nCha
9e9d0 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e r], z, N);. p->
9e9e0 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f nChar += N;.}../
9e9f0 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 *.** Finish off
9ea00 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 a string by maki
9ea10 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 ng sure it is ze
9ea20 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a ro-terminated..*
9ea30 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
9ea40 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 er to the result
9ea50 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 ing string. Ret
9ea60 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f urn a NULL.** po
9ea70 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e inter if any kin
9ea80 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 d of error was e
9ea90 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 ncountered..*/.S
9eaa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
9eab0 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 ar *sqlite3StrAc
9eac0 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 cumFinish(StrAcc
9ead0 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d um *p){. if( p-
9eae0 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d >zText ){. p-
9eaf0 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d >zText[p->nChar]
9eb00 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d = 0;. if( p-
9eb10 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d >useMalloc && p-
9eb20 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 >zText==p->zBase
9eb30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 ){. p->zTe
9eb40 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 xt = sqlite3DbMa
9eb50 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 llocRaw(p->db, p
9eb60 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 ->nChar+1 );.
9eb70 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 if( p->zText
9eb80 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
9eb90 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a y(p->zText, p->z
9eba0 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 Base, p->nChar+1
9ebb0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
9ebc0 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f p->mallo
9ebd0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
9ebe0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
9ebf0 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 return p->zText
9ec00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 ;.}../*.** Reset
9ec10 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 an StrAccum str
9ec20 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c ing. Reclaim al
9ec30 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 l malloced memor
9ec40 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
9ec50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
9ec60 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 3StrAccumReset(S
9ec70 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
9ec80 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e f( p->zText!=p->
9ec90 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c zBase ){. sql
9eca0 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
9ecb0 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d , p->zText);. }
9ecc0 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b . p->zText = 0;
9ecd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
9ece0 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 lize a string ac
9ecf0 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c cumulator.*/.SQL
9ed00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9ed10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
9ed20 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 Init(StrAccum *p
9ed30 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 , char *zBase, i
9ed40 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 nt n, int mx){.
9ed50 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a p->zText = p->z
9ed60 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 Base = zBase;.
9ed70 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e p->db = 0;. p->
9ed80 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e nChar = 0;. p->
9ed90 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d nAlloc = n;. p-
9eda0 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 >mxAlloc = mx;.
9edb0 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 p->useMalloc =
9edc0 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 1;. p->tooBig =
9edd0 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 0;. p->mallocF
9ede0 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ailed = 0;.}../*
9edf0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
9ee00 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
9ee10 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
9ee20 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 (). Use the int
9ee30 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
9ee40 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
9ee50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9ee60 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
9ee70 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 3VMPrintf(sqlite
9ee80 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
9ee90 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c r *zFormat, va_l
9eea0 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 ist ap){. char
9eeb0 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 *z;. char zBase
9eec0 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 [SQLITE_PRINT_BU
9eed0 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 F_SIZE];. StrAc
9eee0 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 cum acc;. asser
9eef0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 t( db!=0 );. sq
9ef00 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
9ef10 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 t(&acc, zBase, s
9ef20 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 izeof(zBase),.
9ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ef40 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 db->aLimit[S
9ef50 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
9ef60 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d TH]);. acc.db =
9ef70 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 db;. sqlite3VX
9ef80 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 Printf(&acc, 1,
9ef90 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
9efa0 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 z = sqlite3StrAc
9efb0 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
9efc0 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f . if( acc.mallo
9efd0 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 cFailed ){. d
9efe0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
9eff0 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
9f000 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
9f010 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
9f020 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
9f030 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 liteMalloc(). U
9f040 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a se the internal.
9f050 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
9f060 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
9f070 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
9f080 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e ar *sqlite3MPrin
9f090 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 tf(sqlite3 *db,
9f0a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
9f0b0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
9f0c0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
9f0d0 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 *z;. va_start(a
9f0e0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
9f0f0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
9f100 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
9f110 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
9f120 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
9f130 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c ../*.** Like sql
9f140 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 ite3MPrintf(), b
9f150 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 ut call sqlite3D
9f160 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 bFree() on zStr
9f170 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 after formatting
9f180 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 .** the string a
9f190 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e nd before return
9f1a0 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 ning. This rout
9f1b0 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ine is intended
9f1c0 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f to be used.** to
9f1d0 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 modify an exist
9f1e0 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 ing string. For
9f1f0 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 example:.**.**
9f200 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 x = sqlite
9f210 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 3MPrintf(db, x,
9f220 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 "prefix %s suffi
9f230 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 x", x);.**.*/.SQ
9f240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
9f250 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
9f260 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 df(sqlite3 *db,
9f270 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 char *zStr, cons
9f280 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
9f290 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
9f2a0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
9f2b0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
9f2c0 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
9f2d0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
9f2e0 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
9f2f0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
9f300 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
9f310 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 b, zStr);. retu
9f320 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
9f330 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
9f340 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
9f350 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
9f360 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 Omit the inter
9f370 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 nal.** %-convers
9f380 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a ion extensions..
9f390 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 */.SQLITE_API ch
9f3a0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 ar *sqlite3_vmpr
9f3b0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
9f3c0 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 *zFormat, va_lis
9f3d0 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a t ap){. char *z
9f3e0 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 ;. char zBase[S
9f3f0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
9f400 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 SIZE];. StrAccu
9f410 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 m acc;.#ifndef S
9f420 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
9f430 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
9f440 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
9f450 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
9f460 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 f. sqlite3StrAc
9f470 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 cumInit(&acc, zB
9f480 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 ase, sizeof(zBas
9f490 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c e), SQLITE_MAX_L
9f4a0 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 ENGTH);. sqlite
9f4b0 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 3VXPrintf(&acc,
9f4c0 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0, zFormat, ap);
9f4d0 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 . z = sqlite3St
9f4e0 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 rAccumFinish(&ac
9f4f0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a c);. return z;.
9f500 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
9f510 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
9f520 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
9f530 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d _malloc()(). Om
9f540 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a it the internal.
9f550 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
9f560 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
9f570 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
9f580 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
9f590 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
9f5a0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
9f5b0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
9f5c0 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 *z;.#ifndef SQLI
9f5d0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
9f5e0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
9f5f0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
9f600 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
9f610 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
9f620 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
9f630 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a lite3_vmprintf(z
9f640 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
9f650 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 a_end(ap);. ret
9f660 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
9f670 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
9f680 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e () works like sn
9f690 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 printf() except
9f6a0 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 that it ignores
9f6b0 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c the.** current l
9f6c0 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 ocale settings.
9f6d0 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 This is importa
9f6e0 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 nt for SQLite be
9f6f0 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 cause we.** are
9f700 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 not able to use
9f710 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 a "," as the dec
9f720 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c imal point in pl
9f730 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a ace of "." as.**
9f740 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f specified by so
9f750 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 me locales..*/.S
9f760 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
9f770 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
9f780 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 (int n, char *zB
9f790 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a uf, const char *
9f7a0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
9f7b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c char *z;. va_l
9f7c0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 ist ap;. StrAcc
9f7d0 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e um acc;.. if( n
9f7e0 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 <=0 ){. retur
9f7f0 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 n zBuf;. }. sq
9f800 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
9f810 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c t(&acc, zBuf, n,
9f820 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 0);. acc.useMa
9f830 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 lloc = 0;. va_s
9f840 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 tart(ap,zFormat)
9f850 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
9f860 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
9f870 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
9f880 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 end(ap);. z = s
9f890 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
9f8a0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 nish(&acc);. re
9f8b0 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 turn z;.}..#if d
9f8c0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
9f8d0 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 BUG)./*.** A ver
9f8e0 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 sion of printf()
9f8f0 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 that understand
9f900 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f s %lld. Used fo
9f910 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 r debugging..**
9f920 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 The printf() bui
9f930 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 lt into some ver
9f940 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 sions of windows
9f950 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 does not unders
9f960 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 tand %lld.** and
9f970 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f segfaults if yo
9f980 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 u give it a long
9f990 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 long int..*/.SQ
9f9a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9f9b0 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 d sqlite3DebugPr
9f9c0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
9f9d0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
9f9e0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
9f9f0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
9fa00 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a char zBuf[500];.
9fa10 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
9fa20 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 mInit(&acc, zBuf
9fa30 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 , sizeof(zBuf),
9fa40 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 0);. acc.useMal
9fa50 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 loc = 0;. va_st
9fa60 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
9fa70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
9fa80 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 tf(&acc, 0, zFor
9fa90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
9faa0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 nd(ap);. sqlite
9fab0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
9fac0 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 &acc);. fprintf
9fad0 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 (stdout,"%s", zB
9fae0 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 uf);. fflush(st
9faf0 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a dout);.}.#endif.
9fb00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9fb10 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 End of printf.c
9fb20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
9fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9fb50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9fb60 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 Begin file rand
9fb70 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a om.c ***********
9fb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9fba0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
9fbb0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
9fbc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
9fbd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
9fbe0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
9fbf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
9fc00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
9fc10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
9fc20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
9fc30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
9fc40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
9fc50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
9fc60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
9fc70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
9fc80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
9fc90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
9fca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
9fcb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
9fcc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
9fcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fd10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
9fd20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
9fd30 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 de to implement
9fd40 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 a pseudo-random
9fd50 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 number.** genera
9fd60 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 tor (PRNG) for S
9fd70 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e QLite..**.** Ran
9fd80 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 dom numbers are
9fd90 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 used by some of
9fda0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 the database bac
9fdb0 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a kends in order.*
9fdc0 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 * to generate ra
9fdd0 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 ndom integer key
9fde0 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 s for tables or
9fdf0 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 random filenames
9fe00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e ..**.** $Id: ran
9fe10 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 dom.c,v 1.29 200
9fe20 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 8/12/10 19:26:24
9fe30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
9fe40 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73 /* All threads s
9fe50 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61 hare a single ra
9fe60 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
9fe70 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 rator..** This s
9fe80 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 tructure is the
9fe90 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
9fea0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a the generator..
9feb0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 */.static SQLITE
9fec0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 _WSD struct sqli
9fed0 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 te3PrngType {.
9fee0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 unsigned char is
9fef0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f Init; /
9ff00 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 * True if initia
9ff10 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 lized */. unsig
9ff20 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 ned char i, j;
9ff30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
9ff40 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a te variables */.
9ff50 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
9ff60 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20 s[256];
9ff70 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 /* State variab
9ff80 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 les */.} sqlite3
9ff90 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 Prng;../*.** Get
9ffa0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 a single 8-bit
9ffb0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f random value fro
9ffc0 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 m the RC4 PRNG.
9ffd0 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 The Mutex.** mu
9ffe0 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 st be held while
9fff0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
a0000 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 routine..**.** W
a0010 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 hy not just use
a0020 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d a library random
a0030 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 generator like
a0040 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 lrand48() for th
a0050 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 is?.** Because t
a0060 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f he OP_NewRowid o
a0070 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 pcode in the VDB
a0080 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 E depends on hav
a0090 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f ing a very.** go
a00a0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e od source of ran
a00b0 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 dom numbers. Th
a00c0 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 e lrand48() libr
a00d0 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 ary function may
a00e0 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 .** well be good
a00f0 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 enough. But ma
a0100 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 ybe not. Or may
a0110 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 be lrand48() has
a0120 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 some.** subtle
a0130 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 problems on some
a0140 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f systems that co
a0150 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 uld cause proble
a0160 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a ms. It is hard.
a0170 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 ** to know. To
a0180 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 minimize the ris
a0190 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 k of problems du
a01a0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 e to bad lrand48
a01b0 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ().** implementa
a01c0 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 tions, SQLite us
a01d0 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e es this random n
a01e0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
a01f0 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c based.** on RC4,
a0200 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 which we know w
a0210 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a orks very well..
a0220 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 **.** (Later):
a0230 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 Actually, OP_New
a0240 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 Rowid does not d
a0250 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 epend on a good
a0260 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e source of.** ran
a0270 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 domness any more
a0280 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c . But we will l
a0290 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 eave this code i
a02a0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a n all the same..
a02b0 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e */.static u8 ran
a02c0 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 domByte(void){.
a02d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
a02e0 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 ;... /* The "ws
a02f0 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c dPrng" macro wil
a0300 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 l resolve to the
a0310 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
a0320 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a umber generator.
a0330 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f ** state vecto
a0340 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 r. If writable
a0350 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 static data is u
a0360 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 nsupported on th
a0370 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 e target,. ** w
a0380 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 e have to locate
a0390 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f the state vecto
a03a0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 r at run-time.
a03b0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d In the more comm
a03c0 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 on. ** case whe
a03d0 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 re writable stat
a03e0 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f ic data is suppo
a03f0 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61 rted, wsdPrng ca
a0400 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
a0410 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 . ** to the "sq
a0420 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65 lite3Prng" state
a0430 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 vector declared
a0440 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 above.. */.#if
a0450 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
a0460 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c WSD. struct sql
a0470 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20 ite3PrngType *p
a0480 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 = &GLOBAL(struct
a0490 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a04a0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a , sqlite3Prng);.
a04b0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 # define wsdPrng
a04c0 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 p[0].#else.# de
a04d0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c fine wsdPrng sql
a04e0 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a ite3Prng.#endif.
a04f0 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
a0500 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
a0510 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 he random number
a0520 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c generator once,
a0530 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 . ** the first
a0540 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e time this routin
a0550 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 e is called. Th
a0560 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65 e seed value doe
a0570 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 s. ** not need
a0580 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 to contain a lot
a0590 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 of randomness s
a05a0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a ince we are not.
a05b0 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 ** trying to d
a05c0 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74 o secure encrypt
a05d0 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 ion or anything
a05e0 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a like that.... *
a05f0 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 *. ** Nothing i
a0600 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61 n this file or a
a0610 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20 nywhere else in
a0620 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20 SQLite does any
a0630 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 kind of. ** enc
a0640 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 ryption. The RC
a0650 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 4 algorithm is b
a0660 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50 eing used as a P
a0670 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 RNG (pseudo-rand
a0680 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 om. ** number g
a0690 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 enerator) not as
a06a0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 an encryption d
a06b0 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 evice.. */. if
a06c0 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 ( !wsdPrng.isIni
a06d0 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a t ){. int i;.
a06e0 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b char k[256];
a06f0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d . wsdPrng.j =
a0700 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 0;. wsdPrng.
a0710 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 i = 0;. sqlit
a0720 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
a0730 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
a0740 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 0), 256, k);.
a0750 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b for(i=0; i<256;
a0760 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 i++){. wsd
a0770 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 Prng.s[i] = (u8)
a0780 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 i;. }. for
a0790 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b (i=0; i<256; i++
a07a0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ){. wsdPrng
a07b0 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b .j += wsdPrng.s[
a07c0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 i] + k[i];.
a07d0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 t = wsdPrng.s[w
a07e0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 sdPrng.j];.
a07f0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a0800 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.j] = wsdPrng.
a0810 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 s[i];. wsdP
a0820 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 rng.s[i] = t;.
a0830 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e }. wsdPrng.
a0840 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a isInit = 1;. }.
a0850 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 . /* Generate a
a0860 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 nd return single
a0870 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a random byte. *
a0880 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b /. wsdPrng.i++;
a0890 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 . t = wsdPrng.s
a08a0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 [wsdPrng.i];. w
a08b0 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 sdPrng.j += t;.
a08c0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a08d0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.i] = wsdPrng.
a08e0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 s[wsdPrng.j];.
a08f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a0900 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d g.j] = t;. t +=
a0910 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a0920 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 ng.i];. return
a0930 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a wsdPrng.s[t];.}.
a0940 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 ./*.** Return N
a0950 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f random bytes..*/
a0960 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
a0970 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
a0980 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 ess(int N, void
a0990 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e *pBuf){. unsign
a09a0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 ed char *zBuf =
a09b0 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 pBuf;.#if SQLITE
a09c0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 _THREADSAFE. sq
a09d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
a09e0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
a09f0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
a0a00 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 TEX_STATIC_PRNG)
a0a10 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
a0a20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
a0a30 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 utex);. while(
a0a40 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 N-- ){. *(zBu
a0a50 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 f++) = randomByt
a0a60 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 e();. }. sqlit
a0a70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
a0a80 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 utex);.}..#ifnde
a0a90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
a0aa0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
a0ab0 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 For testing pur
a0ac0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 poses, we someti
a0ad0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 mes want to pres
a0ae0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f erve the state o
a0af0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 f.** PRNG and re
a0b00 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 store the PRNG t
a0b10 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 o its saved stat
a0b20 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d e at a later tim
a0b30 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 e, or.** to rese
a0b40 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 t the PRNG to it
a0b50 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e s initial state.
a0b60 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
a0b70 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 accomplish.** t
a0b80 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a hose tasks..**.*
a0b90 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 * The sqlite3_te
a0ba0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 st_control() int
a0bb0 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 erface calls the
a0bc0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a se routines to.*
a0bd0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 * control the PR
a0be0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 NG..*/.static SQ
a0bf0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
a0c00 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 sqlite3PrngType
a0c10 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a0c20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a0c30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
a0c40 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 gSaveState(void)
a0c50 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 {. memcpy(.
a0c60 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
a0c70 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a0c80 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
a0c90 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
a0ca0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a0cb0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 gType, sqlite3Pr
a0cc0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 ng),. sizeof(
a0cd0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 sqlite3Prng). )
a0ce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
a0cf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
a0d00 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 rngRestoreState(
a0d10 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 void){. memcpy(
a0d20 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 . &GLOBAL(str
a0d30 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
a0d40 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 ype, sqlite3Prng
a0d50 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
a0d60 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a0d70 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 gType, sqlite3Sa
a0d80 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 vedPrng),. si
a0d90 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 zeof(sqlite3Prng
a0da0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ). );.}.SQLITE_
a0db0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a0dc0 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 ite3PrngResetSta
a0dd0 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 te(void){. GLOB
a0de0 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
a0df0 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
a0e00 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d e3Prng).isInit =
a0e10 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0;.}.#endif /*
a0e20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
a0e30 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
a0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
a0e50 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a of random.c ***
a0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
a0e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
a0ea0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a in file utf.c **
a0eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
a0ee0 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33 ** 2004 April 13
a0ef0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
a0f00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
a0f10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
a0f20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
a0f30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
a0f40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
a0f50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
a0f60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
a0f70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
a0f80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
a0f90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
a0fa0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
a0fb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
a0fc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
a0fd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
a0fe0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
a0ff0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
a1000 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
a1010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
a1050 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
a1060 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
a1070 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 sed to translate
a1080 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 between UTF-8,
a1090 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d .** UTF-16, UTF-
a10a0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 16BE, and UTF-16
a10b0 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 LE..**.** $Id: u
a10c0 74 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39 tf.c,v 1.73 2009
a10d0 2f 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20 /04/01 18:40:32
a10e0 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 drh Exp $.**.**
a10f0 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a Notes on UTF-8:.
a1100 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 **.** Byte-0
a1110 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 Byte-1 Byte
a1120 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 -2 Byte-3
a1130 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78 Value.** 0xxxxx
a1140 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xx
a1150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1160 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 00000000 0000
a1170 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a 0000 0xxxxxxx.**
a1180 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78 110yyyyy 10xx
a1190 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 xxxx
a11a0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 00000
a11b0 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78 000 00000yyy yyx
a11c0 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a xxxxx.** 1110zz
a11d0 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30 zz 10yyyyyy 10
a11e0 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 xxxxxx
a11f0 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 00000000 zzzz
a1200 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a yyyy yyxxxxxx.**
a1210 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75 11110uuu 10uu
a1220 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 zzzz 10yyyyyy
a1230 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75 10xxxxxx 000uu
a1240 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 uuu zzzzyyyy yyx
a1250 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e xxxxx.**.**.** N
a1260 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20 otes on UTF-16:
a1270 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75 (with wwww+1==u
a1280 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 uuuu).**.**
a1290 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20 Word-0
a12a0 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20 Word-1
a12b0 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 Value.**
a12c0 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79 110110ww wwzzzzy
a12d0 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78 y 110111yy yyx
a12e0 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75 xxxxx 000uuuu
a12f0 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 u zzzzyyyy yyxxx
a1300 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 xxx.** zzzzyyyy
a1310 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20 yyxxxxxx
a1320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1330 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 00000000 zzzzyy
a1340 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a yy yyxxxxxx.**.*
a1350 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65 *.** BOM or Byte
a1360 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 Order Mark:.**
a1370 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20 0xff 0xfe
a1380 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 little-endian ut
a1390 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 f-16 follows.**
a13a0 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20 0xfe 0xff
a13b0 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 big-endian utf-1
a13c0 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 6 follows.**.*/.
a13d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a13e0 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e Include vdbeInt.
a13f0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
a1400 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a of utf.c *******
a1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a1420 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a1430 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49 Begin file vdbeI
a1440 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
a1450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a1470 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 /*.** 2003 Septe
a1480 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 mber 6.**.** The
a1490 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
a14a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
a14b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
a14c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
a14d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
a14e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
a14f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
a1500 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
a1510 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
a1520 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
a1530 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
a1540 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
a1550 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
a1560 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
a1570 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
a1580 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
a1590 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
a15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15e0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 *****.** This is
a15f0 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 the header file
a1600 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e for information
a1610 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65 that is private
a1620 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e to the.** VDBE.
a1630 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 This informati
a1640 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62 on used to all b
a1650 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 e at the top of
a1660 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f the single.** so
a1670 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22 urce code file "
a1680 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74 vdbe.c". When t
a1690 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20 hat file became
a16a0 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a too big (over.**
a16b0 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 6000 lines long
a16c0 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75 ) it was split u
a16d0 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73 p into several s
a16e0 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64 maller files and
a16f0 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20 .** this header
a1700 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 information was
a1710 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a factored out..**
a1720 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74 .** $Id: vdbeInt
a1730 2e 68 2c 76 20 31 2e 31 37 30 20 32 30 30 39 2f .h,v 1.170 2009/
a1740 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 64 05/04 11:42:30 d
a1750 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
a1760 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 .*/.#ifndef _VDB
a1770 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 EINT_H_.#define
a1780 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a _VDBEINT_H_../*.
a1790 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29 20 61 6e ** intToKey() an
a17a0 64 20 6b 65 79 54 6f 49 6e 74 28 29 20 75 73 65 d keyToInt() use
a17b0 64 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 74 d to transform t
a17c0 68 65 20 72 6f 77 69 64 2e 20 20 42 75 74 20 77 he rowid. But w
a17d0 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 74 65 73 ith.** the lates
a17e0 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 t versions of th
a17f0 65 20 64 65 73 69 67 6e 20 74 68 65 79 20 61 72 e design they ar
a1800 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 65 e no-ops..*/.#de
a1810 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74 28 58 29 fine keyToInt(X)
a1820 20 20 20 28 58 29 0a 23 64 65 66 69 6e 65 20 69 (X).#define i
a1830 6e 74 54 6f 4b 65 79 28 58 29 20 20 20 28 58 29 ntToKey(X) (X)
a1840 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 .../*.** SQL is
a1850 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 translated into
a1860 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e a sequence of in
a1870 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 structions to be
a1880 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79 20 .** executed by
a1890 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e a virtual machin
a18a0 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75 63 e. Each instruc
a18b0 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 tion is an insta
a18c0 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f nce.** of the fo
a18d0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
a18e0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 e..*/.typedef st
a18f0 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a ruct VdbeOp Op;.
a1900 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 ./*.** Boolean v
a1910 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 alues.*/.typedef
a1920 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 42 unsigned char B
a1930 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 ool;../*.** A cu
a1940 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 rsor is a pointe
a1950 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 r into a single
a1960 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 BTree within a d
a1970 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
a1980 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 The cursor can
a1990 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65 20 seek to a BTree
a19a0 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61 72 entry with a par
a19b0 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a ticular key, or.
a19c0 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c ** loop over all
a19d0 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 entries of the
a19e0 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 Btree. You can
a19f0 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 also insert new
a1a00 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 BTree.** entries
a1a10 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65 or retrieve the
a1a20 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72 6f key or data fro
a1a30 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 m the entry that
a1a40 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 the cursor.** i
a1a50 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
a1a60 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 ting to..** .**
a1a70 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68 61 Every cursor tha
a1a80 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 t the virtual ma
a1a90 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 chine has open i
a1aa0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 s represented by
a1ab0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
a1ac0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
a1ad0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
a1ae0 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75 72 * If the VdbeCur
a1af0 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f 77 sor.isTriggerRow
a1b00 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74 20 flag is set it
a1b10 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 means that this
a1b20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61 cursor is.** rea
a1b30 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 lly a single row
a1b40 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 that represents
a1b50 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 the NEW or OLD
a1b60 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a pseudo-table of.
a1b70 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65 72 ** a row trigger
a1b80 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72 20 . The data for
a1b90 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72 65 the row is store
a1ba0 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e d in VdbeCursor.
a1bb0 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 pData and.** the
a1bc0 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64 62 rowid is in Vdb
a1bd0 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f eCursor.iKey..*/
a1be0 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 .struct VdbeCurs
a1bf0 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 or {. BtCursor
a1c00 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 *pCursor; /*
a1c10 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 The cursor struc
a1c20 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b ture of the back
a1c30 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 end */. int iDb
a1c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a1c50 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73 6f * Index of curso
a1c60 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 r database in db
a1c70 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 ->aDb[] (or -1)
a1c80 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 */. i64 lastRow
a1c90 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 id; /* La
a1ca0 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 st rowid from a
a1cb0 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78 20 Next or NextIdx
a1cc0 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 operation */. B
a1cd0 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 ool zeroed;
a1ce0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1cf0 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 zeroed out and r
a1d00 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 20 2a eady for reuse *
a1d10 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 /. Bool rowidIs
a1d20 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 Valid; /* Tru
a1d30 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69 e if lastRowid i
a1d40 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f s valid */. Boo
a1d50 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 20 20 l atFirst;
a1d60 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f /* True if po
a1d70 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20 inting to first
a1d80 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 entry */. Bool
a1d90 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 useRandomRowid;
a1da0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77 /* Generate new
a1db0 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 record numbers
a1dc0 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f semi-randomly */
a1dd0 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b . Bool nullRow;
a1de0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a1df0 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 if pointing to
a1e00 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 61 a row with no da
a1e10 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 70 73 65 ta */. Bool pse
a1e20 75 64 6f 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a udoTable; /*
a1e30 20 54 68 69 73 20 69 73 20 61 20 4e 45 57 20 6f This is a NEW o
a1e40 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 r OLD pseudo-tab
a1e50 6c 65 73 20 6f 66 20 61 20 74 72 69 67 67 65 72 les of a trigger
a1e60 20 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70 68 65 6d */. Bool ephem
a1e70 50 73 65 75 64 6f 54 61 62 6c 65 3b 0a 20 20 42 PseudoTable;. B
a1e80 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65 ool deferredMove
a1e90 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74 to; /* A call t
a1ea0 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f o sqlite3BtreeMo
a1eb0 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64 veto() is needed
a1ec0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62 */. Bool isTab
a1ed0 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 le; /* T
a1ee0 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72 rue if a table r
a1ef0 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72 equiring integer
a1f00 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 keys */. Bool
a1f10 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 isIndex;
a1f20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69 /* True if an i
a1f30 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ndex containing
a1f40 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 keys only - no d
a1f50 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 ata */. i64 mov
a1f60 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f etoTarget; /
a1f70 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
a1f80 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74 e deferred sqlit
a1f90 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 e3BtreeMoveto()
a1fa0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b */. Btree *pBt;
a1fb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
a1fc0 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64 parate file hold
a1fd0 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
a1fe0 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 ble */. int nDa
a1ff0 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ta; /
a2000 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
a2010 73 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20 20 s in pData */.
a2020 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 char *pData;
a2030 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f /* Data fo
a2040 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 r a NEW or OLD p
a2050 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 seudo-table */.
a2060 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 i64 iKey;
a2070 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66 6f /* Key fo
a2080 72 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 r the NEW or OLD
a2090 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 6f pseudo-table ro
a20a0 77 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a w */. KeyInfo *
a20b0 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 pKeyInfo; /*
a20c0 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 Info about index
a20d0 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20 keys needed by
a20e0 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f index cursors */
a20f0 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 . int nField;
a2100 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
a2110 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
a2120 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
a2130 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 i64 seqCount;
a2140 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 /* Sequenc
a2150 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 e counter */. s
a2160 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
a2170 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
a2180 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
a2190 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 for a virtual ta
a21a0 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ble */. const s
a21b0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
a21c0 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d Module; /* M
a21d0 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 odule for cursor
a21e0 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a pVtabCursor */.
a21f0 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 . /* Result of
a2200 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72 65 last sqlite3Btre
a2210 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20 62 eMoveto() done b
a2220 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 y an OP_NotExist
a2230 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 s or . ** OP_Is
a2240 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e Unique opcode on
a2250 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f this cursor. */
a2260 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c . int seekResul
a2270 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20 t;.. /* Cached
a2280 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
a2290 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72 t the header for
a22a0 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64 the data record
a22b0 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63 that the. ** c
a22c0 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 ursor is current
a22d0 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 ly pointing to.
a22e0 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63 Only valid if c
a22f0 61 63 68 65 56 61 6c 69 64 20 69 73 20 74 72 75 acheValid is tru
a2300 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67 e.. ** aRow mig
a2310 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68 ht point to (eph
a2320 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72 emeral) data for
a2330 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
a2340 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20 , or it might.
a2350 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f ** be NULL.. */
a2360 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74 61 74 . int cacheStat
a2370 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 us; /* Cach
a2380 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 e is valid if th
a2390 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e is matches Vdbe.
a23a0 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e cacheCtr */. in
a23b0 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 t payloadSize;
a23c0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
a23d0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
a23e0 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
a23f0 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 u32 *aType;
a2400 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61 /* Type va
a2410 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74 lues for all ent
a2420 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ries in the reco
a2430 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 rd */. u32 *aOf
a2440 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a fset; /*
a2450 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20 Cached offsets
a2460 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
a2470 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74 each columns dat
a2480 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b a */. u8 *aRow;
a2490 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a24a0 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 Data for the cur
a24b0 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c rent row, if all
a24c0 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a on one page */.
a24d0 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 };.typedef struc
a24e0 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62 t VdbeCursor Vdb
a24f0 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 eCursor;../*.**
a2500 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65 A value for Vdbe
a2510 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 Cursor.cacheVali
a2520 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 d that means the
a2530 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 73 cache is always
a2540 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 invalid..*/.#de
a2550 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45 fine CACHE_STALE
a2560 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 0../*.** Intern
a2570 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d ally, the vdbe m
a2580 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c anipulates nearl
a2590 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 y all SQL values
a25a0 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 as Mem.** struc
a25b0 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 tures. Each Mem
a25c0 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 65 struct may cache
a25d0 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73 multiple repres
a25e0 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e entations (strin
a25f0 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 g,.** integer et
a2600 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20 c.) of the same
a2610 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20 value. A value
a2620 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d (and therefore M
a2630 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a em structure).**
a2640 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 has the followi
a2650 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a ng properties:.*
a2660 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20 *.** Each value
a2670 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74 has a manifest t
a2680 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 ype. The manifes
a2690 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 61 t type of the va
a26a0 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e lue stored.** in
a26b0 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73 a Mem struct is
a26c0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
a26d0 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d MemType(Mem*) m
a26e0 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69 acro. The type i
a26f0 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 s.** one of SQLI
a2700 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
a2710 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f INTEGER, SQLITE_
a2720 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 REAL, SQLITE_TEX
a2730 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 T or.** SQLITE_B
a2740 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d LOB..*/.struct M
a2750 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 em {. union {.
a2760 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 20 i64 i;
a2770 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
a2780 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 r value. */.
a2790 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 int nZero;
a27a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
a27b0 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 bit MEM_Zero is
a27c0 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f set in flags */
a27d0 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 . FuncDef *pD
a27e0 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ef; /* Used
a27f0 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 only when flags
a2800 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 ==MEM_Agg */.
a2810 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 RowSet *pRowSet
a2820 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c ; /* Used onl
a2830 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 y when flags==ME
a2840 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 7d 20 M_RowSet */. }
a2850 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 u;. double r;
a2860 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c /* Real
a2870 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 value */. sqli
a2880 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
a2890 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 /* The associate
a28a0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
a28b0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 ction */. char
a28c0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f *z; /
a28d0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 * String or BLOB
a28e0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
a28f0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a2900 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
a2910 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e racters in strin
a2920 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 g value, excludi
a2930 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 ng '\0' */. u16
a2940 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
a2950 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
a2960 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c tion of MEM_Null
a2970 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 , MEM_Str, MEM_D
a2980 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 yn, etc. */. u8
a2990 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 type;
a29a0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 /* One of SQLI
a29b0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
a29c0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 TEXT, SQLITE_INT
a29d0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 EGER, etc */. u
a29e0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 8 enc;
a29f0 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 /* SQLITE_UTF
a2a00 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 8, SQLITE_UTF16B
a2a10 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
a2a20 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 E */. void (*xD
a2a30 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a el)(void *); /*
a2a40 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 If not null, ca
a2a50 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
a2a60 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a to delete Mem.z
a2a70 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c */. char *zMal
a2a80 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e loc; /* Dyn
a2a90 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f amic buffer allo
a2aa0 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
a2ab0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a _malloc() */.};.
a2ac0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 ./* One or more
a2ad0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
a2ae0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 flags are set t
a2af0 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 o indicate the v
a2b00 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 alidOK.** repres
a2b10 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
a2b20 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
a2b30 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e the Mem struct.
a2b40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 .**.** If the ME
a2b50 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 M_Null flag is s
a2b60 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c et, then the val
a2b70 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c ue is an SQL NUL
a2b80 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f L value..** No o
a2b90 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 ther flags may b
a2ba0 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 e set in this ca
a2bb0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
a2bc0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 MEM_Str flag is
a2bd0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 set then Mem.z
a2be0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 points at a stri
a2bf0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
a2c00 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 n..** Usually th
a2c10 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e is is encoded in
a2c20 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 the same unicod
a2c30 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 e encoding as th
a2c40 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 e main.** databa
a2c50 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f se (see below fo
a2c60 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 r exceptions). I
a2c70 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 f the MEM_Term f
a2c80 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 lag is also.** s
a2c90 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
a2ca0 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 ing is nul termi
a2cb0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 nated. The MEM_I
a2cc0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 nt and MEM_Real
a2cd0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f .** flags may co
a2ce0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d exist with the M
a2cf0 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a EM_Str flag..**.
a2d00 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 ** Multiple of t
a2d10 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 hese values can
a2d20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c appear in Mem.fl
a2d30 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f ags. But only o
a2d40 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 ne.** at a time
a2d50 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 can appear in Me
a2d60 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 m.type..*/.#defi
a2d70 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 ne MEM_Null
a2d80 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 0x0001 /* Val
a2d90 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue is NULL */.#d
a2da0 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 efine MEM_Str
a2db0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 0x0002 /*
a2dc0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e Value is a strin
a2dd0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
a2de0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 _Int 0x000
a2df0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4 /* Value is
a2e00 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 an integer */.#d
a2e10 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 efine MEM_Real
a2e20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 0x0008 /*
a2e30 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 Value is a real
a2e40 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e number */.#defin
a2e50 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 e MEM_Blob
a2e60 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 0x0010 /* Valu
a2e70 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 e is a BLOB */.#
a2e80 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 define MEM_RowSe
a2e90 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a t 0x0020 /*
a2ea0 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53 Value is a RowS
a2eb0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 et object */.#de
a2ec0 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 73 fine MEM_TypeMas
a2ed0 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 4d k 0x00ff /* M
a2ee0 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 73 ask of type bits
a2ef0 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72 */../* Whenever
a2f00 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20 Mem contains a
a2f10 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 20 valid string or
a2f20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 74 blob representat
a2f30 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 ion, one of.** t
a2f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 he following fla
a2f50 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 gs must be set t
a2f60 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
a2f70 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e memory managemen
a2f80 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20 t.** policy for
a2f90 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f Mem.z. The MEM_
a2fa0 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20 Term flag tells
a2fb0 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f us whether or no
a2fc0 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 t the.** string
a2fd0 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 30 is \000 or \u000
a2fe0 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 0 terminated.*/.
a2ff0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d #define MEM_Term
a3000 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 2f 0x0200 /
a3010 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 20 * String rep is
a3020 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 2a nul terminated *
a3030 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79 /.#define MEM_Dy
a3040 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 20 n 0x0400
a3050 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c /* Need to call
a3060 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f 6e sqliteFree() on
a3070 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e Mem.z */.#defin
a3080 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 20 e MEM_Static
a3090 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x0800 /* Mem.
a30a0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 z points to a st
a30b0 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 atic string */.#
a30c0 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d define MEM_Ephem
a30d0 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f 2a 0x1000 /*
a30e0 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem.z points to
a30f0 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 an ephemeral st
a3100 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ring */.#define
a3110 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 78 MEM_Agg 0x
a3120 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 2000 /* Mem.z
a3130 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 67 points to an agg
a3140 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 function contex
a3150 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d t */.#define MEM
a3160 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 30 _Zero 0x400
a3170 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 0 /* Mem.i con
a3180 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30 tains count of 0
a3190 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 6c s appended to bl
a31a0 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 ob */..#ifdef SQ
a31b0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
a31c0 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f OB. #undef MEM_
a31d0 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d Zero. #define M
a31e0 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a 23 EM_Zero 0x0000.#
a31f0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c endif.../*.** Cl
a3200 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67 ear any existing
a3210 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d type flags from
a3220 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61 a Mem and repla
a3230 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a ce them with f.*
a3240 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74 /.#define MemSet
a3250 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c TypeFlag(p, f) \
a3260 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20 . ((p)->flags
a3270 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28 = ((p)->flags&~(
a3280 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d MEM_TypeMask|MEM
a3290 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 _Zero))|f).../*
a32a0 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a 75 A VdbeFunc is ju
a32b0 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 65 st a FuncDef (de
a32c0 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 fined in sqliteI
a32d0 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 61 nt.h) that conta
a32e0 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 ins.** additiona
a32f0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 l information ab
a3300 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 6e out auxiliary in
a3310 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 20 formation bound
a3320 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 to arguments.**
a3330 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e of the function.
a3340 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 This is used t
a3350 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
a3360 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
a3370 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 6c ata().** and sql
a3380 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
a3390 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 61 () APIs. The "a
a33a0 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 20 uxdata" is some
a33b0 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a 2a auxiliary data.*
a33c0 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 73 * that can be as
a33d0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
a33e0 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e constant argumen
a33f0 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e t to a function.
a3400 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 This.** allows
a3410 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 20 functions such
a3420 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 63 as "regexp" to c
a3430 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f 6e ompile their con
a3440 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a 2a stant regular.**
a3450 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 75 expression argu
a3460 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 ment once and re
a3470 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 used the compile
a3480 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 69 d code for multi
a3490 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f ple.** invocatio
a34a0 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 ns..*/.struct Vd
a34b0 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 44 beFunc {. FuncD
a34c0 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 ef *pFunc;
a34d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
a34e0 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 definition of th
a34f0 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
a3500 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 20 int nAux;
a3510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a3520 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
a3530 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
a3540 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 apAux[] */. st
a3550 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a 20 ruct AuxData {.
a3560 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 void *pAux;
a3570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3580 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72 /* Aux data for
a3590 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65 the i-th argume
a35a0 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 nt */. void (
a35b0 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a *xDelete)(void *
a35c0 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 ); /* Destr
a35d0 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75 uctor for the au
a35e0 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 70 x data */. } ap
a35f0 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 Aux[1];
a3600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
a3610 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66 slot for each f
a3620 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 unction argument
a3630 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
a3640 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 75 e "context" argu
a3650 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 61 ment for a insta
a3660 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e 2e llable function.
a3670 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 A pointer to a
a3680 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
a3690 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
a36a0 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 is the first arg
a36b0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f 75 ument to the rou
a36c0 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 6d tines used.** im
a36d0 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 plement the SQL
a36e0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a functions..**.**
a36f0 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 65 There is a type
a3700 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 72 def for this str
a3710 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 65 ucture in sqlite
a3720 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 74 .h. So all rout
a3730 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 ines,.** even th
a3740 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 e public interfa
a3750 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 61 ce to SQLite, ca
a3760 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 20 n use a pointer
a3770 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
a3780 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 66 e..** But this f
a3790 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ile is the only
a37a0 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 20 place where the
a37b0 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73 internal details
a37c0 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75 of this.** stru
a37d0 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e 2e cture are known.
a37e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 .**.** This stru
a37f0 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 64 cture is defined
a3800 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 49 inside of vdbeI
a3810 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 20 nt.h because it
a3820 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 72 uses substructur
a3830 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63 es.** (Mem) whic
a3840 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e h are only defin
a3850 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 ed there..*/.str
a3860 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 uct sqlite3_cont
a3870 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 ext {. FuncDef
a3880 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f 2a *pFunc; /*
a3890 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e 63 Pointer to func
a38a0 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tion information
a38b0 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 . MUST BE FIRST
a38c0 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 2a */. VdbeFunc *
a38d0 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 41 pVdbeFunc; /* A
a38e0 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 66 uxilary data, if
a38f0 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 4d created. */. M
a3900 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 20 em s;
a3910 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 /* The retu
a3920 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 rn value is stor
a3930 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d ed here */. Mem
a3940 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 *pMem;
a3950 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /* Memory cel
a3960 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 l used to store
a3970 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 aggregate contex
a3980 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 t */. int isErr
a3990 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 or; /*
a39a0 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 Error code retur
a39b0 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 74 ned by the funct
a39c0 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 ion. */. CollSe
a39d0 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 q *pColl;
a39e0 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
a39f0 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a uence */.};../*.
a3a00 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 75 ** A Set structu
a3a10 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 71 re is used for q
a3a20 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f 20 uick testing to
a3a30 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a 2a see if a value.*
a3a40 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 73 * is part of a s
a3a50 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 20 mall set. Sets
a3a60 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c are used to impl
a3a70 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 0a ement code like.
a3a80 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 20 ** this:.**
a3a90 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 27 x.y IN ('
a3aa0 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 29 hi','hoo','hum')
a3ab0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
a3ac0 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 75 ct Set Set;.stru
a3ad0 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 20 ct Set {. Hash
a3ae0 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 hash;
a3af0 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a 75 /* A set is ju
a3b00 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 st a hash table
a3b10 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 */. HashElem *p
a3b20 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 rev; /* P
a3b30 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 73 reviously access
a3b40 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 2a ed hash elemen *
a3b50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f /.};../*.** A Co
a3b60 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 ntext stores the
a3b70 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 last insert row
a3b80 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 id, the last sta
a3b90 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f tement change co
a3ba0 75 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 unt,.** and the
a3bb0 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e current statemen
a3bc0 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 t change count (
a3bd0 69 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e i.e. changes sin
a3be0 63 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e ce last statemen
a3bf0 74 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 t)..** The curre
a3c00 6e 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c nt keylist is al
a3c10 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 so stored in the
a3c20 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 context..** Ele
a3c30 6d 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74 ments of Context
a3c40 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20 structure type
a3c50 6d 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74 make up the Cont
a3c60 65 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20 extStack, which
a3c70 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79 is.** updated by
a3c80 20 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68 the ContextPush
a3c90 20 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20 and ContextPop
a3ca0 6f 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79 opcodes (used by
a3cb0 20 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54 triggers)..** T
a3cc0 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75 he context is pu
a3cd0 73 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 shed before exec
a3ce0 75 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20 uting a trigger
a3cf0 61 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68 a popped when th
a3d00 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e e.** trigger fin
a3d10 69 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 ishes..*/.typede
a3d20 66 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 f struct Context
a3d30 20 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 Context;.struct
a3d40 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34 Context {. i64
a3d50 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f lastRowid; /
a3d60 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f * Last insert ro
a3d70 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 wid (sqlite3.las
a3d80 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 tRowid) */. int
a3d90 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f nChange; /
a3da0 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e * Statement chan
a3db0 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 ges (Vdbe.nChang
a3dc0 65 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f es) */.};../
a3dd0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
a3de0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 of the virtual
a3df0 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 machine. This s
a3e00 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
a3e10 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a s the complete.*
a3e20 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 * state of the v
a3e30 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a irtual machine..
a3e40 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 **.** The "sqlit
a3e50 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 e3_stmt" structu
a3e60 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 re pointer that
a3e70 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 is returned by s
a3e80 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 qlite3_compile()
a3e90 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 .** is really a
a3ea0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
a3eb0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
a3ec0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
a3ed0 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d The Vdbe.inVtabM
a3ee0 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 ethod variable i
a3ef0 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 s set to non-zer
a3f00 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 o for the durati
a3f10 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 on of.** any vir
a3f20 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f tual table metho
a3f30 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 d invocations ma
a3f40 64 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70 de by the vdbe p
a3f50 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a rogram. It is.**
a3f60 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 set to 2 for xD
a3f70 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 estroy method ca
a3f80 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c lls and 1 for al
a3f90 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e l other methods.
a3fa0 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c This.** variabl
a3fb0 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 e is used for tw
a3fc0 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 o purposes: to a
a3fd0 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 llow xDestroy me
a3fe0 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 thods to execute
a3ff0 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 .** "DROP TABLE"
a4000 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 statements and
a4010 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 to prevent some
a4020 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63 nasty side effec
a4030 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 ts of.** malloc
a4040 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c failure when SQL
a4050 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 ite is invoked r
a4060 65 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 ecursively by a
a4070 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a virtual table .*
a4080 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f * method functio
a4090 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 n..*/.struct Vdb
a40a0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 e {. sqlite3 *d
a40b0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
a40c0 20 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 whole database
a40d0 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76 */. Vdbe *pPrev
a40e0 2c 2a 70 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b ,*pNext; /* Link
a40f0 65 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 ed list of VDBEs
a4100 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 with the same V
a4110 64 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 dbe.db */. int
a4120 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 nOp;
a4130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 /* Number of ins
a4140 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 tructions in the
a4150 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e program */. in
a4160 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 t nOpAlloc;
a4170 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
a4180 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 lots allocated f
a4190 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 or aOp[] */. Op
a41a0 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 *aOp;
a41b0 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f /* Space to ho
a41c0 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d ld the virtual m
a41d0 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d achine's program
a41e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c */. int nLabel
a41f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
a4200 62 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 ber of labels us
a4210 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 ed */. int nLab
a4220 65 6c 41 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e elAlloc; /* N
a4230 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 umber of slots a
a4240 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c 61 62 llocated in aLab
a4250 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 el[] */. int *a
a4260 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a Label; /*
a4270 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
a4280 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d he labels */. M
a4290 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 em **apArg;
a42a0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 /* Arguments
a42b0 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 to currently exe
a42c0 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63 cuting user func
a42d0 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 tion */. Mem *a
a42e0 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a ColName; /*
a42f0 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f Column names to
a4300 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 return */. int
a4310 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 nCursor;
a4320 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
a4330 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a ots in apCsr[] *
a4340 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
a4350 2a 61 70 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 *apCsr; /* One e
a4360 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 lement of this a
a4370 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 rray for each op
a4380 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 en cursor */. i
a4390 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 nt nVar;
a43a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
a43b0 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b entries in aVar[
a43c0 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 ] */. Mem *aVar
a43d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 ; /* Va
a43e0 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f lues for the OP_
a43f0 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e Variable opcode.
a4400 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 */. char **azV
a4410 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d ar; /* Nam
a4420 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a e of variables *
a4430 2f 0a 20 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20 /. int okVar;
a4440 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a4450 69 66 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 if azVar[] has b
a4460 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 een initialized
a4470 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 */. u32 magic;
a4480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a4490 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 Magic number for
a44a0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
a44b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 */. int nMem;
a44c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a44d0 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 Number of memor
a44e0 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 y locations curr
a44f0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ently allocated
a4500 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 */. Mem *aMem;
a4510 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a4520 54 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 The memory locat
a4530 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 63 61 ions */. int ca
a4540 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 cheCtr;
a4550 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 /* VdbeCursor
a4560 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 row cache genera
a4570 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a tion counter */.
a4580 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 int contextSta
a4590 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64 ckTop; /* Ind
a45a0 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e ex of top elemen
a45b0 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 t in the context
a45c0 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 stack */. int
a45d0 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 contextStackDept
a45e0 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 h; /* The size
a45f0 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22 of the "context"
a4600 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 stack */. Cont
a4610 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63 ext *contextStac
a4620 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65 k; /* Stack use
a4630 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e d by opcodes Con
a4640 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65 textPush & Conte
a4650 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63 xtPop*/. int pc
a4660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a4670 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d /* The program
a4680 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
a4690 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
a46a0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 /* Value t
a46b0 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e o return */. in
a46c0 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 t errorAction;
a46d0 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 /* Recover
a46e0 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 y action to do i
a46f0 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 n case of an err
a4700 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 or */. int nRes
a4710 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 Column;
a4720 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c /* Number of col
a4730 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20 umns in one row
a4740 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
a4750 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a t */. char **az
a4760 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f ResColumn; /
a4770 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 6f 6e 65 * Values for one
a4780 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a row of result *
a4790 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d / . char *zErrM
a47a0 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sg; /*
a47b0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 Error message wr
a47c0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
a47d0 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 3b Mem *pResultSet;
a47e0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
a47f0 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f er to an array o
a4800 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75 f results */. u
a4810 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
a4820 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a4830 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e f EXPLAIN presen
a4840 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 t on SQL command
a4850 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 */. u8 changeC
a4860 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a ntOn; /*
a4870 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20 True to update
a4880 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
a4890 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72 er */. u8 expir
a48a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
a48b0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56 /* True if the V
a48c0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 M needs to be re
a48d0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38 compiled */. u8
a48e0 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 minWriteFileFor
a48f0 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d mat; /* Minimum
a4900 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
a4910 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61 writable databa
a4920 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 se files */. u8
a4930 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20 inVtabMethod;
a4940 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d /* See com
a4950 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 ments above */.
a4960 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 u8 usesStmtJour
a4970 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 nal; /* True
a4980 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65 if uses a state
a4990 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a ment journal */.
a49a0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 u8 readOnly;
a49b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a49c0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 e for read-only
a49d0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 statements */.
a49e0 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20 u8 isPrepareV2;
a49f0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a4a00 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68 if prepared with
a4a10 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f prepare_v2() */
a4a20 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
a4a30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a4a40 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67 mber of db chang
a4a50 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 es made since la
a4a60 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36 st reset */. i6
a4a70 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 4 startTime;
a4a80 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 /* Time wh
a4a90 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64 en query started
a4aa0 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 - used for prof
a4ab0 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 iling */. int b
a4ac0 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 treeMask;
a4ad0 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 /* Bitmask of
a4ae0 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 db->aDb[] entri
a4af0 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f es referenced */
a4b00 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 . BtreeMutexArr
a4b10 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e ay aMutex; /* An
a4b20 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20 array of Btree
a4b30 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65 used here and ne
a4b40 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 eding locks */.
a4b50 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d int aCounter[2]
a4b60 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
a4b70 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c ters used by sql
a4b80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
a4b90 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 () */. char *zS
a4ba0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ql; /*
a4bb0 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c Text of the SQL
a4bc0 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
a4bd0 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20 2a generated this *
a4be0 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 65 3b /. void *pFree;
a4bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
a4c00 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 64 65 ree this when de
a4c10 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 65 20 leting the vdbe
a4c20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
a4c30 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 _DEBUG. FILE *t
a4c40 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 2f race; /
a4c50 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75 * Write an execu
a4c60 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c tion trace here,
a4c70 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a if not NULL */.
a4c80 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 53 74 #endif. int iSt
a4c90 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 atement;
a4ca0 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 /* Statement nu
a4cb0 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 mber (or 0 if ha
a4cc0 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d s not opened stm
a4cd0 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c t) */.#ifdef SQL
a4ce0 49 54 45 5f 53 53 45 0a 20 20 69 6e 74 20 66 65 ITE_SSE. int fe
a4cf0 74 63 68 49 64 3b 20 20 20 20 20 20 20 20 20 20 tchId;
a4d00 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d /* Statement num
a4d10 62 65 72 20 75 73 65 64 20 62 79 20 73 71 6c 69 ber used by sqli
a4d20 74 65 33 5f 66 65 74 63 68 5f 73 74 61 74 65 6d te3_fetch_statem
a4d30 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 72 75 ent */. int lru
a4d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a4d50 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20 66 * Counter used f
a4d60 6f 72 20 4c 52 55 20 63 61 63 68 65 20 72 65 70 or LRU cache rep
a4d70 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64 lacement */.#end
a4d80 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
a4d90 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
a4da0 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56 64 62 65 ANAGEMENT. Vdbe
a4db0 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20 20 56 64 *pLruPrev;. Vd
a4dc0 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b 0a 23 65 be *pLruNext;.#e
a4dd0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 ndif.};../*.** T
a4de0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
a4df0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 allowed values
a4e00 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a for Vdbe.magic.*
a4e10 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d /.#define VDBE_M
a4e20 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78 AGIC_INIT 0x
a4e30 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42 26bceaa5 /* B
a4e40 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70 uilding a VDBE p
a4e50 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e rogram */.#defin
a4e60 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e e VDBE_MAGIC_RUN
a4e70 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33 0xbdf20da3
a4e80 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72 /* VDBE is r
a4e90 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20 eady to execute
a4ea0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
a4eb0 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30 MAGIC_HALT 0
a4ec0 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20 x519c2973 /*
a4ed0 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74 VDBE has complet
a4ee0 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a ed execution */.
a4ef0 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 #define VDBE_MAG
a4f00 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36 IC_DEAD 0xb6
a4f10 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65 06c3c8 /* The
a4f20 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64 VDBE has been d
a4f30 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f eallocated */../
a4f40 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72 *.** Function pr
a4f50 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 ototypes.*/.SQLI
a4f60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a4f70 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
a4f80 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64 ursor(Vdbe *, Vd
a4f90 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 beCursor*);.void
a4fa0 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74 sqliteVdbePopSt
a4fb0 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a ack(Vdbe*,int);.
a4fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a4fd0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 nt sqlite3VdbeCu
a4fe0 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 rsorMoveto(VdbeC
a4ff0 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 ursor*);.#if def
a5000 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
a5010 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 G) || defined(VD
a5020 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 BE_PROFILE).SQLI
a5030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a5040 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
a5050 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f Op(FILE*, int, O
a5060 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 p*);.#endif.SQLI
a5070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5080 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
a5090 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 TypeLen(u32);.SQ
a50a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
a50b0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
a50c0 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 alType(Mem*, int
a50d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a50e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a50f0 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67 eSerialPut(unsig
a5100 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 ned char*, int,
a5110 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
a5120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5130 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
a5140 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e Get(const unsign
a5150 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d ed char*, u32, M
a5160 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a5170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a5180 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
a5190 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e ta(VdbeFunc*, in
a51a0 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 t);..int sqlite2
a51b0 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 BtreeKeyCompare(
a51c0 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 BtCursor *, cons
a51d0 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 t void *, int, i
a51e0 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 nt, int *);.SQLI
a51f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5200 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
a5210 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73 Compare(VdbeCurs
a5220 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f or*,UnpackedReco
a5230 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 rd*,int*);.SQLIT
a5240 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a5250 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
a5260 64 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 d(BtCursor *, i6
a5270 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 4 *);.SQLITE_PRI
a5280 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a5290 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 MemCompare(const
a52a0 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d Mem*, const Mem
a52b0 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 *, const CollSeq
a52c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
a52d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
a52e0 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 beExec(Vdbe*);.S
a52f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a5300 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 t sqlite3VdbeLis
a5310 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 t(Vdbe*);.SQLITE
a5320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a5330 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 ite3VdbeHalt(Vdb
a5340 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
a5350 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a5360 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
a5370 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 g(Mem *, int);.S
a5380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a5390 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
a53a0 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 TooBig(Mem*);.SQ
a53b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a53c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 sqlite3VdbeMemC
a53d0 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 opy(Mem*, const
a53e0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
a53f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a5400 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
a5410 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 Copy(Mem*, const
a5420 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Mem*, int);.SQL
a5430 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a5440 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
a5450 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b ove(Mem*, Mem*);
a5460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a5470 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
a5480 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d emNulTerminate(M
a5490 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a54a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a54b0 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 VdbeMemSetStr(Me
a54c0 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c m*, const char*,
a54d0 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a int, u8, void(*
a54e0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
a54f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a5500 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
a5510 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 Int64(Mem*, i64)
a5520 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a5530 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
a5540 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 eMemSetDouble(Me
a5550 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c m*, double);.SQL
a5560 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a5570 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
a5580 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 etNull(Mem*);.SQ
a5590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a55a0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
a55b0 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a SetZeroBlob(Mem*
a55c0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
a55d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a55e0 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
a55f0 65 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 et(Mem*);.SQLITE
a5600 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a5610 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
a5620 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a riteable(Mem*);.
a5630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a5640 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a5650 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c mStringify(Mem*,
a5660 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
a5670 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 IVATE i64 sqlite
a5680 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 3VdbeIntValue(Me
a5690 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
a56a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a56b0 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
a56c0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
a56d0 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 RIVATE double sq
a56e0 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
a56f0 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ue(Mem*);.SQLITE
a5700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a5710 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
a5720 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a Affinity(Mem*);.
a5730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a5740 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a5750 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a mRealify(Mem*);.
a5760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a5770 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a5780 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b mNumerify(Mem*);
a5790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a57a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
a57b0 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75 emFromBtree(BtCu
a57c0 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e rsor*,int,int,in
a57d0 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f t,Mem*);.SQLITE_
a57e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a57f0 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
a5800 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 se(Mem *p);.SQLI
a5810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a5820 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
a5830 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
a5840 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 m *p);.SQLITE_PR
a5850 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a5860 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
a5870 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 (Mem*, FuncDef*)
a5880 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a5890 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
a58a0 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 ite3OpcodeName(i
a58b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
a58c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a58d0 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 dbeOpcodeHasProp
a58e0 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a erty(int, int);.
a58f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a5900 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
a5910 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c mGrow(Mem *pMem,
a5920 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 int n, int pres
a5930 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 erve);.SQLITE_PR
a5940 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a5950 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 3VdbeCloseStatem
a5960 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 ent(Vdbe *, int)
a5970 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
a5980 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
a5990 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f NAGEMENT.SQLITE_
a59a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a59b0 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 te3VdbeReleaseBu
a59c0 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a ffers(Vdbe *p);.
a59d0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
a59e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
a59f0 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f ED_CACHE.SQLITE_
a5a00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a5a10 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 ite3VdbeMutexArr
a5a20 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 ayEnter(Vdbe *p)
a5a30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
a5a40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 sqlite3VdbeMute
a5a50 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23 xArrayEnter(p).#
a5a60 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
a5a70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a5a80 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
a5a90 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 e(Mem*, u8);.#if
a5aa0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
a5ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a5ac0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
a5ad0 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a bePrintSql(Vdbe*
a5ae0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a5af0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
a5b00 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 VdbeMemPrettyPri
a5b10 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 nt(Mem *pMem, ch
a5b20 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 ar *zBuf);.#endi
a5b30 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
a5b40 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
a5b50 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d MemHandleBom(Mem
a5b60 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 *pMem);..#ifnde
a5b70 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
a5b80 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 CRBLOB.SQLITE_PR
a5b90 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
a5ba0 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
a5bb0 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c Blob(Mem *);.#el
a5bc0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
a5bd0 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
a5be0 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f dBlob(x) SQLITE_
a5bf0 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 OK.#endif..#endi
a5c00 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 f /* !defined(_V
a5c10 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f DBEINT_H_) */../
a5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
a5c30 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 nd of vdbeInt.h
a5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
a5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
a5c80 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
a5c90 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 we left off in u
a5ca0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tf.c ***********
a5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
a5cc0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 #ifndef SQLITE_A
a5cd0 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a MALGAMATION./*.*
a5ce0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
a5cf0 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 constant value i
a5d00 73 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51 s used by the SQ
a5d10 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 LITE_BIGENDIAN a
a5d20 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 nd.** SQLITE_LIT
a5d30 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 TLEENDIAN macros
a5d40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a5d50 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 ATE const int sq
a5d60 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 lite3one = 1;.#e
a5d70 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 ndif /* SQLITE_A
a5d80 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a MALGAMATION */..
a5d90 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 /*.** This looku
a5da0 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 p table is used
a5db0 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 to help decode t
a5dc0 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 he first byte of
a5dd0 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 .** a multi-byte
a5de0 20 55 54 46 38 20 63 68 61 72 61 63 74 65 72 2e UTF8 character.
a5df0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
a5e00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 unsigned char s
a5e10 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 qlite3Utf8Trans1
a5e20 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 [] = {. 0x00, 0
a5e30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c x01, 0x02, 0x03,
a5e40 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 0x04, 0x05, 0x0
a5e50 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 6, 0x07,. 0x08,
a5e60 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 0x09, 0x0a, 0x0
a5e70 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 b, 0x0c, 0x0d, 0
a5e80 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 x0e, 0x0f,. 0x1
a5e90 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 0, 0x11, 0x12, 0
a5ea0 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c x13, 0x14, 0x15,
a5eb0 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 0x16, 0x17,. 0
a5ec0 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c x18, 0x19, 0x1a,
a5ed0 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 0x1b, 0x1c, 0x1
a5ee0 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 d, 0x1e, 0x1f,.
a5ef0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
a5f00 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 2, 0x03, 0x04, 0
a5f10 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c x05, 0x06, 0x07,
a5f20 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 . 0x08, 0x09, 0
a5f30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c x0a, 0x0b, 0x0c,
a5f40 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 0x0d, 0x0e, 0x0
a5f50 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c f,. 0x00, 0x01,
a5f60 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 0x02, 0x03, 0x0
a5f70 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 4, 0x05, 0x06, 0
a5f80 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 x07,. 0x00, 0x0
a5f90 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 1, 0x02, 0x03, 0
a5fa0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
a5fb0 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 0x00,.};...#def
a5fc0 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a ine WRITE_UTF8(z
a5fd0 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 Out, c) {
a5fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5ff0 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 \. if( c<0x0
a6000 30 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 0080 ){
a6010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6020 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6030 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
a6040 28 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 (c&0xFF);
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6060 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
a6070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a60a0 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 else if( c<0x0
a60b0 30 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 0800 ){
a60c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a60d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
a60e0 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 ut++ = 0xC0 + (u
a60f0 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 8)((c>>6)&0x1F);
a6100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6110 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
a6120 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 0x80 + (u8)(c &
a6130 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 0x3F);
a6140 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 \. }
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6180 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 \. else if( c
a6190 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 <0x10000 ){
a61a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a61b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a61c0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 *zOut++ = 0xE0
a61d0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 + (u8)((c>>12)&0
a61e0 78 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x0F);
a61f0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
a6200 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 + = 0x80 + (u8)(
a6210 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 (c>>6) & 0x3F);
a6220 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6230 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
a6240 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 0 + (u8)(c & 0x3
a6250 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
a6260 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b \. }else{
a6270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a62a0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
a62b0 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 xF0 + (u8)((c>>1
a62c0 38 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 8) & 0x07);
a62d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
a62e0 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 Out++ = 0x80 + (
a62f0 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 u8)((c>>12) & 0x
a6300 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
a6310 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
a6320 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 0x80 + (u8)((c>
a6330 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 >6) & 0x3F);
a6340 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
a6350 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
a6360 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b (u8)(c & 0x3F);
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6380 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
a6390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
a63c0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 .#define WRITE_U
a63d0 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 TF16LE(zOut, c)
a63e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a63f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6400 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d \. if( c<=
a6410 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 0xFFFF ){
a6420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a6450 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
a6460 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 c&0x00FF);
a6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6490 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
a64a0 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 (u8)((c>>8)&0x0
a64b0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 0FF);
a64c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a64d0 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 \. }else
a64e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6510 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6520 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
a6530 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 )(((c>>10)&0x003
a6540 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 F) + (((c-0x1000
a6550 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 0)>>10)&0x00C0))
a6560 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b ; \. *zOut++
a6570 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 2b = (u8)(0x00D8 +
a6580 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e (((c-0x10000)>>
a6590 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 18)&0x03));
a65a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
a65b0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 zOut++ = (u8)(c&
a65c0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
a65d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a65e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a65f0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
a6600 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 u8)(0x00DC + ((c
a6610 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 >>8)&0x03));
a6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6630 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
a6640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6670 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a \.}..
a6680 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 #define WRITE_UT
a6690 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b F16BE(zOut, c) {
a66a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66c0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 \. if( c<=0
a66d0 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 xFFFF ){
a66e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
a6710 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 *zOut++ = (u8)((
a6720 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 c>>8)&0x00FF);
a6730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6750 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
a6760 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 (u8)(c&0x00FF);
a6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6790 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b \. }else{
a67a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a67e0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
a67f0 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 (0x00D8 + (((c-0
a6800 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 x10000)>>18)&0x0
a6810 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3));
a6820 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
a6830 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 = (u8)(((c>>10)&
a6840 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 0x003F) + (((c-0
a6850 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 x10000)>>10)&0x0
a6860 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 0C0)); \. *z
a6870 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 Out++ = (u8)(0x0
a6880 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 0DC + ((c>>8)&0x
a6890 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 03));
a68a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a68b0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
a68c0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
a68d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a68f0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
a6900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6930 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 \.}..#
a6940 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 define READ_UTF1
a6950 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 6LE(zIn, c){
a6960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6980 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a \. c = (*z
a6990 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 In++);
a69a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a69b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a69d0 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c c += ((*zIn++)<
a69e0 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 <8);
a69f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a10 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d \. if( c>=
a6a20 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 0xD800 && c<0xE0
a6a30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 00 ){
a6a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6a60 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 int c2 = (*zI
a6a70 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
a6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6aa0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d \. c2 +=
a6ab0 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 ((*zIn++)<<8);
a6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6af0 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 c = (c2&0x03F
a6b00 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 F) + ((c&0x003F)
a6b10 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 <<10) + (((c&0x0
a6b20 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 3C0)+0x0040)<<10
a6b30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 ); \. }
a6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
a6b80 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 ..#define READ_U
a6b90 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 TF16BE(zIn, c){
a6ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bc0 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
a6bd0 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 ((*zIn++)<<8);
a6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c10 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b \. c += (*zIn++
a6c20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
a6c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c50 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
a6c60 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 c>=0xD800 && c<0
a6c70 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 xE000 ){
a6c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ca0 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 \. int c2 = (
a6cb0 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 (*zIn++)<<8);
a6cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ce0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 \. c2
a6cf0 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 += (*zIn++);
a6d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d30 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 \. c = (c2&0x
a6d40 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 03FF) + ((c&0x00
a6d50 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 3F)<<10) + (((c&
a6d60 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 0x03C0)+0x0040)<
a6d70 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 <10); \. }
a6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6dc0 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 \.}../*.** Trans
a6dd0 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 late a single UT
a6de0 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 F-8 character.
a6df0 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f Return the unico
a6e00 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 de value..**.**
a6e10 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 During translati
a6e20 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 on, assume that
a6e30 74 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 the byte that zT
a6e40 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 erm points.** is
a6e50 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 a 0x00..**.** W
a6e60 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 rite a pointer t
a6e70 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 o the next unrea
a6e80 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f d byte back into
a6e90 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 *pzNext..**.**
a6ea0 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 Notes On Invalid
a6eb0 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a UTF-8:.**.** *
a6ec0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e This routine n
a6ed0 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d ever allows a 7-
a6ee0 62 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30 bit character (0
a6ef0 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 x00 through 0x7f
a6f00 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 ) to.** be e
a6f10 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 ncoded as a mult
a6f20 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 i-byte character
a6f30 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 . Any multi-byt
a6f40 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 e character that
a6f50 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 .** attempts
a6f60 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c to encode a val
a6f70 75 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 ue between 0x00
a6f80 61 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 and 0x7f is rend
a6f90 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a ered as 0xfffd..
a6fa0 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 **.** * This r
a6fb0 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c outine never all
a6fc0 6f 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72 ows a UTF16 surr
a6fd0 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 ogate value to b
a6fe0 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 e encoded..**
a6ff0 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 If a multi-byt
a7000 65 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65 e character atte
a7010 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 mpts to encode a
a7020 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a value between.*
a7030 2a 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 * 0xd800 and
a7040 20 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 0xe000 then it
a7050 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 is rendered as 0
a7060 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 xfffd..**.** *
a7070 20 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 Bytes in the ra
a7080 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f nge of 0x80 thro
a7090 75 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f ugh 0xbf which o
a70a0 63 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73 ccur as the firs
a70b0 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 t.** byte of
a70c0 20 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65 a character are
a70d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
a70e0 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 single-byte char
a70f0 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e acters.** an
a7100 64 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 d rendered as th
a7110 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 emselves even th
a7120 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65 ough they are te
a7130 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 chnically.**
a7140 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 invalid charact
a7150 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 ers..**.** * T
a7160 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 his routine acce
a7170 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 pts an infinite
a7180 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 number of differ
a7190 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e ent UTF8 encodin
a71a0 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e gs.** for un
a71b0 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 icode values 0x8
a71c0 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 0 and greater.
a71d0 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 It do not change
a71e0 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 over-length.**
a71f0 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f encodings to
a7200 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 0xfffd as some
a7210 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e systems recommen
a7220 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 d..*/.#define RE
a7230 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 AD_UTF8(zIn, zTe
a7240 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 rm, c)
a7250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7260 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b \. c = *(zIn++
a7270 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
a7280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a72a0 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b if( c>=0xc0 ){
a72b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a72c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a72d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a72e0 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c = sqlite3Utf8
a72f0 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 Trans1[c-0xc0];
a7300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7310 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 \. wh
a7320 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 ile( zIn!=zTerm
a7330 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 && (*zIn & 0xc0)
a7340 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 ==0x80 ){
a7350 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d \. c =
a7360 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 (c<<6) + (0x3f
a7370 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 & *(zIn++));
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7390 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 \. }
a73a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a73d0 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 . if( c<0x80
a73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7400 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a7410 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 || (c&0xFF
a7420 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 FFF800)==0xD800
a7430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7440 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 \.
a7450 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 || (c&0xFFFFF
a7460 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 FFE)==0xFFFE ){
a7470 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 c = 0xFFFD; }
a7480 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 \. }.SQLI
a7490 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a74a0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a qlite3Utf8Read(.
a74b0 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
a74c0 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 char *zIn,
a74d0 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
a74e0 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 of UTF-8 charact
a74f0 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e er */. const un
a7500 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a signed char **pz
a7510 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 Next /* Write
a7520 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 first byte past
a7530 20 55 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 UTF-8 char here
a7540 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a */.){. int c;.
a7550 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 . /* Same as RE
a7560 41 44 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20 AD_UTF8() above
a7570 62 75 74 20 77 69 74 68 6f 75 74 20 74 68 65 20 but without the
a7580 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e zTerm parameter.
a7590 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 . ** For this r
a75a0 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d outine, we assum
a75b0 65 20 74 68 65 20 55 54 46 38 20 73 74 72 69 6e e the UTF8 strin
a75c0 67 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f g is always zero
a75d0 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a -terminated.. *
a75e0 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 /. c = *(zIn++)
a75f0 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 ;. if( c>=0xc0
a7600 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 ){. c = sqlit
a7610 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 e3Utf8Trans1[c-0
a7620 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 xc0];. while(
a7630 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d (*zIn & 0xc0)==
a7640 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20 0x80 ){. c
a7650 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 = (c<<6) + (0x3f
a7660 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 & *(zIn++));.
a7670 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 }. if( c<0x
a7680 38 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 80. || (c
a7690 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 &0xFFFFF800)==0x
a76a0 44 38 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 D800. ||
a76b0 28 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d (c&0xFFFFFFFE)==
a76c0 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 0xFFFE ){ c = 0
a76d0 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a xFFFD; }. }. *
a76e0 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 pzNext = zIn;.
a76f0 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a return c;.}.....
a7700 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 /*.** If the TRA
a7710 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 NSLATE_TRACE mac
a7720 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 ro is defined, t
a7730 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 he value of each
a7740 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 Mem is.** print
a7750 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 ed on stderr on
a7760 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 the way into and
a7770 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 out of sqlite3V
a7780 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 dbeMemTranslate(
a7790 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e )..*/ ./* #defin
a77a0 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 e TRANSLATE_TRAC
a77b0 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 E 1 */..#ifndef
a77c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
a77d0 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 6./*.** This rou
a77e0 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 tine transforms
a77f0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 the internal tex
a7800 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 t encoding used
a7810 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 by pMem to.** de
a7820 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 siredEnc. It is
a7830 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 an error if the
a7840 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 string is alread
a7850 79 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 y of the desired
a7860 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 .** encoding, or
a7870 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e if *pMem does n
a7880 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 ot contain a str
a7890 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ing value..*/.SQ
a78a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a78b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
a78c0 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d ranslate(Mem *pM
a78d0 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e em, u8 desiredEn
a78e0 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 c){. int len;
a78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7900 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e /* Maximum len
a7910 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 gth of output st
a7920 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f ring in bytes */
a7930 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a7940 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 *zOut;
a7950 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
a7960 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 ut buffer */. u
a7970 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 nsigned char *zI
a7980 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a7990 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 /* Input it
a79a0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 erator */. unsi
a79b0 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d gned char *zTerm
a79c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a79d0 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 /* End of inpu
a79e0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 t */. unsigned
a79f0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
a7a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a7a10 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 Output iterator
a7a20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
a7a30 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 t c;.. assert(
a7a40 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
a7a50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
a7a60 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
a7a70 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
a7a80 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
a7a90 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Str );. assert(
a7aa0 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 pMem->enc!=desi
a7ab0 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 redEnc );. asse
a7ac0 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 rt( pMem->enc!=0
a7ad0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
a7ae0 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 em->n>=0 );..#if
a7af0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 defined(TRANSLA
a7b00 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 TE_TRACE) && def
a7b10 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
a7b20 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 G). {. char
a7b30 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 zBuf[100];. s
a7b40 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 qlite3VdbeMemPre
a7b50 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a ttyPrint(pMem, z
a7b60 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 Buf);. fprint
a7b70 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 f(stderr, "INPUT
a7b80 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b : %s\n", zBuf);
a7b90 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
a7ba0 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 * If the transla
a7bb0 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 tion is between
a7bc0 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e UTF-16 little an
a7bd0 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 d big endian, th
a7be0 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 en . ** all tha
a7bf0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73 t is required is
a7c00 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 to swap the byt
a7c10 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 e order. This ca
a7c20 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 se is handled.
a7c30 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 ** differently f
a7c40 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a rom the others..
a7c50 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d */. if( pMem-
a7c60 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 >enc!=SQLITE_UTF
a7c70 38 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 8 && desiredEnc!
a7c80 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a =SQLITE_UTF8 ){.
a7c90 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 u8 temp;.
a7ca0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 int rc;. rc
a7cb0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
a7cc0 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d MakeWriteable(pM
a7cd0 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 em);. if( rc!
a7ce0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
a7cf0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
a7d00 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
a7d10 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
a7d20 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
a7d30 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 . zIn = (u8*)
a7d40 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 pMem->z;. zTe
a7d50 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e rm = &zIn[pMem->
a7d60 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 n&~1];. while
a7d70 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
a7d80 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e temp = *zIn
a7d90 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a ;. *zIn = *
a7da0 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a (zIn+1);. z
a7db0 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e In++;. *zIn
a7dc0 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d ++ = temp;. }
a7dd0 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d . pMem->enc =
a7de0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 desiredEnc;.
a7df0 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f goto translate_
a7e00 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 out;. }.. /* S
a7e10 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 et len to the ma
a7e20 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
a7e30 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69 bytes required i
a7e40 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 n the output buf
a7e50 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 fer. */. if( de
a7e60 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
a7e70 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 _UTF8 ){. /*
a7e80 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 When converting
a7e90 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 from UTF-16, the
a7ea0 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 maximum growth
a7eb0 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 results from.
a7ec0 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 ** translating
a7ed0 61 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 a 2-byte charact
a7ee0 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 er to a 4-byte U
a7ef0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a TF-8 character..
a7f00 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 ** A single
a7f10 62 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64 byte is required
a7f20 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 for the output
a7f30 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 string. ** nu
a7f40 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 l-terminator..
a7f50 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e */. pMem->n
a7f60 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 &= ~1;. len
a7f70 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 = pMem->n * 2 +
a7f80 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
a7f90 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 /* When converti
a7fa0 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f ng from UTF-8 to
a7fb0 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 UTF-16 the maxi
a7fc0 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 mum growth is ca
a7fd0 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e used. ** when
a7fe0 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 a 1-byte UTF-8
a7ff0 63 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61 character is tra
a8000 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 nslated into a 2
a8010 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 -byte UTF-16.
a8020 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 ** character. T
a8030 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71 wo bytes are req
a8040 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 uired in the out
a8050 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 put buffer for t
a8060 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 he. ** nul-te
a8070 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f rminator.. */
a8080 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d . len = pMem-
a8090 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a >n * 2 + 2;. }.
a80a0 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f . /* Set zIn to
a80b0 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 point at the st
a80c0 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 art of the input
a80d0 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 buffer and zTer
a80e0 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a m to point 1. *
a80f0 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 * byte past the
a8100 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 end.. **. ** V
a8110 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 ariable zOut is
a8120 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 set to point at
a8130 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
a8140 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 r, space obtaine
a8150 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 d. ** from sqli
a8160 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 te3_malloc()..
a8170 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 */. zIn = (u8*)
a8180 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d pMem->z;. zTerm
a8190 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d = &zIn[pMem->n]
a81a0 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 ;. zOut = sqlit
a81b0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d e3DbMallocRaw(pM
a81c0 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 em->db, len);.
a81d0 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 if( !zOut ){.
a81e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
a81f0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 OMEM;. }. z =
a8200 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 zOut;.. if( pMe
a8210 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 m->enc==SQLITE_U
a8220 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 TF8 ){. if( d
a8230 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 esiredEnc==SQLIT
a8240 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 E_UTF16LE ){.
a8250 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 /* UTF-8 -> U
a8260 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 TF-16 Little-end
a8270 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 ian */. whi
a8280 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
a8290 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 . /* c =
a82a0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
a82b0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e zIn, zTerm, (con
a82c0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a st u8**)&zIn); *
a82d0 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 /. READ_U
a82e0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 TF8(zIn, zTerm,
a82f0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 c);. WRIT
a8300 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b E_UTF16LE(z, c);
a8310 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
a8320 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
a8330 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 ( desiredEnc==SQ
a8340 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a LITE_UTF16BE );.
a8350 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d /* UTF-8 -
a8360 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 > UTF-16 Big-end
a8370 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 ian */. whi
a8380 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
a8390 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 . /* c =
a83a0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
a83b0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e zIn, zTerm, (con
a83c0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a st u8**)&zIn); *
a83d0 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 /. READ_U
a83e0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 TF8(zIn, zTerm,
a83f0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 c);. WRIT
a8400 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b E_UTF16BE(z, c);
a8410 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
a8420 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e pMem->n = (in
a8430 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 t)(z - zOut);.
a8440 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 *z++ = 0;. }e
a8450 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
a8460 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
a8470 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 ITE_UTF8 );.
a8480 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 if( pMem->enc==S
a8490 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b QLITE_UTF16LE ){
a84a0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 . /* UTF-16
a84b0 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d Little-endian -
a84c0 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 > UTF-8 */.
a84d0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 while( zIn<zTer
a84e0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 m ){. REA
a84f0 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 D_UTF16LE(zIn, c
a8500 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 ); . WRIT
a8510 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 E_UTF8(z, c);.
a8520 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
a8530 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 . /* UTF-16
a8540 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 Big-endian -> U
a8550 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 TF-8 */. wh
a8560 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
a8570 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 {. READ_U
a8580 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 TF16BE(zIn, c);
a8590 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
a85a0 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 TF8(z, c);.
a85b0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 }. }. pMe
a85c0 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d m->n = (int)(z -
a85d0 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a zOut);. }. *z
a85e0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
a85f0 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 (pMem->n+(desire
a8600 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
a8610 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 8?1:2))<=len );.
a8620 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
a8630 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
a8640 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d pMem->flags &=
a8650 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 ~(MEM_Static|ME
a8660 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 M_Dyn|MEM_Ephem)
a8670 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
a8680 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d desiredEnc;. pM
a8690 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 em->flags |= (ME
a86a0 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b M_Term|MEM_Dyn);
a86b0 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 . pMem->z = (ch
a86c0 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d ar*)zOut;. pMem
a86d0 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d ->zMalloc = pMem
a86e0 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f ->z;..translate_
a86f0 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 out:.#if defined
a8700 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 (TRANSLATE_TRACE
a8710 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
a8720 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 ITE_DEBUG). {.
a8730 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 char zBuf[100
a8740 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
a8750 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
a8760 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 (pMem, zBuf);.
a8770 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
a8780 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 , "OUTPUT: %s\n"
a8790 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e , zBuf);. }.#en
a87a0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c dif. return SQL
a87b0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
a87c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
a87d0 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d ecks for a byte-
a87e0 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 order mark at th
a87f0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
a8800 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 he .** UTF-16 st
a8810 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a ring stored in *
a8820 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 pMem. If one is
a8830 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 present, it is r
a8840 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 emoved and.** th
a8850 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 e encoding of th
a8860 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 e Mem adjusted.
a8870 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
a8880 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 s not do any.**
a8890 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 byte-swapping, i
a88a0 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e t just sets Mem.
a88b0 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c enc appropriatel
a88c0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c y..**.** The all
a88d0 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c ocation (static,
a88e0 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 dynamic etc.) a
a88f0 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 nd encoding of t
a8900 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a he Mem may be.**
a8910 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
a8920 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 function..*/.SQ
a8930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a8940 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
a8950 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d andleBom(Mem *pM
a8960 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 em){. int rc =
a8970 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 SQLITE_OK;. u8
a8980 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 bom = 0;.. asse
a8990 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 rt( pMem->n>=0 )
a89a0 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e ;. if( pMem->n>
a89b0 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 1 ){. u8 b1 =
a89c0 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b *(u8 *)pMem->z;
a89d0 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 . u8 b2 = *((
a89e0 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b (u8 *)pMem->z) +
a89f0 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 1);. if( b1=
a8a00 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 =0xFE && b2==0xF
a8a10 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d F ){. bom =
a8a20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b SQLITE_UTF16BE;
a8a30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 . }. if( b
a8a40 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 1==0xFF && b2==0
a8a50 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d xFE ){. bom
a8a60 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c = SQLITE_UTF16L
a8a70 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a E;. }. }. .
a8a80 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 if( bom ){.
a8a90 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
a8aa0 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c eMemMakeWriteabl
a8ab0 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 e(pMem);. if(
a8ac0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
a8ad0 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 {. pMem->n
a8ae0 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d -= 2;. memm
a8af0 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d ove(pMem->z, &pM
a8b00 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e em->z[2], pMem->
a8b10 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e n);. pMem->
a8b20 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 z[pMem->n] = '\0
a8b30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a ';. pMem->z
a8b40 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c [pMem->n+1] = '\
a8b50 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 0';. pMem->
a8b60 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 flags |= MEM_Ter
a8b70 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 m;. pMem->e
a8b80 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a nc = bom;. }.
a8b90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
a8ba0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
a8bb0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
a8bc0 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 /../*.** pZ is a
a8bd0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 UTF-8 encoded u
a8be0 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 nicode string. I
a8bf0 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 f nByte is less
a8c00 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 than zero,.** re
a8c10 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
a8c20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 of unicode chara
a8c30 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 cters in pZ up t
a8c40 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 o (but not inclu
a8c50 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 ding).** the fir
a8c60 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 st 0x00 byte. If
a8c70 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 nByte is not le
a8c80 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 ss than zero, re
a8c90 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 turn the.** numb
a8ca0 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 er of unicode ch
a8cb0 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 aracters in the
a8cc0 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 first nByte of p
a8cd0 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 Z (or up to .**
a8ce0 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 the first 0x00,
a8cf0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 whichever comes
a8d00 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 first)..*/.SQLIT
a8d10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a8d20 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e lite3Utf8CharLen
a8d30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e (const char *zIn
a8d40 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
a8d50 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e int r = 0;. con
a8d60 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 st u8 *z = (cons
a8d70 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e t u8*)zIn;. con
a8d80 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 st u8 *zTerm;.
a8d90 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a if( nByte>=0 ){.
a8da0 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e zTerm = &z[n
a8db0 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a Byte];. }else{.
a8dc0 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e zTerm = (con
a8dd0 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d st u8*)(-1);. }
a8de0 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 . assert( z<=zT
a8df0 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 erm );. while(
a8e00 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d *z!=0 && z<zTerm
a8e10 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 ){. SQLITE_S
a8e20 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 KIP_UTF8(z);.
a8e30 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 r++;. }. retu
a8e40 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 rn r;.}../* This
a8e50 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 test function i
a8e60 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
a8e70 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f used by the auto
a8e80 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 mated test-suite
a8e90 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 . .** Hence it i
a8ea0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
a8eb0 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 in debug builds
a8ec0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
a8ed0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 (SQLITE_TEST) &&
a8ee0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
a8ef0 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 DEBUG)./*.** Tra
a8f00 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 nslate UTF-8 to
a8f10 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 UTF-8..**.** Thi
a8f20 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 s has the effect
a8f30 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 of making sure
a8f40 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 that the string
a8f50 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a is well-formed.*
a8f60 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 * UTF-8. Miscod
a8f70 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 ed characters ar
a8f80 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a e removed..**.**
a8f90 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e The translation
a8fa0 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 is done in-plac
a8fb0 65 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 e (since it is i
a8fc0 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 mpossible for th
a8fd0 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 e.** correct UTF
a8fe0 2d 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 -8 encoding to b
a8ff0 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 e longer than a
a9000 6d 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 malformed encodi
a9010 6e 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ng)..*/.SQLITE_P
a9020 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a9030 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e e3Utf8To8(unsign
a9040 65 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 ed char *zIn){.
a9050 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
a9060 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e zOut = zIn;. un
a9070 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 signed char *zSt
a9080 61 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 art = zIn;. u32
a9090 20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 c;.. while( zI
a90a0 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 n[0] ){. c =
a90b0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
a90c0 7a 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a zIn, (const u8**
a90d0 29 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 )&zIn);. if(
a90e0 63 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 c!=0xfffd ){.
a90f0 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f WRITE_UTF8(zO
a9100 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 ut, c);. }.
a9110 7d 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 }. *zOut = 0;.
a9120 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f return (int)(zO
a9130 75 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a ut - zStart);.}.
a9140 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
a9150 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
a9160 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6./*.** Convert
a9170 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 a UTF-16 string
a9180 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e in the native en
a9190 63 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 coding into a UT
a91a0 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d F-8 string..** M
a91b0 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 emory to hold th
a91c0 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 e UTF-8 string i
a91d0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
a91e0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 sqlite3_malloc a
a91f0 6e 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 nd must.** be fr
a9200 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 eed by the calli
a9210 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a ng function..**.
a9220 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 ** NULL is retur
a9230 6e 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 ned if there is
a9240 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 an allocation er
a9250 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ror..*/.SQLITE_P
a9260 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
a9270 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c ite3Utf16to8(sql
a9280 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
a9290 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 void *z, int nBy
a92a0 74 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 te){. Mem m;.
a92b0 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 memset(&m, 0, si
a92c0 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 zeof(m));. m.db
a92d0 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 = db;. sqlite3
a92e0 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d VdbeMemSetStr(&m
a92f0 2c 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 , z, nByte, SQLI
a9300 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 TE_UTF16NATIVE,
a9310 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
a9320 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
a9330 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 ngeEncoding(&m,
a9340 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 SQLITE_UTF8);.
a9350 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
a9360 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 iled ){. sqli
a9370 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
a9380 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d e(&m);. m.z =
a9390 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 0;. }. assert
a93a0 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d ( (m.flags & MEM
a93b0 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d _Term)!=0 || db-
a93c0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
a93d0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c . assert( (m.fl
a93e0 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d ags & MEM_Str)!=
a93f0 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
a9400 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 ailed );. retur
a9410 6e 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d n (m.flags & MEM
a9420 5f 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a _Dyn)!=0 ? m.z :
a9430 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
a9440 28 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a (db, m.z);.}../*
a9450 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d .** pZ is a UTF-
a9460 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 16 encoded unico
a9470 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c 65 61 de string at lea
a9480 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 63 74 st nChar charact
a9490 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 ers long..** Ret
a94a0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
a94b0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 f bytes in the f
a94c0 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f irst nChar unico
a94d0 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a de characters.**
a94e0 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 20 6d in pZ. nChar m
a94f0 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 ust be non-negat
a9500 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ive..*/.SQLITE_P
a9510 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a9520 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 e3Utf16ByteLen(c
a9530 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c 20 onst void *zIn,
a9540 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 69 6e int nChar){. in
a9550 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 t c;. unsigned
a9560 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 char const *z =
a9570 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 zIn;. int n = 0
a9580 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 55 ;. if( SQLITE_U
a9590 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49 TF16NATIVE==SQLI
a95a0 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a 20 20 TE_UTF16BE ){.
a95b0 20 20 2f 2a 20 55 73 69 6e 67 20 61 6e 20 22 69 /* Using an "i
a95c0 66 20 28 53 51 4c 49 54 45 5f 55 54 46 31 36 4e f (SQLITE_UTF16N
a95d0 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 ATIVE==SQLITE_UT
a95e0 46 31 36 42 45 29 22 20 63 6f 6e 73 74 72 75 63 F16BE)" construc
a95f0 74 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 6e t here. ** an
a9600 64 20 69 6e 20 6f 74 68 65 72 20 70 61 72 74 73 d in other parts
a9610 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 6d 65 of this file me
a9620 61 6e 73 20 74 68 61 74 20 61 74 20 6f 6e 65 20 ans that at one
a9630 62 72 61 6e 63 68 20 77 69 6c 6c 0a 20 20 20 20 branch will.
a9640 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 76 65 72 65 ** not be covere
a9650 64 20 62 79 20 63 6f 76 65 72 61 67 65 20 74 65 d by coverage te
a9660 73 74 69 6e 67 20 6f 6e 20 61 6e 79 20 73 69 6e sting on any sin
a9670 67 6c 65 20 68 6f 73 74 2e 20 42 75 74 20 63 6f gle host. But co
a9680 76 65 72 61 67 65 0a 20 20 20 20 2a 2a 20 77 69 verage. ** wi
a9690 6c 6c 20 62 65 20 63 6f 6d 70 6c 65 74 65 20 69 ll be complete i
a96a0 66 20 74 68 65 20 74 65 73 74 73 20 61 72 65 20 f the tests are
a96b0 72 75 6e 20 6f 6e 20 62 6f 74 68 20 61 20 6c 69 run on both a li
a96c0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 61 6e 64 20 ttle-endian and
a96d0 0a 20 20 20 20 2a 2a 20 62 69 67 2d 65 6e 64 69 . ** big-endi
a96e0 61 6e 20 68 6f 73 74 2e 20 42 65 63 61 75 73 65 an host. Because
a96f0 20 62 6f 74 68 20 74 68 65 20 55 54 46 31 36 4e both the UTF16N
a9700 41 54 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 ATIVE and SQLITE
a9710 5f 55 54 46 31 36 42 45 0a 20 20 20 20 2a 2a 20 _UTF16BE. **
a9720 6d 61 63 72 6f 73 20 61 72 65 20 63 6f 6e 73 74 macros are const
a9730 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 ant at compile t
a9740 69 6d 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 ime the compiler
a9750 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 0a 20 can determine.
a9760 20 20 20 2a 2a 20 77 68 69 63 68 20 62 72 61 6e ** which bran
a9770 63 68 20 77 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f ch will be follo
a9780 77 65 64 2e 20 49 74 20 69 73 20 74 68 65 72 65 wed. It is there
a9790 66 6f 72 65 20 61 73 73 75 6d 65 64 20 74 68 61 fore assumed tha
a97a0 74 20 6e 6f 20 72 75 6e 74 69 6d 65 0a 20 20 20 t no runtime.
a97b0 20 2a 2a 20 70 65 6e 61 6c 74 79 20 69 73 20 70 ** penalty is p
a97c0 61 69 64 20 66 6f 72 20 74 68 69 73 20 22 69 66 aid for this "if
a97d0 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 " statement..
a97e0 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e */. while( n
a97f0 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 <nChar ){.
a9800 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 READ_UTF16BE(z,
a9810 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 c);. n++;.
a9820 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
a9830 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 61 72 while( n<nChar
a9840 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 ){. READ_U
a9850 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 TF16LE(z, c);.
a9860 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 n++;. }.
a9870 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 }. return (int
a9880 29 28 7a 2d 28 75 6e 73 69 67 6e 65 64 20 63 68 )(z-(unsigned ch
a9890 61 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 3b ar const *)zIn);
a98a0 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .}..#if defined(
a98b0 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a SQLITE_TEST)./*.
a98c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
a98d0 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 is called from t
a98e0 68 65 20 54 43 4c 20 74 65 73 74 20 66 75 6e 63 he TCL test func
a98f0 74 69 6f 6e 20 22 74 72 61 6e 73 6c 61 74 65 5f tion "translate_
a9900 73 65 6c 66 74 65 73 74 22 2e 0a 2a 2a 20 49 74 selftest"..** It
a9910 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 checks that the
a9920 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72 20 primitives for
a9930 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 6e 64 20 serializing and
a9940 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a deserializing.**
a9950 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 65 characters in e
a9960 61 63 68 20 65 6e 63 6f 64 69 6e 67 20 61 72 65 ach encoding are
a9970 20 69 6e 76 65 72 73 65 73 20 6f 66 20 65 61 63 inverses of eac
a9980 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 h other..*/.SQLI
a9990 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a99a0 73 71 6c 69 74 65 33 55 74 66 53 65 6c 66 54 65 sqlite3UtfSelfTe
a99b0 73 74 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 st(void){. unsi
a99c0 67 6e 65 64 20 69 6e 74 20 69 2c 20 74 3b 0a 20 gned int i, t;.
a99d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a unsigned char z
a99e0 42 75 66 5b 32 30 5d 3b 0a 20 20 75 6e 73 69 67 Buf[20];. unsig
a99f0 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 ned char *z;. i
a9a00 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 nt n;. unsigned
a9a10 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 int c;.. for(i
a9a20 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 =0; i<0x00110000
a9a30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 ; i++){. z =
a9a40 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f zBuf;. WRITE_
a9a50 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 UTF8(z, i);.
a9a60 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 n = (int)(z-zBuf
a9a70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
a9a80 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 >0 && n<=4 );.
a9a90 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 z[0] = 0;.
a9aa0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 63 20 z = zBuf;. c
a9ab0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
a9ac0 64 28 7a 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a d(z, (const u8**
a9ad0 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 20 69 3b )&z);. t = i;
a9ae0 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 . if( i>=0xD8
a9af0 30 30 20 26 26 20 69 3c 3d 30 78 44 46 46 46 20 00 && i<=0xDFFF
a9b00 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 ) t = 0xFFFD;.
a9b10 20 20 69 66 28 20 28 69 26 30 78 46 46 46 46 46 if( (i&0xFFFFF
a9b20 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 20 74 FFE)==0xFFFE ) t
a9b30 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 20 20 61 = 0xFFFD;. a
a9b40 73 73 65 72 74 28 20 63 3d 3d 74 20 29 3b 0a 20 ssert( c==t );.
a9b50 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 assert( (z-zB
a9b60 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 uf)==n );. }.
a9b70 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
a9b80 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
a9b90 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 if( i>=0xD800 &
a9ba0 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e & i<0xE000 ) con
a9bb0 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a tinue;. z = z
a9bc0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 Buf;. WRITE_U
a9bd0 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b 0a 20 20 TF16LE(z, i);.
a9be0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 n = (int)(z-zB
a9bf0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 uf);. assert(
a9c00 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a n>0 && n<=4 );.
a9c10 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 z[0] = 0;.
a9c20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
a9c30 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 READ_UTF16LE(z,
a9c40 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 c);. assert(
a9c50 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 c==i );. asse
a9c60 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 rt( (z-zBuf)==n
a9c70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 );. }. for(i=0
a9c80 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 ; i<0x00110000;
a9c90 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e i++){. if( i>
a9ca0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 =0xD800 && i<0xE
a9cb0 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 000 ) continue;.
a9cc0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
a9cd0 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 WRITE_UTF16BE(
a9ce0 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 z, i);. n = (
a9cf0 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 int)(z-zBuf);.
a9d00 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 assert( n>0 &&
a9d10 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 n<=4 );. z[0
a9d20 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a ] = 0;. z = z
a9d30 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 Buf;. READ_UT
a9d40 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16BE(z, c);.
a9d50 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b assert( c==i );
a9d60 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d . assert( (z-
a9d70 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a zBuf)==n );. }.
a9d80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
a9d90 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69 TE_TEST */.#endi
a9da0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
a9db0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _UTF16 */../****
a9dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
a9dd0 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a f utf.c ********
a9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
a9e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
a9e20 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a file util.c ***
a9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
a9e60 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
a9e70 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
a9e80 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
a9e90 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
a9ea0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
a9eb0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
a9ec0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
a9ed0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
a9ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
a9ef0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
a9f00 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
a9f10 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
a9f20 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
a9f30 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
a9f40 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
a9f50 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
a9f60 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
a9f70 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
a9f80 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
a9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fd0 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e *.** Utility fun
a9fe0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f ctions used thro
a9ff0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a ughout sqlite..*
aa000 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
aa010 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e ontains function
aa020 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67 s for allocating
aa030 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69 memory, compari
aa040 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61 ng.** strings, a
aa050 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68 nd stuff like th
aa060 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 at..**.** $Id: u
aa070 74 69 6c 2e 63 2c 76 20 31 2e 32 35 34 20 32 30 til.c,v 1.254 20
aa080 30 39 2f 30 35 2f 30 36 20 31 39 3a 30 33 3a 31 09/05/06 19:03:1
aa090 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 4 drh Exp $.*/.#
aa0a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 ifdef SQLITE_HAV
aa0b0 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 E_ISNAN.# includ
aa0c0 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69 e <math.h>.#endi
aa0d0 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 f../*.** Routine
aa0e0 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 6f needed to suppo
aa0f0 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 28 rt the testcase(
aa100 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 ) macro..*/.#ifd
aa110 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ef SQLITE_COVERA
aa120 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 GE_TEST.SQLITE_P
aa130 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
aa140 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 20 te3Coverage(int
aa150 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 x){. static int
aa160 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 75 dummy = 0;. du
aa170 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 mmy += x;.}.#end
aa180 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e if../*.** Routin
aa190 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 e needed to supp
aa1a0 6f 72 74 20 74 68 65 20 41 4c 57 41 59 53 28 29 ort the ALWAYS()
aa1b0 20 61 6e 64 20 4e 45 56 45 52 28 29 20 6d 61 63 and NEVER() mac
aa1c0 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 ros..**.** The a
aa1d0 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41 59 rgument to ALWAY
aa1e0 53 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 S() should alway
aa1f0 73 20 62 65 20 74 72 75 65 20 61 6e 64 20 74 68 s be true and th
aa200 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f e argument.** to
aa210 20 4e 45 56 45 52 28 29 20 73 68 6f 75 6c 64 20 NEVER() should
aa220 61 6c 77 61 79 73 20 62 65 20 66 61 6c 73 65 2e always be false.
aa230 20 20 49 66 20 65 69 74 68 65 72 20 69 73 20 6e If either is n
aa240 6f 74 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 74 ot the case.** t
aa250 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
aa260 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 6f 72 is called in or
aa270 64 65 72 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 der to throw an
aa280 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 error..**.** Thi
aa290 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 65 s routine only e
aa2a0 78 69 73 74 73 20 69 66 20 61 73 73 65 72 74 28 xists if assert(
aa2b0 29 20 69 73 20 6f 70 65 72 61 74 69 6f 6e 61 6c ) is operational
aa2c0 2e 20 20 49 74 20 61 6c 77 61 79 73 0a 2a 2a 20 . It always.**
aa2d0 74 68 72 6f 77 73 20 61 6e 20 61 73 73 65 72 74 throws an assert
aa2e0 20 6f 6e 20 69 74 73 20 66 69 72 73 74 20 69 6e on its first in
aa2f0 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 76 vocation. The v
aa300 61 72 69 61 62 6c 65 20 68 61 73 20 61 20 6c 6f ariable has a lo
aa310 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f 20 68 65 ng.** name to he
aa320 6c 70 20 74 68 65 20 61 73 73 65 72 74 28 29 20 lp the assert()
aa330 6d 65 73 73 61 67 65 20 62 65 20 6d 6f 72 65 20 message be more
aa340 72 65 61 64 61 62 6c 65 2e 20 20 54 68 65 20 76 readable. The v
aa350 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 75 73 ariable.** is us
aa360 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 ed to prevent a
aa370 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70 74 69 6d too-clever optim
aa380 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74 69 6d 69 izer from optimi
aa390 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a 2a 2a 20 zing out the.**
aa3a0 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a 2a 2f 0a entire call..*/.
aa3b0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
aa3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
aa3d0 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 28 t sqlite3Assert(
aa3e0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
aa3f0 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 41 4c 57 volatile int ALW
aa400 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f 72 AYS_was_false_or
aa410 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75 65 20 _NEVER_was_true
aa420 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 41 = 0;. assert( A
aa430 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f LWAYS_was_false_
aa440 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75 or_NEVER_was_tru
aa450 65 20 29 3b 20 20 20 20 20 20 2f 2a 20 41 6c 77 e ); /* Alw
aa460 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a 20 20 72 ays fails */. r
aa470 65 74 75 72 6e 20 41 4c 57 41 59 53 5f 77 61 73 eturn ALWAYS_was
aa480 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 45 52 5f _false_or_NEVER_
aa490 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20 20 20 20 was_true++;
aa4a0 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63 68 65 64 /* Not Reached
aa4b0 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a */.}.#endif../*
aa4c0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
aa4d0 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 if the floating
aa4e0 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 4e point value is N
aa4f0 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 4e ot a Number (NaN
aa500 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 )..**.** Use the
aa510 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20 69 73 math library is
aa520 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 nan() function i
aa530 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 f compiled with
aa540 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 SQLITE_HAVE_ISNA
aa550 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c N..** Otherwise,
aa560 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f 77 6e we have our own
aa570 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
aa580 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f that works on mo
aa590 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 st systems..*/.S
aa5a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
aa5b0 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 t sqlite3IsNaN(d
aa5c0 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e 74 20 ouble x){. int
aa5d0 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76 61 6c rc; /* The val
aa5e0 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23 69 66 ue return */.#if
aa5f0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
aa600 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20 20 2f _HAVE_ISNAN). /
aa610 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 20 74 *. ** Systems t
aa620 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 hat support the
aa630 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 79 20 isnan() library
aa640 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
aa650 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 probably. ** ma
aa660 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 79 20 ke use of it by
aa670 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 2d compiling with -
aa680 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e DSQLITE_HAVE_ISN
aa690 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 76 65 AN. But we have
aa6a0 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68 61 74 . ** found that
aa6b0 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20 64 6f many systems do
aa6c0 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f 72 6b not have a work
aa6d0 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75 6e 63 ing isnan() func
aa6e0 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74 68 69 tion so. ** thi
aa6f0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
aa700 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 73 20 is provided as
aa710 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a an alternative..
aa720 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 4e **. ** This N
aa730 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 aN test sometime
aa740 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 s fails if compi
aa750 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20 led on GCC with
aa760 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a -ffast-math.. *
aa770 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
aa780 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20 and, the use of
aa790 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 -ffast-math come
aa7a0 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f s with the follo
aa7b0 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e wing. ** warnin
aa7c0 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 g:. **. **
aa7d0 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d This option [-
aa7e0 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 ffast-math] shou
aa7f0 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e ld never be turn
aa800 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a ed on by any. *
aa810 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e * -O option
aa820 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65 since it can re
aa830 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 sult in incorrec
aa840 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f t output for pro
aa850 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 grams. **
aa860 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 which depend on
aa870 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 an exact impleme
aa880 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 ntation of IEEE
aa890 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 or ISO . **
aa8a0 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63 rules/specific
aa8b0 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 ations for math
aa8c0 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a functions.. **.
aa8d0 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c ** Under MSVC,
aa8e0 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d this NaN test m
aa8f0 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 ay fail if compi
aa900 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 led with a float
aa910 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 ing-. ** point
aa920 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f precision mode o
aa930 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 ther than /fp:pr
aa940 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 ecise. From the
aa950 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 MSDN . ** docu
aa960 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a mentation:. **.
aa970 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f ** The co
aa980 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 mpiler [with /fp
aa990 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 :precise] will p
aa9a0 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 roperly handle c
aa9b0 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a omparisons . **
aa9c0 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 involving
aa9d0 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 NaN. For example
aa9e0 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74 , x != x evaluat
aa9f0 65 73 20 74 6f 20 74 72 75 65 20 69 66 20 78 20 es to true if x
aaa00 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 is NaN . **
aaa10 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 .... */.#ifde
aaa20 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a f __FAST_MATH__.
aaa30 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77 # error SQLite w
aaa40 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 ill not work cor
aaa50 72 65 63 74 6c 79 20 77 69 74 68 20 74 68 65 20 rectly with the
aaa60 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 -ffast-math opti
aaa70 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 on of GCC..#endi
aaa80 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 f. volatile dou
aaa90 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c ble y = x;. vol
aaaa0 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d atile double z =
aaab0 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21 3d 7a y;. rc = (y!=z
aaac0 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 );.#else /* if
aaad0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 defined(SQLITE_H
aaae0 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 AVE_ISNAN) */.
aaaf0 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b 0a 23 rc = isnan(x);.#
aab00 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
aab10 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 HAVE_ISNAN */.
aab20 74 65 73 74 63 61 73 65 28 20 72 63 20 29 3b 0a testcase( rc );.
aab30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
aab40 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 /*.** Compute a
aab50 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 74 68 string length th
aab60 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f at is limited to
aab70 20 77 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f what can be sto
aab80 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 red in.** lower
aab90 33 30 20 62 69 74 73 20 6f 66 20 61 20 33 32 2d 30 bits of a 32-
aaba0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
aabb0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 er..**.** The va
aabc0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c lue returned wil
aabd0 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 61 74 l never be negat
aabe0 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 69 ive. Nor will i
aabf0 74 20 65 76 65 72 20 62 65 20 67 72 65 61 74 65 t ever be greate
aac00 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 63 r.** than the ac
aac10 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 20 74 tual length of t
aac20 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 he string. For
aac30 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 6e 67 very long string
aac40 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 s (greater.** th
aac50 61 6e 20 31 47 69 42 29 20 74 68 65 20 76 61 6c an 1GiB) the val
aac60 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69 67 68 ue returned migh
aac70 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 74 t be less than t
aac80 68 65 20 74 72 75 65 20 73 74 72 69 6e 67 20 6c he true string l
aac90 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ength..*/.SQLITE
aaca0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
aacb0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e ite3Strlen30(con
aacc0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 st char *z){. c
aacd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 onst char *z2 =
aace0 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 z;. while( *z2
aacf0 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 ){ z2++; }. ret
aad00 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 urn 0x3fffffff &
aad10 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a (int)(z2 - z);.
aad20 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
aad30 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 most recent err
aad40 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f or code and erro
aad50 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 r string for the
aad60 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
aad70 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f e "db". The erro
aad80 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f r code is set to
aad90 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a "err_code"..**.
aada0 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 ** If it is not
aadb0 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f NULL, string zFo
aadc0 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74 rmat specifies t
aadd0 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 he format of the
aade0 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 .** error string
aadf0 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 in the style of
aae00 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 the printf func
aae10 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f tions: The follo
aae20 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 wing.** format c
aae30 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c haracters are al
aae40 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 lowed:.**.**
aae50 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 %s Insert
aae60 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 a string.**
aae70 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 %z A stri
aae80 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 ng that should b
aae90 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 e freed after us
aaea0 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 e.** %d
aaeb0 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 Insert an inte
aaec0 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 ger.** %T
aaed0 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b Insert a tok
aaee0 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 en.** %S
aaef0 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 Insert the fi
aaf00 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 rst element of a
aaf10 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a SrcList.**.** z
aaf20 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 Format and any s
aaf30 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 tring tokens tha
aaf40 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 t follow it are
aaf50 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a assumed to be.**
aaf60 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
aaf70 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 8..**.** To clea
aaf80 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e r the most recen
aaf90 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 t error for sqli
aafa0 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 te handle "db",
aafb0 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 sqlite3Error.**
aafc0 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
aafd0 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 with err_code s
aafe0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
aaff0 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a and zFormat set.
ab000 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 ** to NULL..*/.S
ab010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ab020 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 id sqlite3Error(
ab030 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
ab040 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 err_code, const
ab050 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
ab060 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 ...){. if( db &
ab070 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 & (db->pErr || (
ab080 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 db->pErr = sqlit
ab090 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 e3ValueNew(db))!
ab0a0 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 =0) ){. db->e
ab0b0 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 rrCode = err_cod
ab0c0 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d e;. if( zForm
ab0d0 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 at ){. char
ab0e0 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 *z;. va_li
ab0f0 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f st ap;. va_
ab100 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
ab110 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 t);. z = sq
ab120 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
ab130 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
ab140 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 va_end(ap)
ab150 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
ab160 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
ab170 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 Err, -1, z, SQLI
ab180 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
ab190 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 DYNAMIC);. }e
ab1a0 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
ab1b0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 e3ValueSetStr(db
ab1c0 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 ->pErr, 0, 0, SQ
ab1d0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
ab1e0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d E_STATIC);. }
ab1f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 . }.}../*.** Ad
ab200 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
ab210 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 ge to pParse->zE
ab220 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d rrMsg and increm
ab230 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 ent pParse->nErr
ab240 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ..** The followi
ab250 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 ng formatting ch
ab260 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c aracters are all
ab270 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 owed:.**.**
ab280 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 %s Insert
ab290 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 a string.**
ab2a0 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e %z A strin
ab2b0 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 g that should be
ab2c0 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 freed after use
ab2d0 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 .** %d
ab2e0 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 Insert an integ
ab2f0 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 er.** %T
ab300 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 Insert a toke
ab310 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 n.** %S
ab320 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 Insert the fir
ab330 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 st element of a
ab340 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 SrcList.**.** Th
ab350 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
ab360 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 ld be used to re
ab370 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 port any error t
ab380 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 hat occurs whils
ab390 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 t.** compiling a
ab3a0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
ab3b0 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c (i.e. within sql
ab3c0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e ite3_prepare()).
ab3d0 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 The.** last thi
ab3e0 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 ng the sqlite3_p
ab3f0 72 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f repare() functio
ab400 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 n does is copy t
ab410 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 he error.** stor
ab420 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
ab430 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ion into the dat
ab440 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 abase handle usi
ab450 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 ng sqlite3Error(
ab460 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 )..** Function s
ab470 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 qlite3Error() sh
ab480 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 ould be used dur
ab490 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 ing statement ex
ab4a0 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 ecution.** (sqli
ab4b0 74 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 te3_step() etc.)
ab4c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
ab4d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ab4e0 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a ErrorMsg(Parse *
ab4f0 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
ab500 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
ab510 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
ab520 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
ab530 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 pParse->db;. p
ab540 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
ab550 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 73 testcase( pPars
ab560 65 2d 3e 7a 45 72 72 4d 73 67 21 3d 30 20 29 3b e->zErrMsg!=0 );
ab570 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
ab580 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 (db, pParse->zEr
ab590 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 rMsg);. va_star
ab5a0 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
ab5b0 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 pParse->zErrMs
ab5c0 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 g = sqlite3VMPri
ab5d0 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
ab5e0 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
ab5f0 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 p);. if( pParse
ab600 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
ab610 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 ){. pParse->r
ab620 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
ab630 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
ab640 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 6d lear the error m
ab650 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 essage in pParse
ab660 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 , if any.*/.SQLI
ab670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ab680 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 sqlite3ErrorClea
ab690 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 r(Parse *pParse)
ab6a0 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 {. sqlite3DbFre
ab6b0 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 e(pParse->db, pP
ab6c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a arse->zErrMsg);.
ab6d0 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 pParse->zErrMs
ab6e0 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d g = 0;. pParse-
ab6f0 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a >nErr = 0;.}../*
ab700 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 .** Convert an S
ab710 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20 QL-style quoted
ab720 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f string into a no
ab730 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72 rmal string by r
ab740 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 emoving.** the q
ab750 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 2e uote characters.
ab760 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e The conversion
ab770 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 is done in-plac
ab780 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e e. If the.** in
ab790 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 put does not beg
ab7a0 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 20 in with a quote
ab7b0 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 character, then
ab7c0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
ab7d0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
ab7e0 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 69 * The input stri
ab7f0 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d ng must be zero-
ab800 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20 6e terminated. A n
ab810 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 ew zero-terminat
ab820 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 or.** is added t
ab830 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20 73 o the dequoted s
ab840 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tring..**.** The
ab850 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
ab860 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f 74 -1 if no dequot
ab870 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74 68 ing occurs or th
ab880 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 0a e length of the.
ab890 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 ** dequoted stri
ab8a0 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 ng, exclusive of
ab8b0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e the zero termin
ab8c0 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 69 ator, if dequoti
ab8d0 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 ng does.** occur
ab8e0 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62 ..**.** 2002-Feb
ab8f0 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e -14: This routin
ab900 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f e is extended to
ab910 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73 remove MS-Acces
ab920 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b s style.** brack
ab930 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20 ets from around
ab940 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72 identifers. For
ab950 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 example: "[a-b
ab960 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 -c]" becomes.**
ab970 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 "a-b-c"..*/.SQLI
ab980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ab990 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 qlite3Dequote(ch
ab9a0 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 71 ar *z){. char q
ab9b0 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a uote;. int i, j
ab9c0 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 ;. if( z==0 ) r
ab9d0 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 eturn -1;. quot
ab9e0 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 e = z[0];. swit
ab9f0 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 ch( quote ){.
aba00 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 case '\'': bre
aba10 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 ak;. case '"'
aba20 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 : break;. c
aba30 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b ase '`': break
aba40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aba50 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f /* For MySQL co
aba60 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 mpatibility */.
aba70 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 case '[': q
aba80 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 uote = ']'; bre
aba90 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 ak; /* For MS S
abaa0 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 qlServer compati
abab0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 bility */. de
abac0 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e fault: return
abad0 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 -1;. }. for(i
abae0 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53 28 =1, j=0; ALWAYS(
abaf0 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 z[i]); i++){.
abb00 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 if( z[i]==quote
abb10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ){. if( z[
abb20 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 i+1]==quote ){.
abb30 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
abb40 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69 quote;. i
abb50 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ++;. }else{
abb60 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
abb70 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
abb80 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 e{. z[j++]
abb90 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 = z[i];. }.
abba0 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 }. z[j] = 0;.
abbb0 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 return j;.}../*
abbc0 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 74 Convenient short
abbd0 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 -hand */.#define
abbe0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 71 UpperToLower sq
abbf0 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
abc00 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 r../*.** Some sy
abc10 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 63 stems have stric
abc20 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 61 mp(). Others ha
abc30 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 2e ve strcasecmp().
abc40 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 Because.** the
abc50 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 74 re is no consist
abc60 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 65 ency, we will de
abc70 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f fine our own..*/
abc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
abc90 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 int sqlite3StrIC
abca0 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a mp(const char *z
abcb0 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 Left, const char
abcc0 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65 67 *zRight){. reg
abcd0 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 ister unsigned c
abce0 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 har *a, *b;. a
abcf0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
abd00 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 *)zLeft;. b =
abd10 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
abd20 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 )zRight;. while
abd30 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72 ( *a!=0 && Upper
abd40 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 ToLower[*a]==Upp
abd50 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 erToLower[*b]){
abd60 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 a++; b++; }. re
abd70 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77 65 turn UpperToLowe
abd80 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c r[*a] - UpperToL
abd90 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 ower[*b];.}.SQLI
abda0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
abdb0 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63 qlite3StrNICmp(c
abdc0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
abdd0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
abde0 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 ight, int N){.
abdf0 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 register unsigne
abe00 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 d char *a, *b;.
abe10 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 a = (unsigned c
abe20 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 har *)zLeft;. b
abe30 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
abe40 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 r *)zRight;. wh
abe50 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 ile( N-- > 0 &&
abe60 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f *a!=0 && UpperTo
abe70 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 Lower[*a]==Upper
abe80 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b ToLower[*b]){ a+
abe90 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 +; b++; }. retu
abea0 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70 rn N<0 ? 0 : Upp
abeb0 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 erToLower[*a] -
abec0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d UpperToLower[*b]
abed0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
abee0 6e 20 54 52 55 45 20 69 66 20 7a 20 69 73 20 61 n TRUE if z is a
abef0 20 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74 pure numeric st
abf00 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41 ring. Return FA
abf10 4c 53 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a LSE and leave.**
abf20 20 2a 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e *realnum unchan
abf30 67 65 64 20 69 66 20 74 68 65 20 73 74 72 69 6e ged if the strin
abf40 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 g contains any c
abf50 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 haracter which i
abf60 73 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66 s not.** part of
abf70 20 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a a number..**.**
abf80 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 If the string i
abf90 73 20 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20 s pure numeric,
abfa0 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 set *realnum to
abfb0 54 52 55 45 20 69 66 20 74 68 65 20 73 74 72 69 TRUE if the stri
abfc0 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 ng.** contains t
abfd0 68 65 20 27 2e 27 20 63 68 61 72 61 63 74 65 72 he '.' character
abfe0 20 6f 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73 or an "E+000" s
abff0 74 79 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74 tyle exponentiat
ac000 69 6f 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f ion suffix..** O
ac010 74 68 65 72 77 69 73 65 20 73 65 74 20 2a 72 65 therwise set *re
ac020 61 6c 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20 alnum to FALSE.
ac030 20 4e 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20 Note that just
ac040 62 65 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20 becaue *realnum
ac050 69 73 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73 is.** false does
ac060 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 not mean that t
ac070 68 65 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65 he number can be
ac080 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f successfully co
ac090 6e 76 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 nverted into.**
ac0a0 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20 an integer - it
ac0b0 6d 69 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67 might be too big
ac0c0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 ..**.** An empty
ac0d0 20 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69 string is consi
ac0e0 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 dered non-numeri
ac0f0 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 c..*/.SQLITE_PRI
ac100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ac110 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 IsNumber(const c
ac120 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 har *z, int *rea
ac130 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20 lnum, u8 enc){.
ac140 20 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 int incr = (enc
ac150 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a ==SQLITE_UTF8?1:
ac160 32 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 2);. if( enc==S
ac170 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 QLITE_UTF16BE )
ac180 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 z++;. if( *z=='
ac190 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 -' || *z=='+' )
ac1a0 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 z += incr;. if(
ac1b0 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 !sqlite3Isdigit
ac1c0 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 (*z) ){. retu
ac1d0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d rn 0;. }. z +=
ac1e0 20 69 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 incr;. *realnu
ac1f0 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 m = 0;. while(
ac200 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
ac210 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b z) ){ z += incr;
ac220 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 }. if( *z=='.'
ac230 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 ){. z += inc
ac240 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 r;. if( !sqli
ac250 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 te3Isdigit(*z) )
ac260 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 return 0;. w
ac270 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 hile( sqlite3Isd
ac280 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d igit(*z) ){ z +=
ac290 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 incr; }. *re
ac2a0 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 alnum = 1;. }.
ac2b0 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 if( *z=='e' ||
ac2c0 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a *z=='E' ){. z
ac2d0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 += incr;. if
ac2e0 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d ( *z=='+' || *z=
ac2f0 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 ='-' ) z += incr
ac300 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 ;. if( !sqlit
ac310 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 e3Isdigit(*z) )
ac320 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 return 0;. wh
ac330 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
ac340 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 git(*z) ){ z +=
ac350 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 incr; }. *rea
ac360 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 lnum = 1;. }.
ac370 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a return *z==0;.}.
ac380 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e ./*.** The strin
ac390 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63 69 g z[] is an asci
ac3a0 69 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e i representation
ac3b0 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 of a real numbe
ac3c0 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 r..** Convert th
ac3d0 69 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 is string to a d
ac3e0 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ouble..**.** Thi
ac3f0 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 s routine assume
ac400 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c s that z[] reall
ac410 79 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d y is a valid num
ac420 62 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 ber. If it.** i
ac430 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c s not, the resul
ac440 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a t is undefined..
ac450 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
ac460 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 ne is used inste
ac470 61 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 ad of the librar
ac480 79 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f y atof() functio
ac490 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 n because.** the
ac4a0 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 library atof()
ac4b0 6d 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 might want to us
ac4c0 65 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 e "," as the dec
ac4d0 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 imal point inste
ac4e0 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 ad.** of "." dep
ac4f0 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f ending on how lo
ac500 63 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 cale is set. Bu
ac510 74 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 t that would cau
ac520 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 se problems.** f
ac530 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 or SQL. So this
ac540 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 routine always
ac550 75 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c uses "." regardl
ac560 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a ess of locale..*
ac570 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
ac580 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 int sqlite3AtoF
ac590 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
ac5a0 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 double *pResult)
ac5b0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
ac5c0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
ac5d0 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e 20 OINT. int sign
ac5e0 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 = 1;. const cha
ac5f0 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 r *zBegin = z;.
ac600 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
ac610 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 v1 = 0.0;. int
ac620 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d 20 nSignificant =
ac630 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 0;. while( sqli
ac640 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 te3Isspace(*z) )
ac650 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d z++;. if( *z==
ac660 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 '-' ){. sign
ac670 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 = -1;. z++;.
ac680 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 }else if( *z=='
ac690 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 +' ){. z++;.
ac6a0 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d }. while( z[0]
ac6b0 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b 2b =='0' ){. z++
ac6c0 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 73 ;. }. while( s
ac6d0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
ac6e0 29 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76 31 ) ){. v1 = v1
ac6f0 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 30 *10.0 + (*z - '0
ac700 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 ');. z++;.
ac710 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b nSignificant++;
ac720 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 . }. if( *z=='
ac730 2e 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f .' ){. LONGDO
ac740 55 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 6f UBLE_TYPE diviso
ac750 72 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b r = 1.0;. z++
ac760 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69 ;. if( nSigni
ac770 66 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 ficant==0 ){.
ac780 20 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d while( z[0]==
ac790 27 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20 64 '0' ){. d
ac7a0 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a ivisor *= 10.0;.
ac7b0 20 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 z++;.
ac7c0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 }. }. w
ac7d0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 hile( sqlite3Isd
ac7e0 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 igit(*z) ){.
ac7f0 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61 if( nSignifica
ac800 6e 74 3c 31 38 20 29 7b 0a 20 20 20 20 20 20 20 nt<18 ){.
ac810 20 76 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 v1 = v1*10.0 +
ac820 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 (*z - '0');.
ac830 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20 31 divisor *= 1
ac840 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 6e 53 69 0.0;. nSi
ac850 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 gnificant++;.
ac860 20 20 20 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a }. z++;.
ac870 20 20 20 20 7d 0a 20 20 20 20 76 31 20 2f 3d 20 }. v1 /=
ac880 64 69 76 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 divisor;. }. i
ac890 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a f( *z=='e' || *z
ac8a0 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 69 6e 74 =='E' ){. int
ac8b0 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 20 20 esign = 1;.
ac8c0 69 6e 74 20 65 76 61 6c 20 3d 20 30 3b 0a 20 20 int eval = 0;.
ac8d0 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 LONGDOUBLE_TYP
ac8e0 45 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 E scale = 1.0;.
ac8f0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20 z++;. if(
ac900 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 *z=='-' ){.
ac910 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 esign = -1;.
ac920 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 z++;. }els
ac930 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b e if( *z=='+' ){
ac940 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 . z++;.
ac950 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c }. while( sql
ac960 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 ite3Isdigit(*z)
ac970 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 3d 20 ){. eval =
ac980 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20 27 eval*10 + *z - '
ac990 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 0';. z++;.
ac9a0 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 }. while(
ac9b0 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 61 6c eval>=64 ){ scal
ac9c0 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 65 76 e *= 1.0e+64; ev
ac9d0 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 20 20 al -= 64; }.
ac9e0 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 36 20 while( eval>=16
ac9f0 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 ){ scale *= 1.0e
aca00 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 36 3b +16; eval -= 16;
aca10 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 }. while( ev
aca20 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 20 2a al>=4 ){ scale *
aca30 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c 20 2d = 1.0e+4; eval -
aca40 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 = 4; }. while
aca50 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 63 61 ( eval>=1 ){ sca
aca60 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 76 le *= 1.0e+1; ev
aca70 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 69 al -= 1; }. i
aca80 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 f( esign<0 ){.
aca90 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c 65 3b v1 /= scale;
acaa0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
acab0 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b 0a 20 v1 *= scale;.
acac0 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 }. }. *pRes
acad0 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 28 73 ult = (double)(s
acae0 69 67 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 31 ign<0 ? -v1 : v1
acaf0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 );. return (int
acb00 29 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 )(z - zBegin);.#
acb10 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 else. return sq
acb20 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 lite3Atoi64(z, p
acb30 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 Result);.#endif
acb40 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 /* SQLITE_OMIT_F
acb50 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f LOATING_POINT */
acb60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 .}../*.** Compar
acb70 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 e the 19-charact
acb80 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 er string zNum a
acb90 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 gainst the text
acba0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a representation.*
acbb0 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 * value 2^63: 9
acbc0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
acbd0 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 08. Return nega
acbe0 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 tive, zero, or p
acbf0 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e ositive.** if zN
acc00 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c um is less than,
acc10 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 equal to, or gr
acc20 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 eater than the s
acc30 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c tring..**.** Unl
acc40 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 ike memcmp() thi
acc50 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 s routine is gua
acc60 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 ranteed to retur
acc70 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 n the difference
acc80 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 .** in the value
acc90 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 s of the last di
acca0 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 git if the only
accb0 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e difference is in
accc0 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 the.** last dig
accd0 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 it. So, for exa
acce0 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 mple,.**.**
accf0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 compare2pow63("
acd00 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
acd10 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 800").**.** will
acd20 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 return -8..*/.s
acd30 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 tatic int compar
acd40 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 e2pow63(const ch
acd50 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 ar *zNum){. int
acd60 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 c;. c = memcmp
acd70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 (zNum,"922337203
acd80 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b 0a 685477580",18);.
acd90 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 if( c==0 ){.
acda0 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d c = zNum[18] -
acdb0 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75 '8';. }. retu
acdc0 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn c;.}.../*.**
acdd0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a Return TRUE if z
acde0 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 20 Num is a 64-bit
acdf0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 signed integer a
ace00 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 nd write.** the
ace10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 value of the int
ace20 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e eger into *pNum.
ace30 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 If zNum is not
ace40 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f an integer.** o
ace50 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 r is an integer
ace60 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 that is too larg
ace70 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 e to be expresse
ace80 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c 0a d with 64 bits,.
ace90 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 ** then return f
acea0 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e alse..**.** When
aceb0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
acec0 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 69 s originally wri
aced0 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 69 tten it dealt wi
acee0 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 th only.** 32-bi
acef0 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 74 t numbers. At t
acf00 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 73 hat time, it was
acf10 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61 much faster tha
acf20 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20 n the.** atoi()
acf30 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20 library routine
acf40 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a in RedHat 7.2..*
acf50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
acf60 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 int sqlite3Atoi
acf70 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 64(const char *z
acf80 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b Num, i64 *pNum){
acf90 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 . i64 v = 0;.
acfa0 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69 int neg;. int i
acfb0 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 , c;. const cha
acfc0 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 69 r *zStart;. whi
acfd0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 le( sqlite3Isspa
acfe0 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d ce(*zNum) ) zNum
acff0 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d ++;. if( *zNum=
ad000 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 ='-' ){. neg
ad010 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b = 1;. zNum++;
ad020 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e . }else if( *zN
ad030 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e um=='+' ){. n
ad040 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d eg = 0;. zNum
ad050 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ++;. }else{.
ad060 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 neg = 0;. }.
ad070 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 zStart = zNum;.
ad080 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d while( zNum[0]=
ad090 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 ='0' ){ zNum++;
ad0a0 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c } /* Skip over l
ad0b0 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 eading zeros. Ti
ad0c0 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 cket #2454 */.
ad0d0 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d for(i=0; (c=zNum
ad0e0 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d [i])>='0' && c<=
ad0f0 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 '9'; i++){. v
ad100 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 = v*10 + c - '0
ad110 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d ';. }. *pNum =
ad120 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 neg ? -v : v;.
ad130 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d if( c!=0 || (i=
ad140 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e =0 && zStart==zN
ad150 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 um) || i>19 ){.
ad160 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d /* zNum is em
ad170 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 pty or contains
ad180 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 non-numeric text
ad190 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 or is longer.
ad1a0 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67 ** than 19 dig
ad1b0 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61 6e its (thus guaran
ad1c0 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 20 ting that it is
ad1d0 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 too large) */.
ad1e0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
ad1f0 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a lse if( i<19 ){.
ad200 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e /* Less than
ad210 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77 19 digits, so w
ad220 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66 e know that it f
ad230 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a its in 64 bits *
ad240 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a /. return 1;.
ad250 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
ad260 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73 19-digit numbers
ad270 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 must be no larg
ad280 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 30 er than 92233720
ad290 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20 70 36854775807 if p
ad2a0 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f ositive. ** o
ad2b0 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 r 92233720368547
ad2c0 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 76 75808 if negativ
ad2d0 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32 e. Note that 92
ad2e0 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38 30 2337203685466580
ad2f0 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 8. ** is 2^63
ad300 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
ad310 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e compare2pow63(zN
ad320 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a um)<neg;. }.}..
ad330 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 /*.** The string
ad340 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 zNum represents
ad350 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 an unsigned int
ad360 65 67 65 72 2e 20 20 54 68 65 72 65 20 6d 69 67 eger. There mig
ad370 68 74 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 ht be some other
ad380 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
ad390 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 69 6e following the in
ad3a0 74 65 67 65 72 20 74 6f 6f 2c 20 62 75 74 20 74 teger too, but t
ad3b0 68 61 74 20 70 61 72 74 20 69 73 20 69 67 6e 6f hat part is igno
ad3c0 72 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 red..** If the i
ad3d0 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20 nteger that the
ad3e0 70 72 65 66 69 78 20 6f 66 20 7a 4e 75 6d 20 72 prefix of zNum r
ad3f0 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20 66 epresents will f
ad400 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 it in a.** 64-bi
ad410 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
ad420 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 , return TRUE.
ad430 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e Otherwise return
ad440 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 FALSE..**.** If
ad450 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70 61 72 the negFlag par
ad460 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2c 20 ameter is true,
ad470 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
ad480 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65 70 72 zNum really repr
ad490 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65 67 61 esents.** a nega
ad4a0 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 28 54 tive number. (T
ad4b0 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22 20 69 he leading "-" i
ad4c0 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 7a s omitted from z
ad4d0 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a 20 70 Num.) This.** p
ad4e0 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 65 64 arameter is need
ad4f0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
ad500 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73 65 2e a boundary case.
ad510 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 A string.** of
ad520 20 22 39 32 32 33 33 37 33 30 33 36 38 35 34 37 "92233730368547
ad530 37 35 38 30 38 22 20 72 65 74 75 72 6e 73 20 66 75808" returns f
ad540 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61 67 20 alse if negFlag
ad550 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72 75 65 is false or true
ad560 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67 20 69 .** if negFlag i
ad570 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 s true..**.** Le
ad580 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72 65 20 ading zeros are
ad590 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ignored..*/.SQLI
ad5a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ad5b0 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 qlite3FitsIn64Bi
ad5c0 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a ts(const char *z
ad5d0 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 Num, int negFlag
ad5e0 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 ){. int i, c;.
ad5f0 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 int neg = 0;..
ad600 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d assert( zNum[0]
ad610 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d >='0' && zNum[0]
ad620 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d <='9' ); /* zNum
ad630 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 is an unsigned
ad640 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 number */.. if(
ad650 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d negFlag ) neg =
ad660 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 1-neg;. while(
ad670 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 *zNum=='0' ){.
ad680 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 zNum++; /*
ad690 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 Skip leading zer
ad6a0 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 os. Ticket #245
ad6b0 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 4 */. }. for(i
ad6c0 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e =0; (c=zNum[i])>
ad6d0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 ='0' && c<='9';
ad6e0 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 3c 31 i++){}. if( i<1
ad6f0 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 9 ){. /* Guar
ad700 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66 anteed to fit if
ad710 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 less than 19 di
ad720 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 gits */. retu
ad730 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rn 1;. }else if
ad740 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a ( i>19 ){. /*
ad750 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 Guaranteed to b
ad760 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65 e too big if gre
ad770 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67 ater than 19 dig
ad780 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 its */. retur
ad790 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 0;. }else{.
ad7a0 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61 /* Compare aga
ad7b0 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 inst 2^63. */.
ad7c0 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 return compare
ad7d0 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 2pow63(zNum)<neg
ad7e0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
ad7f0 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 f zNum represent
ad800 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 s an integer tha
ad810 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32 t will fit in 32
ad820 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a -bits, then set.
ad830 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68 ** *pValue to th
ad840 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 at integer and r
ad850 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 eturn true. Oth
ad860 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61 erwise return fa
ad870 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e lse..**.** Any n
ad880 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61 on-numeric chara
ad890 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f cters that follo
ad8a0 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67 wing zNum are ig
ad8b0 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69 nored..** This i
ad8c0 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d s different from
ad8d0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29 sqlite3Atoi64()
ad8e0 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20 which requires
ad8f0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d the.** input num
ad900 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74 ber to be zero-t
ad910 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 erminated..*/.SQ
ad920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ad930 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 sqlite3GetInt32
ad940 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 (const char *zNu
ad950 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b m, int *pValue){
ad960 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
ad970 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 v = 0;. int i,
ad980 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 c;. int neg = 0
ad990 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d ;. if( zNum[0]=
ad9a0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 ='-' ){. neg
ad9b0 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b = 1;. zNum++;
ad9c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75 . }else if( zNu
ad9d0 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 m[0]=='+' ){.
ad9e0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 zNum++;. }. w
ad9f0 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 hile( zNum[0]=='
ada00 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 0' ) zNum++;. f
ada10 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20 or(i=0; i<11 &&
ada20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 (c = zNum[i] - '
ada30 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20 0')>=0 && c<=9;
ada40 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a i++){. v = v*
ada50 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 10 + c;. }.. /
ada60 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65 * The longest de
ada70 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61 cimal representa
ada80 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74 tion of a 32 bit
ada90 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64 integer is 10 d
adaa0 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a igits:. **. **
adab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 123
adac0 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20 4567890. **
adad0 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33 2^31 -> 2147483
adae0 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 648. */. if( i
adaf0 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 >10 ){. retur
adb00 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 n 0;. }. if( v
adb10 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20 -neg>2147483647
adb20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
adb30 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29 . }. if( neg )
adb40 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20 {. v = -v;.
adb50 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69 }. *pValue = (i
adb60 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31 nt)v;. return 1
adb70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 ;.}../*.** The v
adb80 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
adb90 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20 nteger encoding
adba0 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
adbb0 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 *.** KEY:.**
adbc0 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 A = 0xxxxxx
adbd0 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 x 7 bits of d
adbe0 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 ata and one flag
adbf0 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 bit.**
adc00 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 B = 1xxxxxxx
adc10 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 7 bits of data a
adc20 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a nd one flag bit.
adc30 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78 ** C = x
adc40 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74 xxxxxxx 8 bit
adc50 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 s of data.**.**
adc60 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 7 bits - A.** 1
adc70 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 4 bits - BA.** 2
adc80 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 1 bits - BBA.**
adc90 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a 28 bits - BBBA.*
adca0 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42 * 35 bits - BBBB
adcb0 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42 A.** 42 bits - B
adcc0 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73 BBBBA.** 49 bits
adcd0 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36 - BBBBBBA.** 56
adce0 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41 bits - BBBBBBBA
adcf0 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42 .** 64 bits - BB
add00 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a BBBBBBC.*/../*.*
add10 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74 * Write a 64-bit
add20 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
add30 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f integer to memo
add40 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 ry starting at p
add50 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 [0]..** The leng
add60 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65 th of data write
add70 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e will be between
add80 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20 1 and 9 bytes.
add90 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f The number.** o
adda0 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 f bytes written
addb0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
addc0 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 ** A variable-le
addd0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e ngth integer con
adde0 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77 sists of the low
addf0 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 er 7 bits of eac
ade00 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c h byte.** for al
ade10 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 l bytes that hav
ade20 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65 e the 8th bit se
ade30 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77 t and one byte w
ade40 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62 ith the 8th.** b
ade50 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70 it clear. Excep
ade60 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20 t, if we get to
ade70 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74 the 9th byte, it
ade80 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c stores the full
ade90 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69 .** 8 bits and i
adea0 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e s the last byte.
adeb0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
adec0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
aded0 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 tVarint(unsigned
adee0 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29 char *p, u64 v)
adef0 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b {. int i, j, n;
adf00 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20 . u8 buf[10];.
adf10 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29 if( v & (((u64)
adf20 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29 0xff000000)<<32)
adf30 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28 ){. p[8] = (
adf40 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 u8)v;. v >>=
adf50 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20 8;. for(i=7;
adf60 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 i>=0; i--){.
adf70 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76 p[i] = (u8)((v
adf80 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 & 0x7f) | 0x80)
adf90 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b ;. v >>= 7;
adfa0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
adfb0 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e n 9;. } . n
adfc0 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 = 0;. do{.
adfd0 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 buf[n++] = (u8)(
adfe0 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 (v & 0x7f) | 0x8
adff0 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0);. v >>= 7;
ae000 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 . }while( v!=0
ae010 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30 );. buf[0] &= 0
ae020 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e x7f;. assert( n
ae030 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 <=9 );. for(i=0
ae040 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a , j=n-1; j>=0; j
ae050 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b --, i++){. p[
ae060 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d i] = buf[j];. }
ae070 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
ae080 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
ae090 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76 ne is a faster v
ae0a0 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 ersion of sqlite
ae0b0 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61 3PutVarint() tha
ae0c0 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 t only.** works
ae0d0 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74 for 32-bit posit
ae0e0 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 ive integers and
ae0f0 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69 which is optimi
ae100 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 zed for.** the c
ae110 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d ommon case of sm
ae120 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41 all integers. A
ae130 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 MACRO version,
ae140 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 putVarint32,.**
ae150 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 is provided whic
ae160 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69 h inlines the si
ae170 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 ngle-byte case.
ae180 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 All code should
ae190 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52 use.** the MACR
ae1a0 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69 O version as thi
ae1b0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
ae1c0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
ae1d0 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61 te case has.** a
ae1e0 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 lready been hand
ae1f0 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
ae200 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ae210 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e e3PutVarint32(un
ae220 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
ae230 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20 u32 v){.#ifndef
ae240 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 putVarint32. if
ae250 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30 ( (v & ~0x7f)==0
ae260 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76 ){. p[0] = v
ae270 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
ae280 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 }.#endif. if(
ae290 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d (v & ~0x3fff)==
ae2a0 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 0 ){. p[0] =
ae2b0 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78 (u8)((v>>7) | 0x
ae2c0 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 80);. p[1] =
ae2d0 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a (u8)(v & 0x7f);.
ae2e0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
ae2f0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
ae300 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76 e3PutVarint(p, v
ae310 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
ae320 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 a 64-bit variab
ae330 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
ae340 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 r from memory st
ae350 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a arting at p[0]..
ae360 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
ae370 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
ae380 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 ad. The value i
ae390 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a s stored in *v..
ae3a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ae3b0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 E u8 sqlite3GetV
ae3c0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 arint(const unsi
ae3d0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 gned char *p, u6
ae3e0 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 4 *v){. u32 a,b
ae3f0 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 ,s;.. a = *p;.
ae400 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 /* a: p0 (unmas
ae410 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
ae420 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
ae430 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 *v = a;. ret
ae440 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 1;. }.. p+
ae450 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f +;. b = *p;. /
ae460 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 * b: p1 (unmaske
ae470 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
ae480 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 0x80)). {. a
ae490 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 &= 0x7f;. a
ae4a0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
ae4b0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a b;. *v = a;.
ae4c0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
ae4d0 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 }.. p++;. a =
ae4e0 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
ae4f0 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 ;. /* a: p0<<14
ae500 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 | p2 (unmasked)
ae510 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
ae520 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
ae530 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
ae540 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 7f);. b &= 0x
ae550 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 7f;. b = b<<7
ae560 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
ae570 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
ae580 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f turn 3;. }.. /
ae590 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f * CSE1 from belo
ae5a0 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 w */. a &= (0x7
ae5b0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
ae5c0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
ae5d0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
ae5e0 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
ae5f0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
ae600 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
ae610 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 . {. b &= (0
ae620 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ae630 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 . /* moved CS
ae640 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 E1 up */. /*
ae650 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
ae660 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 (0x7f); */. a
ae670 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
ae680 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b = b;. *v = a;
ae690 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 . return 4;.
ae6a0 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c }.. /* a: p0<<
ae6b0 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 14 | p2 (masked)
ae6c0 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c */. /* b: p1<<
ae6d0 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 14 | p3 (unmaske
ae6e0 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 d) */. /* 1:sav
ae6f0 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 e off p0<<21 | p
ae700 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 1<<14 | p2<<7 |
ae710 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 p3 (masked) */.
ae720 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 /* moved CSE1 u
ae730 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 p */. /* a &= (
ae740 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
ae750 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 ; */. b &= (0x7
ae760 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
ae770 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 s = a;. /* s:
ae780 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 p0<<14 | p2 (mas
ae790 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a ked) */.. p++;.
ae7a0 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
ae7b0 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
ae7c0 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 p0<<28 | p2<<14
ae7d0 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p4 (unmasked)
ae7e0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
ae7f0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 0)). {. /* w
ae800 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65 e can skip these
ae810 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65 cause they were
ae820 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 (effectively) d
ae830 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c one above in cal
ae840 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f c'ing s */. /
ae850 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 * a &= (0x7f<<28
ae860 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
ae870 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 7f); */. /* b
ae880 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
ae890 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20 0x7f); */. b
ae8a0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = b<<7;. a |=
ae8b0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 b;. s = s>>1
ae8c0 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 8;. *v = ((u6
ae8d0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
ae8e0 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a return 5;. }.
ae8f0 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66 . /* 2:save off
ae900 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 p0<<21 | p1<<14
ae910 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d | p2<<7 | p3 (m
ae920 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20 asked) */. s =
ae930 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a s<<7;. s |= b;.
ae940 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c /* s: p0<<21 |
ae950 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 p1<<14 | p2<<7
ae960 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f | p3 (masked) */
ae970 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 .. p++;. b = b
ae980 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b <<14;. b |= *p;
ae990 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20 . /* b: p1<<28
ae9a0 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75 | p3<<14 | p5 (u
ae9b0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
ae9c0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
ae9d0 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 . /* we can s
ae9e0 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69 kip this cause i
ae9f0 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65 t was (effective
aea00 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 ly) done above i
aea10 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a n calc'ing s */.
aea20 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 /* b &= (0x7
aea30 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
aea40 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 )|(0x7f); */.
aea50 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
aea60 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d |(0x7f);. a =
aea70 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 a<<7;. a |=
aea80 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 b;. s = s>>18
aea90 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 ;. *v = ((u64
aeaa0 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 )s)<<32 | a;.
aeab0 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a return 6;. }..
aeac0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c p++;. a = a<<
aead0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 14;. a |= *p;.
aeae0 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20 /* a: p2<<28 |
aeaf0 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d p4<<14 | p6 (unm
aeb00 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
aeb10 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
aeb20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 a &= (0x1f<<2
aeb30 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
aeb40 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 x7f);. b &= (
aeb50 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
aeb60 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a ;. b = b<<7;.
aeb70 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 a |= b;.
aeb80 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a s = s>>11;. *
aeb90 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 v = ((u64)s)<<32
aeba0 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e | a;. return
aebb0 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 7;. }.. /* CS
aebc0 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f E2 from below */
aebd0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 . a &= (0x7f<<1
aebe0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 4)|(0x7f);. p++
aebf0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
aec00 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
aec10 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 : p3<<28 | p5<<1
aec20 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 4 | p7 (unmasked
aec30 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
aec40 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 x80)). {. b
aec50 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 &= (0x1f<<28)|(0
aec60 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
aec70 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 . /* moved CS
aec80 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 E2 up */. /*
aec90 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
aeca0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 (0x7f); */. a
aecb0 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
aecc0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
aecd0 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 4;. *v = ((u6
aece0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
aecf0 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a return 8;. }.
aed00 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
aed10 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <15;. a |= *p;.
aed20 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c /* a: p4<<29 |
aed30 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e p6<<15 | p8 (un
aed40 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a masked) */.. /*
aed50 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a moved CSE2 up *
aed60 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 /. /* a &= (0x7
aed70 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 f<<29)|(0x7f<<15
aed80 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 )|(0xff); */. b
aed90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
aeda0 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 0x7f);. b = b<<
aedb0 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 8;. a |= b;..
aedc0 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 s = s<<4;. b =
aedd0 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78 p[-4];. b &= 0x
aede0 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 7f;. b = b>>3;.
aedf0 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 s |= b;.. *v
aee00 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c = ((u64)s)<<32 |
aee10 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b a;.. return 9;
aee20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
aee30 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65 32-bit variable
aee40 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
aee50 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 from memory star
aee60 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a ting at p[0]..**
aee70 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
aee80 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 er of bytes read
aee90 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 . The value is
aeea0 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a stored in *v..**
aeeb0 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e A MACRO version
aeec0 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69 , getVarint32, i
aeed0 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 s provided which
aeee0 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a inlines the .**
aeef0 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 single-byte cas
aef00 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f e. All code sho
aef10 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 uld use the MACR
aef20 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a O version as .**
aef30 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
aef40 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c ssumes the singl
aef50 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 20 e-byte case has
aef60 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e already been han
aef70 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dled..*/.SQLITE_
aef80 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 PRIVATE u8 sqlit
aef90 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f e3GetVarint32(co
aefa0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
aefb0 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 r *p, u32 *v){.
aefc0 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 u32 a,b;.. /*
aefd0 54 68 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e The 1-byte case.
aefe0 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 Overwhelmingly
aeff0 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e the most common
af000 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e . Handled inlin
af010 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65 e. ** by the ge
af020 74 56 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f tVarin32() macro
af030 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 */. a = *p;.
af040 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b /* a: p0 (unmask
af050 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 ed) */.#ifndef g
af060 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 etVarint32. if
af070 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
af080 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 /* Values be
af090 74 77 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20 tween 0 and 127
af0a0 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 */. *v = a;.
af0b0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
af0c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 .#endif.. /* Th
af0d0 65 20 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f e 2-byte case */
af0e0 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 . p++;. b = *p
af0f0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e ;. /* b: p1 (un
af100 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
af110 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(b&0x80)). {.
af120 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 /* Values be
af130 74 77 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 tween 128 and 16
af140 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 383 */. a &=
af150 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 0x7f;. a = a<
af160 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c <7;. *v = a |
af170 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 b;. return 2
af180 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
af190 33 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 3-byte case */.
af1a0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
af1b0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
af1c0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* a: p0<<14 | p
af1d0 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 2 (unmasked) */.
af1e0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
af1f0 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 . {. /* Valu
af200 65 73 20 62 65 74 77 65 65 6e 20 31 36 33 38 34 es between 16384
af210 20 61 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a and 2097151 */.
af220 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c a &= (0x7f<<
af230 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
af240 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 b &= 0x7f;. b
af250 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 = b<<7;. *v
af260 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 = a | b;. ret
af270 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 3;. }.. /*
af280 20 41 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74 A 32-bit varint
af290 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 is used to stor
af2a0 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 e size informati
af2b0 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 on in btrees..
af2c0 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72 ** Objects are r
af2d0 61 72 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61 arely larger tha
af2e0 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 n 2MiB limit of
af2f0 61 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e a 3-byte varint.
af300 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 . ** A 3-byte v
af310 61 72 69 6e 74 20 69 73 20 73 75 66 66 69 63 69 arint is suffici
af320 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 ent, for example
af330 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 , to record the
af340 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 size. ** of a 1
af350 30 34 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 048569-byte BLOB
af360 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a or string.. **
af370 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e . ** We only un
af380 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31 roll the first 1
af390 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 -, 2-, and 3- by
af3a0 74 65 20 63 61 73 65 73 2e 20 20 54 68 65 20 76 te cases. The v
af3b0 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 ery. ** rare la
af3c0 72 67 65 72 20 63 61 73 65 73 20 63 61 6e 20 62 rger cases can b
af3d0 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 e handled by the
af3e0 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76 slower 64-bit v
af3f0 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 arint. ** routi
af400 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 ne.. */.#if 1.
af410 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a {. u64 v64;.
af420 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 u8 n;.. p
af430 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 -= 2;. n = s
af440 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
af450 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 p, &v64);. as
af460 73 65 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d sert( n>3 && n<=
af470 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 9 );. *v = (u
af480 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 75 32)v64;. retu
af490 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 rn n;. }..#else
af4a0 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 . /* For follow
af4b0 69 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 ing code (kept f
af4c0 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 or historical re
af4d0 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 cord only) shows
af4e0 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 an. ** unrolli
af4f0 6e 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e ng for the 3- an
af500 64 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 d 4-byte varint
af510 63 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 cases. This cod
af520 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 e is. ** slight
af530 6c 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 ly faster, but i
af540 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 t is also larger
af550 20 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 and much harder
af560 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 to test.. */.
af570 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
af580 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
af590 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
af5a0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
af5b0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
af5c0 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 . {. /* Valu
af5d0 65 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 es between 20971
af5e0 35 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 52 and 268435455
af5f0 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 */. b &= (0x
af600 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
af610 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c a &= (0x7f<<
af620 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
af630 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 a = a<<7;. *v
af640 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 = a | b;. re
af650 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 turn 4;. }.. p
af660 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
af670 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
af680 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c a: p0<<28 | p2<
af690 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b <14 | p4 (unmask
af6a0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 ed) */. if (!(a
af6b0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
af6c0 2f 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 /* Walues betwe
af6d0 65 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 en 268435456 and
af6e0 20 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 34359738367 */.
af6f0 20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c a &= (0x1f<<
af700 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
af710 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 0x7f);. b &=
af720 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 (0x1f<<28)|(0x7f
af730 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
af740 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
af750 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
af760 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 return 5;. }..
af770 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 /* We can only
af780 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
af790 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 when reading a
af7a0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
af7b0 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 . ** file. In
af7c0 74 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65 that case we are
af7d0 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 not in any hurr
af7e0 79 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c y. Use the (rel
af7f0 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f atively. ** slo
af800 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f w) general-purpo
af810 73 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 se sqlite3GetVar
af820 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f int() routine to
af830 20 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a extract the. *
af840 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a * value. */. {.
af850 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 u64 v64;.
af860 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d u8 n;.. p -=
af870 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 4;. n = sqli
af880 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 te3GetVarint(p,
af890 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 &v64);. asser
af8a0 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 t( n>5 && n<=9 )
af8b0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 ;. *v = (u32)
af8c0 76 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 v64;. return
af8d0 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a n;. }.#endif.}.
af8e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
af8f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
af900 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e s that will be n
af910 65 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 eeded to store t
af920 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 he given.** 64-b
af930 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 it integer..*/.S
af940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
af950 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c t sqlite3VarintL
af960 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 en(u64 v){. int
af970 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 i = 0;. do{.
af980 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d i++;. v >>=
af990 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 7;. }while( v!
af9a0 3d 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 =0 && ALWAYS(i<9
af9b0 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b ) );. return i;
af9c0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .}.../*.** Read
af9d0 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d or write a four-
af9e0 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
af9f0 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a integer value..*
afa00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
afa10 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 u32 sqlite3Get4
afa20 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 byte(const u8 *p
afa30 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 ){. return (p[0
afa40 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c ]<<24) | (p[1]<<
afa50 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 16) | (p[2]<<8)
afa60 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 | p[3];.}.SQLITE
afa70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
afa80 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e lite3Put4byte(un
afa90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
afaa0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d u32 v){. p[0] =
afab0 20 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 (u8)(v>>24);.
afac0 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 p[1] = (u8)(v>>1
afad0 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 6);. p[2] = (u8
afae0 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 )(v>>8);. p[3]
afaf0 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 = (u8)v;.}....#i
afb00 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
afb10 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
afb20 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 RAL) || defined(
afb30 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
afb40 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 )./*.** Translat
afb50 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 e a single byte
afb60 6f 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 of Hex into an i
afb70 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 nteger..** This
afb80 72 6f 75 74 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f routinen only wo
afb90 72 6b 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 rks if h really
afba0 69 73 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 is a valid hexad
afbb0 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 ecimal.** charac
afbc0 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e ter: 0..9a..fA.
afbd0 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 .F.*/.static u8
afbe0 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b hexToInt(int h){
afbf0 0a 20 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 . assert( (h>='
afc00 30 27 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 0' && h<='9') ||
afc10 20 20 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d (h>='a' && h<=
afc20 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 'f') || (h>='A'
afc30 20 26 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 && h<='F') );.#
afc40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 ifdef SQLITE_ASC
afc50 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 II. h += 9*(1&(
afc60 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 h>>6));.#endif.#
afc70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 ifdef SQLITE_EBC
afc80 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 DIC. h += 9*(1&
afc90 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 ~(h>>4));.#endif
afca0 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 . return (u8)(h
afcb0 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 & 0xf);.}.#endi
afcc0 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 f /* !SQLITE_OMI
afcd0 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c T_BLOB_LITERAL |
afce0 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 | SQLITE_HAS_COD
afcf0 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 EC */..#if !defi
afd00 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
afd10 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c BLOB_LITERAL) ||
afd20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
afd30 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a HAS_CODEC)./*.**
afd40 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 Convert a BLOB
afd50 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 literal of the f
afd60 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 orm "x'hhhhhh'"
afd70 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a into its binary.
afd80 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 ** value. Retur
afd90 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
afda0 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e ts binary value.
afdb0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
afdc0 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 the.** binary va
afdd0 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 lue has been obt
afde0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
afdf0 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 c and must be fr
afe00 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 eed by.** the ca
afe10 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a lling routine..*
afe20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
afe30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 void *sqlite3He
afe40 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 xToBlob(sqlite3
afe50 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
afe60 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 *z, int n){. ch
afe70 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 ar *zBlob;. int
afe80 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 i;.. zBlob = (
afe90 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 char *)sqlite3Db
afea0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f MallocRaw(db, n/
afeb0 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 2 + 1);. n--;.
afec0 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 if( zBlob ){.
afed0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 for(i=0; i<n;
afee0 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c i+=2){. zBl
afef0 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f ob[i/2] = (hexTo
aff00 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 Int(z[i])<<4) |
aff10 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 hexToInt(z[i+1])
aff20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f ;. }. zBlo
aff30 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a b[i/2] = 0;. }.
aff40 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a return zBlob;.
aff50 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
aff60 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 ITE_OMIT_BLOB_LI
aff70 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f TERAL || SQLITE_
aff80 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f HAS_CODEC */.../
aff90 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
affa0 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f sqlite.magic fro
affb0 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f m SQLITE_MAGIC_O
affc0 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 PEN to SQLITE_MA
affd0 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 GIC_BUSY..** Ret
affe0 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f urn an error (no
afff0 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d n-zero) if the m
b0000 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c agic was not SQL
b0010 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a ITE_MAGIC_OPEN.*
b0020 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * when this rout
b0030 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ine is called..*
b0040 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b0050 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e e is called when
b0060 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c entering an SQL
b0070 69 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 ite API. The SQ
b0080 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a LITE_MAGIC_OPEN.
b0090 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 ** value indicat
b00a0 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 es that the data
b00b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
b00c0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 passed into the
b00d0 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 API is.** open a
b00e0 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 nd is not being
b00f0 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 used by another
b0100 74 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e thread. By chan
b0110 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a ging the value.*
b0120 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 * to SQLITE_MAGI
b0130 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 C_BUSY we indica
b0140 74 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e te that the conn
b0150 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 ection is in use
b0160 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 ..** sqlite3Safe
b0170 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 tyOff() below wi
b0180 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 ll change the va
b0190 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 lue back to SQLI
b01a0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a TE_MAGIC_OPEN.**
b01b0 20 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 when the API ex
b01c0 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 its. .**.** This
b01d0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 routine is a at
b01e0 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 tempt to detect
b01f0 69 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 if two threads u
b0200 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 se the.** same s
b0210 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 qlite* pointer a
b0220 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e t the same time.
b0230 20 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63 There is a rac
b0240 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 e .** condition
b0250 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c so it is possibl
b0260 65 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 e that the error
b0270 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 is not detected
b0280 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 ..** But usually
b0290 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c the problem wil
b02a0 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 l be seen. The
b02b0 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 result will be a
b02c0 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 n.** error which
b02d0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
b02e0 64 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 debug the applic
b02f0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a ation that is.**
b0300 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e using SQLite in
b0310 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a correctly..**.**
b0320 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 Ticket #202: I
b0330 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e f db->magic is n
b0340 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 ot a valid open
b0350 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 value, take care
b0360 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 not.** to modif
b0370 79 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75 y the db structu
b0380 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 re at all. It c
b0390 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 ould be that db
b03a0 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f is a stale.** po
b03b0 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 inter. In other
b03c0 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 words, it could
b03d0 20 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68 be that there h
b03e0 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a as been a prior.
b03f0 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ** call to sqlit
b0400 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 e3_close(db) and
b0410 20 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 db has been dea
b0420 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 llocated. And w
b0430 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 e do.** not want
b0440 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 to write into d
b0450 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 eallocated memor
b0460 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c y..*/.#ifdef SQL
b0470 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
b0480 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b0490 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c ite3SafetyOn(sql
b04a0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 ite3 *db){. if(
b04b0 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 db->magic==SQLI
b04c0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b TE_MAGIC_OPEN ){
b04d0 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d . db->magic =
b04e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b04f0 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 SY;. assert(
b0500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
b0510 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
b0520 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b0530 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d }else if( db->m
b0540 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 agic==SQLITE_MAG
b0550 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 IC_BUSY ){. d
b0560 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 b->magic = SQLIT
b0570 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 E_MAGIC_ERROR;.
b0580 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 db->u1.isInte
b0590 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d rrupted = 1;. }
b05a0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
b05b0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 endif../*.** Cha
b05c0 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 nge the magic fr
b05d0 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f om SQLITE_MAGIC_
b05e0 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d BUSY to SQLITE_M
b05f0 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 AGIC_OPEN..** Re
b0600 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e turn an error (n
b0610 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 on-zero) if the
b0620 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 magic was not SQ
b0630 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a LITE_MAGIC_BUSY.
b0640 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 ** when this rou
b0650 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a tine is called..
b0660 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b0670 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
b0680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0690 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 3SafetyOff(sqlit
b06a0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 e3 *db){. if( d
b06b0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
b06c0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 _MAGIC_BUSY ){.
b06d0 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 db->magic = S
b06e0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b06f0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
b0700 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
b0710 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
b0720 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b0730 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 else{. db->ma
b0740 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 gic = SQLITE_MAG
b0750 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 IC_ERROR;. db
b0760 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 ->u1.isInterrupt
b0770 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
b0780 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 rn 1;. }.}.#end
b0790 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 if../*.** Check
b07a0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 to make sure we
b07b0 68 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 have a valid db
b07c0 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 pointer. This t
b07d0 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f est is not.** fo
b07e0 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 olproof but it d
b07f0 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 oes provide some
b0800 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 measure of prot
b0810 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a ection against.*
b0820 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 * misuse of the
b0830 69 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61 interface such a
b0840 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 s passing in db
b0850 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 pointers that ar
b0860 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 e.** NULL or whi
b0870 63 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 ch have been pre
b0880 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 viously closed.
b0890 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
b08a0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 returns.** 1 it
b08b0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
b08c0 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 db pointer is va
b08d0 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 lid and 0 if it
b08e0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
b08f0 20 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f dereferenced fo
b0900 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 r any reason. T
b0910 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
b0920 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b ion should invok
b0930 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 e.** SQLITE_MISU
b0940 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a SE immediately..
b0950 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 **.** sqlite3Saf
b0960 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 etyCheckOk() req
b0970 75 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64 uires that the d
b0980 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c b pointer be val
b0990 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 id for.** use.
b09a0 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 sqlite3SafetyChe
b09b0 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c ckSickOrOk() all
b09c0 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 ows a db pointer
b09d0 20 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a that failed to.
b09e0 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 ** open properly
b09f0 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 and is not fit
b0a00 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 for general use
b0a10 62 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 but which can be
b0a20 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 .** used as an a
b0a30 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
b0a40 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 e3_errmsg() or s
b0a50 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a qlite3_close()..
b0a60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b0a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
b0a80 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 etyCheckOk(sqlit
b0a90 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d e3 *db){. u32 m
b0aa0 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d agic;. if( db==
b0ab0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
b0ac0 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 magic = db->magi
b0ad0 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d c;. if( magic!=
b0ae0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b0af0 4e 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 N .#ifdef SQLITE
b0b00 5f 44 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d _DEBUG. && m
b0b10 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 agic!=SQLITE_MAG
b0b20 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 IC_BUSY.#endif.
b0b30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
b0b40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
b0b50 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 eturn 1;. }.}.S
b0b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b0b70 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 t sqlite3SafetyC
b0b80 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c heckSickOrOk(sql
b0b90 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 ite3 *db){. u32
b0ba0 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 magic;. magic
b0bb0 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 = db->magic;. i
b0bc0 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 f( magic!=SQLITE
b0bd0 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 _MAGIC_SICK &&.
b0be0 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 magic!=SQLI
b0bf0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 TE_MAGIC_OPEN &&
b0c00 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 . magic!=SQ
b0c10 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
b0c20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 ) return 0;. re
b0c30 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a turn 1;.}../****
b0c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
b0c50 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a f util.c *******
b0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
b0c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
b0ca0 20 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a file hash.c ***
b0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
b0ce0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
b0cf0 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
b0d00 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
b0d10 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
b0d20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
b0d30 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
b0d40 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
b0d50 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
b0d60 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
b0d70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
b0d80 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
b0d90 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
b0da0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
b0db0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
b0dc0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
b0dd0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
b0de0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
b0df0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
b0e00 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
b0e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e50 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
b0e60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
b0e70 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d of generic hash-
b0e80 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 tables.** used i
b0e90 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 n SQLite..**.**
b0ea0 24 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e $Id: hash.c,v 1.
b0eb0 33 37 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33 37 2009/05/02 13
b0ec0 3a 32 39 3a 33 38 20 64 72 68 20 45 78 70 20 24 :29:38 drh Exp $
b0ed0 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c .*/../* Turn bul
b0ee0 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 k memory into a
b0ef0 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 hash table objec
b0f00 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e t by initializin
b0f10 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 g the.** fields
b0f20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75 of the Hash stru
b0f30 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e cture..**.** "pN
b0f40 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ew" is a pointer
b0f50 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 to the hash tab
b0f60 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 le that is to be
b0f70 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f initialized..*/
b0f80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0f90 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
b0fa0 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29 Init(Hash *pNew)
b0fb0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 {. assert( pNew
b0fc0 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 !=0 );. pNew->f
b0fd0 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 irst = 0;. pNew
b0fe0 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 ->count = 0;. p
b0ff0 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b New->htsize = 0;
b1000 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b . pNew->ht = 0;
b1010 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c .}../* Remove al
b1020 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 l entries from a
b1030 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 hash table. Re
b1040 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
b1050 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 ..** Call this r
b1060 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 outine to delete
b1070 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 a hash table or
b1080 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 to reset a hash
b1090 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 table.** to the
b10a0 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f empty state..*/
b10b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b10c0 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
b10d0 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b Clear(Hash *pH){
b10e0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
b10f0 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f m; /* Fo
b1100 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 r looping over a
b1110 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ll elements of t
b1120 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 he table */.. a
b1130 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a ssert( pH!=0 );.
b1140 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 elem = pH->fir
b1150 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 st;. pH->first
b1160 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 = 0;. sqlite3_f
b1170 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 ree(pH->ht);. p
b1180 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d H->ht = 0;. pH-
b1190 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 >htsize = 0;. w
b11a0 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 hile( elem ){.
b11b0 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 HashElem *next
b11c0 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 _elem = elem->ne
b11d0 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f xt;. sqlite3_
b11e0 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 free(elem);.
b11f0 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d elem = next_elem
b1200 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e ;. }. pH->coun
b1210 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t = 0;.}../*.**
b1220 54 68 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63 The hashing func
b1230 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
b1240 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72 unsigned int str
b1250 48 61 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20 Hash(const char
b1260 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 *z, int nKey){.
b1270 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 int h = 0;. as
b1280 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b sert( nKey>=0 );
b1290 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e . while( nKey >
b12a0 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 0 ){. h = (
b12b0 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 h<<3) ^ h ^ sqli
b12c0 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
b12d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a (unsigned char)*
b12e0 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d z++];. nKey--
b12f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 ;. }. return h
b1300 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e ;.}.../* Link pN
b1310 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 ew element into
b1320 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 the hash table p
b1330 48 2e 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30 H. If pEntry!=0
b1340 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e then also.** in
b1350 73 65 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 sert pNew into t
b1360 68 65 20 70 45 6e 74 72 79 20 68 61 73 68 20 62 he pEntry hash b
b1370 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ucket..*/.static
b1380 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d void insertElem
b1390 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c ent(. Hash *pH,
b13a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b13b0 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 The complete ha
b13c0 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 sh table */. st
b13d0 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 ruct _ht *pEntry
b13e0 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 , /* The entr
b13f0 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 y into which pNe
b1400 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f w is inserted */
b1410 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 . HashElem *pNe
b1420 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 w /* The
b1430 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 element to be i
b1440 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 nserted */.){.
b1450 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b HashElem *pHead;
b1460 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
b1470 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 element already
b1480 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 in pEntry */. i
b1490 66 28 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 f( pEntry ){.
b14a0 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d pHead = pEntry-
b14b0 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d >count ? pEntry-
b14c0 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 >chain : 0;.
b14d0 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b pEntry->count++;
b14e0 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 . pEntry->cha
b14f0 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c in = pNew;. }el
b1500 73 65 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20 se{. pHead =
b1510 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65 0;. }. if( pHe
b1520 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e ad ){. pNew->
b1530 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 next = pHead;.
b1540 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 pNew->prev = p
b1550 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 Head->prev;.
b1560 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 if( pHead->prev
b1570 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e ){ pHead->prev->
b1580 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 next = pNew; }.
b1590 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 else
b15a0 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 { pH->first
b15b0 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 = pNew; }. pH
b15c0 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 ead->prev = pNew
b15d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
b15e0 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e New->next = pH->
b15f0 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 first;. if( p
b1600 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e H->first ){ pH->
b1610 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e first->prev = pN
b1620 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e ew; }. pNew->
b1630 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 prev = 0;. pH
b1640 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a ->first = pNew;.
b1650 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a }.}.../* Resiz
b1660 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 e the hash table
b1670 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 so that it cant
b1680 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 ains "new_size"
b1690 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 buckets..**.** T
b16a0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 he hash table mi
b16b0 67 68 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69 ght fail to resi
b16c0 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 ze if sqlite3_ma
b16d0 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a lloc() fails or.
b16e0 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 20 73 69 ** if the new si
b16f0 7a 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ze is the same a
b1700 73 20 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65 s the prior size
b1710 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 ..** Return TRUE
b1720 20 69 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f if the resize o
b1730 63 63 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20 ccurs and false
b1740 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 if not..*/.stati
b1750 63 20 69 6e 74 20 72 65 68 61 73 68 28 48 61 73 c int rehash(Has
b1760 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 h *pH, unsigned
b1770 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 int new_size){.
b1780 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 struct _ht *new
b1790 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 _ht;
b17a0 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 /* The new hash
b17b0 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 table */. HashE
b17c0 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 lem *elem, *next
b17d0 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 _elem; /* For
b17e0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 looping over ex
b17f0 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 isting elements
b1800 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d */..#if SQLITE_M
b1810 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
b1820 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a >0. if( new_siz
b1830 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 e*sizeof(struct
b1840 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c _ht)>SQLITE_MALL
b1850 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b OC_SOFT_LIMIT ){
b1860 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 . new_size =
b1870 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
b1880 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 FT_LIMIT/sizeof(
b1890 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d struct _ht);. }
b18a0 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d . if( new_size=
b18b0 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 =pH->htsize ) re
b18c0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a turn 0;.#endif..
b18d0 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 /* The inabili
b18e0 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 ty to allocates
b18f0 73 70 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67 space for a larg
b1900 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 er hash table is
b1910 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 . ** a performa
b1920 6e 63 65 20 68 69 74 20 62 75 74 20 69 74 20 69 nce hit but it i
b1930 73 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 s not a fatal er
b1940 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 ror. So mark th
b1950 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f e. ** allocatio
b1960 6e 20 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 n as a benign..
b1970 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 */. sqlite3Beg
b1980 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
b1990 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 ;. new_ht = (st
b19a0 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 ruct _ht *)sqlit
b19b0 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 e3Malloc( new_si
b19c0 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 ze*sizeof(struct
b19d0 20 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 _ht) );. sqlit
b19e0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
b19f0 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f c();.. if( new_
b1a00 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 ht==0 ) return 0
b1a10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
b1a20 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e (pH->ht);. pH->
b1a30 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 ht = new_ht;. p
b1a40 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f H->htsize = new_
b1a50 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 size = sqlite3Ma
b1a60 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 llocSize(new_ht)
b1a70 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f /sizeof(struct _
b1a80 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 ht);. memset(ne
b1a90 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a w_ht, 0, new_siz
b1aa0 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 e*sizeof(struct
b1ab0 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 _ht));. for(ele
b1ac0 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d m=pH->first, pH-
b1ad0 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 >first=0; elem;
b1ae0 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d elem = next_elem
b1af0 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
b1b00 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 int h = strHash(
b1b10 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d elem->pKey, elem
b1b20 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 ->nKey) % new_si
b1b30 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 ze;. next_ele
b1b40 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
b1b50 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e insertElemen
b1b60 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d t(pH, &new_ht[h]
b1b70 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 , elem);. }. r
b1b80 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 eturn 1;.}../* T
b1b90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f his function (fo
b1ba0 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f r internal use o
b1bb0 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 nly) locates an
b1bc0 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a element in an.**
b1bd0 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 hash table that
b1be0 20 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 matches the giv
b1bf0 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 en key. The has
b1c00 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 h for this key h
b1c10 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 as.** already be
b1c20 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 en computed and
b1c30 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
b1c40 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 4th parameter..
b1c50 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c */.static HashEl
b1c60 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 em *findElementG
b1c70 69 76 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 ivenHash(. cons
b1c80 74 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 t Hash *pH,
b1c90 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 /* The pH to be
b1ca0 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f searched */. co
b1cb0 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 nst char *pKey,
b1cc0 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 /* The key we
b1cd0 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f are searching fo
b1ce0 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c r */. int nKey,
b1cf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
b1d00 74 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 tes in key (not
b1d10 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 counting zero te
b1d20 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 rminator) */. u
b1d30 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 nsigned int h
b1d40 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 /* The hash f
b1d50 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a or this key. */.
b1d60 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 ){. HashElem *e
b1d70 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 lem;
b1d80 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c /* Used to l
b1d90 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 oop thru the ele
b1da0 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 ment list */. i
b1db0 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 nt count;
b1dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
b1dd0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
b1de0 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 nts left to test
b1df0 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 */.. if( pH->h
b1e00 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 t ){. struct
b1e10 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 _ht *pEntry = &p
b1e20 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c H->ht[h];. el
b1e30 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 em = pEntry->cha
b1e40 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 in;. count =
b1e50 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 pEntry->count;.
b1e60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d }else{. elem
b1e70 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 = pH->first;.
b1e80 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f count = pH->co
b1e90 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 unt;. }. while
b1ea0 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 ( count-- && ALW
b1eb0 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 AYS(elem) ){.
b1ec0 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d if( elem->nKey=
b1ed0 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 =nKey && sqlite3
b1ee0 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 StrNICmp(elem->p
b1ef0 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d Key,pKey,nKey)==
b1f00 30 20 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75 0 ){ . retu
b1f10 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 rn elem;. }.
b1f20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e elem = elem->
b1f30 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 next;. }. retu
b1f40 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f rn 0;.}../* Remo
b1f50 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 ve a single entr
b1f60 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 y from the hash
b1f70 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f table given a po
b1f80 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a inter to that.**
b1f90 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 element and a h
b1fa0 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 ash on the eleme
b1fb0 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 nt's key..*/.sta
b1fc0 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 tic void removeE
b1fd0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 lementGivenHash(
b1fe0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 . Hash *pH,
b1ff0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 /* The pH c
b2000 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 ontaining "elem"
b2010 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 */. HashElem*
b2020 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 elem, /* The e
b2030 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d lement to be rem
b2040 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 oved from the pH
b2050 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
b2060 6e 74 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20 nt h /* Hash
b2070 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c value for the el
b2080 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 ement */.){. st
b2090 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 ruct _ht *pEntry
b20a0 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 ;. if( elem->pr
b20b0 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e ev ){. elem->
b20c0 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 prev->next = ele
b20d0 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 m->next; . }els
b20e0 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 e{. pH->first
b20f0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
b2100 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e }. if( elem->n
b2110 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d ext ){. elem-
b2120 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c >next->prev = el
b2130 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 em->prev;. }.
b2140 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 if( pH->ht ){.
b2150 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e pEntry = &pH->
b2160 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 ht[h];. if( p
b2170 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c Entry->chain==el
b2180 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 em ){. pEnt
b2190 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d ry->chain = elem
b21a0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 ->next;. }.
b21b0 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d pEntry->count-
b21c0 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 -;. assert( p
b21d0 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 Entry->count>=0
b21e0 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
b21f0 5f 66 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 _free( elem );.
b2200 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 pH->count--;.
b2210 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 if( pH->count<=0
b2220 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
b2230 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a pH->first==0 );.
b2240 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e assert( pH->
b2250 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 count==0 );.
b2260 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 sqlite3HashClear
b2270 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 (pH);. }.}../*
b2280 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 Attempt to locat
b2290 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 e an element of
b22a0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 the hash table p
b22b0 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 H with a key.**
b22c0 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 that matches pKe
b22d0 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 y,nKey. Return
b22e0 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 the data for thi
b22f0 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 s element if it
b2300 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 is.** found, or
b2310 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 NULL if there is
b2320 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 no match..*/.SQ
b2330 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b2340 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 d *sqlite3HashFi
b2350 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 nd(const Hash *p
b2360 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 H, const char *p
b2370 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a Key, int nKey){.
b2380 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d HashElem *elem
b2390 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d ; /* The elem
b23a0 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 ent that matches
b23b0 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e key */. unsign
b23c0 65 64 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 ed int h; /*
b23d0 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f A hash on key */
b23e0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d .. assert( pH!=
b23f0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
b2400 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 Key!=0 );. asse
b2410 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 rt( nKey>=0 );.
b2420 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 if( pH->ht ){.
b2430 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 h = strHash(p
b2440 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d Key, nKey) % pH-
b2450 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 >htsize;. }else
b2460 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d {. h = 0;. }
b2470 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c . elem = findEl
b2480 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 ementGivenHash(p
b2490 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 H, pKey, nKey, h
b24a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d );. return elem
b24b0 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 ? elem->data :
b24c0 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 0;.}../* Insert
b24d0 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 an element into
b24e0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 the hash table p
b24f0 48 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 H. The key is p
b2500 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 Key,nKey.** and
b2510 74 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74 the data is "dat
b2520 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 a"..**.** If no
b2530 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 element exists w
b2540 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b ith a matching k
b2550 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a ey, then a new.*
b2560 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 * element is cre
b2570 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 ated and NULL is
b2580 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
b2590 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d If another elem
b25a0 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 ent already exis
b25b0 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 ts with the same
b25c0 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a key, then the.*
b25d0 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 * new data repla
b25e0 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 ces the old data
b25f0 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 and the old dat
b2600 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a a is returned..*
b2610 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 * The key is not
b2620 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 copied in this
b2630 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 instance. If a
b2640 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 malloc fails, th
b2650 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 en.** the new da
b2660 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 ta is returned a
b2670 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c nd the hash tabl
b2680 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a e is unchanged..
b2690 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 **.** If the "da
b26a0 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f ta" parameter to
b26b0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
b26c0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 s NULL, then the
b26d0 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 .** element corr
b26e0 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 esponding to "ke
b26f0 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 y" is removed fr
b2700 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
b2710 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
b2720 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
b2730 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 e3HashInsert(Has
b2740 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 h *pH, const cha
b2750 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 r *pKey, int nKe
b2760 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a y, void *data){.
b2770 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
b2780 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 ; /* the h
b2790 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d ash of the key m
b27a0 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 odulo hash table
b27b0 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 size */. HashE
b27c0 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 lem *elem;
b27d0 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 /* Used to loop
b27e0 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e thru the elemen
b27f0 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 t list */. Hash
b2800 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 Elem *new_elem;
b2810 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 /* New element
b2820 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 added to the pH
b2830 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
b2840 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 H!=0 );. assert
b2850 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 ( pKey!=0 );. a
b2860 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 ssert( nKey>=0 )
b2870 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 ;. if( pH->htsi
b2880 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 ze ){. h = st
b2890 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 rHash(pKey, nKey
b28a0 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a ) % pH->htsize;.
b28b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d }else{. h =
b28c0 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 0;. }. elem =
b28d0 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 findElementGive
b28e0 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b nHash(pH,pKey,nK
b28f0 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 ey,h);. if( ele
b2900 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f m ){. void *o
b2910 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e ld_data = elem->
b2920 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 data;. if( da
b2930 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 ta==0 ){. r
b2940 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 emoveElementGive
b2950 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 nHash(pH,elem,h)
b2960 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
b2970 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 elem->data =
b2980 64 61 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d data;. elem
b2990 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 ->pKey = pKey;.
b29a0 20 20 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79 assert(nKey
b29b0 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 ==elem->nKey);.
b29c0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
b29d0 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 old_data;. }.
b29e0 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 if( data==0 ) re
b29f0 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c turn 0;. new_el
b2a00 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 em = (HashElem*)
b2a10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 sqlite3Malloc( s
b2a20 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 izeof(HashElem)
b2a30 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 );. if( new_ele
b2a40 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 m==0 ) return da
b2a50 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e ta;. new_elem->
b2a60 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e pKey = pKey;. n
b2a70 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 ew_elem->nKey =
b2a80 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d nKey;. new_elem
b2a90 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 ->data = data;.
b2aa0 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 pH->count++;.
b2ab0 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 if( pH->count>=1
b2ac0 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 0 && pH->count >
b2ad0 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 2*pH->htsize ){
b2ae0 0a 20 20 20 20 69 66 28 20 72 65 68 61 73 68 28 . if( rehash(
b2af0 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 pH, pH->count*2)
b2b00 20 26 26 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 && pH->htsize )
b2b10 7b 0a 20 20 20 20 20 20 68 20 3d 20 73 74 72 48 {. h = strH
b2b20 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 ash(pKey, nKey)
b2b30 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 % pH->htsize;.
b2b40 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 }. }. if( pH
b2b50 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 ->ht ){. inse
b2b60 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 rtElement(pH, &p
b2b70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c H->ht[h], new_el
b2b80 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
b2b90 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 insertElement(
b2ba0 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 pH, 0, new_elem)
b2bb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
b2bc0 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
b2bd0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 **** End of hash
b2be0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
b2c20 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
b2c30 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a opcodes.c ******
b2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c60 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 ***/./* Automati
b2c70 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e cally generated.
b2c80 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f Do not edit */
b2c90 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 ./* See the mkop
b2ca0 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74 codec.awk script
b2cb0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f for details. */
b2cc0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
b2cd0 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
b2ce0 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e N) || !defined(N
b2cf0 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 DEBUG) || define
b2d00 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 d(VDBE_PROFILE)
b2d10 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
b2d20 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f E_DEBUG).SQLITE_
b2d30 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
b2d40 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 ar *sqlite3Opcod
b2d50 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 eName(int i){. s
b2d60 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
b2d70 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d *const azName[]
b2d80 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f = { "?",. /
b2d90 2a 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 * 1 */ "VNext"
b2da0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f ,. /* 2 */
b2db0 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 "Affinity",.
b2dc0 20 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c /* 3 */ "Col
b2dd0 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 umn",. /*
b2de0 34 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 4 */ "SetCookie"
b2df0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f ,. /* 5 */
b2e00 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a "Seek",. /*
b2e10 20 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 6 */ "Sequenc
b2e20 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 e",. /* 7
b2e30 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a */ "Savepoint",.
b2e40 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 /* 8 */ "
b2e50 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a RowKey",. /*
b2e60 20 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 9 */ "SCopy",
b2e70 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 . /* 10 */
b2e80 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 "OpenWrite",.
b2e90 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 /* 11 */ "If"
b2ea0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f ,. /* 12 */
b2eb0 20 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 "CollSeq",.
b2ec0 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e /* 13 */ "Open
b2ed0 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Read",. /*
b2ee0 31 34 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 14 */ "Expire",.
b2ef0 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 /* 15 */ "
b2f00 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 AutoCommit",.
b2f10 20 20 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 /* 16 */ "Pag
b2f20 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a ecount",. /*
b2f30 20 20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 17 */ "Integri
b2f40 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 tyCk",. /*
b2f50 31 38 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 18 */ "Sort",.
b2f60 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f /* 19 */ "No
b2f70 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 t",. /* 20
b2f80 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 */ "Copy",.
b2f90 2f 2a 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65 /* 21 */ "Trace
b2fa0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a ",. /* 22 *
b2fb0 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 / "Function",.
b2fc0 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 /* 23 */ "If
b2fd0 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 Neg",. /* 2
b2fe0 34 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 4 */ "Noop",.
b2ff0 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 52 65 74 /* 25 */ "Ret
b3000 75 72 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 urn",. /* 2
b3010 36 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c 6 */ "NewRowid",
b3020 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 . /* 27 */
b3030 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20 "Variable",.
b3040 20 2f 2a 20 20 32 38 20 2a 2f 20 22 53 74 72 69 /* 28 */ "Stri
b3050 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 ng",. /* 29
b3060 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 74 */ "RealAffinit
b3070 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 30 20 y",. /* 30
b3080 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 */ "VRename",.
b3090 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 50 61 /* 31 */ "Pa
b30a0 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 20 rseSchema",.
b30b0 20 2f 2a 20 20 33 32 20 2a 2f 20 22 56 4f 70 65 /* 32 */ "VOpe
b30c0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 n",. /* 33
b30d0 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 */ "Close",.
b30e0 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 72 65 61 /* 34 */ "Crea
b30f0 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f teIndex",. /
b3100 2a 20 20 33 35 20 2a 2f 20 22 49 73 55 6e 69 71 * 35 */ "IsUniq
b3110 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 ue",. /* 36
b3120 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a */ "NotFound",.
b3130 20 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 /* 37 */ "
b3140 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f 2a 20 Int64",. /*
b3150 20 33 38 20 2a 2f 20 22 4d 75 73 74 42 65 49 6e 38 */ "MustBeIn
b3160 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20 t",. /* 39
b3170 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 */ "Halt",.
b3180 2f 2a 20 20 34 30 20 2a 2f 20 22 52 6f 77 69 64 /* 40 */ "Rowid
b3190 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a ",. /* 41 *
b31a0 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20 / "IdxLT",.
b31b0 2f 2a 20 20 34 32 20 2a 2f 20 22 41 64 64 49 6d /* 42 */ "AddIm
b31c0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20 m",. /* 43
b31d0 2a 2f 20 22 53 74 61 74 65 6d 65 6e 74 22 2c 0a */ "Statement",.
b31e0 20 20 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 /* 44 */ "
b31f0 52 6f 77 44 61 74 61 22 2c 0a 20 20 20 20 20 2f RowData",. /
b3200 2a 20 20 34 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 * 45 */ "MemMax
b3210 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a ",. /* 46 *
b3220 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20 / "NotExists",.
b3230 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 47 /* 47 */ "G
b3240 6f 73 75 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 osub",. /*
b3250 34 38 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c 48 */ "Integer",
b3260 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 . /* 49 */
b3270 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 "Prev",. /*
b3280 20 35 30 20 2a 2f 20 22 52 6f 77 53 65 74 52 65 50 */ "RowSetRe
b3290 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31 ad",. /* 51
b32a0 20 2a 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c */ "RowSetAdd",
b32b0 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 . /* 52 */
b32c0 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 "VColumn",.
b32d0 2f 2a 20 20 35 33 20 2a 2f 20 22 43 72 65 61 74 /* 53 */ "Creat
b32e0 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a eTable",. /*
b32f0 20 20 35 34 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 54 */ "Last",.
b3300 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22 /* 55 */ "
b3310 53 65 65 6b 4c 65 22 2c 0a 20 20 20 20 20 2f 2a SeekLe",. /*
b3320 20 20 35 36 20 2a 2f 20 22 49 6e 63 72 56 61 63 56 */ "IncrVac
b3330 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 uum",. /* 5
b3340 37 20 2a 2f 20 22 49 64 78 52 6f 77 69 64 22 2c 7 */ "IdxRowid",
b3350 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 . /* 58 */
b3360 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20 "ResetCount",.
b3370 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 43 6f /* 59 */ "Co
b3380 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20 20 20 ntextPush",.
b3390 20 2f 2a 20 20 36 30 20 2a 2f 20 22 59 69 65 6c /* 60 */ "Yiel
b33a0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 31 20 d",. /* 61
b33b0 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 22 */ "DropTrigger"
b33c0 2c 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f ,. /* 62 */
b33d0 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 "DropIndex",.
b33e0 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 49 64 /* 63 */ "Id
b33f0 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 xGE",. /* 6
b3400 34 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 4 */ "IdxDelete"
b3410 2c 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f ,. /* 65 */
b3420 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 "Vacuum",.
b3430 2f 2a 20 20 36 36 20 2a 2f 20 22 4f 72 22 2c 0a /* 66 */ "Or",.
b3440 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 /* 67 */ "
b3450 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 And",. /* 6
b3460 38 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 8 */ "IfNot",.
b3470 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 44 72 /* 69 */ "Dr
b3480 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f opTable",. /
b3490 2a 20 20 37 30 20 2a 2f 20 22 53 65 65 6b 4c 74 * 70 */ "SeekLt
b34a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a ",. /* 71 *
b34b0 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 / "IsNull",.
b34c0 20 2f 2a 20 20 37 32 20 2a 2f 20 22 4e 6f 74 4e /* 72 */ "NotN
b34d0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 ull",. /* 7
b34e0 33 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20 3 */ "Ne",.
b34f0 2f 2a 20 20 37 34 20 2a 2f 20 22 45 71 22 2c 0a /* 74 */ "Eq",.
b3500 20 20 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20 22 /* 75 */ "
b3510 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 36 Gt",. /* 76
b3520 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f */ "Le",. /
b3530 2a 20 20 37 37 20 2a 2f 20 22 4c 74 22 2c 0a 20 * 77 */ "Lt",.
b3540 20 20 20 20 2f 2a 20 20 37 38 20 2a 2f 20 22 47 /* 78 */ "G
b3550 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 e",. /* 79
b3560 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c */ "MakeRecord",
b3570 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 . /* 80 */
b3580 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f "BitAnd",. /
b3590 2a 20 20 38 31 20 2a 2f 20 22 42 69 74 4f 72 22 * 81 */ "BitOr"
b35a0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f ,. /* 82 */
b35b0 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 "ShiftLeft",.
b35c0 20 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 53 68 /* 83 */ "Sh
b35d0 69 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20 iftRight",.
b35e0 2f 2a 20 20 38 34 20 2a 2f 20 22 41 64 64 22 2c /* 84 */ "Add",
b35f0 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20 . /* 85 */
b3600 22 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20 "Subtract",.
b3610 20 2f 2a 20 20 38 36 20 2a 2f 20 22 4d 75 6c 74 /* 86 */ "Mult
b3620 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 iply",. /*
b3630 38 37 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 87 */ "Divide",.
b3640 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 /* 88 */ "
b3650 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 Remainder",.
b3660 20 2f 2a 20 20 38 39 20 2a 2f 20 22 43 6f 6e 63 /* 89 */ "Conc
b3670 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 at",. /* 90
b3680 20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c */ "ResultRow",
b3690 0a 20 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 . /* 91 */
b36a0 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f "Delete",. /
b36b0 2a 20 20 39 32 20 2a 2f 20 22 41 67 67 46 69 6e * 92 */ "AggFin
b36c0 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 al",. /* 93
b36d0 20 2a 2f 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20 */ "BitNot",.
b36e0 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20 22 53 74 /* 94 */ "St
b36f0 72 69 6e 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20 ring8",. /*
b3700 20 39 35 20 2a 2f 20 22 43 6f 6d 70 61 72 65 22 95 */ "Compare"
b3710 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f ,. /* 96 */
b3720 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f 2a "Goto",. /*
b3730 20 20 39 37 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 97 */ "TableLo
b3740 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 ck",. /* 98
b3750 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20 */ "Clear",.
b3760 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 56 65 72 /* 99 */ "Ver
b3770 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 ifyCookie",.
b3780 20 2f 2a 20 31 30 30 20 2a 2f 20 22 41 67 67 53 /* 100 */ "AggS
b3790 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 tep",. /* 10
b37a0 31 20 2a 2f 20 22 53 65 74 4e 75 6d 43 6f 6c 75 1 */ "SetNumColu
b37b0 6d 6e 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 mns",. /* 10
b37c0 32 20 2a 2f 20 22 54 72 61 6e 73 61 63 74 69 6f 2 */ "Transactio
b37d0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 n",. /* 103
b37e0 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a 20 20 */ "VFilter",.
b37f0 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 56 44 /* 104 */ "VD
b3800 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a estroy",. /*
b3810 20 31 30 35 20 2a 2f 20 22 43 6f 6e 74 65 78 74 105 */ "Context
b3820 50 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 Pop",. /* 10
b3830 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20 6 */ "Next",.
b3840 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22 43 6f 75 /* 107 */ "Cou
b3850 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 nt",. /* 108
b3860 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22 2c */ "IdxInsert",
b3870 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f 20 . /* 109 */
b3880 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f "SeekGe",. /
b3890 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65 72 74 * 110 */ "Insert
b38a0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a ",. /* 111 *
b38b0 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 / "Destroy",.
b38c0 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 65 61 /* 112 */ "Rea
b38d0 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f dCookie",. /
b38e0 2a 20 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 74 * 113 */ "RowSet
b38f0 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Test",. /* 1
b3900 31 34 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 79 14 */ "LoadAnaly
b3910 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 sis",. /* 11
b3920 35 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 2c 0a 5 */ "Explain",.
b3930 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 /* 116 */ "
b3940 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20 20 20 HaltIfNull",.
b3950 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 4f 70 65 /* 117 */ "Ope
b3960 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f nPseudo",. /
b3970 2a 20 31 31 38 20 2a 2f 20 22 4f 70 65 6e 45 70 * 118 */ "OpenEp
b3980 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f hemeral",. /
b3990 2a 20 31 31 39 20 2a 2f 20 22 4e 75 6c 6c 22 2c * 119 */ "Null",
b39a0 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 . /* 120 */
b39b0 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20 "Move",. /*
b39c0 31 32 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20 121 */ "Blob",.
b39d0 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 52 /* 122 */ "R
b39e0 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 ewind",. /*
b39f0 31 32 33 20 2a 2f 20 22 53 65 65 6b 47 74 22 2c 123 */ "SeekGt",
b3a00 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20 . /* 124 */
b3a10 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f "VBegin",. /
b3a20 2a 20 31 32 35 20 2a 2f 20 22 56 55 70 64 61 74 * 125 */ "VUpdat
b3a30 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 e",. /* 126
b3a40 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 */ "IfZero",.
b3a50 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 43 72 /* 127 */ "VCr
b3a60 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 eate",. /* 1
b3a70 32 38 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 28 */ "Found",.
b3a80 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 49 /* 129 */ "I
b3a90 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 fPos",. /* 1
b3aa0 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20 30 */ "Real",.
b3ab0 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 4e 75 /* 131 */ "Nu
b3ac0 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 llRow",. /*
b3ad0 31 33 32 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 132 */ "Jump",.
b3ae0 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 50 /* 133 */ "P
b3af0 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 ermutation",.
b3b00 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f 74 /* 134 */ "Not
b3b10 55 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20 20 Used_134",.
b3b20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55 73 /* 135 */ "NotUs
b3b30 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f 2a ed_135",. /*
b3b40 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65 64 136 */ "NotUsed
b3b50 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _136",. /* 1
b3b60 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 37 */ "NotUsed_1
b3b70 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 38 37",. /* 138
b3b80 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 38 */ "NotUsed_138
b3b90 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a ",. /* 139 *
b3ba0 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39 22 2c / "NotUsed_139",
b3bb0 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f 20 . /* 140 */
b3bc0 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c 0a 20 "NotUsed_140",.
b3bd0 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20 22 54 /* 141 */ "T
b3be0 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 oText",. /*
b3bf0 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c 142 */ "ToBlob",
b3c00 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a 2f 20 . /* 143 */
b3c10 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20 20 20 "ToNumeric",.
b3c20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54 6f 49 /* 144 */ "ToI
b3c30 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 35 nt",. /* 145
b3c40 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 20 20 */ "ToReal",.
b3c50 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 4e 61 };. return azNa
b3c60 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a me[i];.}.#endif.
b3c70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b3c80 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e End of opcodes.
b3c90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b3cc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b3cd0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 6f Begin file os_o
b3ce0 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s2.c ***********
b3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b3d10 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65 62 20 ./*.** 2006 Feb
b3d20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
b3d30 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
b3d40 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
b3d50 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
b3d60 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
b3d70 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
b3d80 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
b3d90 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
b3da0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
b3db0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
b3dc0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
b3dd0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
b3de0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
b3df0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
b3e00 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
b3e10 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
b3e20 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
b3e30 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
b3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e80 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
b3e90 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
b3ea0 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 code that is spe
b3eb0 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a cific to OS/2..*
b3ec0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 6f 73 32 *.** $Id: os_os2
b3ed0 2e 63 2c 76 20 31 2e 36 33 20 32 30 30 38 2f 31 .c,v 1.63 2008/1
b3ee0 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72 2/10 19:26:24 dr
b3ef0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 h Exp $.*/...#if
b3f00 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 0a SQLITE_OS_OS2..
b3f10 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f /*.** A Note Abo
b3f20 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 ut Memory Alloca
b3f30 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion:.**.** This
b3f40 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c driver uses mal
b3f50 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 loc()/free() dir
b3f60 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 ectly rather tha
b3f70 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a n going through.
b3f80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 ** the SQLite-wr
b3f90 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d appers sqlite3_m
b3fa0 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f alloc()/sqlite3_
b3fb0 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 free(). Those w
b3fc0 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 rappers.** are d
b3fd0 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 esigned for use
b3fe0 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 on embedded syst
b3ff0 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 ems where memory
b4000 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a is scarce and.*
b4010 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
b4020 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e s happen frequen
b4030 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65 73 20 tly. OS/2 does
b4040 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 not typically ru
b4050 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 n on.** embedded
b4060 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 systems, and wh
b4070 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 en it does the d
b4080 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c evelopers normal
b4090 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a ly have bigger.*
b40a0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f * problems to wo
b40b0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 rry about than r
b40c0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 unning out of me
b40d0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 mory. So there
b40e0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 is not.** a comp
b40f0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 elling need to u
b4100 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
b4110 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 .**.** But there
b4120 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f is a good reaso
b4130 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 n to not use the
b4140 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 wrappers. If w
b4150 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 e use the.** wra
b4160 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 ppers then we wi
b4170 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 ll get simulated
b4180 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
b4190 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a es within this.*
b41a0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 * driver. And t
b41b0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b hat causes all k
b41c0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 inds of problems
b41d0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 for our tests.
b41e0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 We.** could enh
b41f0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 ance SQLite to d
b4200 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 eal with simulat
b4210 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ed malloc failur
b4220 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 es within.** the
b4230 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 OS driver, but
b4240 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c the code to deal
b4250 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c with those fail
b4260 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a ure would not.**
b4270 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e be exercised on
b4280 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f Linux (which do
b4290 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
b42a0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 alloc() in the d
b42b0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f river).** and so
b42c0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 we would have d
b42d0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e ifficulty writin
b42e0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 g coverage tests
b42f0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 for that.** cod
b4300 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 e. Better to le
b4310 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 ave the code out
b4320 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a , we think..**.*
b4330 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 * The point of t
b4340 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 his discussion i
b4350 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 s as follows: W
b4360 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
b4370 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 ew.** OS layer f
b4380 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 or an embedded s
b4390 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 ystem, if you us
b43a0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 e this file as a
b43b0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 n example,.** av
b43c0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d oid the use of m
b43d0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 alloc()/free().
b43e0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 Those routines
b43f0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f 32 0a work ok on OS/2.
b4400 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74 20 ** desktops but
b4410 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 65 not so well in e
b4420 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2e mbedded systems.
b4430 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f .*/../*.** Macro
b4440 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
b4450 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
b4460 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64 ot to use thread
b4470 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
b4480 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 d(SQLITE_THREADS
b4490 41 46 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 AFE) && SQLITE_T
b44a0 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 HREADSAFE.# defi
b44b0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f 54 48 ne SQLITE_OS2_TH
b44c0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
b44d0 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f /*.** Include co
b44e0 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f de that is commo
b44f0 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 n to all os_*.c
b4500 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a files.*/./******
b4510 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
b4520 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 os_common.h in
b4530 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 the middle of os
b4540 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _os2.c *********
b4550 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
b4560 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
b4570 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 ile os_common.h
b4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b45a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
b45b0 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 004 May 22.**.**
b45c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
b45d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
b45e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
b45f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
b4600 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
b4610 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
b4620 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
b4630 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
b4640 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
b4650 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
b4660 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
b4670 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
b4680 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
b4690 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
b46a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
b46b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
b46c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
b46d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b46e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b46f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
b4720 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
b4730 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 ontains macros a
b4740 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 nd a little bit
b4750 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 of code that is
b4760 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c common to.** all
b4770 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d of the platform
b4780 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 -specific files
b4790 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 (os_*.c) and is
b47a0 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 #included into t
b47b0 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a hose.** files..*
b47c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 *.** This file s
b47d0 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 hould be #includ
b47e0 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 ed by the os_*.c
b47f0 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 files only. It
b4800 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e is not a.** gen
b4810 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 eral purpose hea
b4820 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 der file..**.**
b4830 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 $Id: os_common.h
b4840 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f ,v 1.38 2009/02/
b4850 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 24 18:40:50 dani
b4860 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
b4870 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d .#ifndef _OS_COM
b4880 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f MON_H_.#define _
b4890 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a OS_COMMON_H_../*
b48a0 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f .** At least two
b48b0 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 bugs have slipp
b48c0 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 ed in because we
b48d0 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d changed the MEM
b48e0 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 ORY_DEBUG.** mac
b48f0 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 ro to SQLITE_DEB
b4900 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 UG and some olde
b4910 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 r makefiles have
b4920 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 not yet made th
b4930 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 e.** switch. Th
b4940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 e following code
b4950 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 should catch th
b4960 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f is problem at co
b4970 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 mpile-time..*/.#
b4980 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 ifdef MEMORY_DEB
b4990 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 UG.# error "The
b49a0 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 MEMORY_DEBUG mac
b49b0 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 ro is obsolete.
b49c0 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 Use SQLITE_DEBU
b49d0 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 G instead.".#end
b49e0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
b49f0 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
b4a00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b4a10 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 e3OSTrace = 0;.#
b4a20 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 define OSTRACE1(
b4a30 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73 X) if( s
b4a40 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
b4a50 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
b4a60 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 tf(X).#define OS
b4a70 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 TRACE2(X,Y)
b4a80 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
b4a90 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
b4aa0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 bugPrintf(X,Y).#
b4ab0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 define OSTRACE3(
b4ac0 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 X,Y,Z) if( s
b4ad0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
b4ae0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
b4af0 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e tf(X,Y,Z).#defin
b4b00 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a e OSTRACE4(X,Y,Z
b4b10 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 ,A) if( sqlite
b4b20 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
b4b30 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
b4b40 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f Y,Z,A).#define O
b4b50 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c STRACE5(X,Y,Z,A,
b4b60 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 B) if( sqlite3OS
b4b70 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
b4b80 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
b4b90 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,A,B).#define OS
b4ba0 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE6(X,Y,Z,A,B
b4bb0 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c ,C) \. if(sql
b4bc0 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c ite3OSTrace) sql
b4bd0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
b4be0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
b4bf0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
b4c00 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 Y,Z,A,B,C,D) \.
b4c10 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
b4c20 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
b4c30 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
b4c40 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 ,B,C,D).#else.#d
b4c50 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 efine OSTRACE1(X
b4c60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
b4c70 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 E2(X,Y).#define
b4c80 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a OSTRACE3(X,Y,Z).
b4c90 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 #define OSTRACE4
b4ca0 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
b4cb0 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
b4cc0 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,A,B).#define OS
b4cd0 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE6(X,Y,Z,A,B
b4ce0 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,C).#define OSTR
b4cf0 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE7(X,Y,Z,A,B,C
b4d00 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,D).#endif../*.*
b4d10 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 * Macros for per
b4d20 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 formance tracing
b4d30 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e . Normally turn
b4d40 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f ed off. Only wo
b4d50 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 rks.** on i486 h
b4d60 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 ardware..*/.#ifd
b4d70 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 ef SQLITE_PERFOR
b4d80 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 MANCE_TRACE../*
b4d90 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e .** hwtime.h con
b4da0 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 tains inline ass
b4db0 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 embler code for
b4dc0 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a implementing .**
b4dd0 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 high-performanc
b4de0 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 e timing routine
b4df0 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a s..*/./*********
b4e00 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 ***** Include hw
b4e10 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 time.h in the mi
b4e20 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f ddle of os_commo
b4e30 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
b4e40 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
b4e50 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
b4e60 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e90 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
b4ea0 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 May 27.**.** Th
b4eb0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
b4ec0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
b4ed0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
b4ee0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
b4ef0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
b4f00 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
b4f10 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
b4f20 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
b4f30 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
b4f40 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
b4f50 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
b4f60 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
b4f70 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
b4f80 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
b4f90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
b4fa0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
b4fb0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
b4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
b5010 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
b5020 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 ains inline asm
b5030 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 code for retriev
b5040 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 ing "high-perfor
b5050 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 mance".** counte
b5060 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 rs for x86 class
b5070 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 CPUs..**.** $Id
b5080 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 : hwtime.h,v 1.3
b5090 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 2008/08/01 14:3
b50a0 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 3:15 shane Exp $
b50b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 .*/.#ifndef _HWT
b50c0 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f IME_H_.#define _
b50d0 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a HWTIME_H_../*.**
b50e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
b50f0 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
b5100 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 s on pentium-cla
b5110 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 ss (or newer) pr
b5120 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 ocessors..** It
b5130 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f uses the RDTSC o
b5140 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 pcode to read th
b5150 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 e cycle count va
b5160 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a lue out of the.*
b5170 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 * processor and
b5180 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c returns that val
b5190 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ue. This can be
b51a0 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 used for high-r
b51b0 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e es.** profiling.
b51c0 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 .*/.#if (defined
b51d0 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 (__GNUC__) || de
b51e0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 fined(_MSC_VER))
b51f0 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 && \. (def
b5200 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 ined(i386) || de
b5210 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 fined(__i386__)
b5220 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 || defined(_M_IX
b5230 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 86)).. #if defi
b5240 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 ned(__GNUC__)..
b5250 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
b5260 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
b5270 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
b5280 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
b5290 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f lo, hi;. __
b52a0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
b52b0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
b52c0 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 a" (lo), "=d" (h
b52d0 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e i));. return
b52e0 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 (sqlite_uint64)
b52f0 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 hi << 32 | lo;.
b5300 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 }.. #elif defi
b5310 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 ned(_MSC_VER)..
b5320 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 __declspec(nake
b5330 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 d) __inline sqli
b5340 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 te_uint64 __cdec
b5350 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 l sqlite3Hwtime(
b5360 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 void){. __as
b5370 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 m {. rdts
b5380 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 c. ret
b5390 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c ; return val
b53a0 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 ue at EDX:EAX.
b53b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 }. }.. #end
b53c0 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e if..#elif (defin
b53d0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
b53e0 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
b53f0 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
b5400 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
b5410 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
b5420 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
b5430 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 gned long val;.
b5440 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
b5450 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
b5460 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 c" : "=A" (val))
b5470 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 ;. return v
b5480 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 al;. }. .#elif
b5490 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
b54a0 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
b54b0 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c ppc__)).. __inl
b54c0 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
b54d0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
b54e0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
b54f0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
b5500 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 g retval;.
b5510 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 unsigned long ju
b5520 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f nk;. __asm_
b5530 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
b5540 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 "\n\. 1
b5550 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 : mftbu %
b5560 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
b5570 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 mftb %
b5580 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 L0\n\.
b5590 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 mftbu
b55a0 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %0\n\.
b55b0 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 cmpw
b55c0 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %0,%1\n\.
b55d0 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 bne
b55e0 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 1b".
b55f0 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 : "=r"
b5600 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 (retval), "=r" (
b5610 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 junk));. re
b5620 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d turn retval;. }
b5630 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f ..#else.. #erro
b5640 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 r Need implement
b5650 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
b5660 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
b5670 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f r platform... /
b5680 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c *. ** To compil
b5690 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d e without implem
b56a0 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 enting sqlite3Hw
b56b0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
b56c0 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 platform,. ** y
b56d0 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 ou can remove th
b56e0 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 e above #error a
b56f0 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f nd use the follo
b5700 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 wing. ** stub f
b5710 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 unction. You wi
b5720 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 ll lose timing s
b5730 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a upport for many.
b5740 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 ** of the debu
b5750 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e gging and testin
b5760 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 g utilities, but
b5770 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 it should at.
b5780 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 ** least compile
b5790 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 and run.. */.S
b57a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
b57b0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
b57c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
b57d0 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 ){ return ((sqli
b57e0 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a te_uint64)0); }.
b57f0 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
b5800 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 /* !defined(_HWT
b5810 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a IME_H_) */../***
b5820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
b5830 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a of hwtime.h ****
b5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
b5870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
b5880 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
b5890 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 left off in os_c
b58a0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
b58b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 **********/..sta
b58c0 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 tic sqlite_uint6
b58d0 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 4 g_start;.stati
b58e0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
b58f0 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 g_elapsed;.#defi
b5900 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 ne TIMER_START
b5910 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c g_start=sql
b5920 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 ite3Hwtime().#de
b5930 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 fine TIMER_END
b5940 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 g_elapsed
b5950 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 =sqlite3Hwtime()
b5960 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 -g_start.#define
b5970 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
b5980 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c g_elapsed.#el
b5990 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 se.#define TIMER
b59a0 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 _START.#define T
b59b0 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 IMER_END.#define
b59c0 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
b59d0 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 ((sqlite_uint
b59e0 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 64)0).#endif../*
b59f0 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c .** If we compil
b5a00 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 e with the SQLIT
b5a10 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 E_TEST macro set
b5a20 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
b5a30 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 wing block.** of
b5a40 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 code will give
b5a50 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 us the ability t
b5a60 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 o simulate a dis
b5a70 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 k I/O error. Th
b5a80 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f is.** is used fo
b5a90 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f r testing the I/
b5aa0 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 O recovery logic
b5ab0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
b5ac0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
b5ad0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
b5ae0 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b o_error_hit = 0;
b5af0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
b5b00 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 otal number of I
b5b10 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c /O Errors */.SQL
b5b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b5b30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
b5b40 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 dhit = 0;
b5b50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f /* Number of no
b5b60 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 n-benign errors
b5b70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
b5b80 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
b5b90 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 or_pending = 0;
b5ba0 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 /* Count
b5bb0 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f down to first I/
b5bc0 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 O error */.SQLIT
b5bd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
b5be0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 3_io_error_persi
b5bf0 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f st = 0; /
b5c00 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 * True if I/O er
b5c10 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a rors persist */.
b5c20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
b5c30 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
b5c40 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 benign = 0;
b5c50 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 /* True if e
b5c60 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e rrors are benign
b5c70 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
b5c80 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
b5c90 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ull_pending = 0;
b5ca0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
b5cb0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
b5cc0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 = 0;.#define Si
b5cd0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
b5ce0 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 ign(X) sqlite3_i
b5cf0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 o_error_benign=(
b5d00 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
b5d10 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 ateIOError(CODE)
b5d20 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 \. if( (sqlit
b5d30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
b5d40 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 ist && sqlite3_i
b5d50 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 o_error_hit) \.
b5d60 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
b5d70 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
b5d80 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 g-- == 1 ) \.
b5d90 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f { lo
b5da0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 cal_ioerr(); COD
b5db0 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 E; }.static void
b5dc0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a local_ioerr(){.
b5dd0 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 IOTRACE(("IOER
b5de0 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 R\n"));. sqlite
b5df0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3_io_error_hit++
b5e00 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
b5e10 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
b5e20 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ) sqlite3_io_er
b5e30 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d ror_hardhit++;.}
b5e40 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
b5e50 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 eDiskfullError(C
b5e60 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 ODE) \. if( sq
b5e70 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
b5e80 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 ending ){ \.
b5e90 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 if( sqlite3_dis
b5ea0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d kfull_pending ==
b5eb0 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 1 ){ \. l
b5ec0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a ocal_ioerr(); \.
b5ed0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
b5ee0 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 iskfull = 1; \.
b5ef0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f sqlite3_io
b5f00 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 _error_hit = 1;
b5f10 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c \. CODE; \
b5f20 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 . }else{ \.
b5f30 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 sqlite3_di
b5f40 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d skfull_pending--
b5f50 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 ; \. } \.
b5f60 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
b5f70 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
b5f80 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 enign(X).#define
b5f90 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
b5fa0 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (A).#define Simu
b5fb0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
b5fc0 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a r(A).#endif../*.
b5fd0 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c ** When testing,
b5fe0 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 keep a count of
b5ff0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f the number of o
b6000 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 pen files..*/.#i
b6010 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
b6020 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
b6030 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c sqlite3_open_fil
b6040 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 e_count = 0;.#de
b6050 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
b6060 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 (X) sqlite3_ope
b6070 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 n_file_count+=(X
b6080 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
b6090 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 OpenCounter(X).#
b60a0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
b60b0 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f !defined(_OS_CO
b60c0 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a MMON_H_) */../**
b60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
b60e0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
b60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
b6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
b6130 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
b6140 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
b6150 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
b6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
b6170 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 6c 65 20 .** The os2File
b6180 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62 structure is sub
b6190 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 class of sqlite3
b61a0 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 66 _file specific f
b61b0 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20 70 or the OS/2.** p
b61c0 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 rotability layer
b61d0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
b61e0 75 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32 46 uct os2File os2F
b61f0 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32 46 ile;.struct os2F
b6200 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ile {. const sq
b6210 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
b6220 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 *pMethod; /* A
b6230 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 lways the first
b6240 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 49 4c 45 entry */. HFILE
b6250 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
b6260 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 /* Handle f
b6270 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 or accessing the
b6280 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 2a file */. char*
b6290 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 20 20 20 pathToDel;
b62a0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
b62b0 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 6f file to delete o
b62c0 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c 20 69 66 n close, NULL if
b62d0 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e not */. unsign
b62e0 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 ed char locktype
b62f0 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c ; /* Type of l
b6300 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 ock currently he
b6310 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 ld on this file
b6320 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 4c */.};..#define L
b6330 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 30 4c 20 OCK_TIMEOUT 10L
b6340 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c /* the default l
b6350 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 74 20 2a ocking timeout *
b6360 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
b6370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b63b0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 *.** The next gr
b63c0 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 oup of routines
b63d0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f implement the I/
b63e0 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 66 O methods specif
b63f0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71 ied.** by the sq
b6400 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
b6410 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a object..*******
b6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6460 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
b6470 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f Close a file..*/
b6480 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 .static int os2C
b6490 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 5f 66 69 lose( sqlite3_fi
b64a0 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 50 49 52 le *id ){. APIR
b64b0 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
b64c0 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
b64d0 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 26 26 20 le;. if( id &&
b64e0 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c (pFile = (os2Fil
b64f0 65 2a 29 69 64 29 20 21 3d 20 30 20 29 7b 0a 20 e*)id) != 0 ){.
b6500 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 43 4c OSTRACE2( "CL
b6510 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 OSE %d\n", pFile
b6520 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ->h );. rc =
b6530 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 6c 65 2d DosClose( pFile-
b6540 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d >h );. pFile-
b6550 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c >locktype = NO_L
b6560 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 70 46 69 OCK;. if( pFi
b6570 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 21 3d le->pathToDel !=
b6580 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 NULL ){. r
b6590 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 65 6c 65 c = DosForceDele
b65a0 74 65 28 20 28 50 53 5a 29 70 46 69 6c 65 2d 3e te( (PSZ)pFile->
b65b0 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 pathToDel );.
b65c0 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e free( pFile->
b65d0 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 pathToDel );.
b65e0 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f pFile->pathTo
b65f0 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 Del = NULL;.
b6600 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b 0a 20 20 }. id = 0;.
b6610 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20 2d OpenCounter( -
b6620 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 1 );. }.. retu
b6630 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f rn rc == NO_ERRO
b6640 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 R ? SQLITE_OK :
b6650 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a SQLITE_IOERR;.}.
b6660 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
b6670 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 from a file int
b6680 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 o a buffer. Ret
b6690 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
b66a0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 all.** bytes we
b66b0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 re read successf
b66c0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f ully and SQLITE_
b66d0 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e IOERR if anythin
b66e0 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
b66f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
b6700 73 32 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 s2Read(. sqlite
b6710 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
b6720 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
b6730 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a e to read from *
b6740 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 /. void *pBuf,
b6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6760 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e /* Write con
b6770 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 tent into this b
b6780 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 uffer */. int a
b6790 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
b67a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
b67b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f mber of bytes to
b67c0 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 read */. sqlit
b67d0 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 e3_int64 offset
b67e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 /* Be
b67f0 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 gin reading at t
b6800 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b his offset */.){
b6810 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 . ULONG fileLoc
b6820 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 55 4c ation = 0L;. UL
b6830 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 32 46 69 ONG got;. os2Fi
b6840 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
b6850 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 File*)id;. asse
b6860 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 rt( id!=0 );. S
b6870 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
b6880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
b6890 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 20 4f 53 ERR_READ );. OS
b68a0 54 52 41 43 45 33 28 20 22 52 45 41 44 20 25 64 TRACE3( "READ %d
b68b0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
b68c0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
b68d0 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 cktype );. if(
b68e0 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 DosSetFilePtr(pF
b68f0 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 ile->h, offset,
b6900 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c FILE_BEGIN, &fil
b6910 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f eLocation) != NO
b6920 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 _ERROR ){. re
b6930 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
b6940 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 44 6f 73 R;. }. if( Dos
b6950 52 65 61 64 28 20 70 46 69 6c 65 2d 3e 68 2c 20 Read( pFile->h,
b6960 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 20 pBuf, amt, &got
b6970 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b ) != NO_ERROR ){
b6980 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
b6990 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 TE_IOERR_READ;.
b69a0 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 3d 3d 20 }. if( got ==
b69b0 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a 20 20 20 (ULONG)amt ).
b69c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
b69d0 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 K;. else {.
b69e0 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 74 69 6f /* Unread portio
b69f0 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 ns of the input
b6a00 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a buffer must be z
b6a10 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 ero-filled */.
b6a20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 memset(&((char
b6a30 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c *)pBuf)[got], 0,
b6a40 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 amt-got);. r
b6a50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
b6a60 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 RR_SHORT_READ;.
b6a70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 }.}../*.** Writ
b6a80 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 e data from a bu
b6a90 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 ffer into a file
b6aa0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
b6ab0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a _OK on success.*
b6ac0 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 * or some other
b6ad0 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 error code on fa
b6ae0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ilure..*/.static
b6af0 20 69 6e 74 20 6f 73 32 57 72 69 74 65 28 0a 20 int os2Write(.
b6b00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
b6b10 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
b6b20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 74 /* File to writ
b6b30 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 e into */. cons
b6b40 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 t void *pBuf,
b6b50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
b6b60 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77 he bytes to be w
b6b70 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 ritten */. int
b6b80 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
b6b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
b6ba0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
b6bb0 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c o write */. sql
b6bc0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
b6bd0 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t /*
b6be0 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 Offset into the
b6bf0 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72 file to begin wr
b6c00 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 iting at */.){.
b6c10 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74 ULONG fileLocat
b6c20 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 50 49 52 ion = 0L;. APIR
b6c30 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
b6c40 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f 74 65 3b ;. ULONG wrote;
b6c50 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
b6c60 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
b6c70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d ;. assert( id!=
b6c80 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
b6c90 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
b6ca0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 QLITE_IOERR_WRIT
b6cb0 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 E );. SimulateD
b6cc0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 iskfullError( re
b6cd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
b6ce0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 );. OSTRACE3(
b6cf0 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 "WRITE %d lock=%
b6d00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b6d10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
b6d20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46 );. if( DosSetF
b6d30 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c ilePtr(pFile->h,
b6d40 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 offset, FILE_BE
b6d50 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69 GIN, &fileLocati
b6d60 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 on) != NO_ERROR
b6d70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
b6d80 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a LITE_IOERR;. }.
b6d90 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 assert( amt>0
b6da0 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 20 );. while( amt
b6db0 3e 20 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 > 0 &&.
b6dc0 28 20 72 63 20 3d 20 44 6f 73 57 72 69 74 65 28 ( rc = DosWrite(
b6dd0 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 56 4f 49 pFile->h, (PVOI
b6de0 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 D)pBuf, amt, &wr
b6df0 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f 5f 45 52 ote ) ) == NO_ER
b6e00 52 4f 52 20 26 26 0a 20 20 20 20 20 20 20 20 20 ROR &&.
b6e10 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 7b 0a 20 wrote > 0. ){.
b6e20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b amt -= wrote;
b6e30 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 . pBuf = &((c
b6e40 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 har*)pBuf)[wrote
b6e50 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e ];. }.. return
b6e60 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f ( rc != NO_ERRO
b6e70 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74 29 R || amt > (int)
b6e80 77 72 6f 74 65 20 29 20 3f 20 53 51 4c 49 54 45 wrote ) ? SQLITE
b6e90 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f 4f _FULL : SQLITE_O
b6ea0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e K;.}../*.** Trun
b6eb0 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c cate an open fil
b6ec0 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 e to a specified
b6ed0 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 size.*/.static
b6ee0 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 74 65 28 int os2Truncate(
b6ef0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
b6f00 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29 7b 0a d, i64 nByte ){.
b6f10 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
b6f20 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c _ERROR;. os2Fil
b6f30 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
b6f40 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 ile*)id;. OSTRA
b6f50 43 45 33 28 20 22 54 52 55 4e 43 41 54 45 20 25 CE3( "TRUNCATE %
b6f60 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 d %lld\n", pFile
b6f70 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 ->h, nByte );.
b6f80 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
b6f90 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
b6fa0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b OERR_TRUNCATE );
b6fb0 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 . rc = DosSetFi
b6fc0 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 2d 3e 68 leSize( pFile->h
b6fd0 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 72 65 74 , nByte );. ret
b6fe0 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 urn rc == NO_ERR
b6ff0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a OR ? SQLITE_OK :
b7000 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 SQLITE_IOERR_TR
b7010 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64 65 UNCATE;.}..#ifde
b7020 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
b7030 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 .** Count the nu
b7040 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 mber of fullsync
b7050 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e s and normal syn
b7060 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 cs. This is use
b7070 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 d to test.** tha
b7080 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c t syncs and full
b7090 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69 syncs are occuri
b70a0 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20 ng at the right
b70b0 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 times..*/.SQLITE
b70c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
b70d0 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b _sync_count = 0;
b70e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
b70f0 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 sqlite3_fullsync
b7100 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 _count = 0;.#end
b7110 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 if../*.** Make s
b7120 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 ure all writes t
b7130 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 o a particular f
b7140 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 ile are committe
b7150 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 d to disk..*/.st
b7160 61 74 69 63 20 69 6e 74 20 6f 73 32 53 79 6e 63 atic int os2Sync
b7170 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
b7180 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 20 29 7b id, int flags ){
b7190 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
b71a0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
b71b0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 53 ;. OSTRACE3( "S
b71c0 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e YNC %d lock=%d\n
b71d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 ", pFile->h, pFi
b71e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a le->locktype );.
b71f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
b7200 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 ST. if( flags &
b7210 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
b7220 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f L){. sqlite3_
b7230 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
b7240 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
b7250 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 sync_count++;.#e
b7260 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20 ndif. /* If we
b7270 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
b7280 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 e SQLITE_NO_SYNC
b7290 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 flag, then sync
b72a0 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f ing is a. ** no
b72b0 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 -op. */.#ifdef
b72c0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 SQLITE_NO_SYNC.
b72d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
b72e0 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 75 R(pFile);. retu
b72f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 rn SQLITE_OK;.#e
b7300 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f 73 lse. return Dos
b7310 52 65 73 65 74 42 75 66 66 65 72 28 20 70 46 69 ResetBuffer( pFi
b7320 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 le->h ) == NO_ER
b7330 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 ROR ? SQLITE_OK
b7340 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a : SQLITE_IOERR;.
b7350 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
b7360 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 Determine the cu
b7370 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 rrent size of a
b7380 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f file in bytes.*/
b7390 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 .static int os2F
b73a0 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 33 ileSize( sqlite3
b73b0 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 _file *id, sqlit
b73c0 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 20 e3_int64 *pSize
b73d0 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d ){. APIRET rc =
b73e0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c NO_ERROR;. FIL
b73f0 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46 69 ESTATUS3 fsts3Fi
b7400 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 leInfo;. memset
b7410 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c (&fsts3FileInfo,
b7420 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 0, sizeof(fsts3
b7430 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 73 FileInfo));. as
b7440 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
b7450 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
b7460 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
b7470 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a 20 IOERR_FSTAT );.
b7480 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 69 rc = DosQueryFi
b7490 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 6c leInfo( ((os2Fil
b74a0 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 e*)id)->h, FIL_S
b74b0 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 46 TANDARD, &fsts3F
b74c0 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 ileInfo, sizeof(
b74d0 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a FILESTATUS3) );.
b74e0 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 if( rc == NO_E
b74f0 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 69 RROR ){. *pSi
b7500 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 6e ze = fsts3FileIn
b7510 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 72 fo.cbFile;. r
b7520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
b7530 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
b7540 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
b7550 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a R_FSTAT;. }.}..
b7560 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 /*.** Acquire a
b7570 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a reader lock..*/.
b7580 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 static int getRe
b7590 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 adLock( os2File
b75a0 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c 45 *pFile ){. FILE
b75b0 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a LOCK LockArea,.
b75c0 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
b75d0 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54 ckArea;. APIRET
b75e0 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 res;. memset(&
b75f0 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a LockArea, 0, siz
b7600 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a eof(LockArea));.
b7610 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
b7620 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
b7630 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
b7640 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
b7650 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
b7660 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e . LockArea.lRan
b7670 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 ge = SHARED_SIZE
b7680 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c ;. UnlockArea.l
b7690 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 55 Offset = 0L;. U
b76a0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
b76b0 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 44 = 0L;. res = D
b76c0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
b76d0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
b76e0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
b76f0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
b7700 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 1L );. OSTRACE3
b7710 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 25 ( "GETREADLOCK %
b7720 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 d res=%d\n", pFi
b7730 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 le->h, res );.
b7740 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f return res;.}../
b7750 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 *.** Undo a read
b7760 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lock.*/.static i
b7770 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 nt unlockReadLoc
b7780 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 29 k( os2File *id )
b7790 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f {. FILELOCK Lo
b77a0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 ckArea,.
b77b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a UnlockArea;.
b77c0 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20 APIRET res;.
b77d0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
b77e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
b77f0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
b7800 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
b7810 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
b7820 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 ea));. LockArea
b7830 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 .lOffset = 0L;.
b7840 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 LockArea.lRange
b7850 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 = 0L;. UnlockA
b7860 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 rea.lOffset = SH
b7870 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 6e ARED_FIRST;. Un
b7880 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
b7890 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
b78a0 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c res = DosSetFil
b78b0 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 26 eLocks( id->h, &
b78c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
b78d0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
b78e0 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 OUT, 1L );. OST
b78f0 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d 52 RACE3( "UNLOCK-R
b7900 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 6e EADLOCK file han
b7910 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c 6e dle=%d res=%d?\n
b7920 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 3b ", id->h, res );
b7930 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d . return res;.}
b7940 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
b7950 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
b7960 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
b7970 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
b7980 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
b7990 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
b79a0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
b79b0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
b79c0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
b79d0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
b79e0 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
b79f0 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
b7a00 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
b7a10 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
b7a20 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
b7a30 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
b7a40 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
b7a50 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
b7a60 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
b7a70 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
b7a80 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
b7a90 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
b7aa0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
b7ab0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
b7ac0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
b7ad0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
b7ae0 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
b7af0 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
b7b00 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
b7b10 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
b7b20 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
b7b30 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
b7b40 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
b7b50 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
b7b60 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
b7b70 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
b7b80 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
b7b90 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
b7ba0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
b7bb0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
b7bc0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
b7bd0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
b7be0 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
b7bf0 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
b7c00 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
b7c10 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
b7c20 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 6b . The os2Unlock
b7c30 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 () routine.** er
b7c40 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 ases all locks a
b7c50 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 t once and retur
b7c60 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c ns us immediatel
b7c70 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 y to locking lev
b7c80 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e el 0..** It is n
b7c90 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c ot possible to l
b7ca0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
b7cb0 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 level one step
b7cc0 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a at a time. You.
b7cd0 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 ** must go strai
b7ce0 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c ght to locking l
b7cf0 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 evel 0..*/.stati
b7d00 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 73 c int os2Lock( s
b7d10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
b7d20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b int locktype ){
b7d30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
b7d40 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 TE_OK; /*
b7d50 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d Return code from
b7d60 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a subroutines */.
b7d70 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e APIRET res = N
b7d80 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 52 O_ERROR; /* R
b7d90 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f 32 esult of an OS/2
b7da0 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 lock call */.
b7db0 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b int newLocktype;
b7dc0 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 /* Set pF
b7dd0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f ile->locktype to
b7de0 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f this value befo
b7df0 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 re exiting */.
b7e00 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f int gotPendingLo
b7e10 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 ck = 0;/* True i
b7e20 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 f we acquired a
b7e30 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 PENDING lock thi
b7e40 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c 45 s time */. FILE
b7e50 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a LOCK LockArea,.
b7e60 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
b7e70 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 6c ckArea;. os2Fil
b7e80 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
b7e90 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 65 ile*)id;. memse
b7ea0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 t(&LockArea, 0,
b7eb0 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 sizeof(LockArea)
b7ec0 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c );. memset(&Unl
b7ed0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
b7ee0 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
b7ef0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
b7f00 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 !=0 );. OSTRACE
b7f10 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 4( "LOCK %d %d w
b7f20 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d as %d\n", pFile-
b7f30 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 >h, locktype, pF
b7f40 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
b7f50 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
b7f60 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 is already a loc
b7f70 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f k of this type o
b7f80 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 r more restricti
b7f90 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f ve on the. ** o
b7fa0 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 s2File, do nothi
b7fb0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 ng. Don't use th
b7fc0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 e end_lock: exit
b7fd0 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 path, as. ** s
b7fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
b7ff0 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e er() hasn't been
b8000 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a called yet.. *
b8010 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
b8020 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 ocktype>=locktyp
b8030 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 e ){. OSTRACE
b8040 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 6f 3( "LOCK %d %d o
b8050 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 k (already held)
b8060 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c \n", pFile->h, l
b8070 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 ocktype );. r
b8080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
b8090 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
b80a0 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 sure the locking
b80b0 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 sequence is cor
b80c0 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 rect. */. asse
b80d0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
b80e0 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 ype!=NO_LOCK ||
b80f0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
b8100 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
b8110 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e t( locktype!=PEN
b8120 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 DING_LOCK );. a
b8130 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
b8140 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c =RESERVED_LOCK |
b8150 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 | pFile->locktyp
b8160 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
b8170 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 ;.. /* Lock the
b8180 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 PENDING_LOCK by
b8190 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f te if we need to
b81a0 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 acquire a PENDI
b81b0 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 NG lock or. **
b81c0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 a SHARED lock.
b81d0 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72 If we are acquir
b81e0 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ing a SHARED loc
b81f0 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69 k, the acquisiti
b8200 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 on of. ** the P
b8210 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 ENDING_LOCK byte
b8220 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 is temporary..
b8230 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 */. newLocktyp
b8240 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 e = pFile->lockt
b8250 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 ype;. if( pFile
b8260 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c ->locktype==NO_L
b8270 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f OCK. || (lo
b8280 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
b8290 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d E_LOCK && pFile-
b82a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 >locktype==RESER
b82b0 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 VED_LOCK). ){.
b82c0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
b82d0 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 set = PENDING_BY
b82e0 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
b82f0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
b8300 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
b8310 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
b8320 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
b8330 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77 = 0L;.. /* w
b8340 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 ait longer than
b8350 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 72 LOCK_TIMEOUT her
b8360 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 6f e not to have to
b8370 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 69 try multiple ti
b8380 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 3d mes */. res =
b8390 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
b83a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
b83b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
b83c0 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b 0a ea, 100L, 0L );.
b83d0 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e if( res == N
b83e0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
b83f0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
b8400 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 = 1;. OSTRA
b8410 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 65 CE3( "LOCK %d pe
b8420 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 nding lock boole
b8430 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 5c an set. res=%d\
b8440 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
b8450 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a s );. }. }..
b8460 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 /* Acquire a s
b8470 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a hared lock. */.
b8480 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
b8490 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 SHARED_LOCK && r
b84a0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
b84b0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
b84c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e ile->locktype==N
b84d0 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 O_LOCK );. re
b84e0 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 s = getReadLock(
b84f0 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 pFile);. if(
b8500 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
b8510 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
b8520 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
b8530 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 CK;. }. OS
b8540 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
b8550 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64 20 acquire shared
b8560 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c lock. res=%d\n",
b8570 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
b8580 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
b8590 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c ire a RESERVED l
b85a0 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
b85b0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 ocktype==RESERVE
b85c0 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d D_LOCK && res ==
b85d0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
b85e0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
b85f0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
b8600 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f 63 _LOCK );. Loc
b8610 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
b8620 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 RESERVED_BYTE;.
b8630 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
b8640 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c ge = 1L;. Unl
b8650 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
b8660 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
b8670 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
b8680 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 ;. res = DosS
b8690 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
b86a0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
b86b0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
b86c0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
b86d0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d );. if( res =
b86e0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
b86f0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
b8700 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b = RESERVED_LOCK;
b8710 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 . }. OSTRA
b8720 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 CE3( "LOCK %d ac
b8730 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20 6c quire reserved l
b8740 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 ock. res=%d\n",
b8750 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
b8760 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
b8770 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 re a PENDING loc
b8780 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
b8790 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
b87a0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 _LOCK && res ==
b87b0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
b87c0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 newLocktype = PE
b87d0 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 NDING_LOCK;.
b87e0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
b87f0 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 0;. OSTRACE2
b8800 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 ( "LOCK %d acqui
b8810 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e re pending lock.
b8820 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f pending lock bo
b8830 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c olean unset.\n",
b8840 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d pFile->h );. }
b8850 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
b8860 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
b8870 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
b8880 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
b8890 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e LOCK && res == N
b88a0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 O_ERROR ){. a
b88b0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
b88c0 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c cktype>=SHARED_L
b88d0 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d OCK );. res =
b88e0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 unlockReadLock(
b88f0 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 pFile);. OSTR
b8900 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f 63 ACE2( "unreadloc
b8910 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 k = %d\n", res )
b8920 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
b8930 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
b8940 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b 41 FIRST;. LockA
b8950 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 rea.lRange = SHA
b8960 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 6e RED_SIZE;. Un
b8970 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
b8980 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
b8990 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
b89a0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
b89b0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
b89c0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
b89d0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
b89e0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
b89f0 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
b8a00 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
b8a10 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 newLocktype
b8a20 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 = EXCLUSIVE_LOC
b8a30 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
b8a40 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f OSTRACE2( "O
b8a50 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 3d S/2 error-code =
b8a60 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 %d\n", res );.
b8a70 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b getReadLock
b8a80 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 (pFile);. }.
b8a90 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
b8aa0 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 78 CK %d acquire ex
b8ab0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 72 clusive lock. r
b8ac0 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d es=%d\n", pFile-
b8ad0 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a >h, res );. }..
b8ae0 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 /* If we are h
b8af0 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 olding a PENDING
b8b00 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 lock that ought
b8b10 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c to be released,
b8b20 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 then. ** relea
b8b30 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a se it now.. */.
b8b40 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 if( gotPending
b8b50 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 Lock && locktype
b8b60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
b8b70 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20 . int r;.
b8b80 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
b8b90 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 = 0L;. LockA
b8ba0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b rea.lRange = 0L;
b8bb0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
b8bc0 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e lOffset = PENDIN
b8bd0 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f G_BYTE;. Unlo
b8be0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
b8bf0 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 53 1L;. r = DosS
b8c00 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
b8c10 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
b8c20 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
b8c30 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
b8c40 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
b8c50 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b "LOCK %d unlock
b8c60 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 73 ing pending/is s
b8c70 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c 20 hared. r=%d\n",
b8c80 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 pFile->h, r );.
b8c90 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 }.. /* Update
b8ca0 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
b8cb0 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 lock has held i
b8cc0 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 n the file descr
b8cd0 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 iptor then. **
b8ce0 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f return the appro
b8cf0 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f priate result co
b8d00 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 de.. */. if( r
b8d10 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
b8d20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
b8d30 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
b8d40 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f OSTRACE4( "LO
b8d50 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 CK FAILED %d try
b8d60 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 ing for %d but g
b8d70 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ot %d\n", pFile-
b8d80 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 >h,.
b8d90 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c locktype, newL
b8da0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 ocktype );. r
b8db0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
b8dc0 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
b8dd0 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b cktype = newLock
b8de0 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 type;. OSTRACE3
b8df0 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 ( "LOCK %d now %
b8e00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b8e10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
b8e20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
b8e30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
b8e40 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
b8e50 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
b8e60 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
b8e70 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
b8e80 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
b8e90 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
b8ea0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
b8eb0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 ock is held, ret
b8ec0 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c urn.** non-zero,
b8ed0 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e otherwise zero.
b8ee0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
b8ef0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c s2CheckReservedL
b8f00 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ock( sqlite3_fil
b8f10 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 74 e *id, int *pOut
b8f20 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b ){. int r = 0;
b8f30 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
b8f40 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
b8f50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
b8f60 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 e!=0 );. if( pF
b8f70 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 ile->locktype>=R
b8f80 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a ESERVED_LOCK ){.
b8f90 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 4f r = 1;. O
b8fa0 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 STRACE3( "TEST W
b8fb0 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f R-LOCK %d %d (lo
b8fc0 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e cal)\n", pFile->
b8fd0 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 7b h, r );. }else{
b8fe0 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c . FILELOCK L
b8ff0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 ockArea,.
b9000 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
b9010 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 63 a;. APIRET rc
b9020 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 = NO_ERROR;.
b9030 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 memset(&LockAre
b9040 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 a, 0, sizeof(Loc
b9050 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 6d kArea));. mem
b9060 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
b9070 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
b9080 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f 63 kArea));. Loc
b9090 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
b90a0 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 RESERVED_BYTE;.
b90b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
b90c0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c ge = 1L;. Unl
b90d0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
b90e0 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
b90f0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
b9100 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65 ;. rc = DosSe
b9110 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c tFileLocks( pFil
b9120 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 e->h, &UnlockAre
b9130 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f a, &LockArea, LO
b9140 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 CK_TIMEOUT, 0L )
b9150 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 ;. OSTRACE3(
b9160 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
b9170 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 lock reserved b
b9180 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 46 yte rc=%d\n", pF
b9190 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20 ile->h, rc );.
b91a0 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 if( rc == NO_E
b91b0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 50 RROR ){. AP
b91c0 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 52 IRET rcu = NO_ER
b91d0 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 63 ROR; /* return c
b91e0 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e ode for unlockin
b91f0 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b 41 g */. LockA
b9200 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
b9210 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 ;. LockArea
b9220 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
b9230 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
b9240 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 Offset = RESERVE
b9250 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 6e D_BYTE;. Un
b9260 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
b9270 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 20 = 1L;. rcu
b9280 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
b9290 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
b92a0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
b92b0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
b92c0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 4f T, 0L );. O
b92d0 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 STRACE3( "TEST W
b92e0 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b R-LOCK %d unlock
b92f0 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72 reserved byte r
b9300 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
b9310 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a 20 , rcu );. }.
b9320 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e r = !(rc == N
b9330 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f 53 O_ERROR);. OS
b9340 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 TRACE3( "TEST WR
b9350 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d -LOCK %d %d (rem
b9360 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ote)\n", pFile->
b9370 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 h, r );. }. *p
b9380 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 72 Out = r;. retur
b9390 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
b93a0 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
b93b0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
b93c0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
b93d0 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e id to locktype.
b93e0 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
b93f0 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
b9400 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
b9410 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
b9420 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
b9430 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
b9440 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
b9450 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
b9460 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
b9470 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
b9480 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
b9490 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 no-op..**.** It
b94a0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
b94b0 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 for this routine
b94c0 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 to fail if the
b94d0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a second argument.
b94e0 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 ** is NO_LOCK.
b94f0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
b9500 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 gument is SHARED
b9510 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 _LOCK then this
b9520 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 routine.** might
b9530 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
b9540 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 OERR;.*/.static
b9550 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 73 int os2Unlock( s
b9560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
b9570 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b int locktype ){
b9580 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 6f . int type;. o
b9590 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b95a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b95b0 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c 49 APIRET rc = SQLI
b95c0 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 20 TE_OK;. APIRET
b95d0 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a res = NO_ERROR;.
b95e0 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b FILELOCK Lock
b95f0 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 Area,.
b9600 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 UnlockArea;.
b9610 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
b9620 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
b9630 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
b9640 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
b9650 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
b9660 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ea));. assert(
b9670 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 pFile!=0 );. as
b9680 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d sert( locktype<=
b9690 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
b96a0 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c 4f OSTRACE4( "UNLO
b96b0 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 CK %d to %d was
b96c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
b96d0 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 locktype, pFile
b96e0 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
b96f0 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f type = pFile->lo
b9700 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 cktype;. if( ty
b9710 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe>=EXCLUSIVE_LO
b9720 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 CK ){. LockAr
b9730 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
b9740 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 . LockArea.lR
b9750 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 ange = 0L;. U
b9760 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
b9770 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
b9780 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
b9790 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 .lRange = SHARED
b97a0 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 3d _SIZE;. res =
b97b0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
b97c0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
b97d0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
b97e0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
b97f0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
b9800 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
b9810 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
b9820 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 res=%d\n", pFile
b9830 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 ->h, res );.
b9840 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
b9850 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 74 ARED_LOCK && get
b9860 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 ReadLock(pFile)
b9870 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
b9880 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f /* This sho
b9890 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e uld never happen
b98a0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 . We should alw
b98b0 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 ays be able to.
b98c0 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 ** reacquir
b98d0 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 e the read lock
b98e0 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 */. OSTRACE
b98f0 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 3( "UNLOCK %d to
b9900 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 %d getReadLock(
b9910 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 69 ) failed\n", pFi
b9920 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 le->h, locktype
b9930 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
b9940 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
b9950 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 K;. }. }. i
b9960 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 f( type>=RESERVE
b9970 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f D_LOCK ){. Lo
b9980 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
b9990 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 0L;. LockAre
b99a0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
b99b0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
b99c0 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 ffset = RESERVED
b99d0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 _BYTE;. Unloc
b99e0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
b99f0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
b9a00 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
b9a10 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
b9a20 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
b9a30 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
b9a40 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
b9a50 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 73 ( "UNLOCK %d res
b9a60 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 2c erved res=%d\n",
b9a70 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
b9a80 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b ;. }. if( lock
b9a90 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 type==NO_LOCK &&
b9aa0 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f type>=SHARED_LO
b9ab0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 CK ){. res =
b9ac0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 unlockReadLock(p
b9ad0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 File);. OSTRA
b9ae0 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE5( "UNLOCK %d
b9af0 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 65 is %d want %d re
b9b00 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
b9b10 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 70 h, type, locktyp
b9b20 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 e, res );. }.
b9b30 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e if( type>=PENDIN
b9b40 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f G_LOCK ){. Lo
b9b50 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
b9b60 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 0L;. LockAre
b9b70 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
b9b80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
b9b90 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f ffset = PENDING_
b9ba0 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b BYTE;. Unlock
b9bb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c Area.lRange = 1L
b9bc0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 ;. res = DosS
b9bd0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
b9be0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
b9bf0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
b9c00 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
b9c10 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
b9c20 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e 64 "UNLOCK %d pend
b9c30 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 ing res=%d\n", p
b9c40 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
b9c50 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
b9c60 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
b9c70 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 ;. OSTRACE3( "U
b9c80 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c NLOCK %d now %d\
b9c90 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 n", pFile->h, pF
b9ca0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
b9cb0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
b9cc0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 ./*.** Control a
b9cd0 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 nd query of the
b9ce0 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 open file handle
b9cf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
b9d00 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 os2FileControl(s
b9d10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
b9d20 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 int op, void *p
b9d30 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 Arg){. switch(
b9d40 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 op ){. case S
b9d50 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b QLITE_FCNTL_LOCK
b9d60 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a STATE: {. *
b9d70 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 6f (int*)pArg = ((o
b9d80 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 s2File*)id)->loc
b9d90 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 54 ktype;. OST
b9da0 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c 4f RACE3( "FCNTL_LO
b9db0 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b 3d CKSTATE %d lock=
b9dc0 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c 65 %d\n", ((os2File
b9dd0 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 46 *)id)->h, ((os2F
b9de0 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 ile*)id)->lockty
b9df0 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 pe );. retu
b9e00 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
b9e10 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
b9e20 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d SQLITE_ERROR;.}
b9e30 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
b9e40 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 he sector size i
b9e50 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 n bytes of the u
b9e60 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 nderlying block
b9e70 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 device for.** th
b9e80 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 e specified file
b9e90 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 . This is almost
b9ea0 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
b9eb0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a s, but may be.**
b9ec0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 larger for some
b9ed0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 devices..**.**
b9ee0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 SQLite code assu
b9ef0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f mes this functio
b9f00 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 n cannot fail. I
b9f10 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 t also assumes t
b9f20 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 hat.** if two fi
b9f30 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 les are created
b9f40 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 in the same file
b9f50 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 -system director
b9f60 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 y (i.e..** a dat
b9f70 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f abase and its jo
b9f80 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 urnal file) that
b9f90 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
b9fa0 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 will be the.**
b9fb0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a same for both..*
b9fc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
b9fd0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
b9fe0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
b9ff0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 return SQLITE_DE
ba000 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a FAULT_SECTOR_SIZ
ba010 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 E;.}../*.** Retu
ba020 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 rn a vector of d
ba030 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
ba040 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 stics..*/.static
ba050 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 68 int os2DeviceCh
ba060 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 aracteristics(sq
ba070 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
ba080 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
ba090 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72 ./*.** Character
ba0a0 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 set conversion
ba0b0 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 objects used by
ba0c0 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 69 conversion routi
ba0d0 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 nes..*/.static U
ba0e0 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 66 convObject ucUtf
ba0f0 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 8 = NULL; /* con
ba100 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 46 vert between UTF
ba110 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a -8 and UCS-2 */.
ba120 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 static UconvObje
ba130 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b ct uclCp = NULL;
ba140 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 /* convert bet
ba150 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 70 ween local codep
ba160 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f age and UCS-2 */
ba170 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 ../*.** Helper f
ba180 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 69 unction to initi
ba190 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 72 alize the conver
ba1a0 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f sion objects fro
ba1b0 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a m and to UTF-8..
ba1c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
ba1d0 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 nitUconvObjects(
ba1e0 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 55 void ){. if( U
ba1f0 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a niCreateUconvObj
ba200 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 55 ect( UTF_8, &ucU
ba210 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 tf8 ) != ULS_SUC
ba220 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 66 CESS ). ucUtf
ba230 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 28 8 = NULL;. if (
ba240 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f UniCreateUconvO
ba250 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 20 bject( (UniChar
ba260 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c 20 *)L"@path=yes",
ba270 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 5f &uclCp ) != ULS_
ba280 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 SUCCESS ). uc
ba290 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f lCp = NULL;.}../
ba2a0 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 *.** Helper func
ba2b0 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 65 tion to free the
ba2c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 conversion obje
ba2d0 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 cts from and to
ba2e0 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 UTF-8..*/.static
ba2f0 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 4f void freeUconvO
ba300 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a bjects( void ){.
ba310 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29 0a if ( ucUtf8 ).
ba320 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 UniFreeUconv
ba330 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 29 Object( ucUtf8 )
ba340 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 29 ;. if ( uclCp )
ba350 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e . UniFreeUcon
ba360 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 29 vObject( uclCp )
ba370 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c ;. ucUtf8 = NUL
ba380 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c L;. uclCp = NUL
ba390 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 L;.}../*.** Help
ba3a0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 er function to c
ba3b0 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 6c onvert UTF-8 fil
ba3c0 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c 20 enames to local
ba3d0 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a 2a OS/2 codepage..*
ba3e0 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 * The two-step p
ba3f0 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f rocess: first co
ba400 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 nvert the incomi
ba410 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a ng UTF-8 string.
ba420 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e ** into UCS-2 an
ba430 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d d then from UCS-
ba440 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 2 to the current
ba450 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 codepage..** Th
ba460 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 e returned char
ba470 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 pointer has to b
ba480 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 e freed..*/.stat
ba490 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 74 ic char *convert
ba4a0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 6f Utf8PathToCp( co
ba4b0 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a nst char *in ){.
ba4c0 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61 UniChar tempPa
ba4d0 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a th[CCHMAXPATH];.
ba4e0 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 char *out = (c
ba4f0 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 har *)calloc( CC
ba500 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a HMAXPATH, 1 );..
ba510 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20 if( !out ).
ba520 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 return NULL;..
ba530 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 if( !ucUtf8 ||
ba540 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69 !uclCp ). ini
ba550 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b tUconvObjects();
ba560 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 .. /* determine
ba570 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 string for the
ba580 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 conversion of UT
ba590 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 F-8 which is CP1
ba5a0 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69 208 */. if( Uni
ba5b0 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 38 StrToUcs( ucUtf8
ba5c0 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61 , tempPath, (cha
ba5d0 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 r *)in, CCHMAXPA
ba5e0 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 TH ) != ULS_SUCC
ba5f0 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e ESS ). return
ba600 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 out; /* if conv
ba610 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 ersion fails, re
ba620 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73 turn the empty s
ba630 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63 tring */.. /* c
ba640 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 onversion for cu
ba650 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 rrent codepage w
ba660 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 hich can be used
ba670 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 for paths */.
ba680 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75 UniStrFromUcs( u
ba690 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 50 clCp, out, tempP
ba6a0 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20 ath, CCHMAXPATH
ba6b0 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 );.. return out
ba6c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 ;.}../*.** Helpe
ba6d0 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f r function to co
ba6e0 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 20 nvert filenames
ba6f0 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 70 from local codep
ba700 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a age to UTF-8..**
ba710 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72 The two-step pr
ba720 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e ocess: first con
ba730 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e vert the incomin
ba740 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 69 g codepage-speci
ba750 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e fic.** string in
ba760 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65 to UCS-2 and the
ba770 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 n from UCS-2 to
ba780 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 20 the codepage of
ba790 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 65 UTF-8..** The re
ba7a0 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e turned char poin
ba7b0 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 ter has to be fr
ba7c0 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 eed..**.** This
ba7d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d function is non-
ba7e0 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 6c static to be abl
ba7f0 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e e to use this in
ba800 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20 shell.c and.**
ba810 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 74 similar applicat
ba820 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 63 ions that take c
ba830 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 75 ommand line argu
ba840 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a ments..*/.char *
ba850 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55 convertCpPathToU
ba860 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 20 tf8( const char
ba870 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 *in ){. UniChar
ba880 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 tempPath[CCHMAX
ba890 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f PATH];. char *o
ba8a0 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c ut = (char *)cal
ba8b0 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c loc( CCHMAXPATH,
ba8c0 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 1 );.. if( !ou
ba8d0 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e t ). return N
ba8e0 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 ULL;.. if( !ucU
ba8f0 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a tf8 || !uclCp ).
ba900 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a initUconvObj
ba910 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f ects();.. /* co
ba920 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 nversion for cur
ba930 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68 rent codepage wh
ba940 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 ich can be used
ba950 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 69 for paths */. i
ba960 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 f( UniStrToUcs(
ba970 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 2c uclCp, tempPath,
ba980 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 (char *)in, CCH
ba990 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 MAXPATH ) != ULS
ba9a0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 _SUCCESS ). r
ba9b0 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 eturn out; /* if
ba9c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c conversion fail
ba9d0 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d s, return the em
ba9e0 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 pty string */..
ba9f0 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 /* determine st
baa00 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e ring for the con
baa10 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 version of UTF-8
baa20 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38 which is CP1208
baa30 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d */. UniStrFrom
baa40 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 74 Ucs( ucUtf8, out
baa50 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d , tempPath, CCHM
baa60 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 AXPATH );.. ret
baa70 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn out;.}../*.*
baa80 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 * This vector de
baa90 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 fines all the me
baaa0 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f thods that can o
baab0 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 perate on an.**
baac0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 sqlite3_file for
baad0 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 os2..*/.static
baae0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
baaf0 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d 65 _methods os2IoMe
bab00 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 thod = {. 1,
bab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bab20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
bab30 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c 0a */. os2Close,.
bab40 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 32 os2Read,. os2
bab50 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 6e Write,. os2Trun
bab60 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 2c cate,. os2Sync,
bab70 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c 0a . os2FileSize,.
bab80 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 os2Lock,. os2
bab90 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 65 Unlock,. os2Che
baba0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a ckReservedLock,.
babb0 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c os2FileControl
babc0 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 7a ,. os2SectorSiz
babd0 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 68 e,. os2DeviceCh
babe0 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b aracteristics.};
babf0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
bac40 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 * Here ends the
bac50 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 I/O methods that
bac60 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 form the sqlite
bac70 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
bac80 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ect..**.** The n
bac90 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 ext block of cod
baca0 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
bacb0 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a VFS methods..**
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
bad10 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 ** Create a temp
bad20 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 orary file name
bad30 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d in zBuf. zBuf m
bad40 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 ust be big enoug
bad50 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 h to.** hold at
bad60 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
bad70 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a characters..*/.
bad80 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 static int getTe
bad90 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c mpname(int nBuf,
bada0 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 char *zBuf ){.
badb0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e static const un
badc0 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 signed char zCha
badd0 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 rs[] =. "abcd
bade0 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 efghijklmnopqrst
badf0 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 uvwxyz". "ABC
bae00 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 DEFGHIJKLMNOPQRS
bae10 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 TUVWXYZ". "01
bae20 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74 23456789";. int
bae30 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 i, j;. char zT
bae40 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a 20 empPathBuf[3];.
bae50 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 3d PSZ zTempPath =
bae60 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 68 (PSZ)&zTempPath
bae70 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 Buf;. if( sqlit
bae80 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 e3_temp_director
bae90 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 61 y ){. zTempPa
baea0 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d th = sqlite3_tem
baeb0 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 7d p_directory;. }
baec0 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 6f else{. if( Do
baed0 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 sScanEnv( (PSZ)"
baee0 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 TEMP", &zTempPat
baef0 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 h ) ){. if(
baf00 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 DosScanEnv( (PS
baf10 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50 Z)"TMP", &zTempP
baf20 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20 ath ) ){.
baf30 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 if( DosScanEnv(
baf40 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20 (PSZ)"TMPDIR",
baf50 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a &zTempPath ) ){.
baf60 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47 ULONG
baf70 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c ulDriveNum = 0,
baf80 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b ulDriveMap = 0;
baf90 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 51 . DosQ
bafa0 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 ueryCurrentDisk(
bafb0 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 &ulDriveNum, &u
bafc0 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 lDriveMap );.
bafd0 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 sprintf(
bafe0 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74 (char*)zTempPat
baff0 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29 h, "%c:", (char)
bb000 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e ( 'A' + ulDriveN
bb010 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20 um - 1 ) );.
bb020 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
bb030 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 72 }. }. /* Str
bb040 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 6e ip off a trailin
bb050 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 63 g slashes or bac
bb060 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 77 kslashes, otherw
bb070 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 74 ise we would get
bb080 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c 65 *. * multiple
bb090 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 77 (back)slashes w
bb0a0 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 4f hich causes DosO
bb0b0 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 20 pen() to fail.
bb0c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 20 *.
bb0d0 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 63 * Trailing spac
bb0e0 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 es are not allow
bb0f0 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 20 ed, either.
bb100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb110 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d 20 */. j =
bb120 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
bb130 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77 68 zTempPath);. wh
bb140 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 28 20 ile( j > 0 && (
bb150 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d zTempPath[j-1] =
bb160 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70 50 = '\\' || zTempP
bb170 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27 0a ath[j-1] == '/'.
bb180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb190 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68 || zTempPath
bb1a0 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 29 20 29 [j-1] == ' ' ) )
bb1b0 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 7d 0a 20 {. j--;. }.
bb1c0 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d 20 3d 20 zTempPath[j] =
bb1d0 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 73 71 6c '\0';. if( !sql
bb1e0 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 ite3_temp_direct
bb1f0 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ory ){. char
bb200 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 20 3d 20 *zTempPathUTF =
bb210 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55 convertCpPathToU
bb220 74 66 38 28 20 7a 54 65 6d 70 50 61 74 68 20 29 tf8( zTempPath )
bb230 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e ;. sqlite3_sn
bb240 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c printf( nBuf-30,
bb250 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 zBuf,.
bb260 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 "%s
bb270 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 \\"SQLITE_TEMP_F
bb280 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d ILE_PREFIX, zTem
bb290 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 20 20 pPathUTF );.
bb2a0 66 72 65 65 28 20 7a 54 65 6d 70 50 61 74 68 55 free( zTempPathU
bb2b0 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 TF );. }else{.
bb2c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
bb2d0 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42 ntf( nBuf-30, zB
bb2e0 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 uf,.
bb2f0 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 "%s\\"
bb300 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
bb310 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 _PREFIX, zTempPa
bb320 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a 20 3d 20 th );. }. j =
bb330 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
bb340 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69 74 zBuf );. sqlit
bb350 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 e3_randomness( 2
bb360 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 0, &zBuf[j] );.
bb370 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c for( i = 0; i <
bb380 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 20; i++, j++ ){
bb390 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 . zBuf[j] = (
bb3a0 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
bb3b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
bb3c0 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
bb3d0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a hars)-1) ];. }.
bb3e0 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
bb3f0 20 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d 50 OSTRACE2( "TEMP
bb400 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 FILENAME: %s\n"
bb410 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 75 , zBuf );. retu
bb420 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
bb430 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 ../*.** Turn a r
bb440 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
bb450 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 into a full pat
bb460 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 hname. Write th
bb470 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 e full.** pathna
bb480 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e me into zFull[].
bb490 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 zFull[] will b
bb4a0 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d e at least pVfs-
bb4b0 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 >mxPathname.** b
bb4c0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f ytes in size..*/
bb4d0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 .static int os2F
bb4e0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 ullPathname(. s
bb4f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
bb500 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f , /* Po
bb510 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a inter to vfs obj
bb520 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ect */. const c
bb530 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 har *zRelative,
bb540 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 /* Possibly
bb550 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 relative input
bb560 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 path */. int nF
bb570 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ull,
bb580 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
bb590 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 output buffer i
bb5a0 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
bb5b0 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 r *zFull
bb5c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
bb5d0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a ut buffer */.){.
bb5e0 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 char *zRelativ
bb5f0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 eCp = convertUtf
bb600 38 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c 61 8PathToCp( zRela
bb610 74 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20 7a tive );. char z
bb620 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41 54 FullCp[CCHMAXPAT
bb630 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61 H] = "\0";. cha
bb640 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41 r *zFullUTF;. A
bb650 50 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 75 PIRET rc = DosQu
bb660 65 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 65 eryPathInfo( zRe
bb670 6c 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 55 lativeCp, FIL_QU
bb680 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 ERYFULLNAME, zFu
bb690 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 llCp,.
bb6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb6b0 20 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 48 CCHMAXPATH
bb6c0 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 6c );. free( zRel
bb6d0 61 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 75 ativeCp );. zFu
bb6e0 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 llUTF = convertC
bb6f0 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 75 pPathToUtf8( zFu
bb700 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 65 llCp );. sqlite
bb710 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 3_snprintf( nFul
bb720 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 l, zFull, zFullU
bb730 54 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 TF );. free( zF
bb740 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 75 ullUTF );. retu
bb750 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f rn rc == NO_ERRO
bb760 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 R ? SQLITE_OK :
bb770 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a SQLITE_IOERR;.}.
bb780 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 ../*.** Open a f
bb790 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
bb7a0 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71 nt os2Open(. sq
bb7b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
bb7c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
bb7d0 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e ot used */. con
bb7e0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
bb7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
bb800 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a me of the file *
bb810 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
bb820 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 *id,
bb830 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 /* Write the S
bb840 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c QLite file handl
bb850 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 e here */. int
bb860 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
bb870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
bb880 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a n mode flags */.
bb890 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 int *pOutFlags
bb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb8b0 2f 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e /* Status return
bb8c0 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 flags */.){. H
bb8d0 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 FILE h;. ULONG
bb8e0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 ulFileAttribute
bb8f0 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 = FILE_NORMAL;.
bb900 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 ULONG ulOpenFla
bb910 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 gs = 0;. ULONG
bb920 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a ulOpenMode = 0;.
bb930 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
bb940 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
bb950 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e . APIRET rc = N
bb960 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 O_ERROR;. ULONG
bb970 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61 ulAction;. cha
bb980 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68 r *zNameCp;. ch
bb990 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d ar zTmpname[CCHM
bb9a0 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a AXPATH+1]; /*
bb9b0 20 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 Buffer to hold
bb9c0 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69 6c name of temp fil
bb9d0 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 e */.. /* If th
bb9e0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
bb9f0 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
bba00 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 on is NULL, gene
bba10 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d rate a . ** tem
bba20 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
bba30 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 to use . */.
bba40 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 if( !zName ){.
bba50 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 int rc = getTe
bba60 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 54 mpname(CCHMAXPAT
bba70 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a H+1, zTmpname);.
bba80 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
bba90 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
bbaa0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
bbab0 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 zName = zTmp
bbac0 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 name;. }... me
bbad0 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 mset( pFile, 0,
bbae0 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 sizeof(*pFile) )
bbaf0 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 ;.. OSTRACE2( "
bbb00 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c OPEN want %d\n",
bbb10 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66 28 flags );.. if(
bbb20 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
bbb30 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
bbb40 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 {. ulOpenMode
bbb50 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f |= OPEN_ACCESS_
bbb60 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 4f READWRITE;. O
bbb70 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72 STRACE1( "OPEN r
bbb80 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a ead/write\n" );.
bbb90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f }else{. ulO
bbba0 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f penMode |= OPEN_
bbbb0 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b ACCESS_READONLY;
bbbc0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
bbbd0 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e OPEN read only\n
bbbe0 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 " );. }.. if(
bbbf0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
bbc00 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 PEN_CREATE ){.
bbc10 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d ulOpenFlags |=
bbc20 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 OPEN_ACTION_OPE
bbc30 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 N_IF_EXISTS | OP
bbc40 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 EN_ACTION_CREATE
bbc50 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 _IF_NEW;. OST
bbc60 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 RACE1( "OPEN ope
bbc70 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22 20 n new/create\n"
bbc80 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
bbc90 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f ulOpenFlags |= O
bbca0 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f PEN_ACTION_OPEN_
bbcb0 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e IF_EXISTS | OPEN
bbcc0 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f _ACTION_FAIL_IF_
bbcd0 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 NEW;. OSTRACE
bbce0 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78 1( "OPEN open ex
bbcf0 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d isting\n" );. }
bbd00 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 .. if( flags &
bbd10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
bbd20 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 _DB ){. ulOpe
bbd30 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 nMode |= OPEN_SH
bbd40 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 ARE_DENYNONE;.
bbd50 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
bbd60 4e 20 73 68 61 72 65 20 72 65 61 64 2f 77 72 69 N share read/wri
bbd70 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 te\n" );. }else
bbd80 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 {. ulOpenMode
bbd90 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 |= OPEN_SHARE_D
bbda0 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 ENYWRITE;. OS
bbdb0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 TRACE1( "OPEN sh
bbdc0 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 are read only\n"
bbdd0 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 );. }.. if( f
bbde0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
bbdf0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
bbe00 20 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61 74 ){. char pat
bbe10 68 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54 48 hUtf8[CCHMAXPATH
bbe20 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 ];.#ifdef NDEBUG
bbe30 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67 69 /* when debuggi
bbe40 6e 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 ng we want to ma
bbe50 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 64 65 ke sure it is de
bbe60 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46 leted */. ulF
bbe70 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 ileAttribute = F
bbe80 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64 ILE_HIDDEN;.#end
bbe90 69 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 if. os2FullPa
bbea0 74 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e thname( pVfs, zN
bbeb0 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 2c ame, CCHMAXPATH,
bbec0 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20 pathUtf8 );.
bbed0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
bbee0 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 l = convertUtf8P
bbef0 61 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 66 athToCp( pathUtf
bbf00 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 8 );. OSTRACE
bbf10 31 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 1( "OPEN hidden/
bbf20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 delete on close
bbf30 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 5c file attributes\
bbf40 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n" );. }else{.
bbf50 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f pFile->pathTo
bbf60 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 Del = NULL;.
bbf70 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 OSTRACE1( "OPEN
bbf80 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74 72 normal file attr
bbf90 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a ibute\n" );. }.
bbfa0 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65 . /* always ope
bbfb0 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65 n in random acce
bbfc0 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73 ss mode for poss
bbfd0 69 62 6c 79 20 62 65 74 74 65 72 20 73 70 65 65 ibly better spee
bbfe0 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 d */. ulOpenMod
bbff0 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f e |= OPEN_FLAGS_
bc000 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e RANDOM;. ulOpen
bc010 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 Mode |= OPEN_FLA
bc020 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 GS_FAIL_ON_ERROR
bc030 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c ;. ulOpenMode |
bc040 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 = OPEN_FLAGS_NOI
bc050 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 NHERIT;.. zName
bc060 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 Cp = convertUtf8
bc070 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20 PathToCp( zName
bc080 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 );. rc = DosOpe
bc090 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c n( (PSZ)zNameCp,
bc0a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bc0b0 20 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 &h,.
bc0c0 20 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a &ulAction,.
bc0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc0e0 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0L,.
bc0f0 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 ulFileAttrib
bc100 75 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ute,.
bc110 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 ulOpenFlags
bc120 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
bc130 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 ulOpenMode,.
bc140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 (P
bc150 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 EAOP2)NULL );.
bc160 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b free( zNameCp );
bc170 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f . if( rc != NO_
bc180 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54 ERROR ){. OST
bc190 52 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e 76 RACE7( "OPEN Inv
bc1a0 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d 25 alid handle rc=%
bc1b0 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 d: zName=%s, ulA
bc1c0 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 ction=%#lx, ulAt
bc1d0 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73 tr=%#lx, ulFlags
bc1e0 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 =%#lx, ulMode=%#
bc1f0 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 lx\n",.
bc200 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 rc, zName,
bc210 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 ulAction, ulFile
bc220 41 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70 65 Attribute, ulOpe
bc230 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f nFlags, ulOpenMo
bc240 64 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 46 de );. if( pF
bc250 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
bc260 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 . free( pFi
bc270 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b le->pathToDel );
bc280 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 . pFile->path
bc290 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 ToDel = NULL;.
bc2a0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
bc2b0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
bc2c0 49 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 ITE ){. OST
bc2d0 52 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64 20 RACE2( "OPEN %d
bc2e0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e Invalid handle\n
bc2f0 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c ", ((flags | SQL
bc300 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
bc310 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 Y) & ~SQLITE_OPE
bc320 4e 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b 0a N_READWRITE) );.
bc330 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 32 return os2
bc340 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d Open( pVfs, zNam
bc350 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20 20 e, id,.
bc360 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 ((f
bc370 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 lags | SQLITE_OP
bc380 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e EN_READONLY) & ~
bc390 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
bc3a0 57 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20 20 WRITE),.
bc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f pO
bc3c0 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d utFlags );. }
bc3d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 else{. retu
bc3e0 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 rn SQLITE_CANTOP
bc3f0 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 EN;. }. }..
bc400 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 if( pOutFlags )
bc410 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 {. *pOutFlags
bc420 20 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = flags & SQLIT
bc430 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
bc440 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ? SQLITE_OPEN_R
bc450 45 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49 54 EADWRITE : SQLIT
bc460 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b E_OPEN_READONLY;
bc470 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 . }.. pFile->p
bc480 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d Method = &os2IoM
bc490 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e ethod;. pFile->
bc4a0 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 h = h;. OpenCou
bc4b0 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52 nter(+1);. OSTR
bc4c0 41 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20 70 ACE3( "OPEN %d p
bc4d0 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 OutFlags=%d\n",
bc4e0 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c pFile->h, pOutFl
bc4f0 61 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ags );. return
bc500 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
bc510 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e .** Delete the n
bc520 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 amed file..*/.st
bc530 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c 65 atic int os2Dele
bc540 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 te(. sqlite3_vf
bc550 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
bc560 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bc570 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 Not used on os2
bc580 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
bc590 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 *zFilename,
bc5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
bc5b0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 ame of file to d
bc5c0 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 elete */. int s
bc5d0 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 yncDir
bc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc5f0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
bc600 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 os2 */.){. API
bc610 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f RET rc = NO_ERRO
bc620 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 R;. char *zFile
bc630 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 nameCp = convert
bc640 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46 Utf8PathToCp( zF
bc650 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d ilename );. Sim
bc660 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
bc670 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
bc680 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72 63 R_DELETE );. rc
bc690 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50 = DosDelete( (P
bc6a0 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 SZ)zFilenameCp )
bc6b0 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e ;. free( zFilen
bc6c0 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 ameCp );. OSTRA
bc6d0 43 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25 CE2( "DELETE \"%
bc6e0 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d s\"\n", zFilenam
bc6f0 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 e );. return rc
bc700 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 == NO_ERROR ? S
bc710 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
bc720 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a E_IOERR_DELETE;.
bc730 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 }../*.** Check t
bc740 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 he existance and
bc750 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c status of a fil
bc760 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
bc770 20 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73 71 os2Access(. sq
bc780 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
bc790 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 /* Not u
bc7a0 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 sed on os2 */.
bc7b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
bc7c0 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d ename, /* Nam
bc7d0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 e of file to che
bc7e0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 ck */. int flag
bc7f0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
bc800 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 /* Type of tes
bc810 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 t to make on thi
bc820 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 s file */. int
bc830 2a 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 *pOut
bc840 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 /* Write r
bc850 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
bc860 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 {. FILESTATUS3
bc870 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b fsts3ConfigInfo;
bc880 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e . APIRET rc = N
bc890 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 O_ERROR;. char
bc8a0 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 *zFilenameCp = c
bc8b0 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f onvertUtf8PathTo
bc8c0 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b Cp( zFilename );
bc8d0 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73 74 .. memset( &fst
bc8e0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c s3ConfigInfo, 0,
bc8f0 20 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e sizeof(fsts3Con
bc900 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63 figInfo) );. rc
bc910 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49 = DosQueryPathI
bc920 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e nfo( (PSZ)zFilen
bc930 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44 ameCp, FIL_STAND
bc940 41 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ARD,.
bc950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 66 &f
bc960 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 sts3ConfigInfo,
bc970 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 sizeof(FILESTATU
bc980 53 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20 7a S3) );. free( z
bc990 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 FilenameCp );.
bc9a0 4f 53 54 52 41 43 45 34 28 20 22 41 43 43 45 53 OSTRACE4( "ACCES
bc9b0 53 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 S fsts3ConfigInf
bc9c0 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66 6c o.attrFile=%d fl
bc9d0 61 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c ags=%d rc=%d\n",
bc9e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 73 74 . fst
bc9f0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 s3ConfigInfo.att
bca00 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 rFile, flags, rc
bca10 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c );. switch( fl
bca20 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ags ){. case
bca30 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
bca40 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c AD:. case SQL
bca50 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
bca60 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 S:. rc = (r
bca70 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a c == NO_ERROR);.
bca80 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 OSTRACE3(
bca90 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 "ACCESS %s acces
bcaa0 73 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65 78 s of read and ex
bcab0 69 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20 ists rc=%d\n",
bcac0 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b zFilename, rc );
bcad0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
bcae0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
bcaf0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a CESS_READWRITE:.
bcb00 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d rc = (rc =
bcb10 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20 28 = NO_ERROR) && (
bcb20 20 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 (fsts3ConfigInf
bcb30 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49 4c o.attrFile & FIL
bcb40 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 E_READONLY) == 0
bcb50 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 );. OSTRAC
bcb60 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 E3( "ACCESS %s a
bcb70 63 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77 72 ccess of read/wr
bcb80 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a ite rc=%d\n", z
bcb90 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a Filename, rc );.
bcba0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
bcbb0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 default:.
bcbc0 61 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c 69 assert( !"Invali
bcbd0 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 d flags argument
bcbe0 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 " );. }. *pOut
bcbf0 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 = rc;. return
bcc00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 SQLITE_OK;.}...#
bcc10 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
bcc20 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
bcc30 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 N./*.** Interfac
bcc40 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 es for opening a
bcc50 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
bcc60 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 finding entry p
bcc70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 oints.** within
bcc80 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
bcc90 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ry, and closing
bcca0 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
bccb0 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 ry..*/./*.** Int
bccc0 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
bccd0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
bcce0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
bccf0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
bcd00 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
bcd10 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
bcd20 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
bcd30 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 library..*/.stat
bcd40 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 ic void *os2DlOp
bcd50 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
bcd60 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 pVfs, const char
bcd70 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
bcd80 55 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32 35 UCHAR loadErr[25
bcd90 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6];. HMODULE hm
bcda0 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b od;. APIRET rc;
bcdb0 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
bcdc0 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 meCp = convertUt
bcdd0 66 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 f8PathToCp(zFile
bcde0 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f name);. rc = Do
bcdf0 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a sLoadModule((PSZ
bce00 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 )loadErr, sizeof
bce10 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 (loadErr), zFile
bce20 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a nameCp, &hmod);.
bce30 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 free(zFilename
bce40 43 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 Cp);. return rc
bce50 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 != NO_ERROR ? 0
bce60 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a : (void*)hmod;.
bce70 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 }./*.** A no-op
bce80 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72 20 since the error
bce90 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
bcea0 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d on the DosLoadM
bceb0 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f odule call..** o
bcec0 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73 s2Dlopen returns
bced0 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64 zero if DosLoad
bcee0 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73 75 Module is not su
bcef0 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 ccessful..*/.sta
bcf00 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45 72 tic void os2DlEr
bcf10 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
bcf20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c *pVfs, int nBuf,
bcf30 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b char *zBufOut){
bcf40 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 ./* no-op */.}.s
bcf50 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 tatic void *os2D
bcf60 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
bcf70 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 *pVfs, void *pH
bcf80 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 andle, const cha
bcf90 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 r *zSymbol){. P
bcfa0 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 54 FN pfn;. APIRET
bcfb0 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 rc;. rc = DosQ
bcfc0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d ueryProcAddr((HM
bcfd0 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 ODULE)pHandle, 0
bcfe0 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e L, zSymbol, &pfn
bcff0 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e );. if( rc != N
bd000 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f O_ERROR ){. /
bd010 2a 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c 20 * if the symbol
bd020 69 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 66 itself was not f
bd030 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 61 ound, search aga
bd040 69 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a in for the same.
bd050 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69 * symbol wi
bd060 74 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 65 th an extra unde
bd070 72 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 67 rscore, that mig
bd080 68 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 70 ht be needed dep
bd090 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e ending. * on
bd0a0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e the calling con
bd0b0 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 vention */. c
bd0c0 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 har _zSymbol[256
bd0d0 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 72 ] = "_";. str
bd0e0 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a ncat(_zSymbol, z
bd0f0 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 Symbol, 255);.
bd100 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 rc = DosQueryP
bd110 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 rocAddr((HMODULE
bd120 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a )pHandle, 0L, _z
bd130 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 Symbol, &pfn);.
bd140 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 }. return rc !
bd150 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a = NO_ERROR ? 0 :
bd160 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 (void*)pfn;.}.s
bd170 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c tatic void os2Dl
bd180 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
bd190 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
bd1a0 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72 Handle){. DosFr
bd1b0 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c eeModule((HMODUL
bd1c0 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 E)pHandle);.}.#e
bd1d0 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 lse /* if SQLITE
bd1e0 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
bd1f0 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a SION is defined:
bd200 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 */. #define os
bd210 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65 66 2DlOpen 0. #def
bd220 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20 30 ine os2DlError 0
bd230 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c . #define os2Dl
bd240 53 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65 20 Sym 0. #define
bd250 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e os2DlClose 0.#en
bd260 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 dif.../*.** Writ
bd270 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 e up to nBuf byt
bd280 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 es of randomness
bd290 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 into zBuf..*/.s
bd2a0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61 6e tatic int os2Ran
bd2b0 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f domness(sqlite3_
bd2c0 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
bd2d0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 Buf, char *zBuf
bd2e0 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a ){. int n = 0;.
bd2f0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
bd300 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e TE_TEST). n = n
bd310 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 Buf;. memset(zB
bd320 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 uf, 0, nBuf);.#e
bd330 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a 65 6f 66 lse. int sizeof
bd340 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55 ULong = sizeof(U
bd350 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 28 69 6e LONG);. if( (in
bd360 74 29 73 69 7a 65 6f 66 28 44 41 54 45 54 49 4d t)sizeof(DATETIM
bd370 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 E) <= nBuf - n )
bd380 7b 0a 20 20 20 20 44 41 54 45 54 49 4d 45 20 78 {. DATETIME x
bd390 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 61 74 65 ;. DosGetDate
bd3a0 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 Time(&x);. me
bd3b0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
bd3c0 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 x, sizeof(x));.
bd3d0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 n += sizeof(x
bd3e0 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 );. }.. if( si
bd3f0 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 zeofULong <= nBu
bd400 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 50 49 f - n ){. PPI
bd410 42 20 70 70 69 62 3b 0a 20 20 20 20 44 6f 73 47 B ppib;. DosG
bd420 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 4e 55 4c etInfoBlocks(NUL
bd430 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 20 20 6d L, &ppib);. m
bd440 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 emcpy(&zBuf[n],
bd450 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 &ppib->pib_ulpid
bd460 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a , sizeofULong);.
bd470 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
bd480 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 Long;. }.. if(
bd490 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
bd4a0 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
bd4b0 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20 44 PTIB ptib;. D
bd4c0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
bd4d0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 &ptib, NULL);.
bd4e0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
bd4f0 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 5f 70 74 ], &ptib->tib_pt
bd500 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 2c ib2->tib2_ultid,
bd510 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
bd520 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c n += sizeofUL
bd530 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 ong;. }.. /* i
bd540 66 20 77 65 20 73 74 69 6c 6c 20 68 61 76 65 6e f we still haven
bd550 27 74 20 66 69 6c 6c 65 64 20 74 68 65 20 62 75 't filled the bu
bd560 66 66 65 72 20 79 65 74 20 74 68 65 20 66 6f 6c ffer yet the fol
bd570 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a 2f 0a 20 lowing will */.
bd580 20 2f 2a 20 67 72 61 62 20 65 76 65 72 79 74 68 /* grab everyth
bd590 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 65 61 64 ing once instead
bd5a0 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 76 65 72 of making sever
bd5b0 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 61 20 73 al calls for a s
bd5c0 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f 0a 20 20 ingle item */.
bd5d0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
bd5e0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
bd5f0 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 73 49 6e ULONG ulSysIn
bd600 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a 20 20 20 fo[QSV_MAX];.
bd610 20 44 6f 73 51 75 65 72 79 53 79 73 49 6e 66 6f DosQuerySysInfo
bd620 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c 20 75 6c (1L, QSV_MAX, ul
bd630 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 55 SysInfo, sizeofU
bd640 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 58 29 3b Long * QSV_MAX);
bd650 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 .. memcpy(&zB
bd660 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
bd670 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e 54 20 2d o[QSV_MS_COUNT -
bd680 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 1], sizeofULong
bd690 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
bd6a0 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 20 69 66 ofULong;.. if
bd6b0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d ( sizeofULong <=
bd6c0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
bd6d0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
bd6e0 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 n], &ulSysInfo[Q
bd6f0 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 52 56 41 SV_TIMER_INTERVA
bd700 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c L - 1], sizeofUL
bd710 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d ong);. n +=
bd720 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 sizeofULong;.
bd730 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 }. if( size
bd740 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 ofULong <= nBuf
bd750 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d - n ){. mem
bd760 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 cpy(&zBuf[n], &u
bd770 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d lSysInfo[QSV_TIM
bd780 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 69 7a 65 E_LOW - 1], size
bd790 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
bd7a0 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
bd7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
bd7c0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e sizeofULong <= n
bd7d0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 Buf - n ){.
bd7e0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
bd7f0 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 , &ulSysInfo[QSV
bd800 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 31 5d 2c _TIME_HIGH - 1],
bd810 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
bd820 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 n += sizeof
bd830 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 ULong;. }.
bd840 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
bd850 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
bd860 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
bd870 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
bd880 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 4c 4d 45 o[QSV_TOTAVAILME
bd890 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c M - 1], sizeofUL
bd8a0 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d ong);. n +=
bd8b0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 sizeofULong;.
bd8c0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
bd8d0 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f return n;.}../
bd8e0 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 *.** Sleep for a
bd8f0 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 little while.
bd900 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e Return the amoun
bd910 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e t of time slept.
bd920 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 .** The argument
bd930 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
bd940 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 f microseconds w
bd950 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e e want to sleep.
bd960 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 .** The return v
bd970 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 alue is the numb
bd980 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e er of microsecon
bd990 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 ds of sleep actu
bd9a0 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 ally.** requeste
bd9b0 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 d from the under
bd9c0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
bd9d0 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 system, a number
bd9e0 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 which.** might
bd9f0 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 be greater than
bda00 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
bda10 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f argument, but no
bda20 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 t less.** than t
bda30 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a he argument..*/.
bda40 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 6c static int os2Sl
bda50 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 73 eep( sqlite3_vfs
bda60 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 *pVfs, int micr
bda70 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c 65 osec ){. DosSle
bda80 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 30 ep( (microsec/10
bda90 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 00) );. return
bdaa0 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a microsec;.}../*.
bdab0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
bdac0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 variable, if se
bdad0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 t to a non-zero
bdae0 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 value, becomes t
bdaf0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 he result.** ret
bdb00 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 urned from sqlit
bdb10 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
bdb20 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 ). This is used
bdb30 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f for testing..*/
bdb40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
bdb50 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
bdb60 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 nt sqlite3_curre
bdb70 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e nt_time = 0;.#en
bdb80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 dif../*.** Find
bdb90 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 the current time
bdba0 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 (in Universal C
bdbb0 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 oordinated Time)
bdbc0 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 . Write the.**
bdbd0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 current time and
bdbe0 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 date as a Julia
bdbf0 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 n Day number int
bdc00 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 o *prNow and.**
bdc10 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 return 0. Retur
bdc20 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 n 1 if the time
bdc30 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 and date cannot
bdc40 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 be found..*/.int
bdc50 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 28 os2CurrentTime(
bdc60 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
bdc70 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f fs, double *prNo
bdc80 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e 6f w ){. double no
bdc90 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 74 w;. SHORT minut
bdca0 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 62 e; /* needs to b
bdcb0 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 77 e able to cope w
bdcc0 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 6d ith negative tim
bdcd0 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f 0a ezone offset */.
bdce0 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 2c USHORT second,
bdcf0 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 20 hour,.
bdd00 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72 day, month, year
bdd10 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 3b ;. DATETIME dt;
bdd20 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d . DosGetDateTim
bdd30 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 6f e( &dt );. seco
bdd40 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e nd = (USHORT)dt.
bdd50 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 seconds;. minut
bdd60 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d 69 e = (SHORT)dt.mi
bdd70 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a nutes + dt.timez
bdd80 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55 one;. hour = (U
bdd90 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a SHORT)dt.hours;.
bdda0 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29 day = (USHORT)
bddb0 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 dt.day;. month
bddc0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e = (USHORT)dt.mon
bddd0 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53 th;. year = (US
bdde0 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20 HORT)dt.year;..
bddf0 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73 /* Calculations
bde00 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77 from http://www
bde10 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e .astro.keele.ac.
bde20 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d uk/~rno/Astronom
bde30 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20 y/hjd.html.
bde40 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f http://www.astro
bde50 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e .keele.ac.uk/~rn
bde60 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d o/Astronomy/hjd-
bde70 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 0.1.c */. /* Ca
bde80 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69 lculate the Juli
bde90 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 an days */. now
bdea0 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b = day - 32076 +
bdeb0 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 20 . 1461*(year
bdec0 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20 + 4800 + (month
bded0 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20 - 14)/12)/4 +.
bdee0 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 367*(month - 2
bdef0 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f - (month - 14)/
bdf00 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20 12*12)/12 -.
bdf10 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 20 3*((year + 4900
bdf20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 + (month - 14)/1
bdf30 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 2)/100)/4;.. /*
bdf40 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69 6f Add the fractio
bdf50 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20 nal hours, mins
bdf60 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 and seconds */.
bdf70 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20 now += (hour +
bdf80 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 12.0)/24.0;. no
bdf90 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30 w += minute/1440
bdfa0 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63 .0;. now += sec
bdfb0 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a ond/86400.0;. *
bdfc0 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 prNow = now;.#if
bdfd0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
bdfe0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 if( sqlite3_cu
bdff0 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 rrent_time ){.
be000 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 *prNow = sqlit
be010 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f e3_current_time/
be020 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 86400.0 + 244058
be030 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7.5;. }.#endif.
be040 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 return 0;.}..s
be050 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 74 tatic int os2Get
be060 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 LastError(sqlite
be070 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
be080 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
be090 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a f){. return 0;.
be0a0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
be0b0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
be0c0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
be0d0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
be0e0 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
be0f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
be100 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 s_init(void){.
be110 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
be120 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 20 fs os2Vfs = {.
be130 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
be140 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
be150 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 6f */. sizeof(o
be160 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a s2File), /* sz
be170 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 43 OsFile */. CC
be180 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 20 HMAXPATH,
be190 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
be1a0 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
be1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 /* pNex
be1c0 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c 20 t */. "os2",
be1d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
be1e0 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Name */. 0,
be1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be200 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a 20 * pAppData */..
be210 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 20 os2Open,
be220 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a /* xOpen *
be230 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 2c /. os2Delete,
be240 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c /* xDel
be250 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 63 ete */. os2Ac
be260 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a cess, /*
be270 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 xAccess */.
be280 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c os2FullPathname,
be290 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
be2a0 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c ame */. os2Dl
be2b0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a Open, /*
be2c0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 xDlOpen */.
be2d0 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 os2DlError,
be2e0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a /* xDlError *
be2f0 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c 20 /. os2DlSym,
be300 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 /* xDlS
be310 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 43 ym */. os2DlC
be320 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 lose, /*
be330 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 xDlClose */.
be340 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 os2Randomness,
be350 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 /* xRandomnes
be360 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 65 s */. os2Slee
be370 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 p, /* x
be380 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 32 Sleep */. os2
be390 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 CurrentTime,
be3a0 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 /* xCurrentTime
be3b0 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 73 */. os2GetLas
be3c0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 tError /* xGe
be3d0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 tLastError */.
be3e0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 };. sqlite3_vfs
be3f0 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 66 _register(&os2Vf
be400 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 6f s, 1);. initUco
be410 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 nvObjects();. r
be420 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
be430 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
be440 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 t sqlite3_os_end
be450 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 63 (void){. freeUc
be460 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 onvObjects();.
be470 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
be480 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
be490 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a QLITE_OS_OS2 */.
be4a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
be4b0 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 End of os_os2.c
be4c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
be4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
be4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
be500 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 75 Begin file os_u
be510 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nix.c **********
be520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
be540 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
be550 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
be560 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
be570 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
be580 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
be590 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
be5a0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
be5b0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
be5c0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
be5d0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
be5e0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
be5f0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
be600 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
be610 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
be620 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
be630 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
be640 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
be650 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
be660 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
be670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be6b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
be6c0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
be6d0 74 68 65 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e the VFS implemen
be6e0 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78 2d tation for unix-
be6f0 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 20 73 like operating s
be700 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 6c 75 64 ystems.** includ
be710 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58 2c e Linux, MacOSX,
be720 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57 6f *BSD, QNX, VxWo
be730 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c 20 rks, AIX, HPUX,
be740 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a 2a 0a 2a and others..**.*
be750 2a 20 54 68 65 72 65 20 61 72 65 20 61 63 74 75 * There are actu
be760 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69 66 ally several dif
be770 66 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c 65 ferent VFS imple
be780 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74 68 mentations in th
be790 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 is file..** The
be7a0 64 69 66 66 65 72 65 6e 63 65 73 20 61 72 65 20 differences are
be7b0 69 6e 20 74 68 65 20 77 61 79 20 74 68 61 74 20 in the way that
be7c0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 file locking is
be7d0 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 66 61 75 done. The defau
be7e0 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 lt.** implementa
be7f0 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 69 78 20 tion uses Posix
be800 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 2e 20 Advisory Locks.
be810 20 41 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70 Alternative imp
be820 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
be830 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 64 6f 74 use flock(), dot
be840 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f 75 73 20 -files, various
be850 70 72 6f 70 72 69 65 74 61 72 79 20 6c 6f 63 6b proprietary lock
be860 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 6f 72 20 ing schemas, or
be870 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 70 20 6c simply.** skip l
be880 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f 67 65 74 ocking all toget
be890 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 her..**.** This
be8a0 73 6f 75 72 63 65 20 66 69 6c 65 20 69 73 20 6f source file is o
be8b0 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f 20 64 69 rganized into di
be8c0 76 69 73 69 6f 6e 73 20 77 68 65 72 65 20 74 68 visions where th
be8d0 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 61 72 69 e logic for vari
be8e0 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e 63 74 69 ous.** subfuncti
be8f0 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 ons is contained
be900 20 77 69 74 68 69 6e 20 74 68 65 20 61 70 70 72 within the appr
be910 6f 70 72 69 61 74 65 20 64 69 76 69 73 69 6f 6e opriate division
be920 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 4b 45 45 . PLEASE.** KEE
be930 50 20 54 48 45 20 53 54 52 55 43 54 55 52 45 20 P THE STRUCTURE
be940 4f 46 20 54 48 49 53 20 46 49 4c 45 20 49 4e 54 OF THIS FILE INT
be950 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 65 20 73 ACT. New code s
be960 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 65 64 0a hould be placed.
be970 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 ** in the correc
be980 74 20 64 69 76 69 73 69 6f 6e 20 61 6e 64 20 73 t division and s
be990 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 72 6c 79 hould be clearly
be9a0 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 labeled..**.**
be9b0 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 20 64 69 The layout of di
be9c0 76 69 73 69 6f 6e 73 20 69 73 20 61 73 20 66 6f visions is as fo
be9d0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a llows:.**.** *
be9e0 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 General-purpos
be9f0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 e declarations a
bea00 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 nd utility funct
bea10 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 55 6e ions..** * Un
bea20 69 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f 67 ique file ID log
bea30 69 63 20 75 73 65 64 20 62 79 20 56 78 57 6f 72 ic used by VxWor
bea40 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 61 72 69 ks..** * Vari
bea50 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d ous locking prim
bea60 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 itive implementa
bea70 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65 70 tions (all excep
bea80 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 29 t proxy locking)
bea90 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 :.** + for
beaa0 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c Posix Advisory L
beab0 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 ocks.** + f
beac0 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73 0a 2a or no-op locks.*
bead0 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 64 6f 74 * + for dot
beae0 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 20 -file locks.**
beaf0 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f 63 6b 28 + for flock(
beb00 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 ) locking.**
beb10 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 20 73 65 + for named se
beb20 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 28 56 maphore locks (V
beb30 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a 2a 2a 20 xWorks only).**
beb40 20 20 20 20 20 2b 20 66 6f 72 20 41 46 50 20 66 + for AFP f
beb50 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73 20 ilesystem locks
beb60 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a (MacOSX only).**
beb70 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 * sqlite3_fi
beb80 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f 74 20 61 le methods not a
beb90 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6c ssociated with l
beba0 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 2a 20 20 ocking..** *
bebb0 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 Definitions of s
bebc0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
bebd0 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c s objects for al
bebe0 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 l locking.**
bebf0 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 73 20 22 methods plus "
bec00 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e finder" function
bec10 73 20 66 6f 72 20 65 61 63 68 20 6c 6f 63 6b 69 s for each locki
bec20 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 20 20 ng method..**
bec30 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d * sqlite3_vfs m
bec40 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 6e 74 61 ethod implementa
bec50 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 4c tions..** * L
bec60 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 ocking primitive
bec70 73 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20 s for the proxy
bec80 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 uber-locking-met
bec90 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 6f 6e 6c hod. (MacOSX onl
beca0 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e y).** * Defin
becb0 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 itions of sqlite
becc0 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 66 6f 3_vfs objects fo
becd0 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65 r all locking me
bece0 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 20 70 6c thods.** pl
becf0 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f us implementatio
bed00 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 ns of sqlite3_os
bed10 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 _init() and sqli
bed20 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a te3_os_end()..**
bed30 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 75 6e 69 78 .** $Id: os_unix
bed40 2e 63 2c 76 20 31 2e 32 35 30 20 32 30 30 39 2f .c,v 1.250 2009/
bed50 30 34 2f 30 37 20 30 35 3a 33 35 3a 30 34 20 63 04/07 05:35:04 c
bed60 68 77 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 hw Exp $.*/.#if
bed70 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 SQLITE_OS_UNIX
bed80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
bed90 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 his file is used
beda0 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f on unix only */
bedb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 ../*.** There ar
bedc0 65 20 76 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 e various method
bedd0 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 s for file locki
bede0 6e 67 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 ng used for conc
bedf0 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 urrency.** contr
bee00 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 ol:.**.** 1. P
bee10 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 OSIX locking (th
bee20 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 e default),.**
bee30 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2. No locking,.
bee40 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 ** 3. Dot-file
bee50 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 locking,.** 4
bee60 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e . flock() lockin
bee70 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c g,.** 5. AFP l
bee80 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 ocking (OSX only
bee90 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 ),.** 6. Named
beea0 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 POSIX semaphore
beeb0 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 s (VXWorks only)
beec0 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 ,.** 7. proxy
beed0 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e locking. (OSX on
beee0 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 ly).**.** Styles
beef0 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 61 72 65 4, 5, and 7 are
bef00 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
bef10 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 of SQLITE_ENABLE
bef20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a _LOCKING_STYLE.*
bef30 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 * is defined to
bef40 31 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 45 1. The SQLITE_E
bef50 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
bef60 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 YLE also enables
bef70 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 automatic.** se
bef80 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 lection of the a
bef90 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 ppropriate locki
befa0 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f ng style based o
befb0 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d n the filesystem
befc0 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 64 61 .** where the da
befd0 74 61 62 61 73 65 20 69 73 20 6c 6f 63 61 74 65 tabase is locate
befe0 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 d. .*/.#if !def
beff0 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
bf000 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
bf010 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 ).# if defined(
bf020 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 __APPLE__).#
bf030 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e define SQLITE_EN
bf040 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
bf050 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 LE 1.# else.#
bf060 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
bf070 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
bf080 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a TYLE 0.# endif.
bf090 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 #endif../*.** De
bf0a0 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 58 57 4f fine the OS_VXWO
bf0b0 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 73 6f RKS pre-processo
bf0c0 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 66 20 r macro to 1 if
bf0d0 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 building on .**
bf0e0 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 vxworks, or 0 ot
bf0f0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e herwise..*/.#ifn
bf100 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 def OS_VXWORKS.#
bf110 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 if defined(__R
bf120 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 TP__) || defined
bf130 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 (_WRS_KERNEL).#
bf140 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 define OS_VXW
bf150 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 ORKS 1.# else.#
bf160 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 define OS_VX
bf170 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 WORKS 0.# endif
bf180 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
bf190 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 hese #defines sh
bf1a0 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 ould enable >2GB
bf1b0 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e file support on
bf1c0 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a Posix if the.**
bf1d0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
bf1e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 ating system sup
bf1f0 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 ports it. If th
bf200 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 e OS lacks.** la
bf210 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
bf220 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 , these should b
bf230 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
bf240 4c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f Large file suppo
bf250 72 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c rt can be disabl
bf260 65 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 ed using the -DS
bf270 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 QLITE_DISABLE_LF
bf280 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 S switch.** on t
bf290 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d he compiler comm
bf2a0 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 and line. This
bf2b0 69 73 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 is necessary if
bf2c0 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e you are compilin
bf2d0 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 g.** on a recent
bf2e0 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 machine (ex: Re
bf2f0 64 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f dHat 7.2) but yo
bf300 75 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 u want your code
bf310 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 to work.** on a
bf320 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 n older machine
bf330 28 65 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 (ex: RedHat 6.0)
bf340 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
bf350 65 20 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a e on RedHat 7.2.
bf360 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 ** without this
bf370 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 option, LFS is e
bf380 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 nable. But LFS
bf390 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 does not exist i
bf3a0 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 n the kernel.**
bf3b0 69 6e 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 in RedHat 6.0, s
bf3c0 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 o the code won't
bf3d0 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 work. Hence, f
bf3e0 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 or maximum binar
bf3f0 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 y.** portability
bf400 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 you should omit
bf410 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 LFS..**.** The
bf420 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61 previous paragra
bf430 70 68 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 ph was written i
bf440 6e 20 32 30 30 35 2e 20 20 28 54 68 69 73 20 70 n 2005. (This p
bf450 61 72 61 67 72 61 70 68 20 69 73 20 77 72 69 74 aragraph is writ
bf460 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 ten.** on 2008-1
bf470 31 2d 32 38 2e 29 20 54 68 65 73 65 20 64 61 79 1-28.) These day
bf480 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 s, all Linux ker
bf490 6e 65 6c 73 20 73 75 70 70 6f 72 74 20 6c 61 72 nels support lar
bf4a0 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 ge files, so.**
bf4b0 79 6f 75 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 you should proba
bf4c0 62 6c 79 20 6c 65 61 76 65 20 4c 46 53 20 65 6e bly leave LFS en
bf4d0 61 62 6c 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 abled. But some
bf4e0 20 65 6d 62 65 64 64 65 64 20 70 6c 61 74 66 6f embedded platfo
bf4f0 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 rms might.** lac
bf500 6b 20 4c 46 53 20 69 6e 20 77 68 69 63 68 20 63 k LFS in which c
bf510 61 73 65 20 74 68 65 20 53 51 4c 49 54 45 5f 44 ase the SQLITE_D
bf520 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f ISABLE_LFS macro
bf530 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 62 65 20 might still be
bf540 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 useful..*/.#ifnd
bf550 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
bf560 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 20 5f E_LFS.# define _
bf570 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 20 20 LARGE_FILE
bf580 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 1.# ifndef _FIL
bf590 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a 23 20 E_OFFSET_BITS.#
bf5a0 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f define _FILE_O
bf5b0 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a 23 20 FFSET_BITS 64.#
bf5c0 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 5f endif.# define _
bf5d0 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 LARGEFILE_SOURCE
bf5e0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
bf5f0 20 73 74 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 standard includ
bf600 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 e files..*/.#inc
bf610 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e lude <sys/types.
bf620 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
bf630 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 /stat.h>.#includ
bf640 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 e <fcntl.h>.#inc
bf650 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a lude <unistd.h>.
bf660 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 #include <sys/ti
bf670 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c me.h>.#include <
bf680 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 errno.h>..#if SQ
bf690 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
bf6a0 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c ING_STYLE.# incl
bf6b0 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 ude <sys/ioctl.h
bf6c0 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b >.# if OS_VXWORK
bf6d0 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 S.# include <se
bf6e0 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e maphore.h>.# in
bf6f0 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e clude <limits.h>
bf700 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 .# else.# inclu
bf710 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a de <sys/file.h>.
bf720 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f # include <sys/
bf730 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c param.h>.# incl
bf740 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 ude <sys/mount.h
bf750 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 >.# endif.#endif
bf760 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
bf770 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
bf780 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 */../*.** If we
bf790 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 are to be thread
bf7a0 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 -safe, include t
bf7b0 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64 he pthreads head
bf7c0 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a er and define.**
bf7d0 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 the SQLITE_UNIX
bf7e0 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a _THREADS macro..
bf7f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
bf800 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
bf810 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
bf820 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
bf830 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 /*.** Default pe
bf840 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 rmissions when c
bf850 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 reating a new fi
bf860 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 le.*/.#ifndef SQ
bf870 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
bf880 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 E_PERMISSIONS.#
bf890 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
bf8a0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
bf8b0 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 SSIONS 0644.#end
bf8c0 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 if../*. ** Defau
bf8d0 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 lt permissions w
bf8e0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74 hen creating aut
bf8f0 6f 20 70 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a o proxy dir. */.
bf900 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
bf910 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
bf920 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 PERMISSIONS.# de
bf930 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
bf940 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 ULT_PROXYDIR_PER
bf950 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 MISSIONS 0755.#e
bf960 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 ndif../*.** Maxi
bf970 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 mum supported pa
bf980 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 th-length..*/.#d
bf990 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 efine MAX_PATHNA
bf9a0 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e ME 512../*.** On
bf9b0 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74 45 ly set the lastE
bf9c0 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f rrno if the erro
bf9d0 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c r code is a real
bf9e0 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a error and not .
bf9f0 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 ** a normal expe
bfa00 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65 cted return code
bfa10 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 of SQLITE_BUSY
bfa20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a or SQLITE_OK.*/.
bfa30 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f #define IS_LOCK_
bfa40 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d ERROR(x) ((x !=
bfa50 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 SQLITE_OK) && (
bfa60 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 x != SQLITE_BUSY
bfa70 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 )).../*.** The u
bfa80 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 nixFile structur
bfa90 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 e is subclass of
bfaa0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 sqlite3_file sp
bfab0 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e ecific to the un
bfac0 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d ix.** VFS implem
bfad0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 entations..*/.ty
bfae0 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 pedef struct uni
bfaf0 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a xFile unixFile;.
bfb00 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 struct unixFile
bfb10 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d {. sqlite3_io_m
bfb20 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d ethods const *pM
bfb30 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 ethod; /* Alway
bfb40 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 s the first entr
bfb50 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e y */. struct un
bfb60 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
bfb70 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 ; /* Info
bfb80 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 about all open f
bfb90 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 d's on this inod
bfba0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e e */. struct un
bfbb0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 ixLockInfo *pLoc
bfbc0 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 k; /* Info
bfbd0 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 about locks on t
bfbe0 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 his inode */. i
bfbf0 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 nt h;
bfc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfc10 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
bfc20 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
bfc30 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 dirfd;
bfc40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bfc50 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 File descriptor
bfc60 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 for the director
bfc70 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 y */. unsigned
bfc80 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 char locktype;
bfc90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
bfca0 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 ype of lock held
bfcb0 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 on this fd */.
bfcc0 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 int lastErrno;
bfcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfce0 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72 /* The unix er
bfcf0 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 rno from the las
bfd00 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 t I/O error */.
bfd10 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f void *lockingCo
bfd20 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 ntext;
bfd30 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 /* Locking sty
bfd40 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 74 le specific stat
bfd50 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f e */.#if SQLITE_
bfd60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
bfd70 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 TYLE. int openF
bfd80 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
bfd90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
bfda0 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 61 lags specified a
bfdb0 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 t open() */.#end
bfdc0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 if.#if SQLITE_TH
bfdd0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
bfde0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 ned(__linux__).
bfdf0 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 pthread_t tid;
bfe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfe10 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20 /* The thread
bfe20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73 that "owns" this
bfe30 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e unixFile */.#en
bfe40 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 dif.#if OS_VXWOR
bfe50 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 KS. int isDelet
bfe60 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
bfe70 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 /* Delete
bfe80 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 on close if true
bfe90 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 */. struct vxw
bfea0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b orksFileId *pId;
bfeb0 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 /* Unique
bfec0 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 file ID */.#end
bfed0 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 if.#ifndef NDEBU
bfee0 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 G. /* The next
bfef0 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c group of variabl
bff00 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 74 es are used to t
bff10 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20 rack whether or
bff20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 not the. ** tra
bff30 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
bff40 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20 in bytes 24-27
bff50 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 of database file
bff60 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 20 s are updated.
bff70 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20 ** whenever any
bff80 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
bff90 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 base changes. A
bffa0 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
bffb0 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 t will. ** occu
bffc0 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 75 r if a file is u
bffd0 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 pdated without a
bffe0 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65 lso updating the
bfff0 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a transaction. *
c0000 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 * counter. This
c0010 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f test is made to
c0020 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c avoid new probl
c0030 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 ems similar to t
c0040 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 he. ** one desc
c0050 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 20 ribed by ticket
c0060 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 #3584. . */. u
c0070 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 nsigned char tra
c0080 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a nsCntrChng; /*
c0090 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 61 True if the tra
c00a0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
c00b0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e changed */. un
c00c0 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 signed char dbUp
c00d0 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 date; /*
c00e0 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 True if any part
c00f0 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
c0100 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 e changed */. u
c0110 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e nsigned char inN
c0120 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a ormalWrite; /*
c0130 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f True if in a no
c0140 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61 rmal write opera
c0150 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 tion */.. /* If
c0160 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e true, that mean
c0170 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 s we are dealing
c0180 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65 with a database
c0190 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 0a 20 file that has.
c01a0 20 2a 2a 20 61 20 72 61 6e 67 65 20 6f 66 20 6c ** a range of l
c01b0 6f 63 6b 69 6e 67 20 62 79 74 65 73 20 66 72 6f ocking bytes fro
c01c0 6d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 74 m PENDING_BYTE t
c01d0 68 72 6f 75 67 68 20 50 45 4e 44 49 4e 47 5f 42 hrough PENDING_B
c01e0 59 54 45 2b 35 31 31 0a 20 20 2a 2a 20 77 68 69 YTE+511. ** whi
c01f0 63 68 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 ch should never
c0200 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
c0210 65 6e 2e 20 20 41 73 73 65 72 74 73 28 29 20 77 en. Asserts() w
c0220 69 6c 6c 20 76 65 72 69 66 79 20 74 68 69 73 20 ill verify this
c0230 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
c0240 61 72 20 69 73 4c 6f 63 6b 61 62 6c 65 3b 20 20 ar isLockable;
c0250 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 /* True if f
c0260 69 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 6f 63 ile might be loc
c0270 6b 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ked */.#endif.#i
c0280 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c0290 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f . /* In test mo
c02a0 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 de, increase the
c02b0 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 74 size of this st
c02c0 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73 6f ructure a bit so
c02d0 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 69 that . ** it i
c02e0 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
c02f0 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46 69 e struct CrashFi
c0300 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 65 le defined in te
c0310 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 st6.c.. */. ch
c0320 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b ar aPadding[32];
c0330 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
c0340 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 * Include code t
c0350 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
c0360 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 all os_*.c file
c0370 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.*/./**********
c0380 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f **** Include os_
c0390 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 common.h in the
c03a0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 middle of os_uni
c03b0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
c03c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c03d0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
c03e0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c03f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0410 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
c0420 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 May 22.**.** The
c0430 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
c0440 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
c0450 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
c0460 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
c0470 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
c0480 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
c0490 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
c04a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
c04b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
c04c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
c04d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
c04e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
c04f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
c0500 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
c0510 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
c0520 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
c0530 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
c0540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
c0590 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
c05a0 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 ins macros and a
c05b0 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 little bit of c
c05c0 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
c05d0 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 on to.** all of
c05e0 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 the platform-spe
c05f0 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f cific files (os_
c0600 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 *.c) and is #inc
c0610 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 luded into those
c0620 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a .** files..**.**
c0630 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c This file shoul
c0640 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 d be #included b
c0650 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c y the os_*.c fil
c0660 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 es only. It is
c0670 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c not a.** general
c0680 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 purpose header
c0690 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a file..**.** $Id:
c06a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 os_common.h,v 1
c06b0 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31 .38 2009/02/24 1
c06c0 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 8:40:50 danielk1
c06d0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 977 Exp $.*/.#if
c06e0 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ndef _OS_COMMON_
c06f0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 H_.#define _OS_C
c0700 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 OMMON_H_../*.**
c0710 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 At least two bug
c0720 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 s have slipped i
c0730 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 n because we cha
c0740 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f nged the MEMORY_
c0750 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 DEBUG.** macro t
c0760 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 o SQLITE_DEBUG a
c0770 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 nd some older ma
c0780 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 kefiles have not
c0790 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a yet made the.**
c07a0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f switch. The fo
c07b0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f llowing code sho
c07c0 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 uld catch this p
c07d0 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c roblem at compil
c07e0 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 e-time..*/.#ifde
c07f0 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 f MEMORY_DEBUG.#
c0800 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f error "The MEMO
c0810 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 RY_DEBUG macro i
c0820 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 s obsolete. Use
c0830 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e SQLITE_DEBUG in
c0840 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a stead.".#endif..
c0850 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
c0860 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
c0870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 TE int sqlite3OS
c0880 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 Trace = 0;.#defi
c0890 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 ne OSTRACE1(X)
c08a0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
c08b0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c08c0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c08d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c08e0 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 E2(X,Y) if
c08f0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
c0900 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
c0910 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 rintf(X,Y).#defi
c0920 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
c0930 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 Z) if( sqlit
c0940 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c0950 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c0960 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Y,Z).#define OS
c0970 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 TRACE4(X,Y,Z,A)
c0980 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
c0990 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
c09a0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
c09b0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
c09c0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 CE5(X,Y,Z,A,B) i
c09d0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c09e0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c09f0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
c0a00 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c0a10 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 E6(X,Y,Z,A,B,C)
c0a20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
c0a30 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
c0a40 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
c0a50 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
c0a60 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
c0a70 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 A,B,C,D) \. i
c0a80 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
c0a90 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c0aa0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
c0ab0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ,D).#else.#defin
c0ac0 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 e OSTRACE1(X).#d
c0ad0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
c0ae0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
c0af0 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 ACE3(X,Y,Z).#def
c0b00 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 ine OSTRACE4(X,Y
c0b10 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Z,A).#define OS
c0b20 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE5(X,Y,Z,A,B
c0b30 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c0b40 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a E6(X,Y,Z,A,B,C).
c0b50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
c0b60 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
c0b70 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
c0b80 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d cros for perform
c0b90 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e ance tracing. N
c0ba0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f ormally turned o
c0bb0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a ff. Only works.
c0bc0 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 ** on i486 hardw
c0bd0 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 are..*/.#ifdef S
c0be0 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 QLITE_PERFORMANC
c0bf0 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 E_TRACE../* .**
c0c00 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e hwtime.h contain
c0c10 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c s inline assembl
c0c20 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c er code for impl
c0c30 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 ementing .** hig
c0c40 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 h-performance ti
c0c50 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a ming routines..*
c0c60 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c0c70 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 * Include hwtime
c0c80 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
c0c90 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
c0ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0cb0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c0cc0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 * Begin file hwt
c0cd0 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
c0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0d00 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 /./*.** 2008 May
c0d10 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 27.**.** The au
c0d20 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c0d30 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c0d40 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c0d50 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c0d60 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c0d70 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c0d80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c0d90 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c0da0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c0db0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c0dc0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c0dd0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c0de0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c0df0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c0e00 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c0e10 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c0e20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c0e80 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c0e90 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 inline asm code
c0ea0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 for retrieving
c0eb0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 "high-performanc
c0ec0 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 e".** counters f
c0ed0 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 or x86 class CPU
c0ee0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 s..**.** $Id: hw
c0ef0 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 time.h,v 1.3 200
c0f00 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 8/08/01 14:33:15
c0f10 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
c0f20 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
c0f30 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
c0f40 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
c0f50 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
c0f60 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
c0f70 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
c0f80 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
c0f90 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
c0fa0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
c0fb0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
c0fc0 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
c0fd0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
c0fe0 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
c0ff0 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
c1000 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
c1010 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
c1020 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
c1030 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
c1040 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
c1050 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
c1060 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
c1070 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
c1080 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
c1090 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
c10a0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
c10b0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
c10c0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
c10d0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
c10e0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
c10f0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
c1100 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
c1110 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
c1120 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
c1130 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
c1140 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
c1150 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
c1160 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
c1170 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
c1180 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
c1190 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
c11a0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
c11b0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
c11c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
c11d0 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
c11e0 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
c11f0 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
c1200 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
c1210 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
c1220 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
c1230 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
c1240 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
c1250 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
c1260 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
c1270 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
c1280 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c1290 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
c12a0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
c12b0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
c12c0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
c12d0 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
c12e0 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
c12f0 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
c1300 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
c1310 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
c1320 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
c1330 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
c1340 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
c1350 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
c1360 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
c1370 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
c1380 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
c1390 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
c13a0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
c13b0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
c13c0 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
c13d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c13e0 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
c13f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c1400 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
c1410 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c1420 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
c1430 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
c1440 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
c1450 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
c1460 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
c1470 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
c1480 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
c1490 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
c14a0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
c14b0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
c14c0 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
c14d0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
c14e0 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
c14f0 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
c1500 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
c1510 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
c1520 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
c1530 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
c1540 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
c1550 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
c1560 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
c1570 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
c1580 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
c1590 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
c15a0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
c15b0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
c15c0 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
c15d0 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
c15e0 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
c15f0 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
c1600 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
c1610 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
c1620 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
c1630 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
c1640 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
c1650 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
c1660 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
c1670 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
c1680 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
c1690 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
c16a0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
c16b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c16c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c16d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
c16e0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
c16f0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
c1700 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f off in os_commo
c1710 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
c1720 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 ******/..static
c1730 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
c1740 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 start;.static sq
c1750 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c lite_uint64 g_el
c1760 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 apsed;.#define T
c1770 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 IMER_START
c1780 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 g_start=sqlite3
c1790 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 Hwtime().#define
c17a0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 TIMER_END
c17b0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c g_elapsed=sql
c17c0 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 ite3Hwtime()-g_s
c17d0 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d tart.#define TIM
c17e0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 ER_ELAPSED g
c17f0 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 _elapsed.#else.#
c1800 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
c1810 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 RT.#define TIMER
c1820 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d _END.#define TIM
c1830 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 ER_ELAPSED (
c1840 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
c1850 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
c1860 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 If we compile wi
c1870 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 th the SQLITE_TE
c1880 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 ST macro set, th
c1890 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 en the following
c18a0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 block.** of cod
c18b0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 e will give us t
c18c0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 he ability to si
c18d0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f mulate a disk I/
c18e0 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a O error. This.*
c18f0 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 * is used for te
c1900 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 sting the I/O re
c1910 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f covery logic..*/
c1920 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c1930 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
c1940 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c1950 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 ror_hit = 0;
c1960 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
c1970 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 number of I/O E
c1980 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
c1990 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c19a0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
c19b0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
c19c0 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 Number of non-be
c19d0 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 nign errors */.S
c19e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
c19f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
c1a00 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 ending = 0;
c1a10 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e /* Count down
c1a20 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 to first I/O er
c1a30 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ror */.SQLITE_AP
c1a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
c1a50 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d _error_persist =
c1a60 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
c1a70 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 ue if I/O errors
c1a80 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 persist */.SQLI
c1a90 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c1aa0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
c1ab0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gn = 0;
c1ac0 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 /* True if error
c1ad0 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a s are benign */.
c1ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c1af0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c1b00 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c pending = 0;.SQL
c1b10 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c1b20 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 te3_diskfull = 0
c1b30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ;.#define Simula
c1b40 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
c1b50 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 X) sqlite3_io_er
c1b60 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 ror_benign=(X).#
c1b70 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
c1b80 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a OError(CODE) \.
c1b90 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 if( (sqlite3_i
c1ba0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
c1bb0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 && sqlite3_io_er
c1bc0 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 ror_hit) \.
c1bd0 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f || sqlite3_io_
c1be0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 error_pending--
c1bf0 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 == 1 ) \.
c1c00 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f { local_
c1c10 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d ioerr(); CODE; }
c1c20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
c1c30 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f al_ioerr(){. IO
c1c40 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 TRACE(("IOERR\n"
c1c50 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f ));. sqlite3_io
c1c60 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 _error_hit++;.
c1c70 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f if( !sqlite3_io_
c1c80 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 error_benign ) s
c1c90 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c1ca0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 hardhit++;.}.#de
c1cb0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
c1cc0 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 kfullError(CODE)
c1cd0 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 \. if( sqlite
c1ce0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
c1cf0 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 ng ){ \. if(
c1d00 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c1d10 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 l_pending == 1 )
c1d20 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c { \. local
c1d30 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 _ioerr(); \.
c1d40 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
c1d50 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 ull = 1; \.
c1d60 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
c1d70 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 or_hit = 1; \.
c1d80 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 CODE; \.
c1d90 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 }else{ \.
c1da0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
c1db0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a ll_pending--; \.
c1dc0 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 } \. }.#e
c1dd0 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 lse.#define Simu
c1de0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
c1df0 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d n(X).#define Sim
c1e00 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a ulateIOError(A).
c1e10 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c1e20 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 DiskfullError(A)
c1e30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
c1e40 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 hen testing, kee
c1e50 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 p a count of the
c1e60 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 number of open
c1e70 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 files..*/.#ifdef
c1e80 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
c1e90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c1ea0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
c1eb0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 unt = 0;.#define
c1ec0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 OpenCounter(X)
c1ed0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
c1ee0 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 le_count+=(X).#e
c1ef0 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e lse.#define Open
c1f00 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 Counter(X).#endi
c1f10 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
c1f20 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e fined(_OS_COMMON
c1f30 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
c1f40 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
c1f50 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c1f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1f80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
c1f90 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
c1fa0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
c1fb0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 t off in os_unix
c1fc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
c1fd0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
c1fe0 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d Define various m
c1ff0 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d acros that are m
c2000 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 issing from some
c2010 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 systems..*/.#if
c2020 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 ndef O_LARGEFILE
c2030 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 .# define O_LARG
c2040 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 EFILE 0.#endif.#
c2050 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 ifdef SQLITE_DIS
c2060 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 ABLE_LFS.# undef
c2070 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 O_LARGEFILE.# d
c2080 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c efine O_LARGEFIL
c2090 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 E 0.#endif.#ifnd
c20a0 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 ef O_NOFOLLOW.#
c20b0 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f define O_NOFOLLO
c20c0 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 W 0.#endif.#ifnd
c20d0 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 ef O_BINARY.# de
c20e0 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a fine O_BINARY 0.
c20f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
c2100 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 e DJGPP compiler
c2110 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f environment loo
c2120 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 ks mostly like U
c2130 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c nix, but it.** l
c2140 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 acks the fcntl()
c2150 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 system call. S
c2160 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c o redefine fcntl
c2170 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 () to be somethi
c2180 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 ng.** that alway
c2190 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 s succeeds. Thi
c21a0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 s means that loc
c21b0 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 king does not oc
c21c0 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 cur under.** DJG
c21d0 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 PP. But it is D
c21e0 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f OS - what did yo
c21f0 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 u expect?.*/.#if
c2200 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 def __DJGPP__.#
c2210 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 define fcntl(A,B
c2220 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a ,C) 0.#endif../*
c2230 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 .** The threadid
c2240 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 macro resolves
c2250 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 to the thread-id
c2260 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 or to 0. Used
c2270 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 for.** testing a
c2280 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c nd debugging onl
c2290 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 y..*/.#if SQLITE
c22a0 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 _THREADSAFE.#def
c22b0 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68 ine threadid pth
c22c0 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 read_self().#els
c22d0 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 e.#define thread
c22e0 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a id 0.#endif.../*
c22f0 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
c2300 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 ions to obtain a
c2310 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 nd relinquish th
c2320 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 0a e global mutex..
c2330 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
c2340 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f nixEnterMutex(vo
c2350 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d id){. sqlite3_m
c2360 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
c2370 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
c2380 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
c2390 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 _MASTER));.}.sta
c23a0 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 tic void unixLea
c23b0 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 veMutex(void){.
c23c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
c23d0 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 eave(sqlite3Mute
c23e0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
c23f0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
c2400 52 29 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 R));.}...#ifdef
c2410 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
c2420 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 ** Helper functi
c2430 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 on for printing
c2440 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f 72 6d out trace inform
c2450 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67 ation from debug
c2460 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73 ging.** binaries
c2470 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 20 74 . This returns t
c2480 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 he string repres
c2490 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 etation of the s
c24a0 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67 upplied.** integ
c24b0 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f er lock-type..*/
c24c0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
c24d0 61 72 20 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 ar *locktypeName
c24e0 28 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a (int locktype){.
c24f0 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 switch( lockty
c2500 70 65 20 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f pe ){. case NO_
c2510 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f LOCK: return "NO
c2520 4e 45 22 3b 0a 20 20 63 61 73 65 20 53 48 41 52 NE";. case SHAR
c2530 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 ED_LOCK: return
c2540 22 53 48 41 52 45 44 22 3b 0a 20 20 63 61 73 65 "SHARED";. case
c2550 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 RESERVED_LOCK:
c2560 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 return "RESERVED
c2570 22 3b 0a 20 20 63 61 73 65 20 50 45 4e 44 49 4e ";. case PENDIN
c2580 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 G_LOCK: return "
c2590 50 45 4e 44 49 4e 47 22 3b 0a 20 20 63 61 73 65 PENDING";. case
c25a0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a EXCLUSIVE_LOCK:
c25b0 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49 return "EXCLUSI
c25c0 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 VE";. }. retur
c25d0 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e n "ERROR";.}.#en
c25e0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
c25f0 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a TE_LOCK_TRACE./*
c2600 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e .** Print out in
c2610 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
c2620 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 all locking oper
c2630 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ations..**.** Th
c2640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
c2650 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 ed for troublesh
c2660 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 ooting locks on
c2670 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a multithreaded.**
c2680 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 platforms. Ena
c2690 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 ble by compiling
c26a0 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49 with the -DSQLI
c26b0 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a TE_LOCK_TRACE.**
c26c0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 command-line op
c26d0 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 tion on the comp
c26e0 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65 iler. This code
c26f0 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 is normally.**
c2700 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 turned off..*/.s
c2710 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 tatic int lockTr
c2720 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 ace(int fd, int
c2730 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b op, struct flock
c2740 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f *p){. char *zO
c2750 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 pName, *zType;.
c2760 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 int s;. int sa
c2770 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 vedErrno;. if(
c2780 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 op==F_GETLK ){.
c2790 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 zOpName = "GE
c27a0 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 TLK";. }else if
c27b0 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b ( op==F_SETLK ){
c27c0 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 . zOpName = "
c27d0 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b SETLK";. }else{
c27e0 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 . s = fcntl(f
c27f0 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 d, op, p);. s
c2800 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c2810 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e f("fcntl unknown
c2820 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 %d %d %d\n", fd
c2830 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 , op, s);. re
c2840 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 turn s;. }. if
c2850 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 ( p->l_type==F_R
c2860 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 DLCK ){. zTyp
c2870 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d e = "RDLCK";. }
c2880 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 else if( p->l_ty
c2890 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 pe==F_WRLCK ){.
c28a0 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 zType = "WRLC
c28b0 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 K";. }else if(
c28c0 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c p->l_type==F_UNL
c28d0 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 CK ){. zType
c28e0 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c = "UNLCK";. }el
c28f0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
c2900 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 0 );. }. asser
c2910 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d t( p->l_whence==
c2920 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 SEEK_SET );. s
c2930 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 = fcntl(fd, op,
c2940 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e 6f p);. savedErrno
c2950 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 = errno;. sqli
c2960 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 te3DebugPrintf("
c2970 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 25 fcntl %d %d %s %
c2980 73 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e 22 s %d %d %d %d\n"
c2990 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 2c ,. threadid,
c29a0 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 fd, zOpName, zT
c29b0 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 ype, (int)p->l_s
c29c0 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f tart, (int)p->l_
c29d0 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 len,. (int)p
c29e0 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 ->l_pid, s);. i
c29f0 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 f( s==(-1) && op
c2a00 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d ==F_SETLK && (p-
c2a10 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b >l_type==F_RDLCK
c2a20 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 || p->l_type==F
c2a30 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 _WRLCK) ){. s
c2a40 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a truct flock l2;.
c2a50 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 l2 = *p;.
c2a60 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 fcntl(fd, F_GET
c2a70 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 LK, &l2);. if
c2a80 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 ( l2.l_type==F_R
c2a90 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 DLCK ){. zT
c2aa0 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 ype = "RDLCK";.
c2ab0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e }else if( l2.
c2ac0 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 l_type==F_WRLCK
c2ad0 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d ){. zType =
c2ae0 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 "WRLCK";. }e
c2af0 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 lse if( l2.l_typ
c2b00 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 e==F_UNLCK ){.
c2b10 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c zType = "UNL
c2b20 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a CK";. }else{.
c2b30 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 assert( 0
c2b40 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
c2b50 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c2b60 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 "fcntl-failure-r
c2b70 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 eason: %s %d %d
c2b80 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 %d\n",. zT
c2b90 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 ype, (int)l2.l_s
c2ba0 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f tart, (int)l2.l_
c2bb0 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 len, (int)l2.l_p
c2bc0 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f id);. }. errno
c2bd0 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 = savedErrno;.
c2be0 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 return s;.}.#de
c2bf0 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 fine fcntl lockT
c2c00 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 race.#endif /* S
c2c10 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 QLITE_LOCK_TRACE
c2c20 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 */..../*.** Thi
c2c30 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c s routine transl
c2c40 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20 ates a standard
c2c50 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 POSIX errno code
c2c60 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a into something.
c2c70 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65 ** useful to the
c2c80 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20 clients of the
c2c90 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e sqlite3 function
c2ca0 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 s. Specifically
c2cb0 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e , it is.** inten
c2cc0 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 ded to translate
c2cd0 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74 a variety of "t
c2ce0 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73 ry again" errors
c2cf0 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 into SQLITE_BUS
c2d00 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65 Y.** and a varie
c2d10 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c ty of "please cl
c2d20 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ose the file des
c2d30 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 criptor NOW" err
c2d40 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c ors into .** SQL
c2d50 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a ITE_IOERR.** .**
c2d60 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69 Errors during i
c2d70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
c2d80 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 locks, or file
c2d90 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66 system support f
c2da0 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f or locks,.** sho
c2db0 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 uld handle ENOLC
c2dc0 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e K, ENOTSUP, EOPN
c2dd0 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c OTSUPP separatel
c2de0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
c2df0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
c2e00 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70 PosixError(int p
c2e10 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 osixError, int s
c2e20 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 qliteIOErr) {.
c2e30 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72 switch (posixErr
c2e40 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 or) {. case 0:
c2e50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
c2e60 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 TE_OK;. . ca
c2e70 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 se EAGAIN:. cas
c2e80 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 e ETIMEDOUT:. c
c2e90 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73 ase EBUSY:. cas
c2ea0 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 e EINTR:. case
c2eb0 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a ENOLCK: . /*
c2ec0 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 random NFS retr
c2ed0 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 y error, unless
c2ee0 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 during file syst
c2ef0 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20 em support .
c2f00 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e * introspection
c2f10 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63 , in which it ac
c2f20 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 tually means wha
c2f30 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 t it says */.
c2f40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
c2f50 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 USY;. . case
c2f60 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a EACCES: . /*
c2f70 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20 EACCES is like
c2f80 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f EAGAIN during lo
c2f90 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 cking operations
c2fa0 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 , but not any ot
c2fb0 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 her time*/. i
c2fc0 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20 f( (sqliteIOErr
c2fd0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f == SQLITE_IOERR_
c2fe0 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 LOCK) || ..(sqli
c2ff0 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 teIOErr == SQLIT
c3000 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 E_IOERR_UNLOCK)
c3010 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 || ..(sqliteIOEr
c3020 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 r == SQLITE_IOER
c3030 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 R_RDLOCK) ||..(s
c3040 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
c3050 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
c3060 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b RESERVEDLOCK) ){
c3070 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
c3080 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
c3090 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c . /* else fal
c30a0 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 l through */. c
c30b0 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 ase EPERM: .
c30c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
c30d0 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 RM;. . case
c30e0 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 EDEADLK:. ret
c30f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
c3100 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 _BLOCKED;. .#
c3110 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 if EOPNOTSUPP!=E
c3120 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f NOTSUP. case EO
c3130 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f PNOTSUPP: . /
c3140 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 * something went
c3150 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20 terribly awry,
c3160 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 unless during fi
c3170 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le system suppor
c3180 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 t . * intros
c3190 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 pection, in whic
c31a0 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 h it actually me
c31b0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 ans what it says
c31c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 */.#endif.#ifde
c31d0 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 f ENOTSUP. case
c31e0 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f ENOTSUP: . /
c31f0 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e * invalid fd, un
c3200 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 less during file
c3210 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 system support
c3220 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 introspection, i
c3230 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20 n which . *
c3240 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e it actually mean
c3250 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a s what it says *
c3260 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20 /.#endif. case
c3270 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44 EIO:. case EBAD
c3280 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c F:. case EINVAL
c3290 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e :. case ENOTCON
c32a0 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56 N:. case ENODEV
c32b0 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a :. case ENXIO:.
c32c0 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 case ENOENT:.
c32d0 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 case ESTALE:.
c32e0 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 case ENOSYS:.
c32f0 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 /* these should
c3300 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e force the clien
c3310 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 t to close the f
c3320 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 ile and reconnec
c3330 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 t */. . defa
c3340 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e ult: . return
c3350 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 sqliteIOErr;.
c3360 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}..../********
c3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c33a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c33b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
c33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
c33d0 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 Unique File ID U
c33e0 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 tility Used By V
c33f0 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a xWorks *********
c3400 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 ******.**.** On
c3410 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 most versions of
c3420 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65 unix, we can ge
c3430 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f t a unique ID fo
c3440 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 r a file by conc
c3450 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 atenating.** the
c3460 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61 device number a
c3470 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d nd the inode num
c3480 62 65 72 2e 20 20 42 75 74 20 74 68 69 73 20 64 ber. But this d
c3490 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 oes not work on
c34a0 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 VxWorks..** On V
c34b0 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 xWorks, a unique
c34c0 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65 file id must be
c34d0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61 based on the ca
c34e0 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 nonical filename
c34f0 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 ..**.** A pointe
c3500 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
c3510 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
c3520 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 g structure can
c3530 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 be used as a.**
c3540 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 69 unique file ID i
c3550 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 n VxWorks. Each
c3560 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
c3570 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 s structure cont
c3580 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f ains.** a copy o
c3590 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 f the canonical
c35a0 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 filename. There
c35b0 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72 is also a refer
c35c0 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a ence count. .**
c35d0 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 The structure i
c35e0 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e s reclaimed when
c35f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
c3600 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72 ointers to it dr
c3610 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a ops to.** zero..
c3620 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
c3630 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20 never very many
c3640 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e files open at on
c3650 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 e time and looku
c3660 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 ps are not.** a
c3670 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 performance-crit
c3680 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 ical path, so it
c3690 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 is sufficient t
c36a0 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 o put these.** s
c36b0 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c tructures on a l
c36c0 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 inked list..*/.s
c36d0 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
c36e0 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76 eId {. struct v
c36f0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e xworksFileId *pN
c3700 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e ext; /* Next in
c3710 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 a list of them
c3720 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 all */. int nRe
c3730 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
c3740 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
c3750 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
c3760 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 o this one */.
c3770 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 int nName;
c3780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c3790 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a Length of the z
c37a0 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 CanonicalName[]
c37b0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 string */. char
c37c0 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 *zCanonicalName
c37d0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e ; /* Can
c37e0 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 onical filename
c37f0 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 */.};..#if OS_VX
c3800 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c WORKS./* .** All
c3810 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 unique filename
c3820 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 s are held on a
c3830 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 linked list head
c3840 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 ed by this.** va
c3850 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 riable:.*/.stati
c3860 63 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 c struct vxworks
c3870 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 FileId *vxworksF
c3880 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a ileList = 0;../*
c3890 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 .** Simplify a f
c38a0 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 ilename into its
c38b0 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a canonical form.
c38c0 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 ** by making the
c38d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 following chang
c38e0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d es:.**.** * rem
c38f0 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 oving any traili
c3900 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65 ng and duplicate
c3910 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 /.** * convert
c3920 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f /./ into just /
c3930 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f .** * convert /
c3940 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73 A/../ where A is
c3950 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 any simple name
c3960 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a into just /.**.
c3970 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d ** Changes are m
c3980 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 ade in-place. R
c3990 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 eturn the new na
c39a0 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a me length..**.**
c39b0 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 The original fi
c39c0 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 lename is in z[0
c39d0 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 ..n-1]. Return
c39e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
c39f0 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 characters in t
c3a00 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 he simplified na
c3a10 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e me..*/.static in
c3a20 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 t vxworksSimplif
c3a30 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 yName(char *z, i
c3a40 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 nt n){. int i,
c3a50 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 j;. while( n>1
c3a60 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 && z[n-1]=='/' )
c3a70 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 { n--; }. for(i
c3a80 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b =j=0; i<n; i++){
c3a90 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 . if( z[i]=='
c3aa0 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 /' ){. if(
c3ab0 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f z[i+1]=='/' ) co
c3ac0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 ntinue;. if
c3ad0 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 ( z[i+1]=='.' &&
c3ae0 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d i+2<n && z[i+2]
c3af0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 =='/' ){.
c3b00 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 i += 1;.
c3b10 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
c3b20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 }. if( z[i
c3b30 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c +1]=='.' && i+3<
c3b40 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 n && z[i+2]=='.'
c3b50 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 && z[i+3]=='/'
c3b60 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 ){. while
c3b70 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 ( j>0 && z[j-1]!
c3b80 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 ='/' ){ j--; }.
c3b90 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 if( j>0 )
c3ba0 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 { j--; }.
c3bb0 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 i += 2;.
c3bc0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
c3bd0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a }. }. z[j
c3be0 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a ++] = z[i];. }.
c3bf0 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 z[j] = 0;. re
c3c00 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn j;.}../*.**
c3c10 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66 Find a unique f
c3c20 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20 67 ile ID for the g
c3c30 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 iven absolute pa
c3c40 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a thname. Return.
c3c50 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
c3c60 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 the vxworksFileI
c3c70 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 d object. This
c3c80 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20 75 pointer is the u
c3c90 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 nique.** file ID
c3ca0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 ..**.** The nRef
c3cb0 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78 field of the vx
c3cc0 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 worksFileId obje
c3cd0 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 ct is incremente
c3ce0 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 d before.** the
c3cf0 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e object is return
c3d00 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72 ed. A new vxwor
c3d10 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 ksFileId object
c3d20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e is created.** an
c3d30 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 67 d added to the g
c3d40 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 lobal list if ne
c3d50 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 cessary..**.** I
c3d60 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
c3d70 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 ation error occu
c3d80 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e rs, return NULL.
c3d90 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
c3da0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
c3db0 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 *vxworksFindFile
c3dc0 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a Id(const char *z
c3dd0 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 AbsoluteName){.
c3de0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 struct vxworksF
c3df0 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 ileId *pNew;
c3e00 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b /* search k
c3e10 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 ey and new file
c3e20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 ID */. struct v
c3e30 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 xworksFileId *pC
c3e40 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 andidate; /* F
c3e50 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
c3e60 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 existing file ID
c3e70 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 s */. int n;
c3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
c3ea0 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 ngth of zAbsolut
c3eb0 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a eName string */.
c3ec0 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f . assert( zAbso
c3ed0 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 luteName[0]=='/'
c3ee0 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 );. n = (int)s
c3ef0 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e trlen(zAbsoluteN
c3f00 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 ame);. pNew = s
c3f10 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 qlite3_malloc( s
c3f20 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 izeof(*pNew) + (
c3f30 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e n+1) );. if( pN
c3f40 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 ew==0 ) return 0
c3f50 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e ;. pNew->zCanon
c3f60 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 icalName = (char
c3f70 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 *)&pNew[1];. me
c3f80 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f mcpy(pNew->zCano
c3f90 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f nicalName, zAbso
c3fa0 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a luteName, n+1);.
c3fb0 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d n = vxworksSim
c3fc0 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e plifyName(pNew->
c3fd0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 zCanonicalName,
c3fe0 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 n);.. /* Search
c3ff0 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 for an existing
c4000 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 entry that matc
c4010 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 hing the canonic
c4020 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 al name.. ** If
c4030 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e found, incremen
c4040 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 t the reference
c4050 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e count and return
c4060 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 a pointer to.
c4070 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 ** the existing
c4080 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 file ID.. */.
c4090 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
c40a0 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 ;. for(pCandida
c40b0 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 te=vxworksFileLi
c40c0 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 st; pCandidate;
c40d0 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 pCandidate=pCand
c40e0 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 idate->pNext){.
c40f0 20 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 if( pCandidat
c4100 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 e->nName==n .
c4110 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e && memcmp(pCan
c4120 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 didate->zCanonic
c4130 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 alName, pNew->zC
c4140 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 anonicalName, n)
c4150 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ==0. ){.
c4160 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
c4170 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61 New);. pCa
c4180 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b ndidate->nRef++;
c4190 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 . unixLeav
c41a0 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 eMutex();.
c41b0 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61 return pCandida
c41c0 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 te;. }. }..
c41d0 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 /* No match was
c41e0 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c found. We will
c41f0 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 make a new file
c4200 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e ID */. pNew->n
c4210 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d Ref = 1;. pNew-
c4220 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e >nName = n;. pN
c4230 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f ew->pNext = vxwo
c4240 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 rksFileList;. v
c4250 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d xworksFileList =
c4260 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 pNew;. unixLea
c4270 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 veMutex();. ret
c4280 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
c4290 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 ** Decrement the
c42a0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
c42b0 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c on a vxworksFil
c42c0 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 eId object. Fre
c42d0 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 e.** the object
c42e0 77 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e when the referen
c42f0 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 ce count reaches
c4300 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 zero..*/.static
c4310 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c void vxworksRel
c4320 65 61 73 65 46 69 6c 65 49 64 28 73 74 72 75 63 easeFileId(struc
c4330 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
c4340 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 *pId){. unixEnt
c4350 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 erMutex();. ass
c4360 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 ert( pId->nRef>0
c4370 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d );. pId->nRef-
c4380 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 -;. if( pId->nR
c4390 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 ef==0 ){. str
c43a0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
c43b0 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 d **pp;. for(
c43c0 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c pp=&vxworksFileL
c43d0 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 ist; *pp && *pp!
c43e0 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 =pId; pp = &((*p
c43f0 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 p)->pNext)){}.
c4400 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 assert( *pp==p
c4410 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 Id );. *pp =
c4420 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pId->pNext;.
c4430 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 sqlite3_free(pId
c4440 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 );. }. unixLea
c4450 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e veMutex();.}.#en
c4460 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b dif /* OS_VXWORK
c4470 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a S */./**********
c4480 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 ***** End of Uni
c4490 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c que File ID Util
c44a0 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f ity Used By VxWo
c44b0 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a rks ************
c44c0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
c44d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c44e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c44f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4510 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ***/.../********
c4520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4560 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
c4570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4580 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 ** Posix Advisor
c4590 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a y Locking ******
c45a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c45b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 ******.**.** POS
c45c0 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b IX advisory lock
c45d0 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 s are broken by
c45e0 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 design. ANSI ST
c45f0 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a D 1003.1 (1996).
c4600 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 ** section 6.5.2
c4610 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 .2 lines 483 thr
c4620 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 66 79 ough 490 specify
c4630 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f that when a pro
c4640 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 cess.** sets or
c4650 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 clears a lock, t
c4660 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 hat operation ov
c4670 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f errides any prio
c4680 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 r locks set.** b
c4690 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 y the same proce
c46a0 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 ss. It does not
c46b0 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 explicitly say
c46c0 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 so, but this imp
c46d0 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 lies.** that it
c46e0 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 overrides locks
c46f0 73 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 set by the same
c4700 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 process using a
c4710 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c different.** fil
c4720 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 e descriptor. C
c4730 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73 onsider this tes
c4740 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 t case:.**.**
c4750 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 int fd1 = op
c4760 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f en("./file1", O_
c4770 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 RDWR|O_CREAT, 06
c4780 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 44);.** in
c4790 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f t fd2 = open("./
c47a0 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f file2", O_RDWR|O
c47b0 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a _CREAT, 0644);.*
c47c0 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 *.** Suppose ./f
c47d0 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 ile1 and ./file2
c47e0 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 are really the
c47f0 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 same file (becau
c4800 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 se.** one is a h
c4810 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 ard or symbolic
c4820 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 link to the othe
c4830 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 r) then if you s
c4840 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 et.** an exclusi
c4850 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 ve lock on fd1,
c4860 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 then try to get
c4870 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
c4880 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 k.** on fd2, it
c4890 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 works. I would
c48a0 68 61 76 65 20 65 78 70 65 63 74 65 64 20 74 68 have expected th
c48b0 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f e second lock to
c48c0 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 .** fail since t
c48d0 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 here was already
c48e0 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 a lock on the f
c48f0 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a ile due to fd1..
c4900 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 ** But not so.
c4910 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 Since both locks
c4920 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 came from the s
c4930 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 ame process, the
c4940 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 .** second overr
c4950 69 64 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 ides the first,
c4960 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 even though they
c4970 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 were on differe
c4980 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 nt.** file descr
c4990 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e iptors opened on
c49a0 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 different file
c49b0 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 names..**.** Thi
c49c0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 s means that we
c49d0 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 cannot use POSIX
c49e0 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 locks to synchr
c49f0 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 onize file acces
c4a00 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 s.** among compe
c4a10 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 ting threads of
c4a20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
c4a30 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 . POSIX locks w
c4a40 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a ill work fine.**
c4a50 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 to synchronize
c4a60 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 access for threa
c4a70 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 ds in separate p
c4a80 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f rocesses, but no
c4a90 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 t.** threads wit
c4aa0 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f hin the same pro
c4ab0 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 cess..**.** To w
c4ac0 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 ork around the p
c4ad0 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 roblem, SQLite h
c4ae0 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c as to manage fil
c4af0 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c e locks internal
c4b00 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e ly.** on its own
c4b10 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 . Whenever a ne
c4b20 77 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 w database is op
c4b30 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f ened, we have to
c4b40 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 find the.** spe
c4b50 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 cific inode of t
c4b60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
c4b70 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 (the inode is d
c4b80 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
c4b90 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 .** st_dev and s
c4ba0 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 t_ino fields of
c4bb0 74 68 65 20 73 74 61 74 20 73 74 72 75 63 74 75 the stat structu
c4bc0 72 65 20 74 68 61 74 20 66 73 74 61 74 28 29 20 re that fstat()
c4bd0 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 fills in).** and
c4be0 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 check for locks
c4bf0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e already existin
c4c00 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e g on that inode.
c4c10 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 When locks are
c4c20 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 .** created or r
c4c30 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 emoved, we have
c4c40 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f to look at our o
c4c50 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f wn internal reco
c4c60 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 rd of the.** loc
c4c70 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f ks to see if ano
c4c80 74 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 ther thread has
c4c90 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 previously set a
c4ca0 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 lock on that sa
c4cb0 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a me.** inode..**.
c4cc0 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20 75 ** (Aside: The u
c4cd0 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 se of inode numb
c4ce0 65 72 73 20 61 73 20 75 6e 69 71 75 65 20 49 44 ers as unique ID
c4cf0 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 s does not work
c4d00 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 on VxWorks..** F
c4d10 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 or VxWorks, we h
c4d20 61 76 65 20 74 6f 20 75 73 65 20 74 68 65 20 61 ave to use the a
c4d30 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75 lternative uniqu
c4d40 65 20 49 44 20 73 79 73 74 65 6d 20 62 61 73 65 e ID system base
c4d50 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 d on.** canonica
c4d60 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 l filename and i
c4d70 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 mplemented in th
c4d80 65 20 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 e previous divis
c4d90 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ion.).**.** The
c4da0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 sqlite3_file str
c4db0 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58 ucture for POSIX
c4dc0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 is no longer ju
c4dd0 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69 st an integer fi
c4de0 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 le.** descriptor
c4df0 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73 . It is now a s
c4e00 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68 6f tructure that ho
c4e10 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 lds the integer
c4e20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 file.** descript
c4e30 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 or and a pointer
c4e40 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 to a structure
c4e50 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 that describes t
c4e60 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c he internal.** l
c4e70 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 ocks on the corr
c4e80 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e esponding inode.
c4e90 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c There is one l
c4ea0 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 ocking structure
c4eb0 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 .** per inode, s
c4ec0 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e o if the same in
c4ed0 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77 ode is opened tw
c4ee0 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 ice, both unixFi
c4ef0 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a le structures.**
c4f00 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61 point to the sa
c4f10 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 me locking struc
c4f20 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 ture. The locki
c4f30 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 ng structure kee
c4f40 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 ps.** a referenc
c4f50 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 e count (so we w
c4f60 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f ill know when to
c4f70 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20 delete it) and
c4f80 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 a "cnt".** field
c4f90 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69 that tells us i
c4fa0 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b ts internal lock
c4fb0 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 status. cnt==0
c4fc0 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 means the.** fi
c4fd0 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 le is unlocked.
c4fe0 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 cnt==-1 means t
c4ff0 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65 he file has an e
c5000 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a xclusive lock..*
c5010 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 * cnt>0 means th
c5020 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 72 ere are cnt shar
c5030 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 ed locks on the
c5040 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 file..**.** Any
c5050 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 attempt to lock
c5060 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 or unlock a file
c5070 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 68 first checks th
c5080 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 e locking.** str
c5090 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e ucture. The fcn
c50a0 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c tl() system call
c50b0 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 is only invoked
c50c0 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f to set a .** PO
c50d0 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 SIX lock if the
c50e0 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 internal lock st
c50f0 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 69 ructure transiti
c5100 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 ons between.** a
c5110 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 locked and an u
c5120 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a nlocked state..*
c5130 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20 *.** But wait:
c5140 74 68 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f there are yet mo
c5150 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 re problems with
c5160 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 POSIX advisory
c5170 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 locks..**.** If
c5180 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 you close a file
c5190 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 descriptor that
c51a0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c points to a fil
c51b0 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 e that has locks
c51c0 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f ,.** all locks o
c51d0 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 n that file that
c51e0 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68 are owned by th
c51f0 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 e current proces
c5200 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 s are.** release
c5210 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 d. To work arou
c5220 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c nd this problem,
c5230 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20 73 each unixFile s
c5240 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
c5250 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
c5260 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74 o an unixOpenCnt
c5270 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
c5280 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70 re is one unixOp
c5290 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a enCnt structure.
c52a0 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 ** per open inod
c52b0 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 e, which means t
c52c0 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 hat multiple uni
c52d0 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 xFile can point
c52e0 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 to a single.** u
c52f0 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65 nixOpenCnt. Whe
c5300 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 n an attempt is
c5310 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e made to close an
c5320 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 unixFile, if th
c5330 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 ere are.** other
c5340 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f unixFile open o
c5350 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 n the same inode
c5360 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e that are holdin
c5370 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c g locks, the cal
c5380 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 l.** to close()
c5390 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
c53a0 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 tor is deferred
c53b0 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 until all of the
c53c0 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a locks clear..**
c53d0 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 The unixOpenCnt
c53e0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 structure keeps
c53f0 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 a list of file
c5400 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 74 descriptors that
c5410 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63 need to.** be c
c5420 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 6c losed and that l
c5430 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 61 ist is walked (a
c5440 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 6e nd cleared) when
c5450 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a the last lock.*
c5460 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 * clears..**.**
c5470 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f 62 Yet another prob
c5480 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 61 lem: LinuxThrea
c5490 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77 ds do not play w
c54a0 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 6c ell with posix l
c54b0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 ocks..**.** Many
c54c0 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 older versions
c54d0 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 65 of linux use the
c54e0 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c 69 LinuxThreads li
c54f0 62 72 61 72 79 20 77 68 69 63 68 20 69 73 0a 2a brary which is.*
c5500 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70 * not posix comp
c5510 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69 liant. Under Li
c5520 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c 6f nuxThreads, a lo
c5530 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 ck created by th
c5540 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 read.** A cannot
c5550 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 20 be modified or
c5560 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 20 overridden by a
c5570 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
c5580 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 B..** Only thre
c5590 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 20 ad A can modify
c55a0 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 the lock. Locki
c55b0 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 63 ng behavior is c
c55c0 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 65 orrect.** if the
c55d0 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 73 appliation uses
c55e0 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 76 the newer Nativ
c55f0 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 4c e Posix Thread L
c5600 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a ibrary (NPTL).**
c5610 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 68 on linux - with
c5620 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65 NPTL a lock cre
c5630 61 74 65 64 20 62 79 20 74 68 72 65 61 64 20 41 ated by thread A
c5640 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f can override lo
c5650 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64 cks.** in thread
c5660 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 B. But there i
c5670 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 s no way to know
c5680 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 at compile-time
c5690 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 64 which.** thread
c56a0 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 62 ing library is b
c56b0 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 74 eing used. So t
c56c0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 here is no way t
c56d0 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d o know at.** com
c56e0 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 65 pile-time whethe
c56f0 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 20 r or not thread
c5700 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c A can override l
c5710 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 42 ocks on thread B
c5720 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f 20 ..** We have to
c5730 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 do a run-time ch
c5740 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 20 eck to discover
c5750 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
c5760 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 the.** current p
c5770 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e rocess..**.** On
c5780 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 74 systems where t
c5790 68 72 65 61 64 20 41 20 69 73 20 75 6e 61 62 6c hread A is unabl
c57a0 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b e to modify lock
c57b0 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 s created by.**
c57c0 74 68 72 65 61 64 20 42 2c 20 77 65 20 68 61 76 thread B, we hav
c57d0 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 e to keep track
c57e0 6f 66 20 77 68 69 63 68 20 74 68 72 65 61 64 20 of which thread
c57f0 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a 20 created each.**
c5800 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68 65 lock. Hence the
c5810 72 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 66 re is an extra f
c5820 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79 20 ield in the key
c5830 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 to the unixLockI
c5840 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 nfo.** structure
c5850 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73 20 to record this
c5860 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e information. An
c5870 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 d on those syste
c5880 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 ms it.** is ille
c5890 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20 74 gal to begin a t
c58a0 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e ransaction in on
c58b0 65 20 74 68 72 65 61 64 20 61 6e 64 20 66 69 6e e thread and fin
c58c0 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f ish it.** in ano
c58d0 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73 20 ther. For this
c58e0 6c 61 74 74 65 72 20 72 65 73 74 72 69 63 74 69 latter restricti
c58f0 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 on, there is no
c5900 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 work-around..**
c5910 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74 69 It is a limitati
c5920 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65 61 on of LinuxThrea
c5930 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 ds..*/../*.** Se
c5940 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 75 t or check the u
c5950 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 6c nixFile.tid fiel
c5960 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 69 d. This field i
c5970 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 6e s set when an un
c5980 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72 ixFile.** is fir
c5990 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20 st opened. All
c59a0 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 20 subsequent uses
c59b0 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 of the unixFile
c59c0 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 0a verify that the.
c59d0 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 ** same thread i
c59e0 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 74 s operating on t
c59f0 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f he unixFile. So
c5a00 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 me operating sys
c5a10 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 tems do.** not a
c5a20 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65 llow locks to be
c5a30 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6f overridden by o
c5a40 74 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64 ther threads and
c5a50 20 74 68 61 74 20 72 65 73 74 72 69 63 74 69 6f that restrictio
c5a60 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 n.** means that
c5a70 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 sqlite3* databas
c5a80 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 e handles cannot
c5a90 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f be moved from o
c5aa0 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20 ne thread.** to
c5ab0 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c 6f another while lo
c5ac0 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a cks are held..**
c5ad0 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e .** Version 3.3.
c5ae0 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a 20 1 (2006-01-15):
c5af0 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 65 unixFile can be
c5b00 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 moved from one
c5b10 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f thread to.** ano
c5b20 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 20 ther as long as
c5b30 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f we are running o
c5b40 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 20 n a system that
c5b50 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 73 supports threads
c5b60 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 65 .** overriding e
c5b70 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
c5b80 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20 74 (which is now t
c5b90 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 he most common b
c5ba0 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 ehavior).** or i
c5bb0 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 f no locks are h
c5bc0 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e eld. But the un
c5bd0 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 ixFile.pLock fie
c5be0 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a ld needs to be.*
c5bf0 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63 * recomputed bec
c5c00 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63 ause its key inc
c5c10 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64 ludes the thread
c5c20 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a -id. See the .*
c5c30 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 * transferOwners
c5c40 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 hip() function b
c5c50 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f elow for additio
c5c60 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a nal information.
c5c70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c5c80 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
c5c90 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 ned(__linux__).#
c5ca0 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 define SET_THRE
c5cb0 41 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e 74 ADID(X) (X)->t
c5cc0 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c id = pthread_sel
c5cd0 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 f().# define CHE
c5ce0 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 28 CK_THREADID(X) (
c5cf0 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
c5d00 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d achOthersLocks==
c5d10 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20 20 0 && \.
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5d30 20 20 20 21 70 74 68 72 65 61 64 5f 65 71 75 61 !pthread_equa
c5d40 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 l((X)->tid, pthr
c5d50 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c ead_self())).#el
c5d60 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f se.# define SET_
c5d70 54 48 52 45 41 44 49 44 28 58 29 0a 23 20 64 65 THREADID(X).# de
c5d80 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 fine CHECK_THREA
c5d90 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66 0a DID(X) 0.#endif.
c5da0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
c5db0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
c5dc0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 ing structure se
c5dd0 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 rves as the key
c5de0 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 used.** to locat
c5df0 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 e a particular u
c5e00 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
c5e10 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 ture given its i
c5e20 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 node. This.** i
c5e30 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
c5e40 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78 e unixLockKey ex
c5e50 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 68 cept that the th
c5e60 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74 74 read ID is omitt
c5e70 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e ed..*/.struct un
c5e80 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 ixFileId {. dev
c5e90 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20 _t dev;
c5ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 /* Devi
c5eb0 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 ce number */.#if
c5ec0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 OS_VXWORKS. st
c5ed0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
c5ee0 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 Id *pId; /* Uni
c5ef0 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 que file ID for
c5f00 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 vxworks. */.#els
c5f10 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 e. ino_t ino;
c5f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5f30 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 /* Inode number
c5f40 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
c5f50 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
c5f60 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
c5f70 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 structure serve
c5f80 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 s as the key use
c5f90 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 d.** to locate a
c5fa0 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 particular unix
c5fb0 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 LockInfo structu
c5fc0 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f re given its ino
c5fd0 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 de..**.** If thr
c5fe0 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 eads cannot over
c5ff0 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
c6000 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68 72 locks (LinuxThr
c6010 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a eads), then we.*
c6020 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c 6f * set the unixLo
c6030 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64 20 ckKey.tid field
c6040 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 49 44 to the thread ID
c6050 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 63 61 . If threads ca
c6060 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 n override.** ea
c6070 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 ch others locks
c6080 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c 29 (Posix and NPTL)
c6090 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c 77 then tid is alw
c60a0 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e ays set to zero.
c60b0 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74 74 .** tid is omitt
c60c0 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 ed if we compile
c60d0 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64 69 without threadi
c60e0 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e ng support or on
c60f0 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 an OS.** other
c6100 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 than linux..*/.s
c6110 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 truct unixLockKe
c6120 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 y {. struct uni
c6130 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a xFileId fid; /*
c6140 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 Unique identifi
c6150 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 er for the file
c6160 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c6170 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
c6180 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 ned(__linux__).
c6190 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 pthread_t tid;
c61a0 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f 66 /* Thread ID of
c61b0 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 lock owner. Zer
c61c0 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c o if not using L
c61d0 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a 23 inuxThreads */.#
c61e0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
c61f0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
c6200 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
c6210 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 ucture is alloca
c6220 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 ted for each ope
c6230 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c n.** inode. Or,
c6240 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 73 on LinuxThreads
c6250 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 6f , there is one o
c6260 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
c6270 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69 es for.** each i
c6280 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 65 node opened by e
c6290 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a ach thread..**.*
c62a0 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65 * A single inode
c62b0 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 70 can have multip
c62c0 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 le file descript
c62d0 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e 69 ors, so each uni
c62e0 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 xFile.** structu
c62f0 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f re contains a po
c6300 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
c6310 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
c6320 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 ect and this.**
c6330 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 63 object keeps a c
c6340 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 ount of the numb
c6350 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 70 er of unixFile p
c6360 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a ointing to it..*
c6370 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 /.struct unixLoc
c6380 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 kInfo {. struct
c6390 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 unixLockKey loc
c63a0 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 kKey; /* The
c63b0 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 lookup key */.
c63c0 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 int cnt;
c63d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c63e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 48 /* Number of SH
c63f0 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20 ARED locks held
c6400 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 */. int locktyp
c6410 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
c6420 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 /* One of S
c6430 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 HARED_LOCK, RESE
c6440 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a RVED_LOCK etc. *
c6450 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
c6460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
c6480 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 pointers to thi
c6490 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 s structure */.
c64a0 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b struct unixLock
c64b0 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 Info *pNext;
c64c0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 /* List of all
c64d0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a unixLockInfo obj
c64e0 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ects */. struct
c64f0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
c6500 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20 20 Prev; /*
c6510 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b .... doubly link
c6520 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ed */.};../*.**
c6530 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
c6540 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
c6550 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 ucture is alloca
c6560 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 ted for each ope
c6570 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 n.** inode. Thi
c6580 73 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 s structure keep
c6590 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e s track of the n
c65a0 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f umber of locks o
c65b0 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e n that.** inode.
c65c0 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73 20 If a close is
c65d0 61 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e 73 attempted agains
c65e0 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 t an inode that
c65f0 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f is holding.** lo
c6600 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 cks, the close i
c6610 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c s deferred until
c6620 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 all locks clear
c6630 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a 2a by adding the.*
c6640 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f * file descripto
c6650 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 r to be closed t
c6660 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 o the pending li
c6670 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 st..**.** TODO:
c6680 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69 Consider changi
c6690 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 ng this so that
c66a0 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 there is only a
c66b0 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 single file.** d
c66c0 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65 61 escriptor for ea
c66d0 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76 ch open file, ev
c66e0 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 en when it is op
c66f0 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 ened multiple ti
c6700 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 mes..** The clos
c6710 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 e() system call
c6720 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72 would only occur
c6730 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 64 when the last d
c6740 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67 atabase.** using
c6750 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65 73 the file closes
c6760 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 ..*/.struct unix
c6770 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75 OpenCnt {. stru
c6780 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 ct unixFileId fi
c6790 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c leId; /* The l
c67a0 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 ookup key */. i
c67b0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
c67c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
c67d0 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 mber of pointers
c67e0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
c67f0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 re */. int nLoc
c6800 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
c6810 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
c6820 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 outstanding loc
c6830 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e ks */. int nPen
c6840 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 20 20 ding;
c6850 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
c6860 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29 pending close()
c6870 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 operations */.
c6880 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20 int *aPending;
c6890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
c68a0 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c lloced space hol
c68b0 64 69 6e 67 20 66 64 27 73 20 61 77 61 69 74 69 ding fd's awaiti
c68c0 6e 67 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a ng a close() */.
c68d0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
c68e0 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 sem_t *pSem;
c68f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6900 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 /* Named POSIX
c6910 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 semaphore */.
c6920 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 char aSemName[MA
c6930 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 20 X_PATHNAME+1];
c6940 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 /* Name of that
c6950 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 semaphore */.#e
c6960 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e ndif. struct un
c6970 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 ixOpenCnt *pNext
c6980 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c , *pPrev; /* L
c6990 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f ist of all unixO
c69a0 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a penCnt objects *
c69b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 /.};../*.** List
c69c0 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 s of all unixLoc
c69d0 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 kInfo and unixOp
c69e0 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 enCnt objects.
c69f0 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 These used to be
c6a00 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e hash.** tables.
c6a10 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 But the number
c6a20 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 of objects is r
c6a30 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 arely more than
c6a40 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e a dozen and.** n
c6a50 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66 ever exceeds a f
c6a60 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e ew thousand. An
c6a70 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 d lookup is not
c6a80 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a on a critical.**
c6a90 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c path so a simpl
c6aa0 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 e linked list wi
c6ab0 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 ll suffice..*/.s
c6ac0 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 tatic struct uni
c6ad0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c xLockInfo *lockL
c6ae0 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 ist = 0;.static
c6af0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
c6b00 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 nt *openList = 0
c6b10 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 ;../*.** This va
c6b20 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 riable remembers
c6b30 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
c6b40 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 threads can over
c6b50 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
c6b60 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a .** locks..**.**
c6b70 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 0: No. Thr
c6b80 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 eads cannot over
c6b90 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
c6ba0 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 locks. (LinuxT
c6bb0 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a hreads).** 1:
c6bc0 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 Yes. Threads
c6bd0 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 can override eac
c6be0 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 h others locks.
c6bf0 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a (Posix & NLPT).
c6c00 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e ** -1: We don
c6c10 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 't know yet..**.
c6c20 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 ** On some syste
c6c30 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 ms, we know at c
c6c40 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 ompile-time if t
c6c50 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 hreads can overr
c6c60 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 ide each.** othe
c6c70 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 rs locks. On th
c6c80 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 ose systems, the
c6c90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f SQLITE_THREAD_O
c6ca0 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 VERRIDE_LOCK mac
c6cb0 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 ro.** will be se
c6cc0 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e t appropriately.
c6cd0 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 On other syste
c6ce0 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 ms, we have to c
c6cf0 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 heck at.** runti
c6d00 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 me. On these la
c6d10 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 tter systems, SQ
c6d20 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LTIE_THREAD_OVER
c6d30 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 RIDE_LOCK is.**
c6d40 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
c6d50 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e This variable n
c6d60 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 ormally has file
c6d70 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 scope only. Bu
c6d80 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 t during testing
c6d90 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 , we make.** it
c6da0 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 a global so that
c6db0 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 the test code c
c6dc0 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 an change its va
c6dd0 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 lue in order to
c6de0 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 verify.** that t
c6df0 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68 he right stuff h
c6e00 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 appens in either
c6e10 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 case..*/.#if SQ
c6e20 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
c6e30 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
c6e40 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 ux__).# ifndef
c6e50 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 SQLITE_THREAD_OV
c6e60 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 ERRIDE_LOCK.#
c6e70 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 define SQLITE_T
c6e80 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
c6e90 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a OCK -1.# endif.
c6ea0 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f # ifdef SQLITE_
c6eb0 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 TEST.int threads
c6ec0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
c6ed0 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 rsLocks = SQLITE
c6ee0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
c6ef0 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 _LOCK;.# else.s
c6f00 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 tatic int thread
c6f10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
c6f20 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 ersLocks = SQLIT
c6f30 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
c6f40 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 E_LOCK;.# endif
c6f50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
c6f60 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f his structure ho
c6f70 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 lds information
c6f80 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 passed into indi
c6f90 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 vidual test.** t
c6fa0 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65 hreads by the te
c6fb0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 stThreadLockingB
c6fc0 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e ehavior() routin
c6fd0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 e..*/.struct thr
c6fe0 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 eadTestData {.
c6ff0 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 int fd;
c7000 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
c7010 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 o be locked */.
c7020 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
c7030 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c ck; /* The l
c7040 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
c7050 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 */. int result
c7060 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
c7070 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f Result of the lo
c7080 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 cking operation
c7090 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 */.};..#if SQLIT
c70a0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
c70b0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
c70c0 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 _)./*.** This fu
c70d0 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 nction is used a
c70e0 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 s the main routi
c70f0 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 ne for a thread
c7100 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 launched by.** t
c7110 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
c7120 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 Behavior(). It t
c7130 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65 ests whether the
c7140 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 shared-lock obt
c7150 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 ained.** by the
c7160 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 main thread in t
c7170 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
c7180 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c Behavior() confl
c7190 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 icts with a.** h
c71a0 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 ypothetical writ
c71b0 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 e-lock obtained
c71c0 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f by this thread o
c71d0 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e n the same file.
c71e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 .**.** The write
c71f0 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 -lock is not act
c7200 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 ually acquired,
c7210 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 as this is not p
c7220 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 ossible if .** t
c7230 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 he file is open
c7240 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 in read-only mod
c7250 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 e (see ticket #3
c7260 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 472)..*/ .static
c7270 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 void *threadLoc
c7280 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 kingTest(void *p
c7290 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 Arg){. struct t
c72a0 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 hreadTestData *p
c72b0 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 Data = (struct t
c72c0 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 hreadTestData*)p
c72d0 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 Arg;. pData->re
c72e0 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 sult = fcntl(pDa
c72f0 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c ta->fd, F_GETLK,
c7300 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a &pData->lock);.
c7310 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d return pArg;.}
c7320 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
c7330 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
c7340 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
c7350 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 _) */...#if SQLI
c7360 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
c7370 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
c7380 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 __)./*.** This p
c7390 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 rocedure attempt
c73a0 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 s to determine w
c73b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
c73c0 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 reads.** can ove
c73d0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 rride each other
c73e0 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 s locks then set
c73f0 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 s the .** thread
c7400 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
c7410 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c ersLocks variabl
c7420 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e e appropriately.
c7430 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
c7440 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e testThreadLockin
c7450 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 gBehavior(int fd
c7460 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 _orig){. int fd
c7470 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 ;. int rc;. st
c7480 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 ruct threadTestD
c7490 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 ata d;. struct
c74a0 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 flock l;. pthre
c74b0 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 ad_t t;.. fd =
c74c0 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 dup(fd_orig);.
c74d0 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 if( fd<0 ) retur
c74e0 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 n;. memset(&l,
c74f0 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 0, sizeof(l));.
c7500 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 l.l_type = F_RD
c7510 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d LCK;. l.l_len =
c7520 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 1;. l.l_start
c7530 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 = 0;. l.l_whenc
c7540 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
c7550 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 rc = fcntl(fd_or
c7560 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 ig, F_SETLK, &l)
c7570 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 ;. if( rc!=0 )
c7580 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 return;. memset
c7590 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 (&d, 0, sizeof(d
c75a0 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b ));. d.fd = fd;
c75b0 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 . d.lock = l;.
c75c0 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d d.lock.l_type =
c75d0 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 70 74 68 72 F_WRLCK;. pthr
c75e0 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30 ead_create(&t, 0
c75f0 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 , threadLockingT
c7600 65 73 74 2c 20 26 64 29 3b 0a 20 20 70 74 68 72 est, &d);. pthr
c7610 65 61 64 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a ead_join(t, 0);.
c7620 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 close(fd);. i
c7630 66 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 f( d.result!=0 )
c7640 20 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 return;. threa
c7650 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
c7660 68 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c hersLocks = (d.l
c7670 6f 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e ock.l_type==F_UN
c7680 4c 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f LCK);.}.#endif /
c7690 2a 20 53 51 4c 49 54 45 5f 54 48 45 52 41 44 53 * SQLITE_THERADS
c76a0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
c76b0 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a _linux__) */../*
c76c0 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e .** Release a un
c76d0 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 ixLockInfo struc
c76e0 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 ture previously
c76f0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e allocated by fin
c7700 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a dLockInfo()..*/.
c7710 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 static void rele
c7720 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 aseLockInfo(stru
c7730 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
c7740 2a 70 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 70 *pLock){. if( p
c7750 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 Lock ){. pLoc
c7760 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 k->nRef--;. i
c7770 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d f( pLock->nRef==
c7780 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
c7790 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20 Lock->pPrev ){.
c77a0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
c77b0 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 Lock->pPrev->pNe
c77c0 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 xt==pLock );.
c77d0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 pLock->pPre
c77e0 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b v->pNext = pLock
c77f0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
c7800 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
c7810 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d sert( lockList==
c7820 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 pLock );.
c7830 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 lockList = pLoc
c7840 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 k->pNext;.
c7850 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 }. if( pLoc
c7860 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 k->pNext ){.
c7870 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
c7880 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d k->pNext->pPrev=
c7890 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 =pLock );.
c78a0 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e pLock->pNext->
c78b0 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 pPrev = pLock->p
c78c0 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Prev;. }.
c78d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
c78e0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 (pLock);. }.
c78f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 }.}../*.** Rele
c7900 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e ase a unixOpenCn
c7910 74 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 t structure prev
c7920 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
c7930 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f by findLockInfo
c7940 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ()..*/.static vo
c7950 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e id releaseOpenCn
c7960 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 t(struct unixOpe
c7970 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 nCnt *pOpen){.
c7980 69 66 28 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 if( pOpen ){.
c7990 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a pOpen->nRef--;.
c79a0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e if( pOpen->n
c79b0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ref==0 ){.
c79c0 69 66 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 if( pOpen->pPrev
c79d0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
c79e0 72 74 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 rt( pOpen->pPrev
c79f0 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 ->pNext==pOpen )
c7a00 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
c7a10 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
c7a20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 pOpen->pNext;.
c7a30 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c7a40 20 20 20 61 73 73 65 72 74 28 20 6f 70 65 6e 4c assert( openL
c7a50 69 73 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 ist==pOpen );.
c7a60 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d openList =
c7a70 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 pOpen->pNext;.
c7a80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
c7a90 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b pOpen->pNext ){
c7aa0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
c7ab0 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 pOpen->pNext->p
c7ac0 50 72 65 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 Prev==pOpen );.
c7ad0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e pOpen->pN
c7ae0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 ext->pPrev = pOp
c7af0 65 6e 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 en->pPrev;.
c7b00 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
c7b10 5f 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 _free(pOpen->aPe
c7b20 6e 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 73 71 nding);. sq
c7b30 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e lite3_free(pOpen
c7b40 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
c7b50 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 /*.** Given a fi
c7b60 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c 20 6c le descriptor, l
c7b70 6f 63 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e ocate unixLockIn
c7b80 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 fo and unixOpenC
c7b90 6e 74 20 73 74 72 75 63 74 75 72 65 73 20 74 68 nt structures th
c7ba0 61 74 0a 2a 2a 20 64 65 73 63 72 69 62 65 73 20 at.** describes
c7bb0 74 68 61 74 20 66 69 6c 65 20 64 65 73 63 72 69 that file descri
c7bc0 70 74 6f 72 2e 20 20 43 72 65 61 74 65 20 6e 65 ptor. Create ne
c7bd0 77 20 6f 6e 65 73 20 69 66 20 6e 65 63 65 73 73 w ones if necess
c7be0 61 72 79 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 ary. The.** ret
c7bf0 75 72 6e 20 76 61 6c 75 65 73 20 6d 69 67 68 74 urn values might
c7c00 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 be uninitialize
c7c10 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 d if an error oc
c7c20 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 curs..**.** Retu
c7c30 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 rn an appropriat
c7c40 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f e error code..*/
c7c50 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 .static int find
c7c60 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 LockInfo(. unix
c7c70 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 File *pFile,
c7c80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
c7c90 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c ix file with fil
c7ca0 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74 e desc used in t
c7cb0 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 he key */. stru
c7cc0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
c7cd0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 **ppLock, /* Re
c7ce0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 turn the unixLoc
c7cf0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 kInfo structure
c7d00 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 here */. struct
c7d10 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 unixOpenCnt **p
c7d20 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 pOpen /* Retu
c7d30 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 rn the unixOpenC
c7d40 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72 nt structure her
c7d50 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
c7d60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c7d70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74 /* Syst
c7d80 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 em call return c
c7d90 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b ode */. int fd;
c7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7db0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
c7dc0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
c7dd0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 or pFile */. st
c7de0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 ruct unixLockKey
c7df0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 lockKey; /*
c7e00 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 Lookup key for t
c7e10 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 he unixLockInfo
c7e20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 structure */. s
c7e30 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 truct unixFileId
c7e40 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a fileId; /*
c7e50 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 Lookup key for
c7e60 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 the unixOpenCnt
c7e70 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 struct */. stru
c7e80 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b ct stat statbuf;
c7e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
c7ea0 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 w-level file inf
c7eb0 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 ormation */. st
c7ec0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
c7ed0 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 o *pLock; /*
c7ee0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f Candidate unixLo
c7ef0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f ckInfo object */
c7f00 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
c7f10 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 enCnt *pOpen;
c7f20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 /* Candidate u
c7f30 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 nixOpenCnt objec
c7f40 74 20 2a 2f 0a 0a 20 20 2f 2a 20 47 65 74 20 6c t */.. /* Get l
c7f50 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 ow-level informa
c7f60 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
c7f70 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 ile that we can
c7f80 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 used to. ** cre
c7f90 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d ate a unique nam
c7fa0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a e for the file..
c7fb0 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c */. fd = pFil
c7fc0 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 e->h;. rc = fst
c7fd0 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 at(fd, &statbuf)
c7fe0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b ;. if( rc!=0 ){
c7ff0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
c8000 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 Errno = errno;.#
c8010 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a ifdef EOVERFLOW.
c8020 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c if( pFile->l
c8030 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 astErrno==EOVERF
c8040 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c LOW ) return SQL
c8050 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 ITE_NOLFS;.#endi
c8060 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c f. return SQL
c8070 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a ITE_IOERR;. }..
c8080 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f #ifdef __APPLE__
c8090 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e . /* On OS X on
c80a0 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 an msdos filesy
c80b0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 stem, the inode
c80c0 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 number is report
c80d0 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 ed. ** incorrec
c80e0 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a tly for zero-siz
c80f0 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 e files. See ti
c8100 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 cket #3260. To
c8110 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 work. ** around
c8120 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 this problem (w
c8130 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 e consider it a
c8140 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 bug in OS X, not
c8150 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 SQLite). ** we
c8160 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 always increase
c8170 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 the file size t
c8180 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 o 1 by writing a
c8190 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a single byte. *
c81a0 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 * prior to acces
c81b0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e sing the inode n
c81c0 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 umber. The one
c81d0 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a byte written is.
c81e0 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 ** an ASCII 'S
c81f0 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 ' character whic
c8200 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 h also happens t
c8210 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 o be the first b
c8220 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 yte. ** in the
c8230 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 header of every
c8240 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e SQLite database.
c8250 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 In this way, i
c8260 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 f there. ** is
c8270 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e a race condition
c8280 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 such that anoth
c8290 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c er thread has al
c82a0 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a ready populated.
c82b0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 ** the first p
c82c0 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
c82d0 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 ase, no damage i
c82e0 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 s done.. */. i
c82f0 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 f( statbuf.st_si
c8300 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 ze==0 ){. rc
c8310 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c = write(fd, "S",
c8320 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 1);. if( rc!
c8330 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =1 ){. retu
c8340 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
c8350 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
c8360 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 fstat(fd, &statb
c8370 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 uf);. if( rc!
c8380 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c =0 ){. pFil
c8390 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
c83a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 rrno;. retu
c83b0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
c83c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
c83d0 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 f.. memset(&loc
c83e0 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 kKey, 0, sizeof(
c83f0 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 lockKey));. loc
c8400 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 kKey.fid.dev = s
c8410 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 tatbuf.st_dev;.#
c8420 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
c8430 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 lockKey.fid.pId
c8440 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 = pFile->pId;.#e
c8450 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 lse. lockKey.fi
c8460 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e d.ino = statbuf.
c8470 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 st_ino;.#endif.#
c8480 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
c8490 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
c84a0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 __linux__). if(
c84b0 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 threadsOverride
c84c0 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c EachOthersLocks<
c84d0 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 0 ){. testThr
c84e0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
c84f0 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f or(fd);. }. lo
c8500 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 ckKey.tid = thre
c8510 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
c8520 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a thersLocks ? 0 :
c8530 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b pthread_self();
c8540 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 .#endif. fileId
c8550 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a = lockKey.fid;.
c8560 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 if( ppLock!=0
c8570 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c ){. pLock = l
c8580 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 ockList;. whi
c8590 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d le( pLock && mem
c85a0 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 cmp(&lockKey, &p
c85b0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 Lock->lockKey, s
c85c0 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 izeof(lockKey))
c85d0 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d ){. pLock =
c85e0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
c85f0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f }. if( pLo
c8600 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 ck==0 ){. p
c8610 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d Lock = sqlite3_m
c8620 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 alloc( sizeof(*p
c8630 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 Lock) );. i
c8640 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 f( pLock==0 ){.
c8650 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
c8660 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
c8670 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 goto exit_find
c8680 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 lockinfo;.
c8690 7d 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c }. pLock->l
c86a0 6f 63 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 ockKey = lockKey
c86b0 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e ;. pLock->n
c86c0 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 Ref = 1;. p
c86d0 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 Lock->cnt = 0;.
c86e0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
c86f0 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 type = 0;.
c8700 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c pLock->pNext = l
c8710 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 ockList;. p
c8720 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b Lock->pPrev = 0;
c8730 0a 20 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c . if( lockL
c8740 69 73 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e ist ) lockList->
c8750 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pPrev = pLock;.
c8760 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 lockList =
c8770 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 pLock;. }else
c8780 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e {. pLock->n
c8790 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 Ref++;. }.
c87a0 20 2a 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b *ppLock = pLock
c87b0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 ;. }. if( ppOp
c87c0 65 6e 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 en!=0 ){. pOp
c87d0 65 6e 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 en = openList;.
c87e0 20 20 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 while( pOpen
c87f0 26 26 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 && memcmp(&fileI
c8800 64 2c 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 d, &pOpen->fileI
c8810 64 2c 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 d, sizeof(fileId
c8820 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 )) ){. pOpe
c8830 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 n = pOpen->pNext
c8840 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
c8850 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 pOpen==0 ){.
c8860 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 pOpen = sqlite
c8870 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 3_malloc( sizeof
c8880 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 (*pOpen) );.
c8890 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 if( pOpen==0 )
c88a0 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 {. releas
c88b0 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 eLockInfo(pLock)
c88c0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 ;. rc = S
c88d0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
c88e0 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 goto exit_f
c88f0 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 indlockinfo;.
c8900 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 65 6e }. pOpen
c8910 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 ->fileId = fileI
c8920 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e d;. pOpen->
c8930 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 nRef = 1;.
c8940 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 pOpen->nLock = 0
c8950 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ;. pOpen->n
c8960 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 Pending = 0;.
c8970 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 pOpen->aPendi
c8980 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f ng = 0;. pO
c8990 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 pen->pNext = ope
c89a0 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4f 70 nList;. pOp
c89b0 65 6e 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 en->pPrev = 0;.
c89c0 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 if( openLis
c89d0 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 t ) openList->pP
c89e0 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 rev = pOpen;.
c89f0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
c8a00 70 65 6e 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f pen;.#if OS_VXWO
c8a10 52 4b 53 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d RKS. pOpen-
c8a20 3e 70 53 65 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 >pSem = NULL;.
c8a30 20 20 20 20 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e pOpen->aSemN
c8a40 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 ame[0] = '\0';.#
c8a50 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b endif. }else{
c8a60 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 . pOpen->nR
c8a70 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ef++;. }.
c8a80 2a 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b *ppOpen = pOpen;
c8a90 0a 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c . }..exit_findl
c8aa0 6f 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 ockinfo:. retur
c8ab0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 n rc;.}../*.** I
c8ac0 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 f we are current
c8ad0 6c 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e ly in a differen
c8ae0 74 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 t thread than th
c8af0 65 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 e thread that th
c8b00 65 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 e.** unixFile ar
c8b10 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 gument belongs t
c8b20 6f 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 o, then transfer
c8b30 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 ownership of th
c8b40 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 e unixFile.** ov
c8b50 65 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e er to the curren
c8b60 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 t thread..**.**
c8b70 41 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e A unixFile is on
c8b80 6c 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 ly owned by a th
c8b90 72 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 read on systems
c8ba0 74 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 that use LinuxTh
c8bb0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e reads..**.** Own
c8bc0 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 ership transfer
c8bd0 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 is only allowed
c8be0 69 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 if the unixFile
c8bf0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c is currently unl
c8c00 6f 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 ocked..** If the
c8c10 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 unixFile is loc
c8c20 6b 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 ked and an owner
c8c30 73 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 ship is wrong, t
c8c40 68 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 hen return.** SQ
c8c50 4c 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 LITE_MISUSE. SQ
c8c60 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
c8c70 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e ned if everythin
c8c80 67 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 g works..*/.#if
c8c90 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
c8ca0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c E && defined(__l
c8cb0 69 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 inux__).static i
c8cc0 6e 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 nt transferOwner
c8cd0 73 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 ship(unixFile *p
c8ce0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b File){. int rc;
c8cf0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 . pthread_t hSe
c8d00 6c 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 lf;. if( thread
c8d10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
c8d20 65 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 ersLocks ){.
c8d30 2f 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 /* Ownership tra
c8d40 6e 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 nsfers not neede
c8d50 64 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d d on this system
c8d60 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
c8d70 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
c8d80 68 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f hSelf = pthread_
c8d90 73 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 self();. if( pt
c8da0 68 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c hread_equal(pFil
c8db0 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 e->tid, hSelf) )
c8dc0 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 {. /* We are
c8dd0 73 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d still in the sam
c8de0 65 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 e thread */.
c8df0 4f 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 OSTRACE1("No-tra
c8e00 6e 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 nsfer, same thre
c8e10 61 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 ad\n");. retu
c8e20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
c8e30 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c }. if( pFile->l
c8e40 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
c8e50 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 ){. /* We ca
c8e60 6e 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 nnot change owne
c8e70 72 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 rship while we a
c8e80 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 re holding a loc
c8e90 6b 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e k! */. return
c8ea0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
c8eb0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 }. OSTRACE4("
c8ec0 54 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 Transfer ownersh
c8ed0 69 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 ip of %d from %d
c8ee0 20 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 to %d\n",.
c8ef0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c pFile->h,
c8f00 20 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 pFile->tid, hSe
c8f10 6c 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 lf);. pFile->ti
c8f20 64 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 d = hSelf;. if
c8f30 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d (pFile->pLock !=
c8f40 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c NULL) {. rel
c8f50 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 easeLockInfo(pFi
c8f60 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 le->pLock);.
c8f70 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 rc = findLockInf
c8f80 6f 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d o(pFile, &pFile-
c8f90 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 >pLock, 0);.
c8fa0 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 OSTRACE5("LOCK
c8fb0 20 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 %d is now %s(%
c8fc0 73 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d s,%d)\n", pFile-
c8fd0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c >h,. l
c8fe0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c ocktypeName(pFil
c8ff0 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 e->locktype),.
c9000 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
c9010 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f eName(pFile->pLo
c9020 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 ck->locktype), p
c9030 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 File->pLock->cnt
c9040 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
c9050 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
c9060 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c9070 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 K;. }.}.#else
c9080 2f 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 /* if not SQLITE
c9090 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 _THREADSAFE */.
c90a0 20 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 /* On single-th
c90b0 72 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f readed builds, o
c90c0 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 wnership transfe
c90d0 72 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a r is a no-op */.
c90e0 23 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 # define transfe
c90f0 72 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 rOwnership(X) SQ
c9100 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f LITE_OK.#endif /
c9110 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 * SQLITE_THREADS
c9120 41 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 AFE */.../*.** T
c9130 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
c9140 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ks if there is a
c9150 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 RESERVED lock h
c9160 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 eld on the speci
c9170 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 fied.** file by
c9180 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
c9190 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 r process. If su
c91a0 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c ch a lock is hel
c91b0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a d, set *pResOut.
c91c0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f ** to a non-zero
c91d0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 value otherwise
c91e0 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 *pResOut is set
c91f0 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 to zero. The r
c9200 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 eturn value.** i
c9210 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f s set to SQLITE_
c9220 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f OK unless an I/O
c9230 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
c9240 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 ring lock checki
c9250 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
c9260 74 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 t unixCheckReser
c9270 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
c9280 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
c9290 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 ResOut){. int r
c92a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
c92b0 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 int reserved =
c92c0 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 0;. unixFile *p
c92d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
c92e0 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 *)id;.. Simulat
c92f0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
c9300 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
c9310 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b ECKRESERVEDLOCK;
c9320 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
c9330 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e File );. unixEn
c9340 74 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 terMutex(); /* B
c9350 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c ecause pFile->pL
c9360 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 ock is shared ac
c9370 72 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a ross threads */.
c9380 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 . /* Check if a
c9390 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 thread in this
c93a0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 process holds su
c93b0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ch a lock */. i
c93c0 66 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d f( pFile->pLock-
c93d0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 >locktype>SHARED
c93e0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
c93f0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a erved = 1;. }..
c9400 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
c9410 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
c9420 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
c9430 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 t.. */.#ifndef
c9440 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 __DJGPP__. if(
c9450 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
c9460 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
c9470 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 ck;. lock.l_w
c9480 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
c9490 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 ;. lock.l_sta
c94a0 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 rt = RESERVED_BY
c94b0 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c TE;. lock.l_l
c94c0 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b en = 1;. lock
c94d0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 .l_type = F_WRLC
c94e0 4b 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d K;. if (-1 ==
c94f0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
c9500 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_GETLK, &lock)
c9510 29 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 ) {. int tE
c9520 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
c9530 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
c9540 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
c9550 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
c9560 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
c9570 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 ERVEDLOCK);.
c9580 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
c9590 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
c95a0 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b } else if( lock
c95b0 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b .l_type!=F_UNLCK
c95c0 20 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 ){. reserv
c95d0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ed = 1;. }.
c95e0 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e }.#endif. . un
c95f0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
c9600 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 OSTRACE4("TEST
c9610 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 WR-LOCK %d %d %
c9620 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c9630 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a rc, reserved);..
c9640 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 *pResOut = res
c9650 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 erved;. return
c9660 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 rc;.}../*.** Loc
c9670 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
c9680 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
c9690 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
c96a0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
c96b0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
c96c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
c96d0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
c96e0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
c96f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
c9700 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
c9710 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
c9720 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
c9730 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
c9740 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
c9750 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
c9760 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
c9770 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
c9780 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
c9790 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
c97a0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
c97b0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
c97c0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
c97d0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
c97e0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
c97f0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
c9800 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
c9810 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
c9820 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
c9830 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
c9840 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
c9850 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
c9860 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
c9870 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
c9880 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
c9890 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
c98a0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
c98b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
c98c0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
c98d0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
c98e0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
c98f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
c9900 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
c9910 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
c9920 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
c9930 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
c9940 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 lock. Use the
c9950 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
c9960 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 ).** routine to
c9970 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 lower a locking
c9980 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 level..*/.static
c9990 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 int unixLock(sq
c99a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
c99b0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
c99c0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
c99d0 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 g describes the
c99e0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
c99f0 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f f the various lo
c9a00 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 cks and. ** loc
c9a10 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e k transitions in
c9a20 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f terms of the PO
c9a30 53 49 58 20 61 64 76 69 73 6f 72 79 20 73 68 61 SIX advisory sha
c9a40 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 red and exclusiv
c9a50 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d e. ** lock prim
c9a60 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72 itives (called r
c9a70 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 ead-locks and wr
c9a80 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c ite-locks below,
c9a90 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 to avoid. ** c
c9aa0 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51 onfusion with SQ
c9ab0 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 Lite lock names)
c9ac0 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 . The algorithms
c9ad0 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 are complicated
c9ae0 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 . ** slightly i
c9af0 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 6f n order to be co
c9b00 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 77 69 mpatible with wi
c9b10 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73 69 ndows systems si
c9b20 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a multaneously. *
c9b30 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 * accessing the
c9b40 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 same database fi
c9b50 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68 61 74 le, in case that
c9b60 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 is ever require
c9b70 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d d.. **. ** Sym
c9b80 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 6e 20 bols defined in
c9b90 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 74 os.h indentify t
c9ba0 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 he 'pending byte
c9bb0 27 20 61 6e 64 20 74 68 65 20 27 72 65 73 65 72 ' and the 'reser
c9bc0 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 ved. ** byte',
c9bd0 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79 74 65 each single byte
c9be0 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 s at well known
c9bf0 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74 68 65 offsets, and the
c9c00 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
c9c10 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 61 6e ** range', a ran
c9c20 67 65 20 6f 66 20 35 31 30 20 62 79 74 65 73 20 ge of 510 bytes
c9c30 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 at a well known
c9c40 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a offset.. **. *
c9c50 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 * To obtain a SH
c9c60 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 ARED lock, a rea
c9c70 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e d-lock is obtain
c9c80 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 ed on the 'pendi
c9c90 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 ng. ** byte'.
c9ca0 49 66 20 74 68 69 73 20 69 73 20 73 75 63 63 65 If this is succe
c9cb0 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 ssful, a random
c9cc0 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20 27 73 byte from the 's
c9cd0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
c9ce0 72 61 6e 67 65 27 20 69 73 20 72 65 61 64 2d 6c range' is read-l
c9cf0 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 6c 6f ocked and the lo
c9d00 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 ck on the 'pendi
c9d10 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 73 65 ng byte' release
c9d20 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 d.. **. ** A p
c9d30 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 rocess may only
c9d40 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 obtain a RESERVE
c9d50 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 D lock after it
c9d60 68 61 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 has a SHARED loc
c9d70 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56 k.. ** A RESERV
c9d80 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 ED lock is imple
c9d90 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62 62 69 mented by grabbi
c9da0 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 ng a write-lock
c9db0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 on the. ** 'res
c9dc0 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20 erved byte'. .
c9dd0 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 **. ** A proces
c9de0 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 s may only obtai
c9df0 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b n a PENDING lock
c9e00 20 61 66 74 65 72 20 69 74 20 68 61 73 20 6f 62 after it has ob
c9e10 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 tained a. ** SH
c9e20 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e ARED lock. A PEN
c9e30 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 DING lock is imp
c9e40 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 lemented by obta
c9e50 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f ining a write-lo
c9e60 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 ck. ** on the '
c9e70 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54 pending byte'. T
c9e80 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
c9e90 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c no new SHARED l
c9ea0 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a ocks can be. **
c9eb0 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65 obtained, but e
c9ec0 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c xisting SHARED l
c9ed0 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 ocks are allowed
c9ee0 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70 to persist. A p
c9ef0 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 rocess. ** does
c9f00 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74 not have to obt
c9f10 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c ain a RESERVED l
c9f20 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74 ock on the way t
c9f30 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b o a PENDING lock
c9f40 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 .. ** This prop
c9f50 65 72 74 79 20 69 73 20 75 73 65 64 20 62 79 20 erty is used by
c9f60 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f the algorithm fo
c9f70 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 r rolling back a
c9f80 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 journal file.
c9f90 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61 73 68 ** after a crash
c9fa0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 .. **. ** An E
c9fb0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f XCLUSIVE lock, o
c9fc0 62 74 61 69 6e 65 64 20 61 66 74 65 72 20 61 20 btained after a
c9fd0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 PENDING lock is
c9fe0 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d held, is. ** im
c9ff0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 plemented by obt
ca000 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c aining a write-l
ca010 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 ock on the entir
ca020 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 e 'shared byte.
ca030 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 ** range'. Sinc
ca040 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b e all other lock
ca050 73 20 72 65 71 75 69 72 65 20 61 20 72 65 61 64 s require a read
ca060 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 -lock on one of
ca070 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 the bytes. ** w
ca080 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65 ithin this range
ca090 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 , this ensures t
ca0a0 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 hat no other loc
ca0b0 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 ks are held on t
ca0c0 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 he. ** database
ca0d0 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 . . **. ** The
ca0e0 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 reason a single
ca0f0 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 byte cannot be
ca100 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 used instead of
ca110 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 the 'shared byte
ca120 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 . ** range' is
ca130 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f that some versio
ca140 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f ns of windows do
ca150 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 not support rea
ca160 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a d-locks. By. **
ca170 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f locking a rando
ca180 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61 m byte from a ra
ca190 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 nge, concurrent
ca1a0 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 SHARED locks may
ca1b0 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e exist. ** even
ca1c0 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 if the locking
ca1d0 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 20 69 primitive used i
ca1e0 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 74 65 s always a write
ca1f0 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e -lock.. */. in
ca200 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
ca210 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
ca220 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
ca230 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20 75 6e )id;. struct un
ca240 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 ixLockInfo *pLoc
ca250 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b k = pFile->pLock
ca260 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b ;. struct flock
ca270 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a lock;. int s;.
ca280 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
ca290 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 );. OSTRACE7("
ca2a0 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 LOCK %d %s wa
ca2b0 73 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d s %s(%s,%d) pid=
ca2c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
ca2d0 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e . locktypeN
ca2e0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c ame(locktype), l
ca2f0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c ocktypeName(pFil
ca300 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 e->locktype),.
ca310 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
ca320 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 (pLock->locktype
ca330 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 ), pLock->cnt ,
ca340 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a getpid());.. /*
ca350 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 If there is alr
ca360 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 eady a lock of t
ca370 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 his type or more
ca380 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 restrictive on
ca390 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c the. ** unixFil
ca3a0 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 e, do nothing. D
ca3b0 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 on't use the end
ca3c0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 _lock: exit path
ca3d0 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e , as. ** unixEn
ca3e0 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 terMutex() hasn'
ca3f0 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 t been called ye
ca400 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 t.. */. if( pF
ca410 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c ile->locktype>=l
ca420 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f ocktype ){. O
ca430 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 STRACE3("LOCK
ca440 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 %d %s ok (alrea
ca450 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 dy held)\n", pFi
ca460 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
ca470 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
ca480 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 locktype));.
ca490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ca4a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
ca4b0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e sure the lockin
ca4c0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f g sequence is co
ca4d0 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 rrect. */. ass
ca4e0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ert( pFile->lock
ca4f0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c type!=NO_LOCK ||
ca500 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
ca510 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
ca520 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 rt( locktype!=PE
ca530 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 NDING_LOCK );.
ca540 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
ca550 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 !=RESERVED_LOCK
ca560 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 || pFile->lockty
ca570 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
ca580 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 );.. /* This mu
ca590 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 tex is needed be
ca5a0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f cause pFile->pLo
ca5b0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 ck is shared acr
ca5c0 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f oss threads. */
ca5d0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
ca5e0 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 x();.. /* Make
ca5f0 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 sure the current
ca600 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 thread owns the
ca610 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 pFile.. */. r
ca620 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 c = transferOwne
ca630 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 rship(pFile);.
ca640 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
ca650 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 K ){. unixLea
ca660 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 veMutex();. r
ca670 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
ca680 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 pLock = pFile->p
ca690 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 Lock;.. /* If s
ca6a0 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 ome thread using
ca6b0 20 74 68 69 73 20 50 49 44 20 68 61 73 20 61 20 this PID has a
ca6c0 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 lock via a diffe
ca6d0 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 rent unixFile*.
ca6e0 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 ** handle that
ca6f0 70 72 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 precludes the re
ca700 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 quested lock, re
ca710 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a turn BUSY.. */.
ca720 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f if( (pFile->lo
ca730 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c cktype!=pLock->l
ca740 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 ocktype && .
ca750 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f (pLock->lo
ca760 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f cktype>=PENDING_
ca770 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 LOCK || locktype
ca780 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 >SHARED_LOCK)).
ca790 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
ca7a0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f ITE_BUSY;. go
ca7b0 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d to end_lock;. }
ca7c0 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 .. /* If a SHAR
ca7d0 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 ED lock is reque
ca7e0 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 sted, and some t
ca7f0 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 hread using this
ca800 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a PID already. *
ca810 2a 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6f * has a SHARED o
ca820 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c r RESERVED lock,
ca830 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 then increment
ca840 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 reference counts
ca850 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e and. ** return
ca860 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f SQLITE_OK.. */
ca870 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
ca880 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
ca890 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c . (pLock->l
ca8a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
ca8b0 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c LOCK || pLock->l
ca8c0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 ocktype==RESERVE
ca8d0 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 D_LOCK) ){. a
ca8e0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d ssert( locktype=
ca8f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
ca900 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
ca910 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 e->locktype==0 )
ca920 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c ;. assert( pL
ca930 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 ock->cnt>0 );.
ca940 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
ca950 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
ca960 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b . pLock->cnt+
ca970 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f +;. pFile->pO
ca980 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 pen->nLock++;.
ca990 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b goto end_lock;
ca9a0 0a 20 20 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c . }.. lock.l_l
ca9b0 65 6e 20 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b en = 1L;.. lock
ca9c0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
ca9d0 5f 53 45 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 _SET;.. /* A PE
ca9e0 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 NDING lock is ne
ca9f0 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 eded before acqu
caa00 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c iring a SHARED l
caa10 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 ock and before.
caa20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e ** acquiring an
caa30 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
caa40 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 For the SHARED
caa50 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 lock, the PENDI
caa60 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 NG will. ** be
caa70 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 released.. */.
caa80 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
caa90 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 HARED_LOCK .
caaa0 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d || (locktype==
caab0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 EXCLUSIVE_LOCK &
caac0 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 & pFile->locktyp
caad0 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a e<PENDING_LOCK).
caae0 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f ){. lock.l_
caaf0 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 type = (locktype
cab00 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f ==SHARED_LOCK?F_
cab10 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a RDLCK:F_WRLCK);.
cab20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
cab30 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b = PENDING_BYTE;
cab40 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 . s = fcntl(p
cab50 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b File->h, F_SETLK
cab60 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 , &lock);. if
cab70 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 ( s==(-1) ){.
cab80 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
cab90 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
caba0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
cabb0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
cabc0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
cabd0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 _LOCK);. if
cabe0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
cabf0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 rc) ){. p
cac00 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
cac10 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
cac20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 }. goto end
cac30 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d _lock;. }. }
cac40 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 ... /* If contr
cac50 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 ol gets to this
cac60 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 point, then actu
cac70 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e ally go ahead an
cac80 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 d make. ** oper
cac90 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c ating system cal
caca0 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 ls for the speci
cacb0 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a fied lock.. */.
cacc0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
cacd0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
cace0 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
cacf0 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 0;. assert( p
cad00 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a Lock->cnt==0 );.
cad10 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
cad20 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 k->locktype==0 )
cad30 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 ;.. /* Now ge
cad40 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 t the read-lock
cad50 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 */. lock.l_st
cad60 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 art = SHARED_FIR
cad70 53 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c ST;. lock.l_l
cad80 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 en = SHARED_SIZE
cad90 3b 0a 20 20 20 20 69 66 28 20 28 73 20 3d 20 66 ;. if( (s = f
cada0 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 cntl(pFile->h, F
cadb0 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d _SETLK, &lock))=
cadc0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 74 =(-1) ){. t
cadd0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
cade0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 }. /* Drop
cadf0 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 the temporary P
cae00 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 ENDING lock */.
cae10 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
cae20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
cae30 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
cae40 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 1L;. lock.l_
cae50 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a type = F_UNLCK;.
cae60 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 if( fcntl(pF
cae70 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c ile->h, F_SETLK,
cae80 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 &lock)!=0 ){.
cae90 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20 if( s != -1
caea0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
caeb0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 is could happen
caec0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d with a network m
caed0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 ount */.
caee0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 tErrno = errno;
caef0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
caf00 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
caf10 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
caf20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
caf30 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 OCK); . i
caf40 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
caf50 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
caf60 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
caf70 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
caf80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 }. g
caf90 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
cafa0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
cafb0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 if( s==(-1) ){.
cafc0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
cafd0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
cafe0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
caff0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
cb000 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
cb010 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
cb020 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
cb030 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
cb040 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
cb050 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 lse{. pFile
cb060 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
cb070 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 RED_LOCK;.
cb080 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
cb090 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f ock++;. pLo
cb0a0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 ck->cnt = 1;.
cb0b0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c }. }else if( l
cb0c0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
cb0d0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b VE_LOCK && pLock
cb0e0 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f ->cnt>1 ){. /
cb0f0 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20 * We are trying
cb100 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 for an exclusive
cb110 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 lock but anothe
cb120 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 r thread in this
cb130 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f . ** same pro
cb140 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f cess is still ho
cb150 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c lding a shared l
cb160 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d ock. */. rc =
cb170 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
cb180 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
cb190 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f e request was fo
cb1a0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 r a RESERVED or
cb1b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
cb1c0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 It is. ** as
cb1d0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 sumed that there
cb1e0 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 is a SHARED or
cb1f0 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
cb200 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
cb210 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
cb220 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 assert( 0!=p
cb230 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
cb240 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
cb250 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 e = F_WRLCK;.
cb260 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 switch( locktyp
cb270 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
cb280 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 RESERVED_LOCK:.
cb290 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 lock.l_st
cb2a0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 art = RESERVED_B
cb2b0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65 YTE;. bre
cb2c0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45 ak;. case E
cb2d0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 XCLUSIVE_LOCK:.
cb2e0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 lock.l_st
cb2f0 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 art = SHARED_FIR
cb300 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b ST;. lock
cb310 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f .l_len = SHARED_
cb320 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 62 72 SIZE;. br
cb330 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 eak;. defau
cb340 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65 lt:. asse
cb350 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 rt(0);. }.
cb360 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 s = fcntl(pFile
cb370 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
cb380 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d ock);. if( s=
cb390 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69 =(-1) ){. i
cb3a0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
cb3b0 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
cb3c0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
cb3d0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
cb3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
cb3f0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 K);. if( IS
cb400 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
cb410 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
cb420 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
cb430 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rrno;. }.
cb440 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e }. }. ..#ifn
cb450 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 def NDEBUG. /*
cb460 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73 Set up the trans
cb470 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 action-counter c
cb480 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66 hange checking f
cb490 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 lags when. ** t
cb4a0 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f ransitioning fro
cb4b0 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20 m a SHARED to a
cb4c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 RESERVED lock.
cb4d0 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 The change. **
cb4e0 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52 from SHARED to R
cb4f0 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68 ESERVED marks th
cb500 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 e beginning of a
cb510 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 normal. ** wri
cb520 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f te operation (no
cb530 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 t a hot journal
cb540 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a rollback).. */.
cb550 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
cb560 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d _OK. && pFile-
cb570 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 >locktype<=SHARE
cb580 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 D_LOCK. && loc
cb590 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
cb5a0 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 LOCK. ){. pF
cb5b0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 ile->transCntrCh
cb5c0 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c ng = 0;. pFil
cb5d0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b e->dbUpdate = 0;
cb5e0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f . pFile->inNo
cb5f0 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 rmalWrite = 1;.
cb600 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 }.#endif... if
cb610 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
cb620 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f ){. pFile->lo
cb630 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
cb640 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f e;. pLock->lo
cb650 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
cb660 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c e;. }else if( l
cb670 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
cb680 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 VE_LOCK ){. p
cb690 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
cb6a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
cb6b0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
cb6c0 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 pe = PENDING_LOC
cb6d0 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b K;. }..end_lock
cb6e0 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 :. unixLeaveMut
cb6f0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 ex();. OSTRACE4
cb700 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
cb710 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %s\n", pFile->h,
cb720 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
cb730 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 cktype), .
cb740 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 rc==SQLITE_OK ?
cb750 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
cb760 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
cb770 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
cb780 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
cb790 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
cb7a0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b or pFile to lock
cb7b0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
cb7c0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
cb7d0 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
cb7e0 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
cb7f0 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
cb800 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
cb810 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
cb820 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
cb830 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
cb840 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
cb850 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
cb860 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
cb870 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e tatic int unixUn
cb880 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
cb890 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
cb8a0 79 70 65 29 7b 0a 20 20 73 74 72 75 63 74 20 75 ype){. struct u
cb8b0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
cb8c0 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f ck;. struct flo
cb8d0 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 ck lock;. int r
cb8e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
cb8f0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
cb900 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
cb910 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20 61 73 ;. int h;.. as
cb920 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
cb930 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 OSTRACE7("UNLOC
cb940 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28 K %d %d was %d(
cb950 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 %d,%d) pid=%d\n"
cb960 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
cb970 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 69 6c type,. pFil
cb980 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 e->locktype, pFi
cb990 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 le->pLock->lockt
cb9a0 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ype, pFile->pLoc
cb9b0 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 28 29 k->cnt, getpid()
cb9c0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );.. assert( lo
cb9d0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
cb9e0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69 OCK );. if( pFi
cb9f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f le->locktype<=lo
cba00 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 cktype ){. re
cba10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
cba20 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 4b 5f }. if( CHECK_
cba30 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 29 20 THREADID(pFile)
cba40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
cba50 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
cba60 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
cba70 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 x();. h = pFile
cba80 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 ->h;. pLock = p
cba90 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 File->pLock;. a
cbaa0 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e ssert( pLock->cn
cbab0 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 t!=0 );. if( pF
cbac0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
cbad0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
cbae0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
cbaf0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d locktype==pFile-
cbb00 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
cbb10 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
cbb20 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 Benign(1);. S
cbb30 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
cbb40 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d h=(-1) ). Sim
cbb50 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
cbb60 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 gn(0);..#ifndef
cbb70 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 NDEBUG. /* Wh
cbb80 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 6c 6f en reducing a lo
cbb90 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f 74 68 ck such that oth
cbba0 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e er processes can
cbbb0 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 start. ** re
cbbc0 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
cbbd0 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d se file again, m
cbbe0 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
cbbf0 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 e. ** transac
cbc00 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 tion counter was
cbc10 20 75 70 64 61 74 65 64 20 69 66 20 61 6e 79 20 updated if any
cbc20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
cbc30 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 base. ** file
cbc40 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 74 68 changed. If th
cbc50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
cbc60 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 70 64 unter is not upd
cbc70 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 ated,. ** oth
cbc80 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 er connections t
cbc90 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 o the same file
cbca0 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a might not realiz
cbcb0 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 e that. ** th
cbcc0 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 e file has chang
cbcd0 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 ed and hence mig
cbce0 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 ht not know to f
cbcf0 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 20 2a lush their. *
cbd00 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 75 73 * cache. The us
cbd10 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 61 63 e of a stale cac
cbd20 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 he can lead to d
cbd30 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
cbd40 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 on.. */. a
cbd50 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e ssert( pFile->in
cbd60 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 NormalWrite==0.
cbd70 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 || pFile
cbd80 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 ->dbUpdate==0.
cbd90 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d || pFile-
cbda0 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d >transCntrChng==
cbdb0 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 1 );. pFile->
cbdc0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 inNormalWrite =
cbdd0 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 0;.#endif...
cbde0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
cbdf0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
cbe00 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
cbe10 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20 20 20 F_RDLCK;.
cbe20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
cbe30 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 SEEK_SET;.
cbe40 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 lock.l_start = S
cbe50 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 HARED_FIRST;.
cbe60 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
cbe70 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 SHARED_SIZE;.
cbe80 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 if( fcntl(h,
cbe90 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d F_SETLK, &lock)=
cbea0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 =(-1) ){.
cbeb0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
cbec0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 rno;. rc
cbed0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
cbee0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
cbef0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
cbf00 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 _RDLOCK);.
cbf10 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
cbf20 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
cbf30 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
cbf40 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
cbf50 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
cbf60 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
cbf70 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
cbf80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
cbf90 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
cbfa0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
cbfb0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f SEEK_SET;. lo
cbfc0 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e ck.l_start = PEN
cbfd0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c DING_BYTE;. l
cbfe0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 ock.l_len = 2L;
cbff0 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 assert( PENDING
cc000 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 _BYTE+1==RESERVE
cc010 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 D_BYTE );. if
cc020 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 ( fcntl(h, F_SET
cc030 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 LK, &lock)!=(-1)
cc040 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d ){. pLock-
cc050 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 >locktype = SHAR
cc060 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c ED_LOCK;. }el
cc070 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 se{. int tE
cc080 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
cc090 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
cc0a0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
cc0b0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
cc0c0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
cc0d0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
cc0e0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
cc0f0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
cc100 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
cc110 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 o;. }.
cc120 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b goto end_unlock
cc130 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
cc140 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c ( locktype==NO_L
cc150 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 63 OCK ){. struc
cc160 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 t unixOpenCnt *p
cc170 4f 70 65 6e 3b 0a 20 20 20 20 69 6e 74 20 72 63 Open;. int rc
cc180 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 2 = SQLITE_OK;..
cc190 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 /* Decrement
cc1a0 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b the shared lock
cc1b0 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 counter. Relea
cc1c0 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e se the lock usin
cc1d0 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63 g an. ** OS c
cc1e0 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c all only when al
cc1f0 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 68 69 l threads in thi
cc200 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68 s same process h
cc210 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 ave released.
cc220 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 ** the lock..
cc230 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e */. pLock->
cc240 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 cnt--;. if( p
cc250 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a Lock->cnt==0 ){.
cc260 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 lock.l_typ
cc270 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 e = F_UNLCK;.
cc280 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 lock.l_whence
cc290 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 = SEEK_SET;.
cc2a0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
cc2b0 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 = lock.l_len = 0
cc2c0 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 L;. Simulat
cc2d0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 eIOErrorBenign(1
cc2e0 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 );. Simulat
cc2f0 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 eIOError( h=(-1)
cc300 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 ). Simulat
cc310 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 eIOErrorBenign(0
cc320 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 63 6e );. if( fcn
cc330 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 tl(h, F_SETLK, &
cc340 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 lock)!=(-1) ){.
cc350 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f pLock->lo
cc360 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b cktype = NO_LOCK
cc370 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
cc380 20 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e int tErrn
cc390 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
cc3a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
cc3b0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
cc3c0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
cc3d0 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
cc3e0 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c if( IS_L
cc3f0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
cc400 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
cc410 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
cc420 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a rrno;. }.
cc430 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c pLock->l
cc440 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
cc450 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 K;. pFile
cc460 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f ->locktype = NO_
cc470 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 LOCK;. }.
cc480 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 }.. /* Decr
cc490 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 ement the count
cc4a0 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 of locks against
cc4b0 20 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e this same file.
cc4c0 20 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a When the. *
cc4d0 2a 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 * count reaches
cc4e0 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 zero, close any
cc4f0 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 other file descr
cc500 69 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f iptors whose clo
cc510 73 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65 se. ** was de
cc520 66 65 72 72 65 64 20 62 65 63 61 75 73 65 20 6f ferred because o
cc530 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f f outstanding lo
cc540 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 cks.. */.
cc550 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 pOpen = pFile->p
cc560 4f 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d Open;. pOpen-
cc570 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 >nLock--;. as
cc580 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f sert( pOpen->nLo
cc590 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ck>=0 );. if(
cc5a0 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 pOpen->nLock==0
cc5b0 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 && pOpen->nPend
cc5c0 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 ing>0 ){. i
cc5d0 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 nt i;. for(
cc5e0 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 i=0; i<pOpen->nP
cc5f0 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 ending; i++){.
cc600 20 20 20 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70 /* close p
cc610 65 6e 64 69 6e 67 20 66 64 73 2c 20 62 75 74 20 ending fds, but
cc620 69 66 20 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73 if closing fails
cc630 20 64 6f 6e 27 74 20 66 72 65 65 20 74 68 65 20 don't free the
cc640 61 72 72 61 79 0a 20 20 20 20 20 20 20 20 2a 2a array. **
cc650 20 61 73 73 69 67 6e 20 2d 31 20 74 6f 20 74 68 assign -1 to th
cc660 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 e successfully c
cc670 6c 6f 73 65 64 20 64 65 73 63 72 69 70 74 6f 72 losed descriptor
cc680 73 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65 s and record the
cc690 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f . ** erro
cc6a0 72 2e 20 20 54 68 65 20 6e 65 78 74 20 61 74 74 r. The next att
cc6b0 65 6d 70 74 20 74 6f 20 75 6e 6c 6f 63 6b 20 77 empt to unlock w
cc6c0 69 6c 6c 20 74 72 79 20 61 67 61 69 6e 2e 20 2a ill try again. *
cc6d0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f /. if( pO
cc6e0 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d pen->aPending[i]
cc6f0 20 3c 20 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b < 0 ) continue;
cc700 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f . if( clo
cc710 73 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 se(pOpen->aPendi
cc720 6e 67 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 ng[i]) ){.
cc730 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
cc740 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
cc750 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 53 51 rc2 = SQ
cc760 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 LITE_IOERR_CLOSE
cc770 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
cc780 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e . pOpen
cc790 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 ->aPending[i] =
cc7a0 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 -1;. }.
cc7b0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
cc7c0 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc2==SQLITE_OK )
cc7d0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
cc7e0 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 3_free(pOpen->aP
cc7f0 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 ending);.
cc800 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 pOpen->nPending
cc810 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4f = 0;. pO
cc820 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 pen->aPending =
cc830 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
cc840 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
cc850 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
cc860 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a rc = rc2;. }.
cc870 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b }...end_unlock
cc880 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 :. unixLeaveMut
cc890 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ex();. if( rc==
cc8a0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c SQLITE_OK ) pFil
cc8b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
cc8c0 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e cktype;. return
cc8d0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
cc8e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 is function perf
cc8f0 6f 72 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f orms the parts o
cc900 66 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c f the "close fil
cc910 65 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a e" operation .**
cc920 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c common to all l
cc930 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 ocking schemes.
cc940 49 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69 It closes the di
cc950 72 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 rectory and file
cc960 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 .** handles, if
cc970 74 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 they are valid,
cc980 61 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 and sets all fie
cc990 6c 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 lds of the unixF
cc9a0 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ile.** structure
cc9b0 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 to 0..**.** It
cc9c0 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 is *not* necessa
cc9d0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d ry to hold the m
cc9e0 75 74 65 78 20 77 68 65 6e 20 74 68 69 73 20 72 utex when this r
cc9f0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
cca00 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 ,.** even on VxW
cca10 6f 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77 orks. A mutex w
cca20 69 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64 20 ill be acquired
cca30 6f 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68 on VxWorks by th
cca40 65 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 e.** vxworksRele
cca50 61 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74 aseFileId() rout
cca60 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ine..*/.static i
cca70 6e 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 nt closeUnixFile
cca80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
cca90 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a d){. unixFile *
ccaa0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
ccab0 65 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69 e*)id;. if( pFi
ccac0 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46 le ){. if( pF
ccad0 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b ile->dirfd>=0 ){
ccae0 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d . int err =
ccaf0 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 close(pFile->di
ccb00 72 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 rfd);. if(
ccb10 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 err ){. p
ccb20 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
ccb30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
ccb40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
ccb50 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a OERR_DIR_CLOSE;.
ccb60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
ccb70 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 pFile->dirf
ccb80 64 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 d=-1;. }.
ccb90 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c }. if( pFil
ccba0 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 e->h>=0 ){.
ccbb0 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 int err = close
ccbc0 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 (pFile->h);.
ccbd0 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 if( err ){.
ccbe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
ccbf0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
ccc00 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
ccc10 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 LITE_IOERR_CLOSE
ccc20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
ccc30 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
ccc40 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 if( pFile->pI
ccc50 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 d ){. if( p
ccc60 46 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 File->isDelete )
ccc70 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b {. unlink
ccc80 28 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 (pFile->pId->zCa
ccc90 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 nonicalName);.
ccca0 20 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f }. vxwo
cccb0 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 rksReleaseFileId
cccc0 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 (pFile->pId);.
cccd0 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d pFile->pId =
ccce0 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0;. }.#endif
cccf0 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 . OSTRACE2("C
ccd00 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 LOSE %-3d\n",
ccd10 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f pFile->h);. O
ccd20 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a penCounter(-1);.
ccd30 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 memset(pFile
ccd40 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 , 0, sizeof(unix
ccd50 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 File));. }. re
ccd60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ccd70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
ccd80 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
ccd90 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 int unixClose(s
ccda0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
ccdb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
ccdc0 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 ITE_OK;. if( id
ccdd0 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 ){. unixFile
ccde0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
ccdf0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e ile *)id;. un
cce00 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ixUnlock(id, NO_
cce10 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 LOCK);. unixE
cce20 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
cce30 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 if( pFile->pOpe
cce40 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 n && pFile->pOpe
cce50 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 n->nLock ){.
cce60 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
cce70 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f e outstanding lo
cce80 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 cks, do not actu
cce90 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 ally close the f
ccea0 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a ile just. *
cceb0 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68 * yet because th
ccec0 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 at would clear t
cced0 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 hose locks. Ins
ccee0 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 tead, add the fi
ccef0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 le. ** desc
ccf00 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d riptor to pOpen-
ccf10 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 >aPending. It w
ccf20 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 ill be automatic
ccf30 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e ally closed when
ccf40 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 . ** the la
ccf50 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 st lock is clear
ccf60 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
ccf70 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 int *aNew;.
ccf80 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f struct unixO
ccf90 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 penCnt *pOpen =
ccfa0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 pFile->pOpen;.
ccfb0 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 aNew = sqlit
ccfc0 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70 65 6e e3_realloc(pOpen
ccfd0 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70 4f 70 ->aPending, (pOp
ccfe0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 2a en->nPending+1)*
ccff0 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b 0a 20 sizeof(int) );.
cd000 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 if( aNew==0
cd010 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
cd020 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 f a malloc fails
cd030 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68 65 20 , just leak the
cd040 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
cd050 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a */. }else{.
cd060 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 pOpen->a
cd070 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 3b 0a Pending = aNew;.
cd080 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 pOpen->a
cd090 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e Pending[pOpen->n
cd0a0 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65 Pending] = pFile
cd0b0 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 ->h;. pOp
cd0c0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a en->nPending++;.
cd0d0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 pFile->h
cd0e0 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 = -1;. }.
cd0f0 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 }. release
cd100 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e LockInfo(pFile->
cd110 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 pLock);. rele
cd120 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 aseOpenCnt(pFile
cd130 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 ->pOpen);. rc
cd140 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 = closeUnixFile
cd150 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 (id);. unixLe
cd160 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
cd170 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
cd180 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
cd190 45 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78 End of the posix
cd1a0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 advisory lock i
cd1b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a mplementation **
cd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
cd1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
cd220 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
cd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd270 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
cd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
cd290 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a No-op Locking **
cd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd2c0 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 .**.** Of the va
cd2d0 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d rious locking im
cd2e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 plementations av
cd2f0 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 ailable, this is
cd300 20 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 by far the.** s
cd310 69 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e implest: lockin
cd320 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e g is ignored. N
cd330 6f 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 o attempt is mad
cd340 65 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 e to lock the da
cd350 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 tabase.** file f
cd360 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 or reading or wr
cd370 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 iting..**.** Thi
cd380 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 s locking mode i
cd390 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f s appropriate fo
cd3a0 72 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e r use on read-on
cd3b0 6c 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 ly databases.**
cd3c0 28 65 78 3a 20 64 61 74 61 62 61 73 65 73 20 74 (ex: databases t
cd3d0 68 61 74 20 61 72 65 20 62 75 72 6e 65 64 20 69 hat are burned i
cd3e0 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 nto CD-ROM, for
cd3f0 65 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 example.) It ca
cd400 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 n.** also be use
cd410 64 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 d if the applica
cd420 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d tion employs som
cd430 65 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 e external mecha
cd440 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 nism to.** preve
cd450 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 nt simultaneous
cd460 61 63 63 65 73 73 20 6f 66 20 74 68 65 20 73 61 access of the sa
cd470 6d 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 me database by t
cd480 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 wo or more.** da
cd490 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
cd4a0 6e 73 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 ns. But there i
cd4b0 73 20 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b s a serious risk
cd4c0 20 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 of database.**
cd4d0 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 corruption if th
cd4e0 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 is locking mode
cd4f0 69 73 20 75 73 65 64 20 69 6e 20 73 69 74 75 61 is used in situa
cd500 74 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 tions where mult
cd510 69 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 iple.** database
cd520 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 connections are
cd530 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 accessing the s
cd540 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ame database fil
cd550 65 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a e at the same.**
cd560 20 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 time and one or
cd570 20 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63 more of those c
cd580 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77 onnections are w
cd590 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 riting..*/..stat
cd5a0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 ic int nolockChe
cd5b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
cd5c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
cd5d0 55 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f Used, int *pResO
cd5e0 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ut){. UNUSED_PA
cd5f0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
cd600 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 ;. *pResOut = 0
cd610 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
cd620 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 E_OK;.}.static i
cd630 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 nt nolockLock(sq
cd640 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 lite3_file *NotU
cd650 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 sed, int NotUsed
cd660 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 2){. UNUSED_PAR
cd670 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
cd680 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 NotUsed2);. re
cd690 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
cd6a0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c }.static int nol
cd6b0 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 ockUnlock(sqlite
cd6c0 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 3_file *NotUsed,
cd6d0 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a int NotUsed2){.
cd6e0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
cd6f0 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
cd700 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e Used2);. return
cd710 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
cd720 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 *.** Close the f
cd730 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
cd740 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 nt nolockClose(s
cd750 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
cd760 20 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 {. return clos
cd770 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d eUnixFile(id);.}
cd780 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
cd790 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ****** End of th
cd7a0 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 e no-op lock imp
cd7b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
cd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd7d0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
cd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd820 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
cd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd870 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
cd880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
cd890 69 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b in dot-file Lock
cd8a0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ing ************
cd8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd8c0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 **.**.** The dot
cd8d0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 file locking imp
cd8e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 lementation uses
cd8f0 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6f 66 the existing of
cd900 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a separate lock.*
cd910 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 * files in order
cd920 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 to control acce
cd930 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ss to the databa
cd940 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 se. This works
cd950 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 on just.** about
cd960 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65 every filesyste
cd970 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 m imaginable. B
cd980 75 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72 ut there are ser
cd990 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a ious downsides:.
cd9a0 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 **.** (1) Th
cd9b0 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 ere is zero conc
cd9c0 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 urrency. A sing
cd9d0 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 le reader blocks
cd9e0 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 all other.**
cd9f0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e connection
cda00 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f s from reading o
cda10 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 r writing the da
cda20 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 tabase..**.**
cda30 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61 (2) An applica
cda40 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f tion crash or po
cda50 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 wer loss can lea
cda60 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 ve stale lock fi
cda70 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 les.** s
cda80 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 itting around th
cda90 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c at need to be cl
cdaa0 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a eared manually..
cdab0 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 **.** Neverthele
cdac0 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 ss, a dotlock is
cdad0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
cdae0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 locking mode for
cdaf0 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 use if no.** ot
cdb00 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 her locking stra
cdb10 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c tegy is availabl
cdb20 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 e..**.** Dotfile
cdb30 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 locking works b
cdb40 79 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c y creating a fil
cdb50 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 e in the same di
cdb60 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a rectory as the.*
cdb70 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 * database and w
cdb80 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d ith the same nam
cdb90 65 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c e but with a ".l
cdba0 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 ock" extension a
cdbb0 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 dded..** The exi
cdbc0 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b stance of a lock
cdbd0 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e file implies an
cdbe0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
cdbf0 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b All other lock
cdc00 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45 .** types (SHARE
cdc10 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e D, RESERVED, PEN
cdc20 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64 DING) are mapped
cdc30 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e into EXCLUSIVE.
cdc40 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .*/../*.** The f
cdc50 69 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64 ile suffix added
cdc60 20 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73 to the data bas
cdc70 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 e filename in or
cdc80 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 der to create th
cdc90 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a e.** lock file..
cdca0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f */.#define DOTLO
cdcb0 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b CK_SUFFIX ".lock
cdcc0 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f "../*.** This ro
cdcd0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
cdce0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
cdcf0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
cdd00 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
cdd10 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
cdd20 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
cdd30 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
cdd40 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
cdd50 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
cdd60 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
cdd70 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
cdd80 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
cdd90 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
cdda0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
cddb0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
cddc0 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
cddd0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
cdde0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a ock checking..**
cddf0 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c .** In dotfile l
cde00 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61 ocking, either a
cde10 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20 lock exists or
cde20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f it does not. So
cde30 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 in this.** vari
cde40 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 ation of CheckRe
cde50 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 servedLock(), *p
cde60 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
cde70 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 true if any loc
cde80 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 k.** is held on
cde90 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c the file and fal
cdea0 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 se if the file i
cdeb0 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 s unlocked..*/.s
cdec0 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 tatic int dotloc
cded0 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
cdee0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
cdef0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
cdf00 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t) {. int rc =
cdf10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
cdf20 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
cdf30 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
cdf40 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
cdf50 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 ;.. SimulateIOE
cdf60 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
cdf70 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
cdf80 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
cdf90 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
cdfa0 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 le );.. /* Chec
cdfb0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
cdfc0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
cdfd0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
cdfe0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
cdff0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
ce000 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 LOCK ){. /* E
ce010 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 ither this conne
ce020 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 ction or some ot
ce030 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 her connection i
ce040 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 n the same proce
ce050 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 ss. ** holds
ce060 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 a lock on the fi
ce070 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 le. No need to
ce080 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a check further. *
ce090 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d /. reserved =
ce0a0 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
ce0b0 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 /* The lock is
ce0c0 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 held if and only
ce0d0 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 if the lockfile
ce0e0 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 exists */. c
ce0f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b onst char *zLock
ce100 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 File = (const ch
ce110 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ar*)pFile->locki
ce120 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 ngContext;. r
ce130 65 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73 eserved = access
ce140 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d (zLockFile, 0)==
ce150 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 0;. }. OSTRACE
ce160 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
ce170 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 %d %d %d\n", pFi
ce180 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 le->h, rc, reser
ce190 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 ved);. *pResOut
ce1a0 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
ce1b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
ce1c0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
ce1d0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
ce1e0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
ce1f0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
ce200 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
ce210 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
ce220 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
ce230 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
ce240 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
ce250 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
ce260 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
ce270 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
ce280 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
ce290 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
ce2a0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
ce2b0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
ce2c0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
ce2d0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
ce2e0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
ce2f0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
ce300 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
ce310 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
ce320 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
ce330 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
ce340 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
ce350 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
ce360 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
ce370 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
ce380 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
ce390 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
ce3a0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
ce3b0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
ce3c0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
ce3d0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
ce3e0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
ce3f0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
ce400 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
ce410 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
ce420 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
ce430 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
ce440 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
ce450 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
ce460 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
ce470 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
ce480 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
ce490 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
ce4a0 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
ce4b0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
ce4c0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
ce4d0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a cking level..**.
ce4e0 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c 65 20 ** With dotfile
ce4f0 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c locking, we real
ce500 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 ly only support
ce510 73 74 61 74 65 20 28 34 29 3a 20 45 58 43 4c 55 state (4): EXCLU
ce520 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20 SIVE..** But we
ce530 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65 72 20 track the other
ce540 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69 locking levels i
ce550 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 nternally..*/.st
ce560 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b atic int dotlock
ce570 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
ce580 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
ce590 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ype) {. unixFil
ce5a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
ce5b0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
ce5c0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 fd;. char *zLoc
ce5d0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 kFile = (char *)
ce5e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
ce5f0 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 ntext;. int rc
ce600 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 = SQLITE_OK;...
ce610 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 /* If we have a
ce620 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 ny lock, then th
ce630 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 e lock file alre
ce640 61 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c ady exists. All
ce650 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f we have. ** to
ce660 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75 do is adjust ou
ce670 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 r internal recor
ce680 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 d of the lock le
ce690 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 vel.. */. if(
ce6a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
ce6b0 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 > NO_LOCK ){.
ce6c0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
ce6d0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66 = locktype;.#if
ce6e0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 !OS_VXWORKS.
ce6f0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74 /* Always updat
ce700 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20 e the timestamp
ce710 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20 on the old file
ce720 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 7a 4c */. utimes(zL
ce730 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a ockFile, NULL);.
ce740 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 #endif. retur
ce750 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
ce760 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e . . /* grab an
ce770 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
ce780 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a */. fd = open(z
ce790 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c LockFile,O_RDONL
ce7a0 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c Y|O_CREAT|O_EXCL
ce7b0 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20 66 64 ,0600);. if( fd
ce7c0 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69 <0 ){. /* fai
ce7d0 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 led to open/crea
ce7e0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d te the file, som
ce7f0 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20 68 61 eone else may ha
ce800 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f ve stolen the lo
ce810 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45 ck */. int tE
ce820 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
ce830 20 20 69 66 28 20 45 45 58 49 53 54 20 3d 3d 20 if( EEXIST ==
ce840 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 tErrno ){.
ce850 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
ce860 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ;. } else {.
ce870 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
ce880 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
ce890 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
ce8a0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
ce8b0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
ce8c0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
ce8d0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
ce8e0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
ce8f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
ce900 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
ce910 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73 65 28 } . if( close(
ce920 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 fd) ){. pFile
ce930 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
ce940 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 rno;. rc = SQ
ce950 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 LITE_IOERR_CLOSE
ce960 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f ;. }. . /* go
ce970 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 t it, set the ty
ce980 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b pe and return ok
ce990 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 */. pFile->loc
ce9a0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
ce9b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
ce9c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
ce9d0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
ce9e0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
ce9f0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b or pFile to lock
cea00 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
cea10 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
cea20 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
cea30 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
cea40 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
cea50 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
cea60 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
cea70 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
cea80 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
cea90 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
ceaa0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
ceab0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
ceac0 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69 * When the locki
cead0 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68 65 73 ng level reaches
ceae0 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 NO_LOCK, delete
ceaf0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a the lock file..
ceb00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f */.static int do
ceb10 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 tlockUnlock(sqli
ceb20 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
ceb30 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 t locktype) {.
ceb40 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
ceb50 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
ceb60 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 . char *zLockFi
ceb70 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 le = (char *)pFi
ceb80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
ceb90 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 xt;.. assert( p
ceba0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
cebb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
cebc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
cebd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
cebe0 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c cktype,.. pFil
cebf0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
cec00 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 pid());. assert
cec10 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
cec20 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 ED_LOCK );. .
cec30 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 /* no-op if poss
cec40 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 ible */. if( pF
cec50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c ile->locktype==l
cec60 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
cec70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
cec80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f . }.. /* To do
cec90 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61 72 65 wngrade to share
ceca0 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65 d, simply update
cecb0 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f our internal no
cecc0 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a tion of the. **
cecd0 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f lock state. No
cece0 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20 77 69 need to mess wi
cecf0 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 th the file on d
ced00 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 isk.. */. if(
ced10 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
ced20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
ced30 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 le->locktype = S
ced40 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
ced50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ced60 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f ;. }. . /* To
ced70 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 fully unlock th
ced80 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 6c 65 e database, dele
ced90 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 te the lock file
ceda0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6c 6f */. assert( lo
cedb0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
cedc0 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 );. if( unlink(
cedd0 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 zLockFile) ){.
cede0 20 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f int rc, tErrno
cedf0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 = errno;. if
cee00 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 ( ENOENT != tErr
cee10 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d no ){. rc =
cee20 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
cee30 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
cee40 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
cee50 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 UNLOCK);. }.
cee60 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
cee70 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
cee80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
cee90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
ceea0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
ceeb0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e ; . }. pFile->
ceec0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
ceed0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c CK;. return SQL
ceee0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
ceef0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 Close a file.
cef00 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
cef10 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 ck has been rele
cef20 61 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 ased before clos
cef30 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
cef40 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 nt dotlockClose(
cef50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
cef60 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 ) {. int rc;.
cef70 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e if( id ){. un
cef80 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
cef90 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
cefa0 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b dotlockUnlock
cefb0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
cefc0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
cefd0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
cefe0 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 ntext);. }. rc
ceff0 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 = closeUnixFile
cf000 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 (id);. return r
cf010 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c;.}./**********
cf020 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
cf030 74 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 the dot-file loc
cf040 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
cf050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
cf060 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
cf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0b0 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/../*********
cf0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf100 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
cf110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf120 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 Begin flock Loc
cf130 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a king ***********
cf140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf150 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 *****.**.** Use
cf160 74 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 the flock() syst
cf170 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 em call to do fi
cf180 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a le locking..**.*
cf190 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e * flock() lockin
cf1a0 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 g is like dot-fi
cf1b0 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 le locking in th
cf1c0 61 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a at the various.*
cf1d0 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 * fine-grain loc
cf1e0 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70 king levels supp
cf1f0 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 orted by SQLite
cf200 61 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e are collapsed in
cf210 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 to.** a single e
cf220 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 xclusive lock.
cf230 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
cf240 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 SHARED, RESERVED
cf250 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 , and.** PENDING
cf260 20 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73 locks are the s
cf270 61 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20 ame thing as an
cf280 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
cf290 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c SQLite.** still
cf2a0 20 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 works when you
cf2b0 64 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e do this, but con
cf2c0 63 75 72 72 65 6e 63 79 20 69 73 20 72 65 64 75 currency is redu
cf2d0 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c ced since.** onl
cf2e0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 y a single proce
cf2f0 73 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e ss can be readin
cf300 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 g the database a
cf310 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 t a time..**.**
cf320 4f 6d 69 74 20 74 68 69 73 20 73 65 63 74 69 6f Omit this sectio
cf330 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 n if SQLITE_ENAB
cf340 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
cf350 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f is turned off o
cf360 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e r if.** compilin
cf370 67 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a g for VXWORKS..*
cf380 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 /.#if SQLITE_ENA
cf390 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
cf3a0 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 E && !OS_VXWORKS
cf3b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
cf3c0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
cf3d0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
cf3e0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
cf3f0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
cf400 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
cf410 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
cf420 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
cf430 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 ck is held, set
cf440 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 *pResOut.** to a
cf450 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
cf460 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f otherwise *pResO
cf470 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 ut is set to zer
cf480 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 o. The return v
cf490 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 alue.** is set t
cf4a0 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 o SQLITE_OK unle
cf4b0 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 ss an I/O error
cf4c0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f occurs during lo
cf4d0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a ck checking..*/.
cf4e0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
cf4f0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
cf500 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
cf510 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
cf520 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
cf530 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 LITE_OK;. int r
cf540 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 eserved = 0;. u
cf550 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
cf560 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
cf570 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 . SimulateIOE
cf580 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
cf590 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
cf5a0 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
cf5b0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
cf5c0 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 le );. . /* Ch
cf5d0 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
cf5e0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
cf5f0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
cf600 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
cf610 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
cf620 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 D_LOCK ){. re
cf630 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a served = 1;. }.
cf640 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 . /* Otherwis
cf650 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 e see if some ot
cf660 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 her process hold
cf670 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 s it. */. if( !
cf680 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
cf690 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 /* attempt to ge
cf6a0 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 t the lock */.
cf6b0 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 int lrc = floc
cf6c0 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
cf6d0 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a _EX | LOCK_NB);.
cf6e0 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a if( !lrc ){.
cf6f0 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 /* got the
cf700 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 lock, unlock it
cf710 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 */. lrc =
cf720 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 flock(pFile->h,
cf730 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 LOCK_UN);.
cf740 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 if ( lrc ) {.
cf750 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 int tErrno
cf760 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
cf770 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 /* unlock faile
cf780 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 d with an error
cf790 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d */. lrc =
cf7a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
cf7b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
cf7c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
cf7d0 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 UNLOCK); .
cf7e0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
cf7f0 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 ROR(lrc) ){.
cf800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
cf810 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
cf820 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
cf830 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 lrc;. }.
cf840 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 }. } els
cf850 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 e {. int tE
cf860 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
cf870 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
cf880 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f ;. /* someo
cf890 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 ne else might ha
cf8a0 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a ve it reserved *
cf8b0 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 /. lrc = sq
cf8c0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
cf8d0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
cf8e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
cf8f0 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 K); . if( I
cf900 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 S_LOCK_ERROR(lrc
cf910 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
cf920 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
cf930 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
cf940 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 rc = lrc;.
cf950 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 }. }. }. OS
cf960 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
cf970 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
cf980 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
cf990 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 64 reserved);..#ifd
cf9a0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
cf9b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
cf9c0 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 RS. if( (rc & S
cf9d0 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 QLITE_IOERR) ==
cf9e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a SQLITE_IOERR ){.
cf9f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
cfa00 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 OK;. reserved
cfa10 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f =1;. }.#endif /
cfa20 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f * SQLITE_IGNORE_
cfa30 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 FLOCK_LOCK_ERROR
cfa40 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 S */. *pResOut
cfa50 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
cfa60 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
cfa70 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
cfa80 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
cfa90 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
cfaa0 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 eter locktype -
cfab0 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f one.** of the fo
cfac0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
cfad0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f (1) SHARED_LO
cfae0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 CK.** (2) RE
cfaf0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SERVED_LOCK.**
cfb00 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c (3) PENDING_L
cfb10 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 OCK.** (4) E
cfb20 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a XCLUSIVE_LOCK.**
cfb30 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 .** Sometimes wh
cfb40 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e en requesting on
cfb50 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 e lock state, ad
cfb60 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 ditional lock st
cfb70 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 ates.** are inse
cfb80 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e rted in between.
cfb90 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 The locking mi
cfba0 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 ght fail on one
cfbb0 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 of the later.**
cfbc0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 transitions leav
cfbd0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 ing the lock sta
cfbe0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
cfbf0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 m what it starte
cfc00 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 d but.** still s
cfc10 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c hort of its goal
cfc20 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
cfc30 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 chart shows the
cfc40 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e allowed.** tran
cfc50 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 sitions and the
cfc60 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 inserted interme
cfc70 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a diate states:.**
cfc80 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 .** UNLOCKED
cfc90 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 -> SHARED.**
cfca0 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 SHARED -> RESERV
cfcb0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
cfcc0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
cfcd0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
cfce0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e RESERVED -> (PEN
cfcf0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
cfd00 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 VE.** PENDING
cfd10 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
cfd20 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 .** flock() only
cfd30 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 really support
cfd40 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e EXCLUSIVE locks.
cfd50 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 We track inter
cfd60 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 mediate.** lock
cfd70 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 states in the sq
cfd80 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 lite3_file struc
cfd90 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f ture, but all lo
cfda0 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a cks SHARED or.**
cfdb0 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c above are reall
cfdc0 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b y EXCLUSIVE lock
cfdd0 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c s and exclude al
cfde0 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 l other processe
cfdf0 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 s from.** access
cfe00 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
cfe10 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
cfe20 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
cfe30 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
cfe40 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
cfe50 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
cfe60 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
cfe70 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 g level..*/.stat
cfe80 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b ic int flockLock
cfe90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
cfea0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
cfeb0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 {. int rc = SQ
cfec0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 LITE_OK;. unixF
cfed0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
cfee0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 ixFile*)id;.. a
cfef0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
cff00 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 . /* if we alre
cff10 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c ady have a lock,
cff20 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 it is exclusive
cff30 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 . . ** Just ad
cff40 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 just level and p
cff50 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 unt on outta her
cff60 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c e. */. if (pFil
cff70 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
cff80 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 _LOCK) {. pFi
cff90 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
cffa0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 ocktype;. ret
cffb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
cffc0 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 }. . /* grab
cffd0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
cffe0 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c k */. . if (fl
cfff0 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f ock(pFile->h, LO
d0000 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 CK_EX | LOCK_NB)
d0010 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 ) {. int tErr
d0020 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d0030 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d /* didn't get, m
d0040 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 ust be busy */.
d0050 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d0060 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d0070 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d0080 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 _IOERR_LOCK);.
d0090 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d00a0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d00b0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d00c0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d00d0 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 }. } else {.
d00e0 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 /* got it, set
d00f0 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 the type and ret
d0100 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 urn ok */. pF
d0110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d0120 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
d0130 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 OSTRACE4("LOCK
d0140 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 %d %s %s\n", p
d0150 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
d0160 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
d0170 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d . rc=
d0180 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
d0190 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 " : "failed");.#
d01a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e ifdef SQLITE_IGN
d01b0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
d01c0 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 RRORS. if( (rc
d01d0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 & SQLITE_IOERR)
d01e0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 == SQLITE_IOERR
d01f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
d0200 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e TE_BUSY;. }.#en
d0210 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 dif /* SQLITE_IG
d0220 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f NORE_FLOCK_LOCK_
d0230 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 ERRORS */. retu
d0240 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
d0250 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
d0260 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
d0270 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
d0280 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
d0290 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
d02a0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
d02b0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
d02c0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
d02d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
d02e0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d02f0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
d0300 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
d0310 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
d0320 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
d0330 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
d0340 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
d0350 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 nt flockUnlock(s
d0360 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d0370 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d0380 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d0390 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d03a0 69 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 id;. . assert(
d03b0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
d03c0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
d03d0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
d03e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d03f0 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
d0400 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pFile->lock
d0410 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b type, getpid());
d0420 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d0430 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
d0440 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f );. . /* no-o
d0450 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f p if possible */
d0460 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d0470 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 cktype==locktype
d0480 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
d0490 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d04a0 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e . /* shared can
d04b0 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63 just be set bec
d04c0 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 ause we always h
d04d0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ave an exclusive
d04e0 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 */. if (lockty
d04f0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 pe==SHARED_LOCK)
d0500 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f {. pFile->lo
d0510 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d0520 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 e;. return SQ
d0530 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
d0540 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c /* no, really,
d0550 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e unlock. */. in
d0560 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 t rc = flock(pFi
d0570 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b le->h, LOCK_UN);
d0580 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 . if (rc) {.
d0590 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d int r, tErrno =
d05a0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 errno;. r =
d05b0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d05c0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d05d0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d05e0 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 NLOCK);. if(
d05f0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 IS_LOCK_ERROR(r)
d0600 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
d0610 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d0620 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 rno;. }.#ifde
d0630 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f f SQLITE_IGNORE_
d0640 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 FLOCK_LOCK_ERROR
d0650 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20 53 S. if( (r & S
d0660 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 QLITE_IOERR) ==
d0670 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a SQLITE_IOERR ){.
d0680 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54 45 r = SQLITE
d0690 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e _BUSY;. }.#en
d06a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 dif /* SQLITE_IG
d06b0 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f NORE_FLOCK_LOCK_
d06c0 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20 ERRORS */. .
d06d0 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d return r;. }
d06e0 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 6c else {. pFil
d06f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f e->locktype = NO
d0700 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 _LOCK;. retur
d0710 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d0720 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
d0730 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
d0740 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 c int flockClose
d0750 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d0760 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b d) {. if( id ){
d0770 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b . flockUnlock
d0780 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
d0790 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 }. return clos
d07a0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d eUnixFile(id);.}
d07b0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
d07c0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
d07d0 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 G_STYLE && !OS_V
d07e0 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a XWORK */../*****
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
d0800 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 nd of the flock
d0810 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d0820 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
d0830 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0880 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a ********/../****
d0890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08f0 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 *** Begin Named
d0900 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e Semaphore Lockin
d0910 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a g **************
d0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
d0930 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 Named semaphore
d0940 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 locking is only
d0950 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 supported on Vx
d0960 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d Works..**.** Sem
d0970 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 aphore locking i
d0980 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 s like dot-lock
d0990 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 and flock in tha
d09a0 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 t it really only
d09b0 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43 .** supports EXC
d09c0 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 LUSIVE locking.
d09d0 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 Only a single p
d09e0 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64 20 rocess can read
d09f0 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 or write.** the
d0a00 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 database file at
d0a10 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 a time. This r
d0a20 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c educes potential
d0a30 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 concurrency, bu
d0a40 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c t.** makes the l
d0a50 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ock implementati
d0a60 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a on much easier..
d0a70 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b */.#if OS_VXWORK
d0a80 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f S../*.** This ro
d0a90 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
d0aa0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
d0ab0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
d0ac0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
d0ad0 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
d0ae0 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
d0af0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
d0b00 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
d0b10 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
d0b20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
d0b30 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
d0b40 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
d0b50 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
d0b60 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
d0b70 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
d0b80 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
d0b90 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
d0ba0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f ock checking..*/
d0bb0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 .static int semC
d0bc0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
d0bd0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d0be0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
d0bf0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 {. int rc = SQ
d0c00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 LITE_OK;. int r
d0c10 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 eserved = 0;. u
d0c20 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d0c30 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d0c40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
d0c50 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
d0c60 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
d0c70 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 ERVEDLOCK; );.
d0c80 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d0c90 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 );.. /* Check
d0ca0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 if a thread in t
d0cb0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 his process hold
d0cc0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f s such a lock */
d0cd0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d0ce0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f cktype>SHARED_LO
d0cf0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 CK ){. reserv
d0d00 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 ed = 1;. }. .
d0d10 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
d0d20 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
d0d30 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
d0d40 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 . */. if( !rese
d0d50 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f rved ){. sem_
d0d60 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d t *pSem = pFile-
d0d70 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 >pOpen->pSem;.
d0d80 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74 struct stat st
d0d90 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 atBuf;.. if(
d0da0 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d sem_trywait(pSem
d0db0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 )==-1 ){. i
d0dc0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d0dd0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 47 o;. if( EAG
d0de0 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b AIN != tErrno ){
d0df0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
d0e00 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d0e10 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d0e20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d0e30 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b CKRESERVEDLOCK);
d0e40 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d0e50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d0e60 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 no;. } else
d0e70 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f {. /* so
d0e80 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74 meone else has t
d0e90 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 he lock when we
d0ea0 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a are in NO_LOCK *
d0eb0 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 76 /. reserv
d0ec0 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 ed = (pFile->loc
d0ed0 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f 4c ktype < SHARED_L
d0ee0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OCK);. }.
d0ef0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
d0f00 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 20 * we could have
d0f10 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69 74 it if we want it
d0f20 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f */. sem_po
d0f30 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a st(pSem);. }.
d0f40 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 }. OSTRACE4("
d0f50 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 TEST WR-LOCK %d
d0f60 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d %d %d\n", pFile-
d0f70 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
d0f80 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d );.. *pResOut =
d0f90 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 reserved;. ret
d0fa0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
d0fb0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 Lock the file w
d0fc0 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 ith the lock spe
d0fd0 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 cified by parame
d0fe0 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f ter locktype - o
d0ff0 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ne.** of the fol
d1000 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
d1010 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 (1) SHARED_LOC
d1020 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 K.** (2) RES
d1030 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 ERVED_LOCK.**
d1040 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f (3) PENDING_LO
d1050 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 CK.** (4) EX
d1060 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a CLUSIVE_LOCK.**.
d1070 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 ** Sometimes whe
d1080 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 n requesting one
d1090 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 lock state, add
d10a0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 itional lock sta
d10b0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 tes.** are inser
d10c0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 ted in between.
d10d0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 The locking mig
d10e0 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f ht fail on one o
d10f0 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 f the later.** t
d1100 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 ransitions leavi
d1110 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 ng the lock stat
d1120 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
d1130 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 what it started
d1140 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 but.** still sh
d1150 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e ort of its goal.
d1160 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
d1170 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 chart shows the
d1180 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 allowed.** trans
d1190 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 itions and the i
d11a0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 nserted intermed
d11b0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a iate states:.**.
d11c0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d ** UNLOCKED -
d11d0 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 > SHARED.** S
d11e0 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 HARED -> RESERVE
d11f0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d1200 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
d1210 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 XCLUSIVE.** R
d1220 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 ESERVED -> (PEND
d1230 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d1240 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 E.** PENDING
d1250 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a -> EXCLUSIVE.**.
d1260 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ** Semaphore loc
d1270 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 ks only really s
d1280 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 upport EXCLUSIVE
d1290 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 locks. We trac
d12a0 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a k intermediate.*
d12b0 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e * lock states in
d12c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c the sqlite3_fil
d12d0 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 e structure, but
d12e0 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 all locks SHARE
d12f0 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 D or.** above ar
d1300 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 e really EXCLUSI
d1310 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 VE locks and exc
d1320 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 lude all other p
d1330 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a rocesses from.**
d1340 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 access the file
d1350 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
d1360 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
d1370 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
d1380 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 Use the sqlite3
d1390 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f OsUnlock().** ro
d13a0 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 utine to lower a
d13b0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a locking level..
d13c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
d13d0 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 mLock(sqlite3_fi
d13e0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
d13f0 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 type) {. unixFi
d1400 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d1410 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 xFile*)id;. int
d1420 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 fd;. sem_t *pS
d1430 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 em = pFile->pOpe
d1440 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 n->pSem;. int r
d1450 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
d1460 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 /* if we alrea
d1470 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 dy have a lock,
d1480 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e it is exclusive.
d1490 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a . ** Just adj
d14a0 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 ust level and pu
d14b0 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 nt on outta here
d14c0 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 . */. if (pFile
d14d0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f ->locktype > NO_
d14e0 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c LOCK) {. pFil
d14f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
d1500 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20 3d cktype;. rc =
d1510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
d1520 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 goto sem_end_loc
d1530 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c k;. }. . /* l
d1540 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f ock semaphore no
d1550 77 20 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 w but bail out w
d1560 68 65 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b hen already lock
d1570 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d ed. */. if( sem
d1580 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d _trywait(pSem)==
d1590 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 -1 ){. rc = S
d15a0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
d15b0 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 goto sem_end_loc
d15c0 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 k;. }.. /* got
d15d0 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 it, set the typ
d15e0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 e and return ok
d15f0 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b */. pFile->lock
d1600 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d1610 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a .. sem_end_lock:
d1620 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d1630 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
d1640 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
d1650 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
d1660 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 r pFile to lockt
d1670 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
d1680 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
d1690 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
d16a0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
d16b0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
d16c0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
d16d0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
d16e0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
d16f0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
d1700 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
d1710 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
d1720 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
d1730 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f atic int semUnlo
d1740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d1750 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
d1760 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
d1770 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d1780 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 le*)id;. sem_t
d1790 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 *pSem = pFile->p
d17a0 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 Open->pSem;.. a
d17b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
d17c0 20 20 61 73 73 65 72 74 28 20 70 53 65 6d 20 29 assert( pSem )
d17d0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
d17e0 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
d17f0 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 %d pid=%d\n", pF
d1800 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
d1810 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ,.. pFile->loc
d1820 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 ktype, getpid())
d1830 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
d1840 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 type<=SHARED_LOC
d1850 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d K );. . /* no-
d1860 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a op if possible *
d1870 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d1880 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 ocktype==locktyp
d1890 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
d18a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
d18b0 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 . /* shared ca
d18c0 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 n just be set be
d18d0 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 cause we always
d18e0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
d18f0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 e */. if (lockt
d1900 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d1910 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
d1920 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d1930 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 pe;. return S
d1940 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d1950 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 . /* no, really
d1960 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 unlock. */. if
d1970 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d ( sem_post(pSem
d1980 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e )==-1 ) {. in
d1990 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 t rc, tErrno = e
d19a0 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 rrno;. rc = s
d19b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d19c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d19d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
d19e0 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 LOCK);. if( I
d19f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d1a00 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
d1a10 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d1a20 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rno;. }. r
d1a30 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 eturn rc; . }.
d1a40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d1a50 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 = NO_LOCK;. re
d1a60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d1a70 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 }../*. ** Close
d1a80 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 a file.. */.stat
d1a90 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 ic int semClose(
d1aa0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d1ab0 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a ) {. if( id ){.
d1ac0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
d1ad0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d1ae0 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f )id;. semUnlo
d1af0 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b ck(id, NO_LOCK);
d1b00 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
d1b10 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e le );. unixEn
d1b20 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
d1b30 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 releaseLockInfo(
d1b40 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 pFile->pLock);.
d1b50 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e releaseOpenCn
d1b60 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b t(pFile->pOpen);
d1b70 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
d1b80 74 65 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 tex();. close
d1b90 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 UnixFile(id);.
d1ba0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
d1bb0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
d1bc0 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f /* OS_VXWORKS */
d1bd0 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d ./*.** Named sem
d1be0 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 aphore locking i
d1bf0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
d1c00 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a on VxWorks..**.
d1c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d1c20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 End of the named
d1c30 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 semaphore lock
d1c40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
d1c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
d1c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
d1cb0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1d00 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d1d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
d1d20 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 gin AFP Locking
d1d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1d50 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 *.**.** AFP is t
d1d60 68 65 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 he Apple Filing
d1d70 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 Protocol. AFP i
d1d80 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 s a network file
d1d90 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 system found.**
d1da0 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f on Apple Macinto
d1db0 73 68 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 sh computers - b
d1dc0 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e oth OS9 and OSX.
d1dd0 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 .**.** Third-par
d1de0 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ty implementatio
d1df0 6e 73 20 6f 66 20 41 46 50 20 61 72 65 20 61 76 ns of AFP are av
d1e00 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 68 ailable. But th
d1e10 69 73 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 is code here.**
d1e20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 only works on OS
d1e30 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e X..*/..#if defin
d1e40 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
d1e50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
d1e60 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a OCKING_STYLE./*.
d1e70 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e ** The afpLockin
d1e80 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 gContext structu
d1e90 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 re contains all
d1ea0 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 afp lock specifi
d1eb0 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 c state.*/.typed
d1ec0 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63 ef struct afpLoc
d1ed0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c kingContext afpL
d1ee0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 ockingContext;.s
d1ef0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 truct afpLocking
d1f00 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 Context {. unsi
d1f10 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 gned long long s
d1f20 68 61 72 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e haredByte;. con
d1f30 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b st char *dbPath;
d1f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d1f50 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e Name of the open
d1f60 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 file */.};..str
d1f70 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 uct ByteRangeLoc
d1f80 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 kPB2.{. unsigne
d1f90 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 d long long offs
d1fa0 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 et; /* of
d1fb0 66 73 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 fset to first by
d1fc0 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 te to lock */.
d1fd0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
d1fe0 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 ng length;
d1ff0 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 /* nbr of byte
d2000 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 s to lock */. u
d2010 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
d2020 67 20 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b g retRangeStart;
d2030 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 /* nbr of 1st b
d2040 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 yte locked if su
d2050 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e ccessful */. un
d2060 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f signed char unLo
d2070 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 ckFlag;
d2080 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 /* 1 = unlock, 0
d2090 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 = lock */. uns
d20a0 69 67 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 igned char start
d20b0 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f EndFlag; /
d20c0 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f * 1=rel to end o
d20d0 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f f fork, 0=rel to
d20e0 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 start */. int
d20f0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 fd;
d2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d2110 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 file desc to as
d2120 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 soc this lock wi
d2130 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e th */.};..#defin
d2140 65 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 e afpfsByteRange
d2150 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 Lock2FSCTL
d2160 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c _IOWR('z', 23,
d2170 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 struct ByteRang
d2180 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a eLockPB2)../*.**
d2190 20 54 68 69 73 20 69 73 20 61 20 75 74 69 6c 69 This is a utili
d21a0 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67 20 6f ty for setting o
d21b0 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62 69 74 r clearing a bit
d21c0 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 -range lock on a
d21d0 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73 79 73 n.** AFP filesys
d21e0 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 tem..** .** Retu
d21f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
d2200 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f success, SQLITE_
d2210 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e BUSY on failure.
d2220 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
d2230 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e fpSetLock(. con
d2240 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 20 st char *path,
d2250 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
d2260 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
d2270 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 to be locked or
d2280 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e unlocked */. un
d2290 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 ixFile *pFile,
d22a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d22b0 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 Open file descri
d22c0 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a ptor on path */.
d22d0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
d22e0 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 20 long offset,
d22f0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 74 /* First byte t
d2300 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 o be locked */.
d2310 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
d2320 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 ong length,
d2330 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
d2340 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 es to lock */.
d2350 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 int setLockFlag
d2360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d2370 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 6c 6f * True to set lo
d2380 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 6c ck. False to cl
d2390 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 ear lock */.){.
d23a0 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 struct ByteRang
d23b0 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 eLockPB2 pb;. i
d23c0 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e nt err;. . pb.
d23d0 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 unLockFlag = set
d23e0 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 LockFlag ? 0 : 1
d23f0 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 64 46 ;. pb.startEndF
d2400 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 lag = 0;. pb.of
d2410 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 fset = offset;.
d2420 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e pb.length = len
d2430 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 gth; . pb.fd =
d2440 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f pFile->h;. . O
d2450 53 54 52 41 43 45 36 28 22 41 46 50 53 45 54 4c STRACE6("AFPSETL
d2460 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 OCK [%s] for %d%
d2470 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a s in range %llx:
d2480 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 %llx\n", . (s
d2490 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a etLockFlag?"ON":
d24a0 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 "OFF"), pFile->h
d24b0 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 , (pb.fd==-1?"[t
d24c0 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a estval-1]":""),.
d24d0 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 offset, leng
d24e0 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 th);. err = fsc
d24f0 74 6c 28 70 61 74 68 2c 20 61 66 70 66 73 42 79 tl(path, afpfsBy
d2500 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 teRangeLock2FSCT
d2510 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 L, &pb, 0);. if
d2520 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 ( err==-1 ) {.
d2530 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 int rc;. i
d2540 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d2550 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 o;. OSTRACE4(
d2560 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c "AFPSETLOCK fail
d2570 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 ed to fsctl() '%
d2580 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 s' %d %s\n",.
d2590 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 path,
d25a0 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 tErrno, strerror
d25b0 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 (tErrno));.#ifde
d25c0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f f SQLITE_IGNORE_
d25d0 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a AFP_LOCK_ERRORS.
d25e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
d25f0 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 BUSY;.#else.
d2600 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d2610 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d2620 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 Errno,.
d2630 20 20 20 20 20 20 20 20 20 20 20 73 65 74 4c 6f setLo
d2640 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f ckFlag ? SQLITE_
d2650 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c IOERR_LOCK : SQL
d2660 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
d2670 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c );.#endif /* SQL
d2680 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c ITE_IGNORE_AFP_L
d2690 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 OCK_ERRORS */.
d26a0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d26b0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d26c0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d26d0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d26e0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d26f0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
d2700 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d2710 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
d2720 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
d2730 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ks if there is a
d2740 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 RESERVED lock h
d2750 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 eld on the speci
d2760 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 fied.** file by
d2770 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
d2780 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 r process. If su
d2790 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c ch a lock is hel
d27a0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a d, set *pResOut.
d27b0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f ** to a non-zero
d27c0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 value otherwise
d27d0 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 *pResOut is set
d27e0 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 to zero. The r
d27f0 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 eturn value.** i
d2800 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f s set to SQLITE_
d2810 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f OK unless an I/O
d2820 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
d2830 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 ring lock checki
d2840 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
d2850 74 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 t afpCheckReserv
d2860 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
d2870 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
d2880 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 esOut){. int rc
d2890 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d28a0 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
d28b0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
d28c0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d28d0 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 )id;. . Simula
d28e0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
d28f0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
d2900 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
d2910 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 ; );. . assert
d2920 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 ( pFile );. afp
d2930 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
d2940 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f context = (afpLo
d2950 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 ckingContext *)
d2960 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d2970 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 ntext;. . /* C
d2980 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 heck if a thread
d2990 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 in this process
d29a0 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f holds such a lo
d29b0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c ck */. if( pFil
d29c0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 e->locktype>SHAR
d29d0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
d29e0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d eserved = 1;. }
d29f0 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 . . /* Otherwi
d2a00 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f se see if some o
d2a10 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c ther process hol
d2a20 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 ds it.. */. i
d2a30 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a f( !reserved ){.
d2a40 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 /* lock the
d2a50 52 45 53 45 52 56 45 44 20 62 79 74 65 20 2a 2f RESERVED byte */
d2a60 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 . int lrc = a
d2a70 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
d2a80 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
d2a90 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c , RESERVED_BYTE,
d2aa0 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 28 1,1); . if(
d2ab0 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 SQLITE_OK==lrc
d2ac0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 ){. /* if w
d2ad0 65 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 74 e succeeded in t
d2ae0 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65 72 76 aking the reserv
d2af0 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 ed lock, unlock
d2b00 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 it to restore.
d2b10 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 ** the origi
d2b20 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 nal state */.
d2b30 20 20 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c lrc = afpSetL
d2b40 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
d2b50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 ath, pFile, RESE
d2b60 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 RVED_BYTE, 1, 0)
d2b70 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ;. } else {.
d2b80 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 /* if we fa
d2b90 69 6c 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 iled to get the
d2ba0 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e lock then someon
d2bb0 65 20 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 e else must have
d2bc0 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 it */. res
d2bd0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d erved = 1;. }
d2be0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
d2bf0 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 _ERROR(lrc) ){.
d2c00 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 rc=lrc;.
d2c10 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 }. }. . OSTR
d2c20 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE4("TEST WR-LO
d2c30 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 CK %d %d %d\n",
d2c40 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 pFile->h, rc, re
d2c50 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 served);. . *p
d2c60 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
d2c70 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
d2c80 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
d2c90 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
d2ca0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
d2cb0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
d2cc0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
d2cd0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
d2ce0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
d2cf0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
d2d00 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
d2d10 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
d2d20 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
d2d30 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
d2d40 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
d2d50 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
d2d60 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
d2d70 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
d2d80 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
d2d90 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
d2da0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
d2db0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
d2dc0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
d2dd0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
d2de0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
d2df0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
d2e00 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
d2e10 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
d2e20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
d2e30 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
d2e40 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
d2e50 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
d2e60 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
d2e70 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
d2e80 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
d2e90 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
d2ea0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
d2eb0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d2ec0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
d2ed0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
d2ee0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d2ef0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
d2f00 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
d2f10 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
d2f20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
d2f30 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
d2f40 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
d2f50 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
d2f60 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
d2f70 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
d2f80 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
d2f90 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
d2fa0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
d2fb0 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 afpLock(sqlite3
d2fc0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d2fd0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 ocktype){. int
d2fe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d2ff0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d3000 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d3010 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 d;. afpLockingC
d3020 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 ontext *context
d3030 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e = (afpLockingCon
d3040 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c text *) pFile->l
d3050 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
d3060 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d3070 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 e );. OSTRACE5(
d3080 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 "LOCK %d %s w
d3090 61 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c as %s pid=%d\n",
d30a0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
d30b0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
d30c0 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b (locktype), lock
d30d0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e typeName(pFile->
d30e0 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 locktype), getpi
d30f0 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 d());.. /* If t
d3100 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
d3110 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
d3120 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
d3130 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
d3140 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f ** unixFile, do
d3150 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 nothing. Don't
d3160 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f use the afp_end_
d3170 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c lock: exit path,
d3180 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 as. ** unixEnt
d3190 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 erMutex() hasn't
d31a0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
d31b0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
d31c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
d31d0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
d31e0 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 TRACE3("LOCK
d31f0 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 %d %s ok (alread
d3200 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c y held)\n", pFil
d3210 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
d3220 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d3230 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 cktype));. re
d3240 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d3250 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
d3260 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ure the locking
d3270 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 sequence is corr
d3280 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ect. */. asser
d3290 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
d32a0 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c pe!=NO_LOCK || l
d32b0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d32c0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
d32d0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 ( locktype!=PEND
d32e0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ING_LOCK );. as
d32f0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
d3300 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c RESERVED_LOCK ||
d3310 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d3320 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
d3330 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 . . /* This mu
d3340 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 tex is needed be
d3350 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f cause pFile->pLo
d3360 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 ck is shared acr
d3370 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f oss threads. */
d3380 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
d3390 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 x();.. /* Make
d33a0 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 sure the current
d33b0 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 thread owns the
d33c0 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 pFile.. */. r
d33d0 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 c = transferOwne
d33e0 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 rship(pFile);.
d33f0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
d3400 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 K ){. unixLea
d3410 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 veMutex();. r
d3420 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
d3430 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e . /* A PENDIN
d3440 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 G lock is needed
d3450 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e before acquirin
d3460 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 g a SHARED lock
d3470 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 and before. **
d3480 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 acquiring an EXC
d3490 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f LUSIVE lock. Fo
d34a0 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 r the SHARED loc
d34b0 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 k, the PENDING w
d34c0 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 ill. ** be rele
d34d0 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ased.. */. if(
d34e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
d34f0 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c D_LOCK . ||
d3500 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
d3510 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 USIVE_LOCK && pF
d3520 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 ile->locktype<PE
d3530 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b NDING_LOCK). ){
d3540 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b . int failed;
d3550 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 . failed = af
d3560 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 pSetLock(context
d3570 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
d3580 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 PENDING_BYTE, 1
d3590 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 , 1);. if (fa
d35a0 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 iled) {. rc
d35b0 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 = failed;.
d35c0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
d35d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ck;. }. }.
d35e0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c . /* If control
d35f0 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f gets to this po
d3600 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c int, then actual
d3610 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 ly go ahead and
d3620 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 make. ** operat
d3630 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 ing system calls
d3640 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
d3650 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 ed lock.. */.
d3660 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d3670 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d3680 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c int lk, lrc1, l
d3690 72 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a rc2, lrc1Errno;.
d36a0 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 . /* Now
d36b0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 get the read-loc
d36c0 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f k SHARED_LOCK */
d36d0 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 . /* note tha
d36e0 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66 t the quality of
d36f0 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 the randomness
d3700 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74 doesn't matter t
d3710 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 hat much */.
d3720 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a lk = random(); .
d3730 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 context->sha
d3740 72 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 redByte = (lk &
d3750 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48 41 0x7fffffff)%(SHA
d3760 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 RED_SIZE - 1);.
d3770 20 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 74 lrc1 = afpSet
d3780 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
d3790 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 Path, pFile, .
d37a0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 SHARED_F
d37b0 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 IRST+context->sh
d37c0 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 3b aredByte, 1, 1);
d37d0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
d37e0 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a _ERROR(lrc1) ){.
d37f0 20 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 lrc1Errno
d3800 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 = pFile->lastErr
d3810 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a no;. }. /*
d3820 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 Drop the tempor
d3830 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b ary PENDING lock
d3840 20 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 */. lrc2 = a
d3850 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
d3860 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
d3870 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
d3880 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 1, 0);. .
d3890 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d38a0 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20 20 R(lrc1) ) {.
d38b0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d38c0 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a no = lrc1Errno;.
d38d0 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b rc = lrc1;
d38e0 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f . goto afp_
d38f0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 end_lock;. }
d3900 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b else if( IS_LOCK
d3910 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a _ERROR(lrc2) ){.
d3920 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32 3b rc = lrc2;
d3930 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f . goto afp_
d3940 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 end_lock;. }
d3950 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d else if( lrc1 !=
d3960 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 SQLITE_OK ) {.
d3970 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a rc = lrc1;.
d3980 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
d3990 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d39a0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe = SHARED_LOCK
d39b0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 ;. pFile->p
d39c0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 Open->nLock++;.
d39d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
d39e0 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 /* The request
d39f0 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 was for a RESER
d3a00 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 VED or EXCLUSIVE
d3a10 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 lock. It is.
d3a20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 ** assumed tha
d3a30 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 t there is a SHA
d3a40 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c RED or greater l
d3a50 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a ock on the file.
d3a60 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a ** already..
d3a70 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 */. int f
d3a80 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 ailed = 0;. a
d3a90 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d ssert( 0!=pFile-
d3aa0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
d3ab0 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d if (locktype >=
d3ac0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 RESERVED_LOCK &
d3ad0 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 & pFile->locktyp
d3ae0 65 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 e < RESERVED_LOC
d3af0 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 K) {. /*
d3b00 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 Acquire a RESERV
d3b10 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 ED lock */.
d3b20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 failed = afpS
d3b30 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d3b40 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
d3b50 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
d3b60 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1);. }. if
d3b70 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 (!failed && loc
d3b80 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 ktype == EXCLUSI
d3b90 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 VE_LOCK) {.
d3ba0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 /* Acquire an E
d3bb0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f XCLUSIVE lock */
d3bc0 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 . .
d3bd0 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68 /* Remove the sh
d3be0 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 ared lock before
d3bf0 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67 trying the rang
d3c00 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 e. we'll need t
d3c10 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73 o . ** rees
d3c20 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 tablish the shar
d3c30 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 ed lock if we ca
d3c40 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70 n't get the afp
d3c50 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a Unlock. */.
d3c60 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c if( !(fail
d3c70 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 ed = afpSetLock(
d3c80 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
d3c90 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 pFile, SHARED_F
d3ca0 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 IRST +.
d3cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3cc0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 context->sharedB
d3cd0 79 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 yte, 1, 0)) ){.
d3ce0 20 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65 int faile
d3cf0 64 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a d2 = SQLITE_OK;.
d3d00 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 /* now a
d3d10 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 ttemmpt to get t
d3d20 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 he exclusive loc
d3d30 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 k range */.
d3d40 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 failed = afpS
d3d50 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d3d60 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 dbPath, pFile, S
d3d70 48 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 HARED_FIRST, .
d3d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 SHA
d3da0 52 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 RED_SIZE, 1);.
d3db0 20 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 if( failed
d3dc0 20 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61 && (failed2 = a
d3dd0 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
d3de0 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
d3df0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
d3e00 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 SHARED
d3e10 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 _FIRST + context
d3e20 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c ->sharedByte, 1,
d3e30 20 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 1)) ){.
d3e40 20 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74 /* Can't reest
d3e50 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 ablish the share
d3e60 64 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 d lock. Sqlite
d3e70 63 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73 can't deal, this
d3e80 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a is. **
d3e90 20 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 a critical I/O
d3ea0 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 error.
d3eb0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
d3ec0 3d 20 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c = ((failed & SQL
d3ed0 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 ITE_IOERR) == SQ
d3ee0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 LITE_IOERR) ? fa
d3ef0 69 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20 iled2 : .
d3f00 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 SQLITE_I
d3f10 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 OERR_LOCK;.
d3f20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e goto afp_en
d3f30 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 d_lock;.
d3f40 7d 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a } . }else{.
d3f50 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 rc = fai
d3f60 6c 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 led; . }.
d3f70 20 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c }. if( fail
d3f80 65 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ed ){. rc =
d3f90 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 failed;. }.
d3fa0 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d }. . if( rc==
d3fb0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
d3fc0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d3fd0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d = locktype;. }
d3fe0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 else if( locktyp
d3ff0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
d4000 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d4010 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 locktype = PENDI
d4020 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a NG_LOCK;. }. .
d4030 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 afp_end_lock:.
d4040 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
d4050 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f ;. OSTRACE4("LO
d4060 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e CK %d %s %s\n
d4070 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
d4080 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
d4090 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72 pe), . r
d40a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
d40b0 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b ok" : "failed");
d40c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d40d0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
d40e0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
d40f0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
d4100 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 r pFile to lockt
d4110 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
d4120 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
d4130 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
d4140 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
d4150 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
d4160 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
d4170 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
d4180 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
d4190 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
d41a0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
d41b0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
d41c0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
d41d0 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f atic int afpUnlo
d41e0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d41f0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
d4200 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 e) {. int rc =
d4210 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 SQLITE_OK;. uni
d4220 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d4230 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
d4240 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
d4250 74 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f t *pCtx = (afpLo
d4260 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 ckingContext *)
d4270 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d4280 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 ntext;.. assert
d4290 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 ( pFile );. OST
d42a0 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 RACE5("UNLOCK %
d42b0 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d d %d was %d pid=
d42c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
d42d0 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 locktype,.
d42e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d42f0 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a ype, getpid());.
d4300 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d4310 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
d4320 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
d4330 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 >locktype<=lockt
d4340 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
d4350 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d4360 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 . if( CHECK_THR
d4370 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a EADID(pFile) ){.
d4380 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d4390 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
d43a0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
d43b0 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c ;. if( pFile->l
d43c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d43d0 4f 43 4b 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 OCK ){. .
d43e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d43f0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
d4400 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OCK ){. rc
d4410 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
d4420 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
d4430 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 , SHARED_FIRST,
d4440 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b SHARED_SIZE, 0);
d4450 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
d4460 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b QLITE_OK && lock
d4470 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d4480 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 K ){. /*
d4490 6f 6e 6c 79 20 72 65 2d 65 73 74 61 62 6c 69 73 only re-establis
d44a0 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 h the shared loc
d44b0 6b 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a k if necessary *
d44c0 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 68 /. int sh
d44d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 aredLockByte = S
d44e0 48 41 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78 HARED_FIRST+pCtx
d44f0 2d 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 ->sharedByte;.
d4500 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 rc = afpSe
d4510 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 tLock(pCtx->dbPa
d4520 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65 th, pFile, share
d4530 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 dLockByte, 1, 1)
d4540 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d4550 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
d4560 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e TE_OK && pFile->
d4570 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e locktype>=PENDIN
d4580 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 G_LOCK ){.
d4590 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 rc = afpSetLock(
d45a0 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 pCtx->dbPath, pF
d45b0 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 ile, PENDING_BYT
d45c0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 E, 1, 0);. }
d45d0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
d45e0 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d ITE_OK && pFile-
d45f0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 >locktype>=RESER
d4600 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 VED_LOCK ){.
d4610 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 rc = afpSetLoc
d4620 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 k(pCtx->dbPath,
d4630 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f pFile, RESERVED_
d4640 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 BYTE, 1, 0);.
d4650 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c }. }else if( l
d4660 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
d4670 20 29 7b 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 ){. /* clear
d4680 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b the shared lock
d4690 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 68 61 72 */. int shar
d46a0 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 edLockByte = SHA
d46b0 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e RED_FIRST+pCtx->
d46c0 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 sharedByte;.
d46d0 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 rc = afpSetLock(
d46e0 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 pCtx->dbPath, pF
d46f0 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 ile, sharedLockB
d4700 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a yte, 1, 0);. }.
d4710 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
d4720 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
d4730 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
d4740 4b 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 K ){. struc
d4750 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 t unixOpenCnt *p
d4760 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f Open = pFile->pO
d4770 70 65 6e 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e pen;. pOpen
d4780 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 ->nLock--;.
d4790 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e assert( pOpen->
d47a0 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 nLock>=0 );.
d47b0 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f if( pOpen->nLo
d47c0 63 6b 3d 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e ck==0 && pOpen->
d47d0 6e 50 65 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20 nPending>0 ){.
d47e0 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
d47f0 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
d4800 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b pOpen->nPending;
d4810 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
d4820 20 69 66 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e if( pOpen->aPen
d4830 64 69 6e 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f ding[i] < 0 ) co
d4840 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
d4850 20 20 69 66 28 20 63 6c 6f 73 65 28 70 4f 70 65 if( close(pOpe
d4860 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20 n->aPending[i])
d4870 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
d4880 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d4890 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
d48a0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
d48b0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
d48c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
d48d0 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e pOpen
d48e0 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 ->aPending[i] =
d48f0 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a -1;. }.
d4900 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
d4910 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
d4920 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
d4930 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f sqlite3_free(pO
d4940 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a pen->aPending);.
d4950 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d pOpen-
d4960 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 >nPending = 0;.
d4970 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e pOpen->
d4980 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 aPending = 0;.
d4990 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
d49a0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 }. }. unix
d49b0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
d49c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
d49d0 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 K ) pFile->lockt
d49e0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d49f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d4a00 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
d4a10 6c 65 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50 le & cleanup AFP
d4a20 20 73 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e specific lockin
d4a30 67 20 63 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 g context .*/.st
d4a40 61 74 69 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 atic int afpClos
d4a50 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
d4a60 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 id) {. if( id )
d4a70 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a {. unixFile *
d4a80 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d4a90 65 2a 29 69 64 3b 0a 20 20 20 20 61 66 70 55 6e e*)id;. afpUn
d4aa0 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b lock(id, NO_LOCK
d4ab0 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 );. unixEnter
d4ac0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 Mutex();. if(
d4ad0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 pFile->pOpen &&
d4ae0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e pFile->pOpen->n
d4af0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a Lock ){. /*
d4b00 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 If there are ou
d4b10 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c tstanding locks,
d4b20 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 do not actually
d4b30 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 close the file
d4b40 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 just. ** ye
d4b50 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77 t because that w
d4b60 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 ould clear those
d4b70 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 locks. Instead
d4b80 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 , add the file.
d4b90 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 ** descript
d4ba0 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 or to pOpen->aPe
d4bb0 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 nding. It will
d4bc0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
d4bd0 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 closed when.
d4be0 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c ** the last l
d4bf0 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a ock is cleared..
d4c00 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
d4c10 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20 nt *aNew;.
d4c20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
d4c30 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c nt *pOpen = pFil
d4c40 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 e->pOpen;.
d4c50 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 aNew = sqlite3_r
d4c60 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 ealloc(pOpen->aP
d4c70 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e ending, (pOpen->
d4c80 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 nPending+1)*size
d4c90 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 of(int) );.
d4ca0 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a if( aNew==0 ){.
d4cb0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 /* If a
d4cc0 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 malloc fails, ju
d4cd0 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 st leak the file
d4ce0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
d4cf0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
d4d00 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 pOpen->aPend
d4d10 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 ing = aNew;.
d4d20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 pOpen->aPend
d4d30 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 ing[pOpen->nPend
d4d40 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b ing] = pFile->h;
d4d50 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e . pOpen->
d4d60 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 nPending++;.
d4d70 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d pFile->h = -
d4d80 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
d4d90 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e . releaseOpen
d4da0 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e Cnt(pFile->pOpen
d4db0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
d4dc0 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ree(pFile->locki
d4dd0 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 ngContext);.
d4de0 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
d4df0 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 );. unixLeave
d4e00 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 Mutex();. }. r
d4e10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d4e20 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 .}..#endif /* de
d4e30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
d4e40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
d4e50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d4e60 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 */./*.** The cod
d4e70 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41 e above is the A
d4e80 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e FP lock implemen
d4e90 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 tation. The cod
d4ea0 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a e is specific.**
d4eb0 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 to MacOSX and d
d4ec0 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 oes not work on
d4ed0 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66 other unix platf
d4ee0 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e orms. No altern
d4ef0 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 ative.** is avai
d4f00 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 lable. If you d
d4f10 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 on't compile for
d4f20 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 a mac, then the
d4f30 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 "unix-afp".** V
d4f40 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 FS is not availa
d4f50 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ble..**.********
d4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
d4f70 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63 d of the AFP loc
d4f80 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
d4f90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
d4fa0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4ff0 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5040 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d5050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c ********** Non-l
d5060 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 ocking sqlite3_f
d5070 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a ile methods ****
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5090 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
d50a0 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f The next divisio
d50b0 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 n contains imple
d50c0 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 mentations for a
d50d0 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 ll methods of th
d50e0 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 e .** sqlite3_fi
d50f0 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 le object other
d5100 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 than the locking
d5110 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c methods. The l
d5120 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 ocking.** method
d5130 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 s were defined i
d5140 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 n divisions abov
d5150 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d e (one locking m
d5160 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 ethod per.** div
d5170 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d ision). Those m
d5180 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 ethods that are
d5190 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f common to all lo
d51a0 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 cking modes.** a
d51b0 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68 re gather togeth
d51c0 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76 er into this div
d51d0 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ision..*/../*.**
d51e0 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 Seek to the off
d51f0 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 set passed as th
d5200 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
d5210 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 t, then read cnt
d5220 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 .** bytes into
d5230 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 pBuf. Return the
d5240 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
d5250 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a actually read..
d5260 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f **.** NB: If yo
d5270 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 u define USE_PRE
d5280 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 AD or USE_PREAD6
d5290 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 4, then it might
d52a0 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 also.** be nece
d52b0 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 ssary to define
d52c0 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f _XOPEN_SOURCE to
d52d0 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 be 500. This v
d52e0 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e aries from.** on
d52f0 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 e system to anot
d5300 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 her. Since SQLi
d5310 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 te does not defi
d5320 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 ne USE_PREAD.**
d5330 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 any any form by
d5340 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c default, we will
d5350 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 not attempt to
d5360 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
d5370 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 URCE..** See tic
d5380 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 kets #2741 and #
d5390 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 2681..**.** To a
d53a0 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 void stomping th
d53b0 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e e errno value on
d53c0 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74 a failed read t
d53d0 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c he lastErrno val
d53e0 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 ue.** is set bef
d53f0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
d5400 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 /.static int see
d5410 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c kAndRead(unixFil
d5420 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 e *id, sqlite3_i
d5430 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 nt64 offset, voi
d5440 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 d *pBuf, int cnt
d5450 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 ){. int got;.
d5460 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 i64 newOffset;.
d5470 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 TIMER_START;.#i
d5480 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 f defined(USE_PR
d5490 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 EAD). got = pre
d54a0 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 ad(id->h, pBuf,
d54b0 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 cnt, offset);.
d54c0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
d54d0 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c got = -1 );.#el
d54e0 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 if defined(USE_P
d54f0 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 READ64). got =
d5500 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 pread64(id->h, p
d5510 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
d5520 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
d5530 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
d5540 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 ;.#else. newOff
d5550 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e set = lseek(id->
d5560 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f h, offset, SEEK_
d5570 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 SET);. Simulate
d5580 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 IOError( newOffs
d5590 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 et-- );. if( ne
d55a0 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 wOffset!=offset
d55b0 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 ){. if( newOf
d55c0 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 fset == -1 ){.
d55d0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
d55e0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
d55f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 errno;. }els
d5600 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 e{. ((unixF
d5610 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
d5620 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 rno = 0;....
d5630 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
d5640 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 . }. got = rea
d5650 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 d(id->h, pBuf, c
d5660 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 nt);.#endif. TI
d5670 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 MER_END;. if( g
d5680 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e ot<0 ){. ((un
d5690 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
d56a0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d56b0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22 }. OSTRACE5("
d56c0 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 READ %-3d %5d
d56d0 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 %7lld %llu\n",
d56e0 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 id->h, got, offs
d56f0 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 et, TIMER_ELAPSE
d5700 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 D);. return got
d5710 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
d5720 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
d5730 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
d5740 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
d5750 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
d5760 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
d5770 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
d5780 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
d5790 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
d57a0 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
d57b0 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 nt unixRead(. s
d57c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d57d0 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 . void *pBuf,
d57e0 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 . int amt,. sq
d57f0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
d5800 65 74 0a 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b et.){. int got;
d5810 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b . assert( id );
d5820 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 .. /* Never rea
d5830 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f d or write any o
d5840 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 f the bytes in t
d5850 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 he locking range
d5860 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28 */. assert( ((
d5870 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 unixFile*)id)->i
d5880 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 sLockable==0.
d5890 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
d58a0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 >=PENDING_BYTE+5
d58b0 31 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 12. ||
d58c0 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 offset+amt<=PEND
d58d0 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 20 20 67 ING_BYTE );.. g
d58e0 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 ot = seekAndRead
d58f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 ((unixFile*)id,
d5900 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d offset, pBuf, am
d5910 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 t);. if( got==a
d5920 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
d5930 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
d5940 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b lse if( got<0 ){
d5950 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e . /* lastErrn
d5960 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 o set by seekAnd
d5970 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 Read */. retu
d5980 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d5990 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 READ;. }else{.
d59a0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
d59b0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
d59c0 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 0; /* not a syst
d59d0 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 em error */.
d59e0 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 20 /* Unread parts
d59f0 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d 75 of the buffer mu
d5a00 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 st be zero-fille
d5a10 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 d */. memset(
d5a20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 &((char*)pBuf)[g
d5a30 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 ot], 0, amt-got)
d5a40 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
d5a50 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
d5a60 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a READ;. }.}../*.
d5a70 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f ** Seek to the o
d5a80 66 66 73 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 ffset in id->off
d5a90 73 65 74 20 74 68 65 6e 20 72 65 61 64 20 63 6e set then read cn
d5aa0 74 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 t bytes into pBu
d5ab0 66 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 f..** Return the
d5ac0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
d5ad0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 actually read.
d5ae0 20 55 70 64 61 74 65 20 74 68 65 20 6f 66 66 73 Update the offs
d5af0 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f et..**.** To avo
d5b00 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 id stomping the
d5b10 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 errno value on a
d5b20 20 66 61 69 6c 65 64 20 77 72 69 74 65 20 74 68 failed write th
d5b30 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 e lastErrno valu
d5b40 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f e.** is set befo
d5b50 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f re returning..*/
d5b60 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b .static int seek
d5b70 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 69 6c AndWrite(unixFil
d5b80 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 73 65 e *id, i64 offse
d5b90 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 t, const void *p
d5ba0 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 Buf, int cnt){.
d5bb0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 int got;. i64
d5bc0 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d newOffset;. TIM
d5bd0 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 65 ER_START;.#if de
d5be0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 fined(USE_PREAD)
d5bf0 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 28 . got = pwrite(
d5c00 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 id->h, pBuf, cnt
d5c10 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 69 66 , offset);.#elif
d5c20 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
d5c30 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 77 AD64). got = pw
d5c40 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 rite64(id->h, pB
d5c50 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 uf, cnt, offset)
d5c60 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 ;.#else. newOff
d5c70 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e set = lseek(id->
d5c80 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f h, offset, SEEK_
d5c90 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f SET);. if( newO
d5ca0 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b ffset!=offset ){
d5cb0 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73 . if( newOffs
d5cc0 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 et == -1 ){.
d5cd0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
d5ce0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 )->lastErrno = e
d5cf0 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b rrno;. }else{
d5d00 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c . ((unixFil
d5d10 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e e*)id)->lastErrn
d5d20 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a o = 0;.... }.
d5d30 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
d5d40 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72 69 74 65 }. got = write
d5d50 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
d5d60 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d t);.#endif. TIM
d5d70 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f ER_END;. if( go
d5d80 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 t<0 ){. ((uni
d5d90 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
d5da0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d5db0 20 7d 0a 0a 20 20 4f 53 54 52 41 43 45 35 28 22 }.. OSTRACE5("
d5dc0 57 52 49 54 45 20 20 20 25 2d 33 64 20 25 35 64 WRITE %-3d %5d
d5dd0 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 %7lld %llu\n",
d5de0 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 id->h, got, offs
d5df0 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 et, TIMER_ELAPSE
d5e00 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 D);. return got
d5e10 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 ;.}.../*.** Writ
d5e20 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 e data from a bu
d5e30 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 ffer into a file
d5e40 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
d5e50 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a _OK on success.*
d5e60 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 * or some other
d5e70 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 error code on fa
d5e80 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ilure..*/.static
d5e90 20 69 6e 74 20 75 6e 69 78 57 72 69 74 65 28 0a int unixWrite(.
d5ea0 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
d5eb0 69 64 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 id, . const voi
d5ec0 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 d *pBuf, . int
d5ed0 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 amt,. sqlite3_i
d5ee0 6e 74 36 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a nt64 offset .){.
d5ef0 20 20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b int wrote = 0;
d5f00 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b . assert( id );
d5f10 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 . assert( amt>0
d5f20 20 29 3b 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20 );.. /* Never
d5f30 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e read or write an
d5f40 79 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 y of the bytes i
d5f50 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 n the locking ra
d5f60 6e 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 nge */. assert(
d5f70 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
d5f80 2d 3e 69 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a ->isLockable==0.
d5f90 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 || off
d5fa0 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 set>=PENDING_BYT
d5fb0 45 2b 35 31 32 0a 20 20 20 20 20 20 20 20 20 20 E+512.
d5fc0 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 || offset+amt<=P
d5fd0 45 4e 44 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a ENDING_BYTE );..
d5fe0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
d5ff0 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f /* If we are do
d6000 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 ing a normal wri
d6010 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 te to a database
d6020 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 file (as oppose
d6030 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 d to. ** doing
d6040 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f a hot-journal ro
d6050 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 llback or a writ
d6060 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f e to some file o
d6070 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a ther than a. **
d6080 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 normal database
d6090 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f file) then reco
d60a0 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 rd the fact that
d60b0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 the database.
d60c0 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 ** has changed.
d60d0 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 If the transact
d60e0 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d ion counter is m
d60f0 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 odified, record
d6100 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 that. ** fact t
d6110 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 oo.. */. if( (
d6120 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
d6130 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b inNormalWrite ){
d6140 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
d6150 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d6160 2a 29 69 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d *)id;. pFile-
d6170 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 >dbUpdate = 1;
d6180 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
d6190 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 has been modifie
d61a0 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 d */. if( off
d61b0 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 set<=24 && offse
d61c0 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 t+amt>=27 ){.
d61d0 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 int rc;.
d61e0 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d char oldCntr[4]
d61f0 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 ;. Simulate
d6200 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
d6210 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 ;. rc = see
d6220 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 kAndRead(pFile,
d6230 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 24, oldCntr, 4);
d6240 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
d6250 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
d6260 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 . if( rc!=4
d6270 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e || memcmp(oldCn
d6280 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 tr, &((char*)pBu
d6290 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 f)[24-offset], 4
d62a0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
d62b0 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 pFile->transCntr
d62c0 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 Chng = 1; /* Th
d62d0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
d62e0 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 unter has change
d62f0 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 d */. }.
d6300 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
d6310 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 while( amt>0 &&
d6320 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e (wrote = seekAn
d6330 64 57 72 69 74 65 28 28 75 6e 69 78 46 69 6c 65 dWrite((unixFile
d6340 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c 20 70 42 *)id, offset, pB
d6350 75 66 2c 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20 uf, amt))>0 ){.
d6360 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b amt -= wrote;
d6370 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 . offset += w
d6380 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d rote;. pBuf =
d6390 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
d63a0 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 wrote];. }. Si
d63b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 28 20 mulateIOError((
d63c0 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d wrote=(-1), amt=
d63d0 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 1 ));. Simulate
d63e0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 DiskfullError((
d63f0 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 wrote=0, amt=1 )
d6400 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e 30 20 29 );. if( amt>0 )
d6410 7b 0a 20 20 20 20 69 66 28 20 77 72 6f 74 65 3c {. if( wrote<
d6420 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 0 ){. /* la
d6430 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 stErrno set by s
d6440 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 eekAndWrite */.
d6450 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
d6460 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a TE_IOERR_WRITE;.
d6470 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d6480 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
d6490 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b ->lastErrno = 0;
d64a0 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d /* not a system
d64b0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 error */.
d64c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 return SQLITE_FU
d64d0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LL;. }. }.
d64e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d64f0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
d6500 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f TE_TEST./*.** Co
d6510 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f unt the number o
d6520 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 f fullsyncs and
d6530 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 normal syncs. T
d6540 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 his is used to t
d6550 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 est.** that sync
d6560 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 s and fullsyncs
d6570 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74 are occurring at
d6580 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 the right times
d6590 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
d65a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 int sqlite3_sync
d65b0 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 _count = 0;.SQLI
d65c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
d65d0 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
d65e0 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
d65f0 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 *.** We do not t
d6600 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20 rust systems to
d6610 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e provide a workin
d6620 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20 g fdatasync().
d6630 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 Some do..** Othe
d6640 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 rs do no. To be
d6650 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 safe, we will s
d6660 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 73 tick with the (s
d6670 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a lower) fsync()..
d6680 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 ** If you know t
d6690 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20 hat your system
d66a0 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 61 does support fda
d66b0 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74 tasync() correct
d66c0 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 ly,.** then simp
d66d0 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 ly compile with
d66e0 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74 -Dfdatasync=fdat
d66f0 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 async.*/.#if !de
d6700 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29 fined(fdatasync)
d6710 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c && !defined(__l
d6720 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 inux__).# define
d6730 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 fdatasync fsync
d6740 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
d6750 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 efine HAVE_FULLF
d6760 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 SYNC to 0 or 1 d
d6770 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 epending on whet
d6780 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 her or not.** th
d6790 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 e F_FULLFSYNC ma
d67a0 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 cro is defined.
d67b0 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 F_FULLFSYNC is
d67c0 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c currently.** onl
d67d0 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d y available on M
d67e0 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 ac OS X. But th
d67f0 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e at could change.
d6800 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c .*/.#ifdef F_FUL
d6810 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 LFSYNC.# define
d6820 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 HAVE_FULLFSYNC 1
d6830 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
d6840 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 HAVE_FULLFSYNC 0
d6850 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
d6860 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 The fsync() syst
d6870 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 em call does not
d6880 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 work as adverti
d6890 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 sed on many.** u
d68a0 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 nix systems. Th
d68b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 e following proc
d68c0 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 edure is an atte
d68d0 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 mpt to make.** i
d68e0 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a t work better..*
d68f0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
d6900 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 NO_SYNC macro di
d6910 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 sables all fsync
d6920 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 ()s. This is us
d6930 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 eful.** for test
d6940 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 ing when we want
d6950 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 to run through
d6960 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 71 the test suite q
d6970 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 uickly..** You a
d6980 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 re strongly advi
d6990 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 sed *not* to dep
d69a0 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f loy with SQLITE_
d69b0 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c NO_SYNC.** enabl
d69c0 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e ed, however, sin
d69d0 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e ce with SQLITE_N
d69e0 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 O_SYNC enabled,
d69f0 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f an OS crash.** o
d6a00 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 r power failure
d6a10 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 will likely corr
d6a20 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 65 upt the database
d6a30 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c file..**.** SQL
d6a40 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 74 ite sets the dat
d6a50 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 aOnly flag if th
d6a60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
d6a70 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e le is unchanged.
d6a80 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 .** The idea beh
d6a90 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 ind dataOnly is
d6aa0 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f that it should o
d6ab0 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 69 nly write the fi
d6ac0 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f le content.** to
d6ad0 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 disk, not the i
d6ae0 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 node. We only s
d6af0 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 et dataOnly if t
d6b00 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 he file size is
d6b10 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 .** unchanged si
d6b20 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a nce the file siz
d6b30 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 e is part of the
d6b40 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 inode. However
d6b50 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 , .** Ted Ts'o t
d6b60 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 ells us that fda
d6b70 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c tasync() will al
d6b80 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f so write the ino
d6b90 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c de if the.** fil
d6ba0 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 e size has chang
d6bb0 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 ed. The only re
d6bc0 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 al difference be
d6bd0 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 tween fdatasync(
d6be0 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 ).** and fsync()
d6bf0 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 , Ted tells us,
d6c00 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e is that fdatasyn
d6c10 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 c() will not flu
d6c20 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 sh the.** inode
d6c30 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 if the mtime or
d6c40 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 owner or other i
d6c50 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 20 node attributes
d6c60 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a have changed..**
d6c70 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 We only care ab
d6c80 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a out the file siz
d6c90 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 e, not the other
d6ca0 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 file attributes
d6cb0 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 , so.** as far a
d6cc0 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 s SQLite is conc
d6cd0 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 erned, an fdatas
d6ce0 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 ync() is always
d6cf0 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c adequate..** So,
d6d00 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 we always use f
d6d10 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 datasync() if it
d6d20 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 is available, r
d6d30 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 egardless of.**
d6d40 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
d6d50 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a dataOnly flag..
d6d60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 */.static int fu
d6d70 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c ll_fsync(int fd,
d6d80 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 int fullSync, i
d6d90 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 nt dataOnly){.
d6da0 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 int rc;.. /* Th
d6db0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 e following "ifd
d6dc0 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 ef/elif/else/" b
d6dd0 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d lock has the sam
d6de0 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 e structure as.
d6df0 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f ** the one belo
d6e00 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 w. It is replica
d6e10 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 ted here solely
d6e20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 to avoid clutter
d6e30 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 ing . ** up the
d6e40 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 real code with
d6e50 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d the UNUSED_PARAM
d6e60 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 ETER() macros..
d6e70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
d6e80 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 E_NO_SYNC. UNUS
d6e90 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 ED_PARAMETER(fd)
d6ea0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
d6eb0 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a ETER(fullSync);.
d6ec0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
d6ed0 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 ER(dataOnly);.#e
d6ee0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 lif HAVE_FULLFSY
d6ef0 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 NC. UNUSED_PARA
d6f00 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
d6f10 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f .#else. UNUSED_
d6f20 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 PARAMETER(fullSy
d6f30 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 nc);. UNUSED_PA
d6f40 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 RAMETER(dataOnly
d6f50 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
d6f60 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 Record the numbe
d6f70 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 r of times that
d6f80 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 we do a normal f
d6f90 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a sync() and . **
d6fa0 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 FULLSYNC. This
d6fb0 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 is used during
d6fc0 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 testing to verif
d6fd0 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 y that this proc
d6fe0 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 edure. ** gets
d6ff0 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 called with the
d7000 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 correct argument
d7010 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 s.. */.#ifdef S
d7020 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
d7030 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 fullSync ) sqli
d7040 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
d7050 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f nt++;. sqlite3_
d7060 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 sync_count++;.#e
d7070 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 ndif.. /* If we
d7080 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
d7090 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e he SQLITE_NO_SYN
d70a0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e C flag, then syn
d70b0 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e cing is a. ** n
d70c0 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 o-op. */.#ifdef
d70d0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a SQLITE_NO_SYNC.
d70e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
d70f0 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c ;.#elif HAVE_FUL
d7100 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c LFSYNC. if( ful
d7110 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 lSync ){. rc
d7120 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 = fcntl(fd, F_FU
d7130 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d LLFSYNC, 0);. }
d7140 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 else{. rc = 1
d7150 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 ;. }. /* If th
d7160 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c e FULLFSYNC fail
d7170 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f ed, fall back to
d7180 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 attempting an f
d7190 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 sync().. ** It
d71a0 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 shouldn't be pos
d71b0 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 sible for fullfs
d71c0 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 ync to fail on t
d71d0 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 he local . ** f
d71e0 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f ile system (on O
d71f0 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 SX), so failure
d7200 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46 indicates that F
d7210 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 ULLFSYNC. ** is
d7220 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f n't supported fo
d7230 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 r this file syst
d7240 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 em. So, attempt
d7250 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 an fsync . ** a
d7260 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e nd (for now) ign
d7270 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 ore the overhead
d7280 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 of a superfluou
d7290 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a s fcntl call. .
d72a0 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 ** It'd be bet
d72b0 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 ter to detect fu
d72c0 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 llfsync support
d72d0 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a once and avoid .
d72e0 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 ** the fcntl c
d72f0 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 all every time s
d7300 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 ync is called..
d7310 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 */. if( rc ) r
d7320 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a c = fsync(fd);..
d7330 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64 #else . rc = fd
d7340 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 atasync(fd);.#if
d7350 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 OS_VXWORKS. if
d7360 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e ( rc==-1 && errn
d7370 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 o==ENOTSUP ){.
d7380 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 rc = fsync(fd)
d7390 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
d73a0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 OS_VXWORKS */.#e
d73b0 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 ndif /* ifdef SQ
d73c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 LITE_NO_SYNC eli
d73d0 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
d73e0 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 */.. if( OS_VX
d73f0 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 WORKS && rc!= -1
d7400 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a ){. rc = 0;.
d7410 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d7420 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
d7430 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 ure all writes t
d7440 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 o a particular f
d7450 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 ile are committe
d7460 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a d to disk..**.**
d7470 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 If dataOnly==0
d7480 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 then both the fi
d7490 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 le itself and it
d74a0 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 s metadata (file
d74b0 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 .** size, access
d74c0 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 time, etc) are
d74d0 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 synced. If data
d74e0 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c Only!=0 then onl
d74f0 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 y the.** file da
d7500 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a ta is synced..**
d7510 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 .** Under Unix,
d7520 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 also make sure t
d7530 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 hat the director
d7540 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 y entry for the
d7550 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e file.** has been
d7560 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e created by fsyn
d7570 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 c-ing the direct
d7580 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ory that contain
d7590 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 s the file..** I
d75a0 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 f we do not do t
d75b0 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 his and we encou
d75c0 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 nter a power fai
d75d0 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 lure, the direct
d75e0 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 ory.** entry for
d75f0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 the journal mig
d7600 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 ht not exist aft
d7610 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 er we reboot. T
d7620 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 he next.** SQLit
d7630 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 e to access the
d7640 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e file will not kn
d7650 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ow that the jour
d7660 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 nal exists (beca
d7670 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 use.** the direc
d7680 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 tory entry for t
d7690 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e he journal was n
d76a0 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e ever created) an
d76b0 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f d the transactio
d76c0 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f n.** will not ro
d76d0 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 ll back - possib
d76e0 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 ly leading to da
d76f0 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
d7700 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
d7710 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 unixSync(sqlite
d7720 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d7730 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 flags){. int rc
d7740 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
d7750 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d7760 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 )id;.. int isDa
d7770 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 taOnly = (flags&
d7780 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
d7790 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 ONLY);. int isF
d77a0 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 ullsync = (flags
d77b0 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
d77c0 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 YNC_FULL;.. /*
d77d0 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f Check that one o
d77e0 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f f SQLITE_SYNC_NO
d77f0 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 RMAL or FULL was
d7800 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 passed */. ass
d7810 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 ert((flags&0x0F)
d7820 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f ==SQLITE_SYNC_NO
d7830 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 RMAL. || (f
d7840 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 lags&0x0F)==SQLI
d7850 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 TE_SYNC_FULL. )
d7860 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e ;.. /* Unix can
d7870 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 not, but some sy
d7880 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e stems may return
d7890 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f SQLITE_FULL fro
d78a0 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a m here. This. *
d78b0 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 * line is to tes
d78c0 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 t that doing so
d78d0 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 does not cause a
d78e0 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a ny problems.. *
d78f0 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b /. SimulateDisk
d7900 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 fullError( retur
d7910 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b n SQLITE_FULL );
d7920 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
d7930 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 e );. OSTRACE2(
d7940 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 "SYNC %-3d\n"
d7950 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 , pFile->h);. r
d7960 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 c = full_fsync(p
d7970 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 File->h, isFulls
d7980 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 ync, isDataOnly)
d7990 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
d79a0 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 ror( rc=1 );. i
d79b0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69 f( rc ){. pFi
d79c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d79d0 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 errno;. retur
d79e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 n SQLITE_IOERR_F
d79f0 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 SYNC;. }. if(
d7a00 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 pFile->dirfd>=0
d7a10 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a ){. int err;.
d7a20 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 49 OSTRACE4("DI
d7a30 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65 RSYNC %-3d (have
d7a40 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 _fullfsync=%d fu
d7a50 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 llsync=%d)\n", p
d7a60 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 File->dirfd,.
d7a70 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 55 HAVE_FU
d7a80 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 LLFSYNC, isFulls
d7a90 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ync);.#ifndef SQ
d7aa0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 LITE_DISABLE_DIR
d7ab0 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 SYNC. /* The
d7ac0 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 directory sync i
d7ad0 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 s only attempted
d7ae0 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 if full_fsync i
d7af0 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 s. ** turned
d7b00 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 off or unavailab
d7b10 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 le. If a full_f
d7b20 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62 sync occurred ab
d7b30 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e ove,. ** then
d7b40 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 the directory s
d7b50 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f ync is superfluo
d7b60 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 us.. */. i
d7b70 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 f( (!HAVE_FULLFS
d7b80 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 YNC || !isFullsy
d7b90 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e nc) && full_fsyn
d7ba0 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 c(pFile->dirfd,0
d7bb0 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a ,0) ){. /*
d7bc0 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61 . ** We ha
d7bd0 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74 ve received mult
d7be0 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20 iple reports of
d7bf0 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e fsync() returnin
d7c00 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f g. ** erro
d7c10 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20 rs when applied
d7c20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f to directories o
d7c30 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73 n certain file s
d7c40 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a ystems.. *
d7c50 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 63 * A failed direc
d7c60 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 tory sync is not
d7c70 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f a big deal. So
d7c80 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20 it seems.
d7c90 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67 ** better to ig
d7ca0 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20 nore the error.
d7cb0 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20 Ticket #1657.
d7cc0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f */. /
d7cd0 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 * pFile->lastErr
d7ce0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 no = errno; */.
d7cf0 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 /* return
d7d00 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f SQLITE_IOERR; */
d7d10 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
d7d20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 err = close(pF
d7d30 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 ile->dirfd); /*
d7d40 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e Only need to syn
d7d50 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 c once, so close
d7d60 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 the */. if(
d7d70 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 err==0 ){
d7d80 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74 /* direct
d7d90 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 20 ory when we are
d7da0 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 done */. pF
d7db0 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b ile->dirfd = -1;
d7dc0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
d7dd0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d7de0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d7df0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
d7e00 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 ERR_DIR_CLOSE;.
d7e10 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
d7e20 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
d7e30 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
d7e40 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
d7e50 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
d7e60 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 ic int unixTrunc
d7e70 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
d7e80 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 *id, i64 nByte)
d7e90 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
d7ea0 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 sert( id );. Si
d7eb0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d7ec0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d7ed0 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 RR_TRUNCATE );.
d7ee0 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 rc = ftruncate(
d7ef0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d7f00 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 >h, (off_t)nByte
d7f10 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
d7f20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
d7f30 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
d7f40 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 errno;. retur
d7f50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
d7f60 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 RUNCATE;. }else
d7f70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d7f80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f ITE_OK;. }.}../
d7f90 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
d7fa0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
d7fb0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
d7fc0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
d7fd0 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 unixFileSize(sq
d7fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d7ff0 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 i64 *pSize){. i
d8000 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 nt rc;. struct
d8010 73 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 stat buf;. asse
d8020 72 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d rt( id );. rc =
d8030 20 66 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c fstat(((unixFil
d8040 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 e*)id)->h, &buf)
d8050 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
d8060 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 ror( rc=1 );. i
d8070 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 f( rc!=0 ){.
d8080 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d8090 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d80a0 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 no;. return S
d80b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 QLITE_IOERR_FSTA
d80c0 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 T;. }. *pSize
d80d0 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a = buf.st_size;..
d80e0 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e /* When openin
d80f0 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 g a zero-size da
d8100 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 tabase, the find
d8110 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 LockInfo() proce
d8120 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 dure. ** writes
d8130 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 a single byte i
d8140 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e nto that file in
d8150 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 order to work a
d8160 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a round a bug. **
d8170 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 in the OS-X msd
d8180 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 os filesystem.
d8190 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 In order to avoi
d81a0 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 d problems with
d81b0 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 upper. ** layer
d81c0 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 s, we need to re
d81d0 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 port this file s
d81e0 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e ize as zero even
d81f0 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 though it is.
d8200 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 ** really 1. T
d8210 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a icket #3260.. *
d8220 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d /. if( *pSize==
d8230 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 1 ) *pSize = 0;.
d8240 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
d8250 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c E_OK;.}..#if SQL
d8260 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d8270 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
d8280 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f ned(__APPLE__)./
d8290 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 *.** Handler for
d82a0 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 proxy-locking f
d82b0 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 ile-control verb
d82c0 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f s. Defined belo
d82d0 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 w in the.** prox
d82e0 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 ying locking div
d82f0 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ision..*/.static
d8300 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f int proxyFileCo
d8310 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
d8320 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a le*,int,void*);.
d8330 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 #endif.../*.** I
d8340 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 nformation and c
d8350 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 ontrol of an ope
d8360 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a n file handle..*
d8370 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d8380 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c xFileControl(sql
d8390 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d83a0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
d83b0 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 g){. switch( op
d83c0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
d83d0 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 ITE_FCNTL_LOCKST
d83e0 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 ATE: {. *(i
d83f0 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 nt*)pArg = ((uni
d8400 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b xFile*)id)->lock
d8410 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75 type;. retu
d8420 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d8430 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
d8440 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 ITE_LAST_ERRNO:
d8450 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 {. *(int*)p
d8460 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 Arg = ((unixFile
d8470 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
d8480 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
d8490 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
d84a0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
d84b0 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 /* The pager
d84c0 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f calls this metho
d84d0 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 d to signal that
d84e0 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 it has done.
d84f0 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 ** a rollback a
d8500 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 nd that the data
d8510 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72 base is therefor
d8520 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a e unchanged and.
d8530 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 ** it hence
d8540 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 it is OK for the
d8550 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 transaction cha
d8560 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 nge counter to b
d8570 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 e. ** unchang
d8580 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 ed.. */. c
d8590 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
d85a0 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b _DB_UNCHANGED: {
d85b0 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c . ((unixFil
d85c0 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65 e*)id)->dbUpdate
d85d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
d85e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d85f0 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 }.#endif.#if S
d8600 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
d8610 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
d8620 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
d8630 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
d8640 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
d8650 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c LE:. case SQL
d8660 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
d8670 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 YFILE: {. r
d8680 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 eturn proxyFileC
d8690 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 ontrol(id,op,pAr
d86a0 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 g);. }.#endif
d86b0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
d86c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d86d0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
d86e0 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 LE__) */. }. r
d86f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
d8700 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
d8710 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
d8720 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
d8730 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
d8740 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
d8750 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
d8760 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
d8770 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
d8780 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
d8790 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
d87a0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
d87b0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
d87c0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
d87d0 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
d87e0 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
d87f0 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
d8800 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
d8810 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
d8820 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
d8830 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
d8840 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
d8850 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
d8860 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
d8870 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
d8880 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
d8890 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
d88a0 74 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 t unixSectorSize
d88b0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e (sqlite3_file *N
d88c0 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
d88d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
d88e0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 sed);. return S
d88f0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
d8900 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a CTOR_SIZE;.}../*
d8910 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 .** Return the d
d8920 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
d8930 73 74 69 63 73 20 66 6f 72 20 74 68 65 20 66 69 stics for the fi
d8940 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 le. This is alwa
d8950 79 73 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a ys 0 for unix..*
d8960 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d8970 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 xDeviceCharacter
d8980 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 istics(sqlite3_f
d8990 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 ile *NotUsed){.
d89a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d89b0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 R(NotUsed);. re
d89c0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
d89d0 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 69 Here ends the i
d89e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
d89f0 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c all sqlite3_fil
d8a00 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a e methods..**.**
d8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a20 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 **** End sqlite3
d8a30 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a _file Methods **
d8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
d8ab0 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69 *.** This divisi
d8ac0 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 on contains defi
d8ad0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 nitions of sqlit
d8ae0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
d8af0 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d jects that.** im
d8b00 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 plement various
d8b10 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 file locking str
d8b20 61 74 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73 ategies. It als
d8b30 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e o contains defin
d8b40 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 itions.** of "fi
d8b50 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e nder" functions.
d8b60 20 20 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 A finder-funct
d8b70 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c ion is used to l
d8b80 6f 63 61 74 65 20 74 68 65 20 61 70 70 72 6f 70 ocate the approp
d8b90 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 riate.** sqlite3
d8ba0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
d8bb0 63 74 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 ct for a particu
d8bc0 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c lar database fil
d8bd0 65 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 e. The pAppData
d8be0 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
d8bf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53 sqlite3_vfs VFS
d8c00 20 6f 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69 objects are ini
d8c10 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 tialized to be p
d8c20 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 ointers to.** th
d8c30 65 20 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72 e correct finder
d8c40 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 -function for th
d8c50 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f at VFS..**.** Mo
d8c60 73 74 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 st finder functi
d8c70 6f 6e 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69 ons return a poi
d8c80 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 nter to a fixed
d8c90 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
d8ca0 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 ds.** object. T
d8cb0 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 he only interest
d8cc0 69 6e 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 ing finder-funct
d8cd0 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 ion is autolockI
d8ce0 6f 46 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a oFinder, which.*
d8cf0 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 * looks at the f
d8d00 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 61 ilesystem type a
d8d10 6e 64 20 74 72 69 65 73 20 74 6f 20 67 75 65 73 nd tries to gues
d8d20 73 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 s the best locki
d8d30 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66 ng.** strategy f
d8d40 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 rom that..**.**
d8d50 46 6f 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 For finder-funti
d8d60 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 on F, two object
d8d70 73 20 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a s are created:.*
d8d80 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20 *.** (1) The
d8d90 72 65 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 real finder-func
d8da0 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 tion named "FImp
d8db0 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 t()"..**.** (
d8dc0 32 29 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 2) A constant po
d8dd0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 inter to this fu
d8de0 6e 63 74 69 6f 20 6e 61 6d 65 64 20 6a 75 73 74 nctio named just
d8df0 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 "F"..**.**.** A
d8e00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
d8e10 46 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 F pointer is use
d8e20 64 20 61 73 20 74 68 65 20 70 41 70 70 44 61 74 d as the pAppDat
d8e30 61 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53 0a a value for VFS.
d8e40 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 ** objects. We
d8e50 68 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73 20 have to do this
d8e60 69 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74 69 instead of letti
d8e70 6e 67 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e ng pAppData poin
d8e80 74 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61 74 t.** directly at
d8e90 20 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 the finder-func
d8ea0 74 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20 72 tion since C90 r
d8eb0 75 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20 76 ules prevent a v
d8ec0 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 oid*.** from be
d8ed0 63 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 cast into a func
d8ee0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a tion pointer..**
d8ef0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 .**.** Each inst
d8f00 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61 63 ance of this mac
d8f10 72 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f ro generates two
d8f20 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 objects:.**.**
d8f30 20 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 * A constant
d8f40 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
d8f50 64 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d ds object call M
d8f60 45 54 48 4f 44 20 74 68 61 74 20 68 61 73 20 6c ETHOD that has l
d8f70 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d ocking.** m
d8f80 65 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f ethods CLOSE, LO
d8f90 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 CK, UNLOCK, CKRE
d8fa0 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a SLOCK..**.** *
d8fb0 20 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 An I/O method
d8fc0 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 finder function
d8fd0 63 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74 68 called FINDER th
d8fe0 61 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 at returns a poi
d8ff0 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 nter.** to
d9000 74 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 the METHOD objec
d9010 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 t in the previou
d9020 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 s bullet..*/.#de
d9030 66 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 fine IOMETHODS(F
d9040 49 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 INDER, METHOD, C
d9050 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f LOSE, LOCK, UNLO
d9060 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 CK, CKLOCK)
d9070 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 \.stat
d9080 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
d9090 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 _io_methods METH
d90a0 4f 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 20 OD = {
d90b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d90c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31 2c \. 1,
d90d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d90e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
d90f0 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 rsion */
d9100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f \. CLO
d9120 53 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 SE,
d9130 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
d9140 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 se */
d9150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9160 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 \. unix
d9170 52 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 Read,
d9180 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 /* xRead
d9190 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d91a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d91b0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 \. unixW
d91c0 72 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 rite,
d91d0 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 /* xWrite
d91e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d91f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9200 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 \. unixTr
d9210 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 uncate,
d9220 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 /* xTrunca
d9230 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 te */
d9240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9250 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e \. unixSyn
d9260 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
d9270 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f /* xSync */
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d92a0 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 \. unixFile
d92b0 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 Size,
d92c0 20 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 /* xFileSize
d92d0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d92e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d92f0 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 \. LOCK,
d9300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9310 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 /* xLock */
d9320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9340 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 \. UNLOCK,
d9350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9360 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 /* xUnlock */
d9370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9390 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 \. CKLOCK,
d93a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d93b0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
d93c0 65 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 edLock */
d93d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
d93e0 0a 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 . unixFileCont
d93f0 72 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 rol,
d9400 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 /* xFileControl
d9410 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d9420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
d9430 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a unixSectorSiz
d9440 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e, /
d9450 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f * xSectorSize */
d9460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9470 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
d9480 20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 unixDeviceChar
d9490 61 63 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a acteristics /*
d94a0 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 xDeviceCapabili
d94b0 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20 ties */
d94c0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b \.};
d94d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d94e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d94f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9510 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 \.sta
d9520 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
d9530 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 3_io_methods *FI
d9540 4e 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 NDER##Impl(const
d9550 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 68 29 char *z, int h)
d9560 7b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 55 4e { \. UN
d9570 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
d9580 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 ); UNUSED_PARAME
d9590 54 45 52 28 68 29 3b 20 20 20 20 20 20 20 20 20 TER(h);
d95a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65 74 \. ret
d95c0 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 urn &METHOD;
d95d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9600 20 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 \.}
d9610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9650 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
d9660 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
d9670 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 _methods *(*cons
d9680 74 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 t FINDER)(const
d9690 63 68 61 72 2a 2c 69 6e 74 29 20 20 20 20 20 20 char*,int)
d96a0 20 20 20 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 \. = FI
d96b0 4e 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a NDER##Impl;../*.
d96c0 2a 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c 20 ** Here are all
d96d0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 of the sqlite3_i
d96e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
d96f0 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 s for each of th
d9700 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 e.** locking str
d9710 61 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74 69 ategies. Functi
d9720 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20 ons that return
d9730 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 73 pointers to thes
d9740 65 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 e methods.** are
d9750 20 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a also created..*
d9760 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 /.IOMETHODS(. p
d9770 6f 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 osixIoFinder,
d9780 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
d9790 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
d97a0 20 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 */. posixIoMet
d97b0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 hods,
d97c0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
d97d0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
d97e0 65 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 e */. unixClose
d97f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d9800 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f /* xClose metho
d9810 64 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c d */. unixLock,
d9820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9830 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 /* xLock method
d9840 20 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b */. unixUnlock
d9850 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d9860 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f /* xUnlock metho
d9870 64 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b d */. unixCheck
d9880 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
d9890 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
d98a0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f edLock method */
d98b0 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 .).IOMETHODS(.
d98c0 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 nolockIoFinder,
d98d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
d98e0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
d98f0 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d e */. nolockIoM
d9900 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 ethods,
d9910 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
d9920 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
d9930 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c me */. nolockCl
d9940 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ose,
d9950 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
d9960 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f od */. nolockLo
d9970 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
d9980 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
d9990 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c d */. nolockUnl
d99a0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
d99b0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
d99c0 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 od */. nolockCh
d99d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
d99e0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
d99f0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
d9a00 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 /.).IOMETHODS(.
d9a10 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 dotlockIoFinder
d9a20 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 , /* Fi
d9a30 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 nder function na
d9a40 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 me */. dotlockI
d9a50 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
d9a60 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f /* sqlite3_io_
d9a70 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e methods object n
d9a80 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b ame */. dotlock
d9a90 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
d9aa0 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 /* xClose met
d9ab0 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b hod */. dotlock
d9ac0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
d9ad0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 /* xLock meth
d9ae0 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 od */. dotlockU
d9af0 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
d9b00 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 /* xUnlock met
d9b10 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b hod */. dotlock
d9b20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d9b30 6b 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 k /* xCheckRese
d9b40 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 rvedLock method
d9b50 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 */.)..#if SQLITE
d9b60 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
d9b70 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 STYLE && !OS_VXW
d9b80 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a ORKS.IOMETHODS(.
d9b90 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c flockIoFinder,
d9ba0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
d9bb0 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e inder function n
d9bc0 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f ame */. flockIo
d9bd0 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 Methods,
d9be0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f /* sqlite3_io
d9bf0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
d9c00 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 name */. flockC
d9c10 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 lose,
d9c20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 /* xClose me
d9c30 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c thod */. flockL
d9c40 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
d9c50 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 /* xLock met
d9c60 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e hod */. flockUn
d9c70 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 lock,
d9c80 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 /* xUnlock me
d9c90 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 thod */. flockC
d9ca0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
d9cb0 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 /* xCheckRes
d9cc0 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 ervedLock method
d9cd0 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 */.).#endif..#i
d9ce0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d f OS_VXWORKS.IOM
d9cf0 45 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 ETHODS(. semIoF
d9d00 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
d9d10 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 /* Finder fu
d9d20 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
d9d30 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 semIoMethods,
d9d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 /* sq
d9d50 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
d9d60 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a object name */.
d9d70 20 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 semClose,
d9d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
d9d90 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a Close method */.
d9da0 20 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 semLock,
d9db0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
d9dc0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 Lock method */.
d9dd0 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 semUnlock,
d9de0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 /* xU
d9df0 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a nlock method */.
d9e00 20 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 semCheckReserv
d9e10 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 edLock /* x
d9e20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d9e30 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 k method */.).#e
d9e40 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 ndif..#if define
d9e50 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 d(__APPLE__) &&
d9e60 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
d9e70 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 CKING_STYLE.IOME
d9e80 54 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 THODS(. afpIoFi
d9e90 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 nder,
d9ea0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e /* Finder fun
d9eb0 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
d9ec0 61 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 afpIoMethods,
d9ed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c /* sql
d9ee0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
d9ef0 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 object name */.
d9f00 20 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 afpClose,
d9f10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
d9f20 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lose method */.
d9f30 20 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 afpLock,
d9f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
d9f50 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
d9f60 61 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 afpUnlock,
d9f70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
d9f80 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lock method */.
d9f90 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 afpCheckReserve
d9fa0 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 dLock /* xC
d9fb0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
d9fc0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e method */.).#en
d9fd0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 dif../*.** The p
d9fe0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 roxy locking met
d9ff0 68 6f 64 20 69 73 20 61 20 22 73 75 70 65 72 2d hod is a "super-
da000 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 method" in the s
da010 65 6e 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 ense that it.**
da020 6f 70 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 opens secondary
da030 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
da040 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 for the conch a
da050 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e nd lock files an
da060 64 0a 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f d.** it uses pro
da070 78 79 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 xy, dot-file, AF
da080 50 2c 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c P, and flock() l
da090 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f ocking methods o
da0a0 6e 20 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e n those.** secon
da0b0 64 61 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 dary files. For
da0c0 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 this reason, th
da0d0 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 e division that
da0e0 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 implements.** pr
da0f0 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c oxy locking is l
da100 6f 63 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 ocated much furt
da110 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 her down in the
da120 66 69 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 file. But we ne
da130 65 64 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 ed.** to go ahea
da140 64 20 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 d and define the
da150 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
da160 6f 64 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 ods and finder f
da170 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 unction.** for p
da180 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 roxy locking her
da190 65 2e 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 e. So we forwar
da1a0 64 20 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f d declare the I/
da1b0 4f 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 O methods..*/.#i
da1c0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
da1d0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
da1e0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
da1f0 59 4c 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 YLE.static int p
da200 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 roxyClose(sqlite
da210 33 5f 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 3_file*);.static
da220 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 int proxyLock(s
da230 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
da240 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 t);.static int p
da250 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 roxyUnlock(sqlit
da260 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
da270 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
da280 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
da290 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c k(sqlite3_file*,
da2a0 20 69 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 int*);.IOMETHOD
da2b0 53 28 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 S(. proxyIoFind
da2c0 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f er, /
da2d0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
da2e0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 n name */. prox
da2f0 79 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 yIoMethods,
da300 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
da310 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
da320 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f ct name */. pro
da330 78 79 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 xyClose,
da340 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
da350 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f method */. pro
da360 78 79 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 xyLock,
da370 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
da380 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 method */. prox
da390 79 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 yUnlock,
da3a0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
da3b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f method */. pro
da3c0 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xyCheckReservedL
da3d0 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b ock /* xCheck
da3e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
da3f0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
da400 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
da410 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
da420 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
da430 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 G_STYLE./* .** T
da440 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e his "finder" fun
da450 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
da460 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
da470 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 best locking str
da480 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 ategy .** for th
da490 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
da4a0 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 "filePath". It
da4b0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 then returns the
da4c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
da4d0 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 ods.** object th
da4e0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
da4f0 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a at strategy..**.
da500 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d ** This is for M
da510 61 63 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 acOSX only..*/.s
da520 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
da530 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
da540 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
da550 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 Impl(. const ch
da560 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 ar *filePath,
da570 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 /* name of the
da580 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
da590 0a 20 20 69 6e 74 20 66 64 20 20 20 20 20 20 20 . int fd
da5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
da5b0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f ile descriptor o
da5c0 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 pen on the datab
da5d0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 ase file */.){.
da5e0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 static const st
da5f0 72 75 63 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20 ruct Mapping {.
da600 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
da610 46 69 6c 65 73 79 73 74 65 6d 3b 20 20 20 20 20 Filesystem;
da620 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
da630 73 79 73 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 system type name
da640 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 */. const sq
da650 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
da660 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a *pMethods; /*
da670 20 41 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 Appropriate loc
da680 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 king method */.
da690 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 } aMap[] = {.
da6a0 20 20 7b 20 22 68 66 73 22 2c 20 20 20 20 26 70 { "hfs", &p
da6b0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c osixIoMethods },
da6c0 0a 20 20 20 20 7b 20 22 75 66 73 22 2c 20 20 20 . { "ufs",
da6d0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 &posixIoMethods
da6e0 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 },. { "afpfs
da6f0 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 ", &afpIoMethod
da700 73 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 s },.#ifdef SQLI
da710 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f TE_ENABLE_AFP_LO
da720 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20 CKING_SMB. {
da730 22 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f "smbfs", &afpIo
da740 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 Methods },.#else
da750 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 . { "smbfs",
da760 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 &flockIoMethods
da770 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b },.#endif. {
da780 20 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f "webdav", &nolo
da790 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 ckIoMethods },.
da7a0 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b { 0, 0 }. };
da7b0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
da7c0 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f ct statfs fsInfo
da7d0 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b ;. struct flock
da7e0 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 lockInfo;.. if
da7f0 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 ( !filePath ){.
da800 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 /* If filePat
da810 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 h==NULL that mea
da820 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e ns we are dealin
da830 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 g with a transie
da840 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 nt file. ** t
da850 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 hat does not nee
da860 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 d to be locked.
da870 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e */. return &n
da880 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a olockIoMethods;.
da890 20 20 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73 }. if( statfs
da8a0 28 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e (filePath, &fsIn
da8b0 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 fo) != -1 ){.
da8c0 20 69 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c if( fsInfo.f_fl
da8d0 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 ags & MNT_RDONLY
da8e0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
da8f0 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
da900 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 s;. }. for
da910 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 (i=0; aMap[i].zF
da920 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b ilesystem; i++){
da930 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d . if( strcm
da940 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 p(fsInfo.f_fstyp
da950 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a ename, aMap[i].z
da960 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 Filesystem)==0 )
da970 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
da980 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 aMap[i].pMethod
da990 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d s;. }. }
da9a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 . }.. /* Defau
da9b0 6c 74 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 lt case. Handles
da9c0 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 , amongst others
da9d0 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 , "nfs".. ** Te
da9e0 73 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f st byte-range lo
da9f0 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 ck using fcntl()
daa00 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 . If the call su
daa10 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 cceeds, . ** as
daa20 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 sume that the fi
daa30 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le-system suppor
daa40 74 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c ts POSIX style l
daa50 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f ocks. . */. lo
daa60 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 ckInfo.l_len = 1
daa70 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 ;. lockInfo.l_s
daa80 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b tart = 0;. lock
daa90 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 Info.l_whence =
daaa0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b SEEK_SET;. lock
daab0 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f Info.l_type = F_
daac0 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e RDLCK;. if( fcn
daad0 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 tl(fd, F_GETLK,
daae0 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 &lockInfo)!=-1 )
daaf0 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 {. return &p
dab00 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 osixIoMethods;.
dab10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
dab20 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 rn &dotlockIoMet
dab30 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 hods;. }.}.stat
dab40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
dab50 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 _io_methods *(*c
dab60 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 onst autolockIoF
dab70 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 inder)(const cha
dab80 72 2a 2c 69 6e 74 29 0a 20 20 20 20 20 20 20 20 r*,int).
dab90 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 = autolockIoFind
daba0 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 erImpl;..#endif
dabb0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 /* defined(__APP
dabc0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
dabd0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dabe0 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f TYLE */..#if OS_
dabf0 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 VXWORKS && SQLIT
dac00 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
dac10 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 _STYLE./* .** Th
dac20 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 is "finder" func
dac30 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f tion attempts to
dac40 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 determine the b
dac50 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 est locking stra
dac60 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 tegy .** for the
dac70 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 database file "
dac80 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 filePath". It t
dac90 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 hen returns the
daca0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
dacb0 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 ds.** object tha
dacc0 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 t implements tha
dacd0 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a t strategy..**.*
dace0 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 56 58 * This is for VX
dacf0 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 Works only..*/.s
dad00 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
dad10 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
dad20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
dad30 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 Impl(. const ch
dad40 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 ar *filePath,
dad50 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 /* name of the
dad60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
dad70 0a 20 20 69 6e 74 20 66 64 20 20 20 20 20 20 20 . int fd
dad80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
dad90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f ile descriptor o
dada0 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 pen on the datab
dadb0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 ase file */.){.
dadc0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
dadd0 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 ckInfo;.. if( !
dade0 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 filePath ){.
dadf0 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d /* If filePath==
dae00 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 NULL that means
dae10 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
dae20 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 ith a transient
dae30 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 file. ** that
dae40 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
dae50 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a o be locked. */.
dae60 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f return &nolo
dae70 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ckIoMethods;. }
dae80 0a 0a 20 20 2f 2a 20 54 65 73 74 20 69 66 20 66 .. /* Test if f
dae90 63 6e 74 6c 28 29 20 69 73 20 73 75 70 70 6f 72 cntl() is suppor
daea0 74 65 64 20 61 6e 64 20 75 73 65 20 50 4f 53 49 ted and use POSI
daeb0 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 X style locks..
daec0 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 ** Otherwise fa
daed0 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 6e ll back to the n
daee0 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6d amed semaphore m
daef0 65 74 68 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f ethod.. */. lo
daf00 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 ckInfo.l_len = 1
daf10 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 ;. lockInfo.l_s
daf20 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b tart = 0;. lock
daf30 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 Info.l_whence =
daf40 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b SEEK_SET;. lock
daf50 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f Info.l_type = F_
daf60 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e RDLCK;. if( fcn
daf70 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 tl(fd, F_GETLK,
daf80 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 &lockInfo)!=-1 )
daf90 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 {. return &p
dafa0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 osixIoMethods;.
dafb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
dafc0 72 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 rn &semIoMethods
dafd0 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 ;. }.}.static c
dafe0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
daff0 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 methods *(*const
db000 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
db010 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 r)(const char*,i
db020 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20 61 75 nt). = au
db030 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d tolockIoFinderIm
db040 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f pl;..#endif /* O
db050 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c S_VXWORKS && SQL
db060 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
db070 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a NG_STYLE */../*.
db080 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 74 20 74 ** An abstract t
db090 79 70 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65 ype for a pointe
db0a0 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64 r to a IO method
db0b0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
db0c0 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e :.*/.typedef con
db0d0 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
db0e0 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f thods *(*finder_
db0f0 74 79 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72 type)(const char
db100 2a 2c 69 6e 74 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a *,int);.../*****
db110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db150 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
db160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db170 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 **** sqlite3_vfs
db180 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a methods *******
db190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db1a0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
db1b0 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 division contai
db1c0 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ns the implement
db1d0 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73 ation of methods
db1e0 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 on the.** sqlit
db1f0 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 2a e3_vfs object..*
db200 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c /../*.** Initial
db210 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ize the contents
db220 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 of the unixFile
db230 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 structure point
db240 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a 2f ed to by pId..*/
db250 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c .static int fill
db260 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73 71 InUnixFile(. sq
db270 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
db280 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
db290 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a to vfs object *
db2a0 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20 /. int h,
db2b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
db2c0 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 pen file descrip
db2d0 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69 6e tor of file bein
db2e0 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e g opened */. in
db2f0 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20 20 t dirfd,
db300 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f /* Directo
db310 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ry file descript
db320 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f or */. sqlite3_
db330 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 20 file *pId,
db340 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65 20 /* Write to the
db350 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
db360 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e re here */. con
db370 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
db380 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 me, /* Name of
db390 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20 6f the file being o
db3a0 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e pened */. int n
db3b0 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 oLock,
db3c0 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b 69 /* Omit locki
db3d0 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 ng if true */.
db3e0 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 int isDelete
db3f0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 /* Delet
db400 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 e on close if tr
db410 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 ue */.){. const
db420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
db430 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74 79 ods *pLockingSty
db440 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a le;. unixFile *
db450 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 pNew = (unixFile
db460 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72 63 *)pId;. int rc
db470 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
db480 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 assert( pNew->p
db490 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20 Lock==NULL );.
db4a0 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4f assert( pNew->pO
db4b0 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 pen==NULL );..
db4c0 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73 44 /* Parameter isD
db4d0 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 73 elete is only us
db4e0 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 0a 20 ed on vxworks..
db4f0 20 2a 2a 20 45 78 70 72 65 73 73 20 74 68 69 73 ** Express this
db500 20 65 78 70 6c 69 63 69 74 6c 79 20 68 65 72 65 explicitly here
db510 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f 6d 70 to prevent comp
db520 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 0a 20 20 iler warnings.
db530 2a 2a 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 ** about unused
db540 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f parameters.. */
db550 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 .#if !OS_VXWORKS
db560 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
db570 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 23 TER(isDelete);.#
db580 65 6e 64 69 66 0a 0a 20 20 4f 53 54 52 41 43 45 endif.. OSTRACE
db590 33 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 3("OPEN %-3d
db5a0 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e %s\n", h, zFilen
db5b0 61 6d 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77 ame); . pNew
db5c0 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d ->h = h;. pNew-
db5d0 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a >dirfd = dirfd;.
db5e0 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70 SET_THREADID(p
db5f0 4e 65 77 29 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 New);..#if OS_VX
db600 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 WORKS. pNew->pI
db610 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 d = vxworksFindF
db620 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 ileId(zFilename)
db630 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 ;. if( pNew->pI
db640 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f d==0 ){. noLo
db650 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d ck = 1;. rc =
db660 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
db670 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 }.#endif.. if(
db680 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 noLock ){. p
db690 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 LockingStyle = &
db6a0 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b nolockIoMethods;
db6b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c . }else{. pL
db6c0 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a ockingStyle = (*
db6d0 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 *(finder_type*)p
db6e0 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a Vfs->pAppData)(z
db6f0 46 69 6c 65 6e 61 6d 65 2c 20 68 29 3b 0a 23 69 Filename, h);.#i
db700 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
db710 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
db720 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65 /* Cache zFile
db730 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b name in the lock
db740 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50 ing context (AFP
db750 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 and dotlock ove
db760 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a rride) for. *
db770 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69 * proxyLock acti
db780 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 vation is possib
db790 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79 le (remote proxy
db7a0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20 is based on db
db7b0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69 name). ** zFi
db7c0 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76 lename remains v
db7d0 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20 alid until file
db7e0 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75 is closed, to su
db7f0 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65 pport */. pNe
db800 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
db810 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65 t = (void*)zFile
db820 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d name;.#endif. }
db830 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 .. if( pLocking
db840 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49 Style == &posixI
db850 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
db860 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
db870 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
db880 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
db890 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
db8a0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75 w->pOpen);. u
db8b0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
db8c0 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 . }..#if SQLITE
db8d0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
db8e0 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
db8f0 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c (__APPLE__). el
db900 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 se if( pLockingS
db910 74 79 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65 tyle == &afpIoMe
db920 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
db930 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 AFP locking uses
db940 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 the file path s
db950 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 o it needs to be
db960 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 included in.
db970 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 ** the afpLocki
db980 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a ngContext.. *
db990 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 /. afpLocking
db9a0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 Context *pCtx;.
db9b0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 pNew->locking
db9c0 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d Context = pCtx =
db9d0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
db9e0 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 sizeof(*pCtx) )
db9f0 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d ;. if( pCtx==
dba00 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
dba10 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
dba20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
dba30 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 * NB: zFilename
dba40 65 78 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69 exists and remai
dba50 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 ns valid until t
dba60 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 he file is close
dba70 64 0a 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72 d. ** accor
dba80 64 69 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d ding to requirem
dba90 65 6e 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20 ent F11141. So
dbaa0 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 we do not need t
dbab0 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a o make a. *
dbac0 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69 * copy of the fi
dbad0 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 lename. */.
dbae0 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 pCtx->dbPath =
dbaf0 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 zFilename;.
dbb00 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 srandomdev();.
dbb10 20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 unixEnterMu
dbb20 74 65 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20 tex();. rc
dbb30 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 = findLockInfo(p
dbb40 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 New, NULL, &pNew
dbb50 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 ->pOpen);.
dbb60 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
dbb70 3b 20 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a ; . }.
dbb80 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c }.#endif.. el
dbb90 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 se if( pLockingS
dbba0 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b tyle == &dotlock
dbbb0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
dbbc0 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b /* Dotfile lock
dbbd0 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c ing uses the fil
dbbe0 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 e path so it nee
dbbf0 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 ds to be include
dbc00 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 d in. ** the
dbc10 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f dotlockLockingCo
dbc20 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20 ntext . */.
dbc30 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c char *zLockFil
dbc40 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 e;. int nFile
dbc50 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e name;. nFilen
dbc60 61 6d 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 ame = (int)strle
dbc70 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36 n(zFilename) + 6
dbc80 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 ;. zLockFile
dbc90 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
dbca0 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 3_malloc(nFilena
dbcb0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f me);. if( zLo
dbcc0 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 ckFile==0 ){.
dbcd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
dbce0 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b OMEM;. }else{
dbcf0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
dbd00 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d nprintf(nFilenam
dbd10 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 e, zLockFile, "%
dbd20 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 s" DOTLOCK_SUFFI
dbd30 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 X, zFilename);.
dbd40 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c }. pNew->l
dbd50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
dbd60 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a zLockFile;. }..
dbd70 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
dbd80 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 else if( pLocki
dbd90 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49 ngStyle == &semI
dbda0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
dbdb0 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f /* Named semapho
dbdc0 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 re locking uses
dbdd0 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f the file path so
dbde0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a it needs to be.
dbdf0 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 ** included
dbe00 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e in the semLockin
dbe10 67 43 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a gContext. */.
dbe20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 unixEnterMut
dbe30 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 ex();. rc = f
dbe40 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 indLockInfo(pNew
dbe50 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 , &pNew->pLock,
dbe60 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 &pNew->pOpen);.
dbe70 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 if( (rc==SQLI
dbe80 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d TE_OK) && (pNew-
dbe90 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 >pOpen->pSem==NU
dbea0 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61 LL) ){. cha
dbeb0 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e r *zSemName = pN
dbec0 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e ew->pOpen->aSemN
dbed0 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e ame;. int n
dbee0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
dbef0 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
dbf00 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c HNAME, zSemName,
dbf10 20 22 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 "%s.sem",.
dbf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dbf30 20 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 pNew->pId->zCa
dbf40 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 nonicalName);.
dbf50 20 20 20 20 66 6f 72 28 20 6e 3d 30 3b 20 7a 53 for( n=0; zS
dbf60 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 emName[n]; n++ )
dbf70 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 65 . if( zSe
dbf80 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 mName[n]=='/' )
dbf90 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f zSemName[n] = '_
dbfa0 27 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 ';. pNew->p
dbfb0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d Open->pSem = sem
dbfc0 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 _open(zSemName,
dbfd0 4f 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20 31 O_CREAT, 0666, 1
dbfe0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 );. if( pNe
dbff0 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d w->pOpen->pSem =
dc000 3d 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a = SEM_FAILED ){.
dc010 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
dc020 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
dc030 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e pNew->pOpen->
dc040 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c aSemName[0] = '\
dc050 30 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0';. }.
dc060 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d }. unixLeaveM
dc070 75 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 utex();. }.#end
dc080 69 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 if. . pNew->la
dc090 73 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 stErrno = 0;.#if
dc0a0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 OS_VXWORKS. if
dc0b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
dc0c0 29 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 ){. unlink(zF
dc0d0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 ilename);. is
dc0e0 44 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a Delete = 0;. }.
dc0f0 20 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 pNew->isDelete
dc100 20 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e = isDelete;.#en
dc110 64 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53 51 dif. if( rc!=SQ
dc120 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
dc130 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c f( dirfd>=0 ) cl
dc140 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 ose(dirfd); /* s
dc150 69 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 ilent leak if fa
dc160 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 il, already in e
dc170 72 72 6f 72 20 2a 2f 0a 20 20 20 20 63 6c 6f 73 rror */. clos
dc180 65 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 e(h);. }else{.
dc190 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 pNew->pMethod
dc1a0 20 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 = pLockingStyle
dc1b0 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 ;. OpenCounte
dc1c0 72 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 r(+1);. }. ret
dc1d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
dc1e0 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65 73 Open a file des
dc1f0 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 64 criptor to the d
dc200 69 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e irectory contain
dc210 69 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e 61 ing file zFilena
dc220 6d 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 me..** If succes
dc230 73 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73 65 sful, *pFd is se
dc240 74 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64 20 t to the opened
dc250 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
dc260 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b and.** SQLITE_OK
dc270 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
dc280 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
dc290 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f , either SQLITE_
dc2a0 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 NOMEM.** or SQLI
dc2b0 54 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20 72 TE_CANTOPEN is r
dc2c0 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46 64 eturned and *pFd
dc2d0 20 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75 6e is set to an un
dc2e0 64 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 defined.** value
dc2f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ..**.** If SQLIT
dc300 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
dc310 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 , the caller is
dc320 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
dc330 63 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 closing.** the f
dc340 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
dc350 70 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28 pFd using close(
dc360 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
dc370 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63 openDirectory(c
dc380 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
dc390 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b name, int *pFd){
dc3a0 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 . int ii;. int
dc3b0 20 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 72 fd = -1;. char
dc3c0 20 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 zDirname[MAX_PA
dc3d0 54 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 THNAME+1];.. sq
dc3e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d lite3_snprintf(M
dc3f0 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69 AX_PATHNAME, zDi
dc400 72 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69 rname, "%s", zFi
dc410 6c 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 lename);. for(i
dc420 69 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44 i=(int)strlen(zD
dc430 69 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26 26 irname); ii>1 &&
dc440 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 zDirname[ii]!='
dc450 2f 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 28 /'; ii--);. if(
dc460 20 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 69 ii>0 ){. zDi
dc470 72 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 27 rname[ii] = '\0'
dc480 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 ;. fd = open(
dc490 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e zDirname, O_RDON
dc4a0 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b LY|O_BINARY, 0);
dc4b0 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 . if( fd>=0 )
dc4c0 7b 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 {.#ifdef FD_CLOE
dc4d0 58 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c 28 XEC. fcntl(
dc4e0 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e fd, F_SETFD, fcn
dc4f0 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 tl(fd, F_GETFD,
dc500 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 0) | FD_CLOEXEC)
dc510 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 4f ;.#endif. O
dc520 53 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49 52 STRACE3("OPENDIR
dc530 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64 2c %-3d %s\n", fd,
dc540 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20 20 zDirname);.
dc550 7d 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66 }. }. *pFd = f
dc560 64 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64 3e d;. return (fd>
dc570 3d 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c =0?SQLITE_OK:SQL
dc580 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d ITE_CANTOPEN);.}
dc590 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
dc5a0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
dc5b0 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
dc5c0 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f Buf must be allo
dc5d0 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 cated.** by the
dc5e0 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 20 calling process
dc5f0 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 20 and must be big
dc600 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 61 enough to hold a
dc610 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 2d t least.** pVfs-
dc620 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 >mxPathname byte
dc630 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
dc640 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 getTempname(int
dc650 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
dc660 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e f){. static con
dc670 73 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73 5b st char *azDirs[
dc680 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20 20 ] = {. 0,.
dc690 20 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 0,. "/var
dc6a0 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 /tmp",. "/us
dc6b0 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 r/tmp",. "/t
dc6c0 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 mp",. ".",.
dc6d0 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e };. static con
dc6e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
dc6f0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
dc700 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
dc710 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
dc720 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
dc730 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
dc740 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
dc750 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
dc760 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 74 , j;. struct st
dc770 61 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74 20 at buf;. const
dc780 63 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e 22 char *zDir = "."
dc790 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 ;.. /* It's odd
dc7a0 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 to simulate an
dc7b0 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 io-error here, b
dc7c0 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 ut really this i
dc7d0 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e s just. ** usin
dc7e0 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 g the io-error i
dc7f0 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f nfrastructure to
dc800 20 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 test that SQLit
dc810 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 e handles this.
dc820 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 ** function fai
dc830 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 ling. . */. Si
dc840 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
dc850 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
dc860 52 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73 5b RR );.. azDirs[
dc870 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 0] = sqlite3_tem
dc880 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69 p_directory;. i
dc890 66 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 f (NULL == azDir
dc8a0 73 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44 69 s[1]) {. azDi
dc8b0 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28 22 rs[1] = getenv("
dc8c0 54 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20 20 TMPDIR");. }.
dc8d0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 . for(i=0; i<si
dc8e0 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a zeof(azDirs)/siz
dc8f0 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20 eof(azDirs[0]);
dc900 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a i++){. if( az
dc910 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e Dirs[i]==0 ) con
dc920 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73 tinue;. if( s
dc930 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26 tat(azDirs[i], &
dc940 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b buf) ) continue;
dc950 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 . if( !S_ISDI
dc960 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 R(buf.st_mode) )
dc970 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
dc980 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73 f( access(azDirs
dc990 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 [i], 07) ) conti
dc9a0 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20 nue;. zDir =
dc9b0 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62 azDirs[i];. b
dc9c0 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 reak;. }.. /*
dc9d0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f Check that the o
dc9e0 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20 utput buffer is
dc9f0 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 large enough for
dca00 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 the temporary f
dca10 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 ile . ** name.
dca20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 If it is not, re
dca30 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
dca40 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73 R.. */. if( (s
dca50 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74 trlen(zDir) + st
dca60 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50 rlen(SQLITE_TEMP
dca70 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20 _FILE_PREFIX) +
dca80 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 17) >= (size_t)n
dca90 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Buf ){. retur
dcaa0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
dcab0 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 73 }.. do{. s
dcac0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
dcad0 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20 22 nBuf-17, zBuf, "
dcae0 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f %s/"SQLITE_TEMP_
dcaf0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 69 FILE_PREFIX, zDi
dcb00 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e 74 r);. j = (int
dcb10 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 )strlen(zBuf);.
dcb20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
dcb30 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b mness(15, &zBuf[
dcb40 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 j]);. for(i=0
dcb50 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b ; i<15; i++, j++
dcb60 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d ){. zBuf[j]
dcb70 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b = (char)zChars[
dcb80 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ((unsigned char
dcb90 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f )zBuf[j])%(sizeo
dcba0 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a f(zChars)-1) ];.
dcbb0 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a }. zBuf[j
dcbc0 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 ] = 0;. }while(
dcbd0 20 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d access(zBuf,0)=
dcbe0 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 =0 );. return S
dcbf0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
dcc00 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
dcc10 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 OCKING_STYLE &&
dcc20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
dcc30 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 _)./*.** Routine
dcc40 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 20 to transform a
dcc50 75 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 20 unixFile into a
dcc60 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e proxy-locking un
dcc70 69 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 ixFile..** Imple
dcc80 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 65 mentation in the
dcc90 20 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 69 proxy-lock divi
dcca0 73 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 62 sion, but used b
dccb0 79 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 y unixOpen().**
dccc0 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 if SQLITE_PREFER
dccd0 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 _PROXY_LOCKING i
dcce0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 s defined..*/.st
dccf0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 atic int proxyTr
dcd00 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 ansformUnixFile(
dcd10 75 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 unixFile*, const
dcd20 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
dcd30 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ../*.** Open the
dcd40 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 file zPath..**
dcd50 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 2c 20 .** Previously,
dcd60 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20 6c 61 the SQLite OS la
dcd70 79 65 72 20 75 73 65 64 20 74 68 72 65 65 20 66 yer used three f
dcd80 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c 61 63 unctions in plac
dcd90 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 e of this.** one
dcda0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 :.**.** sqli
dcdb0 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69 te3OsOpenReadWri
dcdc0 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c te();.** sql
dcdd0 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 4f 6e ite3OsOpenReadOn
dcde0 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c ly();.** sql
dcdf0 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 ite3OsOpenExclus
dce00 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 ive();.**.** The
dce10 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 73 70 se calls corresp
dce20 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ond to the follo
dce30 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e wing combination
dce40 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a s of flags:.**.*
dce50 2a 20 20 20 20 20 52 65 61 64 57 72 69 74 65 28 * ReadWrite(
dce60 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44 57 52 ) -> (READWR
dce70 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a 2a 2a ITE | CREATE).**
dce80 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 29 20 ReadOnly()
dce90 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f 4e 4c -> (READONL
dcea0 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 45 Y) .** OpenE
dceb0 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 28 52 xclusive() -> (R
dcec0 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 EADWRITE | CREAT
dced0 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29 0a 2a E | EXCLUSIVE).*
dcee0 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f 70 65 *.** The old Ope
dcef0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61 63 63 nExclusive() acc
dcf00 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 6e 20 epted a boolean
dcf10 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 6c 46 argument - "delF
dcf20 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 75 65 lag". If.** true
dcf30 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 63 , the file was c
dcf40 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62 65 20 onfigured to be
dcf50 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 automatically de
dcf60 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 0a 2a leted when the.*
dcf70 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 63 6c * file handle cl
dcf80 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65 76 65 osed. To achieve
dcf90 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74 the same effect
dcfa0 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65 77 20 using this new
dcfb0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 .** interface, a
dcfc0 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f 4e 43 dd the DELETEONC
dcfd0 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 68 6f LOSE flag to tho
dcfe0 73 65 20 73 70 65 63 69 66 69 65 64 20 61 62 6f se specified abo
dcff0 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 ve for .** OpenE
dd000 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 xclusive()..*/.s
dd010 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4f 70 tatic int unixOp
dd020 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
dd030 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
dd040 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 66 6f /* The VFS fo
dd050 72 20 77 68 69 63 68 20 74 68 69 73 20 69 73 20 r which this is
dd060 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 the xOpen method
dd070 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
dd080 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 *zPath,
dd090 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65 20 6f /* Pathname o
dd0a0 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f 70 65 f file to be ope
dd0b0 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ned */. sqlite3
dd0c0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 _file *pFile,
dd0d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c /* The fil
dd0e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 e descriptor to
dd0f0 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a be filled in */.
dd100 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
dd110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
dd120 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20 74 6f * Input flags to
dd130 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f 70 65 control the ope
dd140 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ning */. int *p
dd150 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20 OutFlags
dd160 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 /* Output
dd170 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 flags returned
dd180 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a to SQLite core *
dd190 2f 0a 29 7b 0a 20 20 69 6e 74 20 66 64 20 3d 20 /.){. int fd =
dd1a0 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 -1;
dd1b0 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 /* File d
dd1c0 65 73 63 72 69 70 74 6f 72 20 72 65 74 75 72 6e escriptor return
dd1d0 65 64 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f 0a ed by open() */.
dd1e0 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 int dirfd = -1
dd1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
dd200 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 /* Directory fi
dd210 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f le descriptor */
dd220 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 . int openFlags
dd230 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
dd240 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 /* Flags to pa
dd250 73 73 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a ss to open() */.
dd260 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 66 6c int eType = fl
dd270 61 67 73 26 30 78 46 46 46 46 46 46 30 30 3b 20 ags&0xFFFFFF00;
dd280 20 2f 2a 20 54 79 70 65 20 6f 66 20 66 69 6c 65 /* Type of file
dd290 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
dd2a0 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20 20 t noLock;
dd2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dd2c0 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 True to omit loc
dd2d0 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 king primitives
dd2e0 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
dd2f0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 6e 74 20 LITE_OK;.. int
dd300 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d 20 28 isExclusive = (
dd310 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
dd320 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a PEN_EXCLUSIVE);.
dd330 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 int isDelete
dd340 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 = (flags & SQ
dd350 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
dd360 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 ONCLOSE);. int
dd370 69 73 43 72 65 61 74 65 20 20 20 20 20 3d 20 28 isCreate = (
dd380 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
dd390 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 PEN_CREATE);. i
dd3a0 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 nt isReadonly
dd3b0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
dd3c0 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
dd3d0 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 57 72 ;. int isReadWr
dd3e0 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 ite = (flags &
dd3f0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
dd400 57 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 WRITE);.. /* If
dd410 20 63 72 65 61 74 69 6e 67 20 61 20 6d 61 73 74 creating a mast
dd420 65 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 er or main-file
dd430 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 journal, this fu
dd440 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e nction will open
dd450 0a 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 . ** a file-des
dd460 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 criptor on the d
dd470 69 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 irectory too. Th
dd480 65 20 66 69 72 73 74 20 74 69 6d 65 20 75 6e 69 e first time uni
dd490 78 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 xSync(). ** is
dd4a0 63 61 6c 6c 65 64 20 74 68 65 20 64 69 72 65 63 called the direc
dd4b0 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 tory file descri
dd4c0 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 66 73 79 ptor will be fsy
dd4d0 6e 63 28 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 nc()ed and close
dd4e0 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ()d.. */. int
dd4f0 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 isOpenDirectory
dd500 3d 20 28 69 73 43 72 65 61 74 65 20 26 26 20 0a = (isCreate && .
dd510 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51 (eType==SQ
dd520 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 LITE_OPEN_MASTER
dd530 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 _JOURNAL || eTyp
dd540 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
dd550 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 AIN_JOURNAL). )
dd560 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67 75 6d ;.. /* If argum
dd570 65 6e 74 20 7a 50 61 74 68 20 69 73 20 61 20 4e ent zPath is a N
dd580 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 ULL pointer, thi
dd590 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 s function is re
dd5a0 71 75 69 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20 quired to open.
dd5b0 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ** a temporary
dd5c0 66 69 6c 65 2e 20 55 73 65 20 74 68 69 73 20 62 file. Use this b
dd5d0 75 66 66 65 72 20 74 6f 20 73 74 6f 72 65 20 74 uffer to store t
dd5e0 68 65 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e he file name in.
dd5f0 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d . */. char zTm
dd600 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 pname[MAX_PATHNA
dd610 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 ME+1];. const c
dd620 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 har *zName = zPa
dd630 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 th;.. /* Check
dd640 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
dd650 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72 75 atements are tru
dd660 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 e: . **. **
dd670 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 (a) Exactly one
dd680 6f 66 20 74 68 65 20 52 45 41 44 57 52 49 54 45 of the READWRITE
dd690 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c and READONLY fl
dd6a0 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 2c ags must be set,
dd6b0 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 62 29 and . ** (b)
dd6c0 20 69 66 20 43 52 45 41 54 45 20 69 73 20 73 65 if CREATE is se
dd6d0 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52 49 54 t, then READWRIT
dd6e0 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 E must also be s
dd6f0 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 et, and. ** (
dd700 63 29 20 69 66 20 45 58 43 4c 55 53 49 56 45 20 c) if EXCLUSIVE
dd710 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 is set, then CRE
dd720 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 ATE must also be
dd730 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29 set.. ** (d)
dd740 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 if DELETEONCLOS
dd750 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 E is set, then C
dd760 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 REATE must also
dd770 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 be set.. */. a
dd780 73 73 65 72 74 28 28 69 73 52 65 61 64 6f 6e 6c ssert((isReadonl
dd790 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 y==0 || isReadWr
dd7a0 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73 52 65 ite==0) && (isRe
dd7b0 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52 65 61 adWrite || isRea
dd7c0 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 65 72 donly));. asser
dd7d0 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c t(isCreate==0 ||
dd7e0 20 69 73 52 65 61 64 57 72 69 74 65 29 3b 0a 20 isReadWrite);.
dd7f0 20 61 73 73 65 72 74 28 69 73 45 78 63 6c 75 73 assert(isExclus
dd800 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 ive==0 || isCrea
dd810 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 te);. assert(is
dd820 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 Delete==0 || isC
dd830 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 reate);.. /* Th
dd840 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 e main DB, main
dd850 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 journal, and mas
dd860 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 ter journal are
dd870 6e 65 76 65 72 20 61 75 74 6f 6d 61 74 69 63 61 never automatica
dd880 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 lly. ** deleted
dd890 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
dd8a0 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 eType!=SQLITE_OP
dd8b0 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 7c 20 21 69 EN_MAIN_DB || !i
dd8c0 73 44 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73 sDelete );. ass
dd8d0 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 ert( eType!=SQLI
dd8e0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
dd8f0 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65 74 RNAL || !isDelet
dd900 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 e );. assert( e
dd910 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 Type!=SQLITE_OPE
dd920 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
dd930 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b || !isDelete );
dd940 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
dd950 61 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 at the upper lay
dd960 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f er has set one o
dd970 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 f the "file-type
dd980 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 " flags. */. as
dd990 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c sert( eType==SQL
dd9a0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
dd9b0 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d || eType==
dd9c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
dd9d0 5f 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 _DB . || e
dd9e0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
dd9f0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c N_MAIN_JOURNAL |
dda00 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
dda10 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 OPEN_TEMP_JOURNA
dda20 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 L . || eTy
dda30 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
dda40 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 SUBJOURNAL ||
dda50 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
dda60 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
dda70 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 L . || eTy
dda80 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
dda90 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 TRANSIENT_DB. )
ddaa0 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c ;.. memset(pFil
ddab0 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 e, 0, sizeof(uni
ddac0 78 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20 xFile));.. if(
ddad0 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 61 73 !zName ){. as
ddae0 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26 sert(isDelete &&
ddaf0 20 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 !isOpenDirector
ddb00 79 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 y);. rc = get
ddb10 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 Tempname(MAX_PAT
ddb20 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d HNAME+1, zTmpnam
ddb30 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
ddb40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
ddb50 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
ddb60 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 }. zName =
ddb70 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 zTmpname;. }..
ddb80 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20 if( isReadonly
ddb90 29 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ) openFlags |=
ddba0 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 O_RDONLY;. if(
ddbb0 69 73 52 65 61 64 57 72 69 74 65 20 29 20 6f 70 isReadWrite ) op
ddbc0 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57 enFlags |= O_RDW
ddbd0 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 R;. if( isCreat
ddbe0 65 20 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 e ) openFlags
ddbf0 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 |= O_CREAT;. i
ddc00 66 28 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 f( isExclusive )
ddc10 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f openFlags |= (O
ddc20 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 _EXCL|O_NOFOLLOW
ddc30 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c );. openFlags |
ddc40 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f = (O_LARGEFILE|O
ddc50 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 66 64 20 _BINARY);.. fd
ddc60 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 = open(zName, op
ddc70 65 6e 46 6c 61 67 73 2c 20 69 73 44 65 6c 65 74 enFlags, isDelet
ddc80 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f 44 45 e?0600:SQLITE_DE
ddc90 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
ddca0 53 53 49 4f 4e 53 29 3b 0a 20 20 4f 53 54 52 41 SSIONS);. OSTRA
ddcb0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 CE4("OPENX %-3
ddcc0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c d %s 0%o\n", fd,
ddcd0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 zName, openFlag
ddce0 73 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 26 s);. if( fd<0 &
ddcf0 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52 20 & errno!=EISDIR
ddd00 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20 26 && isReadWrite &
ddd10 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20 29 & !isExclusive )
ddd20 7b 0a 20 20 20 20 2f 2a 20 46 61 69 6c 65 64 20 {. /* Failed
ddd30 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 to open the file
ddd40 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 for read/write
ddd50 61 63 63 65 73 73 2e 20 54 72 79 20 72 65 61 64 access. Try read
ddd60 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 20 66 6c -only. */. fl
ddd70 61 67 73 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f ags &= ~(SQLITE_
ddd80 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 OPEN_READWRITE|S
ddd90 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
ddda0 45 29 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d E);. flags |=
dddb0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
dddc0 44 4f 4e 4c 59 3b 0a 20 20 20 20 72 65 74 75 72 DONLY;. retur
dddd0 6e 20 75 6e 69 78 4f 70 65 6e 28 70 56 66 73 2c n unixOpen(pVfs,
ddde0 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 zPath, pFile, f
dddf0 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29 lags, pOutFlags)
dde00 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30 ;. }. if( fd<0
dde10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
dde20 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
dde30 20 20 7d 0a 20 20 69 66 28 20 69 73 44 65 6c 65 }. if( isDele
dde40 74 65 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 te ){.#if OS_VXW
dde50 4f 52 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d ORKS. zPath =
dde60 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 zName;.#else.
dde70 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b unlink(zName);
dde80 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 20 .#endif. }.#if
dde90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
ddea0 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 6c CKING_STYLE. el
ddeb0 73 65 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 se{. ((unixFi
ddec0 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6f 70 65 6e le*)pFile)->open
dded0 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67 Flags = openFlag
ddee0 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 s;. }.#endif.
ddef0 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b if( pOutFlags ){
ddf00 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 . *pOutFlags
ddf10 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 23 69 = flags;. }..#i
ddf20 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 fndef NDEBUG. i
ddf30 66 28 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 f( (flags & SQLI
ddf40 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 TE_OPEN_MAIN_DB)
ddf50 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 !=0 ){. ((uni
ddf60 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 69 xFile*)pFile)->i
ddf70 73 4c 6f 63 6b 61 62 6c 65 20 3d 20 31 3b 0a 20 sLockable = 1;.
ddf80 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 }.#endif.. ass
ddf90 65 72 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 20 ert( fd>=0 );.
ddfa0 69 66 28 20 69 73 4f 70 65 6e 44 69 72 65 63 74 if( isOpenDirect
ddfb0 6f 72 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ory ){. rc =
ddfc0 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 openDirectory(zP
ddfd0 61 74 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 ath, &dirfd);.
ddfe0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
ddff0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f _OK ){. clo
de000 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e se(fd); /* silen
de010 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c tly leak if fail
de020 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 , already in err
de030 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 or */. retu
de040 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d rn rc;. }. }
de050 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 ..#ifdef FD_CLOE
de060 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20 XEC. fcntl(fd,
de070 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 F_SETFD, fcntl(f
de080 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c d, F_GETFD, 0) |
de090 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 FD_CLOEXEC);.#e
de0a0 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d ndif.. noLock =
de0b0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f eType!=SQLITE_O
de0c0 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69 PEN_MAIN_DB;..#i
de0d0 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f f SQLITE_PREFER_
de0e0 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20 PROXY_LOCKING.
de0f0 69 66 28 20 7a 50 61 74 68 21 3d 4e 55 4c 4c 20 if( zPath!=NULL
de100 26 26 20 21 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 && !noLock ){.
de110 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72 63 65 char *envforce
de120 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c 49 54 = getenv("SQLIT
de130 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f E_FORCE_PROXY_LO
de140 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69 6e 74 CKING");. int
de150 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b 0a 0a useProxy = 0;..
de160 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f /* SQLITE_FO
de170 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e RCE_PROXY_LOCKIN
de180 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72 63 65 G==1 means force
de190 20 61 6c 77 61 79 73 20 75 73 65 20 70 72 6f 78 always use prox
de1a0 79 2c 20 0a 20 20 20 20 2a 2a 20 30 20 6d 65 61 y, . ** 0 mea
de1b0 6e 73 20 6e 65 76 65 72 20 75 73 65 20 70 72 6f ns never use pro
de1c0 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 xy, NULL means u
de1d0 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e 6f 6e se proxy for non
de1e0 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f 6e 6c -local files onl
de1f0 79 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 y. */. if(
de200 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20 envforce!=NULL
de210 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 ){. useProx
de220 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63 y = atoi(envforc
de230 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b e)>0;. }else{
de240 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74 . struct st
de250 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 atfs fsInfo;..
de260 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a if( statfs(z
de270 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d Path, &fsInfo) =
de280 3d 20 2d 31 20 29 7b 0a 09 09 09 09 28 28 75 6e = -1 ){.....((un
de290 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e ixFile*)pFile)->
de2a0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
de2b0 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 o;. if( d
de2c0 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 irfd>=0 ) close(
de2d0 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e dirfd); /* silen
de2e0 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c tly leak if fail
de2f0 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 , in error */.
de300 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b close(fd);
de310 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
de320 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 k if fail, in er
de330 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 ror */. r
de340 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
de350 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 RR_ACCESS;.
de360 20 7d 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 }. useProx
de370 79 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 y = !(fsInfo.f_f
de380 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b lags&MNT_LOCAL);
de390 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
de3a0 73 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 seProxy ){.
de3b0 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 rc = fillInUnix
de3c0 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 File(pVfs, fd, d
de3d0 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 irfd, pFile, zPa
de3e0 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 th, noLock, isDe
de3f0 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 lete);. if(
de400 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
de410 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
de420 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 roxyTransformUni
de430 78 46 69 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a xFile((unixFile*
de440 29 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 )pFile, ":auto:"
de450 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
de460 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
de470 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a }. }.#endif. .
de480 20 20 72 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55 return fillInU
de490 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 nixFile(pVfs, fd
de4a0 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 , dirfd, pFile,
de4b0 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 zPath, noLock, i
de4c0 73 44 65 6c 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a sDelete);.}../*.
de4d0 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 ** Delete the fi
de4e0 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 le at zPath. If
de4f0 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 the dirSync argu
de500 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 ment is true, fs
de510 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 ync().** the dir
de520 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c ectory after del
de530 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a eting the file..
de540 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
de550 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 ixDelete(. sqli
de560 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
de570 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e , /* VFS con
de580 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20 taining this as
de590 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68 the xDelete meth
de5a0 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 od */. const ch
de5b0 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
de5c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
de5d0 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 e to be deleted
de5e0 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63 */. int dirSync
de5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
de600 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63 * If true, fsync
de610 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 () directory aft
de620 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65 er deleting file
de630 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
de640 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 = SQLITE_OK;. U
de650 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
de660 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 NotUsed);. Simu
de670 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
de680 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
de690 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69 6e DELETE);. unlin
de6a0 6b 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64 65 k(zPath);.#ifnde
de6b0 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 f SQLITE_DISABLE
de6c0 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 64 _DIRSYNC. if( d
de6d0 69 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 6e irSync ){. in
de6e0 74 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 6f t fd;. rc = o
de6f0 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 penDirectory(zPa
de700 74 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 66 th, &fd);. if
de710 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
de720 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b ){.#if OS_VXWORK
de730 53 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e S. if( fsyn
de740 63 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 c(fd)==-1 ).#els
de750 65 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e e. if( fsyn
de760 63 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a 20 c(fd) ).#endif.
de770 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 72 {. r
de780 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
de790 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20 _DIR_FSYNC;.
de7a0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6c }. if( cl
de7b0 6f 73 65 28 66 64 29 26 26 21 72 63 20 29 7b 0a ose(fd)&&!rc ){.
de7c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
de7d0 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c ITE_IOERR_DIR_CL
de7e0 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 OSE;. }.
de7f0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
de800 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
de810 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 .** Test the exi
de820 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 stance of or acc
de830 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 ess permissions
de840 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 of file zPath. T
de850 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f he.** test perfo
de860 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 rmed depends on
de870 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 the value of fla
de880 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 gs:.**.** SQ
de890 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
de8a0 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 TS: Return 1 if
de8b0 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a the file exists.
de8c0 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 ** SQLITE_AC
de8d0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 CESS_READWRITE:
de8e0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
de8f0 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 file is read and
de900 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 writable..**
de910 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f SQLITE_ACCESS_
de920 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e READONLY: Return
de930 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 1 if the file i
de940 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a s readable..**.*
de950 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 * Otherwise retu
de960 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rn 0..*/.static
de970 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a int unixAccess(.
de980 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e sqlite3_vfs *N
de990 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65 otUsed, /* The
de9a0 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 VFS containing
de9b0 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74 this xAccess met
de9c0 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
de9d0 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
de9e0 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20 /* Path of the
de9f0 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 file to examine
dea00 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
dea10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dea20 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20 What do we want
dea30 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74 to learn about t
dea40 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a he zPath file? *
dea50 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 /. int *pResOut
dea60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
dea70 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c rite result bool
dea80 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ean here */.){.
dea90 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a int amode = 0;.
deaa0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
deab0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 ER(NotUsed);. S
deac0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
dead0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
deae0 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 ERR_ACCESS; );.
deaf0 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 switch( flags )
deb00 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
deb10 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a E_ACCESS_EXISTS:
deb20 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 . amode = F
deb30 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
deb40 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
deb50 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
deb60 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 TE:. amode
deb70 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 = W_OK|R_OK;.
deb80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
deb90 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
deba0 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f _READ:. amo
debb0 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 de = R_OK;.
debc0 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 break;.. def
debd0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 ault:. asse
debe0 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 rt(!"Invalid fla
debf0 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 gs argument");.
dec00 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 }. *pResOut =
dec10 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61 (access(zPath, a
dec20 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74 mode)==0);. ret
dec30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
dec40 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 .../*.** Turn a
dec50 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
dec60 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 e into a full pa
dec70 74 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61 thname. The rela
dec80 74 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20 tive path.** is
dec90 73 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d stored as a nul-
deca0 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
decb0 67 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 g in the buffer
decc0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a pointed to by.**
decd0 20 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a zPath. .**.** z
dece0 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 Out points to a
decf0 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 buffer of at lea
ded00 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d st sqlite3_vfs.m
ded10 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20 xPathname bytes
ded20 0a 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73 .** (in this cas
ded30 65 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 e, MAX_PATHNAME
ded40 62 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c bytes). The full
ded50 2d 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e -path is written
ded60 20 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 to.** this buff
ded70 65 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e er before return
ded80 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
ded90 6e 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e nt unixFullPathn
deda0 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ame(. sqlite3_v
dedb0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
dedc0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
dedd0 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f to vfs object */
dede0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
dedf0 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 Path,
dee00 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c /* Possibly rel
dee10 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 ative input path
dee20 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 */. int nOut,
dee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dee40 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f /* Size of o
dee50 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 utput buffer in
dee60 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 bytes */. char
dee70 2a 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 *zOut
dee80 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
dee90 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a ut buffer */.){.
deea0 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 . /* It's odd t
deeb0 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f o simulate an io
deec0 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 -error here, but
deed0 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 really this is
deee0 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 just. ** using
deef0 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 the io-error inf
def00 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 rastructure to t
def10 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 est that SQLite
def20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a handles this. *
def30 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 * function faili
def40 6e 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ng. This functio
def50 6e 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c n could fail if,
def60 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 for example, th
def70 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 e. ** current w
def80 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79 orking directory
def90 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b has been unlink
defa0 65 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c ed.. */. Simul
defb0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
defc0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 rn SQLITE_ERROR
defd0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 );.. assert( pV
defe0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d fs->mxPathname==
deff0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a MAX_PATHNAME );.
df000 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
df010 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 ER(pVfs);.. zOu
df020 74 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27 t[nOut-1] = '\0'
df030 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d ;. if( zPath[0]
df040 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 6c =='/' ){. sql
df050 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f ite3_snprintf(nO
df060 75 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 ut, zOut, "%s",
df070 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b zPath);. }else{
df080 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20 . int nCwd;.
df090 20 20 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f if( getcwd(zO
df0a0 75 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 ut, nOut-1)==0 )
df0b0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
df0c0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
df0d0 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d }. nCwd =
df0e0 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75 (int)strlen(zOu
df0f0 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f t);. sqlite3_
df100 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 snprintf(nOut-nC
df110 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c wd, &zOut[nCwd],
df120 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a "/%s", zPath);.
df130 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
df140 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e ITE_OK;.}...#ifn
df150 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
df160 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f LOAD_EXTENSION./
df170 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 *.** Interfaces
df180 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 for opening a sh
df190 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 ared library, fi
df1a0 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e nding entry poin
df1b0 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 ts.** within the
df1c0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
df1d0 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 and closing the
df1e0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e shared library.
df1f0 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c .*/.#include <dl
df200 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f fcn.h>.static vo
df210 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 id *unixDlOpen(s
df220 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
df230 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 sed, const char
df240 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 *zFilename){. U
df250 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
df260 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
df270 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e rn dlopen(zFilen
df280 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 ame, RTLD_NOW |
df290 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a RTLD_GLOBAL);.}.
df2a0 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 ./*.** SQLite ca
df2b0 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
df2c0 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 n immediately af
df2d0 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e ter a call to un
df2e0 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 ixDlSym() or.**
df2f0 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 unixDlOpen() fai
df300 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 ls (returns a nu
df310 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 ll pointer). If
df320 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 a more detailed
df330 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 error.** message
df340 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 is available, i
df350 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 t is written to
df360 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 zBufOut. If no e
df370 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
df380 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 is available, zB
df390 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e ufOut is left un
df3a0 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c modified and SQL
df3b0 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75 ite uses a defau
df3c0 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 lt.** error mess
df3d0 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
df3e0 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 oid unixDlError(
df3f0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
df400 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 Used, int nBuf,
df410 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
df420 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 char *zErr;.
df430 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
df440 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e 69 (NotUsed);. uni
df450 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
df460 20 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 zErr = dlerror(
df470 29 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b );. if( zErr ){
df480 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
df490 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 rintf(nBuf, zBuf
df4a0 4f 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 Out, "%s", zErr)
df4b0 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 ;. }. unixLeav
df4c0 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61 74 eMutex();.}.stat
df4d0 69 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44 6c ic void (*unixDl
df4e0 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 Sym(sqlite3_vfs
df4f0 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a *NotUsed, void *
df500 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53 p, const char*zS
df510 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f 2a ym))(void){. /*
df520 20 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68 20 . ** GCC with
df530 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 -pedantic-errors
df540 20 73 61 79 73 20 74 68 61 74 20 43 39 30 20 64 says that C90 d
df550 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 oes not allow a
df560 76 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a 2a void* to be. **
df570 20 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f 69 cast into a poi
df580 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 nter to a functi
df590 6f 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68 65 on. And yet the
df5a0 20 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28 29 library dlsym()
df5b0 20 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72 65 routine. ** re
df5c0 74 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77 68 turns a void* wh
df5d0 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61 20 ich is really a
df5e0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e pointer to a fun
df5f0 63 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20 64 ction. So how d
df600 6f 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64 6c o we. ** use dl
df610 73 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64 61 sym() with -peda
df620 6e 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20 2a ntic-errors?. *
df630 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 *. ** Variable
df640 78 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69 6e x below is defin
df650 65 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e 74 ed to be a point
df660 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e er to a function
df670 20 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61 72 taking. ** par
df680 61 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61 6e ameters void* an
df690 64 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61 6e d const char* an
df6a0 64 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f d returning a po
df6b0 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 inter to a funct
df6c0 69 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e 69 ion.. ** We ini
df6d0 74 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73 73 tialize x by ass
df6e0 69 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69 6e igning it a poin
df6f0 74 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79 6d ter to the dlsym
df700 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a () function.. *
df710 2a 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d 65 * (That assignme
df720 6e 74 20 72 65 71 75 69 72 65 73 20 61 20 63 61 nt requires a ca
df730 73 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63 61 st.) Then we ca
df740 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ll the function
df750 74 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69 6e that. ** x poin
df760 74 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 ts to. . **.
df770 2a 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72 6f ** This work-aro
df780 75 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 und is unlikely
df790 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c to work correctl
df7a0 79 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d 20 y on any system
df7b0 77 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20 72 where. ** you r
df7c0 65 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61 73 eally cannot cas
df7d0 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 t a function poi
df7e0 6e 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a 2e nter into void*.
df7f0 20 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20 74 But then, on t
df800 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68 61 he. ** other ha
df810 6e 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c 6c nd, dlsym() will
df820 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75 63 not work on suc
df830 68 20 61 20 73 79 73 74 65 6d 20 65 69 74 68 65 h a system eithe
df840 72 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20 20 r, so we have.
df850 2a 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c 6f ** not really lo
df860 73 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20 2a st anything.. *
df870 2f 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29 28 /. void (*(*x)(
df880 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
df890 2a 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e 55 *))(void);. UNU
df8a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
df8b0 74 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28 76 tUsed);. x = (v
df8c0 6f 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 oid(*(*)(void*,c
df8d0 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 onst char*))(voi
df8e0 64 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74 75 d))dlsym;. retu
df8f0 72 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d 29 rn (*x)(p, zSym)
df900 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
df910 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69 unixDlClose(sqli
df920 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
df930 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 , void *pHandle)
df940 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
df950 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
df960 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 dlclose(pHandle
df970 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 );.}.#else /* if
df980 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
df990 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 D_EXTENSION is d
df9a0 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 efined: */. #de
df9b0 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20 fine unixDlOpen
df9c0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 0. #define uni
df9d0 78 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 xDlError 0. #de
df9e0 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20 fine unixDlSym
df9f0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 0. #define uni
dfa00 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 xDlClose 0.#endi
dfa10 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e f../*.** Write n
dfa20 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e Buf bytes of ran
dfa30 64 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20 dom data to the
dfa40 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 20 supplied buffer
dfa50 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zBuf..*/.static
dfa60 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 int unixRandomne
dfa70 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ss(sqlite3_vfs *
dfa80 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 NotUsed, int nBu
dfa90 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
dfaa0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
dfab0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 ER(NotUsed);. a
dfac0 73 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e 42 ssert((size_t)nB
dfad0 75 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d 65 uf>=(sizeof(time
dfae0 5f 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29 _t)+sizeof(int))
dfaf0 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 );.. /* We have
dfb00 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a to initialize z
dfb10 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 76 Buf to prevent v
dfb20 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 70 algrind from rep
dfb30 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f orting. ** erro
dfb40 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73 rs. The reports
dfb50 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 72 issued by valgr
dfb60 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63 ind are incorrec
dfb70 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 2a t - we would. *
dfb80 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68 * prefer that th
dfb90 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20 e randomness be
dfba0 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b increased by mak
dfbb0 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a 20 ing use of the.
dfbc0 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 ** uninitialize
dfbd0 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20 d space in zBuf
dfbe0 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65 - but valgrind e
dfbf0 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f rrors tend to wo
dfc00 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 73 rry. ** some us
dfc10 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 61 ers. Rather tha
dfc20 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 6d n argue, it seem
dfc30 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f s easier just to
dfc40 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a initialize. **
dfc50 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 79 the whole array
dfc60 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 6c and silence val
dfc70 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 74 grind, even if t
dfc80 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 72 hat means less r
dfc90 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 andomness. ** i
dfca0 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 65 n the random see
dfcb0 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 d.. **. ** Whe
dfcc0 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 69 n testing, initi
dfcd0 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 74 alizing zBuf[] t
dfce0 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 65 o zero is all we
dfcf0 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 do. That means
dfd00 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 6c . ** that we al
dfd10 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61 6d ways use the sam
dfd20 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 e random number
dfd30 73 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 20 sequence. This
dfd40 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74 makes the. ** t
dfd50 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e ests repeatable.
dfd60 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 7a . */. memset(z
dfd70 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 Buf, 0, nBuf);.#
dfd80 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
dfd90 54 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 20 TE_TEST). {.
dfda0 20 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 20 int pid, fd;.
dfdb0 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 fd = open("/de
dfdc0 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 v/urandom", O_RD
dfdd0 4f 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 66 ONLY);. if( f
dfde0 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 6d d<0 ){. tim
dfdf0 65 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 6d e_t t;. tim
dfe00 65 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 6d e(&t);. mem
dfe10 63 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 69 cpy(zBuf, &t, si
dfe20 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 20 zeof(t));.
dfe30 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a pid = getpid();.
dfe40 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
dfe50 75 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 uf[sizeof(t)], &
dfe60 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 pid, sizeof(pid)
dfe70 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
dfe80 20 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 6f sizeof(t)+sizeo
dfe90 66 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29 f(pid)<=(size_t)
dfea0 6e 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 nBuf );. nB
dfeb0 75 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b uf = sizeof(t) +
dfec0 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 sizeof(pid);.
dfed0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
dfee0 42 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a Buf = read(fd, z
dfef0 42 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20 Buf, nBuf);.
dff00 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 close(fd);.
dff10 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
dff20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a return nBuf;.}..
dff30 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 ./*.** Sleep for
dff40 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e a little while.
dff50 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f Return the amo
dff60 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 unt of time slep
dff70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 t..** The argume
dff80 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 nt is the number
dff90 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 of microseconds
dffa0 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 we want to slee
dffb0 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e p..** The return
dffc0 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 value is the nu
dffd0 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 mber of microsec
dffe0 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 onds of sleep ac
dfff0 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 tually.** reques
e0000 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 ted from the und
e0010 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e erlying operatin
e0020 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 g system, a numb
e0030 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 er which.** migh
e0040 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 t be greater tha
e0050 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
e0060 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 e argument, but
e0070 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e not less.** than
e0080 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a the argument..*
e0090 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e00a0 78 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 xSleep(sqlite3_v
e00b0 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 fs *NotUsed, int
e00c0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a microseconds){.
e00d0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
e00e0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 struct timespec
e00f0 20 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65 sp;.. sp.tv_se
e0100 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 c = microseconds
e0110 20 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70 / 1000000;. sp
e0120 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72 .tv_nsec = (micr
e0130 6f 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30 oseconds % 10000
e0140 30 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61 00) * 1000;. na
e0150 6e 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c nosleep(&sp, NUL
e0160 4c 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 L);. UNUSED_PAR
e0170 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e0180 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 . return micros
e0190 65 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65 econds;.#elif de
e01a0 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 fined(HAVE_USLEE
e01b0 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 P) && HAVE_USLEE
e01c0 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f P. usleep(micro
e01d0 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53 seconds);. UNUS
e01e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e01f0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
e0200 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 microseconds;.#e
e0210 6c 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64 lse. int second
e0220 73 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 s = (microsecond
e0230 73 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30 s+999999)/100000
e0240 30 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 0;. sleep(secon
e0250 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ds);. UNUSED_PA
e0260 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e0270 3b 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e ;. return secon
e0280 64 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 ds*1000000;.#end
e0290 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 if.}../*.** The
e02a0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 following variab
e02b0 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 le, if set to a
e02c0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 non-zero value,
e02d0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
e02e0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 s.** the number
e02f0 6f 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 of seconds since
e0300 20 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65 1970 and is use
e0310 64 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73 d to set the res
e0320 75 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 ult of.** sqlite
e0330 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 3OsCurrentTime()
e0340 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e during testing.
e0350 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
e0360 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
e0370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 I int sqlite3_cu
e0380 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20 rrent_time = 0;
e0390 20 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20 /* Fake system
e03a0 74 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20 time in seconds
e03b0 73 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23 since 1970. */.#
e03c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e endif../*.** Fin
e03d0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 d the current ti
e03e0 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c me (in Universal
e03f0 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d Coordinated Tim
e0400 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a e). Write the.*
e0410 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 * current time a
e0420 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c nd date as a Jul
e0430 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 ian Day number i
e0440 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a nto *prNow and.*
e0450 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 * return 0. Ret
e0460 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d urn 1 if the tim
e0470 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f e and date canno
e0480 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 t be found..*/.s
e0490 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 tatic int unixCu
e04a0 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 rrentTime(sqlite
e04b0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
e04c0 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a double *prNow){.
e04d0 23 69 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 #if defined(NO_G
e04e0 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 ETTOD). time_t
e04f0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 t;. time(&t);.
e0500 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 *prNow = t/8640
e0510 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0.0 + 2440587.5;
e0520 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b .#elif OS_VXWORK
e0530 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 S. struct times
e0540 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 pec sNow;. cloc
e0550 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f k_gettime(CLOCK_
e0560 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 REALTIME, &sNow)
e0570 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 ;. *prNow = 244
e0580 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 0587.5 + sNow.tv
e0590 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 _sec/86400.0 + s
e05a0 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 Now.tv_nsec/8640
e05b0 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 0000000000.0;.#e
e05c0 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d lse. struct tim
e05d0 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 eval sNow;. get
e05e0 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c timeofday(&sNow,
e05f0 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 0);. *prNow =
e0600 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2440587.5 + sNow
e0610 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 .tv_sec/86400.0
e0620 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 + sNow.tv_usec/8
e0630 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6400000000.0;.#e
e0640 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
e0650 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 ITE_TEST. if( s
e0660 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
e0670 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f ime ){. *prNo
e0680 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 w = sqlite3_curr
e0690 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 ent_time/86400.0
e06a0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 + 2440587.5;.
e06b0 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 }.#endif. UNUSE
e06c0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e06d0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 sed);. return 0
e06e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 ;.}../*.** We ad
e06f0 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 ded the xGetLast
e0700 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 Error() method w
e0710 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f ith the intentio
e0720 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a n of providing.*
e0730 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 * better low-lev
e0740 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 el error message
e0750 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 s when operating
e0760 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 -system problems
e0770 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 come up.** duri
e0780 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 ng SQLite operat
e0790 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 ion. But so far
e07a0 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 , none of that h
e07b0 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e as been implemen
e07c0 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f ted.** in the co
e07d0 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 re. So this rou
e07e0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
e07f0 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 lled. For now,
e0800 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 it is merely.**
e0810 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a a place-holder..
e0820 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e0830 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 ixGetLastError(s
e0840 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
e0850 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 sed, int NotUsed
e0860 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 2, char *NotUsed
e0870 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 3){. UNUSED_PAR
e0880 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e0890 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e08a0 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 TER(NotUsed2);.
e08b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e08c0 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 R(NotUsed3);. r
e08d0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
e08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e08f0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 ******* End of s
e0900 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f qlite3_vfs metho
e0910 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
e0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
e0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
e0980 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
e0990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
e09d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
e09f0 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a Proxy Locking *
e0a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
e0a20 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b **.** Proxy lock
e0a30 69 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c ing is a "uber-l
e0a40 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 ocking-method" i
e0a50 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 n this sense: I
e0a60 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 t uses the.** ot
e0a70 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 her locking meth
e0a80 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 ods on secondary
e0a90 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 lock files. Pr
e0aa0 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 oxy locking is a
e0ab0 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f .** meta-layer o
e0ac0 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 ver top of the p
e0ad0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 rimitive locking
e0ae0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f implemented abo
e0af0 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 ve. For.** this
e0b00 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 reason, the div
e0b10 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 ision that imple
e0b20 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c ments of proxy l
e0b30 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 ocking is deferr
e0b40 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 ed.** until late
e0b50 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 in the file (he
e0b60 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 re) after all of
e0b70 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d the other I/O m
e0b80 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 ethods have.** b
e0b90 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f een defined - so
e0ba0 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 that the primit
e0bb0 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 ive locking meth
e0bc0 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ods are availabl
e0bd0 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 e.** as services
e0be0 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 to help with th
e0bf0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
e0c00 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e of proxy lockin
e0c10 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a g..**.****.**.**
e0c20 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 The default loc
e0c30 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 king schemes in
e0c40 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d SQLite use byte-
e0c50 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 range locks on t
e0c60 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
e0c70 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 ile to coordinat
e0c80 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 e safe, concurre
e0c90 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c nt access by mul
e0ca0 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a tiple readers.**
e0cb0 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 and writers [ht
e0cc0 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f tp://sqlite.org/
e0cd0 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e lockingv3.html].
e0ce0 20 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 The five file
e0cf0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 locking.** state
e0d00 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e s (UNLOCKED, PEN
e0d10 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 DING, SHARED, RE
e0d20 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 SERVED, EXCLUSIV
e0d30 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 E) are implement
e0d40 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 ed.** as POSIX r
e0d50 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b ead & write lock
e0d60 73 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 s over fixed set
e0d70 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 of locations (v
e0d80 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e ia fsctl),.** on
e0d90 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c AFP and SMB onl
e0da0 79 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 y exclusive byte
e0db0 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 -range locks are
e0dc0 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 available via f
e0dd0 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f sctl.** with _IO
e0de0 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 WR('z', 23, stru
e0df0 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b ct ByteRangeLock
e0e00 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 PB2) to track th
e0e10 65 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e e same 5 states.
e0e20 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 .** To simulate
e0e30 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 a F_RDLCK on the
e0e40 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f shared range, o
e0e50 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 n AFP a randomly
e0e60 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 selected.** add
e0e70 72 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 ress in the shar
e0e80 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 ed range is take
e0e90 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c n for a SHARED l
e0ea0 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a ock, the entire.
e0eb0 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 ** shared range
e0ec0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 is taken for an
e0ed0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a EXCLUSIVE lock):
e0ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 .**.** PEND
e0ef0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 ING_BYTE
e0f00 30 78 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0x40000000.. .
e0f10 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 .** RESERVE
e0f20 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 78 34 D_BYTE 0x4
e0f30 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 0000001.**
e0f40 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 SHARED_RANGE
e0f50 20 20 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 0x40000002 -
e0f60 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a > 0x40000200.**.
e0f70 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 ** This works we
e0f80 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ll on the local
e0f90 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 file system, but
e0fa0 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 shows a nearly
e0fb0 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 100x.** slowdown
e0fc0 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d in read perform
e0fd0 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 ance on AFP beca
e0fe0 75 73 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 use the AFP clie
e0ff0 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 nt disables.** t
e1000 68 65 20 72 65 61 64 20 63 61 63 68 65 20 77 68 he read cache wh
e1010 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f en byte-range lo
e1020 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e cks are present.
e1030 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 Enabling the r
e1040 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 ead.** cache exp
e1050 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 oses a cache coh
e1060 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 erency problem t
e1070 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f hat is present o
e1080 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 n all OS X.** su
e1090 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 pported network
e10a0 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e file systems. N
e10b0 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 FS and AFP both
e10c0 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 observe the.** c
e10d0 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d lose-to-open sem
e10e0 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 antics for ensur
e10f0 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 ing cache cohere
e1100 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e ncy.** [http://n
e1110 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e fs.sourceforge.n
e1120 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 et/#faq_a8], whi
e1130 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 ch does not effe
e1140 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 ctively.** addre
e1150 73 73 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 ss the requireme
e1160 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 nts for concurre
e1170 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 63 65 nt database acce
e1180 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a ss by multiple.*
e1190 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 * readers and wr
e11a0 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f iters.** [http:/
e11b0 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f /www.nabble.com/
e11c0 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 SQLite-on-NFS-ca
e11d0 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 che-coherency-td
e11e0 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 15655701.html]..
e11f0 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 **.** To address
e1200 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 the performance
e1210 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 and cache coher
e1220 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f ency issues, pro
e1230 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a xy file locking.
e1240 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 ** changes the w
e1250 61 79 20 64 61 74 61 62 61 73 65 20 61 63 63 65 ay database acce
e1260 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 ss is controlled
e1270 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 by limiting acc
e1280 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 ess to a.** sing
e1290 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d le host at a tim
e12a0 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c e and moving fil
e12b0 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 e locks off of t
e12c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
e12d0 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 .** and onto a p
e12e0 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 roxy file on the
e12f0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 local file syst
e1300 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 em. .**.**.** U
e1310 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 sing proxy locks
e1320 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ------------
e1330 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 -----.**.** C AP
e1340 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 Is.**.** sqlite
e1350 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 3_file_control(d
e1360 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 b, dbname, SQLIT
e1370 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_SET_LOCKPROXYF
e1380 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ILE,.**
e1390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 <p
e13a0 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 roxy_path> | ":a
e13b0 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 uto:");.** sqli
e13c0 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
e13d0 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c (db, dbname, SQL
e13e0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
e13f0 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 YFILE, &<proxy_p
e1400 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 ath>);.**.**.**
e1410 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a SQL pragmas.**.*
e1420 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 * PRAGMA [datab
e1430 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f ase.]lock_proxy_
e1440 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 file=<proxy_path
e1450 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 > | :auto:.** P
e1460 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e RAGMA [database.
e1470 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 ]lock_proxy_file
e1480 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e .**.** Specifyin
e1490 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 g ":auto:" means
e14a0 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69 that if there i
e14b0 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 s a conch file w
e14c0 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a ith a matching.*
e14d0 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c * host ID in it,
e14e0 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 the proxy path
e14f0 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c in the conch fil
e1500 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 e will be used,
e1510 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 otherwise.** a p
e1520 72 6f 78 79 20 70 61 74 68 20 62 61 73 65 64 20 roxy path based
e1530 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20 74 65 on the user's te
e1540 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 mp dir.** (via c
e1550 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 onfstr(_CS_DARWI
e1560 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c N_USER_TEMP_DIR,
e1570 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 ...)) will be us
e1580 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 ed and the.** ac
e1590 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 tual proxy file
e15a0 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 name is generate
e15b0 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 d from the name
e15c0 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a and path of the.
e15d0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
e15e0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a . For example:.
e15f0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 **.** For
e1600 64 61 74 61 62 61 73 65 20 70 61 74 68 20 22 2f database path "/
e1610 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 Users/me/foo.db"
e1620 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c .** The l
e1630 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 ock path will be
e1640 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 "<tmpdir>/sqlit
e1650 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d eplocks/_Users_m
e1660 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 e_foo.db:auto:")
e1670 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f .**.** Once a lo
e1680 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 ck proxy is conf
e1690 69 67 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 igured for a dat
e16a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
e16b0 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 , it can not.**
e16c0 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 be removed, howe
e16d0 76 65 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 ver it may be sw
e16e0 69 74 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 itched to a diff
e16f0 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 erent proxy path
e1700 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 via.** the abov
e1710 65 20 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 e APIs (assuming
e1720 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
e1730 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c is not being hel
e1740 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 d by another.**
e1750 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 connection or pr
e1760 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a ocess). .**.**.*
e1770 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b * How proxy lock
e1780 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d ing works.** ---
e1790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
e17a0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 ----.**.** Proxy
e17b0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 file locking re
e17c0 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f lies primarily o
e17d0 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 n two new suppor
e17e0 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a ting files: .**.
e17f0 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 ** * conch fi
e1800 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 le to limit acce
e1810 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ss to the databa
e1820 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e se file to a sin
e1830 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 gle host.**
e1840 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a at a time.**.**
e1850 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 * proxy file
e1860 20 74 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f to act as a pro
e1870 78 79 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 xy for the advis
e1880 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c ory locks normal
e1890 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e ly.** taken
e18a0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
e18b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 .**.** The conch
e18c0 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 file - to use a
e18d0 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c proxy file, sql
e18e0 69 74 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 ite must first "
e18f0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a hold the conch".
e1900 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 ** by taking an
e1910 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 sqlite-style sha
e1920 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 red lock on the
e1930 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 conch file, read
e1940 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 ing the.** conte
e1950 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e nts and comparin
e1960 67 20 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 g the host's uni
e1970 71 75 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 que host ID (see
e1980 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b below) and lock
e1990 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 .** proxy path a
e19a0 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 gainst the value
e19b0 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
e19c0 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 conch. The conc
e19d0 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f h file is.** sto
e19e0 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 red in the same
e19f0 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 directory as the
e1a00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
e1a10 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 nd the file name
e1a20 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 .** is patterned
e1a30 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62 after the datab
e1a40 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 ase file name as
e1a50 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 ".<databasename
e1a60 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 >-conch"..** If
e1a70 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 the conch file d
e1a80 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f oes not exist, o
e1a90 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 r it's contents
e1aa0 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 do not match the
e1ab0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f .** host ID and/
e1ac0 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 or proxy path, t
e1ad0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 hen the lock is
e1ae0 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 escalated to an
e1af0 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 exclusive.** loc
e1b00 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 k and the conch
e1b10 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 file contents is
e1b20 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 updated with th
e1b30 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 e host ID and pr
e1b40 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 oxy.** path and
e1b50 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e the lock is down
e1b60 67 72 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 graded to a shar
e1b70 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 ed lock again.
e1b80 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 If the conch.**
e1b90 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 is held by anoth
e1ba0 65 72 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 er process (with
e1bb0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c a shared lock),
e1bc0 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c the exclusive l
e1bd0 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c ock.** will fail
e1be0 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 and SQLITE_BUSY
e1bf0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
e1c00 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 .** The proxy fi
e1c10 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 le - a single-by
e1c20 74 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 te file used for
e1c30 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 all advisory fi
e1c40 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d le locks.** norm
e1c50 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 ally taken on th
e1c60 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
e1c70 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 This allows f
e1c80 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a or safe sharing.
e1c90 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ** of the databa
e1ca0 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 se file for mult
e1cb0 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 iple readers and
e1cc0 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 writers on the
e1cd0 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 same.** host (th
e1ce0 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 e conch ensures
e1cf0 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 that they all us
e1d00 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c e the same local
e1d10 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a lock file)..**.
e1d20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 68 ** There is a th
e1d30 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 ird file - the h
e1d40 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 ost ID file - us
e1d50 65 64 20 61 73 20 61 20 70 65 72 73 69 73 74 65 ed as a persiste
e1d60 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 nt record.** of
e1d70 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 a unique identif
e1d80 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 ier for the host
e1d90 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 , a 128-byte uni
e1da0 71 75 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 que host id file
e1db0 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 .** in the path
e1dc0 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 defined by the H
e1dd0 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 OSTIDPATH macro
e1de0 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 (default value i
e1df0 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 s.** /Library/Ca
e1e00 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e ches/.com.apple.
e1e10 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 sqliteConchHostI
e1e20 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 d)..**.** Reques
e1e30 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 ting the lock pr
e1e40 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d oxy does not imm
e1e50 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 ediately take th
e1e60 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a e conch, it is.*
e1e70 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 * only taken whe
e1e80 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 n the first requ
e1e90 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 est to lock data
e1ea0 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 base file is mad
e1eb0 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 e. .** This mat
e1ec0 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 ches the semanti
e1ed0 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 cs of the tradit
e1ee0 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 ional locking be
e1ef0 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a havior, where.**
e1f00 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 opening a conne
e1f10 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 ction to a datab
e1f20 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f ase file does no
e1f30 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e t take a lock on
e1f40 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 it..** The shar
e1f50 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f ed lock and an o
e1f60 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 pen file descrip
e1f70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e tor are maintain
e1f80 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 ed until .** the
e1f90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 connection to t
e1fa0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 he database is c
e1fb0 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 losed. .**.** Th
e1fc0 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 e proxy file and
e1fd0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 the lock file a
e1fe0 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 re never deleted
e1ff0 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 so they only ne
e2000 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 ed.** to be crea
e2010 74 65 64 20 74 68 65 20 66 69 72 73 74 20 74 69 ted the first ti
e2020 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 65 64 me they are used
e2030 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 ..**.** Configur
e2040 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a ation options.**
e2050 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
e2060 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 ------.**.** SQ
e2070 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 LITE_PREFER_PROX
e2080 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 Y_LOCKING.**.**
e2090 20 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 Database f
e20a0 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e iles accessed on
e20b0 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 non-local file
e20c0 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 systems are.**
e20d0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c automatical
e20e0 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f ly configured fo
e20f0 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c r proxy locking,
e2100 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a lock files are.
e2110 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 ** named a
e2120 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 utomatically usi
e2130 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 ng the same logi
e2140 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 c as.** PR
e2150 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f AGMA lock_proxy_
e2160 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a file=":auto:".**
e2170 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f .** SQLITE_
e2180 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a PROXY_DEBUG.**.*
e2190 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 * Enables
e21a0 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 the logging of e
e21b0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 rror messages du
e21c0 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c ring host id fil
e21d0 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 e.** retri
e21e0 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f eval and creatio
e21f0 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 n.**.** HOSTIDP
e2200 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ATH.**.**
e2210 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 Overrides the de
e2220 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 fault host ID fi
e2230 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e le path location
e2240 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 .**.** LOCKPROX
e2250 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 YDIR.**.**
e2260 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 Overrides the d
e2270 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 efault directory
e2280 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 used for lock p
e2290 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a roxy files that.
e22a0 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d ** are nam
e22b0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
e22c0 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a via the ":auto:
e22d0 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 " setting.**.**
e22e0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
e22f0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
e2300 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 IONS.**.**
e2310 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 Permissions to
e2320 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e use when creatin
e2330 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f g a directory fo
e2340 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a r storing the.**
e2350 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 lock prox
e2360 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 y files, only us
e2370 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 ed when LOCKPROX
e2380 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e YDIR is not set.
e2390 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a .** .** .*
e23a0 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 * As mentioned a
e23b0 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 bove, when compi
e23c0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f led with SQLITE_
e23d0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
e23e0 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 KING,.** setting
e23f0 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 the environment
e2400 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 variable SQLITE
e2410 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
e2420 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a KING to 1 will.*
e2430 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f * force proxy lo
e2440 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 cking to be used
e2450 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 for every datab
e2460 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c ase file opened,
e2470 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 and 0.** will f
e2480 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 orce automatic p
e2490 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 roxy locking to
e24a0 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 be disabled for
e24b0 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 all database.**
e24c0 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 files (explicity
e24d0 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c calling the SQL
e24e0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_SET_LOCKPROX
e24f0 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a YFILE pragma or.
e2500 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 ** sqlite_file_c
e2510 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f ontrol API is no
e2520 74 20 61 66 66 65 63 74 65 64 20 62 79 20 53 51 t affected by SQ
e2530 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
e2540 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f _LOCKING)..*/../
e2550 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 *.** Proxy locki
e2560 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ng is only avail
e2570 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a able on MacOSX .
e2580 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f */.#if defined(_
e2590 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
e25a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e25b0 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 NG_STYLE..#ifdef
e25c0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 SQLITE_TEST./*
e25d0 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c simulate multipl
e25e0 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 e hosts by creat
e25f0 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 ing unique hosti
e2600 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a d file paths */.
e2610 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
e2620 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 qlite3_hostid_nu
e2630 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f m = 0;.#endif../
e2640 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f *.** The proxyLo
e2650 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 ckingContext has
e2660 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 the path and fi
e2670 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f le structures fo
e2680 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a r the remote .**
e2690 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 and local proxy
e26a0 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a files in it.*/.
e26b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70 typedef struct p
e26c0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
e26d0 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 xt proxyLockingC
e26e0 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 ontext;.struct p
e26f0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
e2700 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 xt {. unixFile
e2710 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 *conchFile;
e2720 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 /* Open conc
e2730 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 h file */. char
e2740 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b *conchFilePath;
e2750 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
e2760 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 of the conch fi
e2770 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 le */. unixFile
e2780 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 *lockProxy;
e2790 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f /* Open pro
e27a0 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a xy lock file */.
e27b0 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 char *lockProx
e27c0 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f yPath; /
e27d0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 * Name of the pr
e27e0 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f oxy lock file */
e27f0 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b . char *dbPath;
e2800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2810 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f /* Name of the o
e2820 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e pen file */. in
e2830 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 t conchHeld;
e2840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
e2850 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 ue if the conch
e2860 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c is currently hel
e2870 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 d */. void *old
e2880 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 LockingContext;
e2890 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
e28a0 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 lockingcontext t
e28b0 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f o restore on clo
e28c0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f se */. sqlite3_
e28d0 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 io_methods const
e28e0 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 *pOldMethod;
e28f0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f /* Original I/
e2900 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c O methods for cl
e2910 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f ose */.};../* HO
e2920 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 STIDLEN and CONC
e2930 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 HLEN both includ
e2940 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 e space for the
e2950 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 string .** termi
e2960 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 nating nul .*/.#
e2970 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e define HOSTIDLEN
e2980 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 128.#de
e2990 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 fine CONCHLEN
e29a0 20 20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c (MAXPATHL
e29b0 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a EN+HOSTIDLEN+1).
e29c0 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 #ifndef HOSTIDPA
e29d0 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 TH.# define HOST
e29e0 49 44 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c IDPATH "/L
e29f0 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 ibrary/Caches/.c
e2a00 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 om.apple.sqliteC
e2a10 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 onchHostId".#end
e2a20 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 if../* basically
e2a30 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 a copy of unixR
e2a40 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 andomness with d
e2a50 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 ifferent.** test
e2a60 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 behavior built
e2a70 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 in */.static int
e2a80 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f proxyGenerateHo
e2a90 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 stID(char *pHost
e2aa0 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 ID){. int pid,
e2ab0 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 fd, len;. unsig
e2ac0 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 ned char *key =
e2ad0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
e2ae0 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d )pHostID;. . m
e2af0 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f emset(key, 0, HO
e2b00 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 STIDLEN);. len
e2b10 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e = 0;. fd = open
e2b20 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c ("/dev/urandom",
e2b30 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 O_RDONLY);. if
e2b40 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c ( fd>=0 ){. l
e2b50 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 en = read(fd, ke
e2b60 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 y, HOSTIDLEN);.
e2b70 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a close(fd); /*
e2b80 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 silently leak t
e2b90 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c he fd if it fail
e2ba0 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c s */. }. if( l
e2bb0 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 en < HOSTIDLEN )
e2bc0 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a {. time_t t;.
e2bd0 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
e2be0 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 memcpy(key, &t
e2bf0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 , sizeof(t));.
e2c00 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 pid = getpid()
e2c10 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 ;. memcpy(&ke
e2c20 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 y[sizeof(t)], &p
e2c30 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 id, sizeof(pid))
e2c40 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 ;. }. .#ifdef
e2c50 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 MAKE_PRETTY_HOST
e2c60 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 ID. {. int i
e2c70 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 ;. /* filter
e2c80 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 the bytes into p
e2c90 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 rintable ascii c
e2ca0 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 haracters and NU
e2cb0 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 L terminate */.
e2cc0 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 key[(HOSTIDLE
e2cd0 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 N-1)] = 0x00;.
e2ce0 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 for( i=0; i<(H
e2cf0 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b OSTIDLEN-1); i++
e2d00 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e ){. unsign
e2d10 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 ed char pa = key
e2d20 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 [i]&0x7F;.
e2d30 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 if( pa<0x20 ){.
e2d40 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 key[i] =
e2d50 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 (key[i]&0x80 ==
e2d60 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 0x80) ? pa+0x40
e2d70 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 : pa+0x20;.
e2d80 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 }else if( pa==0
e2d90 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b x7F ){. k
e2da0 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 ey[i] = (key[i]&
e2db0 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 0x80 == 0x80) ?
e2dc0 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 pa=0x20 : pa+0x7
e2dd0 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
e2de0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
e2df0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e2e00 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 }../* writes the
e2e10 20 68 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f host id path to
e2e20 20 70 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 path, path shou
e2e30 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c ld be an pre-all
e2e40 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a ocated buffer.**
e2e50 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 with enough spa
e2e60 63 65 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a ce for a path .*
e2e70 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 /.static void pr
e2e80 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 oxyGetHostIDPath
e2e90 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a (char *path, siz
e2ea0 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c e_t len){. strl
e2eb0 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 cpy(path, HOSTID
e2ec0 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 PATH, len);.#ifd
e2ed0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
e2ee0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 if( sqlite3_hos
e2ef0 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 tid_num>0 ){.
e2f00 20 63 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 char suffix[2]
e2f10 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 = "1";. suffi
e2f20 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d x[0] = suffix[0]
e2f30 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 + sqlite3_hosti
e2f40 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 d_num;. strlc
e2f50 61 74 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c at(path, suffix,
e2f60 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 len);. }.#endi
e2f70 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 f. OSTRACE3("GE
e2f80 54 48 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 THOSTIDPATH %s
e2f90 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c pid=%d\n", path,
e2fa0 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f getpid());.}../
e2fb0 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 * get the host I
e2fc0 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 D from a sqlite
e2fd0 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 hostid file stor
e2fe0 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 ed in the .** us
e2ff0 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 er-specific tmp
e3000 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 directory, creat
e3010 65 20 74 68 65 20 49 44 20 69 66 20 69 74 27 73 e the ID if it's
e3020 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 not there alrea
e3030 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e dy .*/.static in
e3040 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 t proxyGetHostID
e3050 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 (char *pHostID,
e3060 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 int *pError){.
e3070 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 int fd;. char p
e3080 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b ath[MAXPATHLEN];
e3090 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a . size_t len;.
e30a0 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f int rc=SQLITE_
e30b0 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 OK;.. proxyGetH
e30c0 6f 73 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 ostIDPath(path,
e30d0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f MAXPATHLEN);. /
e30e0 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 * try to create
e30f0 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 the host ID file
e3100 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 , if it already
e3110 65 78 69 73 74 73 20 72 65 61 64 20 74 68 65 20 exists read the
e3120 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 contents */. fd
e3130 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f = open(path, O_
e3140 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f CREAT|O_WRONLY|O
e3150 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 _EXCL, 0644);.
e3160 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
e3170 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 int err=errno;..
e3180 09 0a 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 .. if( err!=E
e3190 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 EXIST ){.#ifdef
e31a0 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 SQLITE_PROXY_DEB
e31b0 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 UG /* set the sq
e31c0 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 lite error messa
e31d0 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 ge instead */.
e31e0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
e31f0 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f rr, "sqlite erro
e3200 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 r creating host
e3210 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e ID file %s: %s\n
e3220 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
e3230 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 path, strerror(
e3240 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 err));.#endif.
e3250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e3260 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 E_PERM;. }.
e3270 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 /* couldn't cr
e3280 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 eate the file, r
e3290 65 61 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a ead it instead *
e32a0 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 /. fd = open(
e32b0 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f path, O_RDONLY|O
e32c0 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 _EXCL);. if(
e32d0 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 fd<0 ){.#ifdef S
e32e0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 QLITE_PROXY_DEBU
e32f0 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c G /* set the sql
e3300 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ite error messag
e3310 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 e instead */.
e3320 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 int err = err
e3330 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 no;. fprint
e3340 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 f(stderr, "sqlit
e3350 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 e error opening
e3360 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a host ID file %s:
e3370 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
e3380 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 path, stre
e3390 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 rror(err));.#end
e33a0 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 if. return
e33b0 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 SQLITE_PERM;.
e33c0 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 }. len = pre
e33d0 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 ad(fd, pHostID,
e33e0 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 HOSTIDLEN, 0);.
e33f0 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a if( len<0 ){.
e3400 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 *pError =
e3410 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
e3420 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 = SQLITE_IOERR_R
e3430 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EAD;. }else i
e3440 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e f( len<HOSTIDLEN
e3450 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f ){. *pErro
e3460 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 r = 0;. rc
e3470 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 = SQLITE_IOERR_S
e3480 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d HORT_READ;. }
e3490 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 . close(fd);
e34a0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
e34b0 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 the fd if it fa
e34c0 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 ils */. OSTRA
e34d0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 CE3("GETHOSTID
e34e0 72 65 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e read %s pid=%d\n
e34f0 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 ", pHostID, getp
e3500 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 id());. retur
e3510 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n rc;. }else{.
e3520 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 /* we're crea
e3530 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 ting the host ID
e3540 20 66 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e file (use a ran
e3550 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 dom string of by
e3560 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 tes) */. prox
e3570 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 yGenerateHostID(
e3580 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 pHostID);. le
e3590 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 n = pwrite(fd, p
e35a0 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 HostID, HOSTIDLE
e35b0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c N, 0);. if( l
e35c0 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 en<0 ){. *p
e35d0 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 Error = errno;.
e35e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e35f0 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 _IOERR_WRITE;.
e3600 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c }else if( len<
e3610 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 HOSTIDLEN ){.
e3620 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a *pError = 0;.
e3630 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e3640 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 E_IOERR_WRITE;.
e3650 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 }. close(f
e3660 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
e3670 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 leak the fd if i
e3680 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f t fails */. O
e3690 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 STRACE3("GETHOST
e36a0 49 44 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 ID wrote %s pid
e36b0 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c =%d\n", pHostID,
e36c0 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 getpid());.
e36d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d return rc;. }.}
e36e0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f ..static int pro
e36f0 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f xyGetLockPath(co
e3700 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 nst char *dbPath
e3710 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 , char *lPath, s
e3720 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 ize_t maxLen){.
e3730 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 int len;. int
e3740 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a dbLen;. int i;.
e3750 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 .#ifdef LOCKPROX
e3760 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 YDIR. len = str
e3770 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b lcpy(lPath, LOCK
e3780 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e PROXYDIR, maxLen
e3790 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 );.#else.# ifdef
e37a0 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 _CS_DARWIN_USER
e37b0 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 _TEMP_DIR. {.
e37c0 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 confstr(_CS_DA
e37d0 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 RWIN_USER_TEMP_D
e37e0 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 IR, lPath, maxLe
e37f0 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 n);. len = st
e3800 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 rlcat(lPath, "sq
e3810 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 liteplocks", max
e3820 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b Len);. if( mk
e3830 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 dir(lPath, SQLIT
e3840 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 E_DEFAULT_PROXYD
e3850 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 IR_PERMISSIONS)
e3860 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d ){. /* if m
e3870 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 kdir fails, hand
e3880 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 le as lock file
e3890 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 creation failure
e38a0 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72 */. int er
e38b0 72 20 3d 20 65 72 72 6e 6f 3b 0a 23 20 20 69 66 r = errno;.# if
e38c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
e38d0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 21 3d . if( err!=
e38e0 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 EEXIST ){.
e38f0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
e3900 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 , "proxyGetLockP
e3910 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 ath: mkdir(%s,0%
e3920 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e o) error %d %s\n
e3930 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 ", lPath,.
e3940 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
e3950 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 _DEFAULT_PROXYDI
e3960 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 R_PERMISSIONS, e
e3970 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 rr, strerror(err
e3980 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 ));. }.# e
e3990 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ndif. }else{.
e39a0 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 OSTRACE3("
e39b0 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 GETLOCKPATH mkd
e39c0 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c ir %s pid=%d\n",
e39d0 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 lPath, getpid()
e39e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 );. }. .
e39f0 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d }.# else. len =
e3a00 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 strlcpy(lPath,
e3a10 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 "/tmp/", maxLen)
e3a20 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 ;.# endif.#endif
e3a30 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 .. if( lPath[le
e3a40 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 n-1]!='/' ){.
e3a50 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c len = strlcat(l
e3a60 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 Path, "/", maxLe
e3a70 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 n);. }. . /*
e3a80 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 transform the db
e3a90 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 path to a uniqu
e3aa0 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a e cache name */.
e3ab0 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 dbLen = (int)s
e3ac0 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 trlen(dbPath);.
e3ad0 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c for( i=0; i<dbL
e3ae0 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c en && (i+len+7)<
e3af0 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 maxLen; i++){.
e3b00 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 char c = dbPat
e3b10 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b h[i];. lPath[
e3b20 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 i+len] = (c=='/'
e3b30 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c )?'_':c;. }. l
e3b40 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 Path[i+len]='\0'
e3b50 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 ;. strlcat(lPat
e3b60 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 h, ":auto:", max
e3b70 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 Len);. return S
e3b80 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
e3b90 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
e3ba0 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 VFS file descrip
e3bb0 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d tor (stored in m
e3bc0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
e3bd0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d rom.** sqlite3_m
e3be0 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 alloc) and open
e3bf0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 the file named "
e3c00 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c path" in the fil
e3c10 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a e descriptor..**
e3c20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 .** The caller i
e3c30 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f s responsible no
e3c40 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 t only for closi
e3c50 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 ng the file desc
e3c60 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c riptor.** but al
e3c70 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 so for freeing t
e3c80 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 he memory associ
e3c90 61 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69 ated with the fi
e3ca0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a le descriptor..*
e3cb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
e3cc0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 xyCreateUnixFile
e3cd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 (const char *pat
e3ce0 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 h, unixFile **pp
e3cf0 46 69 6c 65 29 20 7b 0a 20 20 69 6e 74 20 66 64 File) {. int fd
e3d00 3b 0a 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20 ;. int dirfd =
e3d10 2d 31 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a -1;. unixFile *
e3d20 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d pNew;. int rc =
e3d30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 SQLITE_OK;. sq
e3d40 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 lite3_vfs dummyV
e3d50 66 73 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e fs;.. fd = open
e3d60 28 70 61 74 68 2c 20 4f 5f 52 44 57 52 20 7c 20 (path, O_RDWR |
e3d70 4f 5f 43 52 45 41 54 2c 20 53 51 4c 49 54 45 5f O_CREAT, SQLITE_
e3d80 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 DEFAULT_FILE_PER
e3d90 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 69 66 28 MISSIONS);. if(
e3da0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 fd<0 ){. ret
e3db0 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f urn SQLITE_CANTO
e3dc0 50 45 4e 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 4e PEN;. }. . pN
e3dd0 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a ew = (unixFile *
e3de0 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 )sqlite3_malloc(
e3df0 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 sizeof(unixFile)
e3e00 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 4e );. if( pNew==N
e3e10 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ULL ){. rc =
e3e20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
e3e30 20 20 67 6f 74 6f 20 65 6e 64 5f 63 72 65 61 74 goto end_creat
e3e40 65 5f 70 72 6f 78 79 3b 0a 20 20 7d 0a 20 20 6d e_proxy;. }. m
e3e50 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 emset(pNew, 0, s
e3e60 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 izeof(unixFile))
e3e70 3b 0a 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 ;.. dummyVfs.pA
e3e80 70 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 ppData = (void*)
e3e90 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 &autolockIoFinde
e3ea0 72 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e r;. rc = fillIn
e3eb0 55 6e 69 78 46 69 6c 65 28 26 64 75 6d 6d 79 56 UnixFile(&dummyV
e3ec0 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 28 fs, fd, dirfd, (
e3ed0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 4e sqlite3_file*)pN
e3ee0 65 77 2c 20 70 61 74 68 2c 20 30 2c 20 30 29 3b ew, path, 0, 0);
e3ef0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
e3f00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 46 E_OK ){. *ppF
e3f10 69 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 ile = pNew;.
e3f20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e3f30 3b 0a 20 20 7d 0a 65 6e 64 5f 63 72 65 61 74 65 ;. }.end_create
e3f40 5f 70 72 6f 78 79 3a 20 20 20 20 0a 20 20 63 6c _proxy: . cl
e3f50 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 ose(fd); /* sile
e3f60 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20 ntly leak fd if
e3f70 65 72 72 6f 72 2c 20 77 65 27 72 65 20 61 6c 72 error, we're alr
e3f80 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f eady in error */
e3f90 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
e3fa0 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 pNew);. return
e3fb0 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 rc;.}../* takes
e3fc0 74 68 65 20 63 6f 6e 63 68 20 62 79 20 74 61 6b the conch by tak
e3fd0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ing a shared loc
e3fe0 6b 20 61 6e 64 20 72 65 61 64 20 74 68 65 20 63 k and read the c
e3ff0 6f 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 ontents conch, i
e4000 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 f .** lockPath i
e4010 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 s non-NULL, the
e4020 68 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b host ID and lock
e4030 20 66 69 6c 65 20 70 61 74 68 20 6d 75 73 74 20 file path must
e4040 6d 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a match. A NULL .
e4050 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e ** lockPath mean
e4060 73 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 50 s that the lockP
e4070 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 ath in the conch
e4080 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 file will be us
e4090 65 64 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f ed if the .** ho
e40a0 73 74 20 49 44 73 20 6d 61 74 63 68 2c 20 6f 72 st IDs match, or
e40b0 20 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68 a new lock path
e40c0 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 will be generat
e40d0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
e40e0 20 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74 65 6e .** and written
e40f0 20 74 6f 20 74 68 65 20 63 6f 6e 63 68 20 66 69 to the conch fi
e4100 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
e4110 74 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 t proxyTakeConch
e4120 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
e4130 29 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e ){. proxyLockin
e4140 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
e4150 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f (proxyLockingCo
e4160 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c ntext *)pFile->l
e4170 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a ockingContext; .
e4180 20 20 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 63 . if( pCtx->c
e4190 6f 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20 onchHeld>0 ){.
e41a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e41b0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
e41c0 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 unixFile *conch
e41d0 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e File = pCtx->con
e41e0 63 68 46 69 6c 65 3b 0a 20 20 20 20 63 68 61 72 chFile;. char
e41f0 20 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48 testValue[CONCH
e4200 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 63 LEN];. char c
e4210 6f 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c onchValue[CONCHL
e4220 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f EN];. char lo
e4230 63 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 ckPath[MAXPATHLE
e4240 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 74 4c N];. char *tL
e4250 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a ockPath = NULL;.
e4260 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c int rc = SQL
e4270 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 ITE_OK;. int
e4280 72 65 61 64 52 63 20 3d 20 53 51 4c 49 54 45 5f readRc = SQLITE_
e4290 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79 6e 63 OK;. int sync
e42a0 50 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20 Perms = 0;..
e42b0 4f 53 54 52 41 43 45 34 28 22 54 41 4b 45 43 4f OSTRACE4("TAKECO
e42c0 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 NCH %d for %s p
e42d0 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 id=%d\n", conchF
e42e0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
e42f0 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b (pCtx->lock
e4300 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 ProxyPath ? pCtx
e4310 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
e4320 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 : ":auto:"), get
e4330 70 69 64 28 29 29 3b 0a 0a 20 20 20 20 72 63 20 pid());.. rc
e4340 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 = conchFile->pMe
e4350 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c thod->xLock((sql
e4360 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
e4370 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 File, SHARED_LOC
e4380 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d K);. if( rc==
e4390 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e43a0 20 20 20 69 6e 74 20 70 45 72 72 6f 72 20 3d 20 int pError =
e43b0 30 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 0;. memset(
e43c0 74 65 73 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f testValue, 0, CO
e43d0 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 NCHLEN); /* conc
e43e0 68 20 69 73 20 66 69 78 65 64 20 73 69 7a 65 20 h is fixed size
e43f0 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 */. rc = pr
e4400 6f 78 79 47 65 74 48 6f 73 74 49 44 28 74 65 73 oxyGetHostID(tes
e4410 74 56 61 6c 75 65 2c 20 26 70 45 72 72 6f 72 29 tValue, &pError)
e4420 3b 0a 20 20 20 20 20 20 69 66 28 20 28 72 63 26 ;. if( (rc&
e4430 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 0xff)==SQLITE_IO
e4440 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ERR ){. p
e4450 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
e4460 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 = pError;.
e4470 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 }. if( pCtx
e4480 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
e4490 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 6c 63 ){. strlc
e44a0 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f py(&testValue[HO
e44b0 53 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e STIDLEN], pCtx->
e44c0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d lockProxyPath, M
e44d0 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 AXPATHLEN);.
e44e0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
e44f0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
e4500 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e ){. goto en
e4510 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 d_takeconch;.
e4520 20 7d 0a 20 20 20 20 0a 20 20 20 20 72 65 61 64 }. . read
e4530 52 63 20 3d 20 75 6e 69 78 52 65 61 64 28 28 73 Rc = unixRead((s
e4540 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f qlite3_file *)co
e4550 6e 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61 nchFile, conchVa
e4560 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 lue, CONCHLEN, 0
e4570 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 52 );. if( readR
e4580 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c!=SQLITE_IOERR_
e4590 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 SHORT_READ ){.
e45a0 20 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d if( readRc!=
e45b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e45c0 20 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 if( (rc&0xf
e45d0 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 f)==SQLITE_IOERR
e45e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
e45f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
e4600 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74 conchFile->last
e4610 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d Errno;. }
e4620 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 . rc = re
e4630 61 64 52 63 3b 0a 20 20 20 20 20 20 20 20 67 6f adRc;. go
e4640 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 to end_takeconch
e4650 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
e4660 2f 2a 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 /* if the conch
e4670 68 61 73 20 64 61 74 61 20 63 6f 6d 70 61 72 65 has data compare
e4680 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f the contents */
e4690 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 74 78 . if( !pCtx
e46a0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
e46b0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 6f ){. /* fo
e46c0 72 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 r auto-named loc
e46d0 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 al lock file, ju
e46e0 73 74 20 63 68 65 63 6b 20 74 68 65 20 68 6f 73 st check the hos
e46f0 74 20 49 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20 t ID and we'll.
e4700 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 74 ** use t
e4710 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 he local lock fi
e4720 6c 65 20 70 61 74 68 20 74 68 61 74 27 73 20 61 le path that's a
e4730 6c 72 65 61 64 79 20 69 6e 20 74 68 65 72 65 20 lready in there
e4740 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 */. if( !
e4750 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 memcmp(testValue
e4760 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f , conchValue, HO
e4770 53 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20 STIDLEN) ){.
e4780 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 tLockPath
e4790 3d 20 28 63 68 61 72 20 2a 29 26 63 6f 6e 63 68 = (char *)&conch
e47a0 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d Value[HOSTIDLEN]
e47b0 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
e47c0 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a end_takeconch;.
e47d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
e47e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
e47f0 2a 20 77 65 27 76 65 20 67 6f 74 20 74 68 65 20 * we've got the
e4800 63 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68 56 61 conch if conchVa
e4810 6c 75 65 20 6d 61 74 63 68 65 73 20 6f 75 72 20 lue matches our
e4820 70 61 74 68 20 61 6e 64 20 68 6f 73 74 20 49 44 path and host ID
e4830 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
e4840 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 !memcmp(testValu
e4850 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 e, conchValue, C
e4860 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20 ONCHLEN) ){.
e4870 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 goto end_t
e4880 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 akeconch;.
e4890 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
e48a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
e48b0 61 20 73 68 6f 72 74 20 72 65 61 64 20 6d 65 61 a short read mea
e48c0 6e 73 20 77 65 27 72 65 20 22 63 72 65 61 74 69 ns we're "creati
e48d0 6e 67 22 20 74 68 65 20 63 6f 6e 63 68 20 28 65 ng" the conch (e
e48e0 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 63 6f ven though it co
e48f0 75 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20 68 61 uld . ** ha
e4900 76 65 20 62 65 65 6e 20 75 73 65 72 2d 69 6e 74 ve been user-int
e4910 65 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77 ervention), if w
e4920 65 20 61 63 71 75 69 72 65 20 74 68 65 20 65 78 e acquire the ex
e4930 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 clusive lock,.
e4940 20 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 ** we'll try
e4950 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 63 75 to match the cu
e4960 72 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65 rrent on-disk pe
e4970 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 rmissions of the
e4980 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 database.
e4990 2a 2f 0a 20 20 20 20 20 20 73 79 6e 63 50 65 72 */. syncPer
e49a0 6d 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ms = 1;. }.
e49b0 20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68 65 72 . /* either
e49c0 20 63 6f 6e 63 68 20 77 61 73 20 65 6d 74 70 79 conch was emtpy
e49d0 20 6f 72 20 64 69 64 6e 27 74 20 6d 61 74 63 68 or didn't match
e49e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 43 74 */. if( !pCt
e49f0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
e4a00 20 29 7b 0a 20 20 20 20 20 20 70 72 6f 78 79 47 ){. proxyG
e4a10 65 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 78 2d etLockPath(pCtx-
e4a20 3e 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 >dbPath, lockPat
e4a30 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a h, MAXPATHLEN);.
e4a40 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 tLockPath
e4a50 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20 = lockPath;.
e4a60 20 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56 strlcpy(&testV
e4a70 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c alue[HOSTIDLEN],
e4a80 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 lockPath, MAXPA
e4a90 54 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 THLEN);. }.
e4aa0 20 20 0a 20 20 20 20 2f 2a 20 75 70 64 61 74 65 . /* update
e4ab0 20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f 73 74 conch with host
e4ac0 20 61 6e 64 20 70 61 74 68 20 28 74 68 69 73 20 and path (this
e4ad0 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 will fail if oth
e4ae0 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20 20 20 er process.
e4af0 2a 2a 20 68 61 73 20 61 20 73 68 61 72 65 64 20 ** has a shared
e4b00 6c 6f 63 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f lock already) */
e4b10 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 . rc = conchF
e4b20 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c ile->pMethod->xL
e4b30 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
e4b40 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58 e*)conchFile, EX
e4b50 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 CLUSIVE_LOCK);.
e4b60 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
e4b70 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
e4b80 20 3d 20 75 6e 69 78 57 72 69 74 65 28 28 73 71 = unixWrite((sq
e4b90 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e lite3_file *)con
e4ba0 63 68 46 69 6c 65 2c 20 74 65 73 74 56 61 6c 75 chFile, testValu
e4bb0 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b e, CONCHLEN, 0);
e4bc0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
e4bd0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63 QLITE_OK && sync
e4be0 50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20 20 20 Perms ){.
e4bf0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 struct stat buf
e4c00 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72 ;. int er
e4c10 72 20 3d 20 66 73 74 61 74 28 70 46 69 6c 65 2d r = fstat(pFile-
e4c20 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 20 20 20 >h, &buf);.
e4c30 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29 7b if( err==0 ){
e4c40 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 . /* tr
e4c50 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 64 y to match the d
e4c60 61 74 61 62 61 73 65 20 66 69 6c 65 20 70 65 72 atabase file per
e4c70 6d 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 missions, ignore
e4c80 20 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e failure */.#ifn
e4c90 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 def SQLITE_PROXY
e4ca0 5f 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 _DEBUG.
e4cb0 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c fchmod(conchFil
e4cc0 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 e->h, buf.st_mod
e4cd0 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 e);.#else.
e4ce0 20 20 20 20 69 66 28 20 66 63 68 6d 6f 64 28 63 if( fchmod(c
e4cf0 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 onchFile->h, buf
e4d00 2e 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a .st_mode)!=0 ){.
e4d10 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
e4d20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 code = errno;.
e4d30 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 fprint
e4d40 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f f(stderr, "fchmo
e4d50 64 20 25 6f 20 46 41 49 4c 45 44 20 77 69 74 68 d %o FAILED with
e4d60 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 %d %s\n",.
e4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4d80 20 20 20 20 20 20 20 20 62 75 66 2e 73 74 5f 6d buf.st_m
e4d90 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 ode, code, strer
e4da0 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 ror(code));.
e4db0 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
e4dc0 20 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e fprin
e4dd0 74 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d tf(stderr, "fchm
e4de0 6f 64 20 25 6f 20 53 55 43 43 45 44 45 44 5c 6e od %o SUCCEDED\n
e4df0 22 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a ",buf.st_mode);.
e4e00 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
e4e10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
e4e20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 int code =
e4e30 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 errno;.
e4e40 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
e4e50 20 22 53 54 41 54 20 46 41 49 4c 45 44 5b 25 64 "STAT FAILED[%d
e4e60 5d 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c ] with %d %s\n",
e4e70 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
e4e80 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c err,
e4e90 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 code, strerror(
e4ea0 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 code));.#endif.
e4eb0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
e4ec0 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68 . }. conch
e4ed0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
e4ee0 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f Unlock((sqlite3_
e4ef0 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c file*)conchFile,
e4f00 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
e4f10 20 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a .end_takeconch:
e4f20 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 . OSTRACE2("T
e4f30 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45 RANSPROXY: CLOSE
e4f40 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e %d\n", pFile->
e4f50 68 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d h);. if( rc==
e4f60 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 SQLITE_OK && pFi
e4f70 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b le->openFlags ){
e4f80 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 . if( pFile
e4f90 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 ->h>=0 ){.#ifdef
e4fa0 20 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52 STRICT_CLOSE_ER
e4fb0 52 4f 52 0a 20 20 20 20 20 20 20 20 69 66 28 20 ROR. if(
e4fc0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 20 close(pFile->h)
e4fd0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
e4fe0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
e4ff0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 errno;.
e5000 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
e5010 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 OERR_CLOSE;.
e5020 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
e5030 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d close(pFile-
e5040 3e 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 >h); /* silently
e5050 20 6c 65 61 6b 20 66 64 20 69 66 20 66 61 69 6c leak fd if fail
e5060 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 */.#endif.
e5070 20 7d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e }. pFile->
e5080 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e h = -1;. in
e5090 74 20 66 64 20 3d 20 6f 70 65 6e 28 70 43 74 78 t fd = open(pCtx
e50a0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2d ->dbPath, pFile-
e50b0 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 >openFlags,.
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e50d0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
e50e0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 ILE_PERMISSIONS)
e50f0 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 ;. OSTRACE2
e5100 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50 ("TRANSPROXY: OP
e5110 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a EN %d\n", fd);.
e5120 20 20 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 if( fd>=0
e5130 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
e5140 2d 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20 20 20 ->h = fd;.
e5150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
e5160 63 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 c=SQLITE_CANTOPE
e5170 4e 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 N; /* SQLITE_BUS
e5180 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 Y? proxyTakeConc
e5190 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 h called.
e51a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e51b0 20 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 6c during l
e51c0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 ocking */.
e51d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
e51e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
e51f0 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 !pCtx->lockProx
e5200 79 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 y ){. char
e5210 2a 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74 *path = tLockPat
e5220 68 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20 h ? tLockPath :
e5230 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
e5240 61 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43 ath;. /* AC
e5250 53 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20 S: Need to make
e5260 61 20 63 6f 70 79 20 6f 66 20 70 61 74 68 20 73 a copy of path s
e5270 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 ometimes */.
e5280 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 rc = proxyCrea
e5290 74 65 55 6e 69 78 46 69 6c 65 28 70 61 74 68 2c teUnixFile(path,
e52a0 20 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 &pCtx->lockProx
e52b0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 y);. }. if
e52c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
e52d0 29 7b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 63 ){. pCtx->c
e52e0 6f 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 onchHeld = 1;..
e52f0 20 20 20 20 20 69 66 28 20 74 4c 6f 63 6b 50 61 if( tLockPa
e5300 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 th ){. pC
e5310 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
e5320 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
e5330 44 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68 Dup(0, tLockPath
e5340 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
e5350 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e Ctx->lockProxy->
e5360 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 pMethod == &afpI
e5370 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
e5380 20 20 20 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 ((afpLocki
e5390 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 ngContext *)pCtx
e53a0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 ->lockProxy->loc
e53b0 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 kingContext)->db
e53c0 50 61 74 68 20 3d 0a 20 20 20 20 20 20 20 20 20 Path =.
e53d0 20 20 20 20 20 20 20 20 20 20 20 20 70 43 74 78 pCtx
e53e0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b ->lockProxyPath;
e53f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
e5400 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
e5410 20 20 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d conchFile-
e5420 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
e5430 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
e5440 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c )conchFile, NO_L
e5450 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OCK);. }.
e5460 4f 53 54 52 41 43 45 33 28 22 54 41 4b 45 43 4f OSTRACE3("TAKECO
e5470 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 NCH %d %s\n", c
e5480 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d onchFile->h, rc=
e5490 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a =SQLITE_OK?"ok":
e54a0 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 "failed");. r
e54b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a eturn rc;. }.}.
e54c0 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 ./*.** If pFile
e54d0 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 holds a lock on
e54e0 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68 a conch file, th
e54f0 65 6e 20 72 65 6c 65 61 73 65 20 74 68 61 74 20 en release that
e5500 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lock..*/.static
e5510 69 6e 74 20 70 72 6f 78 79 52 65 6c 65 61 73 65 int proxyRelease
e5520 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a Conch(unixFile *
e5530 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 pFile){. int rc
e5540 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
e5550 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 /* Subrout
e5560 69 6e 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ine return code
e5570 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e */. proxyLockin
e5580 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 gContext *pCtx;
e5590 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 /* The locking
e55a0 63 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 context for the
e55b0 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 proxy lock */.
e55c0 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 unixFile *conchF
e55d0 69 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ile; /* N
e55e0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 ame of the conch
e55f0 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78 file */.. pCtx
e5600 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 = (proxyLocking
e5610 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d Context *)pFile-
e5620 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
e5630 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 . conchFile = p
e5640 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a Ctx->conchFile;.
e5650 20 20 4f 53 54 52 41 43 45 34 28 22 52 45 4c 45 OSTRACE4("RELE
e5660 41 53 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 ASECONCH %d for
e5670 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 %s pid=%d\n", c
e5680 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 onchFile->h,.
e5690 20 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c (pCtx->l
e56a0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 ockProxyPath ? p
e56b0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
e56c0 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 th : ":auto:"),
e56d0 0a 20 20 20 20 20 20 20 20 20 20 20 67 65 74 70 . getp
e56e0 69 64 28 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63 id());. pCtx->c
e56f0 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 onchHeld = 0;.
e5700 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
e5710 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
e5720 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
e5730 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f conchFile, NO_LO
e5740 43 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 CK);. OSTRACE3(
e5750 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 "RELEASECONCH %
e5760 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d %s\n", conchFi
e5770 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
e5780 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b (rc==SQLITE_OK
e5790 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 ? "ok" : "faile
e57a0 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 d"));. return r
e57b0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 c;.}../*.** Give
e57c0 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 n the name of a
e57d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 63 database file, c
e57e0 6f 6d 70 75 74 65 20 74 68 65 20 6e 61 6d 65 20 ompute the name
e57f0 6f 66 20 69 74 73 20 63 6f 6e 63 68 20 66 69 6c of its conch fil
e5800 65 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 e..** Store the
e5810 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 69 conch filename i
e5820 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
e5830 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
e5840 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 alloc()..** Make
e5850 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 70 6f 69 *pConchPath poi
e5860 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 61 nt to the new na
e5870 6d 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 me. Return SQLI
e5880 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
e5890 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f .** or SQLITE_NO
e58a0 4d 45 4d 20 69 66 20 75 6e 61 62 6c 65 20 74 6f MEM if unable to
e58b0 20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a obtain memory..
e58c0 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
e58d0 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
e58e0 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 for ensuring tha
e58f0 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 t the allocated
e5900 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 memory.** space
e5910 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 is eventually fr
e5920 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e eed..**.** *pCon
e5930 63 68 50 61 74 68 20 69 73 20 73 65 74 20 74 6f chPath is set to
e5940 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 NULL if a memor
e5950 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
e5960 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 or occurs..*/.st
e5970 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 atic int proxyCr
e5980 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d eateConchPathnam
e5990 65 28 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 e(char *dbPath,
e59a0 63 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 char **pConchPat
e59b0 68 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 h){. int i;
e59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e59d0 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
e59e0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e ter */. int len
e59f0 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 = (int)strlen(d
e5a00 62 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 bPath); /* Lengt
e5a10 68 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 h of database fi
e5a20 6c 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 68 20 lename - dbPath
e5a30 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 */. char *conch
e5a40 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 Path;
e5a50 20 20 20 2f 2a 20 62 75 66 66 65 72 20 69 6e 20 /* buffer in
e5a60 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 72 75 which to constru
e5a70 63 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f ct conch name */
e5a80 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
e5a90 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 63 6f space for the co
e5aa0 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 nch filename and
e5ab0 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
e5ac0 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65 name to. ** the
e5ad0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 72 69 name of the ori
e5ae0 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 ginal database f
e5af0 69 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f ile. */ . *pCo
e5b00 6e 63 68 50 61 74 68 20 3d 20 63 6f 6e 63 68 50 nchPath = conchP
e5b10 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29 73 71 ath = (char *)sq
e5b20 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e lite3_malloc(len
e5b30 20 2b 20 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e + 8);. if( con
e5b40 63 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 chPath==0 ){.
e5b50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
e5b60 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 OMEM;. }. memc
e5b70 70 79 28 63 6f 6e 63 68 50 61 74 68 2c 20 64 62 py(conchPath, db
e5b80 50 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 Path, len+1);.
e5b90 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74 . /* now insert
e5ba0 20 61 20 22 2e 22 20 62 65 66 6f 72 65 20 74 68 a "." before th
e5bb0 65 20 6c 61 73 74 20 2f 20 63 68 61 72 61 63 74 e last / charact
e5bc0 65 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28 er */. for( i=(
e5bd0 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d len-1); i>=0; i-
e5be0 2d 20 29 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e - ){. if( con
e5bf0 63 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 chPath[i]=='/' )
e5c00 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 {. i++;.
e5c10 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
e5c20 20 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b }. conchPath[
e5c30 69 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20 i]='.';. while
e5c40 28 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63 ( i<len ){. c
e5c50 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62 onchPath[i+1]=db
e5c60 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b Path[i];. i++
e5c70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 ;. }.. /* appe
e5c80 6e 64 20 74 68 65 20 22 2d 63 6f 6e 63 68 22 20 nd the "-conch"
e5c90 73 75 66 66 69 78 20 74 6f 20 74 68 65 20 66 69 suffix to the fi
e5ca0 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 le */. memcpy(&
e5cb0 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20 conchPath[i+1],
e5cc0 22 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20 "-conch", 7);.
e5cd0 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 assert( (int)str
e5ce0 6c 65 6e 28 63 6f 6e 63 68 50 61 74 68 29 20 3d len(conchPath) =
e5cf0 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 = len+7 );.. re
e5d00 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e5d10 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66 }.../* Takes a f
e5d20 75 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 ully configured
e5d30 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 proxy locking-st
e5d40 79 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 61 6e yle unix file an
e5d50 64 20 73 77 69 74 63 68 65 73 0a 2a 2a 20 74 68 d switches.** th
e5d60 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c e local lock fil
e5d70 65 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 e path .*/.stati
e5d80 63 20 69 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b c int switchLock
e5d90 50 72 6f 78 79 50 61 74 68 28 75 6e 69 78 46 69 ProxyPath(unixFi
e5da0 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 le *pFile, const
e5db0 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 char *path) {.
e5dc0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
e5dd0 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
e5de0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e5df0 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e t*)pFile->lockin
e5e00 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68 61 72 gContext;. char
e5e10 20 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43 74 78 *oldPath = pCtx
e5e20 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b ->lockProxyPath;
e5e30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
e5e40 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46 TE_OK;.. if( pF
e5e50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
e5e60 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 O_LOCK ){. re
e5e70 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
e5e80 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f ;. } .. /* no
e5e90 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 thing to do if t
e5ea0 68 65 20 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c he path is NULL,
e5eb0 20 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 :auto: or match
e5ec0 65 73 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 es the existing
e5ed0 70 61 74 68 20 2a 2f 0a 20 20 69 66 28 20 21 70 path */. if( !p
e5ee0 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d ath || path[0]==
e5ef0 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 '\0' || !strcmp(
e5f00 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 path, ":auto:")
e5f10 7c 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74 68 20 ||. (oldPath
e5f20 26 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 && !strncmp(oldP
e5f30 61 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 ath, path, MAXPA
e5f40 54 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20 20 72 THLEN)) ){. r
e5f50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e5f60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e . }else{. un
e5f70 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 ixFile *lockProx
e5f80 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 y = pCtx->lockPr
e5f90 6f 78 79 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c oxy;. pCtx->l
e5fa0 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 ockProxy=NULL;.
e5fb0 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 pCtx->conchHe
e5fc0 6c 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 ld = 0;. if(
e5fd0 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 lockProxy!=NULL
e5fe0 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b ){. rc=lock
e5ff0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e Proxy->pMethod->
e6000 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
e6010 66 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 file *)lockProxy
e6020 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
e6030 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
e6040 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
e6050 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 lockProxy);.
e6060 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
e6070 65 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20 ee(oldPath);.
e6080 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
e6090 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 Path = sqlite3Db
e60a0 53 74 72 44 75 70 28 30 2c 20 70 61 74 68 29 3b StrDup(0, path);
e60b0 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e . }. . return
e60c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 rc;.}../*.** pF
e60d0 69 6c 65 20 69 73 20 61 20 66 69 6c 65 20 74 68 ile is a file th
e60e0 61 74 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e at has been open
e60f0 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 78 4f ed by a prior xO
e6100 70 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50 61 74 pen call. dbPat
e6110 68 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 h.** is a string
e6120 20 62 75 66 66 65 72 20 61 74 20 6c 65 61 73 74 buffer at least
e6130 20 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20 63 68 MAXPATHLEN+1 ch
e6140 61 72 61 63 74 65 72 73 20 69 6e 20 73 69 7a 65 aracters in size
e6150 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
e6160 74 69 6e 65 20 66 69 6e 64 20 74 68 65 20 66 69 tine find the fi
e6170 6c 65 6e 61 6d 65 20 61 73 73 6f 63 69 61 74 65 lename associate
e6180 64 20 77 69 74 68 20 70 46 69 6c 65 20 61 6e 64 d with pFile and
e6190 20 77 72 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e writes it.** in
e61a0 74 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 t dbPath..*/.sta
e61b0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 tic int proxyGet
e61c0 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c DbPathForUnixFil
e61d0 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c e(unixFile *pFil
e61e0 65 2c 20 63 68 61 72 20 2a 64 62 50 61 74 68 29 e, char *dbPath)
e61f0 7b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f {.#if defined(__
e6200 41 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70 APPLE__). if( p
e6210 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d File->pMethod ==
e6220 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 &afpIoMethods )
e6230 7b 0a 20 20 20 20 2f 2a 20 61 66 70 20 73 74 79 {. /* afp sty
e6240 6c 65 20 6b 65 65 70 73 20 61 20 72 65 66 65 72 le keeps a refer
e6250 65 6e 63 65 20 74 6f 20 74 68 65 20 64 62 20 70 ence to the db p
e6260 61 74 68 20 69 6e 20 74 68 65 20 66 69 6c 65 50 ath in the fileP
e6270 61 74 68 20 66 69 65 6c 64 20 0a 20 20 20 20 2a ath field . *
e6280 2a 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 20 * of the struct
e6290 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
e62a0 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 68 61 72 int)strlen((char
e62b0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
e62c0 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 Context)<=MAXPAT
e62d0 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 HLEN );. strc
e62e0 70 79 28 64 62 50 61 74 68 2c 20 28 28 61 66 70 py(dbPath, ((afp
e62f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
e6300 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
e6310 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 29 ontext)->dbPath)
e6320 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 ;. }else.#endif
e6330 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d . if( pFile->pM
e6340 65 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 ethod == &dotloc
e6350 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 kIoMethods ){.
e6360 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74 /* dot lock st
e6370 79 6c 65 20 75 73 65 73 20 74 68 65 20 6c 6f 63 yle uses the loc
e6380 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 king context to
e6390 73 74 6f 72 65 20 74 68 65 20 64 6f 74 20 6c 6f store the dot lo
e63a0 63 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 70 ck. ** file p
e63b0 61 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c ath */. int l
e63c0 65 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68 61 en = strlen((cha
e63d0 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 r *)pFile->locki
e63e0 6e 67 43 6f 6e 74 65 78 74 29 20 2d 20 73 74 72 ngContext) - str
e63f0 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 len(DOTLOCK_SUFF
e6400 49 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 IX);. memcpy(
e6410 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 dbPath, (char *)
e6420 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
e6430 6e 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b ntext, len + 1);
e6440 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
e6450 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 79 6c 65 all other style
e6460 73 20 75 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e s use the lockin
e6470 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f g context to sto
e6480 72 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 70 re the db file p
e6490 61 74 68 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 ath */. asser
e64a0 74 28 20 73 74 72 6c 65 6e 28 28 63 68 61 72 2a t( strlen((char*
e64b0 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
e64c0 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 ontext)<=MAXPATH
e64d0 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 LEN );. strcp
e64e0 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 y(dbPath, (char
e64f0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
e6500 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 Context);. }.
e6510 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e6520 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 ;.}../*.** Takes
e6530 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69 6c 6c an already fill
e6540 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c 65 20 ed in unix file
e6550 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20 73 6f and alters it so
e6560 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e all file lockin
e6570 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65 g .** will be pe
e6580 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c rformed on the l
e6590 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20 ocal proxy lock
e65a0 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f file. The follo
e65b0 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61 wing fields.** a
e65c0 72 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20 re preserved in
e65d0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 the locking cont
e65e0 65 78 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 ext so that they
e65f0 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 can be restored
e6600 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69 and .** the uni
e6610 78 20 73 74 72 75 63 74 75 72 65 20 70 72 6f 70 x structure prop
e6620 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 70 20 erly cleaned up
e6630 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a at close time:.*
e6640 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 * ->lockingCont
e6650 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f ext.** ->pMetho
e6660 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 d.*/.static int
e6670 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e proxyTransformUn
e6680 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 ixFile(unixFile
e6690 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 *pFile, const ch
e66a0 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 ar *path) {. pr
e66b0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e66c0 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 72 20 t *pCtx;. char
e66d0 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 dbPath[MAXPATHLE
e66e0 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e N+1]; /* N
e66f0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
e6700 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 ase file */. ch
e6710 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c ar *lockPath=NUL
e6720 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 L;. int rc = SQ
e6730 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 LITE_OK;. . if
e6740 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
e6750 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 e!=NO_LOCK ){.
e6760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e6770 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 BUSY;. }. prox
e6780 79 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 yGetDbPathForUni
e6790 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 64 62 50 xFile(pFile, dbP
e67a0 61 74 68 29 3b 0a 20 20 69 66 28 20 21 70 61 74 ath);. if( !pat
e67b0 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c h || path[0]=='\
e67c0 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 0' || !strcmp(pa
e67d0 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b th, ":auto:") ){
e67e0 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 . lockPath=NU
e67f0 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 LL;. }else{.
e6800 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 72 20 lockPath=(char
e6810 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 *)path;. }. .
e6820 20 4f 53 54 52 41 43 45 34 28 22 54 52 41 4e 53 OSTRACE4("TRANS
e6830 50 52 4f 58 59 20 20 25 64 20 66 6f 72 20 25 73 PROXY %d for %s
e6840 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c pid=%d\n", pFil
e6850 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
e6860 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63 (lockPath ? loc
e6870 6b 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 kPath : ":auto:"
e6880 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 ), getpid());..
e6890 20 70 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f pCtx = sqlite3_
e68a0 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a malloc( sizeof(*
e68b0 70 43 74 78 29 20 29 3b 0a 20 20 69 66 28 20 70 pCtx) );. if( p
e68c0 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 Ctx==0 ){. re
e68d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
e68e0 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 M;. }. memset(
e68f0 70 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pCtx, 0, sizeof(
e6900 2a 70 43 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d *pCtx));.. rc =
e6910 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 proxyCreateConc
e6920 68 50 61 74 68 6e 61 6d 65 28 64 62 50 61 74 68 hPathname(dbPath
e6930 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 , &pCtx->conchFi
e6940 6c 65 50 61 74 68 29 3b 0a 20 20 69 66 28 20 72 lePath);. if( r
e6950 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
e6960 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 rc = proxyCr
e6970 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 43 74 eateUnixFile(pCt
e6980 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
e6990 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 , &pCtx->conchFi
e69a0 6c 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 le);. } . if(
e69b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
e69c0 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 & lockPath ){.
e69d0 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 pCtx->lockProx
e69e0 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 yPath = sqlite3D
e69f0 62 53 74 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50 bStrDup(0, lockP
e6a00 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ath);. }.. if(
e6a10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
e6a20 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d {. /* all mem
e6a30 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ory is allocated
e6a40 2c 20 70 72 6f 78 79 73 20 61 72 65 20 63 72 65 , proxys are cre
e6a50 61 74 65 64 20 61 6e 64 20 61 73 73 69 67 6e 65 ated and assigne
e6a60 64 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 d, . ** switc
e6a70 68 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f h the locking co
e6a80 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f ntext and pMetho
e6a90 64 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20 d then return..
e6aa0 20 20 20 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e */. pCtx->
e6ab0 64 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 dbPath = sqlite3
e6ac0 44 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61 DbStrDup(0, dbPa
e6ad0 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f th);. pCtx->o
e6ae0 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ldLockingContext
e6af0 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e = pFile->lockin
e6b00 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 gContext;. pF
e6b10 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
e6b20 65 78 74 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 ext = pCtx;.
e6b30 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 pCtx->pOldMethod
e6b40 20 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f = pFile->pMetho
e6b50 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d d;. pFile->pM
e6b60 65 74 68 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f ethod = &proxyIo
e6b70 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 Methods;. }else
e6b80 7b 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e {. if( pCtx->
e6b90 63 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a 20 20 conchFile ){ .
e6ba0 20 20 20 20 72 63 20 3d 20 70 43 74 78 2d 3e 63 rc = pCtx->c
e6bb0 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
e6bc0 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 d->xClose((sqlit
e6bd0 65 33 5f 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e e3_file *)pCtx->
e6be0 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 conchFile);.
e6bf0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
e6c00 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 n rc;. sqli
e6c10 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 te3_free(pCtx->c
e6c20 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d onchFile);. }
e6c30 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
e6c40 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c e(pCtx->conchFil
e6c50 65 50 61 74 68 29 3b 20 0a 20 20 20 20 73 71 6c ePath); . sql
e6c60 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b ite3_free(pCtx);
e6c70 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 . }. OSTRACE3(
e6c80 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20 "TRANSPROXY %d
e6c90 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %s\n", pFile->h,
e6ca0 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d . (rc=
e6cb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
e6cc0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a " : "failed"));.
e6cd0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
e6ce0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
e6cf0 69 6e 65 20 68 61 6e 64 6c 65 73 20 73 71 6c 69 ine handles sqli
e6d00 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
e6d10 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20 61 72 () calls that ar
e6d20 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f e specific.** to
e6d30 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a proxy locking..
e6d40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
e6d50 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 oxyFileControl(s
e6d60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
e6d70 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 int op, void *p
e6d80 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 Arg){. switch(
e6d90 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 op ){. case S
e6da0 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 QLITE_GET_LOCKPR
e6db0 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 OXYFILE: {.
e6dc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
e6dd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
e6de0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c ;. if( pFil
e6df0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 e->pMethod == &p
e6e00 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b roxyIoMethods ){
e6e10 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f . proxyLo
e6e20 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
e6e30 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
e6e40 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 ngContext*)pFile
e6e50 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
e6e60 3b 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 54 ;. proxyT
e6e70 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b akeConch(pFile);
e6e80 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74 . if( pCt
e6e90 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
e6ea0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
e6eb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 const char **)pA
e6ec0 72 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 rg = pCtx->lockP
e6ed0 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 roxyPath;.
e6ee0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
e6ef0 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 *(const char
e6f00 2a 2a 29 70 41 72 67 20 3d 20 22 3a 61 75 74 6f **)pArg = ":auto
e6f10 3a 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20 : (not held)";.
e6f20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
e6f30 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
e6f40 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 *(const char **)
e6f50 70 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 pArg = NULL;.
e6f60 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
e6f70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
e6f80 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 }. case SQLI
e6f90 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 TE_SET_LOCKPROXY
e6fa0 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e FILE: {. un
e6fb0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
e6fc0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
e6fd0 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 int rc = SQ
e6fe0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 LITE_OK;. i
e6ff0 6e 74 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 nt isProxyStyle
e7000 3d 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f = (pFile->pMetho
e7010 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 d == &proxyIoMet
e7020 68 6f 64 73 29 3b 0a 20 20 20 20 20 20 69 66 28 hods);. if(
e7030 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 pArg==NULL || (
e7040 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72 const char *)pAr
e7050 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 g==0 ){.
e7060 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 if( isProxyStyle
e7070 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
e7080 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 turn off proxy
e7090 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75 locking - not su
e70a0 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 pported */.
e70b0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e70c0 5f 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f _ERROR /*SQLITE_
e70d0 50 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 PROTOCOL? SQLITE
e70e0 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 _MISUSE?*/;.
e70f0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
e7100 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 /* turn off
e7110 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d proxy locking -
e7120 20 61 6c 72 65 61 64 79 20 6f 66 66 20 2d 20 4e already off - N
e7130 4f 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 OOP */.
e7140 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
e7150 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
e7160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
e7170 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 6f 78 const char *prox
e7180 79 50 61 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 yPath = (const c
e7190 68 61 72 20 2a 29 70 41 72 67 3b 0a 20 20 20 20 har *)pArg;.
e71a0 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 if( isProxyS
e71b0 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 tyle ){.
e71c0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
e71d0 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 0a 20 ntext *pCtx = .
e71e0 20 20 20 20 20 20 20 20 20 20 20 28 70 72 6f 78 (prox
e71f0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a yLockingContext*
e7200 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
e7210 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 ontext;.
e7220 20 20 69 66 28 20 21 73 74 72 63 6d 70 28 70 41 if( !strcmp(pA
e7230 72 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20 rg, ":auto:") .
e7240 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 43 || (pC
e7250 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
e7260 68 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 h &&.
e7270 20 20 20 20 21 73 74 72 6e 63 6d 70 28 70 43 74 !strncmp(pCt
e7280 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
e7290 2c 20 70 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 , proxyPath, MAX
e72a0 50 41 54 48 4c 45 4e 29 29 0a 20 20 20 20 20 20 PATHLEN)).
e72b0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ){.
e72c0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
e72d0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c K;. }el
e72e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
e72f0 72 63 20 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50 rc = switchLockP
e7300 72 6f 78 79 50 61 74 68 28 70 46 69 6c 65 2c 20 roxyPath(pFile,
e7310 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 proxyPath);.
e7320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e7330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
e7340 20 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78 /* turn on prox
e7350 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a y file locking *
e7360 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
e7370 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 proxyTransformU
e7380 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 70 nixFile(pFile, p
e7390 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 roxyPath);.
e73a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
e73b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
e73c0 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
e73d0 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
e73e0 20 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 63 61 0 ); /* The ca
e73f0 6c 6c 20 61 73 73 75 72 65 73 20 74 68 61 74 20 ll assures that
e7400 6f 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 6f 64 only valid opcod
e7410 65 73 20 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20 es are sent */.
e7420 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 }. }. /*NOT
e7430 52 45 41 43 48 45 44 2a 2f 0a 20 20 72 65 74 75 REACHED*/. retu
e7440 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
e7450 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e .}../*.** Within
e7460 20 74 68 69 73 20 64 69 76 69 73 69 6f 6e 20 28 this division (
e7470 74 68 65 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 the proxying loc
e7480 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 king implementat
e7490 69 6f 6e 29 20 74 68 65 20 70 72 6f 63 65 64 75 ion) the procedu
e74a0 72 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69 res.** above thi
e74b0 73 20 70 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20 s point are all
e74c0 75 74 69 6c 69 74 69 65 73 2e 20 20 54 68 65 20 utilities. The
e74d0 6c 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74 lock-related met
e74e0 68 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 hods of the.** p
e74f0 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c roxy-locking sql
e7500 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f ite3_io_method o
e7510 62 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f bject follow..*/
e7520 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
e7530 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
e7540 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
e7550 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
e7560 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
e7570 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
e7580 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
e7590 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
e75a0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
e75b0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
e75c0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
e75d0 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
e75e0 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
e75f0 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
e7600 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
e7610 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
e7620 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
e7630 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
e7640 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f ock checking..*/
e7650 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
e7660 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f yCheckReservedLo
e7670 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
e7680 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
e7690 74 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 t) {. unixFile
e76a0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
e76b0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 le*)id;. int rc
e76c0 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 = proxyTakeConc
e76d0 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 h(pFile);. if(
e76e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
e76f0 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e . proxyLockin
e7700 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
e7710 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f (proxyLockingCo
e7720 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c ntext *)pFile->l
e7730 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
e7740 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f unixFile *pro
e7750 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 xy = pCtx->lockP
e7760 72 6f 78 79 3b 0a 20 20 20 20 72 65 74 75 72 6e roxy;. return
e7770 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d proxy->pMethod-
e7780 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c >xCheckReservedL
e7790 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
e77a0 65 2a 29 70 72 6f 78 79 2c 20 70 52 65 73 4f 75 e*)proxy, pResOu
e77b0 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
e77c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
e77d0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
e77e0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
e77f0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
e7800 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
e7810 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
e7820 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
e7830 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
e7840 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
e7850 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
e7860 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
e7870 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
e7880 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
e7890 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
e78a0 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
e78b0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
e78c0 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
e78d0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
e78e0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
e78f0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
e7900 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
e7910 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
e7920 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
e7930 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
e7940 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
e7950 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
e7960 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
e7970 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
e7980 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
e7990 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
e79a0 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
e79b0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
e79c0 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
e79d0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
e79e0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
e79f0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
e7a00 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
e7a10 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
e7a20 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
e7a30 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
e7a40 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
e7a50 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
e7a60 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
e7a70 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
e7a80 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
e7a90 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
e7aa0 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 a lock. Use the
e7ab0 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
e7ac0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f ().** routine to
e7ad0 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 lower a locking
e7ae0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 level..*/.stati
e7af0 63 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 c int proxyLock(
e7b00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
e7b10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
e7b20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
e7b30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
e7b40 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
e7b50 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 proxyTakeConch(p
e7b60 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d File);. if( rc=
e7b70 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e7b80 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
e7b90 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 ntext *pCtx = (p
e7ba0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
e7bb0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
e7bc0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
e7bd0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 unixFile *proxy
e7be0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
e7bf0 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 y;. rc = prox
e7c00 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 y->pMethod->xLoc
e7c10 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
e7c20 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 )proxy, locktype
e7c30 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f );. pFile->lo
e7c40 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e cktype = proxy->
e7c50 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
e7c60 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
e7c70 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
e7c80 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
e7c90 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e7ca0 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 pFile to locktyp
e7cb0 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 e. locktype.**
e7cc0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
e7cd0 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
e7ce0 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
e7cf0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
e7d00 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
e7d10 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
e7d20 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
e7d30 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
e7d40 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
e7d50 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
e7d60 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
e7d70 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f ic int proxyUnlo
e7d80 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
e7d90 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
e7da0 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
e7db0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
e7dc0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 le*)id;. int rc
e7dd0 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 = proxyTakeConc
e7de0 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 h(pFile);. if(
e7df0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
e7e00 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e . proxyLockin
e7e10 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
e7e20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f (proxyLockingCo
e7e30 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c ntext *)pFile->l
e7e40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
e7e50 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f unixFile *pro
e7e60 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 xy = pCtx->lockP
e7e70 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 roxy;. rc = p
e7e80 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
e7e90 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f Unlock((sqlite3_
e7ea0 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 file*)proxy, loc
e7eb0 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c ktype);. pFil
e7ec0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 e->locktype = pr
e7ed0 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 oxy->locktype;.
e7ee0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
e7ef0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
e7f00 20 66 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 file that uses
e7f10 70 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a proxy locks..*/.
e7f20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
e7f30 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
e7f40 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 le *id) {. if(
e7f50 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 id ){. unixFi
e7f60 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
e7f70 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70 xFile*)id;. p
e7f80 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
e7f90 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
e7fa0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
e7fb0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
e7fc0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
e7fd0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 xFile *lockProxy
e7fe0 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f = pCtx->lockPro
e7ff0 78 79 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 xy;. unixFile
e8000 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 *conchFile = pC
e8010 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 tx->conchFile;.
e8020 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 int rc = SQLI
e8030 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20 TE_OK;. .
e8040 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b if( lockProxy ){
e8050 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b . rc = lock
e8060 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e Proxy->pMethod->
e8070 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 xUnlock((sqlite3
e8080 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 _file*)lockProxy
e8090 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 , NO_LOCK);.
e80a0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
e80b0 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d n rc;. rc =
e80c0 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 lockProxy->pMet
e80d0 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c hod->xClose((sql
e80e0 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 ite3_file*)lockP
e80f0 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 roxy);. if(
e8100 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
e8110 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
e8120 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a ree(lockProxy);.
e8130 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b pCtx->lock
e8140 50 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d Proxy = 0;. }
e8150 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 46 69 . if( conchFi
e8160 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 le ){. if(
e8170 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 pCtx->conchHeld
e8180 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
e8190 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 proxyReleaseConc
e81a0 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 h(pFile);.
e81b0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
e81c0 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
e81d0 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 rc = conchFi
e81e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c le->pMethod->xCl
e81f0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ose((sqlite3_fil
e8200 65 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 e*)conchFile);.
e8210 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
e8220 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 turn rc;. s
e8230 71 6c 69 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 qlite3_free(conc
e8240 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 hFile);. }.
e8250 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
e8260 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
e8270 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 th);. sqlite3
e8280 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 _free(pCtx->conc
e8290 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 hFilePath);.
e82a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
e82b0 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 20 20 x->dbPath);.
e82c0 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 6f /* restore the o
e82d0 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 riginal locking
e82e0 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 context and pMet
e82f0 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 hod then close i
e8300 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e t */. pFile->
e8310 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d lockingContext =
e8320 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e pCtx->oldLockin
e8330 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 gContext;. pF
e8340 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 ile->pMethod = p
e8350 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b Ctx->pOldMethod;
e8360 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
e8370 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72 65 74 e(pCtx);. ret
e8380 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 urn pFile->pMeth
e8390 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a od->xClose(id);.
e83a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
e83b0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e ITE_OK;.}....#en
e83c0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f dif /* defined(_
e83d0 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
e83e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e83f0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a NG_STYLE */./*.*
e8400 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b * The proxy lock
e8410 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69 6e 74 ing style is int
e8420 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 77 69 ended for use wi
e8430 74 68 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 th AFP filesyste
e8440 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65 ms..** And since
e8450 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 75 70 AFP is only sup
e8460 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58 ported on MacOSX
e8470 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b , the proxy lock
e8480 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 ing is also.** r
e8490 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d 61 63 estricted to Mac
e84a0 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a OSX..** .**.****
e84b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
e84c0 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 78 79 End of the proxy
e84d0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 lock implementa
e84e0 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
e84f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
e8500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
e8550 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
e8560 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
e8570 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a em interface..**
e8580 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
e8590 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c 20 56 registers all V
e85a0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
e85b0 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 ns for unix-like
e85c0 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79 operating.** sy
e85d0 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72 6f 75 stems. This rou
e85e0 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 73 71 tine, and the sq
e85f0 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72 lite3_os_end() r
e8600 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c outine that foll
e8610 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 ows,.** should b
e8620 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 e the only routi
e8630 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nes in this file
e8640 20 74 68 61 74 20 61 72 65 20 76 69 73 69 62 6c that are visibl
e8650 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 e from other.**
e8660 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
e8670 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
e8680 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 led once during
e8690 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c 69 7a SQLite initializ
e86a0 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a ation and by a.*
e86b0 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e * single thread.
e86c0 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c The memory all
e86d0 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65 ocation and mute
e86e0 78 20 73 75 62 73 79 73 74 65 6d 73 20 68 61 76 x subsystems hav
e86f0 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61 e not.** necessa
e8700 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 69 61 rily been initia
e8710 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69 73 20 lized when this
e8720 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
e8730 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a d, and so they.*
e8740 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 * should not be
e8750 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
e8760 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
e8770 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a os_init(void){ .
e8780 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66 /* . ** The f
e8790 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64 ollowing macro d
e87a0 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 69 61 efines an initia
e87b0 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 71 6c lizer for an sql
e87c0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e ite3_vfs object.
e87d0 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f . ** The name o
e87e0 66 20 74 68 65 20 56 46 53 20 69 73 20 4e 41 4d f the VFS is NAM
e87f0 45 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 E. The pAppData
e8800 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
e8810 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 a pointer. **
e8820 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72 22 20 to the "finder"
e8830 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 70 70 function. (pApp
e8840 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 Data is a pointe
e8850 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 62 r to a pointer b
e8860 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c ecause. ** sill
e8870 79 20 43 39 30 20 72 75 6c 65 73 20 70 72 6f 68 y C90 rules proh
e8880 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66 72 6f ibit a void* fro
e8890 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74 6f 20 m being cast to
e88a0 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 a function point
e88b0 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77 er. ** and so w
e88c0 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74 68 72 e have to go thr
e88d0 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72 6d 65 ough the interme
e88e0 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20 74 6f diate pointer to
e88f0 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a avoid problems.
e8900 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c ** when compil
e8910 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61 6e 74 ing with -pedant
e8920 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 43 43 ic-errors on GCC
e8930 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 .). **. ** The
e8940 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65 74 65 FINDER paramete
e8950 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 r to this macro
e8960 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
e8970 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 he pointer to th
e8980 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75 e. ** finder-fu
e8990 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e nction. The fin
e89a0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74 der-function ret
e89b0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
e89c0 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 o the. ** sqlit
e89d0 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a e_io_methods obj
e89e0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ect that impleme
e89f0 6e 74 73 20 74 68 65 20 64 65 73 69 72 65 64 20 nts the desired
e8a00 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68 locking. ** beh
e8a10 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74 68 65 aviors. See the
e8a20 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 65 20 division above
e8a30 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
e8a40 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a e IOMETHODS. **
e8a50 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 69 74 macro for addit
e8a60 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ion information
e8a70 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 on finder-functi
e8a80 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d ons.. **. ** M
e8a90 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69 6d 70 ost finders simp
e8aa0 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e ly return a poin
e8ab0 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 ter to a fixed s
e8ac0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e8ad0 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 s. ** object.
e8ae0 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 But the "autoloc
e8af0 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61 69 6c kIoFinder" avail
e8b00 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 able on MacOSX d
e8b10 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a oes a little. *
e8b20 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 61 74 * more than that
e8b30 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 ; it looks at th
e8b40 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70 e filesystem typ
e8b50 65 20 74 68 61 74 20 68 6f 73 74 73 20 74 68 65 e that hosts the
e8b60 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 . ** database
e8b70 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73 20 74 file and tries t
e8b80 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b o choose an lock
e8b90 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 72 6f ing method appro
e8ba0 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 priate for. **
e8bb0 74 68 61 74 20 66 69 6c 65 73 79 73 74 65 6d 20 that filesystem
e8bc0 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 time.. */. #de
e8bd0 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56 46 53 fine UNIXVFS(VFS
e8be0 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 7b 20 NAME, FINDER) {
e8bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8c00 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 \. 1,
e8c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8c20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a /* iVersion *
e8c30 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e8c40 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f \. sizeo
e8c50 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20 20 f(unixFile),
e8c60 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 /* szOsFile */
e8c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8c80 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54 \. MAX_PAT
e8c90 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 2f HNAME, /
e8ca0 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20 * mxPathname */
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8cc0 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 \. 0,
e8cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e8ce0 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20 20 pNext */
e8cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e8d00 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 20 20 . VFSNAME,
e8d10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e /* zN
e8d20 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ame */
e8d30 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e8d40 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e 44 45 (void*)&FINDE
e8d50 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 R, /* pApp
e8d60 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20 20 Data */
e8d70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e8d80 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20 20 unixOpen,
e8d90 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
e8da0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8db0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
e8dc0 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20 20 nixDelete,
e8dd0 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 /* xDelete
e8de0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8df0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
e8e00 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 xAccess,
e8e10 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f /* xAccess */
e8e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8e30 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 \. unixF
e8e40 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 ullPathname,
e8e50 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d /* xFullPathnam
e8e60 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e8e70 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f \. unixDlO
e8e80 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f pen, /
e8e90 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 * xDlOpen */
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8eb0 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 72 \. unixDlErr
e8ec0 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 or, /*
e8ed0 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 xDlError */
e8ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e8ef0 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20 . unixDlSym,
e8f00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 /* xD
e8f10 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 20 lSym */
e8f20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e8f30 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 unixDlClose,
e8f40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 /* xDlC
e8f50 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 lose */
e8f60 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e8f70 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c unixRandomness,
e8f80 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f /* xRando
e8f90 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 mness */
e8fa0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
e8fb0 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 20 nixSleep,
e8fc0 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a /* xSleep *
e8fd0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e8fe0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
e8ff0 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 xCurrentTime,
e9000 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 /* xCurrentTi
e9010 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 me */
e9020 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 47 \. unixG
e9030 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 20 etLastError
e9040 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f /* xGetLastErro
e9050 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 r */
e9060 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 \. }.. /*.
e9070 20 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c 74 20 ** All default
e9080 56 46 53 65 73 20 66 6f 72 20 75 6e 69 78 20 61 VFSes for unix a
e9090 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 re contained in
e90a0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 the following ar
e90b0 72 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e ray.. **. ** N
e90c0 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 71 6c ote that the sql
e90d0 69 74 65 33 5f 76 66 73 2e 70 4e 65 78 74 20 66 ite3_vfs.pNext f
e90e0 69 65 6c 64 20 6f 66 20 74 68 65 20 56 46 53 20 ield of the VFS
e90f0 6f 62 6a 65 63 74 20 69 73 20 6d 6f 64 69 66 69 object is modifi
e9100 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 53 ed. ** by the S
e9110 51 4c 69 74 65 20 63 6f 72 65 20 77 68 65 6e 20 QLite core when
e9120 74 68 65 20 56 46 53 20 69 73 20 72 65 67 69 73 the VFS is regis
e9130 74 65 72 65 64 2e 20 20 53 6f 20 74 68 65 20 66 tered. So the f
e9140 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72 ollowing. ** ar
e9150 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f ray cannot be co
e9160 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 nst.. */. stat
e9170 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 61 ic sqlite3_vfs a
e9180 56 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51 Vfs[] = {.#if SQ
e9190 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
e91a0 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 28 4f 53 ING_STYLE && (OS
e91b0 5f 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69 _VXWORKS || defi
e91c0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a ned(__APPLE__)).
e91d0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
e91e0 78 22 2c 20 20 20 20 20 20 20 20 20 20 61 75 74 x", aut
e91f0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c olockIoFinder ),
e9200 0a 23 65 6c 73 65 0a 20 20 20 20 55 4e 49 58 56 .#else. UNIXV
e9210 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20 FS("unix",
e9220 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 posixIoFinde
e9230 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 r ),.#endif.
e9240 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f UNIXVFS("unix-no
e9250 6e 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 ne", nolockI
e9260 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 oFinder ),. U
e9270 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 64 6f 74 NIXVFS("unix-dot
e9280 66 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 file", dotlockI
e9290 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f oFinder ),.#if O
e92a0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e S_VXWORKS. UN
e92b0 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65 IXVFS("unix-name
e92c0 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64 dsem", semIoFind
e92d0 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 er ),.#endif.#if
e92e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
e92f0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 OCKING_STYLE.
e9300 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 UNIXVFS("unix-p
e9310 6f 73 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49 osix", posixI
e9320 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21 oFinder ),.#if !
e9330 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 OS_VXWORKS. U
e9340 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f NIXVFS("unix-flo
e9350 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46 ck", flockIoF
e9360 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a inder ),.#endif.
e9370 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
e9380 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e9390 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
e93a0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
e93b0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61 UNIXVFS("unix-a
e93c0 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46 fp", afpIoF
e93d0 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 inder ),. UNI
e93e0 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79 XVFS("unix-proxy
e93f0 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e ", proxyIoFin
e9400 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 der ),.#endif.
e9410 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e };. unsigned in
e9420 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t i; /*
e9430 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
e9440 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 .. /* Register
e9450 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 all VFSes define
e9460 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 d in the aVfs[]
e9470 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 array */. for(i
e9480 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 =0; i<(sizeof(aV
e9490 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 fs)/sizeof(sqlit
e94a0 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a e3_vfs)); i++){.
e94b0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f sqlite3_vfs_
e94c0 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 register(&aVfs[i
e94d0 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 ], i==0);. }.
e94e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e94f0 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 ; .}../*.** Shut
e9500 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 down the operati
e9510 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
e9520 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 ace..**.** Some
e9530 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
e9540 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 s might need to
e9550 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 do some cleanup
e9560 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c in this routine,
e9570 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 .** to release d
e9580 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
e9590 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 ated objects. B
e95a0 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a ut not on unix..
e95b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
e95c0 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 is a no-op for u
e95d0 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 nix..*/.SQLITE_A
e95e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
e95f0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 s_end(void){ .
e9600 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e9610 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a ; .}. .#endif /*
e9620 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
e9630 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
e9640 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e *** End of os_un
e9650 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
e9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9680 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
e9690 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
e96a0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
e96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e96d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
e96e0 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
e96f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
e9700 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
e9710 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
e9720 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
e9730 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
e9740 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
e9750 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
e9760 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
e9770 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
e9780 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
e9790 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
e97a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
e97b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
e97c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
e97d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
e97e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
e97f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
e9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9840 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
e9850 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
e9860 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
e9870 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 specific to wind
e9880 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ows..**.** $Id:
e9890 6f 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 35 36 os_win.c,v 1.156
e98a0 20 32 30 30 39 2f 30 34 2f 32 33 20 31 39 3a 30 2009/04/23 19:0
e98b0 38 3a 33 33 20 73 68 61 6e 65 20 45 78 70 20 24 8:33 shane Exp $
e98c0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
e98d0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 S_WIN
e98e0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 /* This file
e98f0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e is used for win
e9900 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f dows only */.../
e9910 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 *.** A Note Abou
e9920 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 t Memory Allocat
e9930 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion:.**.** This
e9940 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c driver uses mall
e9950 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 oc()/free() dire
e9960 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e ctly rather than
e9970 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a going through.*
e9980 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 * the SQLite-wra
e9990 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 ppers sqlite3_ma
e99a0 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 lloc()/sqlite3_f
e99b0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 ree(). Those wr
e99c0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 appers.** are de
e99d0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f signed for use o
e99e0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
e99f0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 ms where memory
e9a00 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a is scarce and.**
e9a10 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
e9a20 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 happen frequent
e9a30 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 ly. Win32 does
e9a40 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 not typically ru
e9a50 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 n on.** embedded
e9a60 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 systems, and wh
e9a70 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 en it does the d
e9a80 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c evelopers normal
e9a90 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a ly have bigger.*
e9aa0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f * problems to wo
e9ab0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 rry about than r
e9ac0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 unning out of me
e9ad0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 mory. So there
e9ae0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 is not.** a comp
e9af0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 elling need to u
e9b00 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
e9b10 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 .**.** But there
e9b20 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f is a good reaso
e9b30 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 n to not use the
e9b40 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 wrappers. If w
e9b50 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 e use the.** wra
e9b60 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 ppers then we wi
e9b70 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 ll get simulated
e9b80 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
e9b90 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a es within this.*
e9ba0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 * driver. And t
e9bb0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b hat causes all k
e9bc0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 inds of problems
e9bd0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 for our tests.
e9be0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 We.** could enh
e9bf0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 ance SQLite to d
e9c00 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 eal with simulat
e9c10 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ed malloc failur
e9c20 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 es within.** the
e9c30 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 OS driver, but
e9c40 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c the code to deal
e9c50 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c with those fail
e9c60 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a ure would not.**
e9c70 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e be exercised on
e9c80 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f Linux (which do
e9c90 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
e9ca0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 alloc() in the d
e9cb0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f river).** and so
e9cc0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 we would have d
e9cd0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e ifficulty writin
e9ce0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 g coverage tests
e9cf0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 for that.** cod
e9d00 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 e. Better to le
e9d10 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 ave the code out
e9d20 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a , we think..**.*
e9d30 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 * The point of t
e9d40 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 his discussion i
e9d50 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 s as follows: W
e9d60 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
e9d70 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 ew.** OS layer f
e9d80 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 or an embedded s
e9d90 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 ystem, if you us
e9da0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 e this file as a
e9db0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 n example,.** av
e9dc0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d oid the use of m
e9dd0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 alloc()/free().
e9de0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 Those routines
e9df0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f work ok on windo
e9e00 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 ws.** desktops b
e9e10 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
e9e20 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
e9e30 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 ms..*/..#include
e9e40 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 <winbase.h>..#i
e9e50 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a fdef __CYGWIN__.
e9e60 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 # include <sys/c
e9e70 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a ygwin.h>.#endif.
e9e80 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 ./*.** Macros us
e9e90 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
e9ea0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
e9eb0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a o use threads..*
e9ec0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 /.#if defined(TH
e9ed0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 READSAFE) && THR
e9ee0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
e9ef0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 SQLITE_W32_THRE
e9f00 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a ADS 1.#endif../*
e9f10 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 .** Include code
e9f20 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
e9f30 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 to all os_*.c fi
e9f40 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a les.*/./********
e9f50 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
e9f60 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 s_common.h in th
e9f70 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 e middle of os_w
e9f80 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
e9f90 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
e9fa0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
e9fb0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a e os_common.h **
e9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9fe0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
e9ff0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
ea000 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
ea010 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
ea020 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
ea030 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
ea040 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
ea050 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
ea060 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
ea070 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
ea080 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
ea090 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
ea0a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
ea0b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
ea0c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
ea0d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
ea0e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
ea0f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
ea100 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
ea110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
ea160 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
ea170 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 tains macros and
ea180 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 a little bit of
ea190 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
ea1a0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f mmon to.** all o
ea1b0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 f the platform-s
ea1c0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f pecific files (o
ea1d0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 s_*.c) and is #i
ea1e0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f ncluded into tho
ea1f0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a se.** files..**.
ea200 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f ** This file sho
ea210 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 uld be #included
ea220 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 by the os_*.c f
ea230 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 iles only. It i
ea240 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 s not a.** gener
ea250 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 al purpose heade
ea260 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 r file..**.** $I
ea270 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 d: os_common.h,v
ea280 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 1.38 2009/02/24
ea290 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 18:40:50 daniel
ea2a0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
ea2b0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
ea2c0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
ea2d0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
ea2e0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
ea2f0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
ea300 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
ea310 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
ea320 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
ea330 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
ea340 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
ea350 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
ea360 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
ea370 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
ea380 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
ea390 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
ea3a0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
ea3b0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
ea3c0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
ea3d0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
ea3e0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
ea3f0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
ea400 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
ea410 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
ea420 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
ea430 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
ea440 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ea450 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 OSTrace = 0;.#de
ea460 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
ea470 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
ea480 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
ea490 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
ea4a0 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
ea4b0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 ACE2(X,Y)
ea4c0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
ea4d0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
ea4e0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 gPrintf(X,Y).#de
ea4f0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
ea500 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c Y,Z) if( sql
ea510 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
ea520 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
ea530 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
ea540 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
ea550 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f ) if( sqlite3O
ea560 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
ea570 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
ea580 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
ea590 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
ea5a0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
ea5b0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
ea5c0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
ea5d0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
ea5e0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
ea5f0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
ea600 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
ea610 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
ea620 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
ea630 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
ea640 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 Z,A,B,C,D) \.
ea650 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
ea660 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
ea670 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
ea680 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 ,C,D).#else.#def
ea690 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a ine OSTRACE1(X).
ea6a0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
ea6b0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
ea6c0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 TRACE3(X,Y,Z).#d
ea6d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
ea6e0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
ea6f0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
ea700 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
ea710 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
ea720 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
ea730 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
ea740 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
ea750 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f Macros for perfo
ea760 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 rmance tracing.
ea770 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
ea780 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b off. Only work
ea790 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 s.** on i486 har
ea7a0 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 dware..*/.#ifdef
ea7b0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 SQLITE_PERFORMA
ea7c0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a NCE_TRACE../* .*
ea7d0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
ea7e0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
ea7f0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
ea800 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
ea810 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
ea820 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
ea830 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
ea840 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
ea850 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
ea860 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e le of os_common.
ea870 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
ea880 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
ea890 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
ea8a0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
ea8e0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
ea8f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
ea900 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
ea910 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
ea920 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
ea930 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
ea940 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
ea950 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
ea960 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
ea970 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
ea980 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
ea990 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
ea9a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
ea9b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
ea9c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
ea9d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
ea9e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
ea9f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
eaa50 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
eaa60 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
eaa70 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
eaa80 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
eaa90 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
eaaa0 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
eaab0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
eaac0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
eaad0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
eaae0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
eaaf0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
eab00 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
eab10 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
eab20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
eab30 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
eab40 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
eab50 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
eab60 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
eab70 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
eab80 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
eab90 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
eaba0 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
eabb0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
eabc0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
eabd0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
eabe0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
eabf0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
eac00 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
eac10 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
eac20 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
eac30 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
eac40 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
eac50 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
eac60 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
eac70 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
eac80 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
eac90 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
eaca0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
eacb0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
eacc0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
eacd0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
eace0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
eacf0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
ead00 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
ead10 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
ead20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
ead30 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
ead40 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
ead50 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
ead60 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
ead70 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
ead80 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
ead90 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
eada0 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
eadb0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
eadc0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
eadd0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
eade0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
eadf0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
eae00 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
eae10 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
eae20 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
eae30 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
eae40 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
eae50 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
eae60 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
eae70 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
eae80 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
eae90 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
eaea0 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
eaeb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
eaec0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
eaed0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
eaee0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
eaef0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
eaf00 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
eaf10 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
eaf20 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
eaf30 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
eaf40 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
eaf50 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
eaf60 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
eaf70 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
eaf80 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
eaf90 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
eafa0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
eafb0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
eafc0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
eafd0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
eafe0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
eaff0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
eb000 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
eb010 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
eb020 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
eb030 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
eb040 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
eb050 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
eb060 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
eb070 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
eb080 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
eb090 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
eb0a0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
eb0b0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
eb0c0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
eb0d0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
eb0e0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
eb0f0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
eb100 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
eb110 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
eb120 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
eb130 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
eb140 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
eb150 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
eb160 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
eb170 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
eb180 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
eb190 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
eb1a0 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
eb1b0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
eb1c0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
eb1d0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
eb1e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
eb1f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
eb200 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
eb210 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
eb220 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
eb230 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
eb240 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
eb250 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
eb260 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
eb270 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
eb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb2a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
eb2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
eb2c0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
eb2d0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d ft off in os_com
eb2e0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
eb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 ********/..stati
eb300 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
eb310 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 g_start;.static
eb320 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
eb330 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 elapsed;.#define
eb340 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 TIMER_START
eb350 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 g_start=sqlit
eb360 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 e3Hwtime().#defi
eb370 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 ne TIMER_END
eb380 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 g_elapsed=s
eb390 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 qlite3Hwtime()-g
eb3a0 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 _start.#define T
eb3b0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
eb3c0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 g_elapsed.#else
eb3d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 .#define TIMER_S
eb3e0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d TART.#define TIM
eb3f0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 ER_END.#define T
eb400 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
eb410 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
eb420 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a )0).#endif../*.*
eb430 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 * If we compile
eb440 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
eb450 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 TEST macro set,
eb460 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 then the followi
eb470 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 ng block.** of c
eb480 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 ode will give us
eb490 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 the ability to
eb4a0 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 simulate a disk
eb4b0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 I/O error. This
eb4c0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 .** is used for
eb4d0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 testing the I/O
eb4e0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a recovery logic..
eb4f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
eb500 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
eb510 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
eb520 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 error_hit = 0;
eb530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
eb540 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f al number of I/O
eb550 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 Errors */.SQLIT
eb560 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
eb570 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
eb580 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f it = 0; /
eb590 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d * Number of non-
eb5a0 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f benign errors */
eb5b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
eb5c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
eb5d0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 _pending = 0;
eb5e0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f /* Count do
eb5f0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 wn to first I/O
eb600 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f error */.SQLITE_
eb610 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
eb620 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 io_error_persist
eb630 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
eb640 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f True if I/O erro
eb650 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 rs persist */.SQ
eb660 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
eb670 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
eb680 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 nign = 0;
eb690 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 /* True if err
eb6a0 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a ors are benign *
eb6b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
eb6c0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
eb6d0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 l_pending = 0;.S
eb6e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
eb6f0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
eb700 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 0;.#define Simu
eb710 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
eb720 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f n(X) sqlite3_io_
eb730 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 error_benign=(X)
eb740 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
eb750 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 eIOError(CODE)
eb760 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 \. if( (sqlite3
eb770 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
eb780 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f t && sqlite3_io_
eb790 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 error_hit) \.
eb7a0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 || sqlite3_i
eb7b0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d o_error_pending-
eb7c0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 - == 1 ) \.
eb7d0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 { loca
eb7e0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b l_ioerr(); CODE;
eb7f0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c }.static void l
eb800 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 ocal_ioerr(){.
eb810 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c IOTRACE(("IOERR\
eb820 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f n"));. sqlite3_
eb830 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a io_error_hit++;.
eb840 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 if( !sqlite3_i
eb850 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 o_error_benign )
eb860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
eb870 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 r_hardhit++;.}.#
eb880 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
eb890 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 iskfullError(COD
eb8a0 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 E) \. if( sqli
eb8b0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
eb8c0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 ding ){ \. i
eb8d0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
eb8e0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 ull_pending == 1
eb8f0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 ){ \. loc
eb900 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 al_ioerr(); \.
eb910 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
eb920 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 kfull = 1; \.
eb930 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 sqlite3_io_e
eb940 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a rror_hit = 1; \.
eb950 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 CODE; \.
eb960 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 }else{ \.
eb970 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b sqlite3_disk
eb980 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 full_pending--;
eb990 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a \. } \. }.
eb9a0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 #else.#define Si
eb9b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
eb9c0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 ign(X).#define S
eb9d0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 imulateIOError(A
eb9e0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
eb9f0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
eba00 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a A).#endif../*.**
eba10 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b When testing, k
eba20 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 eep a count of t
eba30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 he number of ope
eba40 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 n files..*/.#ifd
eba50 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
eba60 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
eba70 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
eba80 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 count = 0;.#defi
eba90 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
ebaa0 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f ) sqlite3_open_
ebab0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a file_count+=(X).
ebac0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 #else.#define Op
ebad0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e enCounter(X).#en
ebae0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
ebaf0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d defined(_OS_COMM
ebb00 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ON_H_) */../****
ebb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
ebb20 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
ebb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
ebb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
ebb70 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
ebb80 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 eft off in os_wi
ebb90 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
ebba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
ebbb0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 * Some microsoft
ebbc0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 compilers lack
ebbd0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e this definition.
ebbe0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 .*/.#ifndef INVA
ebbf0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
ebc00 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 TES.# define INV
ebc10 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
ebc20 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 UTES ((DWORD)-1)
ebc30 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
ebc40 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
ebc50 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
ebc60 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
ebc70 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
ebc80 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
ebc90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
ebca0 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 INCE.# define Ar
ebcb0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
ebcc0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69 1.# define GetDi
ebcd0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30 skFreeSpaceW() 0
ebce0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
ebcf0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 inCE lacks nativ
ebd00 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 e support for fi
ebd10 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 le locking so we
ebd20 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 have to fake it
ebd30 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f .** with some co
ebd40 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a de of our own..*
ebd50 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
ebd60 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 WINCE.typedef st
ebd70 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b ruct winceLock {
ebd80 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b . int nReaders;
ebd90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ebda0 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 of reader locks
ebdb0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 obtained */. B
ebdc0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 OOL bPending;
ebdd0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 /* Indicates
ebde0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 a pending lock h
ebdf0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 as been obtained
ebe00 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 */. BOOL bRese
ebe10 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 rved; /* Ind
ebe20 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 icates a reserve
ebe30 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 d lock has been
ebe40 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f obtained */. BO
ebe50 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 OL bExclusive;
ebe60 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 /* Indicates a
ebe70 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
ebe80 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
ebe90 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 ed */.} winceLoc
ebea0 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a k;.#endif../*.**
ebeb0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 The winFile str
ebec0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 ucture is a subc
ebed0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
ebee0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 file* specific t
ebef0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 o the win32.** p
ebf00 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 ortability layer
ebf10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
ebf20 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 uct winFile winF
ebf30 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 ile;.struct winF
ebf40 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ile {. const sq
ebf50 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
ebf60 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 *pMethod;/* Mus
ebf70 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 t be first */.
ebf80 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 HANDLE h;
ebf90 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
ebfa0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
ebfb0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e the file */. un
ebfc0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
ebfd0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 type; /* Type of
ebfe0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 lock currently
ebff0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c held on this fil
ec000 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 e */. short sha
ec010 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f redLockByte; /
ec020 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 * Randomly chose
ec030 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 n byte used as a
ec040 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
ec050 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e DWORD lastErrn
ec060 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 o; /* The
ec070 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66 Windows errno f
ec080 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f rom the last I/O
ec090 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 error */. DWOR
ec0a0 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 D sectorSize;
ec0b0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 /* Sector si
ec0c0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65 ze of the device
ec0d0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 file is on */.#
ec0e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ec0f0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c CE. WCHAR *zDel
ec100 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
ec110 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
ec120 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
ec130 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
ec140 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
ec150 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
ec160 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
ec170 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
ec180 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
ec190 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
ec1a0 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
ec1b0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
ec1c0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
ec1d0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
ec1e0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
ec1f0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
ec200 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
ec210 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
ec220 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
ec230 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
ec240 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
ec250 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
ec260 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
ec270 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70 Forward prototyp
ec280 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
ec290 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
ec2a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
ec2b0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
ec2c0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
ec2d0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
ec2e0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a ile name */.);..
ec2f0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
ec300 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 ing variable is
ec310 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f (normally) set o
ec320 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 nce and never ch
ec330 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 anges.** thereaf
ec340 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 ter. It records
ec350 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 whether the ope
ec360 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
ec370 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e Win95.** or Win
ec380 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f NT..**.** 0: O
ec390 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
ec3a0 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 unknown..** 1:
ec3b0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
ec3c0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 m is Win95..** 2
ec3d0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
ec3e0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a stem is WinNT..*
ec3f0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f *.** In order to
ec400 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 facilitate test
ec410 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 ing on a WinNT s
ec420 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 ystem, the test
ec430 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d fixture.** can m
ec440 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 anually set this
ec450 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 value to 1 to e
ec460 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 mulate Win98 beh
ec470 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 avior..*/.#ifdef
ec480 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
ec490 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
ec4a0 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b te3_os_type = 0;
ec4b0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e .#else.static in
ec4c0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 t sqlite3_os_typ
ec4d0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
ec4e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
ec4f0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
ec500 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
ec510 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
ec520 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
ec530 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
ec540 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
ec550 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
ec560 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
ec570 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
ec580 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
ec590 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
ec5a0 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
ec5b0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
ec5c0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
ec5d0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
ec5e0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
ec5f0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
ec600 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
ec610 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72 t call it when r
ec620 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f unning Win95/98/
ec630 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a ME. A call to.*
ec640 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
ec650 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
ec660 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 ine if the host
ec670 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f is Win95/98/ME o
ec680 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 r.** WinNT/2K/XP
ec690 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c so that we will
ec6a0 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 know whether or
ec6b0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 not we can safe
ec6c0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c ly call.** the L
ec6d0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
ec6e0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
ec6f0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
ec700 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c isNT() (1).#el
ec710 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
ec720 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 isNT(void){.
ec730 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 if( sqlite3_os_t
ec740 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ype==0 ){.
ec750 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 OSVERSIONINFO sI
ec760 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f nfo;. sInfo
ec770 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f .dwOSVersionInfo
ec780 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 Size = sizeof(sI
ec790 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 nfo);. GetV
ec7a0 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 ersionEx(&sInfo)
ec7b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
ec7c0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e os_type = sInfo.
ec7d0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 dwPlatformId==VE
ec7e0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 R_PLATFORM_WIN32
ec7f0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 _NT ? 2 : 1;.
ec800 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 }. return sq
ec810 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 lite3_os_type==2
ec820 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
ec830 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
ec840 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 */../*.** Conver
ec850 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 t a UTF-8 string
ec860 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e to microsoft un
ec870 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e icode (UTF-16?).
ec880 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f .**.** Space to
ec890 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
ec8a0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 ed string is obt
ec8b0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
ec8c0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 c..*/.static WCH
ec8d0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 AR *utf8ToUnicod
ec8e0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 e(const char *zF
ec8f0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 ilename){. int
ec900 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a nChar;. WCHAR *
ec910 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a zWideFilename;..
ec920 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 nChar = MultiB
ec930 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 yteToWideChar(CP
ec940 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e _UTF8, 0, zFilen
ec950 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 ame, -1, NULL, 0
ec960 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 );. zWideFilena
ec970 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 me = malloc( nCh
ec980 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 ar*sizeof(zWideF
ec990 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 ilename[0]) );.
ec9a0 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 if( zWideFilena
ec9b0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 me==0 ){. ret
ec9c0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 urn 0;. }. nCh
ec9d0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
ec9e0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
ec9f0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
eca00 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d -1, zWideFilenam
eca10 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 e, nChar);. if(
eca20 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 nChar==0 ){.
eca30 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e free(zWideFilen
eca40 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 ame);. zWideF
eca50 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
eca60 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 . return zWideF
eca70 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
eca80 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
eca90 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 oft unicode to U
ecaa0 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 TF-8. Space to
ecab0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
ecac0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f d string is.** o
ecad0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c btained from mal
ecae0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
ecaf0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
ecb00 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 Utf8(const WCHAR
ecb10 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
ecb20 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
ecb30 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
ecb40 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 ;.. nByte = Wid
ecb50 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
ecb60 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 (CP_UTF8, 0, zWi
ecb70 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
ecb80 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 0, 0, 0, 0);. z
ecb90 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f Filename = mallo
ecba0 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 c( nByte );. if
ecbb0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 ( zFilename==0 )
ecbc0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
ecbd0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 }. nByte = Wi
ecbe0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
ecbf0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
ecc00 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
ecc10 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 zFilename, nByt
ecc20 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
ecc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ecc40 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
ecc50 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
ecc60 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b free(zFilename);
ecc70 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d . zFilename =
ecc80 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
ecc90 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f zFilename;.}../
ecca0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 *.** Convert an
eccb0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d ansi string to m
eccc0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
eccd0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a , based on the.*
ecce0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 * current codepa
eccf0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 ge settings for
ecd00 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a file apis..** .*
ecd10 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
ecd20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
ecd30 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a ing is obtained.
ecd40 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ** from malloc..
ecd50 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 */.static WCHAR
ecd60 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 *mbcsToUnicode(c
ecd70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
ecd80 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
ecd90 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 te;. WCHAR *zMb
ecda0 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e csFilename;. in
ecdb0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 t codepage = Are
ecdc0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f FileApisANSI() ?
ecdd0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d CP_ACP : CP_OEM
ecde0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d CP;.. nByte = M
ecdf0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
ece00 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 ar(codepage, 0,
ece10 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e zFilename, -1, N
ece20 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 ULL,0)*sizeof(WC
ece30 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c HAR);. zMbcsFil
ece40 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
ece50 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 nByte*sizeof(zMb
ece60 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 csFilename[0]) )
ece70 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c ;. if( zMbcsFil
ece80 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 ename==0 ){.
ece90 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
ecea0 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 nByte = MultiByt
eceb0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 eToWideChar(code
ecec0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 page, 0, zFilena
eced0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c me, -1, zMbcsFil
ecee0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 ename, nByte);.
ecef0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b if( nByte==0 ){
ecf00 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 . free(zMbcsF
ecf10 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d ilename);. zM
ecf20 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b bcsFilename = 0;
ecf30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d . }. return zM
ecf40 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a bcsFilename;.}..
ecf50 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 /*.** Convert mi
ecf60 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
ecf70 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 to multibyte cha
ecf80 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 racter string, b
ecf90 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 ased on the.** u
ecfa0 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 ser's Ansi codep
ecfb0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 age..**.** Space
ecfc0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
ecfd0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
ecfe0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
ecff0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
ed000 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f atic char *unico
ed010 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 deToMbcs(const W
ed020 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e CHAR *zWideFilen
ed030 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
ed040 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 e;. char *zFile
ed050 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 name;. int code
ed060 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 page = AreFileAp
ed070 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 isANSI() ? CP_AC
ed080 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 P : CP_OEMCP;..
ed090 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
ed0a0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 rToMultiByte(cod
ed0b0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 epage, 0, zWideF
ed0c0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 ilename, -1, 0,
ed0d0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 0, 0, 0);. zFil
ed0e0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
ed0f0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a nByte );. if( z
ed100 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Filename==0 ){.
ed110 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
ed120 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 . nByte = WideC
ed130 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 harToMultiByte(c
ed140 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 odepage, 0, zWid
ed150 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a eFilename, -1, z
ed160 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c Filename, nByte,
ed170 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ed180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
ed190 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 , 0);. if( nByt
ed1a0 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 e == 0 ){. fr
ed1b0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ee(zFilename);.
ed1c0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 zFilename = 0
ed1d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
ed1e0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
ed1f0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 ** Convert multi
ed200 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
ed210 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 tring to UTF-8.
ed220 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
ed230 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 he.** returned s
ed240 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
ed250 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e d from malloc().
ed260 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
ed270 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e har *sqlite3_win
ed280 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
ed290 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
ed2a0 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a ename){. char *
ed2b0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 zFilenameUtf8;.
ed2c0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 WCHAR *zTmpWide
ed2d0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 ;.. zTmpWide =
ed2e0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 mbcsToUnicode(zF
ed2f0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ilename);. if(
ed300 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 zTmpWide==0 ){.
ed310 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
ed320 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 . zFilenameUtf8
ed330 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
ed340 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 (zTmpWide);. fr
ed350 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 ee(zTmpWide);.
ed360 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 return zFilename
ed370 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Utf8;.}../*.** C
ed380 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 onvert UTF-8 to
ed390 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 multibyte charac
ed3a0 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 ter string. Spa
ed3b0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a ce to hold the .
ed3c0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 ** returned stri
ed3d0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ng is obtained f
ed3e0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f rom malloc()..*/
ed3f0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 .static char *ut
ed400 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 f8ToMbcs(const c
ed410 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
ed420 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
ed430 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 meMbcs;. WCHAR
ed440 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 *zTmpWide;.. zT
ed450 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 mpWide = utf8ToU
ed460 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
ed470 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 );. if( zTmpWid
ed480 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
ed490 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c rn 0;. }. zFil
ed4a0 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 enameMbcs = unic
ed4b0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 odeToMbcs(zTmpWi
ed4c0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 de);. free(zTmp
ed4d0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Wide);. return
ed4e0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d zFilenameMbcs;.}
ed4f0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
ed500 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a WINCE./*********
ed510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed550 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e .** This section
ed560 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 contains code f
ed570 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a or WinCE only..*
ed580 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 /./*.** WindowsC
ed590 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 E does not have
ed5a0 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 a localtime() fu
ed5b0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 nction. So crea
ed5c0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 te a.** substitu
ed5d0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d te..*/.struct tm
ed5e0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 *__cdecl localt
ed5f0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 ime(const time_t
ed600 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 *t).{. static
ed610 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 struct tm y;. F
ed620 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d ILETIME uTm, lTm
ed630 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 ;. SYSTEMTIME p
ed640 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e Tm;. sqlite3_in
ed650 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d t64 t64;. t64 =
ed660 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 *t;. t64 = (t6
ed670 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 4 + 11644473600)
ed680 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d *10000000;. uTm
ed690 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d .dwLowDateTime =
ed6a0 20 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 t64 & 0xFFFFFFF
ed6b0 46 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 F;. uTm.dwHighD
ed6c0 61 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 ateTime= t64 >>
ed6d0 33 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 32;. FileTimeTo
ed6e0 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 LocalFileTime(&u
ed6f0 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 Tm,&lTm);. File
ed700 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 TimeToSystemTime
ed710 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 (&lTm,&pTm);. y
ed720 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 .tm_year = pTm.w
ed730 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 Year - 1900;. y
ed740 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d .tm_mon = pTm.wM
ed750 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d onth - 1;. y.tm
ed760 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 _wday = pTm.wDay
ed770 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d OfWeek;. y.tm_m
ed780 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a day = pTm.wDay;.
ed790 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 y.tm_hour = pT
ed7a0 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f m.wHour;. y.tm_
ed7b0 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 min = pTm.wMinut
ed7c0 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 e;. y.tm_sec =
ed7d0 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 pTm.wSecond;. r
ed7e0 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 eturn &y;.}../*
ed7f0 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 This will never
ed800 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 be called, but d
ed810 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 efined to make t
ed820 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 he code compile
ed830 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 */.#define GetTe
ed840 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 mpPathA(a,b)..#d
ed850 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 efine LockFile(a
ed860 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 ,b,c,d,e)
ed870 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 winceLockFile(&a
ed880 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 , b, c, d, e).#d
ed890 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 efine UnlockFile
ed8a0 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 (a,b,c,d,e)
ed8b0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 winceUnlockFile(
ed8c0 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a &a, b, c, d, e).
ed8d0 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 #define LockFile
ed8e0 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 Ex(a,b,c,d,e,f)
ed8f0 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 winceLockFileE
ed900 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 x(&a, b, c, d, e
ed910 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 , f)..#define HA
ed920 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 NDLE_TO_WINFILE(
ed930 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 a) (winFile*)&((
ed940 63 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 char*)a)[-offset
ed950 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a of(winFile,h)]..
ed960 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 /*.** Acquire a
ed970 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 lock on the hand
ed980 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 le h.*/.static v
ed990 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63 oid winceMutexAc
ed9a0 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b quire(HANDLE h){
ed9b0 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b . DWORD dwErr;
ed9c0 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 . do {. dw
ed9d0 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e Err = WaitForSin
ed9e0 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 gleObject(h, INF
ed9f0 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 INITE);. } whi
eda00 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49 le (dwErr != WAI
eda10 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77 T_OBJECT_0 && dw
eda20 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e Err != WAIT_ABAN
eda30 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 DONED);.}./*.**
eda40 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 Release a lock a
eda50 63 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65 cquired by wince
eda60 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a MutexAcquire().*
eda70 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d /.#define winceM
eda80 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52 utexRelease(h) R
eda90 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a eleaseMutex(h)..
edaa0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 /*.** Create the
edab0 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65 mutex and share
edac0 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f d memory used fo
edad0 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 r locking in the
edae0 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 file.** descrip
edaf0 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 tor pFile.*/.sta
edb00 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 tic BOOL winceCr
edb10 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 eateLock(const c
edb20 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
edb30 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b winFile *pFile){
edb40 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a . WCHAR *zTok;.
edb50 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d WCHAR *zName =
edb60 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a utf8ToUnicode(z
edb70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f Filename);. BOO
edb80 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a L bInit = TRUE;.
edb90 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 . /* Initialize
edba0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 the local lockd
edbb0 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d ata */. ZeroMem
edbc0 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 ory(&pFile->loca
edbd0 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d l, sizeof(pFile-
edbe0 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 >local));.. /*
edbf0 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b Replace the back
edc00 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65 slashes from the
edc10 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f filename and lo
edc20 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 wercase it. **
edc30 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65 to derive a mute
edc40 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f x name. */. zTo
edc50 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a k = CharLowerW(z
edc60 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a Name);. for (;*
edc70 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 zTok;zTok++){.
edc80 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 if (*zTok == '
edc90 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 \\') *zTok = '_'
edca0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 ;. }.. /* Crea
edcb0 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 te/open the name
edcc0 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 d mutex */. pFi
edcd0 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 le->hMutex = Cre
edce0 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 ateMutexW(NULL,
edcf0 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 FALSE, zName);.
edd00 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 if (!pFile->hMu
edd10 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d tex){. pFile-
edd20 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 >lastErrno = Get
edd30 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
edd40 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 free(zName);.
edd50 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a return FALSE;.
edd60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
edd70 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 66 6f e the mutex befo
edd80 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f re continuing */
edd90 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 . winceMutexAcq
edda0 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 uire(pFile->hMut
eddb0 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 69 6e ex);. . /* Sin
eddc0 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 ce the names of
eddd0 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c 20 73 named mutexes, s
edde0 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c 65 20 emaphores, file
eddf0 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 72 65 mappings etc are
ede00 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 6e 73 . ** case-sens
ede10 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 76 61 itive, take adva
ede20 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 62 79 ntage of that by
ede30 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 68 65 uppercasing the
ede40 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 2a 2a mutex name. **
ede50 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 74 20 and using that
ede60 61 73 20 74 68 65 20 73 68 61 72 65 64 20 66 69 as the shared fi
ede70 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 2e 0a lemapping name..
ede80 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 65 72 */. CharUpper
ede90 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 69 6c W(zName);. pFil
edea0 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 72 65 e->hShared = Cre
edeb0 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 28 ateFileMappingW(
edec0 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 INVALID_HANDLE_V
eded0 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 ALUE, NULL,.
edee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edf00 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 49 54 PAGE_READWRIT
edf10 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e E, 0, sizeof(win
edf20 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20 ceLock),.
edf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edf50 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 zName); .. /*
edf60 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 74 20 Set a flag that
edf70 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 65 20 indicates we're
edf80 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 72 65 the first to cre
edf90 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 ate the memory s
edfa0 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 74 20 o it . ** must
edfb0 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 6c 69 be zero-initiali
edfc0 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 65 74 zed */. if (Get
edfd0 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d 20 45 LastError() == E
edfe0 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 58 49 RROR_ALREADY_EXI
edff0 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 74 20 STS){. bInit
ee000 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 = FALSE;. }..
ee010 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 free(zName);..
ee020 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 65 64 /* If we succeed
ee030 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 68 65 ed in making the
ee040 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 shared memory h
ee050 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e 20 2a andle, map it. *
ee060 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 /. if (pFile->h
ee070 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 46 69 Shared){. pFi
ee080 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 77 69 le->shared = (wi
ee090 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 65 77 nceLock*)MapView
ee0a0 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53 OfFile(pFile->hS
ee0b0 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 20 20 hared, .
ee0c0 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45 FILE_MAP_RE
ee0d0 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54 AD|FILE_MAP_WRIT
ee0e0 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 E, 0, 0, sizeof(
ee0f0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 winceLock));.
ee100 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20 66 /* If mapping f
ee110 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68 65 ailed, close the
ee120 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 shared memory h
ee130 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65 20 andle and erase
ee140 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 70 it */. if (!p
ee150 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b 0a 20 File->shared){.
ee160 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
ee170 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 Errno = GetLastE
ee180 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 43 6c rror();. Cl
ee190 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
ee1a0 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 >hShared);.
ee1b0 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 pFile->hShared
ee1c0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 = NULL;. }.
ee1d0 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65 }.. /* If share
ee1e0 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e d memory could n
ee1f0 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74 ot be created, t
ee200 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 hen close the mu
ee210 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a tex and fail */.
ee220 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 if (pFile->hSh
ee230 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 ared == NULL){.
ee240 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c winceMutexRel
ee250 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ease(pFile->hMut
ee260 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 ex);. CloseHa
ee270 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ndle(pFile->hMut
ee280 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ex);. pFile->
ee290 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 hMutex = NULL;.
ee2a0 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b return FALSE;
ee2b0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 . }. . /* Ini
ee2c0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72 tialize the shar
ee2d0 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 ed memory if we'
ee2e0 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a re supposed to *
ee2f0 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b /. if (bInit) {
ee300 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 . ZeroMemory(
ee310 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 pFile->shared, s
ee320 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 izeof(winceLock)
ee330 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d );. }.. winceM
ee340 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
ee350 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 e->hMutex);. re
ee360 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a turn TRUE;.}../*
ee370 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20 .** Destroy the
ee380 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 part of winFile
ee390 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20 that deals with
ee3a0 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 wince locks.*/.s
ee3b0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 tatic void wince
ee3c0 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 DestroyLock(winF
ee3d0 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
ee3e0 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 f (pFile->hMutex
ee3f0 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72 ){. /* Acquir
ee400 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 e the mutex */.
ee410 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 winceMutexAcq
ee420 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 uire(pFile->hMut
ee430 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ex);.. /* The
ee440 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
ee450 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c s should probabl
ee460 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75 y assert in debu
ee470 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79 g mode, but they
ee480 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63 . are to c
ee490 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61 leanup in case a
ee4a0 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 ny locks remaine
ee4b0 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 d open */. if
ee4c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e (pFile->local.n
ee4d0 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 Readers){.
ee4e0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e pFile->shared->n
ee4f0 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 Readers --;.
ee500 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d }. if (pFile-
ee510 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 >local.bReserved
ee520 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
ee530 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 shared->bReserve
ee540 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d d = FALSE;. }
ee550 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
ee560 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b local.bPending){
ee570 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
ee580 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d ared->bPending =
ee590 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 FALSE;. }.
ee5a0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ee5b0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a al.bExclusive){.
ee5c0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
ee5d0 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 red->bExclusive
ee5e0 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a = FALSE;. }..
ee5f0 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 /* De-refere
ee600 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 nce and close ou
ee610 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68 r copy of the sh
ee620 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 ared memory hand
ee630 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 le */. UnmapV
ee640 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d iewOfFile(pFile-
ee650 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c >shared);. Cl
ee660 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
ee670 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 >hShared);..
ee680 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 /* Done with the
ee690 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 mutex */. wi
ee6a0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 nceMutexRelease(
ee6b0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 pFile->hMutex);
ee6c0 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e . CloseHan
ee6d0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 dle(pFile->hMute
ee6e0 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 x);. pFile->h
ee6f0 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 Mutex = NULL;.
ee700 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 }.}../* .** An i
ee710 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
ee720 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 the LockFile()
ee730 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 API of windows f
ee740 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 or wince.*/.stat
ee750 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 ic BOOL winceLoc
ee760 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 kFile(. HANDLE
ee770 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 *phFile,. DWORD
ee780 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 dwFileOffsetLow
ee790 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 ,. DWORD dwFile
ee7a0 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 OffsetHigh,. DW
ee7b0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
ee7c0 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 esToLockLow,. D
ee7d0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
ee7e0 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b tesToLockHigh.){
ee7f0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
ee800 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 e = HANDLE_TO_WI
ee810 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 NFILE(phFile);.
ee820 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 BOOL bReturn =
ee830 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 FALSE;.. if (!p
ee840 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 File->hMutex) re
ee850 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e turn TRUE;. win
ee860 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
ee870 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
ee880 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 /* Wanting an
ee890 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 exclusive lock?
ee8a0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
ee8b0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 ffsetLow == SHAR
ee8c0 45 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 ED_FIRST.
ee8d0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
ee8e0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 sToLockLow == SH
ee8f0 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 ARED_SIZE){.
ee900 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 if (pFile->share
ee910 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 d->nReaders == 0
ee920 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 && pFile->share
ee930 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d d->bExclusive ==
ee940 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 0){. pFil
ee950 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c e->shared->bExcl
ee960 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 usive = TRUE;.
ee970 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
ee980 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 l.bExclusive = T
ee990 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 RUE;. bRet
ee9a0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
ee9b0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
ee9c0 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 a read-only loc
ee9d0 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 k? */. else if
ee9e0 28 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f ((dwFileOffsetLo
ee9f0 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53 w >= SHARED_FIRS
eea00 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 T &&.
eea10 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 dwFileOffsetLow
eea20 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 < SHARED_FIRST
eea30 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26 + SHARED_SIZE) &
eea40 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e &. nN
eea50 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
eea60 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 ckLow == 1){.
eea70 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 if (pFile->shar
eea80 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d ed->bExclusive =
eea90 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c = 0){. pFil
eeaa0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
eeab0 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 s ++;. if (
eeac0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
eead0 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 aders == 1){.
eeae0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
eeaf0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b ed->nReaders ++;
eeb00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
eeb10 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
eeb20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 }. }.. /* W
eeb30 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f ant a pending lo
eeb40 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 ck? */. else if
eeb50 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f (dwFileOffsetLo
eeb60 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 w == PENDING_BYT
eeb70 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
eeb80 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 tesToLockLow ==
eeb90 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 1){. /* If no
eeba0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 pending lock ha
eebb0 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c s been acquired,
eebc0 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 then acquire it
eebd0 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c */. if (pFil
eebe0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
eebf0 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 ing == 0) {.
eec00 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
eec10 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 >bPending = TRUE
eec20 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
eec30 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 ocal.bPending =
eec40 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 TRUE;. bRet
eec50 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
eec60 7d 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 }. }. /* Want
eec70 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f a reserved lock?
eec80 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
eec90 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
eeca0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 = RESERVED_BYTE
eecb0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
eecc0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 sToLockLow == 1)
eecd0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d {. if (pFile-
eece0 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 >shared->bReserv
eecf0 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 ed == 0) {.
eed00 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
eed10 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 bReserved = TRUE
eed20 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
eed30 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d ocal.bReserved =
eed40 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 TRUE;. bRe
eed50 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
eed60 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d }. }.. winceM
eed70 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
eed80 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 e->hMutex);. re
eed90 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a turn bReturn;.}.
eeda0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d ./*.** An implem
eedb0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
eedc0 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f UnlockFile API o
eedd0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 f windows for wi
eede0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f nce.*/.static BO
eedf0 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 OL winceUnlockFi
eee00 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 le(. HANDLE *ph
eee10 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 File,. DWORD dw
eee20 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 FileOffsetLow,.
eee30 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 DWORD dwFileOff
eee40 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 setHigh,. DWORD
eee50 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
eee60 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 oUnlockLow,. DW
eee70 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
eee80 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 esToUnlockHigh.)
eee90 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 {. winFile *pFi
eeea0 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 le = HANDLE_TO_W
eeeb0 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a INFILE(phFile);.
eeec0 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d BOOL bReturn =
eeed0 20 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 FALSE;.. if (!
eeee0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 pFile->hMutex) r
eeef0 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 eturn TRUE;. wi
eef00 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
eef10 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
eef20 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 . /* Releasing
eef30 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 a reader lock or
eef40 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
eef50 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 ck */. if (dwFi
eef60 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 leOffsetLow >= S
eef70 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 HARED_FIRST &&.
eef80 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73 dwFileOffs
eef90 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 etLow < SHARED_F
eefa0 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49 IRST + SHARED_SI
eefb0 5a 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 ZE){. /* Did
eefc0 77 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 we have an exclu
eefd0 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 sive lock? */.
eefe0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
eeff0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a al.bExclusive){.
ef000 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
ef010 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 al.bExclusive =
ef020 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 FALSE;. pFi
ef030 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 le->shared->bExc
ef040 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a lusive = FALSE;.
ef050 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
ef060 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 TRUE;. }..
ef070 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20 /* Did we just
ef080 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f have a reader lo
ef090 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 ck? */. else
ef0a0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
ef0b0 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 .nReaders){.
ef0c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e pFile->local.n
ef0d0 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 Readers --;.
ef0e0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ef0f0 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 al.nReaders == 0
ef100 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 ). {.
ef110 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ef120 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 >nReaders --;.
ef130 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
ef140 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
ef150 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 }. }.. /* Rele
ef160 61 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 asing a pending
ef170 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 lock */. else i
ef180 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c f (dwFileOffsetL
ef190 6f 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 ow == PENDING_BY
ef1a0 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 TE && nNumberOfB
ef1b0 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 ytesToUnlockLow
ef1c0 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
ef1d0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e File->local.bPen
ef1e0 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 ding){. pFi
ef1f0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 le->local.bPendi
ef200 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ng = FALSE;.
ef210 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ef220 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 >bPending = FALS
ef230 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
ef240 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
ef250 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e }. /* Releasin
ef260 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 g a reserved loc
ef270 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
ef280 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
ef290 3d 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 == RESERVED_BYTE
ef2a0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 && nNumberOfByt
ef2b0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d esToUnlockLow ==
ef2c0 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 1){. if (pFi
ef2d0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
ef2e0 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69 ved) {. pFi
ef2f0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
ef300 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ved = FALSE;.
ef310 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
ef320 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 ->bReserved = FA
ef330 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 LSE;. bRetu
ef340 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
ef350 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 . }.. winceMut
ef360 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d exRelease(pFile-
ef370 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 >hMutex);. retu
ef380 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f rn bReturn;.}../
ef390 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e *.** An implemen
ef3a0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f tation of the Lo
ef3b0 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f ckFileEx() API o
ef3c0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 f windows for wi
ef3d0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f nce.*/.static BO
ef3e0 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 OL winceLockFile
ef3f0 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 Ex(. HANDLE *ph
ef400 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 File,. DWORD dw
ef410 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 Flags,. DWORD d
ef420 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f wReserved,. DWO
ef430 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
ef440 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 sToLockLow,. DW
ef450 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
ef460 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 esToLockHigh,.
ef470 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f LPOVERLAPPED lpO
ef480 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f verlapped.){. /
ef490 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
ef4a0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 wants a shared r
ef4b0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 ead lock, forwar
ef4c0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a d this call. **
ef4d0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c to winceLockFil
ef4e0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 e */. if (lpOve
ef4f0 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 rlapped->Offset
ef500 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 == SHARED_FIRST
ef510 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73 &&. dwFlags
ef520 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e == 1 &&. n
ef530 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
ef540 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 ockLow == SHARED
ef550 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 _SIZE){. retu
ef560 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 rn winceLockFile
ef570 28 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f (phFile, SHARED_
ef580 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b FIRST, 0, 1, 0);
ef590 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 . }. return FA
ef5a0 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 LSE;.}./*.** End
ef5b0 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 of the special
ef5c0 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a code for wince.*
ef5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 ************/.#e
ef620 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
ef630 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a S_WINCE */../***
ef640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
ef690 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 e next group of
ef6a0 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 routines impleme
ef6b0 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f nt the I/O metho
ef6c0 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 ds specified.**
ef6d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 by the sqlite3_i
ef6e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
ef6f0 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
ef700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef740 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 /../*.** Close a
ef750 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 file..**.** It
ef760 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 is reported that
ef770 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 an attempt to c
ef780 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 lose a handle mi
ef790 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a ght sometimes.**
ef7a0 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 fail. This is
ef7b0 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 a very unreasona
ef7c0 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 ble result, but
ef7d0 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 windows is notor
ef7e0 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e ious.** for bein
ef7f0 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 g unreasonable s
ef800 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 o I do not doubt
ef810 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 that it might h
ef820 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 appen. If.** th
ef830 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 e close fails, w
ef840 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 e pause for 100
ef850 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 milliseconds and
ef860 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a try again. As.
ef870 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c ** many as MX_CL
ef880 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 OSE_ATTEMPT atte
ef890 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 mpts to close th
ef8a0 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 e handle are mad
ef8b0 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 e before.** givi
ef8c0 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e ng up and return
ef8d0 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ing an error..*/
ef8e0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 .#define MX_CLOS
ef8f0 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 E_ATTEMPT 3.stat
ef900 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 ic int winClose(
ef910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
ef920 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 ){. int rc, cnt
ef930 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 = 0;. winFile
ef940 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
ef950 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 e*)id;.. assert
ef960 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 ( id!=0 );. OST
ef970 52 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c RACE2("CLOSE %d\
ef980 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
ef990 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c do{. rc = Cl
ef9a0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d oseHandle(pFile-
ef9b0 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 >h);. }while( r
ef9c0 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 c==0 && ++cnt <
ef9d0 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 MX_CLOSE_ATTEMPT
ef9e0 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c && (Sleep(100),
ef9f0 20 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 1) );.#if SQLIT
efa00 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 E_OS_WINCE.#defi
efa10 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f ne WINCE_DELETIO
efa20 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 N_ATTEMPTS 3. w
efa30 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 inceDestroyLock(
efa40 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 pFile);. if( pF
efa50 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
efa60 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 ose ){. int c
efa70 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c nt = 0;. whil
efa80 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 e(. De
efa90 6c 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d leteFileW(pFile-
efaa0 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 >zDeleteOnClose)
efab0 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 ==0. && G
efac0 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
efad0 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 W(pFile->zDelete
efae0 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 OnClose)!=0xffff
efaf0 66 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 ffff . &&
efb00 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 cnt++ < WINCE_D
efb10 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
efb20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 . ){. S
efb30 6c 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 leep(100); /* W
efb40 61 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 ait a little bef
efb50 6f 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e ore trying again
efb60 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 */. }. fr
efb70 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 ee(pFile->zDelet
efb80 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 eOnClose);. }.#
efb90 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e endif. OpenCoun
efba0 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 ter(-1);. retur
efbb0 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b n rc ? SQLITE_OK
efbc0 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
efbd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d .}../*.** Some m
efbe0 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 icrosoft compile
efbf0 72 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 rs lack this def
efc00 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e inition..*/.#ifn
efc10 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f def INVALID_SET_
efc20 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 FILE_POINTER.# d
efc30 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 efine INVALID_SE
efc40 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 T_FILE_POINTER (
efc50 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 (DWORD)-1).#endi
efc60 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 f../*.** Read da
efc70 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 ta from a file i
efc80 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 nto a buffer. R
efc90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
efca0 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 if all.** bytes
efcb0 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 were read succes
efcc0 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 sfully and SQLIT
efcd0 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 E_IOERR if anyth
efce0 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e ing goes.** wron
efcf0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
efd00 20 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 winRead(. sqli
efd10 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
efd20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
efd30 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 o read from */.
efd40 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 void *pBuf,
efd50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
efd60 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 rite content int
efd70 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f o this buffer */
efd80 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
efd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
efda0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
efdb0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 to read */. sq
efdc0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
efdd0 65 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 et /* Begi
efde0 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 n reading at thi
efdf0 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 s offset */.){.
efe00 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 LONG upperBits
efe10 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 = (LONG)((offset
efe20 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 >>32) & 0x7fffff
efe30 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 ff);. LONG lowe
efe40 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f rBits = (LONG)(o
efe50 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 ffset & 0xffffff
efe60 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b ff);. DWORD rc;
efe70 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
efe80 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
efe90 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b ;. DWORD error;
efea0 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 . DWORD got;..
efeb0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
efec0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
efed0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
efee0 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 E_IOERR_READ);.
efef0 20 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20 OSTRACE3("READ
eff00 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 %d lock=%d\n", p
eff10 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
eff20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 locktype);. rc
eff30 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
eff40 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
eff50 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
eff60 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
eff70 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 if( rc==INVALID
eff80 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
eff90 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c R && (error=GetL
effa0 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f astError())!=NO_
effb0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 ERROR ){. pFi
effc0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
effd0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 error;. retur
effe0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
efff0 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 }. if( !ReadFi
f0000 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 le(pFile->h, pBu
f0010 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 f, amt, &got, 0)
f0020 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
f0030 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
f0040 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 stError();. r
f0050 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
f0060 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 RR_READ;. }. i
f0070 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 f( got==(DWORD)a
f0080 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
f0090 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
f00a0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 lse{. /* Unre
f00b0 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 ad parts of the
f00c0 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a buffer must be z
f00d0 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 ero-filled */.
f00e0 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 memset(&((char
f00f0 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c *)pBuf)[got], 0,
f0100 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 amt-got);. r
f0110 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
f0120 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 RR_SHORT_READ;.
f0130 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 }.}../*.** Writ
f0140 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 e data from a bu
f0150 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 ffer into a file
f0160 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
f0170 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a _OK on success.*
f0180 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 * or some other
f0190 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 error code on fa
f01a0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ilure..*/.static
f01b0 20 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 int winWrite(.
f01c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
f01d0 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 d, /* Fi
f01e0 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f le to write into
f01f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
f0200 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
f0210 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 /* The bytes to
f0220 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 be written */.
f0230 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
f0240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
f0250 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
f0260 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 write */. sqlit
f0270 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 e3_int64 offset
f0280 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
f0290 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 nto the file to
f02a0 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 begin writing at
f02b0 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 */.){. LONG up
f02c0 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 perBits = (LONG)
f02d0 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 ((offset>>32) &
f02e0 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 0x7fffffff);. L
f02f0 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 ONG lowerBits =
f0300 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 (LONG)(offset &
f0310 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 0xffffffff);. D
f0320 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 WORD rc;. winFi
f0330 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
f0340 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
f0350 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 D error;. DWORD
f0360 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 wrote = 0;.. a
f0370 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
f0380 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
f0390 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
f03a0 49 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 IOERR_WRITE);.
f03b0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
f03c0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
f03d0 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 ITE_FULL);. OST
f03e0 52 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20 RACE3("WRITE %d
f03f0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c lock=%d\n", pFil
f0400 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 e->h, pFile->loc
f0410 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 ktype);. rc = S
f0420 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 etFilePointer(pF
f0430 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 ile->h, lowerBit
f0440 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 s, &upperBits, F
f0450 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 ILE_BEGIN);. if
f0460 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 ( rc==INVALID_SE
f0470 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 T_FILE_POINTER &
f0480 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 & (error=GetLast
f0490 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 Error())!=NO_ERR
f04a0 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d OR ){. pFile-
f04b0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
f04c0 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 or;. return S
f04d0 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a QLITE_FULL;. }.
f04e0 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 assert( amt>0
f04f0 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 );. while(.
f0500 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 amt>0. && (
f0510 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 rc = WriteFile(p
f0520 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 File->h, pBuf, a
f0530 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 mt, &wrote, 0))!
f0540 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 =0. && wrote
f0550 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 >0. ){. amt
f0560 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 -= wrote;. pB
f0570 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 uf = &((char*)pB
f0580 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a uf)[wrote];. }.
f0590 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 if( !rc || amt
f05a0 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 >(int)wrote ){.
f05b0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
f05c0 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 rno = GetLastErr
f05d0 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e or();. return
f05e0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
f05f0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
f0600 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
f0610 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
f0620 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
f0630 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
f0640 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 ic int winTrunca
f0650 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
f0660 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 *id, sqlite3_int
f0670 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 64 nByte){. LON
f0680 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c G upperBits = (L
f0690 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 ONG)((nByte>>32)
f06a0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
f06b0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 LONG lowerBits
f06c0 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 = (LONG)(nByte
f06d0 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 & 0xffffffff);.
f06e0 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e DWORD rc;. win
f06f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
f0700 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 inFile*)id;. DW
f0710 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 ORD error;.. as
f0720 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
f0730 20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 OSTRACE3("TRUNC
f0740 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 ATE %d %lld\n",
f0750 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 pFile->h, nByte)
f0760 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
f0770 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
f0780 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
f0790 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c );. rc = SetFil
f07a0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e ePointer(pFile->
f07b0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 h, lowerBits, &u
f07c0 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 pperBits, FILE_B
f07d0 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d EGIN);. if( rc=
f07e0 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c =INVALID_SET_FIL
f07f0 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 E_POINTER && (er
f0800 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 ror=GetLastError
f0810 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b ())!=NO_ERROR ){
f0820 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
f0830 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 Errno = error;.
f0840 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f0850 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
f0860 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 . }. /* SetEnd
f0870 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c OfFile will fail
f0880 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 if nByte is neg
f0890 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 ative */. if( !
f08a0 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 SetEndOfFile(pFi
f08b0 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 le->h) ){. pF
f08c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
f08d0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
f08e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f08f0 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
f0900 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 E;. }. return
f0910 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 SQLITE_OK;.}..#i
f0920 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
f0930 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 ./*.** Count the
f0940 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 number of fulls
f0950 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 yncs and normal
f0960 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 syncs. This is
f0970 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 used to test.**
f0980 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 that syncs and f
f0990 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 ullsyncs are occ
f09a0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 uring at the rig
f09b0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c ht times..*/.SQL
f09c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f09d0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d te3_sync_count =
f09e0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
f09f0 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 nt sqlite3_fulls
f0a00 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ync_count = 0;.#
f0a10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b endif../*.** Mak
f0a20 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 e sure all write
f0a30 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 s to a particula
f0a40 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 r file are commi
f0a50 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f tted to disk..*/
f0a60 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 .static int winS
f0a70 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
f0a80 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 *id, int flags)
f0a90 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
f0aa0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 _NO_SYNC. winFi
f0ab0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
f0ac0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 File*)id;.. ass
f0ad0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
f0ae0 4f 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 OSTRACE3("SYNC %
f0af0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
f0b00 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
f0b10 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a ocktype);.#else.
f0b20 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f0b30 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 ER(id);.#endif.#
f0b40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 ifndef SQLITE_TE
f0b50 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ST. UNUSED_PARA
f0b60 4d 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 METER(flags);.#e
f0b70 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 lse. if( flags
f0b80 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 & SQLITE_SYNC_FU
f0b90 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 LL ){. sqlite
f0ba0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
f0bb0 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ++;. }. sqlite
f0bc0 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 3_sync_count++;.
f0bd0 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 #endif. /* If w
f0be0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 e compiled with
f0bf0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 the SQLITE_NO_SY
f0c00 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 NC flag, then sy
f0c10 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 ncing is a. **
f0c20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 no-op. */.#ifde
f0c30 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 f SQLITE_NO_SYNC
f0c40 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f0c50 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 TE_OK;.#else. i
f0c60 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 f( FlushFileBuff
f0c70 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b ers(pFile->h) ){
f0c80 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f0c90 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
f0ca0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
f0cb0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
f0cc0 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 ror();. retur
f0cd0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
f0ce0 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a }.#endif.}../*
f0cf0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
f0d00 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
f0d10 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
f0d20 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
f0d30 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 winFileSize(sqli
f0d40 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 te3_file *id, sq
f0d50 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
f0d60 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 ze){. DWORD upp
f0d70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 erBits;. DWORD
f0d80 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e lowerBits;. win
f0d90 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
f0da0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 inFile*)id;. DW
f0db0 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 ORD error;.. as
f0dc0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
f0dd0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
f0de0 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
f0df0 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c OERR_FSTAT);. l
f0e00 6f 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 owerBits = GetFi
f0e10 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c leSize(pFile->h,
f0e20 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 &upperBits);.
f0e30 69 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 if( (lowerBits
f0e40 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 == INVALID_FILE
f0e50 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 _SIZE). && (
f0e60 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 (error = GetLast
f0e70 45 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 Error()) != NO_E
f0e80 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 RROR) ). {.
f0e90 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
f0ea0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 = error;. re
f0eb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
f0ec0 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a R_FSTAT;. }. *
f0ed0 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 pSize = (((sqlit
f0ee0 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 e3_int64)upperBi
f0ef0 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 ts)<<32) + lower
f0f00 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 Bits;. return S
f0f10 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
f0f20 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c ** LOCKFILE_FAIL
f0f30 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 _IMMEDIATELY is
f0f40 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d undefined on som
f0f50 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d e Windows system
f0f60 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f s..*/.#ifndef LO
f0f70 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 CKFILE_FAIL_IMME
f0f80 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 DIATELY.# define
f0f90 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
f0fa0 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e MMEDIATELY 1.#en
f0fb0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 dif../*.** Acqui
f0fc0 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
f0fd0 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 ..** Different A
f0fe0 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 PI routines are
f0ff0 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 called depending
f1000 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
f1010 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 ot this.** is Wi
f1020 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f n95 or WinNT..*/
f1030 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 .static int getR
f1040 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 eadLock(winFile
f1050 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 *pFile){. int r
f1060 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 es;. if( isNT()
f1070 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 ){. OVERLAPP
f1080 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c ED ovlp;. ovl
f1090 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 p.Offset = SHARE
f10a0 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c D_FIRST;. ovl
f10b0 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 p.OffsetHigh = 0
f10c0 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e ;. ovlp.hEven
f10d0 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d t = 0;. res =
f10e0 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c LockFileEx(pFil
f10f0 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 e->h, LOCKFILE_F
f1100 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c AIL_IMMEDIATELY,
f1110 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f1120 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 0, SHARED_
f1130 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b SIZE, 0, &ovlp);
f1140 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
f1150 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f1160 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
f1170 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
f1180 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 cuted. .*/.#if S
f1190 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
f11a0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 0. }else{. i
f11b0 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 nt lk;. sqlit
f11c0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
f11d0 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a zeof(lk), &lk);.
f11e0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f11f0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f dLockByte = (sho
f1200 72 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 rt)((lk & 0x7fff
f1210 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 ffff)%(SHARED_SI
f1220 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 ZE - 1));. re
f1230 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 s = LockFile(pFi
f1240 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
f1250 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 RST+pFile->share
f1260 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c dLockByte, 0, 1,
f1270 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0);.#endif. }.
f1280 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 if( res == 0 )
f1290 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
f12a0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
f12b0 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 Error();. }. r
f12c0 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
f12d0 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c .** Undo a readl
f12e0 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ock.*/.static in
f12f0 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b t unlockReadLock
f1300 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 (winFile *pFile)
f1310 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 {. int res;. i
f1320 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
f1330 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c res = UnlockFil
f1340 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
f1350 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
f1360 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a RED_SIZE, 0);./*
f1370 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
f1380 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
f1390 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
f13a0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
f13b0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 ed. .*/.#if SQLI
f13c0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
f13d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 }else{. res
f13e0 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 = UnlockFile(pFi
f13f0 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
f1400 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 RST + pFile->sha
f1410 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 redLockByte, 0,
f1420 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 1, 0);.#endif.
f1430 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 }. if( res == 0
f1440 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
f1450 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
f1460 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 stError();. }.
f1470 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
f1480 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 /*.** Lock the f
f1490 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 ile with the loc
f14a0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 k specified by p
f14b0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 arameter locktyp
f14c0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 e - one.** of th
f14d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
f14e0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 ** (1) SHARE
f14f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 D_LOCK.** (2
f1500 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ) RESERVED_LOCK.
f1510 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 ** (3) PENDI
f1520 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 NG_LOCK.** (
f1530 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4) EXCLUSIVE_LOC
f1540 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 K.**.** Sometime
f1550 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e s when requestin
f1560 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 g one lock state
f1570 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 , additional loc
f1580 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 k states.** are
f1590 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 inserted in betw
f15a0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e een. The lockin
f15b0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 g might fail on
f15c0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 one of the later
f15d0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
f15e0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b leaving the lock
f15f0 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 state different
f1600 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 from what it st
f1610 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 arted but.** sti
f1620 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 ll short of its
f1630 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f goal. The follo
f1640 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 wing chart shows
f1650 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 the allowed.**
f1660 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 transitions and
f1670 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 the inserted int
f1680 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 ermediate states
f1690 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 :.**.** UNLOC
f16a0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a KED -> SHARED.**
f16b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 SHARED -> RE
f16c0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 SERVED.** SHA
f16d0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 RED -> (PENDING)
f16e0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
f16f0 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 RESERVED ->
f1700 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
f1710 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e LUSIVE.** PEN
f1720 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 DING -> EXCLUSIV
f1730 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 E.**.** This rou
f1740 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
f1750 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
f1760 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 The winUnlock()
f1770 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 routine.** eras
f1780 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 es all locks at
f1790 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 once and returns
f17a0 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 us immediately
f17b0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c to locking level
f17c0 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 0..** It is not
f17d0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 possible to low
f17e0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
f17f0 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 evel one step at
f1800 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a a time. You.**
f1810 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 must go straigh
f1820 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 t to locking lev
f1830 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 el 0..*/.static
f1840 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 int winLock(sqli
f1850 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
f1860 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
f1870 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
f1880 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 K; /* Return
f1890 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 code from subrou
f18a0 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 tines */. int r
f18b0 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 es = 1;
f18c0 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 /* Result of a
f18d0 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 windows lock ca
f18e0 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c ll */. int newL
f18f0 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f ocktype; /
f1900 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 * Set pFile->loc
f1910 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 ktype to this va
f1920 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 lue before exiti
f1930 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 ng */. int gotP
f1940 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f endingLock = 0;/
f1950 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71 * True if we acq
f1960 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 uired a PENDING
f1970 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a lock this time *
f1980 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 /. winFile *pFi
f1990 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
f19a0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 d;. DWORD error
f19b0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 = NO_ERROR;..
f19c0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
f19d0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 . OSTRACE5("LOC
f19e0 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 K %d %d was %d(%
f19f0 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 d)\n",.
f1a00 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
f1a10 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ype, pFile->lock
f1a20 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 type, pFile->sha
f1a30 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 redLockByte);..
f1a40 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
f1a50 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
f1a60 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
f1a70 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
f1a80 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 on the. ** OsFi
f1a90 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 le, do nothing.
f1aa0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e Don't use the en
f1ab0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 d_lock: exit pat
f1ac0 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 h, as. ** sqlit
f1ad0 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 e3OsEnterMutex()
f1ae0 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c hasn't been cal
f1af0 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 led yet.. */.
f1b00 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
f1b10 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype>=locktype ){
f1b20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f1b30 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
f1b40 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c Make sure the l
f1b50 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 ocking sequence
f1b60 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a is correct. */.
f1b70 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
f1b80 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f >locktype!=NO_LO
f1b90 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d CK || locktype==
f1ba0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
f1bb0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
f1bc0 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 e!=PENDING_LOCK
f1bd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
f1be0 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f ktype!=RESERVED_
f1bf0 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c LOCK || pFile->l
f1c00 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
f1c10 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f LOCK );.. /* Lo
f1c20 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c ck the PENDING_L
f1c30 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e OCK byte if we n
f1c40 65 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 eed to acquire a
f1c50 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 PENDING lock or
f1c60 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c . ** a SHARED l
f1c70 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 ock. If we are
f1c80 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 acquiring a SHAR
f1c90 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 ED lock, the acq
f1ca0 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a uisition of. **
f1cb0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
f1cc0 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 K byte is tempor
f1cd0 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c ary.. */. newL
f1ce0 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d ocktype = pFile-
f1cf0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 >locktype;. if(
f1d00 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 (pFile->lockt
f1d10 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 ype==NO_LOCK).
f1d20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 || ( (lockt
f1d30 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
f1d40 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 OCK). &&
f1d50 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
f1d60 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e==RESERVED_LOCK
f1d70 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 )). ){. int
f1d80 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 cnt = 3;. whi
f1d90 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 le( cnt-->0 && (
f1da0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
f1db0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
f1dc0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 _BYTE, 0, 1, 0))
f1dd0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
f1de0 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 Try 3 times to g
f1df0 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c et the pending l
f1e00 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e ock. The pendin
f1e10 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a g lock might be.
f1e20 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 ** held by
f1e30 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 another reader
f1e40 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c process who will
f1e50 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 release it mome
f1e60 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a ntarily.. *
f1e70 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 /. OSTRACE2
f1e80 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 ("could not get
f1e90 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 a PENDING lock.
f1ea0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b cnt=%d\n", cnt);
f1eb0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
f1ec0 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 . }. gotPe
f1ed0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b ndingLock = res;
f1ee0 0a 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b . if( !res ){
f1ef0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
f1f00 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f1f10 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
f1f20 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20 cquire a shared
f1f30 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
f1f40 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
f1f50 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
f1f60 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
f1f70 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f e->locktype==NO_
f1f80 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
f1f90 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 = getReadLock(pF
f1fa0 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 ile);. if( re
f1fb0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f s ){. newLo
f1fc0 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f cktype = SHARED_
f1fd0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
f1fe0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
f1ff0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f2000 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
f2010 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 cquire a RESERVE
f2020 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 D lock. */. if
f2030 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ( locktype==RESE
f2040 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 RVED_LOCK && res
f2050 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
f2060 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
f2070 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
f2080 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 res = LockFi
f2090 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 le(pFile->h, RES
f20a0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 ERVED_BYTE, 0, 1
f20b0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 , 0);. if( re
f20c0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f s ){. newLo
f20d0 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 cktype = RESERVE
f20e0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 D_LOCK;. }els
f20f0 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d e{. error =
f2100 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
f2110 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f2120 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 Acquire a PENDI
f2130 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 NG lock. */. i
f2140 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
f2150 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 LUSIVE_LOCK && r
f2160 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 es ){. newLoc
f2170 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f ktype = PENDING_
f2180 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e LOCK;. gotPen
f2190 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 dingLock = 0;.
f21a0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
f21b0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
f21c0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
f21d0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
f21e0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
f21f0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
f2200 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
f2210 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
f2220 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
f2230 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
f2240 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 OSTRACE2("unrea
f2250 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 dlock = %d\n", r
f2260 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c es);. res = L
f2270 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
f2280 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 , SHARED_FIRST,
f2290 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 0, SHARED_SIZE,
f22a0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 0);. if( res
f22b0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
f22c0 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 type = EXCLUSIVE
f22d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
f22e0 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 {. error =
f22f0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
f2300 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 OSTRACE2("
f2310 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c error-code = %d\
f2320 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 n", error);.
f2330 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 getReadLock(pF
f2340 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ile);. }. }.
f2350 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
f2360 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e holding a PENDIN
f2370 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 G lock that ough
f2380 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 t to be released
f2390 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 , then. ** rele
f23a0 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f ase it now.. */
f23b0 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e . if( gotPendin
f23c0 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 gLock && locktyp
f23d0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
f23e0 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 {. UnlockFile
f23f0 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 (pFile->h, PENDI
f2400 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 NG_BYTE, 0, 1, 0
f2410 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 );. }.. /* Upd
f2420 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ate the state of
f2430 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 the lock has he
f2440 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 ld in the file d
f2450 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 escriptor then.
f2460 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 ** return the a
f2470 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c ppropriate resul
f2480 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 t code.. */. i
f2490 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 f( res ){. rc
f24a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f24b0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 }else{. OSTRA
f24c0 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 CE4("LOCK FAILED
f24d0 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 %d trying for %
f24e0 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c d but got %d\n",
f24f0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
f2500 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 locktype,
f2510 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 newLocktype);.
f2520 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
f2530 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 no = error;.
f2540 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
f2550 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c ;. }. pFile->l
f2560 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 ocktype = (u8)ne
f2570 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 wLocktype;. ret
f2580 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
f2590 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
f25a0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
f25b0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
f25c0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
f25d0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
f25e0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
f25f0 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
f2600 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
f2610 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e eld, return.** n
f2620 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 on-zero, otherwi
f2630 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 se zero..*/.stat
f2640 69 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 ic int winCheckR
f2650 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
f2660 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
f2670 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 t *pResOut){. i
f2680 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 nt rc;. winFile
f2690 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
f26a0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
f26b0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 t( id!=0 );. if
f26c0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
f26d0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
f26e0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a ){. rc = 1;.
f26f0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 OSTRACE3("TE
f2700 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
f2710 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 (local)\n", pFi
f2720 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 le->h, rc);. }e
f2730 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f lse{. rc = Lo
f2740 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
f2750 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
f2760 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 0, 1, 0);. if
f2770 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e ( rc ){. Un
f2780 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
f2790 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 h, RESERVED_BYTE
f27a0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 , 0, 1, 0);.
f27b0 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a }. rc = !rc;.
f27c0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 OSTRACE3("TE
f27d0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
f27e0 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 (remote)\n", pF
f27f0 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d ile->h, rc);. }
f2800 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 . *pResOut = rc
f2810 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
f2820 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c E_OK;.}../*.** L
f2830 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
f2840 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
f2850 65 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 escriptor id to
f2860 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
f2870 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
f2880 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
f2890 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
f28a0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
f28b0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
f28c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
f28d0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
f28e0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
f28f0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
f2900 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
f2910 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
f2920 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 **.** It is not
f2930 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 possible for thi
f2940 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 s routine to fai
f2950 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 l if the second
f2960 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e argument.** is N
f2970 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 O_LOCK. If the
f2980 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
f2990 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 is SHARED_LOCK t
f29a0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
f29b0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e .** might return
f29c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a SQLITE_IOERR;.*
f29d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
f29e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
f29f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
f2a00 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 ktype){. int ty
f2a10 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 pe;. winFile *p
f2a20 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
f2a30 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
f2a40 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
f2a50 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
f2a60 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
f2a70 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
f2a80 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
f2a90 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 UNLOCK %d to %d
f2aa0 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 was %d(%d)\n", p
f2ab0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
f2ac0 65 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 e,. pFi
f2ad0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 le->locktype, pF
f2ae0 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 ile->sharedLockB
f2af0 79 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 yte);. type = p
f2b00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a File->locktype;.
f2b10 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c if( type>=EXCL
f2b20 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
f2b30 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 UnlockFile(pFi
f2b40 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 le->h, SHARED_FI
f2b50 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 RST, 0, SHARED_S
f2b60 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 IZE, 0);. if(
f2b70 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
f2b80 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 D_LOCK && !getRe
f2b90 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b adLock(pFile) ){
f2ba0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 . /* This s
f2bb0 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 hould never happ
f2bc0 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 en. We should a
f2bd0 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f lways be able to
f2be0 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 . ** reacqu
f2bf0 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ire the read loc
f2c00 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 k */. rc =
f2c10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
f2c20 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 OCK;. }. }.
f2c30 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 if( type>=RESER
f2c40 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 VED_LOCK ){.
f2c50 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
f2c60 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ->h, RESERVED_BY
f2c70 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
f2c80 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 }. if( locktype
f2c90 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 ==NO_LOCK && typ
f2ca0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e>=SHARED_LOCK )
f2cb0 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 {. unlockRead
f2cc0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d Lock(pFile);. }
f2cd0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e . if( type>=PEN
f2ce0 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 DING_LOCK ){.
f2cf0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
f2d00 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 e->h, PENDING_BY
f2d10 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
f2d20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 }. pFile->lockt
f2d30 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 ype = (u8)lockty
f2d40 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b pe;. return rc;
f2d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f .}../*.** Contro
f2d60 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 l and query of t
f2d70 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e he open file han
f2d80 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 dle..*/.static i
f2d90 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f nt winFileContro
f2da0 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
f2db0 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
f2dc0 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 *pArg){. switc
f2dd0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
f2de0 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c e SQLITE_FCNTL_L
f2df0 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 OCKSTATE: {.
f2e00 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 *(int*)pArg =
f2e10 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e ((winFile*)id)->
f2e20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 locktype;.
f2e30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f2e40 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
f2e50 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 SQLITE_LAST_ERR
f2e60 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e NO: {. *(in
f2e70 74 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 t*)pArg = (int)(
f2e80 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (winFile*)id)->l
f2e90 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 astErrno;.
f2ea0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f2eb0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
f2ec0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
f2ed0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 R;.}../*.** Retu
f2ee0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 rn the sector si
f2ef0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
f2f00 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c he underlying bl
f2f10 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a ock device for.*
f2f20 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 * the specified
f2f30 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c file. This is al
f2f40 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 most always 512
f2f50 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 bytes, but may b
f2f60 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 e.** larger for
f2f70 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a some devices..**
f2f80 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 .** SQLite code
f2f90 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e assumes this fun
f2fa0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 ction cannot fai
f2fb0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d l. It also assum
f2fc0 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 es that.** if tw
f2fd0 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 o files are crea
f2fe0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
f2ff0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 file-system dire
f3000 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 ctory (i.e..** a
f3010 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 database and it
f3020 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 s journal file)
f3030 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 that the sector
f3040 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 size will be the
f3050 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 .** same for bot
f3060 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h..*/.static int
f3070 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 winSectorSize(s
f3080 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
f3090 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d {. assert( id!=
f30a0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 0 );. return (i
f30b0 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 nt)(((winFile*)i
f30c0 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b d)->sectorSize);
f30d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
f30e0 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 a vector of dev
f30f0 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 ice characterist
f3100 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ics..*/.static i
f3110 6e 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 nt winDeviceChar
f3120 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 acteristics(sqli
f3130 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
f3140 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f3150 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 R(id);. return
f3160 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 0;.}../*.** This
f3170 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 vector defines
f3180 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 all the methods
f3190 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 that can operate
f31a0 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 on an.** sqlite
f31b0 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 3_file for win32
f31c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
f31d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
f31e0 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 hods winIoMethod
f31f0 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 = {. 1,
f3200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3210 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
f3220 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 winClose,. wi
f3230 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 nRead,. winWrit
f3240 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 e,. winTruncate
f3250 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 ,. winSync,. w
f3260 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 inFileSize,. wi
f3270 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f nLock,. winUnlo
f3280 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 ck,. winCheckRe
f3290 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 servedLock,. wi
f32a0 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 nFileControl,.
f32b0 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 winSectorSize,.
f32c0 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 winDeviceCharac
f32d0 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a teristics.};../*
f32e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 **********.** He
f3330 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 re ends the I/O
f3340 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 methods that for
f3350 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f m the sqlite3_io
f3360 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
f3370 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
f3380 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d block of code im
f3390 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 plements the VFS
f33a0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a methods..******
f33b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33f0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 ******/../*.** C
f3400 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 onvert a UTF-8 f
f3410 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 ilename into wha
f3420 74 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 tever form the u
f3430 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 nderlying.** ope
f3440 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 rating system wa
f3450 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e nts filenames in
f3460 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 . Space to hold
f3470 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
f3480 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
f3490 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 malloc and must
f34a0 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 be freed by the
f34b0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 calling.** funct
f34c0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
f34d0 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 oid *convertUtf8
f34e0 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 Filename(const c
f34f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
f3500 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
f3510 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 ted = 0;. if( i
f3520 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f sNT() ){. zCo
f3530 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f nverted = utf8To
f3540 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d Unicode(zFilenam
f3550 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 e);./* isNT() is
f3560 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
f3570 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
f3580 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
f3590 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 executed. .*/.#i
f35a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f35b0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
f35c0 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 zConverted = u
f35d0 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e tf8ToMbcs(zFilen
f35e0 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d ame);.#endif. }
f35f0 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c . /* caller wil
f3600 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 l handle out of
f3610 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 memory */. retu
f3620 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d rn zConverted;.}
f3630 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
f3640 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
f3650 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
f3660 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 Buf must be big
f3670 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c enough to.** hol
f3680 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 d at pVfs->mxPat
f3690 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 hname characters
f36a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f36b0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
f36c0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
f36d0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 ){. static char
f36e0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
f36f0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
f3700 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
f3710 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
f3720 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
f3730 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
f3740 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 size_t i, j;.
f3750 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b char zTempPath[
f3760 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 MAX_PATH+1];. i
f3770 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f f( sqlite3_temp_
f3780 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 directory ){.
f3790 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
f37a0 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a f(MAX_PATH-30, z
f37b0 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 TempPath, "%s",
f37c0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
f37d0 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 ectory);. }else
f37e0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
f37f0 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b char *zMulti;
f3800 0a 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 . WCHAR zWide
f3810 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a Path[MAX_PATH];.
f3820 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 GetTempPathW
f3830 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 (MAX_PATH-30, zW
f3840 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d idePath);. zM
f3850 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f ulti = unicodeTo
f3860 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b Utf8(zWidePath);
f3870 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 . if( zMulti
f3880 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
f3890 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 _snprintf(MAX_PA
f38a0 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 TH-30, zTempPath
f38b0 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b , "%s", zMulti);
f38c0 0a 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c . free(zMul
f38d0 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ti);. }else{.
f38e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f38f0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
f3900 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
f3910 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f3920 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
f3930 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
f3940 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
f3950 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
f3960 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
f3970 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
f3980 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
f3990 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
f39a0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
f39b0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
f39c0 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
f39d0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f39e0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
f39f0 20 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 char *zUtf8;.
f3a00 20 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 char zMbcsPath
f3a10 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 [MAX_PATH];.
f3a20 47 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 GetTempPathA(MAX
f3a30 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 _PATH-30, zMbcsP
f3a40 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 ath);. zUtf8
f3a50 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f = sqlite3_win32_
f3a60 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 mbcs_to_utf8(zMb
f3a70 63 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 csPath);. if(
f3a80 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 zUtf8 ){.
f3a90 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
f3aa0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
f3ab0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a empPath, "%s", z
f3ac0 55 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 Utf8);. fre
f3ad0 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 e(zUtf8);. }e
f3ae0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
f3af0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f3b00 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
f3b10 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 . for(i=sqlite3
f3b20 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 Strlen30(zTempPa
f3b30 74 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d th); i>0 && zTem
f3b40 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 pPath[i-1]=='\\'
f3b50 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 ; i--){}. zTemp
f3b60 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 Path[i] = 0;. s
f3b70 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
f3b80 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 nBuf-30, zBuf,.
f3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3ba0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
f3bb0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
f3bc0 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a zTempPath);. j
f3bd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
f3be0 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 30(zBuf);. sqli
f3bf0 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 te3_randomness(2
f3c00 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 0, &zBuf[j]);.
f3c10 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 for(i=0; i<20; i
f3c20 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 ++, j++){. zB
f3c30 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 uf[j] = (char)zC
f3c40 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 hars[ ((unsigned
f3c50 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 char)zBuf[j])%(
f3c60 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 sizeof(zChars)-1
f3c70 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b ) ];. }. zBuf[
f3c80 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 j] = 0;. OSTRAC
f3c90 45 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d E2("TEMP FILENAM
f3ca0 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b E: %s\n", zBuf);
f3cb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f3cc0 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 _OK; .}../*.** T
f3cd0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
f3ce0 6f 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d of getLastErrorM
f3cf0 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 sg.** is zero if
f3d00 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
f3d10 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 ge fits in the b
f3d20 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 uffer, or non-ze
f3d30 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 ro.** otherwise
f3d40 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 (if the message
f3d50 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a was truncated)..
f3d60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
f3d70 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e tLastErrorMsg(in
f3d80 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
f3d90 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 65 72 72 uf){. DWORD err
f3da0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
f3db0 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 r();..#if SQLITE
f3dc0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69 _OS_WINCE. sqli
f3dd0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
f3de0 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f f, zBuf, "OsErro
f3df0 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 r 0x%x (%u)", er
f3e00 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c ror, error);.#el
f3e10 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 se. /* FormatMe
f3e20 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 ssage returns 0
f3e30 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 on failure. Oth
f3e40 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 erwise it. ** r
f3e50 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
f3e60 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 r of TCHARs writ
f3e70 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ten to the outpu
f3e80 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 t. ** buffer, e
f3e90 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 xcluding the ter
f3ea0 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 minating null ch
f3eb0 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21 ar.. */. if (!
f3ec0 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46 FormatMessageA(F
f3ed0 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 ORMAT_MESSAGE_FR
f3ee0 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20 20 20 OM_SYSTEM,.
f3ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3f00 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 NULL,.
f3f10 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 err
f3f20 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 or,.
f3f30 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
f3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3f50 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 zBuf,.
f3f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
f3f70 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 Buf-1,.
f3f80 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 29 0))
f3f90 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 . {. sqlite3
f3fa0 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 _snprintf(nBuf,
f3fb0 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 zBuf, "OsError 0
f3fc0 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 x%x (%u)", error
f3fd0 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 , error);. }.#e
f3fe0 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 30 ndif.. return 0
f3ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
f4000 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
f4010 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 c int winOpen(.
f4020 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
f4030 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f fs, /* No
f4040 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 t used */. cons
f4050 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
f4060 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
f4070 20 74 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 the file (UTF-8
f4080 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) */. sqlite3_f
f4090 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 ile *id,
f40a0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 /* Write the SQ
f40b0 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 Lite file handle
f40c0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
f40d0 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
f40e0 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 /* Open mod
f40f0 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 e flags */. int
f4100 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
f4110 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 /* Status
f4120 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f return flags */
f4130 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a .){. HANDLE h;.
f4140 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 DWORD dwDesire
f4150 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 dAccess;. DWORD
f4160 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 dwShareMode;.
f4170 44 57 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e DWORD dwCreation
f4180 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 Disposition;. D
f4190 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 WORD dwFlagsAndA
f41a0 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 ttributes = 0;.#
f41b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f41c0 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 CE. int isTemp
f41d0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 = 0;.#endif. wi
f41e0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
f41f0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 winFile*)id;. v
f4200 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b oid *zConverted;
f4210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4220 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 /* Filename in
f4230 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 OS encoding */.
f4240 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 const char *zUt
f4250 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 f8Name = zName;
f4260 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 /* Filename i
f4270 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 n UTF-8 encoding
f4280 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e */. char zTmpn
f4290 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b ame[MAX_PATH+1];
f42a0 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 /* Buffe
f42b0 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 r used to create
f42c0 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a temp filename *
f42d0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 /.. assert( id!
f42e0 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =0 );. UNUSED_P
f42f0 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
f4300 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 . /* If the sec
f4310 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
f4320 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
f4330 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 NULL, generate
f4340 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 a . ** temporar
f4350 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 y file name to u
f4360 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 se . */. if( !
f4370 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 zUtf8Name ){.
f4380 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d int rc = getTem
f4390 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31 pname(MAX_PATH+1
f43a0 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 , zTmpname);.
f43b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
f43c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
f43d0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
f43e0 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d zUtf8Name = zTm
f43f0 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pname;. }.. /*
f4400 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c Convert the fil
f4410 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 ename to the sys
f4420 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f tem encoding. */
f4430 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 . zConverted =
f4440 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
f4450 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a ame(zUtf8Name);.
f4460 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 if( zConverted
f4470 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f4480 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f4490 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
f44a0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
f44b0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
f44c0 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 dwDesiredAccess
f44d0 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c = GENERIC_READ |
f44e0 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a GENERIC_WRITE;.
f44f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44 }else{. dwD
f4500 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 esiredAccess = G
f4510 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d ENERIC_READ;. }
f4520 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 50 45 . /* SQLITE_OPE
f4530 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 73 20 75 N_EXCLUSIVE is u
f4540 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 sed to make sure
f4550 20 74 68 61 74 20 61 20 6e 65 77 20 66 69 6c 65 that a new file
f4560 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 61 74 65 is . ** create
f4570 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 73 6e 27 d. SQLite doesn'
f4580 74 20 75 73 65 20 69 74 20 74 6f 20 69 6e 64 69 t use it to indi
f4590 63 61 74 65 20 22 65 78 63 6c 75 73 69 76 65 20 cate "exclusive
f45a0 61 63 63 65 73 73 22 20 0a 20 20 2a 2a 20 61 73 access" . ** as
f45b0 20 69 74 20 69 73 20 75 73 75 61 6c 6c 79 20 75 it is usually u
f45c0 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a nderstood.. */.
f45d0 20 20 61 73 73 65 72 74 28 21 28 66 6c 61 67 73 assert(!(flags
f45e0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 & SQLITE_OPEN_E
f45f0 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 28 66 6c XCLUSIVE) || (fl
f4600 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
f4610 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 20 69 66 N_CREATE));. if
f4620 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
f4630 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 _OPEN_EXCLUSIVE
f4640 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 ){. /* Create
f4650 73 20 61 20 6e 65 77 20 66 69 6c 65 2c 20 6f 6e s a new file, on
f4660 6c 79 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f ly if it does no
f4670 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e t already exist.
f4680 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 */. /* If th
f4690 65 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 69 e file exists, i
f46a0 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 t fails. */.
f46b0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
f46c0 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 45 5f 4e ition = CREATE_N
f46d0 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 EW;. }else if(
f46e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
f46f0 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 PEN_CREATE ){.
f4700 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 73 74 69 /* Open existi
f4710 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 72 65 61 ng file, or crea
f4720 74 65 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 te if it doesn't
f4730 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 20 64 77 exist */. dw
f4740 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
f4750 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 ion = OPEN_ALWAY
f4760 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 S;. }else{.
f4770 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 6c 65 2c /* Opens a file,
f4780 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 78 69 73 only if it exis
f4790 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 ts. */. dwCre
f47a0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
f47b0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 = OPEN_EXISTING
f47c0 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 72 65 4d ;. }. dwShareM
f47d0 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 ode = FILE_SHARE
f47e0 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 _READ | FILE_SHA
f47f0 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 66 28 20 RE_WRITE;. if(
f4800 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
f4810 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
f4820 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f E ){.#if SQLITE_
f4830 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 OS_WINCE. dwF
f4840 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
f4850 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 s = FILE_ATTRIBU
f4860 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 TE_HIDDEN;. i
f4870 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 sTemp = 1;.#else
f4880 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 . dwFlagsAndA
f4890 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 ttributes = FILE
f48a0 5f 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f _ATTRIBUTE_TEMPO
f48b0 52 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 RARY.
f48c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f48d0 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 | FILE_ATTRI
f48e0 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 BUTE_HIDDEN.
f48f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4900 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c | FIL
f4910 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e E_FLAG_DELETE_ON
f4920 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 _CLOSE;.#endif.
f4930 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c }else{. dwFl
f4940 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
f4950 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 = FILE_ATTRIBUT
f4960 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 E_NORMAL;. }.
f4970 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 /* Reports from
f4980 74 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 the internet are
f4990 20 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 that performanc
f49a0 65 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a e is always. **
f49b0 20 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f better if FILE_
f49c0 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 FLAG_RANDOM_ACCE
f49d0 53 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 SS is used. Tic
f49e0 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 ket #2699. */.#i
f49f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f4a00 45 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 E. dwFlagsAndAt
f4a10 74 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 tributes |= FILE
f4a20 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 _FLAG_RANDOM_ACC
f4a30 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 ESS;.#endif. if
f4a40 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
f4a50 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 h = CreateFileW(
f4a60 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 (WCHAR*)zConvert
f4a70 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 ed,. dwDes
f4a80 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 iredAccess,.
f4a90 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a dwShareMode,.
f4aa0 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 NULL,.
f4ab0 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 dwCreationDi
f4ac0 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 sposition,.
f4ad0 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 dwFlagsAndAttr
f4ae0 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e ibutes,. N
f4af0 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 ULL. );./* is
f4b00 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
f4b10 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
f4b20 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
f4b30 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
f4b40 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
f4b50 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
f4b60 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
f4b70 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
f4b80 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
f4b90 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
f4ba0 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
f4bb0 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
f4bc0 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
f4bd0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
f4be0 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 }else{. h = C
f4bf0 72 65 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 reateFileA((char
f4c00 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 *)zConverted,.
f4c10 20 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 dwDesiredAc
f4c20 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 cess,. dwS
f4c30 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 hareMode,.
f4c40 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 NULL,. dw
f4c50 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
f4c60 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c ion,. dwFl
f4c70 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
f4c80 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 ,. NULL.
f4c90 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a );.#endif. }.
f4ca0 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 if( h==INVALID
f4cb0 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b _HANDLE_VALUE ){
f4cc0 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
f4cd0 72 74 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 rted);. if( f
f4ce0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
f4cf0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a EN_READWRITE ){.
f4d00 20 20 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e return win
f4d10 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 Open(pVfs, zName
f4d20 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20 , id, .
f4d30 20 20 20 20 28 28 66 6c 61 67 73 7c 53 51 4c 49 ((flags|SQLI
f4d40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
f4d50 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 )&~SQLITE_OPEN_R
f4d60 45 41 44 57 52 49 54 45 29 2c 20 70 4f 75 74 46 EADWRITE), pOutF
f4d70 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 lags);. }else
f4d80 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
f4d90 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
f4da0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
f4db0 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 pOutFlags ){.
f4dc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
f4dd0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
f4de0 54 45 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 TE ){. *pOu
f4df0 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f tFlags = SQLITE_
f4e00 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a OPEN_READWRITE;.
f4e10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
f4e20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 *pOutFlags = SQ
f4e30 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
f4e40 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LY;. }. }.
f4e50 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c memset(pFile, 0,
f4e60 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29 sizeof(*pFile))
f4e70 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 ;. pFile->pMeth
f4e80 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f od = &winIoMetho
f4e90 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 d;. pFile->h =
f4ea0 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 h;. pFile->last
f4eb0 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 Errno = NO_ERROR
f4ec0 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f ;. pFile->secto
f4ed0 72 53 69 7a 65 20 3d 20 67 65 74 53 65 63 74 6f rSize = getSecto
f4ee0 72 53 69 7a 65 28 70 56 66 73 2c 20 7a 55 74 66 rSize(pVfs, zUtf
f4ef0 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 51 4c 49 8Name);.#if SQLI
f4f00 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66 TE_OS_WINCE. if
f4f10 28 20 28 66 6c 61 67 73 20 26 20 28 53 51 4c 49 ( (flags & (SQLI
f4f20 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
f4f30 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 E|SQLITE_OPEN_MA
f4f40 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20 20 IN_DB)) ==.
f4f50 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
f4f60 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
f4f70 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 |SQLITE_OPEN_MAI
f4f80 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26 26 20 N_DB). &&
f4f90 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b !winceCreateLock
f4fa0 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a 20 (zName, pFile).
f4fb0 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e ){. CloseHan
f4fc0 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72 65 65 dle(h);. free
f4fd0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
f4fe0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f4ff0 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 CANTOPEN;. }.
f5000 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 if( isTemp ){.
f5010 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 pFile->zDelete
f5020 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 OnClose = zConve
f5030 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 rted;. }else.#e
f5040 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 65 ndif. {. fre
f5050 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
f5060 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 }. OpenCounter
f5070 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 (+1);. return S
f5080 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
f5090 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 ** Delete the na
f50a0 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 med file..**.**
f50b0 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64 6f 77 Note that window
f50c0 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 s does not allow
f50d0 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65 a file to be de
f50e0 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 leted if some ot
f50f0 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68 her.** process h
f5100 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f 6d as it open. Som
f5110 65 74 69 6d 65 73 20 61 20 76 69 72 75 73 20 73 etimes a virus s
f5120 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 69 canner or indexi
f5130 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 ng program.** wi
f5140 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 ll open a journa
f5150 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 61 l file shortly a
f5160 66 74 65 72 20 69 74 20 69 73 20 63 72 65 61 74 fter it is creat
f5170 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 ed in order to d
f5180 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74 o.** whatever it
f5190 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68 does. While th
f51a0 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 is other process
f51b0 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a is holding the.
f51c0 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65 ** file open, we
f51d0 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20 will be unable
f51e0 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54 to delete it. T
f51f0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
f5200 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 is.** problem, w
f5210 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c e delay 100 mill
f5220 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 iseconds and try
f5230 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e to delete again
f5240 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 . Up.** to MX_D
f5250 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 73 ELETION_ATTEMPTs
f5260 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d 70 deletion attemp
f5270 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f 72 ts are run befor
f5280 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 61 e giving.** up a
f5290 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 nd returning an
f52a0 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e error..*/.#defin
f52b0 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 e MX_DELETION_AT
f52c0 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69 63 20 TEMPTS 5.static
f52d0 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 0a 20 int winDelete(.
f52e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
f52f0 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 fs, /*
f5300 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 Not used on win3
f5310 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 2 */. const cha
f5320 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 r *zFilename,
f5330 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 /* Name of fi
f5340 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a le to delete */.
f5350 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 int syncDir
f5360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f5370 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e Not used on win
f5380 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 32 */.){. int c
f5390 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 nt = 0;. DWORD
f53a0 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f rc;. DWORD erro
f53b0 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a r = 0;. void *z
f53c0 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 Converted = conv
f53d0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
f53e0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e zFilename);. UN
f53f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
f5400 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 Vfs);. UNUSED_P
f5410 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72 ARAMETER(syncDir
f5420 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 );. if( zConver
f5430 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ted==0 ){. re
f5440 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
f5450 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 M;. }. Simulat
f5460 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 eIOError(return
f5470 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
f5480 45 54 45 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 ETE);. if( isNT
f5490 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 () ){. do{.
f54a0 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 DeleteFileW(
f54b0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
f54c0 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 }while( ( (
f54d0 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 (rc = GetFileAtt
f54e0 72 69 62 75 74 65 73 57 28 7a 43 6f 6e 76 65 72 ributesW(zConver
f54f0 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 ted)) != INVALID
f5500 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
f5510 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
f5520 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 || ((error = Ge
f5530 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d tLastError()) ==
f5540 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 ERROR_ACCESS_DE
f5550 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 NIED)).
f5560 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 && (++cnt < MX
f5570 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 _DELETION_ATTEMP
f5580 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 TS). &
f5590 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 & (Sleep(100), 1
f55a0 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 ) );./* isNT() i
f55b0 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
f55c0 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
f55d0 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
f55e0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 executed. .** S
f55f0 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 ince the ASCII v
f5600 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 ersion of these
f5610 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e Windows API do n
f5620 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e ot exist for WIN
f5630 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f CE,.** it's impo
f5640 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 rtant to not ref
f5650 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 erence them for
f5660 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f WINCE builds..*/
f5670 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f5680 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
f5690 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 . do{. D
f56a0 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 6f 6e 76 eleteFileA(zConv
f56b0 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 erted);. }whi
f56c0 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20 3d le( ( ((rc =
f56d0 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 GetFileAttribut
f56e0 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 esA(zConverted))
f56f0 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 != INVALID_FILE
f5700 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20 20 _ATTRIBUTES).
f5710 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 || (
f5720 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 (error = GetLast
f5730 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f Error()) == ERRO
f5740 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29 R_ACCESS_DENIED)
f5750 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 ). &&
f5760 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 (++cnt < MX_DELE
f5770 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 TION_ATTEMPTS).
f5780 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53 6c && (Sl
f5790 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a eep(100), 1) );.
f57a0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 #endif. }. fre
f57b0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
f57c0 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c 45 54 OSTRACE2("DELET
f57d0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 E \"%s\"\n", zFi
f57e0 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 lename);. retur
f57f0 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 49 4e 56 n ( (rc == INV
f5800 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f5810 55 54 45 53 29 20 0a 20 20 20 20 20 20 20 20 20 UTES) .
f5820 20 26 26 20 28 65 72 72 6f 72 20 3d 3d 20 45 52 && (error == ER
f5830 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 ROR_FILE_NOT_FOU
f5840 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b ND)) ? SQLITE_OK
f5850 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f : SQLITE_IOERR_
f5860 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a DELETE;.}../*.**
f5870 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 73 74 Check the exist
f5880 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 73 20 ance and status
f5890 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 of a file..*/.st
f58a0 61 74 69 63 20 69 6e 74 20 77 69 6e 41 63 63 65 atic int winAcce
f58b0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 ss(. sqlite3_vf
f58c0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
f58d0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
f58e0 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 win32 */. const
f58f0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f5900 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 , /* Name of
f5910 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a file to check *
f5920 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
f5930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f5940 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 * Type of test t
f5950 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 o make on this f
f5960 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 ile */. int *pR
f5970 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 esOut
f5980 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 73 75 /* OUT: Resu
f5990 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 lt */.){. DWORD
f59a0 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 72 63 20 attr;. int rc
f59b0 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f = 0;. void *zCo
f59c0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 nverted = conver
f59d0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 tUtf8Filename(zF
f59e0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 ilename);. UNUS
f59f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
f5a00 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 s);. if( zConve
f5a10 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 rted==0 ){. r
f5a20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
f5a30 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 EM;. }. if( is
f5a40 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 NT() ){. attr
f5a50 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 = GetFileAttrib
f5a60 75 74 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 utesW((WCHAR*)zC
f5a70 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 onverted);./* is
f5a80 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
f5a90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
f5aa0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
f5ab0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
f5ac0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
f5ad0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
f5ae0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
f5af0 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
f5b00 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
f5b10 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
f5b20 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
f5b30 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
f5b40 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
f5b50 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
f5b60 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20 }else{. attr
f5b70 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 = GetFileAttribu
f5b80 74 65 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e tesA((char*)zCon
f5b90 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a verted);.#endif.
f5ba0 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 }. free(zConv
f5bb0 65 72 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68 erted);. switch
f5bc0 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 ( flags ){. c
f5bd0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
f5be0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 S_READ:. case
f5bf0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
f5c00 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 XISTS:. rc
f5c10 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f = attr!=INVALID_
f5c20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b FILE_ATTRIBUTES;
f5c30 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
f5c40 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
f5c50 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a CESS_READWRITE:.
f5c60 20 20 20 20 20 20 72 63 20 3d 20 28 61 74 74 72 rc = (attr
f5c70 20 26 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 & FILE_ATTRIBUT
f5c80 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a E_READONLY)==0;.
f5c90 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
f5ca0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 default:.
f5cb0 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64 assert(!"Invalid
f5cc0 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 flags argument"
f5cd0 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 );. }. *pResOu
f5ce0 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e t = rc;. return
f5cf0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
f5d00 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c /*.** Turn a rel
f5d10 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 ative pathname i
f5d20 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e nto a full pathn
f5d30 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 ame. Write the
f5d40 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 full.** pathname
f5d50 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a into zOut[]. z
f5d60 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 Out[] will be at
f5d70 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 least pVfs->mxP
f5d80 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 athname.** bytes
f5d90 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
f5da0 74 69 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 tic int winFullP
f5db0 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 athname(. sqlit
f5dc0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
f5dd0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
f5de0 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 ter to vfs objec
f5df0 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 t */. const cha
f5e00 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 r *zRelative,
f5e10 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 /* Possibly
f5e20 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 relative input
f5e30 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 path */. int nF
f5e40 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ull,
f5e50 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
f5e60 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 of output buffer
f5e70 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 in bytes */. c
f5e80 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 har *zFull
f5e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f5ea0 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f Output buffer */
f5eb0 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e .){. .#if defin
f5ec0 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 ed(__CYGWIN__).
f5ed0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f5ee0 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 79 67 77 R(nFull);. cygw
f5ef0 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f in_conv_to_full_
f5f00 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c 61 win32_path(zRela
f5f10 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20 tive, zFull);.
f5f20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f5f30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 ;.#endif..#if SQ
f5f40 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
f5f50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f5f60 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69 (nFull);. /* Wi
f5f70 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65 nCE has no conce
f5f80 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65 pt of a relative
f5f90 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f pathname, or so
f5fa0 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 I am told. */.
f5fb0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
f5fc0 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 f(pVfs->mxPathna
f5fd0 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c me, zFull, "%s",
f5fe0 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72 zRelative);. r
f5ff0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
f6000 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 .#endif..#if !SQ
f6010 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 LITE_OS_WINCE &&
f6020 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 !defined(__CYGW
f6030 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74 IN__). int nByt
f6040 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 e;. void *zConv
f6050 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a erted;. char *z
f6060 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 Out;. UNUSED_PA
f6070 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a RAMETER(nFull);.
f6080 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 zConverted = c
f6090 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
f60a0 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 me(zRelative);.
f60b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
f60c0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 3b WCHAR *zTemp;
f60d0 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 . nByte = Get
f60e0 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 FullPathNameW((W
f60f0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
f6100 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a , 0, 0, 0) + 3;.
f6110 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c zTemp = mall
f6120 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 oc( nByte*sizeof
f6130 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 (zTemp[0]) );.
f6140 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 if( zTemp==0 )
f6150 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f {. free(zCo
f6160 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 nverted);.
f6170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
f6180 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 MEM;. }. G
f6190 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 etFullPathNameW(
f61a0 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 (WCHAR*)zConvert
f61b0 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 ed, nByte, zTemp
f61c0 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a , 0);. free(z
f61d0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
f61e0 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f zOut = unicodeTo
f61f0 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 Utf8(zTemp);.
f6200 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a free(zTemp);./*
f6210 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
f6220 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
f6230 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
f6240 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
f6250 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
f6260 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
f6270 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
f6280 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
f6290 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
f62a0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
f62b0 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
f62c0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
f62d0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
f62e0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
f62f0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 . }else{. ch
f6300 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e ar *zTemp;. n
f6310 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 Byte = GetFullPa
f6320 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a thNameA((char*)z
f6330 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c Converted, 0, 0,
f6340 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 0) + 3;. zTe
f6350 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 mp = malloc( nBy
f6360 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b te*sizeof(zTemp[
f6370 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 0]) );. if( z
f6380 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Temp==0 ){.
f6390 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 free(zConverted
f63a0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
f63b0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
f63c0 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 }. GetFullP
f63d0 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 athNameA((char*)
f63e0 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 zConverted, nByt
f63f0 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 e, zTemp, 0);.
f6400 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
f6410 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73 d);. zOut = s
f6420 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 qlite3_win32_mbc
f6430 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 s_to_utf8(zTemp)
f6440 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 ;. free(zTemp
f6450 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
f6460 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 20 20 20 if( zOut ){.
f6470 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
f6480 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d (pVfs->mxPathnam
f6490 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 e, zFull, "%s",
f64a0 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 65 65 28 zOut);. free(
f64b0 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 zOut);. retur
f64c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
f64d0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
f64e0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
f64f0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a }.#endif.}../*.
f6500 2a 2a 20 47 65 74 20 74 68 65 20 73 65 63 74 6f ** Get the secto
f6510 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 r size of the de
f6520 76 69 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f vice used to sto
f6530 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 re.** file..*/.s
f6540 74 61 74 69 63 20 69 6e 74 20 67 65 74 53 65 63 tatic int getSec
f6550 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 73 71 6c torSize(. sql
f6560 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a ite3_vfs *pVfs,.
f6570 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
f6580 7a 52 65 6c 61 74 69 76 65 20 20 20 20 20 2f 2a zRelative /*
f6590 20 55 54 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65 UTF-8 file name
f65a0 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 62 */.){. DWORD b
f65b0 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 ytesPerSector =
f65c0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 SQLITE_DEFAULT_S
f65d0 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 63 68 ECTOR_SIZE;. ch
f65e0 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58 ar zFullpath[MAX
f65f0 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 _PATH+1];. int
f6600 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65 rc;. DWORD dwRe
f6610 74 20 3d 20 30 2c 20 64 77 44 75 6d 6d 79 3b 0a t = 0, dwDummy;.
f6620 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 . /*. ** We ne
f6630 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75 ed to get the fu
f6640 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20 ll path name of
f6650 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f the file. ** to
f6660 20 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c get the drive l
f6670 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 etter to look up
f6680 20 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a the sector. **
f6690 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 size.. */. rc
f66a0 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 = winFullPathna
f66b0 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69 me(pVfs, zRelati
f66c0 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46 ve, MAX_PATH, zF
f66d0 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20 ullpath);. if(
f66e0 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 rc == SQLITE_OK
f66f0 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a ). {. void *
f6700 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
f6710 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
f6720 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20 (zFullpath);.
f6730 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20 if( zConverted
f6740 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e ){. if( isN
f6750 54 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f T() ){. /
f6760 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a * trim path to j
f6770 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 ust drive refere
f6780 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 nce */. W
f6790 43 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 CHAR *p = zConve
f67a0 72 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f rted;. fo
f67b0 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 r(;*p;p++){.
f67c0 20 20 20 20 20 20 69 66 28 20 2a 70 20 3d 3d 20 if( *p ==
f67d0 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 '\\' ){.
f67e0 20 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 *p = '\0';.
f67f0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
f6800 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
f6810 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
f6820 64 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46 dwRet = GetDiskF
f6830 72 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52 reeSpaceW((WCHAR
f6840 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 *)zConverted,.
f6850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6870 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 &dwDummy,.
f6880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6890 20 20 20 20 20 20 20 20 20 20 20 20 26 62 79 74 &byt
f68a0 65 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20 20 esPerSector,.
f68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f68c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
f68d0 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 dwDummy,.
f68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f68f0 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 &dwDu
f6900 6d 6d 79 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 mmy);.#if SQLITE
f6910 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 20 _OS_WINCE==0.
f6920 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
f6930 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74 /* trim path t
f6940 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66 o just drive ref
f6950 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 erence */.
f6960 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43 48 41 CHAR *p = (CHA
f6970 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a R *)zConverted;.
f6980 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b for(;*p;
f6990 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 p++){.
f69a0 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 if( *p == '\\' )
f69b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 {. *p
f69c0 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 = '\0';.
f69d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
f69e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
f69f0 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 }. dwRet
f6a00 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 = GetDiskFreeSpa
f6a10 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76 ceA((CHAR*)zConv
f6a20 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 erted,.
f6a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6a40 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d &dwDumm
f6a50 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
f6a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6a70 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 &bytesPerSe
f6a80 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 ctor,.
f6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6aa0 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 &dwDummy
f6ab0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
f6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ad0 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 23 &dwDummy);.#
f6ae0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 endif. }.
f6af0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
f6b00 74 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ted);. }.
f6b10 69 66 28 20 21 64 77 52 65 74 20 29 7b 0a 20 20 if( !dwRet ){.
f6b20 20 20 20 20 62 79 74 65 73 50 65 72 53 65 63 74 bytesPerSect
f6b30 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 or = SQLITE_DEFA
f6b40 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b ULT_SECTOR_SIZE;
f6b50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
f6b60 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73 50 urn (int) bytesP
f6b70 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 erSector; .}..#i
f6b80 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
f6b90 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
f6ba0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 ./*.** Interface
f6bb0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 s for opening a
f6bc0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
f6bd0 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f finding entry po
f6be0 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 ints.** within t
f6bf0 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
f6c00 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 y, and closing t
f6c10 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
f6c20 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 y..*/./*.** Inte
f6c30 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
f6c40 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
f6c50 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
f6c60 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
f6c70 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
f6c80 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
f6c90 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
f6ca0 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 ibrary..*/.stati
f6cb0 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 c void *winDlOpe
f6cc0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 n(sqlite3_vfs *p
f6cd0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Vfs, const char
f6ce0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 *zFilename){. H
f6cf0 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 ANDLE h;. void
f6d00 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f *zConverted = co
f6d10 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d nvertUtf8Filenam
f6d20 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f6d30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f6d40 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 (pVfs);. if( zC
f6d50 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
f6d60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
f6d70 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
f6d80 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 . h = LoadLib
f6d90 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43 raryW((WCHAR*)zC
f6da0 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 onverted);./* is
f6db0 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
f6dc0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
f6dd0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
f6de0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
f6df0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
f6e00 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
f6e10 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
f6e20 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
f6e30 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
f6e40 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
f6e50 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
f6e60 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
f6e70 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
f6e80 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
f6e90 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c }else{. h = L
f6ea0 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61 oadLibraryA((cha
f6eb0 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a r*)zConverted);.
f6ec0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 #endif. }. fre
f6ed0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
f6ee0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 return (void*)h
f6ef0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
f6f00 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 winDlError(sqlit
f6f10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
f6f20 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
f6f30 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 ufOut){. UNUSED
f6f40 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
f6f50 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72 ;. getLastError
f6f60 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 Msg(nBuf, zBufOu
f6f70 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e t);.}.void (*win
f6f80 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
f6f90 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
f6fa0 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 Handle, const ch
f6fb0 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f ar *zSymbol))(vo
f6fc0 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 id){. UNUSED_PA
f6fd0 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 RAMETER(pVfs);.#
f6fe0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f6ff0 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50 CE. /* The GetP
f7000 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 6f rocAddressA() ro
f7010 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 utine is only av
f7020 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 ailable on wince
f7030 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 . */. return (v
f7040 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74 oid(*)(void))Get
f7050 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41 ProcAddressA((HA
f7060 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 NDLE)pHandle, zS
f7070 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 ymbol);.#else.
f7080 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e /* All other win
f7090 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 dows platforms e
f70a0 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64 xpect GetProcAdd
f70b0 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 ress() to take.
f70c0 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69 ** an Ansi stri
f70d0 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 ng regardless of
f70e0 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 the _UNICODE se
f70f0 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 tting */. retur
f7100 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 n (void(*)(void)
f7110 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 )GetProcAddress(
f7120 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c (HANDLE)pHandle,
f7130 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 zSymbol);.#endi
f7140 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c f.}.void winDlCl
f7150 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
f7160 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 *pVfs, void *pHa
f7170 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f ndle){. UNUSED_
f7180 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
f7190 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 28 28 . FreeLibrary((
f71a0 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b HANDLE)pHandle);
f71b0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 .}.#else /* if S
f71c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
f71d0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 EXTENSION is def
f71e0 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 ined: */. #defi
f71f0 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a ne winDlOpen 0.
f7200 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 #define winDlE
f7210 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 rror 0. #define
f7220 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 winDlSym 0.
f7230 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f #define winDlClo
f7240 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a se 0.#endif.../*
f7250 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20 .** Write up to
f7260 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 nBuf bytes of ra
f7270 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 ndomness into zB
f7280 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e uf..*/.static in
f7290 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 t winRandomness(
f72a0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f72b0 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
f72c0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 r *zBuf){. int
f72d0 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f n = 0;. UNUSED_
f72e0 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
f72f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
f7300 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 ITE_TEST). n =
f7310 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a nBuf;. memset(z
f7320 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 Buf, 0, nBuf);.#
f7330 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65 6f else. if( sizeo
f7340 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e f(SYSTEMTIME)<=n
f7350 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 Buf-n ){. SYS
f7360 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 TEMTIME x;. G
f7370 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29 etSystemTime(&x)
f7380 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
f7390 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f uf[n], &x, sizeo
f73a0 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 f(x));. n +=
f73b0 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 sizeof(x);. }.
f73c0 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 if( sizeof(DWOR
f73d0 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 D)<=nBuf-n ){.
f73e0 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65 DWORD pid = Ge
f73f0 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 tCurrentProcessI
f7400 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 d();. memcpy(
f7410 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 &zBuf[n], &pid,
f7420 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 sizeof(pid));.
f7430 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 n += sizeof(pi
f7440 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 d);. }. if( si
f7450 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 zeof(DWORD)<=nBu
f7460 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 f-n ){. DWORD
f7470 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f cnt = GetTickCo
f7480 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 unt();. memcp
f7490 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 y(&zBuf[n], &cnt
f74a0 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a , sizeof(cnt));.
f74b0 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 n += sizeof(
f74c0 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 cnt);. }. if(
f74d0 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 sizeof(LARGE_INT
f74e0 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b EGER)<=nBuf-n ){
f74f0 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 . LARGE_INTEG
f7500 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50 ER i;. QueryP
f7510 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 erformanceCounte
f7520 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 r(&i);. memcp
f7530 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 y(&zBuf[n], &i,
f7540 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 sizeof(i));.
f7550 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a n += sizeof(i);.
f7560 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
f7570 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn n;.}.../*.**
f7580 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 Sleep for a lit
f7590 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 tle while. Retu
f75a0 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 rn the amount of
f75b0 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a time slept..*/.
f75c0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 6c static int winSl
f75d0 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 eep(sqlite3_vfs
f75e0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f *pVfs, int micro
f75f0 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d sec){. Sleep((m
f7600 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 icrosec+999)/100
f7610 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0);. UNUSED_PAR
f7620 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
f7630 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 return ((microse
f7640 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30 30 c+999)/1000)*100
f7650 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
f7660 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 following variab
f7670 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 le, if set to a
f7680 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 non-zero value,
f7690 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75 becomes the resu
f76a0 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 lt.** returned f
f76b0 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72 rom sqlite3OsCur
f76c0 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69 rentTime(). Thi
f76d0 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 s is used for te
f76e0 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 sting..*/.#ifdef
f76f0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
f7700 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f7710 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
f7720 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
f7730 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 .** Find the cur
f7740 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e rent time (in Un
f7750 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 iversal Coordina
f7760 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 ted Time). Writ
f7770 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 e the.** current
f7780 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 time and date a
f7790 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e s a Julian Day n
f77a0 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f umber into *prNo
f77b0 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 w and.** return
f77c0 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 0. Return 1 if
f77d0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 the time and dat
f77e0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e e cannot be foun
f77f0 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 d..*/.int winCur
f7800 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
f7810 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 _vfs *pVfs, doub
f7820 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 le *prNow){. FI
f7830 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 LETIME ft;. /*
f7840 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74 75 FILETIME structu
f7850 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 20 76 re is a 64-bit v
f7860 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e alue representin
f7870 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 g the number of
f7880 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 . 100-nanose
f7890 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 73 cond intervals s
f78a0 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 ince January 1,
f78b0 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35 38 1601 (= JD 23058
f78c0 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 13.5). . */. s
f78d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d qlite3_int64 tim
f78e0 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 eW; /* Whole d
f78f0 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ays */. sqlite3
f7900 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20 20 _int64 timeF;
f7910 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 /* Fractional Da
f7920 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 ys */.. /* Numb
f7930 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 er of 100-nanose
f7940 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 69 cond intervals i
f7950 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20 2a n a single day *
f7960 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 /. static const
f7970 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e sqlite3_int64 n
f7980 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20 20 tuPerDay = .
f7990 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 10000000*(sqli
f79a0 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 3b te3_int64)86400;
f79b0 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 .. /* Number of
f79c0 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 100-nanosecond
f79d0 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61 6c intervals in hal
f79e0 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 f of a day */.
f79f0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
f7a00 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 ite3_int64 ntuPe
f7a10 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 rHalfDay = .
f7a20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 10000000*(sqli
f7a30 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30 3b te3_int64)43200;
f7a40 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f .. /* 2^32 - to
f7a50 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c avoid use of LL
f7a60 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69 6e and warnings in
f7a70 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 gcc */. static
f7a80 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
f7a90 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 61 6c nt64 max32BitVal
f7aa0 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71 6c ue = . (sql
f7ab0 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 ite3_int64)20000
f7ac0 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 00000 + (sqlite3
f7ad0 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 _int64)200000000
f7ae0 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 0 + (sqlite3_int
f7af0 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 64)294967296;..#
f7b00 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f7b10 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 CE. SYSTEMTIME
f7b20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 65 time;. GetSyste
f7b30 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 mTime(&time);.
f7b40 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d 65 /* if SystemTime
f7b50 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 69 ToFileTime() fai
f7b60 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 7a ls, it returns z
f7b70 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 ero. */. if (!S
f7b80 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 ystemTimeToFileT
f7b90 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 7b ime(&time,&ft)){
f7ba0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
f7bb0 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 79 }.#else. GetSy
f7bc0 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 stemTimeAsFileTi
f7bd0 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 me( &ft );.#endi
f7be0 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d f. UNUSED_PARAM
f7bf0 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74 69 ETER(pVfs);. ti
f7c00 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65 33 meW = (((sqlite3
f7c10 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68 _int64)ft.dwHigh
f7c20 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 DateTime)*max32B
f7c30 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 itValue) + (sqli
f7c40 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c te3_int64)ft.dwL
f7c50 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 owDateTime;. ti
f7c60 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 meF = timeW % nt
f7c70 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 uPerDay;
f7c80 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 /* fractional
f7c90 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 days (100-nanose
f7ca0 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 conds) */. time
f7cb0 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 W = timeW / ntuP
f7cc0 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 erDay;
f7cd0 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f /* whole days */
f7ce0 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 . timeW = timeW
f7cf0 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20 20 + 2305813;
f7d00 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77 68 /* add wh
f7d10 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20 32 ole days (from 2
f7d20 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 305813.5) */. t
f7d30 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e imeF = timeF + n
f7d40 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20 20 tuPerHalfDay;
f7d50 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 /* add half a
f7d60 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35 38 day (from 23058
f7d70 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 13.5) */. timeW
f7d80 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 = timeW + (time
f7d90 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20 2f F/ntuPerDay); /
f7da0 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 20 * add whole day
f7db0 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64 65 if half day made
f7dc0 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 one */. timeF
f7dd0 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65 72 = timeF % ntuPer
f7de0 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Day; /*
f7df0 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72 61 compute new fra
f7e00 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a ctional days */.
f7e10 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 *prNow = (doub
f7e20 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 le)timeW + ((dou
f7e30 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 ble)timeF / (dou
f7e40 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b 0a ble)ntuPerDay);.
f7e50 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
f7e60 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ST. if( sqlite3
f7e70 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b _current_time ){
f7e80 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 . *prNow = ((
f7e90 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 double)sqlite3_c
f7ea0 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 urrent_time + (d
f7eb0 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20 28 ouble)43200) / (
f7ec0 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20 28 double)86400 + (
f7ed0 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b 0a double)2440587;.
f7ee0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
f7ef0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
f7f00 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 The idea is that
f7f10 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
f7f20 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 orks like a comb
f7f30 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 ination of.** Ge
f7f40 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e 64 tLastError() and
f7f50 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 29 FormatMessage()
f7f60 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 on windows (or
f7f70 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 errno and.** str
f7f80 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 error_r() on uni
f7f90 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72 x). After an err
f7fa0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 or is returned b
f7fb0 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 y an OS.** funct
f7fc0 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c ion, SQLite call
f7fd0 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 s this function
f7fe0 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 with zBuf pointi
f7ff0 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 ng to.** a buffe
f8000 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 2e r of nBuf bytes.
f8010 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 68 The OS layer sh
f8020 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 68 ould populate th
f8030 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74 68 e.** buffer with
f8040 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
f8050 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 d UTF-8 encoded
f8060 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
f8070 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 20 describing the
f8080 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74 6f last IO error to
f8090 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 77 have occurred w
f80a0 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e ithin the callin
f80b0 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a g.** thread..**.
f80c0 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 ** If the error
f80d0 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20 6c message is too l
f80e0 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 75 70 arge for the sup
f80f0 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a plied buffer,.**
f8100 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74 72 it should be tr
f8110 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65 74 uncated. The ret
f8120 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47 65 urn value of xGe
f8130 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 tLastError.** is
f8140 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 zero if the err
f8150 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 or message fits
f8160 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f in the buffer, o
f8170 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 r non-zero.** ot
f8180 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 herwise (if the
f8190 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e message was trun
f81a0 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a cated). If non-z
f81b0 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c ero is returned,
f81c0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 6e .** then it is n
f81d0 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 ot necessary to
f81e0 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d include the nul-
f81f0 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 terminator chara
f8200 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f cter.** in the o
f8210 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a utput buffer..**
f8220 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e .** Not supplyin
f8230 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 g an error messa
f8240 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 ge will have no
f8250 61 64 76 65 72 73 65 20 65 66 66 65 63 74 0a 2a adverse effect.*
f8260 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 * on SQLite. It
f8270 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 20 is fine to have
f8280 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f an implementatio
f8290 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 n that never.**
f82a0 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
f82b0 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 message:.**.**
f82c0 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 72 int xGetLastEr
f82d0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
f82e0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c *pVfs, int nBuf,
f82f0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a char *zBuf){.**
f8300 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 75 66 assert(zBuf
f8310 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 [0]=='\0');.**
f8320 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 return 0;.**
f8330 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 }.**.** Howeve
f8340 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 r if an error me
f8350 73 73 61 67 65 20 69 73 20 73 75 70 70 6c 69 65 ssage is supplie
f8360 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e d, it will be in
f8370 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 corporated.** by
f8380 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 sqlite into the
f8390 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 error message a
f83a0 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 vailable to the
f83b0 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 user using.** sq
f83c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 lite3_errmsg(),
f83d0 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 possibly making
f83e0 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65 72 IO errors easier
f83f0 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 to debug..*/.st
f8400 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74 4c atic int winGetL
f8410 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33 astError(sqlite3
f8420 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
f8430 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
f8440 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
f8450 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 METER(pVfs);. r
f8460 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72 72 eturn getLastErr
f8470 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 orMsg(nBuf, zBuf
f8480 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 );.}../*.** Init
f8490 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
f84a0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 tialize the oper
f84b0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
f84c0 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erface..*/.SQLIT
f84d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f84e0 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 3_os_init(void){
f84f0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
f8500 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 3_vfs winVfs = {
f8510 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 . 1,
f8520 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
f8530 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f ion */. sizeo
f8540 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a f(winFile), /*
f8550 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 szOsFile */.
f8560 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 20 MAX_PATH,
f8570 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d /* mxPathnam
f8580 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 e */. 0,
f8590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
f85a0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e Next */. "win
f85b0 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 2f 32", /
f85c0 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 * zName */. 0
f85d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
f85e0 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f /* pAppData */
f85f0 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 . . winOpen,
f8600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 /* xOp
f8610 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c en */. winDel
f8620 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ete, /*
f8630 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 xDelete */. w
f8640 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 inAccess,
f8650 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a /* xAccess */.
f8660 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e winFullPathn
f8670 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 ame, /* xFullP
f8680 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 athname */. w
f8690 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 inDlOpen,
f86a0 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a /* xDlOpen */.
f86b0 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 winDlError,
f86c0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 /* xDlErr
f86d0 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 or */. winDlS
f86e0 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ym, /*
f86f0 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 xDlSym */. wi
f8700 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 nDlClose,
f8710 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a /* xDlClose */.
f8720 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 winRandomnes
f8730 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f s, /* xRando
f8740 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e mness */. win
f8750 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 Sleep,
f8760 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 /* xSleep */.
f8770 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c winCurrentTime,
f8780 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 /* xCurrentT
f8790 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 ime */. winGe
f87a0 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a tLastError /*
f87b0 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a xGetLastError *
f87c0 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
f87d0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 _vfs_register(&w
f87e0 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 inVfs, 1);. ret
f87f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a urn SQLITE_OK; .
f8800 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
f8810 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
f8820 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e void){ . return
f8830 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
f8840 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
f8850 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a OS_WIN */../****
f8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f8870 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a f os_win.c *****
f8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
f88c0 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a file bitvec.c *
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
f8900 20 32 30 30 38 20 46 65 62 72 75 61 72 79 20 31 2008 February 1
f8910 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
f8920 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
f8930 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
f8940 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
f8950 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
f8960 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
f8970 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
f8980 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
f8990 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
f89a0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
f89b0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
f89c0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
f89d0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
f89e0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
f89f0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
f8a00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
f8a10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
f8a20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
f8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a70 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
f8a80 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 plements an obje
f8a90 63 74 20 74 68 61 74 20 72 65 70 72 65 73 65 6e ct that represen
f8aa0 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 ts a fixed-lengt
f8ab0 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 h.** bitmap. Bi
f8ac0 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 ts are numbered
f8ad0 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e starting with 1.
f8ae0 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 .**.** A bitmap
f8af0 69 73 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 is used to recor
f8b00 64 20 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 d which pages of
f8b10 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
f8b20 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f have been.** jo
f8b30 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 urnalled during
f8b40 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f a transaction, o
f8b50 72 20 77 68 69 63 68 20 70 61 67 65 73 20 68 61 r which pages ha
f8b60 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 ve the "dont-wri
f8b70 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e te".** property.
f8b80 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 Usually only a
f8b90 20 66 65 77 20 70 61 67 65 73 20 61 72 65 20 6d few pages are m
f8ba0 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 eet either condi
f8bb0 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 tion..** So the
f8bc0 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c bitmap is usuall
f8bd0 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 y sparse and has
f8be0 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 low cardinality
f8bf0 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d ..** But sometim
f8c00 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 es (for example
f8c10 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 when during a DR
f8c20 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 OP of a large ta
f8c30 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 ble) most.** or
f8c40 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 all of the pages
f8c50 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 in a database c
f8c60 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 an get journalle
f8c70 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 d. In those cas
f8c80 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d es, .** the bitm
f8c90 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 ap becomes dense
f8ca0 20 77 69 74 68 20 68 69 67 68 20 63 61 72 64 69 with high cardi
f8cb0 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 nality. The alg
f8cc0 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a orithm needs .**
f8cd0 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 to handle both
f8ce0 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a cases well..**.*
f8cf0 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 * The size of th
f8d00 65 20 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 e bitmap is fixe
f8d10 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 d when the objec
f8d20 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a t is created..**
f8d30 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 .** All bits are
f8d40 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 clear when the
f8d50 62 69 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 bitmap is create
f8d60 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 d. Individual b
f8d70 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 its.** may be se
f8d80 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 t or cleared one
f8d90 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a at a time..**.*
f8da0 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e * Test operation
f8db0 73 20 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 s are about 100
f8dc0 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f times more commo
f8dd0 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 n that set opera
f8de0 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 tions..** Clear
f8df0 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 operations are e
f8e00 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e xceedingly rare.
f8e10 20 20 54 68 65 72 65 20 61 72 65 20 75 73 75 61 There are usua
f8e20 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 lly between.** 5
f8e30 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 and 500 set ope
f8e40 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 rations per Bitv
f8e50 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 ec object, thoug
f8e60 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 h the number of
f8e70 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 sets can.** some
f8e80 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 times grow into
f8e90 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 tens of thousand
f8ea0 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 s or larger. Th
f8eb0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a e size of the.**
f8ec0 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 Bitvec object i
f8ed0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
f8ee0 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
f8ef0 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 abase file at th
f8f00 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 e.** start of a
f8f10 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 transaction, and
f8f20 20 69 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 is thus usually
f8f30 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 less than a few
f8f40 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 thousand,.** bu
f8f50 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 t can be as larg
f8f60 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 e as 2 billion f
f8f70 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 or a really big
f8f80 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
f8f90 40 28 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 @(#) $Id: bitvec
f8fa0 2e 63 2c 76 20 31 2e 31 34 20 32 30 30 39 2f 30 .c,v 1.14 2009/0
f8fb0 34 2f 30 31 20 32 33 3a 34 39 3a 30 34 20 64 72 4/01 23:49:04 dr
f8fc0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 h Exp $.*/../* S
f8fd0 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 ize of the Bitve
f8fe0 63 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 c structure in b
f8ff0 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 ytes. */.#define
f9000 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 BITVEC_SZ
f9010 20 20 35 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 512../* Round
f9020 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 the union size d
f9030 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65 own to the neare
f9040 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 st pointer bound
f9050 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27 ary, since that'
f9060 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c s how .** it wil
f9070 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74 l be aligned wit
f9080 68 69 6e 20 74 68 65 20 42 69 74 76 65 63 20 73 hin the Bitvec s
f9090 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e truct. */.#defin
f90a0 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20 e BITVEC_USIZE
f90b0 20 20 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d (((BITVEC_SZ-
f90c0 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29 29 29 (3*sizeof(u32)))
f90d0 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 /sizeof(Bitvec*)
f90e0 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a )*sizeof(Bitvec*
f90f0 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 ))../* Type of t
f9100 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d 65 6e he array "elemen
f9110 74 22 20 66 6f 72 20 74 68 65 20 62 69 74 6d 61 t" for the bitma
f9120 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e p representation
f9130 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 . .** Should be
f9140 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e a power of 2, an
f9150 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c d ideally, evenl
f9160 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20 42 49 y divide into BI
f9170 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 TVEC_USIZE. .**
f9180 53 65 74 74 69 6e 67 20 74 68 69 73 20 74 6f 20 Setting this to
f9190 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 the "natural wor
f91a0 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 d" size of your
f91b0 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a CPU may improve.
f91c0 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 ** performance.
f91d0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
f91e0 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f C_TELEM u8./
f91f0 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c * Size, in bits,
f9200 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 65 of the bitmap e
f9210 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 lement. */.#defi
f9220 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d ne BITVEC_SZELEM
f9230 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 8./* Number
f9240 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 of elements in a
f9250 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a bitmap array. *
f9260 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
f9270 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49 54 56 _NELEM (BITV
f9280 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 EC_USIZE/sizeof(
f9290 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f BITVEC_TELEM))./
f92a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 * Number of bits
f92b0 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70 20 61 in the bitmap a
f92c0 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 rray. */.#define
f92d0 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 20 20 BITVEC_NBIT
f92e0 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a (BITVEC_NELEM*
f92f0 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a BITVEC_SZELEM)..
f9300 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 /* Number of u32
f9310 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73 68 20 values in hash
f9320 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e table. */.#defin
f9330 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 e BITVEC_NINT
f9340 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 (BITVEC_USIZE
f9350 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a /sizeof(u32))./*
f9360 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 Maximum number
f9370 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 68 61 of entries in ha
f9380 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20 sh table before
f9390 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69 6e 67 .** sub-dividing
f93a0 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e and re-hashing.
f93b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
f93c0 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28 42 49 EC_MXHASH (BI
f93d0 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 TVEC_NINT/2)./*
f93e0 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e Hashing function
f93f0 20 66 6f 72 20 74 68 65 20 61 48 61 73 68 20 72 for the aHash r
f9400 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a epresentation..*
f9410 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65 73 74 * Empirical test
f9420 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61 74 20 ing showed that
f9430 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 the *37 multipli
f9440 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 er .** (an arbit
f9450 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20 74 68 rary prime)in th
f9460 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 e hash function
f9470 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 provided .** no
f9480 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 fewer collisions
f9490 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 than the no-op
f94a0 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 *1. */.#define B
f94b0 49 54 56 45 43 5f 48 41 53 48 28 58 29 20 20 20 ITVEC_HASH(X)
f94c0 28 28 28 58 29 2a 31 29 25 42 49 54 56 45 43 5f (((X)*1)%BITVEC_
f94d0 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 NINT)..#define B
f94e0 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20 ITVEC_NPTR
f94f0 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 (BITVEC_USIZE/si
f9500 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a zeof(Bitvec *)).
f9510 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 ../*.** A bitmap
f9520 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
f9530 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
f9540 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
f9550 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 72 65 * This bitmap re
f9560 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 74 61 cords the exista
f9570 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d nce of zero or m
f9580 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 ore bits.** with
f9590 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 values between
f95a0 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 1 and iSize, inc
f95b0 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 lusive..**.** Th
f95c0 65 72 65 20 61 72 65 20 74 68 72 65 65 20 70 6f ere are three po
f95d0 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74 ssible represent
f95e0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 ations of the bi
f95f0 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a tmap..** If iSiz
f9600 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 e<=BITVEC_NBIT,
f9610 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 then Bitvec.u.aB
f9620 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72 itmap[] is a str
f9630 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e aight.** bitmap.
f9640 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e The least sign
f9650 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 20 62 ificant bit is b
f9660 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 it 1..**.** If i
f9670 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 Size>BITVEC_NBIT
f9680 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 and iDivisor==0
f9690 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 then Bitvec.u.a
f96a0 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 Hash[] is.** a h
f96b0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 77 ash table that w
f96c0 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 ill hold up to B
f96d0 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73 ITVEC_MXHASH dis
f96e0 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a tinct values..**
f96f0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 .** Otherwise, t
f9700 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 72 65 he value i is re
f9710 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e directed into on
f9720 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 e of BITVEC_NPTR
f9730 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20 .** sub-bitmaps
f9740 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69 pointed to by Bi
f9750 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 tvec.u.apSub[].
f9760 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a Each subbitmap.
f9770 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f ** handles up to
f9780 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 72 61 iDivisor separa
f9790 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 te values of i.
f97a0 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a apSub[0] holds.
f97b0 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 ** values betwee
f97c0 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 n 1 and iDivisor
f97d0 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 . apSub[1] hold
f97e0 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e s values between
f97f0 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61 .** iDivisor+1 a
f9800 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 nd 2*iDivisor.
f9810 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 apSub[N] holds v
f9820 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a alues between.**
f9830 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e N*iDivisor+1 an
f9840 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 d (N+1)*iDivisor
f9850 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 . Each subbitma
f9860 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a p is normalized.
f9870 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 ** to hold deal
f9880 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 with values betw
f9890 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 een 1 and iDivis
f98a0 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 or..*/.struct Bi
f98b0 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69 tvec {. u32 iSi
f98c0 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 ze; /* Maxi
f98d0 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e 20 20 mum bit index.
f98e0 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34 2c 32 Max iSize is 4,2
f98f0 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 94,967,296. */.
f9900 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20 u32 nSet;
f9910 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 /* Number of bi
f9920 74 73 20 74 68 61 74 20 61 72 65 20 73 65 74 20 ts that are set
f9930 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 - only valid for
f9940 20 61 48 61 73 68 0a 20 20 20 20 20 20 20 20 20 aHash.
f9950 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d ** elem
f9960 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42 49 54 ent. Max is BIT
f9970 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42 VEC_NINT. For B
f9980 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c ITVEC_SZ of 512,
f9990 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f99a0 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75 6c 64 ** this would
f99b0 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 be 125. */. u3
f99c0 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 2 iDivisor; /*
f99d0 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 Number of bits
f99e0 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20 handled by each
f99f0 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a apSub[] entry. *
f9a00 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
f9a10 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d /* Should >=
f9a20 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 0 for apSub elem
f9a30 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ent. */.
f9a40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
f9a50 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78 iDivisor is max
f9a60 28 75 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e (u32) / BITVEC_N
f9a70 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 PTR + 1. */.
f9a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f9a90 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53 * For a BITVEC_S
f9aa0 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77 Z of 512, this w
f9ab0 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37 ould be 34,359,7
f9ac0 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 39. */. union {
f9ad0 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45 . BITVEC_TELE
f9ae0 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43 M aBitmap[BITVEC
f9af0 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 _NELEM]; /* B
f9b00 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 itmap representa
f9b10 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 tion */. u32
f9b20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e aHash[BITVEC_NIN
f9b30 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68 T]; /* Hash
f9b40 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 table represent
f9b50 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 ation */. Bit
f9b60 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45 vec *apSub[BITVE
f9b70 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 C_NPTR]; /* Rec
f9b80 75 72 73 69 76 65 20 72 65 70 72 65 73 65 6e 74 ursive represent
f9b90 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a ation */. } u;.
f9ba0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 };../*.** Create
f9bb0 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 a new bitmap ob
f9bc0 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e ject able to han
f9bd0 64 6c 65 20 62 69 74 73 20 62 65 74 77 65 65 6e dle bits between
f9be0 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 0 and iSize,.**
f9bf0 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 inclusive. Ret
f9c00 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
f9c10 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e the new object.
f9c20 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 Return NULL if
f9c30 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c .** malloc fail
f9c40 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
f9c50 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c VATE Bitvec *sql
f9c60 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
f9c70 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 (u32 iSize){. B
f9c80 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 itvec *p;. asse
f9c90 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d rt( sizeof(*p)==
f9ca0 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 BITVEC_SZ );. p
f9cb0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
f9cc0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
f9cd0 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
f9ce0 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 p->iSize = iS
f9cf0 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ize;. }. retur
f9d00 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 n p;.}../*.** Ch
f9d10 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
f9d20 65 20 69 2d 74 68 20 62 69 74 20 69 73 20 73 65 e i-th bit is se
f9d30 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 t. Return true
f9d40 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 or false..** If
f9d50 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 p is NULL (if th
f9d60 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 e bitmap has not
f9d70 20 62 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f been created) o
f9d80 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 r if.** i is out
f9d90 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 of range, then
f9da0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f return false..*/
f9db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
f9dc0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
f9dd0 63 54 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c cTest(Bitvec *p,
f9de0 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70 u32 i){. if( p
f9df0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
f9e00 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 if( i>p->iSize
f9e10 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72 || i==0 ) retur
f9e20 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 n 0;. i--;. wh
f9e30 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 ile( p->iDivisor
f9e40 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 ){. u32 bin
f9e50 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i/p->iDivisor;
f9e60 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 . i = i%p->iD
f9e70 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 ivisor;. p =
f9e80 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b p->u.apSub[bin];
f9e90 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 . if (!p) {.
f9ea0 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
f9eb0 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
f9ec0 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f ->iSize<=BITVEC_
f9ed0 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 NBIT ){. retu
f9ee0 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 rn (p->u.aBitmap
f9ef0 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d [i/BITVEC_SZELEM
f9f00 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56 ] & (1<<(i&(BITV
f9f10 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 EC_SZELEM-1))))!
f9f20 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 =0;. } else{.
f9f30 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 u32 h = BITVEC
f9f40 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 _HASH(i++);.
f9f50 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 while( p->u.aHas
f9f60 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 h[h] ){. if
f9f70 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d ( p->u.aHash[h]=
f9f80 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 =i ) return 1;.
f9f90 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 h++;.
f9fa0 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 if( h>=BITVEC_NI
f9fb0 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 NT ) h = 0;.
f9fc0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
f9fd0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 }.}../*.** Set
f9fe0 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 the i-th bit.
f9ff0 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 Return 0 on succ
fa000 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ess and an error
fa010 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 code if.** anyt
fa020 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
fa030 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
fa040 69 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 ine might cause
fa050 73 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 sub-bitmaps to b
fa060 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 e allocated. Fa
fa070 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 iling.** to get
fa080 74 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 the memory neede
fa090 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 d to hold the su
fa0a0 62 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 b-bitmap is the
fa0b0 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e only.** that can
fa0c0 20 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 go wrong with a
fa0d0 6e 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 n insert, assumi
fa0e0 6e 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 ng p and i are v
fa0f0 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 alid..**.** The
fa100 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
fa110 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 must ensure tha
fa120 74 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 t p is a valid B
fa130 69 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 itvec object.**
fa140 61 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c and that the val
fa150 75 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 ue for "i" is wi
fa160 74 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 thin range of th
fa170 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e e Bitvec object.
fa180 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 .** Otherwise th
fa190 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e e behavior is un
fa1a0 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 defined..*/.SQLI
fa1b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
fa1c0 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
fa1d0 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 Bitvec *p, u32 i
fa1e0 29 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73 ){. u32 h;. as
fa1f0 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
fa200 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 assert( i>0 );.
fa210 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69 assert( i<=p->i
fa220 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 Size );. i--;.
fa230 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65 while((p->iSize
fa240 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20 > BITVEC_NBIT)
fa250 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20 && p->iDivisor)
fa260 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 {. u32 bin =
fa270 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 i/p->iDivisor;.
fa280 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 i = i%p->iDiv
fa290 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d isor;. if( p-
fa2a0 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 >u.apSub[bin]==0
fa2b0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61 ){. p->u.a
fa2c0 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 pSub[bin] = sqli
fa2d0 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
fa2e0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a p->iDivisor );.
fa2f0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 if( p->u.a
fa300 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 pSub[bin]==0 ) r
fa310 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
fa320 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 EM;. }. p
fa330 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e = p->u.apSub[bin
fa340 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e ];. }. if( p->
fa350 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
fa360 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 IT ){. p->u.a
fa370 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f Bitmap[i/BITVEC_
fa380 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 SZELEM] |= 1 <<
fa390 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 (i&(BITVEC_SZELE
fa3a0 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72 M-1));. retur
fa3b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
fa3c0 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 . h = BITVEC_HA
fa3d0 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 SH(i++);. /* if
fa3e0 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20 there wasn't a
fa3f0 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 hash collision,
fa400 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74 and this doesn't
fa410 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 */. /* complet
fa420 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73 ely fill the has
fa430 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64 h, then just add
fa440 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20 it without */.
fa450 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75 /* worring abou
fa460 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 t sub-dividing a
fa470 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a nd re-hashing. *
fa480 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48 /. if( !p->u.aH
fa490 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66 ash[h] ){. if
fa4a0 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45 (p->nSet<(BITVE
fa4b0 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20 C_NINT-1)) {.
fa4c0 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 goto bitvec_s
fa4d0 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c et_end;. } el
fa4e0 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 se {. goto
fa4f0 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 bitvec_set_rehas
fa500 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f h;. }. }. /
fa510 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f * there was a co
fa520 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74 llision, check t
fa530 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c o see if it's al
fa540 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e ready */. /* in
fa550 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74 hash, if not, t
fa560 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f ry to find a spo
fa570 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f t for it */. do
fa580 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e {. if( p->u.
fa590 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 aHash[h]==i ) re
fa5a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fa5b0 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28 h++;. if(
fa5c0 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 h>=BITVEC_NINT
fa5d0 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 ) h = 0;. } whi
fa5e0 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 le( p->u.aHash[h
fa5f0 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64 ] );. /* we did
fa600 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74 n't find it in t
fa610 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e he hash. h poin
fa620 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
fa630 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c */. /* availabl
fa640 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65 e free spot. che
fa650 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 ck to see if thi
fa660 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f s is going to */
fa670 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68 . /* make our h
fa680 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 ash too "full".
fa690 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72 */.bitvec_set_r
fa6a0 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e ehash:. if( p->
fa6b0 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48 nSet>=BITVEC_MXH
fa6c0 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 ASH ){. unsig
fa6d0 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69 ned int j;. i
fa6e0 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61 nt rc;. u32 a
fa6f0 69 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e iValues[BITVEC_N
fa700 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 INT];. memcpy
fa710 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e (aiValues, p->u.
fa720 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 aHash, sizeof(ai
fa730 56 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 Values));. me
fa740 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c mset(p->u.apSub,
fa750 20 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 0, sizeof(aiVal
fa760 75 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44 ues));. p->iD
fa770 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 ivisor = (p->iSi
fa780 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 ze + BITVEC_NPTR
fa790 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 - 1)/BITVEC_NPT
fa7a0 52 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 R;. rc = sqli
fa7b0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 te3BitvecSet(p,
fa7c0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b i);. for(j=0;
fa7d0 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 j<BITVEC_NINT;
fa7e0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 j++){. if(
fa7f0 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 aiValues[j] ) rc
fa800 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 |= sqlite3Bitve
fa810 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 cSet(p, aiValues
fa820 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 [j]);. }.
fa830 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 return rc;. }.b
fa840 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 itvec_set_end:.
fa850 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d p->nSet++;. p-
fa860 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b >u.aHash[h] = i;
fa870 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
fa880 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
fa890 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 ear the i-th bit
fa8a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
fa8b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
fa8c0 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 BitvecClear(Bitv
fa8d0 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
fa8e0 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
fa8f0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 . assert( i>0 )
fa900 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 ;. i--;. while
fa910 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b ( p->iDivisor ){
fa920 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 . u32 bin = i
fa930 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 /p->iDivisor;.
fa940 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 i = i%p->iDivi
fa950 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e sor;. p = p->
fa960 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 u.apSub[bin];.
fa970 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 if (!p) {.
fa980 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
fa990 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 }. if( p->iSi
fa9a0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 ze<=BITVEC_NBIT
fa9b0 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 ){. p->u.aBit
fa9c0 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 map[i/BITVEC_SZE
fa9d0 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 LEM] &= ~(1 << (
fa9e0 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d i&(BITVEC_SZELEM
fa9f0 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a -1)));. }else{.
faa00 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
faa10 20 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 j;. u32 aiVa
faa20 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 lues[BITVEC_NINT
faa30 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 ];. memcpy(ai
faa40 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 Values, p->u.aHa
faa50 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c sh, sizeof(aiVal
faa60 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 ues));. memse
faa70 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c t(p->u.aHash, 0,
faa80 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 sizeof(aiValues
faa90 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 ));. p->nSet
faaa0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 = 0;. for(j=0
faab0 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b ; j<BITVEC_NINT;
faac0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 j++){. if(
faad0 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 aiValues[j] &&
faae0 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b aiValues[j]!=(i+
faaf0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 1) ){. u3
fab00 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 2 h = BITVEC_HAS
fab10 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 H(aiValues[j]-1)
fab20 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 ;. p->nSe
fab30 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 t++;. whi
fab40 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 le( p->u.aHash[h
fab50 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68 ] ){. h
fab60 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ++;. if
fab70 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 ( h>=BITVEC_NINT
fab80 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 ) h = 0;.
fab90 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 }. p->u
faba0 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 .aHash[h] = aiVa
fabb0 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d lues[j];. }
fabc0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
fabd0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 .** Destroy a bi
fabe0 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 tmap object. Re
fabf0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
fac00 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 used..*/.SQLITE
fac10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
fac20 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
fac30 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 oy(Bitvec *p){.
fac40 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
fac50 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 rn;. if( p->iDi
fac60 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 visor ){. uns
fac70 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 igned int i;.
fac80 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 for(i=0; i<BITV
fac90 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 EC_NPTR; i++){.
faca0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
facb0 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 ecDestroy(p->u.a
facc0 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a pSub[i]);. }.
facd0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
face0 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ee(p);.}../*.**
facf0 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 Return the value
fad00 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20 70 61 of the iSize pa
fad10 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 rameter specifie
fad20 64 20 77 68 65 6e 20 42 69 74 76 65 63 20 2a 70 d when Bitvec *p
fad30 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2e .** was created.
fad40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fad50 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 TE u32 sqlite3Bi
fad60 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 20 tvecSize(Bitvec
fad70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
fad80 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 >iSize;.}..#ifnd
fad90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
fada0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a UILTIN_TEST./*.*
fadb0 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 * Let V[] be an
fadc0 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 array of unsigne
fadd0 64 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 d characters suf
fade0 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a ficient to hold.
fadf0 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e ** up to N bits.
fae00 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e Let I be an in
fae10 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 teger between 0
fae20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a and N. 0<=I<N..
fae30 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c ** Then the foll
fae40 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e owing macros can
fae50 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c be used to set,
fae60 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a clear, or test.
fae70 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 ** individual bi
fae80 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a ts within V..*/.
fae90 23 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 #define SETBIT(V
faea0 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d ,I) V[I>>3]
faeb0 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 |= (1<<(I&7)).#
faec0 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 define CLEARBIT(
faed0 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 V,I) V[I>>3]
faee0 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 &= ~(1<<(I&7)).#
faef0 64 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 define TESTBIT(V
faf00 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d ,I) (V[I>>3]
faf10 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a &(1<<(I&7)))!=0.
faf20 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
faf30 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 ine runs an exte
faf40 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 nsive test of th
faf50 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a e Bitvec code..*
faf60 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 *.** The input i
faf70 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e s an array of in
faf80 74 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 tegers that acts
faf90 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a as a program.**
fafa0 20 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 to test the Bit
fafb0 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 vec. The intege
fafc0 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 rs are opcodes f
fafd0 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c ollowed.** by 0,
fafe0 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 1, or 3 operand
faff0 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 s, depending on
fb000 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f the opcode. Ano
fb010 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 ther.** opcode f
fb020 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 ollows immediate
fb030 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 ly after the las
fb040 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a t operand..**.**
fb050 20 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 There are 6 opc
fb060 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 odes numbered fr
fb070 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 om 0 through 5.
fb080 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 0 is the.** "ha
fb090 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 lt" opcode and c
fb0a0 61 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 auses the test t
fb0b0 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 o end..**.**
fb0c0 30 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 0 Halt
fb0d0 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e and return the n
fb0e0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a umber of errors.
fb0f0 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 ** 1 N S X
fb100 20 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 Set N bits begi
fb110 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 nning with S and
fb120 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 incrementing by
fb130 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 X.** 2 N S X
fb140 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 Clear N bits
fb150 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
fb160 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 S and incrementi
fb170 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 ng by X.** 3
fb180 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 N Set N r
fb190 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 andomly chosen b
fb1a0 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 its.** 4 N
fb1b0 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e Clear N ran
fb1c0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 domly chosen bit
fb1d0 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 s.** 5 N S X
fb1e0 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 Set N bits fr
fb1f0 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 om S increment X
fb200 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 in array only,
fb210 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a not in bitvec.**
fb220 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 .** The opcodes
fb230 31 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 1 through 4 perf
fb240 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 orm set and clea
fb250 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 r operations are
fb260 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e performed.** on
fb270 20 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f both a Bitvec o
fb280 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c bject and on a l
fb290 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 inear array of b
fb2a0 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f its obtained fro
fb2b0 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 m malloc..** Opc
fb2c0 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 ode 5 works on t
fb2d0 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 he linear array
fb2e0 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 only, not on the
fb2f0 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f Bitvec..** Opco
fb300 64 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 de 5 is used to
fb310 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 deliberately ind
fb320 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f uce a fault in o
fb330 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 rder to.** confi
fb340 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 rm that error de
fb350 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a tection works..*
fb360 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 *.** At the conc
fb370 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 lusion of the te
fb380 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 st the linear ar
fb390 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a ray is compared.
fb3a0 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 ** against the B
fb3b0 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 itvec object. I
fb3c0 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 f there are any
fb3d0 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 differences,.**
fb3e0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
fb3f0 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 rned. If they a
fb400 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 re the same, zer
fb410 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a o is returned..*
fb420 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
fb430 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
fb440 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e r occurs, return
fb450 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 -1..*/.SQLITE_P
fb460 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
fb470 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 e3BitvecBuiltinT
fb480 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 est(int sz, int
fb490 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 *aOp){. Bitvec
fb4a0 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 *pBitvec = 0;.
fb4b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
fb4c0 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 V = 0;. int rc
fb4d0 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e = -1;. int i, n
fb4e0 78 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a x, pc, op;.. /*
fb4f0 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 Allocate the Bi
fb500 74 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 tvec to be teste
fb510 64 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 d and a linear a
fb520 72 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 rray of. ** bit
fb530 73 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 s to act as the
fb540 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 reference */. p
fb550 42 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 Bitvec = sqlite3
fb560 42 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a BitvecCreate( sz
fb570 20 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 );. pV = sqlit
fb580 65 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 e3_malloc( (sz+7
fb590 29 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 )/8 + 1 );. if(
fb5a0 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 pBitvec==0 || p
fb5b0 56 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 V==0 ) goto bitv
fb5c0 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 ec_end;. memset
fb5d0 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 (pV, 0, (sz+7)/8
fb5e0 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e + 1);.. /* Run
fb5f0 20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a the program */.
fb600 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c pc = 0;. whil
fb610 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d e( (op = aOp[pc]
fb620 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 )!=0 ){. swit
fb630 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 ch( op ){.
fb640 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 case 1:. ca
fb650 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 se 2:. case
fb660 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 5: {. nx
fb670 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 = 4;. i
fb680 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b = aOp[pc+2] - 1;
fb690 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b . aOp[pc+
fb6a0 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 2] += aOp[pc+3];
fb6b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
fb6c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
fb6d0 73 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 se 3:. case
fb6e0 20 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 4: . defau
fb6f0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 lt: {. nx
fb700 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 = 2;. sq
fb710 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
fb720 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b (sizeof(i), &i);
fb730 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
fb740 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
fb750 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b if( (--aOp[pc+
fb760 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 1]) > 0 ) nx = 0
fb770 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a ;. pc += nx;.
fb780 20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 i = (i & 0x7
fb790 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 fffffff)%sz;.
fb7a0 20 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 if( (op & 1)!=0
fb7b0 20 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 ){. SETBIT
fb7c0 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 (pV, (i+1));.
fb7d0 20 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a if( op!=5 ){.
fb7e0 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
fb7f0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 te3BitvecSet(pBi
fb800 74 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 tvec, i+1) ) got
fb810 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 o bitvec_end;.
fb820 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
fb830 0a 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 . CLEARBIT(
fb840 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 pV, (i+1));.
fb850 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 sqlite3BitvecC
fb860 6c 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b lear(pBitvec, i+
fb870 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 1);. }. }..
fb880 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 /* Test to make
fb890 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 sure the linear
fb8a0 20 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d array exactly m
fb8b0 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 atches the. **
fb8c0 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 Bitvec object.
fb8d0 53 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 Start with the a
fb8e0 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 ssumption that t
fb8f0 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 hey do. ** matc
fb900 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e h (rc==0). Chan
fb910 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 ge rc to non-zer
fb920 6f 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e o if a discrepan
fb930 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 cy. ** is found
fb940 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
fb950 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
fb960 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 0,0) + sqlite3Bi
fb970 74 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 tvecTest(pBitvec
fb980 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 , sz+1).
fb990 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 + sqlite3Bitve
fb9a0 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 cTest(pBitvec, 0
fb9b0 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 ). + (s
fb9c0 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 qlite3BitvecSize
fb9d0 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b (pBitvec) - sz);
fb9e0 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 . for(i=1; i<=s
fb9f0 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 z; i++){. if(
fba00 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 (TESTBIT(pV,i)
fba10 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 )!=sqlite3Bitvec
fba20 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 Test(pBitvec,i)
fba30 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b ){. rc = i;
fba40 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
fba50 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 }. }.. /* Fr
fba60 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 ee allocated str
fba70 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63 ucture */.bitvec
fba80 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f _end:. sqlite3_
fba90 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c 69 free(pV);. sqli
fbaa0 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
fbab0 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65 74 (pBitvec);. ret
fbac0 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
fbad0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
fbae0 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a BUILTIN_TEST */.
fbaf0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fbb00 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e 63 End of bitvec.c
fbb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
fbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
fbb40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fbb50 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 63 Begin file pcac
fbb60 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.c ***********
fbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
fbb90 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
fbba0 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 05.**.** The
fbbb0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
fbbc0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
fbbd0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
fbbe0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
fbbf0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
fbc00 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
fbc10 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
fbc20 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
fbc30 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
fbc40 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
fbc50 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
fbc60 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
fbc70 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
fbc80 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
fbc90 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
fbca0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
fbcb0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
fbcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbd00 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
fbd10 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 e implements tha
fbd20 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a t page cache..**
fbd30 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 .** @(#) $Id: pc
fbd40 61 63 68 65 2e 63 2c 76 20 31 2e 34 34 20 32 30 ache.c,v 1.44 20
fbd50 30 39 2f 30 33 2f 33 31 20 30 31 3a 33 32 3a 31 09/03/31 01:32:1
fbd60 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 8 drh Exp $.*/..
fbd70 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 /*.** A complete
fbd80 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 page cache is a
fbd90 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
fbda0 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f is structure..*/
fbdb0 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 20 7b .struct PCache {
fbdc0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
fbdd0 2c 20 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 , *pDirtyTail;
fbde0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
fbdf0 66 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e f dirty pages in
fbe00 20 4c 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20 LRU order */.
fbe10 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20 PgHdr *pSynced;
fbe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbe30 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63 /* Last sync
fbe40 65 64 20 70 61 67 65 20 69 6e 20 64 69 72 74 79 ed page in dirty
fbe50 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 page list */.
fbe60 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
fbe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbe80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
fbe90 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 referenced page
fbea0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 3b s */. int nMax;
fbeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
fbed0 6e 66 69 67 75 72 65 64 20 63 61 63 68 65 20 73 nfigured cache s
fbee0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 ize */. int szP
fbef0 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
fbf00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fbf10 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 Size of every pa
fbf20 67 65 20 69 6e 20 74 68 69 73 20 63 61 63 68 65 ge in this cache
fbf30 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 */. int szExtr
fbf40 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
fbf50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
fbf60 65 20 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 e of extra space
fbf70 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a for each page *
fbf80 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 /. int bPurgeab
fbf90 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
fbfa0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
fbfb0 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 if pages are on
fbfc0 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f backing store */
fbfd0 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 . int (*xStress
fbfe0 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b )(void*,PgHdr*);
fbff0 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 /* Call t
fc000 6f 20 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67 o try make a pag
fc010 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 e clean */. voi
fc020 64 20 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20 d *pStress;
fc030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc040 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
fc050 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c xStress */. sql
fc060 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61 ite3_pcache *pCa
fc070 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
fc080 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61 /* Pluggable ca
fc090 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 che module */.
fc0a0 50 67 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20 PgHdr *pPage1;
fc0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc0c0 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 /* Reference
fc0d0 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b to page 1 */.};
fc0e0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 ../*.** Some of
fc0f0 74 68 65 20 61 73 73 65 72 74 28 29 20 6d 61 63 the assert() mac
fc100 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65 ros in this code
fc110 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 are too expensi
fc120 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 ve to run.** eve
fc130 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 n during normal
fc140 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 debugging. Use
fc150 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 them only rarely
fc160 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 on long-running
fc170 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 .** tests. Enab
fc180 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 le the expensive
fc190 20 61 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 asserts using t
fc1a0 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45 he.** -DSQLITE_E
fc1b0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f NABLE_EXPENSIVE_
fc1c0 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65 ASSERT=1 compile
fc1d0 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f -time option..*/
fc1e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
fc1f0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f NABLE_EXPENSIVE_
fc200 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20 ASSERT.# define
fc210 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 expensive_assert
fc220 28 58 29 20 20 61 73 73 65 72 74 28 58 29 0a 23 (X) assert(X).#
fc230 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 else.# define ex
fc240 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 pensive_assert(X
fc250 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a ).#endif../*****
fc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 ************* Li
fc280 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 nked List Manage
fc290 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ment ***********
fc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 *********/..#if
fc2b0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
fc2c0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
fc2d0 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 TE_ENABLE_EXPENS
fc2e0 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a IVE_ASSERT)./*.*
fc2f0 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
fc300 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 pCache->pSynced
fc310 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
fc320 20 63 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69 correctly. If i
fc330 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 t.** is not, eit
fc340 68 65 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65 her fail an asse
fc350 72 74 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 rt or return zer
fc360 6f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 o. Otherwise, re
fc370 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f turn.** non-zero
fc380 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 . This is only u
fc390 73 65 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 sed in debugging
fc3a0 20 62 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c builds, as foll
fc3b0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 ows:.**.** exp
fc3c0 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 ensive_assert( p
fc3d0 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
fc3e0 28 70 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 (pCache) );.*/.s
fc3f0 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 tatic int pcache
fc400 43 68 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63 CheckSynced(PCac
fc410 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 he *pCache){. P
fc420 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 gHdr *p;. for(p
fc430 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 =pCache->pDirtyT
fc440 61 69 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e ail; p!=pCache->
fc450 70 53 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44 pSynced; p=p->pD
fc460 69 72 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61 irtyPrev){. a
fc470 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c ssert( p->nRef |
fc480 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 | (p->flags&PGHD
fc490 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a R_NEED_SYNC) );.
fc4a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d }. return (p=
fc4b0 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c =0 || p->nRef ||
fc4c0 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 (p->flags&PGHDR
fc4d0 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b _NEED_SYNC)==0);
fc4e0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 .}.#endif /* !ND
fc4f0 45 42 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 EBUG && SQLITE_E
fc500 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f NABLE_EXPENSIVE_
fc510 41 53 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ASSERT */../*.**
fc520 20 52 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61 Remove page pPa
fc530 67 65 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 ge from the list
fc540 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 2e of dirty pages.
fc550 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
fc560 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d pcacheRemoveFrom
fc570 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 DirtyList(PgHdr
fc580 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 *pPage){. PCach
fc590 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 e *p = pPage->pC
fc5a0 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 ache;.. assert(
fc5b0 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
fc5c0 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e xt || pPage==p->
fc5d0 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 pDirtyTail );.
fc5e0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
fc5f0 44 69 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61 DirtyPrev || pPa
fc600 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b ge==p->pDirty );
fc610 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
fc620 65 20 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65 e PCache1.pSynce
fc630 64 20 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65 d variable if ne
fc640 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66 cessary. */. if
fc650 28 20 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 ( p->pSynced==pP
fc660 61 67 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 age ){. PgHdr
fc670 20 2a 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 *pSynced = pPag
fc680 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 e->pDirtyPrev;.
fc690 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 while( pSynce
fc6a0 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 d && (pSynced->f
fc6b0 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
fc6c0 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70 SYNC) ){. p
fc6d0 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 Synced = pSynced
fc6e0 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 ->pDirtyPrev;.
fc6f0 20 20 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 }. p->pSync
fc700 65 64 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20 ed = pSynced;.
fc710 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e }.. if( pPage->
fc720 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 pDirtyNext ){.
fc730 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e pPage->pDirtyN
fc740 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 ext->pDirtyPrev
fc750 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 = pPage->pDirtyP
fc760 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 rev;. }else{.
fc770 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d assert( pPage=
fc780 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 =p->pDirtyTail )
fc790 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 ;. p->pDirtyT
fc7a0 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 ail = pPage->pDi
fc7b0 72 74 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 rtyPrev;. }. i
fc7c0 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 f( pPage->pDirty
fc7d0 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67 Prev ){. pPag
fc7e0 65 2d 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 e->pDirtyPrev->p
fc7f0 44 69 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 DirtyNext = pPag
fc800 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 e->pDirtyNext;.
fc810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
fc820 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 rt( pPage==p->pD
fc830 69 72 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70 irty );. p->p
fc840 44 69 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70 Dirty = pPage->p
fc850 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 DirtyNext;. }.
fc860 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
fc870 78 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d xt = 0;. pPage-
fc880 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b >pDirtyPrev = 0;
fc890 0a 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 .. expensive_as
fc8a0 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 sert( pcacheChec
fc8b0 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a kSynced(p) );.}.
fc8c0 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20 ./*.** Add page
fc8d0 70 50 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 pPage to the hea
fc8e0 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c d of the dirty l
fc8f0 69 73 74 20 28 50 43 61 63 68 65 31 2e 70 44 69 ist (PCache1.pDi
fc900 72 74 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a rty is set to.**
fc910 20 70 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 pPage)..*/.stat
fc920 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 ic void pcacheAd
fc930 64 54 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48 dToDirtyList(PgH
fc940 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 dr *pPage){. PC
fc950 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d ache *p = pPage-
fc960 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 >pCache;.. asse
fc970 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 rt( pPage->pDirt
fc980 79 4e 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67 yNext==0 && pPag
fc990 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 e->pDirtyPrev==0
fc9a0 20 26 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70 && p->pDirty!=p
fc9b0 50 61 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65 Page );.. pPage
fc9c0 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 ->pDirtyNext = p
fc9d0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20 ->pDirty;. if(
fc9e0 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
fc9f0 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 t ){. assert(
fca00 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
fca10 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d xt->pDirtyPrev==
fca20 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 0 );. pPage->
fca30 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 pDirtyNext->pDir
fca40 74 79 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a tyPrev = pPage;.
fca50 20 20 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 }. p->pDirty
fca60 3d 20 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21 = pPage;. if( !
fca70 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b p->pDirtyTail ){
fca80 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 . p->pDirtyTa
fca90 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a il = pPage;. }.
fcaa0 20 20 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65 if( !p->pSynce
fcab0 64 20 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e d && 0==(pPage->
fcac0 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
fcad0 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d _SYNC) ){. p-
fcae0 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 >pSynced = pPage
fcaf0 3b 0a 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76 ;. }. expensiv
fcb00 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 e_assert( pcache
fcb10 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 CheckSynced(p) )
fcb20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 ;.}../*.** Wrapp
fcb30 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c er around the pl
fcb40 75 67 67 61 62 6c 65 20 63 61 63 68 65 73 20 78 uggable caches x
fcb50 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66 Unpin method. If
fcb60 20 74 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a the cache is.**
fcb70 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 being used for
fcb80 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
fcb90 61 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 abase, this func
fcba0 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
fcbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
fcbc0 70 63 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64 pcacheUnpin(PgHd
fcbd0 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 r *p){. PCache
fcbe0 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 *pCache = p->pCa
fcbf0 63 68 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 che;. if( pCach
fcc00 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b e->bPurgeable ){
fcc10 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f . if( p->pgno
fcc20 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ==1 ){. pCa
fcc30 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b che->pPage1 = 0;
fcc40 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
fcc50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
fcc60 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 cache.xUnpin(pCa
fcc70 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 che->pCache, p,
fcc80 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 0);. }.}../****
fcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fccb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
fccc0 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66 61 63 General Interfac
fccd0 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 es ******.**.**
fcce0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 Initialize and s
fccf0 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 hutdown the page
fcd00 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
fcd10 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 . Neither of the
fcd20 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 se .** functions
fcd30 20 61 72 65 20 74 68 72 65 61 64 73 61 66 65 2e are threadsafe.
fcd40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fcd50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
fcd60 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 acheInitialize(v
fcd70 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 oid){. if( sqli
fcd80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fcd90 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 pcache.xInit==0
fcda0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 43 ){. sqlite3PC
fcdb0 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 29 acheSetDefault()
fcdc0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 ;. }. return s
fcdd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fcde0 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 28 ig.pcache.xInit(
fcdf0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fce00 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29 fig.pcache.pArg)
fce10 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
fce20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fce30 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f cacheShutdown(vo
fce40 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 id){. if( sqlit
fce50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
fce60 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 cache.xShutdown
fce70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
fce80 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
fce90 65 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 e.xShutdown(sqli
fcea0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fceb0 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 20 20 pcache.pArg);.
fcec0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
fced0 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 n the size in by
fcee0 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 tes of a PCache
fcef0 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 object..*/.SQLIT
fcf00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
fcf10 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 lite3PcacheSize(
fcf20 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 void){ return si
fcf30 7a 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a zeof(PCache); }.
fcf40 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
fcf50 6e 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63 new PCache objec
fcf60 74 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63 65 t. Storage space
fcf70 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a to hold the obj
fcf80 65 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 ect.** has alrea
fcf90 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 dy been allocate
fcfa0 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 d and is passed
fcfb0 69 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e in as the p poin
fcfc0 74 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c ter. .** The cal
fcfd0 6c 65 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f ler discovers ho
fcfe0 77 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65 w much space nee
fcff0 64 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 ds to be allocat
fd000 65 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e ed by .** callin
fd010 67 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 g sqlite3PcacheS
fd020 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ize()..*/.SQLITE
fd030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
fd040 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
fd050 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 . int szPage,
fd060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd070 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 /* Size of every
fd080 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 page */. int s
fd090 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 zExtra,
fd0a0 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
fd0b0 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 space associate
fd0c0 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 d with each page
fd0d0 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 */. int bPurge
fd0e0 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 able,
fd0f0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 /* True if pa
fd100 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 ges are on backi
fd110 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e ng store */. in
fd120 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 t (*xStress)(voi
fd130 64 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61 d*,PgHdr*),/* Ca
fd140 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b ll to try to mak
fd150 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f e pages clean */
fd160 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 . void *pStress
fd170 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
fd180 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 /* Argument to x
fd190 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 Stress */. PCac
fd1a0 68 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 he *p
fd1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 /* Prea
fd1c0 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 llocated space f
fd1d0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f or the PCache */
fd1e0 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 .){. memset(p,
fd1f0 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 0, sizeof(PCache
fd200 29 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 ));. p->szPage
fd210 3d 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73 = szPage;. p->s
fd220 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61 zExtra = szExtra
fd230 3b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c ;. p->bPurgeabl
fd240 65 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a e = bPurgeable;.
fd250 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78 p->xStress = x
fd260 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74 Stress;. p->pSt
fd270 72 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a ress = pStress;.
fd280 20 20 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30 3b p->nMax = 100;
fd290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
fd2a0 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 the page size f
fd2b0 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 or PCache object
fd2c0 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 . The caller mus
fd2d0 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 t ensure that th
fd2e0 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 ere.** are no ou
fd2f0 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 tstanding page r
fd300 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e 20 74 eferences when t
fd310 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
fd320 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 called..*/.SQLIT
fd330 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
fd340 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 qlite3PcacheSetP
fd350 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a ageSize(PCache *
fd360 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 pCache, int szPa
fd370 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 ge){. assert( p
fd380 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 Cache->nRef==0 &
fd390 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 & pCache->pDirty
fd3a0 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 61 ==0 );. if( pCa
fd3b0 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
fd3c0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
fd3d0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 44 Config.pcache.xD
fd3e0 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 estroy(pCache->p
fd3f0 43 61 63 68 65 29 3b 0a 20 20 20 20 70 43 61 63 Cache);. pCac
fd400 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b 0a he->pCache = 0;.
fd410 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73 7a }. pCache->sz
fd420 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 7d Page = szPage;.}
fd430 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f ../*.** Try to o
fd440 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f btain a page fro
fd450 6d 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a m the cache..*/.
fd460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
fd470 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
fd480 46 65 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 Fetch(. PCache
fd490 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f *pCache, /
fd4a0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 * Obtain the pag
fd4b0 65 20 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 e from this cach
fd4c0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
fd4d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
fd4e0 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f Page number to o
fd4f0 62 74 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 btain */. int c
fd500 72 65 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20 reateFlag,
fd510 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 /* If true, cre
fd520 61 74 65 20 70 61 67 65 20 69 66 20 69 74 20 64 ate page if it d
fd530 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c oes not exist al
fd540 72 65 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72 ready */. PgHdr
fd550 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 20 20 **ppPage
fd560 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 /* Write the pa
fd570 67 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ge here */.){.
fd580 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30 PgHdr *pPage = 0
fd590 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65 3b ;. int eCreate;
fd5a0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 .. assert( pCac
fd5b0 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 he!=0 );. asser
fd5c0 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20 t( pgno>0 );..
fd5d0 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 61 /* If the plugga
fd5e0 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 74 ble cache (sqlit
fd5f0 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 20 e3_pcache*) has
fd600 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 not been allocat
fd610 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 ed,. ** allocat
fd620 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 e it now.. */.
fd630 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70 43 if( !pCache->pC
fd640 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46 6c ache && createFl
fd650 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ag ){. sqlite
fd660 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20 20 3_pcache *p;.
fd670 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 int nByte;.
fd680 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d 3e nByte = pCache->
fd690 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65 2d szPage + pCache-
fd6a0 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 6f >szExtra + sizeo
fd6b0 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70 20 f(PgHdr);. p
fd6c0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
fd6d0 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 72 onfig.pcache.xCr
fd6e0 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61 63 eate(nByte, pCac
fd6f0 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 3b he->bPurgeable);
fd700 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 . if( !p ){.
fd710 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
fd720 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
fd730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
fd740 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
fd750 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43 61 Cachesize(p, pCa
fd760 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 che->nMax);.
fd770 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d pCache->pCache =
fd780 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65 61 p;. }.. eCrea
fd790 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 20 te = createFlag
fd7a0 3f 20 31 20 3a 20 30 3b 0a 20 20 69 66 28 20 65 ? 1 : 0;. if( e
fd7b0 43 72 65 61 74 65 20 26 26 20 28 21 70 43 61 63 Create && (!pCac
fd7c0 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c he->bPurgeable |
fd7d0 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 | !pCache->pDirt
fd7e0 79 29 20 29 7b 0a 20 20 20 20 65 43 72 65 61 74 y) ){. eCreat
fd7f0 65 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 e = 2;. }. if(
fd800 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
fd810 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 ){. pPage = s
fd820 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fd830 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 ig.pcache.xFetch
fd840 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
fd850 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b pgno, eCreate);
fd860 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 . }.. if( !pPa
fd870 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 ge && eCreate==1
fd880 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 ){. PgHdr *p
fd890 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 Pg;.. /* Find
fd8a0 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f a dirty page to
fd8b0 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 write-out and r
fd8c0 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 ecycle. First tr
fd8d0 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 y to find a .
fd8e0 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f ** page that do
fd8f0 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 es not require a
fd900 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f journal-sync (o
fd910 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 ne with PGHDR_NE
fd920 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 ED_SYNC. ** c
fd930 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 leared), but if
fd940 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 that is not poss
fd950 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 ible settle for
fd960 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a any other . *
fd970 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 * unreferenced d
fd980 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a irty page.. *
fd990 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f /. expensive_
fd9a0 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 assert( pcacheCh
fd9b0 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 eckSynced(pCache
fd9c0 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 ) );. for(pPg
fd9d0 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 =pCache->pSynced
fd9e0 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 ; . pPg &
fd9f0 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 & (pPg->nRef ||
fda00 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
fda10 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a R_NEED_SYNC)); .
fda20 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d pPg=pPg-
fda30 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 >pDirtyPrev.
fda40 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 );. if( !pPg
fda50 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 ){. for(pPg
fda60 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 =pCache->pDirtyT
fda70 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d ail; pPg && pPg-
fda80 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e >nRef; pPg=pPg->
fda90 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 pDirtyPrev);.
fdaa0 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 }. if( pPg )
fdab0 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a {. int rc;.
fdac0 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 rc = pCach
fdad0 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 e->xStress(pCach
fdae0 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 e->pStress, pPg)
fdaf0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
fdb00 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
fdb10 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a =SQLITE_BUSY ){.
fdb20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
fdb30 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
fdb40 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 .. pPage = sq
fdb50 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
fdb60 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 g.pcache.xFetch(
fdb70 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
fdb80 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 pgno, 2);. }..
fdb90 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 if( pPage ){.
fdba0 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 if( !pPage->pD
fdbb0 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ata ){. mem
fdbc0 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 set(pPage, 0, si
fdbd0 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 zeof(PgHdr) + pC
fdbe0 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a ache->szExtra);.
fdbf0 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 pPage->pEx
fdc00 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 tra = (void*)&pP
fdc10 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 age[1];. pP
fdc20 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f age->pData = (vo
fdc30 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 id *)&((char *)p
fdc40 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 Page)[sizeof(PgH
fdc50 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a dr) + pCache->sz
fdc60 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 Extra];. pP
fdc70 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 age->pCache = pC
fdc80 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 ache;. pPag
fdc90 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a e->pgno = pgno;.
fdca0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
fdcb0 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d ( pPage->pCache=
fdcc0 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 =pCache );. a
fdcd0 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 ssert( pPage->pg
fdce0 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 no==pgno );.
fdcf0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
fdd00 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 Extra==(void *)&
fdd10 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 pPage[1] );..
fdd20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e if( 0==pPage->n
fdd30 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 Ref ){. pCa
fdd40 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 che->nRef++;.
fdd50 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 }. pPage->nR
fdd60 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 ef++;. if( pg
fdd70 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 no==1 ){. p
fdd80 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 Cache->pPage1 =
fdd90 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d pPage;. }. }
fdda0 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 . *ppPage = pPa
fddb0 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 ge;. return (pP
fddc0 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 age==0 && eCreat
fddd0 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 e) ? SQLITE_NOME
fdde0 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d M : SQLITE_OK;.}
fddf0 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e ../*.** Decremen
fde00 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 t the reference
fde10 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e count on a page.
fde20 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 If the page is
fde30 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a clean and the.**
fde40 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
fde50 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 drops to 0, the
fde60 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 n it is made eli
fde70 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e ble for recyclin
fde80 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
fde90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
fdea0 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 3PcacheRelease(P
fdeb0 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 gHdr *p){. asse
fdec0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
fded0 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 . p->nRef--;.
fdee0 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 if( p->nRef==0 )
fdef0 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 {. PCache *pC
fdf00 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 ache = p->pCache
fdf10 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 ;. pCache->nR
fdf20 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 ef--;. if( (p
fdf30 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
fdf40 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 RTY)==0 ){.
fdf50 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b pcacheUnpin(p);
fdf60 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
fdf70 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 /* Move the pa
fdf80 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f ge to the head o
fdf90 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 f the dirty list
fdfa0 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 . */. pcach
fdfb0 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
fdfc0 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 List(p);. p
fdfd0 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c cacheAddToDirtyL
fdfe0 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ist(p);. }.
fdff0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 }.}../*.** Incre
fe000 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 ase the referenc
fe010 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 e count of a sup
fe020 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 2e plied page by 1.
fe030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fe040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fe050 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 2a cacheRef(PgHdr *
fe060 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e p){. assert(p->
fe070 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 nRef>0);. p->nR
fe080 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ef++;.}../*.** D
fe090 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 rop a page from
fe0a0 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 the cache. There
fe0b0 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 must be exactly
fe0c0 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 one reference t
fe0d0 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 o the.** page. T
fe0e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c his function del
fe0f0 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 65 etes that refere
fe100 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 nce, so after it
fe110 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 returns the.**
fe120 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 page pointed to
fe130 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e by p is invalid.
fe140 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fe150 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fe160 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 cacheDrop(PgHdr
fe170 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 *p){. PCache *p
fe180 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 Cache;. assert(
fe190 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 p->nRef==1 );.
fe1a0 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 if( p->flags&PG
fe1b0 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 HDR_DIRTY ){.
fe1c0 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f pcacheRemoveFro
fe1d0 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 mDirtyList(p);.
fe1e0 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d }. pCache = p-
fe1f0 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 >pCache;. pCach
fe200 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 e->nRef--;. if(
fe210 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 p->pgno==1 ){.
fe220 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 pCache->pPage
fe230 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 1 = 0;. }. sql
fe240 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
fe250 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 .pcache.xUnpin(p
fe260 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
fe270 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d , 1);.}../*.** M
fe280 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 ake sure the pag
fe290 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 e is marked as d
fe2a0 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 irty. If it isn'
fe2b0 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c t dirty already,
fe2c0 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a .** make it so..
fe2d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
fe2e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
fe2f0 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 acheMakeDirty(Pg
fe300 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c Hdr *p){. p->fl
fe310 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f ags &= ~PGHDR_DO
fe320 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 NT_WRITE;. asse
fe330 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
fe340 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c . if( 0==(p->fl
fe350 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 ags & PGHDR_DIRT
fe360 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 Y) ){. p->fla
fe370 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 gs |= PGHDR_DIRT
fe380 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 Y;. pcacheAdd
fe390 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b ToDirtyList( p);
fe3a0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 . }.}../*.** Ma
fe3b0 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 ke sure the page
fe3c0 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c is marked as cl
fe3d0 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 ean. If it isn't
fe3e0 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a clean already,.
fe3f0 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a ** make it so..*
fe400 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fe410 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
fe420 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 cheMakeClean(PgH
fe430 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 dr *p){. if( (p
fe440 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f ->flags & PGHDR_
fe450 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 DIRTY) ){. pc
fe460 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
fe470 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 rtyList(p);.
fe480 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 p->flags &= ~(PG
fe490 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f HDR_DIRTY|PGHDR_
fe4a0 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 NEED_SYNC);.
fe4b0 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 if( p->nRef==0 )
fe4c0 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e {. pcacheUn
fe4d0 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 pin(p);. }.
fe4e0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 }.}../*.** Make
fe4f0 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 every page in th
fe500 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a e cache clean..*
fe510 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fe520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
fe530 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 cheCleanAll(PCac
fe540 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 he *pCache){. P
fe550 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 gHdr *p;. while
fe560 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 ( (p = pCache->p
fe570 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 Dirty)!=0 ){.
fe580 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
fe590 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a keClean(p);. }.
fe5a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 }../*.** Clear t
fe5b0 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
fe5c0 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c NC flag from all
fe5d0 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f dirty pages..*/
fe5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
fe5f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
fe600 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 heClearSyncFlags
fe610 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
fe620 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
fe630 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
fe640 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 irty; p; p=p->pD
fe650 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 irtyNext){. p
fe660 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 ->flags &= ~PGHD
fe670 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d R_NEED_SYNC;. }
fe680 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 . pCache->pSync
fe690 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 ed = pCache->pDi
fe6a0 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a rtyTail;.}../*.*
fe6b0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 * Change the pag
fe6c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
fe6d0 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a p to newPgno. .
fe6e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
fe6f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
fe700 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a acheMove(PgHdr *
fe710 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 p, Pgno newPgno)
fe720 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
fe730 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
fe740 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
fe750 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 f>0 );. assert(
fe760 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 newPgno>0 );.
fe770 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fe780 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 fig.pcache.xReke
fe790 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 y(pCache->pCache
fe7a0 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 , p, p->pgno, ne
fe7b0 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e wPgno);. p->pgn
fe7c0 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 o = newPgno;. i
fe7d0 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 f( (p->flags&PGH
fe7e0 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d DR_DIRTY) && (p-
fe7f0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
fe800 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 D_SYNC) ){. p
fe810 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 cacheRemoveFromD
fe820 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 irtyList(p);.
fe830 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 pcacheAddToDirt
fe840 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a yList(p);. }.}.
fe850 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 ./*.** Drop ever
fe860 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 y cache entry wh
fe870 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ose page number
fe880 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
fe890 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 "pgno". The.** c
fe8a0 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 aller must ensur
fe8b0 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 e that there are
fe8c0 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
fe8d0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e references to an
fe8e0 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 y pages.** other
fe8f0 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 than page 1 wit
fe900 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 h a page number
fe910 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e greater than pgn
fe920 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 o..**.** If ther
fe930 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 e is a reference
fe940 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 to page 1 and t
fe950 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 he pgno paramete
fe960 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 r passed to this
fe970 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 .** function is
fe980 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 0, then the data
fe990 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 area associated
fe9a0 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73 20 with page 1 is
fe9b0 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 zeroed, but.** t
fe9c0 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 he page object i
fe9d0 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a s not dropped..*
fe9e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fe9f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
fea00 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 cheTruncate(PCac
fea10 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f he *pCache, Pgno
fea20 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 pgno){. if( pC
fea30 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
fea40 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 PgHdr *p;.
fea50 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a PgHdr *pNext;.
fea60 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 for(p=pCache
fea70 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 ->pDirty; p; p=p
fea80 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 Next){. pNe
fea90 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 xt = p->pDirtyNe
feaa0 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d xt;. if( p-
feab0 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 >pgno>pgno ){.
feac0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
fead0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
feae0 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 TY );. sq
feaf0 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
feb00 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d lean(p);. }
feb10 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
feb20 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 gno==0 && pCache
feb30 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 ->pPage1 ){.
feb40 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d memset(pCache-
feb50 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 >pPage1->pData,
feb60 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 0, pCache->szPag
feb70 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d e);. pgno =
feb80 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 1;. }. sq
feb90 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
feba0 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 g.pcache.xTrunca
febb0 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 te(pCache->pCach
febc0 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a e, pgno+1);. }.
febd0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
febe0 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
febf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
fec00 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 qlite3PcacheClos
fec10 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 e(PCache *pCache
fec20 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d ){. if( pCache-
fec30 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 >pCache ){. s
fec40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fec50 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 ig.pcache.xDestr
fec60 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 oy(pCache->pCach
fec70 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a e);. }.}../* .*
fec80 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f * Discard the co
fec90 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 ntents of the ca
feca0 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
fecb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
fecc0 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 te3PcacheClear(P
fecd0 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
fece0 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 sqlite3PcacheT
fecf0 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 runcate(pCache,
fed00 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 0);.}../*.** Mer
fed10 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 ge two lists of
fed20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 pages connected
fed30 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e by pDirty and in
fed40 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 pgno order..**
fed50 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 Do not both fixi
fed60 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65 ng the pDirtyPre
fed70 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 v pointers..*/.s
fed80 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
fed90 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
feda0 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 t(PgHdr *pA, PgH
fedb0 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 dr *pB){. PgHdr
fedc0 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b result, *pTail;
fedd0 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 . pTail = &resu
fede0 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 lt;. while( pA
fedf0 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 && pB ){. if(
fee00 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 pA->pgno<pB->pg
fee10 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 no ){. pTai
fee20 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a l->pDirty = pA;.
fee30 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 pTail = pA
fee40 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d ;. pA = pA-
fee50 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c >pDirty;. }el
fee60 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d se{. pTail-
fee70 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 >pDirty = pB;.
fee80 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a pTail = pB;.
fee90 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 pB = pB->p
feea0 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d Dirty;. }. }
feeb0 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 . if( pA ){.
feec0 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d pTail->pDirty =
feed0 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 pA;. }else if(
feee0 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c pB ){. pTail
feef0 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 ->pDirty = pB;.
fef00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 }else{. pTai
fef10 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 l->pDirty = 0;.
fef20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 }. return resu
fef30 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a lt.pDirty;.}../*
fef40 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 .** Sort the lis
fef50 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 t of pages in ac
fef60 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 cending order by
fef70 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 pgno. Pages ar
fef80 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 e.** connected b
fef90 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 y pDirty pointer
fefa0 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72 s. The pDirtyPr
fefb0 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a ev pointers are.
fefc0 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 ** corrupted by
fefd0 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 this sort..*/.#d
fefe0 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 efine N_SORT_BUC
feff0 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 KET_ALLOC 25.#de
ff000 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b fine N_SORT_BUCK
ff010 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 ET 25.#ifd
ff020 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
ff030 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 int sqlite3_pag
ff040 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 er_n_sort_bucket
ff050 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e = 0;. #undef N
ff060 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 _SORT_BUCKET. #
ff070 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 define N_SORT_BU
ff080 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 CKET \. (sqlit
ff090 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f e3_pager_n_sort_
ff0a0 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 bucket?sqlite3_p
ff0b0 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b ager_n_sort_buck
ff0c0 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 et:N_SORT_BUCKET
ff0d0 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 _ALLOC).#endif.s
ff0e0 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
ff0f0 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 cheSortDirtyList
ff100 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 (PgHdr *pIn){.
ff110 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f PgHdr *a[N_SORT_
ff120 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a BUCKET_ALLOC], *
ff130 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 p;. int i;. me
ff140 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f mset(a, 0, sizeo
ff150 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 f(a));. while(
ff160 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 pIn ){. p = p
ff170 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d In;. pIn = p-
ff180 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e >pDirty;. p->
ff190 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 pDirty = 0;.
ff1a0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 for(i=0; i<N_SOR
ff1b0 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 T_BUCKET-1; i++)
ff1c0 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d {. if( a[i]
ff1d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ==0 ){. a
ff1e0 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 [i] = p;.
ff1f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 break;. }e
ff200 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d lse{. p =
ff210 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 pcacheMergeDirt
ff220 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a yList(a[i], p);.
ff230 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 a[i] = 0
ff240 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
ff250 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 if( i==N_SOR
ff260 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 T_BUCKET-1 ){.
ff270 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a /* Coverage:
ff280 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 To get here, th
ff290 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 ere need to be 2
ff2a0 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 ^(N_SORT_BUCKET)
ff2b0 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 . ** eleme
ff2c0 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 nts in the input
ff2d0 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 list. This is p
ff2e0 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 ossible, but imp
ff2f0 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 ractical..
ff300 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 ** Testing this
ff310 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e line is the poin
ff320 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 t of global vari
ff330 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 able. ** sq
ff340 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f lite3_pager_n_so
ff350 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 rt_bucket..
ff360 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d */. a[i] =
ff370 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 pcacheMergeDirt
ff380 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a yList(a[i], p);.
ff390 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 }. }. p =
ff3a0 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b a[0];. for(i=1;
ff3b0 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 i<N_SORT_BUCKET
ff3c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 ; i++){. p =
ff3d0 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 pcacheMergeDirty
ff3e0 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 List(p, a[i]);.
ff3f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
ff400 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
ff410 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 list of all dir
ff420 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
ff430 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 cache, sorted by
ff440 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f page number..*/
ff450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ff460 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 PgHdr *sqlite3Pc
ff470 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 acheDirtyList(PC
ff480 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
ff490 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 PgHdr *p;. for
ff4a0 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
ff4b0 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
ff4c0 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 yNext){. p->p
ff4d0 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 Dirty = p->pDirt
ff4e0 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 yNext;. }. ret
ff4f0 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69 urn pcacheSortDi
ff500 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e rtyList(pCache->
ff510 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a pDirty);.}../* .
ff520 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f ** Return the to
ff530 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 tal number of re
ff540 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 68 ferenced pages h
ff550 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 65 eld by the cache
ff560 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
ff570 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
ff580 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 cacheRefCount(PC
ff590 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
ff5a0 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e return pCache->
ff5b0 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 nRef;.}../*.** R
ff5c0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
ff5d0 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
ff5e0 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70 6c o the page suppl
ff5f0 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 ied as an argume
ff600 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
ff610 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ff620 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 3PcachePageRefco
ff630 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 unt(PgHdr *p){.
ff640 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b return p->nRef;
ff650 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 .}../* .** Retur
ff660 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 n the total numb
ff670 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
ff680 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c he cache..*/.SQL
ff690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ff6a0 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 sqlite3PcachePag
ff6b0 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 ecount(PCache *p
ff6c0 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 Cache){. int nP
ff6d0 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 age = 0;. if( p
ff6e0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
ff6f0 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c . nPage = sql
ff700 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
ff710 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 .pcache.xPagecou
ff720 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 nt(pCache->pCach
ff730 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
ff740 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 nPage;.}..#ifde
ff750 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
ff760 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67 67 .** Get the sugg
ff770 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 ested cache-size
ff780 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
ff790 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ff7a0 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 lite3PcacheGetCa
ff7b0 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a chesize(PCache *
ff7c0 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 pCache){. retur
ff7d0 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a n pCache->nMax;.
ff7e0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
ff7f0 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65 Set the suggeste
ff800 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c d cache-size val
ff810 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
ff820 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ff830 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 e3PcacheSetCache
ff840 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 size(PCache *pCa
ff850 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 che, int mxPage)
ff860 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 {. pCache->nMax
ff870 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 = mxPage;. if(
ff880 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
ff890 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
ff8a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
ff8b0 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 61 e.xCachesize(pCa
ff8c0 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 che->pCache, mxP
ff8d0 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 age);. }.}..#if
ff8e0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
ff8f0 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 _PAGES./*.** For
ff900 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
ff910 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 currently in th
ff920 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20 e cache, invoke
ff930 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
ff940 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20 callback. This
ff950 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
ff960 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b the SQLITE_CHECK
ff970 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a _PAGES macro is.
ff980 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 ** defined..*/.S
ff990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ff9a0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
ff9b0 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61 IterateDirty(PCa
ff9c0 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 che *pCache, voi
ff9d0 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 d (*xIter)(PgHdr
ff9e0 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 *)){. PgHdr *p
ff9f0 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69 Dirty;. for(pDi
ffa00 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 rty=pCache->pDir
ffa10 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72 ty; pDirty; pDir
ffa20 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74 ty=pDirty->pDirt
ffa30 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 yNext){. xIte
ffa40 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d r(pDirty);. }.}
ffa50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
ffa60 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
ffa70 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a pcache.c *******
ffa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffaa0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
ffab0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
ffac0 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a ile pcache1.c **
ffad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffaf0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
ffb00 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a 008 November 05.
ffb10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
ffb20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
ffb30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
ffb40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
ffb50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
ffb60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
ffb70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
ffb80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
ffb90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
ffba0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
ffbb0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
ffbc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
ffbd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
ffbe0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
ffbf0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
ffc00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
ffc10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
ffc20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
ffc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
ffc70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
ffc80 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 mplements the de
ffc90 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
ffca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
ffcb0 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f (the.** sqlite3_
ffcc0 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 pcache interface
ffcd0 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 ). It also conta
ffce0 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 ins part of the
ffcf0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
ffd00 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f * of the SQLITE_
ffd10 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
ffd20 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c and sqlite3_rel
ffd30 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65 ease_memory() fe
ffd40 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68 atures..** If th
ffd50 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 e default page c
ffd60 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ache implementat
ffd70 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e ion is overriden
ffd80 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f , then neither o
ffd90 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66 f.** these two f
ffda0 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69 eatures are avai
ffdb0 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 lable..**.** @(#
ffdc0 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63 ) $Id: pcache1.c
ffdd0 2c 76 20 31 2e 31 31 2e 32 2e 31 20 32 30 30 39 ,v 1.11.2.1 2009
ffde0 2f 30 35 2f 31 38 20 31 36 3a 31 34 3a 32 35 20 /05/18 16:14:25
ffdf0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 drh Exp $.*/...t
ffe00 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 ypedef struct PC
ffe10 61 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 ache1 PCache1;.t
ffe20 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 ypedef struct Pg
ffe30 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 Hdr1 PgHdr1;.typ
ffe40 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 edef struct PgFr
ffe50 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f eeslot PgFreeslo
ffe60 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 t;../* Pointers
ffe70 74 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 to structures of
ffe80 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 this type are c
ffe90 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 ast and returned
ffea0 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 as .** opaque s
ffeb0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 qlite3_pcache* h
ffec0 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 andles.*/.struct
ffed0 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 PCache1 {. /*
ffee0 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 Cache configurat
ffef0 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 ion parameters.
fff00 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 Page size (szPag
fff10 65 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 e) and the purge
fff20 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 able. ** flag (
fff30 62 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 bPurgeable) are
fff40 73 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 set when the cac
fff50 68 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e he is created. n
fff60 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a Max may be . **
fff70 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 modified at any
fff80 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 time by a call
fff90 74 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 to the pcache1Ca
fffa0 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 cheSize() method
fffb0 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .. ** The globa
fffc0 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
fffd0 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 held when access
fffe0 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 ing nMax.. */.
ffff0 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 int szPage;
10000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10001 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
10002 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 allocated pages
10003 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e in bytes */. in
10004 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 t bPurgeable;
10005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10006 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 /* True if cac
10007 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 he is purgeable
10008 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
10009 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 t nMin;
1000a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 /* Mini
1000b 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 mum number of pa
1000c 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a ges reserved */.
1000d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
1000e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Max;
1000f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
10010 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 red "cache_size"
10011 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 value */.. /*
10012 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c Hash table of al
10013 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c l pages. The fol
10014 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 lowing variables
10015 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 may only be acc
10016 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 essed. ** when
10017 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 the accessor is
10018 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 holding the glob
10019 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 al mutex (see pc
1001a 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
1001b 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 ) . ** and pcac
1001c 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 he1LeaveMutex())
1001d 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 .. */. unsigne
1001e 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c d int nRecyclabl
1001f 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
10020 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
10021 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 in the LRU list
10022 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
10023 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 t nPage;
10024 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
10025 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
10026 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 s in apHash */.
10027 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 unsigned int nH
10028 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 ash;
10029 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1002a 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 f slots in apHas
1002b 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 h[] */. PgHdr1
1002c 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 **apHash;
1002d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1002e 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 Hash table for f
1002f 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 ast lookup by ke
10030 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 y */.. unsigned
10031 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 int iMaxKey;
10032 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
10033 61 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 argest key seen
10034 73 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 since xTruncate(
10035 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 ) */.};../*.** E
10036 61 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 ach cache entry
10037 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 is represented b
10038 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
10039 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a the following .
1003a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 ** structure. A
1003b 62 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 buffer of PgHdr1
1003c 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 .pCache->szPage
1003d 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 bytes is allocat
1003e 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 ed .** directly
1003f 61 66 74 65 72 20 74 68 65 20 73 74 72 75 63 74 after the struct
10040 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73 ure in memory (s
10041 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f ee the PGHDR1_TO
10042 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72 _PAGE() .** macr
10043 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72 o below)..*/.str
10044 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75 uct PgHdr1 {. u
10045 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 nsigned int iKey
10046 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
10047 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65 Key value (page
10048 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67 number) */. Pg
10049 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20 Hdr1 *pNext;
1004a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1004b 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62 Next in hash tab
1004c 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43 le chain */. PC
1004d 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 ache1 *pCache;
1004e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1004f 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65 Cache that curre
10050 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70 ntly owns this p
10051 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 age */. PgHdr1
10052 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20 *pLruNext;
10053 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
10054 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 in LRU list of u
10055 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f npinned pages */
10056 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50 . PgHdr1 *pLruP
10057 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 rev;
10058 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e /* Previous in
10059 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 LRU list of unp
1005a 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d inned pages */.}
1005b 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c ;../*.** Free sl
1005c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63 ots in the alloc
1005d 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76 ator used to div
1005e 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65 ide up the buffe
1005f 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67 r provided using
10060 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43 .** the SQLITE_C
10061 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 ONFIG_PAGECACHE
10062 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 mechanism..*/.st
10063 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 ruct PgFreeslot
10064 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a {. PgFreeslot *
10065 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
10066 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a free slot */.};.
10067 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61 ./*.** Global da
10068 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20 ta used by this
10069 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 cache..*/.static
1006a 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
1006b 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20 ct PCacheGlobal
1006c 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
1006d 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 x *mutex;
1006e 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69 /* stati
1006f 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54 c mutex MUTEX_ST
10070 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69 ATIC_LRU */.. i
10071 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 nt nMaxPage;
10072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10073 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61 /* Sum of nMa
10074 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 xPage for purgea
10075 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 ble caches */.
10076 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20 int nMinPage;
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10078 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d /* Sum of nM
10079 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 inPage for purge
1007a 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 able caches */.
1007b 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 int nCurrentPag
1007c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1007d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1007e 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 f purgeable page
1007f 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 s allocated */.
10080 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61 PgHdr1 *pLruHea
10081 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20 d, *pLruTail;
10082 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 /* LRU list
10083 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 of unpinned pag
10084 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69 es */.. /* Vari
10085 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f ables related to
10086 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 SQLITE_CONFIG_P
10087 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67 AGECACHE setting
10088 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c s. */. int szSl
10089 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ot;
1008a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1008b 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 ize of each free
1008c 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20 slot */. void
1008d 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 *pStart, *pEnd;
1008e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1008f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65 * Bounds of page
10090 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e cache malloc ran
10091 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c ge */. PgFreesl
10092 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 ot *pFree;
10093 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
10094 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 ree page blocks
10095 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a */.} pcache1_g;.
10096 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 ./*.** All code
10097 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f in this file sho
10098 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67 uld access the g
10099 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 lobal structure
1009a 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a above via the.**
1009b 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22 alias "pcache1"
1009c 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 . This ensures t
1009d 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c hat the WSD emul
1009e 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 ation is used wh
1009f 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 en.** compiling
100a0 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 for systems that
100a1 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
100a2 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 real WSD..*/.#de
100a3 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c fine pcache1 (GL
100a4 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 OBAL(struct PCac
100a5 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 heGlobal, pcache
100a6 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 1_g))../*.** Whe
100a7 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 n a PgHdr1 struc
100a8 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 ture is allocate
100a9 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 d, the associate
100aa 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 d PCache1.szPage
100ab 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74 .** bytes of dat
100ac 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69 a are located di
100ad 72 65 63 74 6c 79 20 61 66 74 65 72 20 69 74 20 rectly after it
100ae 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20 in memory (i.e.
100af 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a the total.** siz
100b0 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
100b1 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67 ion is sizeof(Pg
100b2 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a Hdr1)+PCache1.sz
100b3 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a Page byte). The.
100b4 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 ** PGHDR1_TO_PAG
100b5 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 E() macro takes
100b6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 a pointer to a P
100b7 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 gHdr1 structure
100b8 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e as.** an argumen
100b9 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 t and returns a
100ba 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 pointer to the a
100bb 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20 ssociated block
100bc 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74 of szPage.** byt
100bd 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f es. The PAGE_TO_
100be 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64 PGHDR1() macro d
100bf 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65 oes the opposite
100c0 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 : its argument i
100c1 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
100c2 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50 o a block of szP
100c3 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 age bytes of dat
100c4 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e a and the return
100c5 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70 value is.** a p
100c6 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73 ointer to the as
100c7 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20 sociated PgHdr1
100c8 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
100c9 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52 assert( PGHDR
100ca 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54 1_TO_PAGE(PAGE_T
100cb 4f 5f 50 47 48 44 52 31 28 58 29 29 3d 3d 58 20 O_PGHDR1(X))==X
100cc 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 );.*/.#define PG
100cd 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29 20 HDR1_TO_PAGE(p)
100ce 28 76 6f 69 64 20 2a 29 28 26 28 28 75 6e 73 69 (void *)(&((unsi
100cf 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b 73 gned char *)p)[s
100d0 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29 0a izeof(PgHdr1)]).
100d1 23 64 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f #define PAGE_TO_
100d2 50 47 48 44 52 31 28 70 29 20 28 50 67 48 64 72 PGHDR1(p) (PgHdr
100d3 31 20 2a 29 28 26 28 28 75 6e 73 69 67 6e 65 64 1 *)(&((unsigned
100d4 20 63 68 61 72 20 2a 29 70 29 5b 2d 31 2a 28 69 char *)p)[-1*(i
100d5 6e 74 29 73 69 7a 65 6f 66 28 50 67 48 64 72 31 nt)sizeof(PgHdr1
100d6 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f )])../*.** Macro
100d7 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c s to enter and l
100d8 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 eave the global
100d9 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 LRU mutex..*/.#d
100da 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 efine pcache1Ent
100db 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 erMutex() sqlite
100dc 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 3_mutex_enter(pc
100dd 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 ache1.mutex).#de
100de 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 fine pcache1Leav
100df 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 eMutex() sqlite3
100e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 _mutex_leave(pca
100e1 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a che1.mutex)../**
100e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
100e7 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c ******* Page All
100e8 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 ocation/SQLITE_C
100e9 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c ONFIG_PCACHE Rel
100ea 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a ated Functions *
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
100ec 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
100ed 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 ion is called du
100ee 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
100ef 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 ion if a static
100f0 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 buffer is .** su
100f1 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f pplied to use fo
100f2 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 r the page-cache
100f3 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 by passing the
100f4 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
100f5 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 GECACHE.** verb
100f6 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 to sqlite3_confi
100f7 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 g(). Parameter p
100f8 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e Buf points to an
100f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
100fa 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 e.** enough to c
100fb 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 ontain 'n' buffe
100fc 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 rs of 'sz' bytes
100fd 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 each..*/.SQLITE
100fe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
100ff 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 lite3PCacheBuffe
10100 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 rSetup(void *pBu
10101 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e f, int sz, int n
10102 29 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 ){. PgFreeslot
10103 2a 70 3b 0a 20 20 73 7a 20 3d 20 52 4f 55 4e 44 *p;. sz = ROUND
10104 44 4f 57 4e 38 28 73 7a 29 3b 0a 20 20 70 63 61 DOWN8(sz);. pca
10105 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a che1.szSlot = sz
10106 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 ;. pcache1.pSta
10107 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 70 63 61 rt = pBuf;. pca
10108 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a che1.pFree = 0;.
10109 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a while( n-- ){.
1010a 20 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73 p = (PgFrees
1010b 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 70 lot*)pBuf;. p
1010c 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 ->pNext = pcache
1010d 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 1.pFree;. pca
1010e 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 3b 0a che1.pFree = p;.
1010f 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 pBuf = (void
10110 2a 29 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 *)&((char*)pBuf)
10111 5b 73 7a 5d 3b 0a 20 20 7d 0a 20 20 70 63 61 63 [sz];. }. pcac
10112 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b he1.pEnd = pBuf;
10113 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 .}../*.** Malloc
10114 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 function used w
10115 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ithin this file
10116 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 to allocate spac
10117 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 e from the buffe
10118 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 r.** configured
10119 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f using sqlite3_co
1011a 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 nfig(SQLITE_CONF
1011b 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 IG_PAGECACHE) op
1011c 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20 tion. If no .**
1011d 73 75 63 68 20 62 75 66 66 65 72 20 65 78 69 73 such buffer exis
1011e 74 73 20 6f 72 20 74 68 65 72 65 20 69 73 20 6e ts or there is n
1011f 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 o space left in
10120 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f it, this functio
10121 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b n falls .** back
10122 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f to sqlite3Mallo
10123 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
10124 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f oid *pcache1Allo
10125 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 c(int nByte){.
10126 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 void *p;. asser
10127 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
10128 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 _held(pcache1.mu
10129 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 6e 42 tex) );. if( nB
1012a 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53 yte<=pcache1.szS
1012b 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31 2e 70 lot && pcache1.p
1012c 46 72 65 65 20 29 7b 0a 20 20 20 20 70 20 3d 20 Free ){. p =
1012d 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 (PgHdr1 *)pcache
1012e 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 1.pFree;. pca
1012f 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61 che1.pFree = pca
10130 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78 che1.pFree->pNex
10131 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 t;. sqlite3St
10132 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
10133 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10134 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20 SIZE, nByte);.
10135 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
10136 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
10137 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c _PAGECACHE_USED,
10138 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 1);. }else{..
10139 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /* Allocate a
1013a 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e new buffer usin
1013b 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e g sqlite3Malloc.
1013c 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f Before doing so
1013d 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a , exit the. *
1013e 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20 * global pcache
1013f 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b mutex and unlock
10140 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
10141 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20 object pCache.
10142 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20 This is . **
10143 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61 so that if the a
10144 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
10145 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 te a new buffer
10146 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a causes the the .
10147 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 ** configure
10148 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69 d soft-heap-limi
10149 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64 t to be breached
1014a 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73 , it will be pos
1014b 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20 sible to. **
1014c 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66 reclaim memory f
1014d 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63 rom this pager-c
1014e 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ache.. */.
1014f 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10150 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 ex();. p = sq
10151 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 lite3Malloc(nByt
10152 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 e);. pcache1E
10153 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
10154 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
10155 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33 int sz = sqlite3
10156 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
10157 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
10158 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
10159 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
1015a 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 ERFLOW, sz);.
1015b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1015c 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
1015d 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 an allocated bu
1015e 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 ffer obtained fr
1015f 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 om pcache1Alloc(
10160 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10161 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f d pcache1Free(vo
10162 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
10163 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
10164 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 held(pcache1.mut
10165 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d ex) );. if( p==
10166 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
10167 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74 ( p>=pcache1.pSt
10168 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31 art && p<pcache1
10169 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46 .pEnd ){. PgF
1016a 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a reeslot *pSlot;.
1016b 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
1016c 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
1016d 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
1016e 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f D, -1);. pSlo
1016f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a t = (PgFreeslot*
10170 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70 )p;. pSlot->p
10171 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 Next = pcache1.p
10172 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 Free;. pcache
10173 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b 1.pFree = pSlot;
10174 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
10175 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 t iSize = sqlite
10176 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
10177 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10178 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10179 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
1017a 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a RFLOW, -iSize);.
1017b 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
1017c 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
1017d 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
1017e 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69 page object ini
1017f 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 tially associate
10180 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61 d with cache pCa
10181 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 che..*/.static P
10182 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c gHdr1 *pcache1Al
10183 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20 locPage(PCache1
10184 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 *pCache){. int
10185 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50 nByte = sizeof(P
10186 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d gHdr1) + pCache-
10187 3e 73 7a 50 61 67 65 3b 0a 20 20 50 67 48 64 72 >szPage;. PgHdr
10188 31 20 2a 70 20 3d 20 28 50 67 48 64 72 31 20 2a 1 *p = (PgHdr1 *
10189 29 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 6e 42 )pcache1Alloc(nB
1018a 79 74 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b yte);. if( p ){
1018b 0a 20 20 20 20 69 66 28 20 70 43 61 63 68 65 2d . if( pCache-
1018c 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 >bPurgeable ){.
1018d 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75 pcache1.nCu
1018e 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 20 rrentPage++;.
1018f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
10190 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10191 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 a page object a
10192 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 llocated by pcac
10193 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a he1AllocPage()..
10194 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10195 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 50 cache1FreePage(P
10196 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66 28 gHdr1 *p){. if(
10197 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
10198 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 >pCache->bPurgea
10199 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 ble ){. pca
1019a 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 che1.nCurrentPag
1019b 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 e--;. }. p
1019c 63 61 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20 cache1Free(p);.
1019d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c }.}../*.** Mall
1019e 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 oc function used
1019f 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 6f 62 by SQLite to ob
101a0 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f 6d 20 tain space from
101a1 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e 66 69 the buffer confi
101a2 67 75 72 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 gured.** using s
101a3 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
101a4 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
101a5 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 CACHE) option. I
101a6 66 20 6e 6f 20 73 75 63 68 20 62 75 66 66 65 72 f no such buffer
101a7 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 69 73 .** exists, this
101a8 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 function falls
101a9 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d back to sqlite3M
101aa 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
101ab 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
101ac 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c *sqlite3PageMall
101ad 6f 63 28 69 6e 74 20 73 7a 29 7b 0a 20 20 76 6f oc(int sz){. vo
101ae 69 64 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 31 id *p;. pcache1
101af 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
101b0 70 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 p = pcache1Alloc
101b1 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31 4c (sz);. pcache1L
101b2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
101b3 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
101b4 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 * Free an alloca
101b5 74 65 64 20 62 75 66 66 65 72 20 6f 62 74 61 69 ted buffer obtai
101b6 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
101b7 50 61 67 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f PageMalloc()..*/
101b8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
101b9 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
101ba 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Free(void *p){.
101bb 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
101bc 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 46 ex();. pcache1F
101bd 72 65 65 28 70 29 3b 0a 20 20 70 63 61 63 68 65 ree(p);. pcache
101be 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 1LeaveMutex();.}
101bf 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
101c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c4 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e */./******** Gen
101c5 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 74 eral Implementat
101c6 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a ion Functions **
101c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c9 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 **/../*.** This
101ca 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
101cb 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20 68 to resize the h
101cc 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20 62 ash table used b
101cd 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73 73 y the cache pass
101ce 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 ed.** as the fir
101cf 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a st argument..**.
101d0 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 ** The global mu
101d1 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 tex must be held
101d2 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
101d3 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ion is called..*
101d4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 /.static int pca
101d5 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 50 che1ResizeHash(P
101d6 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50 67 Cache1 *p){. Pg
101d7 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 20 Hdr1 **apNew;.
101d8 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e 65 unsigned int nNe
101d9 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e w;. unsigned in
101da 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 t i;.. assert(
101db 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
101dc 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 ld(pcache1.mutex
101dd 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20 70 ) );.. nNew = p
101de 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 20 69 66 28 ->nHash*2;. if(
101df 20 6e 4e 65 77 3c 32 35 36 20 29 7b 0a 20 20 20 nNew<256 ){.
101e0 20 6e 4e 65 77 20 3d 20 32 35 36 3b 0a 20 20 7d nNew = 256;. }
101e1 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 .. pcache1Leave
101e2 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70 Mutex();. if( p
101e3 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 74 ->nHash ){ sqlit
101e4 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
101e5 6c 6f 63 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77 loc(); }. apNew
101e6 20 3d 20 28 50 67 48 64 72 31 20 2a 2a 29 73 71 = (PgHdr1 **)sq
101e7 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a lite3_malloc(siz
101e8 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e eof(PgHdr1 *)*nN
101e9 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 ew);. if( p->nH
101ea 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45 6e ash ){ sqlite3En
101eb 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
101ec 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 }. pcache1Ente
101ed 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 rMutex();. if(
101ee 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d apNew ){. mem
101ef 73 65 74 28 61 70 4e 65 77 2c 20 30 2c 20 73 69 set(apNew, 0, si
101f0 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e zeof(PgHdr1 *)*n
101f1 4e 65 77 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d New);. for(i=
101f2 30 3b 20 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 0; i<p->nHash; i
101f3 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 48 64 72 ++){. PgHdr
101f4 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 1 *pPage;.
101f5 50 67 48 64 72 31 20 2a 70 4e 65 78 74 20 3d 20 PgHdr1 *pNext =
101f6 70 2d 3e 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20 p->apHash[i];.
101f7 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 while( (pPag
101f8 65 20 3d 20 70 4e 65 78 74 29 21 3d 30 20 29 7b e = pNext)!=0 ){
101f9 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 . unsigne
101fa 64 20 69 6e 74 20 68 20 3d 20 70 50 61 67 65 2d d int h = pPage-
101fb 3e 69 4b 65 79 20 25 20 6e 4e 65 77 3b 0a 20 20 >iKey % nNew;.
101fc 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 50 pNext = pP
101fd 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 age->pNext;.
101fe 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 pPage->pNext
101ff 20 3d 20 61 70 4e 65 77 5b 68 5d 3b 0a 20 20 20 = apNew[h];.
10200 20 20 20 20 20 61 70 4e 65 77 5b 68 5d 20 3d 20 apNew[h] =
10201 70 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 20 pPage;. }.
10202 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
10203 5f 66 72 65 65 28 70 2d 3e 61 70 48 61 73 68 29 _free(p->apHash)
10204 3b 0a 20 20 20 20 70 2d 3e 61 70 48 61 73 68 20 ;. p->apHash
10205 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e = apNew;. p->
10206 6e 48 61 73 68 20 3d 20 6e 4e 65 77 3b 0a 20 20 nHash = nNew;.
10207 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e }.. return (p->
10208 61 70 48 61 73 68 20 3f 20 53 51 4c 49 54 45 5f apHash ? SQLITE_
10209 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 OK : SQLITE_NOME
1020a 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 M);.}../*.** Thi
1020b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
1020c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f ed internally to
1020d 20 72 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 remove the page
1020e 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 20 pPage from the
1020f 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c .** global LRU l
10210 69 73 74 2c 20 69 66 20 69 73 20 70 61 72 74 20 ist, if is part
10211 6f 66 20 69 74 2e 20 49 66 20 70 50 61 67 65 20 of it. If pPage
10212 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 is not part of t
10213 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52 55 he global.** LRU
10214 20 6c 69 73 74 2c 20 74 68 65 6e 20 74 68 69 73 list, then this
10215 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
10216 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o-op..**.** The
10217 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 global mutex mus
10218 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 t be held when t
10219 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1021a 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
1021b 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50 69 c void pcache1Pi
1021c 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70 50 nPage(PgHdr1 *pP
1021d 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 age){. assert(
1021e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1021f 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 ld(pcache1.mutex
10220 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ) );. if( pPage
10221 20 26 26 20 28 70 50 61 67 65 2d 3e 70 4c 72 75 && (pPage->pLru
10222 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 Next || pPage==p
10223 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 cache1.pLruTail)
10224 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
10225 65 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a 20 e->pLruPrev ){.
10226 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 pPage->pLru
10227 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Prev->pLruNext =
10228 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10229 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1022a 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
1022b 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e ){. pPage->
1022c 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 pLruNext->pLruPr
1022d 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 ev = pPage->pLru
1022e 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
1022f 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 if( pcache1.pLru
10230 48 65 61 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 Head==pPage ){.
10231 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 pcache1.pLr
10232 75 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 uHead = pPage->p
10233 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 LruNext;. }.
10234 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 if( pcache1.p
10235 4c 72 75 54 61 69 6c 3d 3d 70 50 61 67 65 20 29 LruTail==pPage )
10236 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10237 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 pLruTail = pPage
10238 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 ->pLruPrev;.
10239 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 }. pPage->pLr
1023a 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 uNext = 0;. p
1023b 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d Page->pLruPrev =
1023c 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 0;. pPage->p
1023d 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 Cache->nRecyclab
1023e 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a le--;. }.}.../*
1023f 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 .** Remove the p
10240 61 67 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 age supplied as
10241 61 6e 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d an argument from
10242 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
10243 0a 2a 2a 20 28 50 43 61 63 68 65 31 2e 61 70 48 .** (PCache1.apH
10244 61 73 68 20 73 74 72 75 63 74 75 72 65 29 20 74 ash structure) t
10245 68 61 74 20 69 74 20 69 73 20 63 75 72 72 65 6e hat it is curren
10246 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 2e 0a 2a tly stored in..*
10247 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 *.** The global
10248 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
10249 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
1024a 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
1024b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1024c 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f pcache1RemoveFro
1024d 6d 48 61 73 68 28 50 67 48 64 72 31 20 2a 70 50 mHash(PgHdr1 *pP
1024e 61 67 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 age){. unsigned
1024f 20 69 6e 74 20 68 3b 0a 20 20 50 43 61 63 68 65 int h;. PCache
10250 31 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61 67 1 *pCache = pPag
10251 65 2d 3e 70 43 61 63 68 65 3b 0a 20 20 50 67 48 e->pCache;. PgH
10252 64 72 31 20 2a 2a 70 70 3b 0a 0a 20 20 68 20 3d dr1 **pp;.. h =
10253 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 70 pPage->iKey % p
10254 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10255 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d 3e for(pp=&pCache->
10256 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70 29 apHash[h]; (*pp)
10257 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a 70 !=pPage; pp=&(*p
10258 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a 70 p)->pNext);. *p
10259 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74 p = (*pp)->pNext
1025a 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 ;.. pCache->nPa
1025b 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ge--;.}../*.** I
1025c 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72 72 f there are curr
1025d 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 ently more than
1025e 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 20 pcache.nMaxPage
1025f 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 2c pages allocated,
10260 20 74 72 79 0a 2a 2a 20 74 6f 20 72 65 63 79 63 try.** to recyc
10261 6c 65 20 70 61 67 65 73 20 74 6f 20 72 65 64 75 le pages to redu
10262 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 61 6c ce the number al
10263 6c 6f 63 61 74 65 64 20 74 6f 20 70 63 61 63 68 located to pcach
10264 65 2e 6e 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a 73 e.nMaxPage..*/.s
10265 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10266 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 e1EnforceMaxPage
10267 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 (void){. assert
10268 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
10269 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 held(pcache1.mut
1026a 65 78 29 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 ex) );. while(
1026b 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
1026c 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 Page>pcache1.nMa
1026d 78 50 61 67 65 20 26 26 20 70 63 61 63 68 65 31 xPage && pcache1
1026e 2e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20 20 .pLruTail ){.
1026f 20 50 67 48 64 72 31 20 2a 70 20 3d 20 70 63 61 PgHdr1 *p = pca
10270 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 che1.pLruTail;.
10271 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 pcache1PinPag
10272 65 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 e(p);. pcache
10273 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 1RemoveFromHash(
10274 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 46 p);. pcache1F
10275 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 7d 0a reePage(p);. }.
10276 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 61 72 64 }../*.** Discard
10277 20 61 6c 6c 20 70 61 67 65 73 20 66 72 6f 6d 20 all pages from
10278 63 61 63 68 65 20 70 43 61 63 68 65 20 77 69 74 cache pCache wit
10279 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 h a page number
1027a 28 6b 65 79 20 76 61 6c 75 65 29 20 0a 2a 2a 20 (key value) .**
1027b 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1027c 65 71 75 61 6c 20 74 6f 20 69 4c 69 6d 69 74 2e equal to iLimit.
1027d 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 Any pinned page
1027e 73 20 74 68 61 74 20 6d 65 65 74 20 74 68 69 73 s that meet this
1027f 20 0a 2a 2a 20 63 72 69 74 65 72 69 61 20 61 72 .** criteria ar
10280 65 20 75 6e 70 69 6e 6e 65 64 20 62 65 66 6f 72 e unpinned befor
10281 65 20 74 68 65 79 20 61 72 65 20 64 69 73 63 61 e they are disca
10282 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rded..**.** The
10283 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 global mutex mus
10284 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 t be held when t
10285 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10286 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
10287 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 c void pcache1Tr
10288 75 6e 63 61 74 65 55 6e 73 61 66 65 28 0a 20 20 uncateUnsafe(.
10289 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 2c PCache1 *pCache,
1028a 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 . unsigned int
1028b 20 69 4c 69 6d 69 74 20 0a 29 7b 0a 20 20 54 45 iLimit .){. TE
1028c 53 54 4f 4e 4c 59 28 20 69 6e 74 20 6e 50 61 67 STONLY( int nPag
1028d 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20 2f 2a e = 0; ) /*
1028e 20 55 73 65 64 20 74 6f 20 61 73 73 65 72 74 20 Used to assert
1028f 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 pCache->nPage is
10290 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e correct */. un
10291 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 signed int h;.
10292 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
10293 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 mutex_held(pcach
10294 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 e1.mutex) );. f
10295 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 68 65 or(h=0; h<pCache
10296 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20 ->nHash; h++){.
10297 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 20 3d PgHdr1 **pp =
10298 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 &pCache->apHash
10299 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 72 31 [h]; . PgHdr1
1029a 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 68 69 *pPage;. whi
1029b 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a 70 70 le( (pPage = *pp
1029c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 )!=0 ){. if
1029d 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69 ( pPage->iKey>=i
1029e 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 Limit ){.
1029f 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d pCache->nPage--
102a0 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20 ;. *pp =
102a1 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 pPage->pNext;.
102a2 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e pcache1Pin
102a3 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
102a4 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 pcache1Free
102a5 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
102a6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
102a7 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 pp = &pPage->p
102a8 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 54 45 Next;. TE
102a9 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 20 STONLY( nPage++
102aa 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
102ab 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
102ac 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50 Cache->nPage==nP
102ad 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a age );.}../*****
102ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
102b3 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 **** sqlite3_pca
102b4 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a che Methods ****
102b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
102b8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
102b9 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
102ba 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 _pcache.xInit me
102bb 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 thod..*/.static
102bc 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 int pcache1Init(
102bd 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
102be 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
102bf 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d ER(NotUsed);. m
102c0 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 emset(&pcache1,
102c1 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 0, sizeof(pcache
102c2 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 1));. if( sqlit
102c3 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
102c4 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 CoreMutex ){.
102c5 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 3d pcache1.mutex =
102c6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
102c7 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
102c8 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 X_STATIC_LRU);.
102c9 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
102ca 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
102cb 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
102cc 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
102cd 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d ache.xShutdown m
102ce 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 ethod..*/.static
102cf 20 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75 void pcache1Shu
102d0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
102d1 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
102d2 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
102d3 29 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f );. /* no-op */
102d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
102d5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
102d6 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
102d7 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a Create method..*
102d8 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
102d9 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 new cache..*/.st
102da 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 atic sqlite3_pca
102db 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61 che *pcache1Crea
102dc 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 te(int szPage, i
102dd 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a nt bPurgeable){.
102de 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
102df 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28 e;.. pCache = (
102e0 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65 PCache1 *)sqlite
102e1 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 3_malloc(sizeof(
102e2 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 PCache1));. if(
102e3 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d pCache ){. m
102e4 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c emset(pCache, 0,
102e5 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29 sizeof(PCache1)
102e6 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73 );. pCache->s
102e7 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a zPage = szPage;.
102e8 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72 pCache->bPur
102e9 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65 geable = (bPurge
102ea 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 able ? 1 : 0);.
102eb 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c if( bPurgeabl
102ec 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 e ){. pCach
102ed 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 e->nMin = 10;.
102ee 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 pcache1Enter
102ef 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70 Mutex();. p
102f0 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 cache1.nMinPage
102f1 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b += pCache->nMin;
102f2 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65 . pcache1Le
102f3 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 aveMutex();.
102f4 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 }. }. return (
102f5 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
102f6 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a )pCache;.}../*.*
102f7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
102f8 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
102f9 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a pcache.xCachesiz
102fa 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a e method. .**.**
102fb 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 Configure the c
102fc 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 ache_size limit
102fd 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a for a cache..*/.
102fe 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
102ff 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c he1Cachesize(sql
10300 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 ite3_pcache *p,
10301 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 int nMax){. PCa
10302 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10303 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 PCache1 *)p;. i
10304 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10305 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 eable ){. pca
10306 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10307 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d ;. pcache1.nM
10308 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 axPage += (nMax
10309 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b - pCache->nMax);
1030a 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 . pCache->nMa
1030b 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 x = nMax;. pc
1030c 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 ache1EnforceMaxP
1030d 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68 age();. pcach
1030e 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
1030f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
10310 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10311 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10312 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 e.xPagecount met
10313 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 hod. .*/.static
10314 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63 int pcache1Pagec
10315 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61 ount(sqlite3_pca
10316 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e che *p){. int n
10317 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10318 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 Mutex();. n = (
10319 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e (PCache1 *)p)->n
1031a 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c Page;. pcache1L
1031b 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
1031c 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
1031d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
1031e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
1031f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65 pcache.xFetch me
10320 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 thod. .**.** Fet
10321 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79 ch a page by key
10322 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 value..**.** Wh
10323 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e ether or not a n
10324 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 ew page may be a
10325 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 llocated by this
10326 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 function depend
10327 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 s on.** the valu
10328 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 e of the createF
10329 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a lag argument..**
1032a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 .** There are th
1032b 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 ree different ap
1032c 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 proaches to obta
1032d 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 ining space for
1032e 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e a page,.** depen
1032f 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 ding on the valu
10330 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 e of parameter c
10331 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 reateFlag (which
10332 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 may be 0, 1 or
10333 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 2)..**.** 1. R
10334 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
10335 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 value of create
10336 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 Flag, the cache
10337 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 is searched for
10338 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 a .** copy
10339 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 of the requested
1033a 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 page. If one is
1033b 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 found, it is re
1033c 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 turned..**.**
1033d 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 2. If createFlag
1033e 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 ==0 and the page
1033f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
10340 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 in the cache, NU
10341 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 LL is.** re
10342 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 turned..**.**
10343 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3. If createFlag
10344 20 69 73 20 31 2c 20 74 68 65 20 63 61 63 68 65 is 1, the cache
10345 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 70 75 is marked as pu
10346 72 67 65 61 62 6c 65 20 61 6e 64 20 74 68 65 20 rgeable and the
10347 70 61 67 65 20 69 73 20 0a 2a 2a 20 20 20 20 20 page is .**
10348 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
10349 74 68 65 20 63 61 63 68 65 2c 20 61 6e 64 20 69 the cache, and i
1034a 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 f either of the
1034b 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
1034c 75 65 2c 20 0a 2a 2a 20 20 20 20 20 20 72 65 74 ue, .** ret
1034d 75 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 urn NULL:.**.**
1034e 20 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 (a) the nu
1034f 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 mber of pages pi
10350 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 nned by the cach
10351 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 e is greater tha
10352 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 n.** P
10353 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a Cache1.nMax, or.
10354 2a 2a 20 20 20 20 20 20 20 28 62 29 20 74 68 65 ** (b) the
10355 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10356 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 pinned by the c
10357 61 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20 ache is greater
10358 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 than.**
10359 20 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 the sum of nMa
1035a 78 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 x for all purgea
1035b 62 6c 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73 ble caches, less
1035c 20 74 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 the sum of .**
1035d 20 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 nMin f
1035e 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 or all other pur
1035f 67 65 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a geable caches. .
10360 2a 2a 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f **.** 4. If no
10361 6e 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 ne of the first
10362 74 68 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 three conditions
10363 20 61 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63 apply and the c
10364 61 63 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a ache is marked.*
10365 2a 20 20 20 20 20 20 61 73 20 70 75 72 67 65 61 * as purgea
10366 62 6c 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 ble, and if one
10367 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
10368 20 69 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 is true:.**.**
10369 20 20 20 20 20 20 28 61 29 20 54 68 65 20 6e 75 (a) The nu
1036a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c mber of pages al
1036b 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 located for the
1036c 63 61 63 68 65 20 69 73 20 61 6c 72 65 61 64 79 cache is already
1036d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 .** P
1036e 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a Cache1.nMax, or.
1036f 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 **.** (b)
10370 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 The number of pa
10371 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ges allocated fo
10372 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 r all purgeable
10373 63 61 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 caches is.**
10374 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 65 already e
10375 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 qual to or great
10376 65 72 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20 er than the sum
10377 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a of nMax for all.
10378 2a 2a 20 20 20 20 20 20 20 20 20 20 20 70 75 72 ** pur
10379 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a geable caches,.*
1037a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 *.** then a
1037b 74 74 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c ttempt to recycl
1037c 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 e a page from th
1037d 65 20 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69 e LRU list. If i
1037e 74 20 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a t is the right.*
1037f 2a 20 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74 * size, ret
10380 75 72 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64 urn the recycled
10381 20 62 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69 buffer. Otherwi
10382 73 65 2c 20 66 72 65 65 20 74 68 65 20 62 75 66 se, free the buf
10383 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 fer and.**
10384 70 72 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20 proceed to step
10385 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 5. .**.** 5. O
10386 74 68 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 therwise, alloca
10387 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 te and return a
10388 6e 65 77 20 70 61 67 65 20 62 75 66 66 65 72 2e new page buffer.
10389 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1038a 2a 70 63 61 63 68 65 31 46 65 74 63 68 28 73 71 *pcache1Fetch(sq
1038b 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c lite3_pcache *p,
1038c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b unsigned int iK
1038d 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c ey, int createFl
1038e 61 67 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ag){. unsigned
1038f 69 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 int nPinned;. P
10390 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10391 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
10392 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d PgHdr1 *pPage =
10393 20 30 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 0;.. pcache1En
10394 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 terMutex();. if
10395 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 ( createFlag==1
10396 29 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 ) sqlite3BeginBe
10397 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 nignMalloc();..
10398 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68 /* Search the h
10399 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e ash table for an
1039a 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e existing entry.
1039b 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 */. if( pCache
1039c 2d 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 ->nHash>0 ){.
1039d 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 unsigned int h
1039e 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d = iKey % pCache-
1039f 3e 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 >nHash;. for(
103a0 70 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 pPage=pCache->ap
103a1 48 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 Hash[h]; pPage&&
103a2 70 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 pPage->iKey!=iKe
103a3 79 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e y; pPage=pPage->
103a4 70 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 pNext);. }.. i
103a5 66 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 f( pPage || crea
103a6 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 teFlag==0 ){.
103a7 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 pcache1PinPage(
103a8 70 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f pPage);. goto
103a9 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a fetch_out;. }.
103aa 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 . /* Step 3 of
103ab 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 header comment.
103ac 2a 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 */. nPinned = p
103ad 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 Cache->nPage - p
103ae 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 Cache->nRecyclab
103af 6c 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 le;. if( create
103b0 46 6c 61 67 3d 3d 31 20 26 26 20 70 43 61 63 68 Flag==1 && pCach
103b1 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 e->bPurgeable &&
103b2 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e (. nPinn
103b3 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 ed>=(pcache1.nMa
103b4 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d xPage+pCache->nM
103b5 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 in-pcache1.nMinP
103b6 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 age). || nPi
103b7 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e nned>=(pCache->n
103b8 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20 20 Max * 9 / 10).
103b9 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 )){. goto fet
103ba 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 ch_out;. }.. i
103bb 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 f( pCache->nPage
103bc 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20 >=pCache->nHash
103bd 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 && pcache1Resize
103be 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a Hash(pCache) ){.
103bf 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f goto fetch_o
103c0 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 ut;. }.. /* St
103c1 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63 ep 4. Try to rec
103c2 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66 ycle a page buff
103c3 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 er if appropriat
103c4 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 e. */. if( pCac
103c5 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 he->bPurgeable &
103c6 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 & pcache1.pLruTa
103c7 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70 43 il && (. (pC
103c8 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d 70 ache->nPage+1>=p
103c9 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c 20 Cache->nMax) ||
103ca 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
103cb 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d Page>=pcache1.nM
103cc 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20 axPage. )){.
103cd 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 pPage = pcache1
103ce 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 .pLruTail;. p
103cf 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d cache1RemoveFrom
103d0 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 Hash(pPage);.
103d1 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 pcache1PinPage(
103d2 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 pPage);. if(
103d3 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73 pPage->pCache->s
103d4 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73 zPage!=pCache->s
103d5 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 zPage ){. p
103d6 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 cache1FreePage(p
103d7 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61 Page);. pPa
103d8 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ge = 0;. }els
103d9 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 e{. pcache1
103da 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d .nCurrentPage -=
103db 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d (pPage->pCache-
103dc 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43 >bPurgeable - pC
103dd 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
103de 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
103df 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20 /* Step 5. If a
103e0 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66 usable page buff
103e1 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74 er has still not
103e2 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 been found, .
103e3 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c ** attempt to al
103e4 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 locate a new one
103e5 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 . . */. if( !p
103e6 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 Page ){. pPag
103e7 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 e = pcache1Alloc
103e8 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20 Page(pCache);.
103e9 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 }.. if( pPage )
103ea 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 {. unsigned i
103eb 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 nt h = iKey % pC
103ec 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 ache->nHash;.
103ed 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47 48 44 *(void **)(PGHD
103ee 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 65 R1_TO_PAGE(pPage
103ef 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43 61 63 )) = 0;. pCac
103f0 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 he->nPage++;.
103f1 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 pPage->iKey = i
103f2 4b 65 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e Key;. pPage->
103f3 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e pNext = pCache->
103f4 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70 apHash[h];. p
103f5 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 Page->pCache = p
103f6 43 61 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65 Cache;. pPage
103f7 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a ->pLruPrev = 0;.
103f8 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e pPage->pLruN
103f9 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 43 61 ext = 0;. pCa
103fa 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d che->apHash[h] =
103fb 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 pPage;. }..fet
103fc 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 ch_out:. if( pP
103fd 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61 63 age && iKey>pCac
103fe 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 he->iMaxKey ){.
103ff 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b pCache->iMaxK
10400 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20 ey = iKey;. }.
10401 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d if( createFlag=
10402 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42 =1 ) sqlite3EndB
10403 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
10404 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10405 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 ex();. return (
10406 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f 54 pPage ? PGHDR1_T
10407 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a 20 O_PAGE(pPage) :
10408 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 0);.}.../*.** Im
10409 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1040a 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
1040b 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64 he.xUnpin method
1040c 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 70 ..**.** Mark a p
1040d 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 20 age as unpinned
1040e 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 73 (eligible for as
1040f 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 63 ynchronous recyc
10410 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 ling)..*/.static
10411 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e 70 void pcache1Unp
10412 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 in(sqlite3_pcach
10413 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67 2c e *p, void *pPg,
10414 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b 65 int reuseUnlike
10415 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a ly){. PCache1 *
10416 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 pCache = (PCache
10417 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 1 *)p;. PgHdr1
10418 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f *pPage = PAGE_TO
10419 5f 50 47 48 44 52 31 28 70 50 67 29 3b 0a 0a 20 _PGHDR1(pPg);..
1041a 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
1041b 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 ex();.. /* It i
1041c 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 61 s an error to ca
1041d 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
1041e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
1041f 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70 61 already . ** pa
10420 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c rt of the global
10421 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a LRU list.. */.
10422 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
10423 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26 20 >pLruPrev==0 &&
10424 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3d pPage->pLruNext=
10425 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
10426 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10427 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63 68 !=pPage && pcach
10428 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50 61 e1.pLruTail!=pPa
10429 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65 75 ge );.. if( reu
1042a 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 seUnlikely || pc
1042b 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 ache1.nCurrentPa
1042c 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 ge>pcache1.nMaxP
1042d 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 age ){. pcach
1042e 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
1042f 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61 (pPage);. pca
10430 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61 che1FreePage(pPa
10431 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ge);. }else{.
10432 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61 67 /* Add the pag
10433 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 e to the global
10434 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c LRU list. Normal
10435 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 ly, the page is
10436 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 added to. **
10437 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 the head of the
10438 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65 20 list (last page
10439 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 2e to be recycled).
1043a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 However, if the
1043b 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55 6e . ** reuseUn
1043c 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73 73 likely flag pass
1043d 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 ed to this funct
1043e 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 ion is true, the
1043f 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a 20 page is added.
10440 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61 69 ** to the tai
10441 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 66 l of the list (f
10442 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65 20 irst page to be
10443 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20 2a recycled).. *
10444 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65 /. if( pcache
10445 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 20 1.pLruHead ){.
10446 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10447 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d Head->pLruPrev =
10448 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 pPage;. pP
10449 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 age->pLruNext =
1044a 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
1044b 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e ;. pcache1.
1044c 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 pLruHead = pPage
1044d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1044e 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 pcache1.pLruT
1044f 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 ail = pPage;.
10450 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 pcache1.pLruH
10451 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 ead = pPage;.
10452 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e }. pCache->n
10453 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 Recyclable++;.
10454 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 }.. pcache1Leav
10455 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a eMutex();.}../*.
10456 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10457 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10458 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20 6d _pcache.xRekey m
10459 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 ethod. .*/.stati
1045a 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52 65 c void pcache1Re
1045b 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 70 key(. sqlite3_p
1045c 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69 64 cache *p,. void
1045d 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e 65 *pPg,. unsigne
1045e 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 6e d int iOld,. un
1045f 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77 0a signed int iNew.
10460 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 ){. PCache1 *pC
10461 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 ache = (PCache1
10462 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 *)p;. PgHdr1 *p
10463 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50 Page = PAGE_TO_P
10464 47 48 44 52 31 28 70 50 67 29 3b 0a 20 20 50 67 GHDR1(pPg);. Pg
10465 48 64 72 31 20 2a 2a 70 70 3b 0a 20 20 75 6e 73 Hdr1 **pp;. uns
10466 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 0a 20 20 igned int h; .
10467 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
10468 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b 0a 0a 20 20 Key==iOld );..
10469 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
1046a 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 x();.. h = iOld
1046b 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a %pCache->nHash;.
1046c 20 20 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e pp = &pCache->
1046d 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 apHash[h];. whi
1046e 6c 65 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 le( (*pp)!=pPage
1046f 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a ){. pp = &(*
10470 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a pp)->pNext;. }.
10471 20 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 *pp = pPage->p
10472 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 Next;.. h = iNe
10473 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b w%pCache->nHash;
10474 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d . pPage->iKey =
10475 20 69 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e iNew;. pPage->
10476 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e pNext = pCache->
10477 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 apHash[h];. pCa
10478 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d che->apHash[h] =
10479 20 70 50 61 67 65 3b 0a 0a 20 20 69 66 28 20 69 pPage;.. if( i
1047a 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 New>pCache->iMax
1047b 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 Key ){. pCach
1047c 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 e->iMaxKey = iNe
1047d 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 w;. }.. pcache
1047e 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 1LeaveMutex();.}
1047f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
10480 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
10481 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72 lite3_pcache.xTr
10482 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a uncate method. .
10483 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c **.** Discard al
10484 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 l unpinned pages
10485 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69 in the cache wi
10486 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
10487 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 equal to.** or
10488 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72 greater than par
10489 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 ameter iLimit. A
1048a 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 ny pinned pages
1048b 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 with a page numb
1048c 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f er.** equal to o
1048d 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 r greater than i
1048e 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63 Limit are implic
1048f 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a itly unpinned..*
10490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10491 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71 ache1Truncate(sq
10492 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c lite3_pcache *p,
10493 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c unsigned int iL
10494 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31 imit){. PCache1
10495 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10496 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 he1 *)p;. pcach
10497 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a e1EnterMutex();.
10498 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 if( iLimit<=pC
10499 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b ache->iMaxKey ){
1049a 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e . pcache1Trun
1049b 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68 cateUnsafe(pCach
1049c 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 e, iLimit);.
1049d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
1049e 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a = iLimit-1;. }.
1049f 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
104a0 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
104a1 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
104a2 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
104a3 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 ache.xDestroy me
104a4 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 thod. .**.** Des
104a5 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c troy a cache all
104a6 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61 ocated using pca
104a7 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f che1Create()..*/
104a8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
104a9 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69 che1Destroy(sqli
104aa 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a te3_pcache *p){.
104ab 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
104ac 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
104ad 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
104ae 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 Mutex();. pcach
104af 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
104b0 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 (pCache, 0);. p
104b1 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
104b2 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b -= pCache->nMax;
104b3 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 . pcache1.nMinP
104b4 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e age -= pCache->n
104b5 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e Min;. pcache1En
104b6 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a forceMaxPage();.
104b7 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
104b8 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 tex();. sqlite3
104b9 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70 _free(pCache->ap
104ba 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 Hash);. sqlite3
104bb 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d _free(pCache);.}
104bc 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
104bd 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
104be 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
104bf 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 ation (sqlite3_i
104c0 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a nitialize()) to.
104c1 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 ** install the d
104c2 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 efault pluggable
104c3 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 cache module, a
104c4 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72 ssuming the user
104c5 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 has not.** alre
104c6 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20 ady provided an
104c7 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a alternative..*/.
104c8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
104c9 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
104ca 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 eSetDefault(void
104cb 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
104cc 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
104cd 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
104ce 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 s = {. 0,
104cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
104d0 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 /* pArg */.
104d1 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20 pcache1Init,
104d2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49 /* xI
104d3 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 nit */. pcach
104d4 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 e1Shutdown,
104d5 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e /* xShutdown
104d6 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 */. pcache1C
104d7 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 reate,
104d8 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 /* xCreate */.
104d9 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73 pcache1Caches
104da 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 ize, /* x
104db 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 Cachesize */.
104dc 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e pcache1Pagecoun
104dd 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61 t, /* xPa
104de 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 gecount */. p
104df 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20 cache1Fetch,
104e0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63 /* xFetc
104e1 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 h */. pcache1
104e2 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20 Unpin,
104e3 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 /* xUnpin */.
104e4 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c pcache1Rekey,
104e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
104e6 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 Rekey */. pca
104e7 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20 che1Truncate,
104e8 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 /* xTrunca
104e9 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 te */. pcache
104ea 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 1Destroy
104eb 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a /* xDestroy *
104ec 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
104ed 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 _config(SQLITE_C
104ee 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64 ONFIG_PCACHE, &d
104ef 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a efaultMethods);.
104f0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
104f1 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
104f2 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 ANAGEMENT./*.**
104f3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
104f4 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 called to free
104f5 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 superfluous dyna
104f6 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
104f7 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 d memory.** held
104f8 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79 by the pager sy
104f9 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 stem. Memory in
104fa 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 use by any SQLit
104fb 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 e pager allocate
104fc 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 d.** by the curr
104fd 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 ent thread may b
104fe 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 e sqlite3_free()
104ff 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 ed..**.** nReq i
10500 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
10501 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
10502 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 required. Once t
10503 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 his much has.**
10504 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 been released, t
10505 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
10506 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20 rns. The return
10507 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 value is the tot
10508 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 al number .** of
10509 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
1050a 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 released..*/.SQ
1050b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1050c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
1050d 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 leaseMemory(int
1050e 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 nReq){. int nFr
1050f 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 ee = 0;. if( pc
10510 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 ache1.pStart==0
10511 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 ){. PgHdr1 *p
10512 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 ;. pcache1Ent
10513 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77 erMutex();. w
10514 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c hile( (nReq<0 ||
10515 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20 nFree<nReq) &&
10516 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54 (p=pcache1.pLruT
10517 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 ail) ){. nF
10518 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 ree += sqlite3Ma
10519 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 llocSize(p);.
1051a 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 pcache1PinPag
1051b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 e(p);. pcac
1051c 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 he1RemoveFromHas
1051d 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 h(p);. pcac
1051e 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a he1FreePage(p);.
1051f 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10520 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10521 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 }. return nFre
10522 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 e;.}.#endif /* S
10523 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
10524 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a ORY_MANAGEMENT *
10525 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
10526 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 _TEST./*.** This
10527 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
10528 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64 d by test proced
10529 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20 ures to inspect
1052a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 the internal sta
1052b 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f te.** of the glo
1052c 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 bal cache..*/.SQ
1052d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1052e 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
1052f 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 tats(. int *pnC
10530 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 urrent, /*
10531 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 OUT: Total numbe
10532 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 r of pages cache
10533 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 d */. int *pnMa
10534 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f x, /* O
10535 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d UT: Global maxim
10536 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f um cache size */
10537 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 . int *pnMin,
10538 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
10539 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e Sum of PCache1.n
1053a 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c Min for purgeabl
1053b 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e e caches */. in
1053c 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 t *pnRecyclable
1053d 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c /* OUT: Total
1053e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1053f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 available for r
10540 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 ecycling */.){.
10541 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e PgHdr1 *p;. in
10542 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 t nRecyclable =
10543 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68 0;. for(p=pcach
10544 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 e1.pLruHead; p;
10545 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a p=p->pLruNext){.
10546 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b nRecyclable+
10547 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 +;. }. *pnCurr
10548 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 ent = pcache1.nC
10549 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 urrentPage;. *p
1054a 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e nMax = pcache1.n
1054b 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 MaxPage;. *pnMi
1054c 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e n = pcache1.nMin
1054d 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 Page;. *pnRecyc
1054e 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 lable = nRecycla
1054f 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ble;.}.#endif../
10550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
10551 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20 nd of pcache1.c
10552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
10556 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 egin file rowset
10557 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1055a 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 *.** 2008 Decemb
1055b 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 er 3.**.** The a
1055c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1055d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1055e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1055f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10560 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10561 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10562 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10563 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10564 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10565 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10566 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10567 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10568 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10569 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1056a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1056b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1056c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1056d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1056e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1056f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10571 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ***.**.** This m
10572 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 odule implements
10573 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61 an object we ca
10574 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a ll a "RowSet"..*
10575 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 *.** The RowSet
10576 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c object is a coll
10577 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 ection of rowids
10578 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 . Rowids.** are
10579 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 inserted into t
1057a 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 he RowSet in an
1057b 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e arbitrary order.
1057c 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e Inserts.** can
1057d 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77 be intermixed w
1057e 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65 ith tests to see
1057f 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69 if a given rowi
10580 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 d has been.** pr
10581 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 eviously inserte
10582 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 d into the RowSe
10583 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 t..**.** After a
10584 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66 ll inserts are f
10585 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70 inished, it is p
10586 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61 ossible to extra
10587 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e ct the.** elemen
10588 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 ts of the RowSet
10589 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 in sorted order
1058a 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74 . Once this ext
1058b 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 raction.** proce
1058c 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20 ss has started,
1058d 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 no new elements
1058e 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e may be inserted.
1058f 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 .**.** Hence, th
10590 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72 e primitive oper
10591 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 ations for a Row
10592 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 Set are:.**.**
10593 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 CREATE.** I
10594 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 NSERT.** TEST
10595 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a .** SMALLEST.
10596 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a ** DESTROY.**
10597 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61 .** The CREATE a
10598 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69 nd DESTROY primi
10599 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f tives are the co
1059a 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65 nstructor and de
1059b 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 structor,.** obv
1059c 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 iously. The INS
1059d 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64 ERT primitive ad
1059e 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 ds a new element
1059f 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a to the RowSet..
105a0 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 ** TEST checks t
105a1 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d o see if an elem
105a2 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 ent is already i
105a3 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 n the RowSet. S
105a4 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 MALLEST.** extra
105a5 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 cts the least va
105a6 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 lue from the Row
105a7 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 Set..**.** The I
105a8 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 NSERT primitive
105a9 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 might allocate a
105aa 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
105ab 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 . Memory is.**
105ac 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75 allocated in chu
105ad 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 nks so most INSE
105ae 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 RTs do no alloca
105af 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20 tion. There is
105b0 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 an .** upper bou
105b1 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f nd on the size o
105b2 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f f allocated memo
105b3 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 ry. No memory i
105b4 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c s freed.** until
105b5 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 DESTROY..**.**
105b6 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 The TEST primiti
105b7 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62 ve includes a "b
105b8 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 atch" number. T
105b9 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 he TEST primitiv
105ba 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 e.** will only s
105bb 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 ee elements that
105bc 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62 were inserted b
105bd 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63 efore the last c
105be 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 hange.** in the
105bf 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 batch number. I
105c0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 n other words, i
105c1 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 f an INSERT occu
105c2 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 rs between.** tw
105c3 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68 o TESTs where th
105c4 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65 e TESTs have the
105c5 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d same batch nubm
105c6 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 er, then the.**
105c7 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74 value added by t
105c8 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e he INSERT will n
105c9 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f ot be visible to
105ca 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54 the second TEST
105cb 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c ..** The initial
105cc 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 batch number is
105cd 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 zero, so if the
105ce 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54 very first TEST
105cf 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e contains.** a n
105d0 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 on-zero batch nu
105d1 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 mber, it will se
105d2 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 e all prior INSE
105d3 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e RTs..**.** No IN
105d4 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 SERTs may occurs
105d5 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 after a SMALLES
105d6 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e T. An assertion
105d7 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a will fail if.**
105d8 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 that is attempt
105d9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ed..**.** The co
105da 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 st of an INSERT
105db 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 is roughly const
105dc 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 ant. (Sometime
105dd 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 new memory.** ha
105de 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 s to be allocate
105df 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 d on an INSERT.)
105e0 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 The cost of a
105e1 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a TEST with a new.
105e2 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 ** batch number
105e3 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 is O(NlogN) wher
105e4 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
105e5 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
105e6 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
105e7 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 The cost of a TE
105e8 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ST using the sam
105e9 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 e batch number i
105ea 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 s O(logN). The
105eb 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 cost.** of the f
105ec 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 irst SMALLEST is
105ed 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f O(NlogN). Seco
105ee 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
105ef 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 t SMALLEST.** pr
105f0 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e imitives are con
105f1 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 stant time. The
105f2 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59 cost of DESTROY
105f3 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 is O(N)..**.**
105f4 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65 There is an adde
105f5 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 d cost of O(N) w
105f6 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65 hen switching be
105f7 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a tween TEST and.*
105f8 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 * SMALLEST primi
105f9 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 tives..**.** $Id
105fa 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 36 : rowset.c,v 1.6
105fb 20 32 30 30 39 2f 30 34 2f 32 32 20 31 35 3a 33 2009/04/22 15:3
105fc 32 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2:59 drh Exp $.*
105fd 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74 /.../*.** Target
105fe 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61 size for alloca
105ff 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a tion chunks..*/.
10600 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41 #define ROWSET_A
10601 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31 LLOCATION_SIZE 1
10602 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 024../*.** The n
10603 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 umber of rowset
10604 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f entries per allo
10605 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f cation chunk..*/
10606 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f .#define ROWSET_
10607 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 ENTRY_PER_CHUNK
10608 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
10609 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 ((ROWS
1060a 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 ET_ALLOCATION_SI
1060b 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 ZE-8)/sizeof(str
1060c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 uct RowSetEntry)
1060d 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e )../*.** Each en
1060e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 try in a RowSet
1060f 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
10610 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
10611 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 object..*/.struc
10612 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 t RowSetEntry {
10613 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36 . i6
10614 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 4 v;
10615 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
10616 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74 OWID value for t
10617 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 his entry */. s
10618 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10619 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 y *pRight; /*
1061a 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c Right subtree (l
1061b 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f arger entries) o
1061c 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 r list */. stru
1061d 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
1061e 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 pLeft; /* Lef
1061f 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c t subtree (small
10620 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d er entries) */.}
10621 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 ;../*.** RowSetE
10622 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65 ntry objects are
10623 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 allocated in la
10624 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 rge chunks (inst
10625 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 ances of the.**
10626 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
10627 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d ure) to reduce m
10628 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10629 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a overhead. The.
1062a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 ** chunks are ke
1062b 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c pt on a linked l
1062c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 ist so that they
1062d 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 can be dealloca
1062e 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ted.** when the
1062f 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f RowSet is destro
10630 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 yed..*/.struct R
10631 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 owSetChunk {. s
10632 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e truct RowSetChun
10633 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 k *pNextChunk;
10634 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
10635 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 unk on list of t
10636 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 hem all */. str
10637 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
10638 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e aEntry[ROWSET_EN
10639 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 TRY_PER_CHUNK];
1063a 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 /* Allocated ent
1063b 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ries */.};../*.*
1063c 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e * A RowSet in an
1063d 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
1063e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
1063f 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 ture..**.** A ty
10640 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74 pedef of this st
10641 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64 ructure if found
10642 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e in sqliteInt.h.
10643 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 .*/.struct RowSe
10644 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 t {. struct Row
10645 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b SetChunk *pChunk
10646 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 ; /* List of
10647 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 all chunk alloca
10648 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tions */. sqlit
10649 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 e3 *db;
1064a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1064b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1064c 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
1064d 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
1064e 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 ntry; /* List
1064f 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e of entries usin
10650 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 g pRight */. st
10651 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10652 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 *pLast; /*
10653 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 Last entry on th
10654 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f e pEntry list */
10655 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
10656 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 Entry *pFresh;
10657 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e /* Source of n
10658 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 ew entry objects
10659 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 */. struct Row
1065a 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b SetEntry *pTree;
1065b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 /* Binary t
1065c 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a ree of entries *
1065d 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 /. u16 nFresh;
1065e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1065f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
10660 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 objects on pFres
10661 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 h */. u8 isSort
10662 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
10663 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
10664 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65 pEntry is sorte
10665 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68 d */. u8 iBatch
10666 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10667 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
10668 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f insert batch */
10669 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .};../*.** Turn
1066a 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f bulk memory into
1066b 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
1066c 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 . N bytes of me
1066d 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 mory.** are avai
1066e 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e lable at pSpace.
1066f 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 The db pointer
10670 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 is used as a me
10671 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 mory context.**
10672 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 for any subseque
10673 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 nt allocations t
10674 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 hat need to occu
10675 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 r..** Return a p
10676 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
10677 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e w RowSet object.
10678 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 .**.** It must b
10679 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
1067a 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 N is sufficient
1067b 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 to make a Rowset
1067c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 . If not.** an
1067d 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1067e 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 occurs..** .** I
1067f 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 f N is larger th
10680 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 an the minimum,
10681 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 use the surplus
10682 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a as an initial.**
10683 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 allocation of e
10684 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 ntries available
10685 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a to be filled..*
10686 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10687 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 RowSet *sqlite3
10688 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 RowSetInit(sqlit
10689 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 e3 *db, void *pS
1068a 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 pace, unsigned i
1068b 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 nt N){. RowSet
1068c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 *p;. assert( N
1068d 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b >= sizeof(*p) );
1068e 0a 20 20 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 . p = pSpace;.
1068f 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a p->pChunk = 0;.
10690 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 p->db = db;.
10691 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 p->pEntry = 0;.
10692 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 p->pLast = 0;.
10693 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 p->pTree = 0;.
10694 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 28 73 74 p->pFresh = (st
10695 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10696 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 6e 46 *)&p[1];. p->nF
10697 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20 resh = (u16)((N
10698 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 2f 73 69 - sizeof(*p))/si
10699 7a 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 zeof(struct RowS
1069a 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e etEntry));. p->
1069b 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 isSorted = 1;.
1069c 70 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 p->iBatch = 0;.
1069d 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
1069e 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 .** Deallocate a
1069f 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 ll chunks from a
106a0 20 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 RowSet. This f
106a1 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 rees all memory
106a2 74 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 that.** the RowS
106a3 65 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 et has allocated
106a4 20 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 over its lifeti
106a5 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e me. This routin
106a6 65 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 e is.** the dest
106a7 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 ructor for the R
106a8 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 owSet..*/.SQLITE
106a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
106aa 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 lite3RowSetClear
106ab 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 (RowSet *p){. s
106ac 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e truct RowSetChun
106ad 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 k *pChunk, *pNex
106ae 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 tChunk;. for(pC
106af 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 hunk=p->pChunk;
106b0 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d pChunk; pChunk =
106b1 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 pNextChunk){.
106b2 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 pNextChunk = p
106b3 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e Chunk->pNextChun
106b4 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 k;. sqlite3Db
106b5 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 Free(p->db, pChu
106b6 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 nk);. }. p->pC
106b7 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e hunk = 0;. p->n
106b8 46 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e Fresh = 0;. p->
106b9 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d pEntry = 0;. p-
106ba 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d >pLast = 0;. p-
106bb 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d >pTree = 0;. p-
106bc 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d >isSorted = 1;.}
106bd 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 ../*.** Insert a
106be 20 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 new value into
106bf 61 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 a RowSet..**.**
106c0 54 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 The mallocFailed
106c1 20 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 flag of the dat
106c2 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
106c3 20 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 is set if a.**
106c4 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
106c5 6e 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 n fails..*/.SQLI
106c6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
106c7 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 sqlite3RowSetIns
106c8 65 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 ert(RowSet *p, i
106c9 36 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 64 rowid){. str
106ca 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
106cb 2a 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 *pEntry; /* The
106cc 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 new entry */.
106cd 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
106ce 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 ry *pLast; /*
106cf 54 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 The last prior e
106d0 6e 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 ntry */. assert
106d1 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 ( p!=0 );. if(
106d2 70 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a p->nFresh==0 ){.
106d3 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 struct RowSe
106d4 74 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 tChunk *pNew;.
106d5 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
106d6 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 DbMallocRaw(p->d
106d7 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 b, sizeof(*pNew)
106d8 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d );. if( pNew=
106d9 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
106da 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e rn;. }. pN
106db 65 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d ew->pNextChunk =
106dc 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 p->pChunk;.
106dd 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 p->pChunk = pNew
106de 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 ;. p->pFresh
106df 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a = pNew->aEntry;.
106e0 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 p->nFresh =
106e1 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 ROWSET_ENTRY_PER
106e2 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 _CHUNK;. }. pE
106e3 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 ntry = p->pFresh
106e4 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d ++;. p->nFresh-
106e5 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d -;. pEntry->v =
106e6 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 rowid;. pEntry
106e7 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
106e8 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 pLast = p->pLast
106e9 3b 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b ;. if( pLast ){
106ea 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f . if( p->isSo
106eb 72 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 rted && rowid<=p
106ec 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 Last->v ){.
106ed 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 p->isSorted = 0
106ee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 ;. }. pLas
106ef 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 t->pRight = pEnt
106f0 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ry;. }else{.
106f1 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 assert( p->pEnt
106f2 72 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 ry==0 ); /* Fire
106f3 73 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 s if INSERT afte
106f4 72 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 r SMALLEST */.
106f5 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 p->pEntry = pE
106f6 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 ntry;. }. p->p
106f7 4c 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d Last = pEntry;.}
106f8 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 ../*.** Merge tw
106f9 6f 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 o lists of RowSe
106fa 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 tEntry objects.
106fb 20 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 Remove duplicat
106fc 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e es..**.** The in
106fd 70 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f put lists are co
106fe 6e 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67 nnected via pRig
106ff 68 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 ht pointers and
10700 61 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 are .** assumed
10701 74 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 to each already
10702 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 be in sorted ord
10703 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 er..*/.static st
10704 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10705 20 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 *rowSetMerge(.
10706 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
10707 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 try *pA, /* F
10708 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 irst sorted list
10709 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f to be merged */
1070a 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
1070b 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a Entry *pB /*
1070c 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c Second sorted l
1070d 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 ist to be merged
1070e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
1070f 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 RowSetEntry head
10710 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 ;. struct RowSe
10711 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a tEntry *pTail;..
10712 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b pTail = &head;
10713 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 . while( pA &&
10714 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 pB ){. assert
10715 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 ( pA->pRight==0
10716 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 || pA->v<=pA->pR
10717 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 ight->v );. a
10718 73 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 ssert( pB->pRigh
10719 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 t==0 || pB->v<=p
1071a 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a B->pRight->v );.
1071b 20 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 if( pA->v<pB
1071c 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 ->v ){. pTa
1071d 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b il->pRight = pA;
1071e 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e . pA = pA->
1071f 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 pRight;. pT
10720 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 ail = pTail->pRi
10721 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ght;. }else i
10722 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 f( pB->v<pA->v )
10723 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
10724 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 Right = pB;.
10725 20 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 pB = pB->pRigh
10726 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d t;. pTail =
10727 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a pTail->pRight;.
10728 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
10729 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 pA = pA->pRight
1072a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1072b 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 ( pA ){. asse
1072c 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d rt( pA->pRight==
1072d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 0 || pA->v<=pA->
1072e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 pRight->v );.
1072f 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d pTail->pRight =
10730 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 pA;. }else{.
10731 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 assert( pB==0
10732 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 || pB->pRight==0
10733 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 || pB->v<=pB->p
10734 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 Right->v );.
10735 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 pTail->pRight =
10736 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e pB;. }. return
10737 20 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a head.pRight;.}.
10738 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 ./*.** Sort all
10739 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 elements on the
1073a 70 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 pEntry list of t
1073b 68 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 he RowSet into a
1073c 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a scending order..
1073d 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */ .static void
1073e 72 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 rowSetSort(RowSe
1073f 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 t *p){. unsigne
10740 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 d int i;. struc
10741 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
10742 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 Entry;. struct
10743 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 RowSetEntry *aBu
10744 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 cket[40];.. ass
10745 65 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 ert( p->isSorted
10746 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 ==0 );. memset(
10747 61 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 aBucket, 0, size
10748 6f 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 of(aBucket));.
10749 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 while( p->pEntry
1074a 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d ){. pEntry =
1074b 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 p->pEntry;.
1074c 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 p->pEntry = pEnt
1074d 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 ry->pRight;.
1074e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d pEntry->pRight =
1074f 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 0;. for(i=0;
10750 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b aBucket[i]; i++
10751 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 ){. pEntry
10752 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 = rowSetMerge(aB
10753 75 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 ucket[i], pEntry
10754 29 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 );. aBucket
10755 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 [i] = 0;. }.
10756 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 aBucket[i] =
10757 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 pEntry;. }. pE
10758 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ntry = 0;. for(
10759 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 i=0; i<sizeof(aB
1075a 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 ucket)/sizeof(aB
1075b 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b ucket[0]); i++){
1075c 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f . pEntry = ro
1075d 77 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 wSetMerge(pEntry
1075e 2c 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 , aBucket[i]);.
1075f 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d }. p->pEntry =
10760 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c pEntry;. p->pL
10761 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 ast = 0;. p->is
10762 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a Sorted = 1;.}...
10763 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c /*.** The input,
10764 20 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 pIn, is a binar
10765 79 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 y tree (or subtr
10766 65 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 ee) of RowSetEnt
10767 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 ry objects..** C
10768 6f 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65 onvert this tree
10769 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c into a linked l
1076a 69 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ist connected by
1076b 20 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e the pRight poin
1076c 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 ters.** and retu
1076d 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 rn pointers to t
1076e 68 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 he first and las
1076f 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 t elements of th
10770 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 e new list..*/.s
10771 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 tatic void rowSe
10772 74 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 tTreeToList(. s
10773 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10774 79 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 y *pIn,
10775 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 /* Root of the i
10776 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 nput tree */. s
10777 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10778 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 y **ppFirst,
10779 2f 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 /* Write head of
1077a 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 the output list
1077b 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 here */. struc
1077c 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a t RowSetEntry **
1077d 70 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 ppLast /* W
1077e 72 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 rite tail of the
1077f 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 output list her
10780 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 e */.){. assert
10781 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 ( pIn!=0 );. if
10782 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a ( pIn->pLeft ){.
10783 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 struct RowSe
10784 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 tEntry *p;. r
10785 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 owSetTreeToList(
10786 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 pIn->pLeft, ppFi
10787 72 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d rst, &p);. p-
10788 3e 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 >pRight = pIn;.
10789 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 }else{. *ppF
1078a 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a irst = pIn;. }.
1078b 20 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 if( pIn->pRigh
1078c 74 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 t ){. rowSetT
1078d 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 reeToList(pIn->p
1078e 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 Right, &pIn->pRi
1078f 67 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 ght, ppLast);.
10790 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 }else{. *ppLa
10791 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 st = pIn;. }.
10792 61 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 assert( (*ppLast
10793 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a )->pRight==0 );.
10794 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 }.../*.** Conver
10795 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 t a sorted list
10796 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e of elements (con
10797 6e 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74 nected by pRight
10798 29 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a ) into a binary.
10799 2a 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70 ** tree with dep
1079a 74 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 th of iDepth. A
1079b 20 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e depth of 1 mean
1079c 73 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 s the tree conta
1079d 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 ins a single.**
1079e 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 node taken from
1079f 74 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c the head of *ppL
107a0 69 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 ist. A depth of
107a1 20 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 2 means a tree
107a2 77 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f with.** three no
107a3 64 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 des. And so for
107a4 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 th..**.** Use as
107a5 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 many entries fr
107a6 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 om the input lis
107a7 74 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e t as required an
107a8 64 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 d update the.**
107a9 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 *ppList to point
107aa 20 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 to the unused e
107ab 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c lements of the l
107ac 69 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 ist. If the inp
107ad 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 ut.** list conta
107ae 69 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d ins too few elem
107af 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 ents, then const
107b0 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 ruct an incomple
107b1 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c te tree.** and l
107b2 65 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 eave *ppList set
107b3 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
107b4 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
107b5 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 to the root of
107b6 74 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 the constructed
107b7 62 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a binary tree..*/.
107b8 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f static struct Ro
107b9 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 wSetEntry *rowSe
107ba 74 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 tNDeepTree(. st
107bb 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
107bc 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 **ppList,. int
107bd 20 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 iDepth.){. str
107be 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
107bf 2a 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 *p; /* R
107c0 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 oot of the new t
107c1 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ree */. struct
107c2 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 RowSetEntry *pLe
107c3 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 ft; /* Left
107c4 73 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 subtree */. if(
107c5 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 *ppList==0 ){.
107c6 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
107c7 0a 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 . if( iDepth==1
107c8 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c ){. p = *ppL
107c9 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 ist;. *ppList
107ca 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
107cb 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e p->pLeft = p->
107cc 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 pRight = 0;.
107cd 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 return p;. }.
107ce 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 pLeft = rowSetND
107cf 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 eepTree(ppList,
107d0 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d iDepth-1);. p =
107d1 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 *ppList;. if(
107d2 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 p==0 ){. retu
107d3 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 rn pLeft;. }.
107d4 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 p->pLeft = pLeft
107d5 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d ;. *ppList = p-
107d6 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 >pRight;. p->pR
107d7 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 ight = rowSetNDe
107d8 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 epTree(ppList, i
107d9 44 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 Depth-1);. retu
107da 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn p;.}../*.** C
107db 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 onvert a sorted
107dc 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 list of elements
107dd 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 into a binary t
107de 72 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 ree. Make the tr
107df 65 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 ee.** as deep as
107e0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
107e1 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 in order to cont
107e2 61 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c ain the entire l
107e3 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ist..*/.static s
107e4 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
107e5 79 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 y *rowSetListToT
107e6 72 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 ree(struct RowSe
107e7 74 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a tEntry *pList){.
107e8 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 int iDepth;
107e9 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 /* Depth
107ea 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 of the tree so
107eb 66 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 far */. struct
107ec 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 RowSetEntry *p;
107ed 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
107ee 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 tree root */.
107ef 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
107f0 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 ry *pLeft; /*
107f1 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a Left subtree */.
107f2 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 . assert( pList
107f3 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 !=0 );. p = pLi
107f4 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d st;. pList = p-
107f5 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c >pRight;. p->pL
107f6 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 eft = p->pRight
107f7 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 = 0;. for(iDept
107f8 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 h=1; pList; iDep
107f9 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 th++){. pLeft
107fa 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c = p;. p = pL
107fb 69 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d ist;. pList =
107fc 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 p->pRight;.
107fd 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 p->pLeft = pLeft
107fe 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 ;. p->pRight
107ff 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 = rowSetNDeepTre
10800 65 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 e(&pList, iDepth
10801 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
10802 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 p;.}../*.** Conv
10803 65 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 ert the list in
10804 70 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 p->pEntry into a
10805 20 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 sorted list if
10806 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 it is not.** sor
10807 74 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 ted already. If
10808 20 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 there is a bina
10809 72 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 ry tree on p->pT
1080a 72 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e ree, then.** con
1080b 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c vert it into a l
1080c 69 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 ist too and merg
1080d 65 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d e it into the p-
1080e 3e 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f >pEntry list..*/
1080f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 .static void row
10810 53 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 SetToList(RowSet
10811 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e *p){. if( !p->
10812 69 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 isSorted ){.
10813 72 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 rowSetSort(p);.
10814 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 }. if( p->pTre
10815 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 e ){. struct
10816 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 RowSetEntry *pHe
10817 61 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 ad, *pTail;.
10818 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 rowSetTreeToList
10819 28 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 (p->pTree, &pHea
1081a 64 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 d, &pTail);.
1081b 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 p->pTree = 0;.
1081c 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f p->pEntry = ro
1081d 77 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e wSetMerge(p->pEn
1081e 74 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d try, pHead);. }
1081f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 .}../*.** Extrac
10820 74 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 t the smallest e
10821 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 lement from the
10822 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 RowSet..** Write
10823 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 the element int
10824 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 o *pRowid. Retu
10825 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e rn 1 on success.
10826 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 Return.** 0 if
10827 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 the RowSet is a
10828 6c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a lready empty..**
10829 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 .** After this r
1082a 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 outine has been
1082b 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 called, the sqli
1082c 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 te3RowSetInsert(
1082d 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 ).** routine may
1082e 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 not be called a
1082f 67 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 gain. .*/.SQLIT
10830 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
10831 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 lite3RowSetNext(
10832 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a RowSet *p, i64 *
10833 70 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 pRowid){. rowSe
10834 74 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 tToList(p);. if
10835 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 ( p->pEntry ){.
10836 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e *pRowid = p->
10837 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 pEntry->v;. p
10838 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 ->pEntry = p->pE
10839 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 ntry->pRight;.
1083a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d if( p->pEntry=
1083b 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1083c 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 te3RowSetClear(p
1083d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 );. }. ret
1083e 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a urn 1;. }else{.
1083f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
10840 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b }.}../*.** Check
10841 20 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 to see if eleme
10842 6e 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e nt iRowid was in
10843 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 serted into the
10844 74 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a the rowset as.**
10845 20 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 part of any ins
10846 65 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 ert batch prior
10847 74 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 to iBatch. Retu
10848 72 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 rn 1 or 0..*/.SQ
10849 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1084a 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 sqlite3RowSetTe
1084b 73 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 st(RowSet *pRowS
1084c 65 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 et, u8 iBatch, s
1084d 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f qlite3_int64 iRo
1084e 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 wid){. struct R
1084f 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 owSetEntry *p;.
10850 20 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f if( iBatch!=pRo
10851 77 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a wSet->iBatch ){.
10852 20 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d if( pRowSet-
10853 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 >pEntry ){.
10854 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 rowSetToList(pR
10855 6f 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 owSet);. pR
10856 6f 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 owSet->pTree = r
10857 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 owSetListToTree(
10858 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 pRowSet->pEntry)
10859 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d ;. pRowSet-
1085a 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 >pEntry = 0;.
1085b 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 pRowSet->pLas
1085c 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 t = 0;. }.
1085d 20 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 pRowSet->iBatch
1085e 20 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 = iBatch;. }.
1085f 20 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 p = pRowSet->pT
10860 72 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 ree;. while( p
10861 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c ){. if( p->v<
10862 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 iRowid ){.
10863 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 p = p->pRight;.
10864 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
10865 76 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 v>iRowid ){.
10866 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a p = p->pLeft;.
10867 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
10868 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
10869 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
1086a 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1086b 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 *** End of rowse
1086c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.c ************
1086d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1086e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1086f 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
10870 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 *** Begin file p
10871 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.c *********
10872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10874 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
10875 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
10876 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
10877 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
10878 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
10879 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1087a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1087b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1087c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1087d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1087e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1087f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
10880 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
10881 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
10882 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
10883 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
10884 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
10885 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
10886 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
10887 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
10888 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10889 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1088a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1088b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1088c 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d is is the implem
1088d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
1088e 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 page cache subsy
1088f 73 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e stem or "pager".
10890 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 .** .** The page
10891 72 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 r is used to acc
10892 65 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64 ess a database d
10893 69 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d isk file. It im
10894 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d plements.** atom
10895 69 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f ic commit and ro
10896 6c 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 llback through t
10897 68 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 he use of a jour
10898 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a nal file that.**
10899 20 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f is separate fro
1089a 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
1089b 69 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 ile. The pager
1089c 61 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 also implements
1089d 66 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 file.** locking
1089e 74 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 to prevent two p
1089f 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 rocesses from wr
108a0 69 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 iting the same d
108a1 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
108a2 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 simultaneously,
108a3 6f 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 or one process f
108a4 72 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 rom reading the
108a5 64 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a database while.*
108a6 2a 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 * another is wri
108a7 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 ting..**.** @(#)
108a8 20 24 49 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 $Id: pager.c,v
108a9 31 2e 35 38 36 2e 32 2e 31 20 32 30 30 39 2f 30 1.586.2.1 2009/0
108aa 35 2f 31 38 20 31 37 3a 31 31 3a 33 31 20 64 72 5/18 17:11:31 dr
108ab 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 h Exp $.*/.#ifnd
108ac 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
108ad 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 ISKIO../*.** Mac
108ae 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 ros for troubles
108af 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c hooting. Normal
108b0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f ly turned off.*/
108b1 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 .#if 0.int sqlit
108b2 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b 20 e3PagerTrace=1;
108b3 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 /* True to enab
108b4 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 le tracing */.#d
108b5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 62 efine sqlite3Deb
108b6 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 0a ugPrintf printf.
108b7 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 #define PAGERTRA
108b8 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73 71 CE(X) if( sq
108b9 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 20 lite3PagerTrace
108ba 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ){ sqlite3DebugP
108bb 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 rintf X; }.#else
108bc 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
108bd 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f ACE(X).#endif../
108be 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
108bf 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 ng two macros ar
108c0 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 e used within th
108c1 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20 6d e PAGERTRACE() m
108c2 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 acros above.** t
108c3 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 o print out file
108c4 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a -descriptors. .*
108c5 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74 *.** PAGERID() t
108c6 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 akes a pointer t
108c7 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 74 o a Pager struct
108c8 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 as its argument
108c9 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 . The.** associa
108ca 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 ted file-descrip
108cb 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e tor is returned.
108cc 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 FILEHANDLEID()
108cd 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33 takes an sqlite3
108ce 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 _file.** struct
108cf 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e as its argument.
108d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 .*/.#define PAGE
108d1 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d RID(p) ((int)(p-
108d2 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49 >fd)).#define FI
108d3 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28 LEHANDLEID(fd) (
108d4 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 (int)fd)../*.**
108d5 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 The page cache a
108d6 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 s a whole is alw
108d7 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 ays in one of th
108d8 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 e following.** s
108d9 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 tates:.**.** P
108da 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 AGER_UNLOCK
108db 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 The page cach
108dc 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 e is not current
108dd 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a ly reading or .*
108de 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
108df 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 20 writing
108e0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
108e1 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a e. There is no.
108e2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
108e3 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68 65 data he
108e4 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 ld in memory. T
108e5 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 69 his is the initi
108e6 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 al.**
108e7 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 stat
108e8 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 e..**.** PAGER
108e9 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20 54 _SHARED T
108ea 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73 he page cache is
108eb 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
108ec 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 abase..**
108ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108ee 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 Writing is not p
108ef 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65 ermitted. There
108f0 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 can be.**
108f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108f2 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 multiple reader
108f3 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 s accessing the
108f4 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a same database.**
108f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108f6 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 74 file at t
108f7 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a he same time..**
108f8 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 .** PAGER_RESE
108f9 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20 70 RVED This p
108fa 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 72 rocess has reser
108fb 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ved the database
108fc 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 for writing.**
108fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108fe 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f but has no
108ff 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63 t yet made any c
10900 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e hanges. Only on
10901 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 e process.**
10902 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10903 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e at a time can
10904 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61 74 reserve the dat
10905 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67 abase. The orig
10906 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 inal.**
10907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
10908 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 tabase file has
10909 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 not been modifie
1090a 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 d so other.**
1090b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1090c 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61 processes ma
1090d 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 y still be readi
1090e 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a ng the on-disk.*
1090f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
10910 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
10911 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 file..**.** P
10912 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 AGER_EXCLUSIVE
10913 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 The page cach
10914 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 65 e is writing the
10915 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 database..**
10916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10917 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65 78 Access is ex
10918 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 clusive. No oth
10919 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a er processes or.
1091a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1091b 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64 73 threads
1091c 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 can be reading
1091d 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65 or writing while
1091e 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 one.**
1091f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 pr
10920 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 ocess is writing
10921 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f ..**.** PAGER_
10922 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 68 SYNCED Th
10923 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f e pager moves to
10924 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d this state from
10925 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
10926 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
10927 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72 20 after
10928 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 all dirty pages
10929 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
1092a 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 n to the.**
1092b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1092c 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 database file
1092d 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 73 and the file has
1092e 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a been synced to.
1092f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
10930 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41 disk. A
10931 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 ll that remains
10932 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f to do is to remo
10933 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 ve or.**
10934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
10935 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 runcate the jour
10936 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65 nal file and the
10937 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a transaction .**
10938 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10939 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63 will be c
1093a 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ommitted..**.**
1093b 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 63 The page cache c
1093c 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 52 omes up in PAGER
1093d 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 _UNLOCK. The fi
1093e 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 rst time a.** sq
1093f 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
10940 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 74 occurs, the stat
10941 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f e transitions to
10942 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a PAGER_SHARED..*
10943 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65 * After all page
10944 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65 s have been rele
10945 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 ased using sqlit
10946 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a e_page_unref(),.
10947 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 61 ** the state tra
10948 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f nsitions back to
10949 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 PAGER_UNLOCK.
1094a 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a The first time.*
1094b 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 * that sqlite3Pa
1094c 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 gerWrite() is ca
1094d 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 lled, the state
1094e 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a transitions to.*
1094f 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 * PAGER_RESERVED
10950 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71 . (Note that sq
10951 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
10952 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a ) can only be.**
10953 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 called on an ou
10954 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77 tstanding page w
10955 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 hich means that
10956 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a the pager must.*
10957 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48 * be in PAGER_SH
10958 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 74 ARED before it t
10959 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 ransitions to PA
1095a 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a GER_RESERVED.).*
1095b 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 * PAGER_RESERVED
1095c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 means that ther
1095d 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c e is an open rol
1095e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a lback journal..*
1095f 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e * The transition
10960 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 to PAGER_EXCLUS
10961 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72 IVE occurs befor
10962 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a e any changes.**
10963 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 are made to the
10964 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
10965 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f though writes to
10966 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a the rollback.**
10967 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 journal occurs
10968 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f with just PAGER_
10969 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 72 RESERVED. After
1096a 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 an sqlite3Pager
1096b 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 Rollback().** or
1096c 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
1096d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74 mitPhaseTwo(), t
1096e 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20 he state can go
1096f 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 back to PAGER_SH
10970 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 ARED,.** or it c
10971 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 52 an stay at PAGER
10972 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 65 _EXCLUSIVE if we
10973 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 are in exclusiv
10974 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a e access mode..*
10975 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
10976 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 UNLOCK 0.#d
10977 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52 efine PAGER_SHAR
10978 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73 ED 1 /* s
10979 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f ame as SHARED_LO
1097a 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 CK */.#define PA
1097b 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 GER_RESERVED
1097c 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 2 /* same as R
1097d 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a ESERVED_LOCK */.
1097e 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58 #define PAGER_EX
1097f 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a CLUSIVE 4 /*
10980 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49 same as EXCLUSI
10981 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 VE_LOCK */.#defi
10982 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 ne PAGER_SYNCED
10983 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 5../*.** A
10984 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 69 macro used for i
10985 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 nvoking the code
10986 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e c if there is on
10987 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 e.*/.#ifdef SQLI
10988 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 TE_HAS_CODEC.# d
10989 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 efine CODEC1(P,D
1098a 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43 6f ,N,X) if( P->xCo
1098b 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43 6f dec!=0 ){ P->xCo
1098c 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 67 dec(P->pCodecArg
1098d 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65 66 ,D,N,X); }.# def
1098e 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e ine CODEC2(P,D,N
1098f 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d 3e ,X) ((char*)(P->
10990 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43 6f xCodec!=0?P->xCo
10991 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 67 dec(P->pCodecArg
10992 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c 73 ,D,N,X):D)).#els
10993 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 e.# define CODEC
10994 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e 4f 1(P,D,N,X) /* NO
10995 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 -OP */.# define
10996 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29 20 CODEC2(P,D,N,X)
10997 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64 69 ((char*)D).#endi
10998 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 f../*.** The max
10999 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 imum allowed sec
1099a 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e 20 tor size. 16MB.
1099b 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 If the xSectorsi
1099c 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 ze() method .**
1099d 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 returns a value
1099e 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 larger than this
1099f 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f , then MAX_SECTO
109a0 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 R_SIZE is used i
109a1 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 nstead..** This
109a2 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c could conceivabl
109a3 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 y cause corrupti
109a4 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 on following a p
109a5 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a ower failure on.
109a6 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d ** such a system
109a7 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e . This is curren
109a8 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e tly an undocumen
109a9 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 ted limit..*/.#d
109aa 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 efine MAX_SECTOR
109ab 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30 0a _SIZE 0x0100000.
109ac 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
109ad 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
109ae 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
109af 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 allocated for e
109b0 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 ach active.** sa
109b1 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 vepoint and stat
109b2 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
109b3 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e n in the system.
109b4 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74 All such struct
109b5 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 ures.** are stor
109b6 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e ed in the Pager.
109b7 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 aSavepoint[] arr
109b8 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c ay, which is all
109b9 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 ocated and.** re
109ba 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69 sized using sqli
109bb 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a te3Realloc()..**
109bc 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70 .** When a savep
109bd 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c oint is created,
109be 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
109bf 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 int.iHdrOffset f
109c0 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 ield is.** set t
109c1 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 o 0. If a journa
109c2 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74 l-header is writ
109c3 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 ten into the mai
109c4 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a n journal while.
109c5 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 ** the savepoint
109c6 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e is active, then
109c7 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 iHdrOffset is s
109c8 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f et to the byte o
109c9 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 ffset .** immedi
109ca 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
109cb 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c the last journal
109cc 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20 record written
109cd 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a into the main.**
109ce 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 journal before
109cf 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 the journal-head
109d0 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 er. This is requ
109d1 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 65 ired during save
109d2 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 point.** rollbac
109d3 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 79 k (see pagerPlay
109d4 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 backSavepoint())
109d5 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
109d6 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 uct PagerSavepoi
109d7 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e nt PagerSavepoin
109d8 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 53 t;.struct PagerS
109d9 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 avepoint {. i64
109da 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
109db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
109dc 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 rting offset in
109dd 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a main journal */.
109de 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 74 i64 iHdrOffset
109df 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
109e0 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 * See above */.
109e1 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65 Bitvec *pInSave
109e2 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a point; /*
109e3 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e Set of pages in
109e4 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20 this savepoint
109e5 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b */. Pgno nOrig;
109e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109e7 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 /* Original nu
109e8 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
109e9 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 file */. Pgno
109ea 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 iSubRec;
109eb 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
109ec 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 64 of first record
109ed 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 in sub-journal
109ee 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f */.};../*.** A o
109ef 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 pen page cache i
109f0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
109f1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
109f2 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
109f3 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 errCode.**.**
109f4 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 Pager.errCode ma
109f5 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 y be set to SQLI
109f6 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 TE_IOERR, SQLITE
109f7 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 _CORRUPT, or.**
109f8 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c or SQLITE_FULL
109f9 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 . Once one of th
109fa 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72 e first three er
109fb 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 rors occurs, it
109fc 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e persists.** an
109fd 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 d is returned as
109fe 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 the result of e
109ff 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 very major pager
10a00 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a API call. The.
10a01 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c ** SQLITE_FULL
10a02 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 return code is
10a03 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 slightly differe
10a04 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 nt. It persists
10a05 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a only until the.*
10a06 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73 73 * next success
10a07 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 ful rollback is
10a08 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 performed on the
10a09 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c pager cache. Al
10a0a 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f so,.** SQLITE_
10a0b 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 FULL does not af
10a0c 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33 fect the sqlite3
10a0d 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73 PagerGet() and s
10a0e 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
10a0f 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 p().** APIs, t
10a10 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 hey may still be
10a11 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 6c used successful
10a12 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 ly..**.** dbSize
10a13 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 Valid, dbSize, d
10a14 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c bOrigSize, dbFil
10a15 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 eSize.**.** Ma
10a16 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20 naging the size
10a17 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
10a18 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73 file in pages is
10a19 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 a little compli
10a1a 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 cated..** The
10a1b 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 variable Pager.d
10a1c 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 bSize contains t
10a1d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
10a1e 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 es that the data
10a1f 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 base.** image
10a20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 currently contai
10a21 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61 62 ns. As the datab
10a22 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20 ase image grows
10a23 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a or shrinks this.
10a24 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69 73 ** variable is
10a25 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76 61 updated. The va
10a26 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46 riable Pager.dbF
10a27 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 ileSize contains
10a28 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 the number.**
10a29 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
10a2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
10a2b 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 66 This may be diff
10a2c 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 erent from Pager
10a2d 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 .dbSize.** if
10a2e 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65 20 some pages have
10a2f 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f been appended to
10a30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
10a31 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20 age but not yet
10a32 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 written.** out
10a33 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
10a34 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 to the actual fi
10a35 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 le on disk. Or i
10a36 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73 20 f the image has
10a37 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 been.** trunca
10a38 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d ted by an increm
10a39 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 ental-vacuum ope
10a3a 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 ration. The Page
10a3b 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72 r.dbOrigSize var
10a3c 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 iable.** conta
10a3d 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ins the number o
10a3e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
10a3f 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 68 atabase image wh
10a40 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a en the current.*
10a41 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 * transaction
10a42 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 was opened. The
10a43 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 contents of all
10a44 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 three of these v
10a45 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 ariables is.**
10a46 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64 only guaranteed
10a47 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69 to be correct i
10a48 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 f the boolean Pa
10a49 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 ger.dbSizeValid
10a4a 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 is true..**.**
10a4b 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 TODO: Under wha
10a4c 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 t conditions is
10a4d 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f dbSizeValid set?
10a4e 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 Cleared?.**.**
10a4f 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a changeCountDone.
10a50 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f **.** This boo
10a51 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73 lean variable is
10a52 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 used to make su
10a53 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 6e re that the chan
10a54 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 ge-counter .**
10a55 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65 61 (the 4-byte hea
10a56 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79 74 der field at byt
10a57 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74 e offset 24 of t
10a58 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10a59 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 ) is .** not u
10a5a 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 pdated more ofte
10a5b 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 n than necessary
10a5c 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 . .**.** It is
10a5d 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68 65 set to true whe
10a5e 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 n the change-cou
10a5f 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75 70 nter field is up
10a60 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a dated, which .**
10a61 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 can only happ
10a62 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69 en if an exclusi
10a63 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 ve lock is held
10a64 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
10a65 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 file..** It is
10a66 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f cleared (set to
10a67 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72 false) whenever
10a68 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
10a69 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 ck is .** reli
10a6a 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20 nquished on the
10a6b 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45 database file. E
10a6c 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 ach time a trans
10a6d 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
10a6e 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 ted,.** The ch
10a6f 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c angeCountDone fl
10a70 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e ag is inspected.
10a71 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c 20 If it is true,
10a72 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 the work of.**
10a73 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63 68 updating the ch
10a74 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 ange-counter is
10a75 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65 20 omitted for the
10a76 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
10a77 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 ion..**.** Thi
10a78 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e s mechanism mean
10a79 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e s that when runn
10a7a 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 ing in exclusive
10a7b 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 mode, a connect
10a7c 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f ion .** need o
10a7d 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 63 nly update the c
10a7e 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e hange-counter on
10a7f 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73 ce, for the firs
10a80 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a t transaction.**
10a81 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a committed..**
10a82 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a .** dbModified.*
10a83 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 *.** The dbMod
10a84 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73 65 ified flag is se
10a85 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61 74 t whenever a dat
10a86 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64 69 abase page is di
10a87 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 rtied..** It i
10a88 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 65 s cleared at the
10a89 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61 end of each tra
10a8a 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
10a8b 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68 65 It is used whe
10a8c 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 n committing or
10a8d 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67 otherwise ending
10a8e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
10a8f 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f If.** the dbMo
10a90 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 63 dified flag is c
10a91 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77 lear then less w
10a92 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f ork has to be do
10a93 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 ne..**.** journa
10a94 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 lStarted.**.**
10a95 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 This flag is se
10a96 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 74 t whenever the t
10a97 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
10a98 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a is synced. .**.*
10a99 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 * The point of
10a9a 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74 68 this flag is th
10a9b 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73 65 at it must be se
10a9c 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 t after the .**
10a9d 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 first journal
10a9e 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 header in a jour
10a9f 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65 nal file has bee
10aa0 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b n synced to disk
10aa1 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69 ..** After thi
10aa2 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 s has happened,
10aa3 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e 64 new pages append
10aa4 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
10aa5 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 se .** do not
10aa6 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e need the PGHDR_N
10aa7 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 EED_SYNC flag se
10aa8 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f t, as they do no
10aa9 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 t need.** to w
10aaa 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 ait for a journa
10aab 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68 l sync before th
10aac 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 ey can be writte
10aad 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 n out to.** th
10aae 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
10aaf 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 (see function pa
10ab0 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a ger_write())..**
10ab1 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 .** setMaster
10ab2 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 .**.** This va
10ab3 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 riable is used t
10ab4 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 o ensure that th
10ab5 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
10ab6 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 file name.**
10ab7 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 (if any) is only
10ab8 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
10ab9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
10aba 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 nce..**.** Whe
10abb 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 n committing a t
10abc 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 ransaction, the
10abd 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
10abe 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 ile name (if any
10abf 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72 ).** may be wr
10ac0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
10ac1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c ournal file whil
10ac2 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 73 e the pager is s
10ac3 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 till in.** PAG
10ac4 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 ER_RESERVED stat
10ac5 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 e (see CommitPha
10ac6 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20 seOne() for the
10ac7 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 action). It.**
10ac8 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 then attempts t
10ac9 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20 o upgrade to an
10aca 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 exclusive lock.
10acb 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a If this attempt.
10acc 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e ** fails, then
10acd 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 SQLITE_BUSY may
10ace 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 be returned to
10acf 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68 65 the user and the
10ad0 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 user.** may a
10ad1 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 ttempt to commit
10ad2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
10ad3 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61 again later (ca
10ad4 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 lling.** Commi
10ad5 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69 tPhaseOne() agai
10ad6 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73 n). This flag is
10ad7 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 used to ensure
10ad8 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d that the .** m
10ad9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
10ada 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 me is only writt
10adb 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 en to the journa
10adc 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73 74 l file the first
10add 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 .** time Commi
10ade 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 tPhaseOne() is c
10adf 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e alled..**.** doN
10ae0 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 otSync.**.** T
10ae1 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 his variable is
10ae2 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20 set and cleared
10ae3 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 by sqlite3PagerW
10ae4 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 rite()..**.** ne
10ae5 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 edSync.**.** T
10ae6 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65 ODO: It might be
10ae7 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20 74 easier to set t
10ae8 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 his variable in
10ae9 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
10aea 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65 ).** and write
10aeb 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 MasterJournal()
10aec 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73 only. Change its
10aed 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 meaning to "uns
10aee 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 ynced data.**
10aef 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e has been written
10af0 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 to the journal"
10af1 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 ..**.** subjInMe
10af2 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 mory.**.** Thi
10af3 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 s is a boolean v
10af4 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65 ariable. If true
10af5 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 , then any requi
10af6 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a red sub-journal.
10af7 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61 ** is opened a
10af8 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a s an in-memory j
10af9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
10afa 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d false, then in-m
10afb 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a emory.** sub-j
10afc 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 ournals are only
10afd 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d used for in-mem
10afe 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e ory pager files.
10aff 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72 .*/.struct Pager
10b00 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 {. sqlite3_vfs
10b01 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
10b02 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 /* OS functions
10b03 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a to use for IO *
10b04 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 /. u8 exclusive
10b05 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Mode;
10b06 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 /* Boolean. True
10b07 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 if locking_mode
10b08 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 ==EXCLUSIVE */.
10b09 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b u8 journalMode;
10b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b0b 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f On of the PAGER_
10b0c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 JOURNALMODE_* va
10b0d 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 lues */. u8 use
10b0e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
10b0f 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 /* Use a r
10b10 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
10b11 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a on this file */.
10b12 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b u8 noReadlock;
10b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b14 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 Do not bother t
10b15 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 o obtain readloc
10b16 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e ks */. u8 noSyn
10b17 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
10b18 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 /* Do not sy
10b19 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 nc the journal i
10b1a 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 f true */. u8 f
10b1b 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20 ullSync;
10b1c 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 /* Do ex
10b1d 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65 tra syncs of the
10b1e 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 journal for rob
10b1f 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 ustness */. u8
10b20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 sync_flags;
10b21 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
10b22 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f of SYNC_NORMAL o
10b23 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 r SYNC_FULL */.
10b24 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 u8 tempFile;
10b25 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b26 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 zFilename is a t
10b27 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f emporary file */
10b28 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
10b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b2a 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61 * True for a rea
10b2b 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 d-only database
10b2c 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 */. u8 memDb;
10b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b2e 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69 /* True to inhi
10b2f 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f bit all file I/O
10b30 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f */.. /* The fo
10b31 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f llowing block co
10b32 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 ntains those cla
10b33 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20 ss members that
10b34 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a are dynamically.
10b35 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 ** modified du
10b36 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 ring normal oper
10b37 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 ations. The othe
10b38 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 r variables in t
10b39 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20 20 his structure.
10b3a 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63 6f ** are either co
10b3b 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 nstant throughou
10b3c 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f t the lifetime o
10b3d 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20 f the pager, or
10b3e 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 else. ** used t
10b3f 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 o store configur
10b40 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 ation parameters
10b41 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 65 that affect the
10b42 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20 0a way the pager .
10b43 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 ** operates..
10b44 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 **. ** The 'st
10b45 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69 73 ate' variable is
10b46 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f described in mo
10b47 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 re detail along
10b48 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 with the. ** de
10b49 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 scriptions of th
10b4a 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20 e values it may
10b4b 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c take - PAGER_UNL
10b4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 OCK etc. Many of
10b4d 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 the. ** other
10b4e 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 variables in thi
10b4f 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 s block are desc
10b50 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d ribed in the com
10b51 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20 ment directly .
10b52 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63 ** above this c
10b53 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e lass definition.
10b54 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 . */. u8 state
10b55 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b56 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c /* PAGER_UNL
10b57 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 OCK, _SHARED, _R
10b58 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f ESERVED, etc. */
10b59 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 . u8 dbModified
10b5a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
10b5b 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 * True if there
10b5c 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 are any changes
10b5d 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 to the Db */. u
10b5e 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 8 needSync;
10b5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
10b60 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 ue if an fsync()
10b61 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 is needed on th
10b62 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 e journal */. u
10b63 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 8 journalStarted
10b64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ; /* Tr
10b65 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 ue if header of
10b66 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 journal is synce
10b67 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 d */. u8 change
10b68 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 CountDone;
10b69 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20 /* Set after
10b6a 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 incrementing the
10b6b 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
10b6c 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65 */. u8 setMaste
10b6d 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
10b6e 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d /* True if a m-
10b6f 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 j name has been
10b70 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 written to jrnl
10b71 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e */. u8 doNotSyn
10b72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
10b73 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 /* Boolean. Whi
10b74 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 le true, do not
10b75 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 spill the cache
10b76 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61 */. u8 dbSizeVa
10b77 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 lid;
10b78 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53 /* Set when dbS
10b79 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a ize is correct *
10b7a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d /. u8 subjInMem
10b7b 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 ory;
10b7c 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69 /* True to use i
10b7d 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 n-memory sub-jou
10b7e 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 rnals */. Pgno
10b7f 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 dbSize;
10b80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
10b81 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
10b82 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 database */. P
10b83 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 gno dbOrigSize;
10b84 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 /* db
10b85 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 Size before the
10b86 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
10b87 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 ion */. Pgno db
10b88 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 FileSize;
10b89 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
10b8a 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
10b8b 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
10b8c 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 int errCode;
10b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b8e 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 One of several
10b8f 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 kinds of errors
10b90 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 */. int nRec;
10b91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b92 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 /* Pages journa
10b93 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 lled since last
10b94 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e j-header written
10b95 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 */. u32 cksumI
10b96 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
10b97 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f /* Quasi-rando
10b98 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f m value added to
10b99 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 every checksum
10b9a 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 */. u32 nSubRec
10b9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b9c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
10b9d 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f cords written to
10b9e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
10b9f 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 Bitvec *pInJou
10ba0 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a rnal; /*
10ba1 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 One bit for eac
10ba2 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 h page in the da
10ba3 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
10ba4 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 sqlite3_file *f
10ba5 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
10ba6 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 File descriptor
10ba7 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a for database */.
10ba8 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
10ba9 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a jfd; /*
10baa 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
10bab 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 for main journa
10bac 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 l */. sqlite3_f
10bad 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 ile *sjfd;
10bae 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
10baf 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f iptor for sub-jo
10bb0 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a urnal */. i64 j
10bb1 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 ournalOff;
10bb2 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
10bb3 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 t write offset i
10bb4 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
10bb5 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 le */. i64 jour
10bb6 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 nalHdr;
10bb7 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 /* Byte offs
10bb8 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a et to previous j
10bb9 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f ournal header */
10bba 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e . PagerSavepoin
10bbb 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f t *aSavepoint; /
10bbc 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 * Array of activ
10bbd 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a e savepoints */.
10bbe 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 int nSavepoint
10bbf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
10bc0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
10bc1 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e nts in aSavepoin
10bc2 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 t[] */. char db
10bc3 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 FileVers[16];
10bc4 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 /* Changes
10bc5 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 whenever databas
10bc6 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a e file changes *
10bc7 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 /. u32 sectorSi
10bc8 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
10bc9 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f /* Assumed secto
10bca 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f r size during ro
10bcb 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e 74 llback */.. int
10bcc 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 nExtra;
10bcd 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 /* Add
10bce 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 this many bytes
10bcf 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 to each in-memor
10bd0 79 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 y page */. u32
10bd1 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 vfsFlags;
10bd2 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
10bd3 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 for sqlite3_vfs
10bd4 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e .xOpen() */. in
10bd5 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 t pageSize;
10bd6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
10bd7 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
10bd8 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f a page */. Pgno
10bd9 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 mxPgno;
10bda 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
10bdb 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 um allowed size
10bdc 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
10bdd 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 */. char *zFile
10bde 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 name;
10bdf 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
10be0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
10be1 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 . char *zJourna
10be2 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f l; /
10be3 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f * Name of the jo
10be4 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 urnal file */.
10be5 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c int (*xBusyHandl
10be6 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 er)(void*); /* F
10be7 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 unction to call
10be8 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 when busy */. v
10be9 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 oid *pBusyHandle
10bea 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f rArg; /* Co
10beb 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 ntext argument f
10bec 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 or xBusyHandler
10bed 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
10bee 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 _TEST. int nHit
10bef 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 , nMiss;
10bf0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 /* Cache hit
10bf1 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f s and missing */
10bf2 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 . int nRead, nW
10bf3 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f rite; /
10bf4 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 * Database pages
10bf5 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f read/written */
10bf6 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 .#endif. void (
10bf7 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 *xReiniter)(DbPa
10bf8 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 ge*); /* Call th
10bf9 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 is routine when
10bfa 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 reloading pages
10bfb 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
10bfc 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 _HAS_CODEC. voi
10bfd 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 d *(*xCodec)(voi
10bfe 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e d*,void*,Pgno,in
10bff 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 t); /* Routine f
10c00 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 or en/decoding d
10c01 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ata */. void *p
10c02 43 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 CodecArg;
10c03 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 /* First ar
10c04 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 gument to xCodec
10c05 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 () */.#endif. c
10c06 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 har *pTmpSpace;
10c07 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
10c08 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 ger.pageSize byt
10c09 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 es of space for
10c0a 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 tmp use */. i64
10c0b 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 journalSizeLimi
10c0c 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 t; /* Size
10c0d 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 limit for persi
10c0e 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
10c0f 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 les */. PCache
10c10 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pPCache;
10c11 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
10c12 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 to page cache ob
10c13 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ject */. sqlite
10c14 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 3_backup *pBacku
10c15 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 p; /* Pointer
10c16 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f to list of ongo
10c17 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 ing backup proce
10c18 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a sses */.};../*.*
10c19 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
10c1a 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 global variables
10c1b 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 hold counters u
10c1c 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 sed for.** testi
10c1d 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 ng purposes only
10c1e 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c . These variabl
10c1f 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 es do not exist
10c20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 in.** a non-test
10c21 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 ing build. Thes
10c22 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 e variables are
10c23 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e not thread-safe.
10c24 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
10c25 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
10c26 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 I int sqlite3_pa
10c27 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 ger_readdb_count
10c28 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 = 0; /* Numb
10c29 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 er of full pages
10c2a 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f read from DB */
10c2b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
10c2c 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 sqlite3_pager_wr
10c2d 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b itedb_count = 0;
10c2e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
10c2f 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 full pages writt
10c30 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 en to DB */.SQLI
10c31 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
10c32 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f e3_pager_writej_
10c33 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a count = 0; /*
10c34 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
10c35 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 written to jour
10c36 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 nal */.# define
10c37 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 PAGER_INCR(v) v
10c38 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ++.#else.# defin
10c39 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a e PAGER_INCR(v).
10c3a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 #endif..../*.**
10c3b 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 Journal files be
10c3c 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c gin with the fol
10c3d 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 lowing magic str
10c3e 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a ing. The data.*
10c3f 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 * was obtained f
10c40 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e rom /dev/random.
10c41 20 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c It is used onl
10c42 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 y as a sanity ch
10c43 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 eck..**.** Since
10c44 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 version 2.8.0,
10c45 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d the journal form
10c46 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 at contains addi
10c47 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a tional sanity.**
10c48 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d checking inform
10c49 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 ation. If the p
10c4a 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 ower fails while
10c4b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
10c4c 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e being.** written
10c4d 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 , semi-random ga
10c4e 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 rbage data might
10c4f 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a appear in the j
10c50 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 ournal.** file a
10c51 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 fter power is re
10c52 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 stored. If an a
10c53 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d ttempt is then m
10c54 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 ade.** to roll t
10c55 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c he journal back,
10c56 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
10c57 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 uld be corrupted
10c58 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 . The additiona
10c59 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 l.** sanity chec
10c5a 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 king data is an
10c5b 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f attempt to disco
10c5c 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 ver the garbage
10c5d 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 in the.** journa
10c5e 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e l and ignore it.
10c5f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 .**.** The sanit
10c60 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 y checking infor
10c61 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e mation for the n
10c62 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 ew journal forma
10c63 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 t consists.** of
10c64 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 a 32-bit checks
10c65 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 um on each page
10c66 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 of data. The ch
10c67 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f ecksum covers bo
10c68 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e th.** the page n
10c69 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 umber and the pP
10c6a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
10c6b 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 ytes of data for
10c6c 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 the page..** Th
10c6d 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 is cksum is init
10c6e 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d ialized to a 32-
10c6f 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 bit random value
10c70 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e that appears in
10c71 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
10c72 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 file right after
10c73 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 the header. Th
10c74 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c e random initial
10c75 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e izer is importan
10c76 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 t,.** because ga
10c77 72 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 rbage data that
10c78 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 appears at the e
10c79 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 nd of a journal
10c7a 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 is likely.** dat
10c7b 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 a that was once
10c7c 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 in other files t
10c7d 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 hat have now bee
10c7e 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 n deleted. If t
10c7f 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 he.** garbage da
10c80 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 ta came from an
10c81 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c obsolete journal
10c82 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b file, the check
10c83 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 sums might.** be
10c84 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 correct. But b
10c85 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
10c86 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 he checksum to r
10c87 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 andom value whic
10c88 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e h.** is differen
10c89 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 t for every jour
10c8a 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 nal, we minimize
10c8b 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 that risk..*/.s
10c8c 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 tatic const unsi
10c8d 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e gned char aJourn
10c8e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 alMagic[] = {.
10c8f 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 0xd9, 0xd5, 0x05
10c90 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 , 0xf9, 0x20, 0x
10c91 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a a1, 0x63, 0xd7,.
10c92 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 };../*.** The si
10c93 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 ze of the of eac
10c94 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e h page record in
10c95 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
10c96 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 given by.** the
10c97 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e following macro.
10c98 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 .*/.#define JOUR
10c99 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 NAL_PG_SZ(pPager
10c9a 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 ) ((pPager->pag
10c9b 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a eSize) + 8)../*.
10c9c 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** The journal h
10c9d 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 eader size for t
10c9e 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 his pager. This
10c9f 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 is usually the s
10ca0 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 ame .** size as
10ca1 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 a single disk se
10ca2 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 ctor. See also s
10ca3 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a etSectorSize()..
10ca4 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
10ca5 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
10ca6 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f ) (pPager->secto
10ca7 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 rSize)../*.** Th
10ca8 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 e macro MEMDB is
10ca9 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20 true if we are
10caa 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 dealing with an
10cab 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
10cac 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 se..** We do thi
10cad 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 s as a macro so
10cae 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 that if the SQLI
10caf 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 TE_OMIT_MEMORYDB
10cb0 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a macro is set,.*
10cb1 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d * the value of M
10cb2 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 EMDB will be a c
10cb3 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 onstant and the
10cb4 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 compiler will op
10cb5 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f timize.** out co
10cb6 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 de that would ne
10cb7 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a ver execute..*/.
10cb8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
10cb9 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 IT_MEMORYDB.# de
10cba 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c fine MEMDB 0.#el
10cbb 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 se.# define MEMD
10cbc 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a B pPager->memDb.
10cbd 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
10cbe 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 e maximum legal
10cbf 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 page number is (
10cc0 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 2^31 - 1)..*/.#d
10cc1 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f efine PAGER_MAX_
10cc2 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a PGNO 2147483647.
10cc3 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 .#ifndef NDEBUG
10cc4 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a ./*.** Usage:.**
10cc5 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 .** assert( as
10cc6 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
10cc7 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 (pPager) );.*/.s
10cc8 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 tatic int assert
10cc9 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 _pager_state(Pag
10cca 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 er *pPager){..
10ccb 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 /* A temp-file i
10ccc 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 s always in PAGE
10ccd 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 R_EXCLUSIVE or P
10cce 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 AGER_SYNCED stat
10ccf 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 e. */. assert(
10cd0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
10cd1 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 ==0 || pPager->s
10cd2 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
10cd3 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 USIVE );.. /* T
10cd4 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
10cd5 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 ne flag is alway
10cd6 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 s set for temp-f
10cd7 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 iles */. assert
10cd8 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
10cd9 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d le==0 || pPager-
10cda 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
10cdb 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b );.. return 1;
10cdc 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
10cdd 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
10cde 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 it is necessary
10cdf 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 to write page *p
10ce0 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d Pg into the sub-
10ce1 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 journal..** A pa
10ce2 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 ge needs to be w
10ce3 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
10ce4 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 sub-journal if t
10ce5 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a here exists one.
10ce6 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 ** or more open
10ce7 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 savepoints for w
10ce8 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 hich:.**.** *
10ce9 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 The page-number
10cea 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
10ceb 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 equal to PagerSa
10cec 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 vepoint.nOrig, a
10ced 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 nd.** * The bi
10cee 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
10cef 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 to the page-numb
10cf0 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e er is not set in
10cf1 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 .** PagerSav
10cf2 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f epoint.pInSavepo
10cf3 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 int..*/.static i
10cf4 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 nt subjRequiresP
10cf5 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
10cf6 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 . Pgno pgno = p
10cf7 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 Pg->pgno;. Page
10cf8 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
10cf9 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 >pPager;. int i
10cfa 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
10cfb 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
10cfc 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 t; i++){. Pag
10cfd 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d erSavepoint *p =
10cfe 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 &pPager->aSavep
10cff 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 oint[i];. if(
10d00 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 p->nOrig>=pgno
10d01 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 && 0==sqlite3Bit
10d02 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 vecTest(p->pInSa
10d03 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 vepoint, pgno) )
10d04 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
10d05 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
10d06 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
10d07 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
10d08 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 the page is alre
10d09 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ady in the journ
10d0a 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 al file..*/.stat
10d0b 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 ic int pageInJou
10d0c 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 rnal(PgHdr *pPg)
10d0d 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
10d0e 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67 e3BitvecTest(pPg
10d0f 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ->pPager->pInJou
10d10 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 rnal, pPg->pgno)
10d11 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
10d12 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
10d13 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 from the given
10d14 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
10d15 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 Store the inte
10d16 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 ger.** that is r
10d17 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 ead in *pRes. R
10d18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
10d19 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
10d1a 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 rked, or an.** e
10d1b 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d rror code is som
10d1c 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ething goes wron
10d1d 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c g..**.** All val
10d1e 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f ues are stored o
10d1f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e n disk as big-en
10d20 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dian..*/.static
10d21 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73 int read32bits(s
10d22 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c qlite3_file *fd,
10d23 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 i64 offset, u32
10d24 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 *pRes){. unsig
10d25 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a ned char ac[4];.
10d26 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
10d27 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c e3OsRead(fd, ac,
10d28 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 sizeof(ac), off
10d29 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d set);. if( rc==
10d2a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10d2b 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 *pRes = sqlite3
10d2c 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 Get4byte(ac);.
10d2d 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
10d2e 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ../*.** Write a
10d2f 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 32-bit integer i
10d30 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 nto a string buf
10d31 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 fer in big-endia
10d32 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f n byte order..*/
10d33 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 .#define put32bi
10d34 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 ts(A,B) sqlite3
10d35 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c Put4byte((u8*)A,
10d36 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 B)../*.** Write
10d37 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
10d38 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 into the given
10d39 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
10d3a 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
10d3b 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 OK.** on success
10d3c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
10d3d 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 e is something g
10d3e 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 oes wrong..*/.st
10d3f 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 atic int write32
10d40 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c bits(sqlite3_fil
10d41 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 e *fd, i64 offse
10d42 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 t, u32 val){. c
10d43 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 har ac[4];. put
10d44 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 32bits(ac, val);
10d45 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
10d46 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 3OsWrite(fd, ac,
10d47 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 4, offset);.}..
10d48 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 /*.** The argume
10d49 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f nt to this macro
10d4a 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 is a file descr
10d4b 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 iptor (type sqli
10d4c 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 te3_file*)..** R
10d4d 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 eturn 0 if it is
10d4e 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f not open, or no
10d4f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 n-zero (but not
10d50 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 1) if it is..**.
10d51 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 ** This is so th
10d52 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 at expressions c
10d53 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 an be written as
10d54 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 :.**.** if( is
10d55 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
10d56 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 ) ){ ....**.** i
10d57 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 nstead of.**.**
10d58 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 if( pPager->jf
10d59 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e d->pMethods ){ .
10d5a 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 ...*/.#define is
10d5b 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 Open(pFd) ((pFd)
10d5c 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a ->pMethods)../*.
10d5d 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 ** If file pFd i
10d5e 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c s open, call sql
10d5f 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f ite3OsUnlock() o
10d60 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 n it..*/.static
10d61 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c int osUnlock(sql
10d62 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 ite3_file *pFd,
10d63 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 int eLock){. if
10d64 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 ( !isOpen(pFd) )
10d65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
10d66 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
10d67 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e turn sqlite3OsUn
10d68 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 lock(pFd, eLock)
10d69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
10d6a 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 function determi
10d6b 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
10d6c 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 ot the atomic-wr
10d6d 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
10d6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 .** can be used
10d6f 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e with this pager.
10d70 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f The optimizatio
10d71 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 n can be used if
10d72 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 :.**.** (a) the
10d73 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
10d74 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 by OsDeviceChara
10d75 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 cteristics() ind
10d76 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 icates that.**
10d77 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 a database p
10d78 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 age may be writt
10d79 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 en atomically, a
10d7a 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 nd.** (b) the v
10d7b 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
10d7c 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 OsSectorSize()
10d7d 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
10d7e 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f equal.** to
10d7f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a the page size..
10d80 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 **.** The optimi
10d81 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 zation is also a
10d82 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f lways enabled fo
10d83 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 r temporary file
10d84 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 s. It is.** an e
10d85 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 rror to call thi
10d86 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 s function if pP
10d87 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f ager is opened o
10d88 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a n an in-memory.*
10d89 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a * database..**.*
10d8a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a * If the optimiz
10d8b 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 ation cannot be
10d8c 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 used, 0 is retur
10d8d 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 ned. If it can b
10d8e 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 e used,.** then
10d8f 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
10d90 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ed is the size o
10d91 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
10d92 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f le when it.** co
10d93 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 ntains rollback
10d94 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 data for exactly
10d95 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 one page..*/.#i
10d96 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
10d97 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
10d98 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 static int jrnlB
10d99 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 ufferSize(Pager
10d9a 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 *pPager){. asse
10d9b 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
10d9c 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d if( !pPager->tem
10d9d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 pFile ){. int
10d9e 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 dc;
10d9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10da0 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74 * Device charact
10da1 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 eristics */.
10da2 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 int nSector;
10da3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10da4 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 /* Sector size
10da5 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 */. int szPa
10da6 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
10da7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
10da8 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 e size */.. a
10da9 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
10daa 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 ager->fd) );.
10dab 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 dc = sqlite3OsD
10dac 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
10dad 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
10dae 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 ;. nSector =
10daf 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
10db0 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d ze;. szPage =
10db1 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
10db2 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 e;.. assert(S
10db3 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
10db4 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 IC512==(512>>8))
10db5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c ;. assert(SQL
10db6 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
10db7 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 64K==(65536>>8))
10db8 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 ;. if( 0==(dc
10db9 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 &(SQLITE_IOCAP_A
10dba 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 TOMIC|(szPage>>8
10dbb 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a )) || nSector>sz
10dbc 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 Page) ){. r
10dbd 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
10dbe 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 }.. return JOU
10dbf 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
10dc0 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 er) + JOURNAL_PG
10dc1 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 _SZ(pPager);.}.#
10dc2 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
10dc3 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
10dc4 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 ES is defined th
10dc5 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 en we do some sa
10dc6 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a nity checking.**
10dc7 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 on the cache us
10dc8 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 ing a hash funct
10dc9 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 ion. This is us
10dca 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a ed for testing.*
10dcb 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 * and debugging
10dcc 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 only..*/.#ifdef
10dcd 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
10dce 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ES./*.** Return
10dcf 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 a 32-bit hash of
10dd0 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 the page data f
10dd1 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 or pPage..*/.sta
10dd2 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 tic u32 pager_da
10dd3 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 tahash(int nByte
10dd4 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
10dd5 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 *pData){. u32 h
10dd6 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 ash = 0;. int i
10dd7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
10dd8 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Byte; i++){.
10dd9 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 hash = (hash*103
10dda 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 9) + pData[i];.
10ddb 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 }. return hash
10ddc 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 ;.}.static u32 p
10ddd 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 ager_pagehash(Pg
10dde 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 Hdr *pPage){. r
10ddf 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 eturn pager_data
10de0 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 hash(pPage->pPag
10de1 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 er->pageSize, (u
10de2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
10de3 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a Page->pData);.}.
10de4 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
10de5 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 r_set_pagehash(P
10de6 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
10de7 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 pPage->pageHash
10de8 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
10de9 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (pPage);.}../*.*
10dea 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 * The CHECK_PAGE
10deb 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 macro takes a P
10dec 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 gHdr* as an argu
10ded 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f ment. If SQLITE_
10dee 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 CHECK_PAGES.** i
10def 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e s defined, and N
10df0 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 DEBUG is not def
10df1 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 ined, an assert(
10df2 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 ) statement chec
10df3 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 ks.** that the p
10df4 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69 age is either di
10df5 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 rty or still mat
10df6 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 ches the calcula
10df7 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a ted page-hash..*
10df8 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f /.#define CHECK_
10df9 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 PAGE(x) checkPag
10dfa 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 e(x).static void
10dfb 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 checkPage(PgHdr
10dfc 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
10dfd 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
10dfe 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 Pager;. assert(
10dff 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 !pPg->pageHash
10e00 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f || pPager->errCo
10e01 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 de. || (pPg
10e02 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
10e03 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 RTY) || pPg->pag
10e04 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 eHash==pager_pag
10e05 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a ehash(pPg) );.}.
10e06 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 .#else.#define p
10e07 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c ager_datahash(X,
10e08 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 Y) 0.#define pa
10e09 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 ger_pagehash(X)
10e0a 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 0.#define CHECK
10e0b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 _PAGE(x).#endif
10e0c 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b /* SQLITE_CHECK
10e0d 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a _PAGES */../*.**
10e0e 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 When this is ca
10e0f 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c lled the journal
10e10 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 file for pager
10e11 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f pPager must be o
10e12 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e pen..** This fun
10e13 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
10e14 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 o read a master
10e15 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
10e16 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 e from the .** e
10e17 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 nd of the file a
10e18 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 nd, if successfu
10e19 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 l, copies it int
10e1a 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 o memory supplie
10e1b 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c d .** by the cal
10e1c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 ler. See comment
10e1d 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 s above writeMas
10e1e 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 terJournal() for
10e1f 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 the format.** u
10e20 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d sed to store a m
10e21 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
10e22 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 le name at the e
10e23 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 nd of a journal
10e24 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 file..**.** zMas
10e25 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 ter must point t
10e26 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 o a buffer of at
10e27 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 least nMaster b
10e28 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 ytes allocated b
10e29 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e y.** the caller.
10e2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 This should be
10e2b 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 sqlite3_vfs.mxPa
10e2c 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 thname+1 (to ens
10e2d 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 ure there is.**
10e2e 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 enough space to
10e2f 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 write the master
10e30 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 journal name).
10e31 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f If the master jo
10e32 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e urnal.** name in
10e33 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
10e34 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 longer than nMas
10e35 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 ter bytes (inclu
10e36 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 ding a.** nul-te
10e37 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 rminator), then
10e38 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 this is handled
10e39 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 as if no master
10e3a 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 journal name.**
10e3b 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 were present in
10e3c 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a the journal..**.
10e3d 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a ** If a master j
10e3e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
10e3f 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 is present at t
10e40 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f he end of the jo
10e41 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 urnal.** file, t
10e42 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 hen it is copied
10e43 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 into the buffer
10e44 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a pointed to by z
10e45 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c Master. A.** nul
10e46 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 -terminator byte
10e47 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 is appended to
10e48 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f the buffer follo
10e49 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a wing the master.
10e4a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
10e4b 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 name..**.** If i
10e4c 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 t is determined
10e4d 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a that no master j
10e4e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
10e4f 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 is present .**
10e50 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 zMaster[0] is se
10e51 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 t to 0 and SQLIT
10e52 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a E_OK returned..*
10e53 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
10e54 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 occurs while re
10e55 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a ading from the j
10e56 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 ournal file, an
10e57 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 SQLite.** error
10e58 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
10e59 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
10e5a 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 readMasterJourna
10e5b 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
10e5c 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 pJrnl, char *zMa
10e5d 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 ster, u32 nMaste
10e5e 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 r){. int rc;
10e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e60 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
10e61 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 */. u32 len;
10e62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e63 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 /* Length in byt
10e64 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 es of master jou
10e65 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 rnal name */. i
10e66 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 64 szJ;
10e67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
10e68 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 al size in bytes
10e69 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 of journal file
10e6a 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 pJrnl */. u32
10e6b 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 cksum;
10e6c 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 /* MJ che
10e6d 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 cksum value read
10e6e 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f from journal */
10e6f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 . u32 u;
10e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10e71 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 Unsigned loop c
10e72 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 ounter */. unsi
10e73 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 gned char aMagic
10e74 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 [8]; /* A buff
10e75 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d er to hold the m
10e76 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 agic header */.
10e77 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c zMaster[0] = '\
10e78 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 0';.. if( SQLIT
10e79 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
10e7a 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a te3OsFileSize(pJ
10e7b 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c rnl, &szJ)). |
10e7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 | szJ<16. || S
10e7d 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
10e7e 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c read32bits(pJrnl
10e7f 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 , szJ-16, &len))
10e80 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 . || len>=nMas
10e81 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 ter . || SQLIT
10e82 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
10e83 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 32bits(pJrnl, sz
10e84 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 J-12, &cksum)).
10e85 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d || SQLITE_OK!=
10e86 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 (rc = sqlite3OsR
10e87 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 ead(pJrnl, aMagi
10e88 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 c, 8, szJ-8)).
10e89 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 || memcmp(aMagi
10e8a 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 c, aJournalMagic
10e8b 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 , 8). || SQLIT
10e8c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
10e8d 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c te3OsRead(pJrnl,
10e8e 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 zMaster, len, s
10e8f 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b zJ-16-len)). ){
10e90 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
10e91 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 }.. /* See if
10e92 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 the checksum ma
10e93 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 tches the master
10e94 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f journal name */
10e95 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 . for(u=0; u<le
10e96 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 n; u++){. cks
10e97 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d um -= zMaster[u]
10e98 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 ;. }. if( cksu
10e99 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 m ){. /* If t
10e9a 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 he checksum does
10e9b 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e n't add up, then
10e9c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 one or more of
10e9d 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 the disk sectors
10e9e 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 . ** containi
10e9f 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f ng the master jo
10ea0 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 urnal filename i
10ea1 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 s corrupted. Thi
10ea2 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 s means. ** d
10ea3 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 efinitely roll b
10ea4 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 ack, so just ret
10ea5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e urn SQLITE_OK an
10ea6 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 d report a (nul)
10ea7 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a . ** master-j
10ea8 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e ournal filename.
10ea9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 . */. len
10eaa 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 = 0;. }. zMast
10eab 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a er[len] = '\0';.
10eac 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c . return SQL
10ead 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
10eae 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 Return the offs
10eaf 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 et of the sector
10eb0 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 boundary at or
10eb1 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 immediately .**
10eb2 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 following the va
10eb3 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a lue in pPager->j
10eb4 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d ournalOff, assum
10eb5 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a ing a sector .**
10eb6 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d size of pPager-
10eb7 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 >sectorSize byte
10eb8 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 s..**.** i.e for
10eb9 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f a sector size o
10eba 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 f 512:.**.** P
10ebb 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ager.journalOff
10ebc 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 Return
10ebd 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d value.** -----
10ebe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10ebf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10ec0 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 --.** 0
10ec1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ec2 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 0.** 512
10ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ec4 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 512.** 100
10ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ec6 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 512.** 2
10ec7 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 000
10ec8 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 2048.**
10ec9 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 .*/.static i64
10eca 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
10ecb 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
10ecc 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 . i64 offset =
10ecd 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 0;. i64 c = pPa
10ece 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
10ecf 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 . if( c ){.
10ed0 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f offset = ((c-1)/
10ed1 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
10ed2 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f Pager) + 1) * JO
10ed3 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
10ed4 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 ger);. }. asse
10ed5 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e rt( offset%JOURN
10ed6 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
10ed7 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )==0 );. assert
10ed8 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 ( offset>=c );.
10ed9 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 assert( (offset
10eda 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f -c)<JOURNAL_HDR_
10edb 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
10edc 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d return offset;.}
10edd 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 ../*.** The jour
10ede 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 nal file must be
10edf 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 open when this
10ee0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
10ee1 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
10ee2 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
10ee3 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 op if the journa
10ee4 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 l file has not b
10ee5 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a een written to.*
10ee6 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 * within the cur
10ee7 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
10ee8 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e (i.e. if Pager.
10ee9 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a journalOff==0)..
10eea 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 **.** If doTrunc
10eeb 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 ate is non-zero
10eec 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 or the Pager.jou
10eed 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 rnalSizeLimit va
10eee 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 riable is.** set
10eef 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e to 0, then trun
10ef0 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
10ef1 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 file to zero by
10ef2 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 tes in size. Oth
10ef3 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 erwise,.** zero
10ef4 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 the 28-byte head
10ef5 65 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 er at the start
10ef6 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
10ef7 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 ile. In either c
10ef8 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 ase, .** if the
10ef9 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 pager is not in
10efa 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 no-sync mode, sy
10efb 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
10efc 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ile immediately
10efd 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e .** after writin
10efe 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 g or truncating
10eff 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 it..**.** If Pag
10f00 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 er.journalSizeLi
10f01 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 mit is set to a
10f02 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 positive, non-ze
10f03 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a ro value, and.**
10f04 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 following the t
10f05 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 runcation or zer
10f06 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 oing described a
10f07 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 bove the size of
10f08 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c the .** journal
10f09 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 file in bytes i
10f0a 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
10f0b 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 is value, then t
10f0c 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a runcate the.** j
10f0d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 ournal file to P
10f0e 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ager.journalSize
10f0f 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 Limit bytes. The
10f10 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f journal file do
10f11 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 es.** not need t
10f12 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c o be synced foll
10f13 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 owing this opera
10f14 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tion..**.** If a
10f15 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
10f16 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 s, abandon proce
10f17 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e ssing and return
10f18 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f the IO error co
10f19 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 de..** Otherwise
10f1a 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
10f1b 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e OK..*/.static in
10f1c 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 t zeroJournalHdr
10f1d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
10f1e 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b int doTruncate){
10f1f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
10f20 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
10f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f22 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
10f23 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ode */. assert(
10f24 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
10f25 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 jfd) );. if( pP
10f26 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10f27 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 ){. const i6
10f28 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 4 iLimit = pPage
10f29 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 r->journalSizeLi
10f2a 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c mit; /* Local
10f2b 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f cache of jsl */
10f2c 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 .. IOTRACE(("
10f2d 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 JZEROHDR %p\n",
10f2e 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 pPager)). if(
10f2f 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 doTruncate || i
10f30 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 Limit==0 ){.
10f31 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
10f32 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
10f33 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 >jfd, 0);. }e
10f34 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 lse{. stati
10f35 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 c const char zer
10f36 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a oHdr[28] = {0};.
10f37 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
10f38 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
10f39 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 ->jfd, zeroHdr,
10f3a 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c sizeof(zeroHdr),
10f3b 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 0);. }. i
10f3c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
10f3d 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 && !pPager->noS
10f3e 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ync ){. rc
10f3f 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
10f40 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c pPager->jfd, SQL
10f41 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
10f42 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 Y|pPager->sync_f
10f43 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 lags);. }..
10f44 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 /* At this poi
10f45 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nt the transacti
10f46 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 on is committed
10f47 62 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f but the write lo
10f48 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 ck . ** is st
10f49 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ill held on the
10f4a 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 file. If there i
10f4b 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 s a size limit c
10f4c 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 onfigured for .
10f4d 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 ** the persis
10f4e 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 tent journal and
10f4f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
10f50 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 e currently cons
10f51 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a umes more. **
10f52 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 space than that
10f53 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f limit allows fo
10f54 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e r, truncate it n
10f55 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 ow. There is no
10f56 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 need. ** to s
10f57 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c ync the file fol
10f58 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 lowing this oper
10f59 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
10f5a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
10f5b 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 _OK && iLimit>0
10f5c 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b ){. i64 sz;
10f5d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
10f5e 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 te3OsFileSize(pP
10f5f 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b ager->jfd, &sz);
10f60 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
10f61 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 QLITE_OK && sz>i
10f62 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 Limit ){.
10f63 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
10f64 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
10f65 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 jfd, iLimit);.
10f66 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
10f67 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
10f68 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
10f69 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
10f6a 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f pen when this ro
10f6b 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
10f6c 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 A journal.** he
10f6d 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 ader (JOURNAL_HD
10f6e 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 R_SZ bytes) is w
10f6f 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
10f70 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 journal file at
10f71 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c the.** current l
10f72 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ocation..**.** T
10f73 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 he format for th
10f74 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
10f75 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a is as follows:.
10f76 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 ** - 8 bytes: Ma
10f77 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 gic identifying
10f78 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a journal format..
10f79 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 ** - 4 bytes: Nu
10f7a 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 mber of records
10f7b 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d in journal, or -
10f7c 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 1 no-sync mode i
10f7d 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 s on..** - 4 byt
10f7e 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 es: Random numbe
10f7f 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 r used for page
10f80 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 hash..** - 4 byt
10f81 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 es: Initial data
10f82 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e base page count.
10f83 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 .** - 4 bytes: S
10f84 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 ector size used
10f85 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 by the process t
10f86 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a hat wrote this j
10f87 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 ournal..** - 4 b
10f88 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 ytes: Database p
10f89 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a age size..** .**
10f8a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f Followed by (JO
10f8b 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 URNAL_HDR_SZ - 2
10f8c 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 8) bytes of unus
10f8d 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 ed space..*/.sta
10f8e 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 tic int writeJou
10f8f 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 rnalHdr(Pager *p
10f90 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
10f91 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
10f92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10f93 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
10f94 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 char *zHeader
10f95 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 = pPager->pTmpSp
10f96 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 ace; /* Tempora
10f97 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f ry space used to
10f98 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f build header */
10f99 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d . u32 nHeader =
10f9a 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
10f9b 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f e; /* Size o
10f9c 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 f buffer pointed
10f9d 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a to by zHeader *
10f9e 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 /. u32 nWrite;
10f9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10fa0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
10fa1 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f of header secto
10fa2 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 r written */. i
10fa3 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
10fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10fa5 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
10fa6 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 er */.. assert(
10fa7 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
10fa8 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 jfd) ); /*
10fa9 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 Journal file mus
10faa 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 t be open. */..
10fab 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 if( nHeader>JOU
10fac 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
10fad 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 er) ){. nHead
10fae 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 er = JOURNAL_HDR
10faf 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d _SZ(pPager);. }
10fb0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
10fb1 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 are active savep
10fb2 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 oints and any of
10fb3 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 them were creat
10fb4 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 ed . ** since t
10fb5 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a he most recent j
10fb6 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 ournal header wa
10fb7 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 s written, updat
10fb8 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 e the . ** Page
10fb9 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
10fba 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 ffset fields now
10fbb 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d .. */. for(ii=
10fbc 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 0; ii<pPager->nS
10fbd 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
10fbe 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
10fbf 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e >aSavepoint[ii].
10fc0 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b iHdrOffset==0 ){
10fc1 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 . pPager->a
10fc2 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 Savepoint[ii].iH
10fc3 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 drOffset = pPage
10fc4 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 r->journalOff;.
10fc5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 }. }.. pPag
10fc6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d er->journalHdr =
10fc7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
10fc8 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 Off = journalHdr
10fc9 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a Offset(pPager);.
10fca 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 memcpy(zHeader
10fcb 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
10fcc 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c sizeof(aJournal
10fcd 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a Magic));.. /* .
10fce 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e ** Write the n
10fcf 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 Rec Field - the
10fd0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 number of page r
10fd1 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c ecords that foll
10fd2 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 ow this. ** jou
10fd3 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 rnal header. Nor
10fd4 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 mally, zero is w
10fd5 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 ritten to this v
10fd6 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d alue at this tim
10fd7 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 e.. ** After th
10fd8 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 e records are ad
10fd9 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ded to the journ
10fda 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 al (and the jour
10fdb 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a nal synced, . *
10fdc 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e * if in full-syn
10fdd 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 c mode), the zer
10fde 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e o is overwritten
10fdf 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e with the true n
10fe0 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 umber. ** of re
10fe1 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a cords (see syncJ
10fe2 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a ournal()).. **.
10fe3 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c ** A faster al
10fe4 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 ternative is to
10fe5 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 write 0xFFFFFFFF
10fe6 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 to the nRec fie
10fe7 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 ld. When. ** re
10fe8 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 ading the journa
10fe9 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c l this value tel
10fea 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 ls SQLite to ass
10feb 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a ume that the. *
10fec 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f * rest of the jo
10fed 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 urnal file conta
10fee 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 ins valid page r
10fef 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 ecords. This ass
10ff0 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 umption. ** is
10ff1 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 dangerous, as if
10ff2 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 a failure occur
10ff3 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 red whilst writi
10ff4 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ng to the journa
10ff5 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d l. ** file it m
10ff6 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 ay contain some
10ff7 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 garbage data. Th
10ff8 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e ere are two scen
10ff9 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 arios. ** where
10ffa 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 this risk can b
10ffb 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a e ignored:. **.
10ffc 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 ** * When th
10ffd 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f e pager is in no
10ffe 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 -sync mode. Corr
10fff 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f uption can follo
11000 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 w a. ** pow
11001 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 er failure in th
11002 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a is case anyway..
11003 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 **. ** * Wh
11004 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f en the SQLITE_IO
11005 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 CAP_SAFE_APPEND
11006 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 flag is set. Thi
11007 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a s guarantees. *
11008 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 * that garba
11009 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 ge data is never
1100a 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
1100b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
1100c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 */. assert( is
1100d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
1100e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 || pPager->noSy
1100f 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 nc );. if( (pPa
11010 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 ger->noSync) ||
11011 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c (pPager->journal
11012 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
11013 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a NALMODE_MEMORY).
11014 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 || (sqlite3Os
11015 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
11016 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
11017 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 )&SQLITE_IOCAP_S
11018 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 AFE_APPEND) . )
11019 7b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 {. put32bits(
1101a 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
1101b 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c aJournalMagic)],
1101c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 0xffffffff);.
1101d 7d 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 }else{. put32
1101e 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 bits(&zHeader[si
1101f 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11020 69 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 ic)], 0);. }..
11021 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 /* The random c
11022 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 heck-hash initia
11023 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 liser */ . sqli
11024 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 te3_randomness(s
11025 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b izeof(pPager->ck
11026 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 sumInit), &pPage
11027 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 r->cksumInit);.
11028 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
11029 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
1102a 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 nalMagic)+4], pP
1102b 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 ager->cksumInit)
1102c 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 ;. /* The initi
1102d 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 al database size
1102e 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 */. put32bits(
1102f 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
11030 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 aJournalMagic)+8
11031 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 ], pPager->dbOri
11032 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 gSize);. /* The
11033 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 assumed sector
11034 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 size for this pr
11035 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 ocess */. put32
11036 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 bits(&zHeader[si
11037 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11038 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d ic)+12], pPager-
11039 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 >sectorSize);..
1103a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a /* The page siz
1103b 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 e */. put32bits
1103c 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 (&zHeader[sizeof
1103d 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b (aJournalMagic)+
1103e 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 16], pPager->pag
1103f 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e eSize);.. /* In
11040 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 itializing the t
11041 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 ail of the buffe
11042 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 r is not necessa
11043 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a ry. Everything.
11044 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 ** works find
11045 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 if the following
11046 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 memset() is omi
11047 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 tted. But initi
11048 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 alizing. ** the
11049 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 memory prevents
1104a 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 valgrind from c
1104b 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 omplaining, so w
1104c 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f e are willing to
1104d 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 . ** take the p
1104e 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a erformance hit..
1104f 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a */. memset(&z
11050 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
11051 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d ournalMagic)+20]
11052 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 , 0,. nH
11053 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a eader-(sizeof(aJ
11054 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 ournalMagic)+20)
11055 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f );.. /* In theo
11056 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e ry, it is only n
11057 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 ecessary to writ
11058 65 20 74 68 65 20 32 38 20 62 79 74 65 73 20 74 e the 28 bytes t
11059 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f hat the . ** jo
1105a 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e urnal header con
1105b 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 sumes to the jou
1105c 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 rnal file here.
1105d 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 Then increment t
1105e 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a he . ** Pager.j
1105f 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 ournalOff variab
11060 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 le by JOURNAL_HD
11061 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 R_SZ so that the
11062 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f next . ** reco
11063 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f rd is written to
11064 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
11065 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 ector (leaving a
11066 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 gap in the file
11067 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 . ** that will
11068 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 be implicitly fi
11069 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f lled in by the O
1106a 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f S).. **. ** Ho
1106b 77 65 76 65 72 20 69 74 20 68 61 73 20 62 65 65 wever it has bee
1106c 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 n discovered tha
1106d 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d t on some system
1106e 73 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 s this pattern c
1106f 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e an . ** be sign
11070 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 ificantly slower
11071 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 than contiguous
11072 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 ly writing data
11073 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a to the file,. *
11074 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d * even if that m
11075 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 eans explicitly
11076 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 writing data to
11077 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 the block of .
11078 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ** (JOURNAL_HDR_
11079 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 SZ - 28) bytes t
1107a 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 hat will not be
1107b 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 used. So that is
1107c 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f what. ** is do
1107d 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ne. . **. ** T
1107e 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 he loop is requi
1107f 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 red here in case
11080 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 the sector-size
11081 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
11082 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 the . ** databa
11083 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 se page size. Si
11084 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 nce the zHeader
11085 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 buffer is only P
11086 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 ager.pageSize.
11087 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
11088 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 , more than one
11089 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f call to sqlite3O
1108a 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 sWrite() may be
1108b 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f required. ** to
1108c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e populate the en
1108d 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 tire journal hea
1108e 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f der sector.. */
1108f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 . for(nWrite=0
11090 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 ; rc==SQLITE_OK&
11091 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f &nWrite<JOURNAL_
11092 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 HDR_SZ(pPager);
11093 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 nWrite+=nHeader)
11094 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 {. IOTRACE(("
11095 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c JHDR %p %lld %d\
11096 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 n", pPager, pPag
11097 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 er->journalHdr,
11098 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 nHeader)). rc
11099 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1109a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a e(pPager->jfd, z
1109b 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c Header, nHeader,
1109c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1109d 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 Off);. pPager
1109e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
1109f 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 nHeader;. }..
110a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
110a1 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
110a2 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 file must be ope
110a3 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 n when this is c
110a4 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c alled. A journal
110a5 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 header file.**
110a6 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 (JOURNAL_HDR_SZ
110a7 62 79 74 65 73 29 20 69 73 20 72 65 61 64 20 66 bytes) is read f
110a8 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 rom the current
110a9 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 location in the
110aa 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e journal.** file.
110ab 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 The current loc
110ac 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 ation in the jou
110ad 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 rnal file is giv
110ae 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d en by.** pPager-
110af 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 >journalOff. See
110b0 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 comments above
110b1 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f function writeJo
110b2 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a urnalHdr() for.*
110b3 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 * a description
110b4 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 of the journal h
110b5 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a eader format..**
110b6 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 .** If the heade
110b7 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73 r is read succes
110b8 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 sfully, *pNRec i
110b9 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
110ba 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 ber of.** page r
110bb 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 ecords following
110bc 20 74 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 this header and
110bd 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 *pDbSize is set
110be 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 to the size of
110bf 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
110c0 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 before the trans
110c1 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e action began, in
110c2 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 pages. Also, pP
110c3 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a ager->cksumInit.
110c4 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ** is set to the
110c5 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
110c6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
110c7 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 der. SQLITE_OK i
110c8 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e s returned.** in
110c9 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a this case..**.*
110ca 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
110cb 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 header file app
110cc 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 ears to be corru
110cd 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e pted, SQLITE_DON
110ce 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 E is.** returned
110cf 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 and *pNRec and
110d0 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 *PDbSize are und
110d1 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 efined. If JOUR
110d2 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 NAL_HDR_SZ bytes
110d3 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 .** cannot be re
110d4 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
110d5 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f nal file an erro
110d6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
110d7 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
110d8 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 t readJournalHdr
110d9 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 (. Pager *pPage
110da 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
110db 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
110dc 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 */. i64 journa
110dd 6c 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 lSize,
110de 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
110df 65 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 e open journal f
110e0 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ile in bytes */.
110e1 20 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 u32 *pNRec,
110e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
110e3 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 * OUT: Value rea
110e4 64 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 d from the nRec
110e5 66 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a field */. u32 *
110e6 70 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 pDbSize
110e7 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
110e8 56 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 Value of origina
110e9 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 l database size
110ea 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e field */.){. in
110eb 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
110ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
110ed 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 turn code */. u
110ee 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 nsigned char aMa
110ef 67 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 gic[8]; /* A
110f0 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
110f1 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 the magic header
110f2 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 */. i64 iHdrOf
110f3 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
110f4 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 /* Offset of
110f5 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 journal header b
110f6 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 eing read */..
110f7 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
110f8 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 Pager->jfd) );
110f9 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 /* Journal f
110fa 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ile must be open
110fb 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e . */.. /* Advan
110fc 63 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c ce Pager.journal
110fd 4f 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 Off to the start
110fe 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 of the next sec
110ff 74 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a tor. If the. **
11100 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
11101 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 too small for t
11102 68 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 here to be a hea
11103 64 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 der stored at th
11104 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 is. ** point, r
11105 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
11106 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 E.. */. pPager
11107 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a ->journalOff = j
11108 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 ournalHdrOffset(
11109 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 pPager);. if( p
1110a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1110b 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a f+JOURNAL_HDR_SZ
1110c 28 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e (pPager) > journ
1110d 61 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 alSize ){. re
1110e 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
1110f 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 ;. }. iHdrOff
11110 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
11111 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 lOff;.. /* Read
11112 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 in the first 8
11113 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 bytes of the jou
11114 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 rnal header. If
11115 74 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 they do not matc
11116 68 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 h. ** the magi
11117 63 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 c string found a
11118 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 t the start of e
11119 61 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ach journal head
1111a 65 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 er, return. **
1111b 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 SQLITE_DONE. If
1111c 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
1111d 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 rs, return an er
1111e 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 ror code. Otherw
1111f 69 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 ise,. ** procee
11120 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 d.. */. rc = s
11121 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
11122 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 ger->jfd, aMagic
11123 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 , sizeof(aMagic)
11124 2c 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69 66 , iHdrOff);. if
11125 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
11126 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 rn rc;. }. if(
11127 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 memcmp(aMagic,
11128 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 aJournalMagic, s
11129 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d izeof(aMagic))!=
1112a 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1112b 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
1112c 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 .. /* Read the
1112d 66 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 first three 32-b
1112e 69 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 it fields of the
1112f 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a journal header:
11130 20 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 The nRec. ** f
11131 69 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 ield, the checks
11132 75 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 um-initializer a
11133 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nd the database
11134 73 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 size at the star
11135 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 t. ** of the tr
11136 61 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 ansaction. Retur
11137 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 n an error code
11138 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
11139 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 wrong.. */. i
1113a 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 f( SQLITE_OK!=(r
1113b 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 c = read32bits(p
1113c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
1113d 4f 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 Off+8, pNRec)).
1113e 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d || SQLITE_OK!=
1113f 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 (rc = read32bits
11140 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
11141 64 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 drOff+12, &pPage
11142 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 r->cksumInit)).
11143 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d || SQLITE_OK!=
11144 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 (rc = read32bits
11145 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
11146 64 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a drOff+16, pDbSiz
11147 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 e)). ){. ret
11148 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 urn rc;. }.. i
11149 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
1114a 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 alOff==0 ){.
1114b 75 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 u32 iPageSize;
1114c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1114d 50 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 Page-size field
1114e 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 of journal heade
1114f 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 r */. u32 iSe
11150 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 ctorSize;
11151 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d /* Sector-
11152 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f size field of jo
11153 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a urnal header */.
11154 20 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a u16 iPageSiz
11155 65 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 e16;
11156 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67 /* Copy of iPag
11157 65 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20 eSize in 16-bit
11158 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 variable */..
11159 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 /* Read the pag
1115a 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f e-size and secto
1115b 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68 r-size journal h
1115c 65 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f eader fields. */
1115d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
1115e 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
1115f 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11160 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69 , iHdrOff+20, &i
11161 53 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20 SectorSize)).
11162 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d || SQLITE_OK!=
11163 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 (rc = read32bits
11164 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
11165 64 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65 drOff+24, &iPage
11166 53 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 Size)). ){.
11167 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
11168 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 }.. /* Che
11169 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 ck that the valu
1116a 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 es read from the
1116b 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 page-size and s
1116c 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 ector-size field
1116d 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74 s. ** are wit
1116e 68 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65 hin range. To be
1116f 20 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74 'in range', bot
11170 68 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f h values need to
11171 20 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20 be a power.
11172 2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 ** of two greate
11173 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
11174 74 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20 to 512, and not
11175 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
11176 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 ir . ** respe
11177 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 ctive compile ti
11178 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 me maximum limit
11179 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
1117a 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 ( iPageSize<512
1117b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1117c 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c || iSectorSize<
1117d 35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 512. || iPag
1117e 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 eSize>SQLITE_MAX
1117f 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 _PAGE_SIZE || iS
11180 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
11181 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c CTOR_SIZE. |
11182 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 | ((iPageSize-1)
11183 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 &iPageSize)!=0
11184 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a || ((iSectorSiz
11185 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 e-1)&iSectorSize
11186 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )!=0 . ){.
11187 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 /* If the eit
11188 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a her the page-siz
11189 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 e or sector-size
1118a 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d in the journal-
1118b 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 header is .
1118c 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 ** invalid, the
1118d 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 n the process th
1118e 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 at wrote the jou
1118f 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 rnal-header must
11190 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 have . **
11191 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 crashed before t
11192 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 he header was sy
11193 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 nced. In this ca
11194 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 se stop reading
11195 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f . ** the jo
11196 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e urnal file here.
11197 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
11198 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
11199 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f NE;. }.. /
1119a 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 * Update the pag
1119b 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 e-size to match
1119c 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 the value read f
1119d 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e rom the journal.
1119e 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 . ** Use a t
1119f 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
111a0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
111a1 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
111a2 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 within . **
111a3 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
111a4 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 () is tested..
111a5 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 */. iPageSi
111a6 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 ze16 = (u16)iPag
111a7 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 eSize;. rc =
111a8 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
111a9 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 agesize(pPager,
111aa 26 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a 20 &iPageSize16);.
111ab 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 testcase( rc!
111ac 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
111ad 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
111ae 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 LITE_OK || iPage
111af 53 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61 Size16==(u16)iPa
111b0 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f geSize );.. /
111b1 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73 73 * Update the ass
111b2 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 umed sector-size
111b3 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 to match the va
111b4 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 20 lue used by .
111b5 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 ** the process
111b6 74 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 that created thi
111b7 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 s journal. If th
111b8 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 is journal was.
111b9 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 ** created by
111ba 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 a process other
111bb 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 than this one,
111bc 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
111bd 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e e. ** is bein
111be 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 g called from wi
111bf 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 thin pager_playb
111c0 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c ack(). The local
111c1 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 value. ** of
111c2 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a Pager.sectorSiz
111c3 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 e is restored at
111c4 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 the end of that
111c5 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f routine.. */
111c6 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
111c7 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f torSize = iSecto
111c8 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 rSize;. }.. pP
111c9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
111ca 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f += JOURNAL_HDR_
111cb 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 SZ(pPager);. re
111cc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
111cd 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70 ** Write the sup
111ce 70 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 plied master jou
111cf 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 rnal name into t
111d0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
111d1 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 for pager.** pPa
111d2 67 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65 ger at the curre
111d3 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 nt location. The
111d4 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
111d5 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 name must be the
111d6 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 last.** thing w
111d7 72 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 ritten to a jour
111d8 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 nal file. If the
111d9 20 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c pager is in ful
111da 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 l-sync mode, the
111db 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
111dc 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
111dd 64 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e dvanced to the n
111de 65 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 ext sector bound
111df 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e ary before.** an
111e0 79 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 ything is writte
111e1 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 n. The format is
111e2 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 :.**.** + 4 by
111e3 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 tes: PAGER_MJ_PG
111e4 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 NO..** + N byt
111e5 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e es: Master journ
111e6 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 al filename in u
111e7 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 tf-8..** + 4 b
111e8 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 ytes: N (length
111e9 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
111ea 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c l name in bytes,
111eb 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 no nul-terminat
111ec 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 or)..** + 4 by
111ed 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 tes: Master jour
111ee 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 nal name checksu
111ef 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 m..** + 8 byte
111f0 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 s: aJournalMagic
111f1 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 []..**.** The ma
111f2 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 ster journal pag
111f3 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 e checksum is th
111f4 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 e sum of the byt
111f5 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 es in the master
111f6 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 .** journal name
111f7 2c 20 77 68 65 72 65 20 65 61 63 68 20 62 79 74 , where each byt
111f8 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 e is interpreted
111f9 20 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62 as a signed 8-b
111fa 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a it integer..**.*
111fb 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 * If zMaster is
111fc 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 a NULL pointer (
111fd 6f 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e occurs for a sin
111fe 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 gle database tra
111ff 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 nsaction), .** t
11200 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f his call is a no
11201 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
11202 6e 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f nt writeMasterJo
11203 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
11204 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ger, const char
11205 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 *zMaster){. int
11206 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
11207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11208 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
11209 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 int nMaster;
1120a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1120b 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
1120c 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a string zMaster *
1120d 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b /. i64 iHdrOff;
1120e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1120f 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f /* Offset o
11210 66 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 f header in jour
11211 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 nal file */. i6
11212 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 4 jrnlSize;
11213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11214 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 * Size of journa
11215 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a l file on disk *
11216 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 /. u32 cksum =
11217 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
11218 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d /* Checksum
11219 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 of string zMast
1121a 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d er */.. if( !zM
1121b 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d aster || pPager-
1121c 3e 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c >setMaster. ||
1121d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1121e 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
1121f 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a NALMODE_MEMORY .
11220 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f || pPager->jo
11221 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
11222 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
11223 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 . ){. retur
11224 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
11225 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 . pPager->setMa
11226 73 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 ster = 1;. asse
11227 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
11228 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a r->jfd) );.. /*
11229 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c Calculate the l
1122a 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 61 ength in bytes a
1122b 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 nd the checksum
1122c 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 of zMaster */.
1122d 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a for(nMaster=0; z
1122e 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b Master[nMaster];
1122f 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20 nMaster++){.
11230 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 cksum += zMaste
11231 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a r[nMaster];. }.
11232 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c . /* If in full
11233 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61 -sync mode, adva
11234 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 nce to the next
11235 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f disk sector befo
11236 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20 re writing. **
11237 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
11238 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 al name. This is
11239 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 72 65 in case the pre
1123a 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 74 74 vious page writt
1123b 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a en to. ** the j
1123c 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61 ournal has alrea
1123d 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a dy been synced..
1123e 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
1123f 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 r->fullSync ){.
11240 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
11241 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 alOff = journalH
11242 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 drOffset(pPager)
11243 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 ;. }. iHdrOff
11244 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
11245 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 lOff;.. /* Writ
11246 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
11247 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65 rnal data to the
11248 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 end of the jour
11249 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a nal file. If. *
1124a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 * an error occur
1124b 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 s, return the er
1124c 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 ror code to the
1124d 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 caller.. */. i
1124e 66 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 f( (0 != (rc = w
1124f 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 rite32bits(pPage
11250 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c r->jfd, iHdrOff,
11251 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 PAGER_MJ_PGNO(p
11252 50 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20 Pager)))). ||
11253 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 (0 != (rc = sqli
11254 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
11255 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c r->jfd, zMaster,
11256 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66 nMaster, iHdrOf
11257 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 f+4))). || (0
11258 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 != (rc = write32
11259 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
1125a 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 , iHdrOff+4+nMas
1125b 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a ter, nMaster))).
1125c 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 || (0 != (rc
1125d 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
1125e 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
1125f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 ff+4+nMaster+4,
11260 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28 cksum))). || (
11261 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 0 != (rc = sqlit
11262 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
11263 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d ->jfd, aJournalM
11264 61 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66 agic, 8, iHdrOff
11265 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a +4+nMaster+8))).
11266 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ){. return
11267 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 rc;. }. pPager
11268 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
11269 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20 (nMaster+20);.
1126a 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
1126b 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 = !pPager->noSy
1126c 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 nc;.. /* If the
1126d 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 pager is in per
1126e 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d istent-journal m
1126f 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 ode, then the ph
11270 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 ysical . ** jou
11271 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 rnal-file may ex
11272 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e tend past the en
11273 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d d of the master-
11274 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a journal name. *
11275 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 * and 8 bytes of
11276 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 magic data just
11277 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
11278 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 file. This is .
11279 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 ** dangerous be
1127a 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 cause the code t
1127b 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 o rollback a hot
1127c 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 -journal file.
1127d 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 ** will not be a
1127e 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 ble to find the
1127f 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e master-journal n
11280 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ame to determine
11281 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f . ** whether o
11282 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 r not the journa
11283 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a l is hot. . **.
11284 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 ** Easiest thi
11285 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 ng to do in this
11286 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 scenario is to
11287 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 truncate the jou
11288 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 rnal . ** file
11289 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 to the required
1128a 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 size.. */ . if
1128b 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 ( SQLITE_OK==(rc
1128c 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
1128d 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 Size(pPager->jfd
1128e 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20 , &jrnlSize)).
1128f 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 && jrnlSize>pPa
11290 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a ger->journalOff.
11291 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
11292 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
11293 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
11294 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 ger->journalOff)
11295 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
11296 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 c;.}../*.** Find
11297 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 a page in the h
11298 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 ash table given
11299 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e its page number.
1129a 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 Return.** a poi
1129b 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 nter to the page
1129c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 or NULL if the
1129d 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
1129e 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64 s not .** alread
1129f 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a y in memory..*/.
112a0 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 61 static PgHdr *pa
112a1 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72 ger_lookup(Pager
112a2 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
112a3 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 gno){. PgHdr *p
112a4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
112a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
112a6 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 urn value */..
112a7 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 /* It is not pos
112a8 73 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c sible for a call
112a9 20 74 6f 20 50 63 61 63 68 65 46 65 74 63 68 28 to PcacheFetch(
112aa 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 ) with createFla
112ab 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69 g==0 to. ** fai
112ac 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65 l, since no atte
112ad 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 mpt to allocate
112ae 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77 dynamic memory w
112af 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a ill be made.. *
112b0 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 /. (void)sqlite
112b1 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 3PcacheFetch(pPa
112b2 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 ger->pPCache, pg
112b3 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 no, 0, &p);. re
112b4 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
112b5 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 Unless the page
112b6 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 r is in error-st
112b7 61 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c ate, discard all
112b8 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
112b9 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 . If.** the page
112ba 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 r is in error-st
112bb 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63 ate, then this c
112bc 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a all is a no-op..
112bd 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20 **.** TODO: Why
112be 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74 can we not reset
112bf 20 74 68 65 20 70 61 67 65 72 20 77 68 69 6c 65 the pager while
112c0 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f in error state?
112c1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
112c2 70 61 67 65 72 5f 72 65 73 65 74 28 50 61 67 65 pager_reset(Page
112c3 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
112c4 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 ( SQLITE_OK==pPa
112c5 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
112c6 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 sqlite3Backu
112c7 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d pRestart(pPager-
112c8 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73 >pBackup);. s
112c9 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 qlite3PcacheClea
112ca 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 r(pPager->pPCach
112cb 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e e);. pPager->
112cc 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b dbSizeValid = 0;
112cd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 . }.}../*.** Fr
112ce 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75 72 65 ee all structure
112cf 73 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 s in the Pager.a
112d0 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 Savepoint[] arra
112d1 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68 0a 2a y and set both.*
112d2 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 * Pager.aSavepoi
112d3 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e 53 61 nt and Pager.nSa
112d4 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f 2e vepoint to zero.
112d5 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62 2d 6a Close the sub-j
112d6 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74 20 ournal.** if it
112d7 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 is open and the
112d8 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 pager is not in
112d9 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a exclusive mode..
112da 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
112db 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 eleaseAllSavepoi
112dc 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 nts(Pager *pPage
112dd 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 r){. int ii;
112de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
112df 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 terator for loop
112e0 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61 67 65 ing through Page
112e1 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f 0a r.aSavepoint */.
112e2 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 for(ii=0; ii<p
112e3 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
112e4 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 71 t; ii++){. sq
112e5 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
112e6 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 oy(pPager->aSave
112e7 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 point[ii].pInSav
112e8 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 epoint);. }. i
112e9 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
112ea 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71 6c usiveMode || sql
112eb 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c ite3IsMemJournal
112ec 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 (pPager->sjfd) )
112ed 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 {. sqlite3OsC
112ee 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 lose(pPager->sjf
112ef 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 d);. }. sqlite
112f0 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 3_free(pPager->a
112f1 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 Savepoint);. pP
112f2 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
112f3 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
112f4 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a nSavepoint = 0;.
112f5 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 pPager->nSubRe
112f6 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 c = 0;.}../*.**
112f7 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62 Set the bit numb
112f8 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 er pgno in the P
112f9 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 agerSavepoint.pI
112fa 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 nSavepoint .** b
112fb 69 74 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70 itvecs of all op
112fc 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 52 en savepoints. R
112fd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
112fe 69 66 20 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a if successful.**
112ff 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d or SQLITE_NOMEM
11300 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 if a malloc fai
11301 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a lure occurs..*/.
11302 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f static int addTo
11303 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 SavepointBitvecs
11304 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
11305 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e Pgno pgno){. in
11306 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
11307 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
11308 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
11309 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1130a 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
1130b 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 code */.. for(
1130c 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d ii=0; ii<pPager-
1130d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b >nSavepoint; ii+
1130e 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 +){. PagerSav
1130f 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 epoint *p = &pPa
11310 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
11311 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 6e ii];. if( pgn
11312 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 o<=p->nOrig ){.
11313 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 74 rc |= sqlit
11314 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e 70 e3BitvecSet(p->p
11315 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e InSavepoint, pgn
11316 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 o);. testca
11317 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e se( rc==SQLITE_N
11318 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 61 73 OMEM );. as
11319 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1131a 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
1131b 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d E_NOMEM );. }
1131c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1131d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 ;.}../*.** Unloc
1131e 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 k the database f
1131f 69 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 ile. This functi
11320 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 on is a no-op if
11321 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 the pager.** is
11322 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
11323 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 de..**.** If the
11324 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e pager is curren
11325 74 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61 tly in error sta
11326 74 65 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 te, discard the
11327 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 contents of .**
11328 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 65 the cache and re
11329 73 65 74 20 74 68 65 20 50 61 67 65 72 20 73 74 set the Pager st
1132a 72 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c ructure internal
1132b 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 72 65 state. If there
1132c 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a is.** an open j
1132d 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 ournal-file, the
1132e 6e 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 n the next time
1132f 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 a shared-lock is
11330 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 obtained.** on
11331 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 28 the pager file (
11332 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
11333 74 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69 ther process), i
11334 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 t will be.** tre
11335 61 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f ated as a hot-jo
11336 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 urnal and rolled
11337 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 back..*/.static
11338 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f void pager_unlo
11339 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
1133a 29 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 ){. if( !pPager
1133b 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
1133c 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 ){. int rc;
1133d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1133e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1133f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c de */.. /* Al
11340 77 61 79 73 20 63 6c 6f 73 65 20 74 68 65 20 6a ways close the j
11341 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e ournal file when
11342 20 64 72 6f 70 70 69 6e 67 20 74 68 65 20 64 61 dropping the da
11343 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 tabase lock..
11344 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 ** Otherwise, a
11345 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f nother connectio
11346 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d n with journal_m
11347 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 ode=delete might
11348 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 . ** delete t
11349 68 65 20 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d he file out from
1134a 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 2a under us.. *
1134b 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 /. sqlite3OsC
1134c 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
1134d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 );. sqlite3Bi
1134e 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
1134f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b er->pInJournal);
11350 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e . pPager->pIn
11351 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 Journal = 0;.
11352 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 releaseAllSavep
11353 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a oints(pPager);..
11354 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 /* If the fi
11355 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 le is unlocked,
11356 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d 69 somebody else mi
11357 67 68 74 20 63 68 61 6e 67 65 20 69 74 2e 20 54 ght change it. T
11358 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 he. ** values
11359 20 73 74 6f 72 65 64 20 69 6e 20 50 61 67 65 72 stored in Pager
1135a 2e 64 62 53 69 7a 65 20 65 74 63 2e 20 6d 69 67 .dbSize etc. mig
1135b 68 74 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69 ht become invali
1135c 64 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 69 73 d if. ** this
1135d 20 68 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 happens. TODO:
1135e 52 65 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65 Really, this doe
1135f 73 6e 27 74 20 6e 65 65 64 20 74 6f 20 62 65 20 sn't need to be
11360 63 6c 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 cleared. ** u
11361 6e 74 69 6c 20 74 68 65 20 63 68 61 6e 67 65 2d ntil the change-
11362 63 6f 75 6e 74 65 72 20 63 68 65 63 6b 20 66 61 counter check fa
11363 69 6c 73 20 69 6e 20 70 61 67 65 72 53 68 61 72 ils in pagerShar
11364 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f edLock().. */
11365 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 . pPager->dbS
11366 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 izeValid = 0;..
11367 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b rc = osUnlock
11368 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f (pPager->fd, NO_
11369 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 LOCK);. if( r
1136a 63 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 c ){. pPage
1136b 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b r->errCode = rc;
1136c 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 . }. IOTRA
1136d 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e CE(("UNLOCK %p\n
1136e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 ", pPager))..
1136f 20 2f 2a 20 49 66 20 50 61 67 65 72 2e 65 72 72 /* If Pager.err
11370 43 6f 64 65 20 69 73 20 73 65 74 2c 20 74 68 65 Code is set, the
11371 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
11372 20 70 61 67 65 72 20 63 61 63 68 65 20 63 61 6e pager cache can
11373 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 not be. ** tr
11374 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 usted. Now that
11375 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 69 the pager file i
11376 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 s unlocked, the
11377 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a contents of the.
11378 20 20 20 20 2a 2a 20 63 61 63 68 65 20 63 61 6e ** cache can
11379 20 62 65 20 64 69 73 63 61 72 64 65 64 20 61 6e be discarded an
1137a 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 d the error code
1137b 20 73 61 66 65 6c 79 20 63 6c 65 61 72 65 64 2e safely cleared.
1137c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1137d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
1137e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d ){. if( rc=
1137f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11380 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 pPager->er
11381 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f rCode = SQLITE_O
11382 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
11383 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
11384 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ger);. }..
11385 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
11386 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 ountDone = 0;.
11387 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
11388 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a = PAGER_UNLOCK;.
11389 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
1138a 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c s function shoul
1138b 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e d be called when
1138c 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 55 an IOERR, CORRU
1138d 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72 PT or FULL error
1138e 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 63 .** may have occ
1138f 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 74 urred. The first
11390 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
11391 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 ointer to the pa
11392 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 ger .** structur
11393 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 68 e, the second th
11394 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f e error-code abo
11395 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 ut to be returne
11396 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a 2a d by a pager .**
11397 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 API function. T
11398 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
11399 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 d is a copy of t
1139a 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
1139b 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 nt .** to this f
1139c 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 unction. .**.**
1139d 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
1139e 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 gument is SQLITE
1139f 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 _IOERR, SQLITE_C
113a0 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 ORRUPT, or SQLIT
113a1 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 E_FULL.** the er
113a2 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 ror becomes pers
113a3 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 istent. Until th
113a4 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f e persisten erro
113a5 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a r is cleared,.**
113a6 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 subsequent API
113a7 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 calls on this Pa
113a8 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 ger will immedia
113a9 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 tely return the
113aa 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 same .** error c
113ab 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 ode..**.** A per
113ac 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e sistent error in
113ad 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 dicates that the
113ae 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
113af 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a pager-cache .**
113b0 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 cannot be trust
113b1 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63 ed. This state c
113b2 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 an be cleared by
113b3 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 completely disc
113b4 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 arding .** the c
113b5 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
113b6 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61 ager-cache. If a
113b7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
113b8 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 active when.**
113b9 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65 the persistent e
113ba 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 rror occurred, t
113bb 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b hen the rollback
113bc 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 journal may nee
113bd 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 d.** to be repla
113be 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74 yed to restore t
113bf 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
113c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
113c1 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 (as if.** it we
113c2 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c re a hot-journal
113c3 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
113c4 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67 pager_error(Pag
113c5 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
113c6 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d rc){. int rc2 =
113c7 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73 rc & 0xff;. as
113c8 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 sert(. pPa
113c9 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
113ca 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 LITE_FULL ||.
113cb 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 pPager->errC
113cc 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c ode==SQLITE_OK |
113cd 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 |. (pPager
113ce 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 ->errCode & 0xff
113cf 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a )==SQLITE_IOERR.
113d0 20 20 29 3b 0a 20 20 69 66 28 0a 20 20 20 20 72 );. if(. r
113d1 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 c2==SQLITE_FULL
113d2 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c 49 ||. rc2==SQLI
113d3 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 20 20 20 TE_IOERR ||.
113d4 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 rc2==SQLITE_CORR
113d5 55 50 54 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 UPT. ){. pPa
113d6 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 ger->errCode = r
113d7 63 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 c;. if( pPage
113d8 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
113d9 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 26 26 20 UNLOCK . &&
113da 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
113db 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
113dc 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 29 Cache)==0 . )
113dd 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
113de 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 61 e pager is alrea
113df 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 63 61 6c dy unlocked, cal
113e0 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 l pager_unlock()
113e1 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 20 2a 2a now to. **
113e2 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 clear the error
113e3 20 73 74 61 74 65 20 61 6e 64 20 65 6e 73 75 72 state and ensur
113e4 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 e that the pager
113e5 2d 63 61 63 68 65 20 69 73 20 0a 20 20 20 20 20 -cache is .
113e6 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 ** completely e
113e7 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 mpty.. */.
113e8 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 pager_unloc
113e9 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d k(pPager);. }
113ea 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
113eb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 ;.}../*.** Execu
113ec 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 te a rollback if
113ed 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
113ee 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e 6c s active and unl
113ef 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 ock the .** data
113f0 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a base file. .**.*
113f1 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68 * If the pager h
113f2 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 as already enter
113f3 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
113f4 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d te, do not attem
113f5 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 pt .** the rollb
113f6 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 6d 65 ack at this time
113f7 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 65 72 . Instead, pager
113f8 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c _unlock() is cal
113f9 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c led. The.** call
113fa 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b to pager_unlock
113fb 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64 20 () will discard
113fc 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 all in-memory pa
113fd 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 ges, unlock.** t
113fe 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
113ff 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 65 and clear the e
11400 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 20 74 rror state. If t
11401 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a his means that.*
11402 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 * there is a hot
11403 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e -journal left in
11404 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
11405 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65 , the next conne
11406 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 ction.** to obta
11407 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b in a shared lock
11408 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28 77 on the pager (w
11409 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69 73 hich may be this
1140a 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f one) will.** ro
1140b 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a ll it back..**.*
1140c 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68 * If the pager h
1140d 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 as not already e
1140e 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 ntered the error
1140f 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e 20 49 state, but an I
11410 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 O or.** malloc e
11411 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
11412 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 ng a rollback, t
11413 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 74 hen this will it
11414 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 74 self cause .** t
11415 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 he pager to ente
11416 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
11417 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 62 65 e. Which will be
11418 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 0a cleared by the.
11419 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 ** call to pager
1141a 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65 _unlock(), as de
1141b 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a scribed above..*
1141c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
1141d 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c gerUnlockAndRoll
1141e 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 back(Pager *pPag
1141f 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 er){. if( pPage
11420 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 r->errCode==SQLI
11421 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d TE_OK && pPager-
11422 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 >state>=PAGER_RE
11423 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71 SERVED ){. sq
11424 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
11425 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71 Malloc();. sq
11426 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
11427 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ck(pPager);.
11428 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
11429 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 Malloc();. }.
1142a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 pager_unlock(pPa
1142b 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ger);.}../*.** T
1142c 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 his routine ends
1142d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
1142e 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 A transaction is
1142f 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 usually ended b
11430 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 43 y .** either a C
11431 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 OMMIT or a ROLLB
11432 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 ACK operation. T
11433 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 his routine may
11434 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 be called .** af
11435 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 ter rollback of
11436 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f a hot-journal, o
11437 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 r if an error oc
11438 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 curs while openi
11439 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 ng.** the journa
1143a 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 69 6e l file or writin
1143b 67 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74 g the very first
1143c 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
1143d 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 of a.** database
1143e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
1143f 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 .** If the page
11440 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 48 r is in PAGER_SH
11441 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f 55 4e ARED or PAGER_UN
11442 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 LOCK state when
11443 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
11444 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 is called, it is
11445 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e a no-op (return
11446 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a s SQLITE_OK)..**
11447 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 .** Otherwise, a
11448 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 70 6f ny active savepo
11449 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 73 65 ints are release
1144a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1144b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
1144c 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 73 open, then it is
1144d 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e "finalized". On
1144e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a ce a journal .**
1144f 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 66 file has been f
11450 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 20 6e inalized it is n
11451 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 ot possible to u
11452 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61 se it to roll ba
11453 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63 ck a .** transac
11454 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 tion. Nor will i
11455 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 t be considered
11456 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72 to be a hot-jour
11457 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f nal by this.** o
11458 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61 r any other data
11459 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
1145a 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 20 6a Exactly how a j
1145b 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 ournal is finali
1145c 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f zed.** depends o
1145d 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 n whether or not
1145e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
1145f 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
11460 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 ve mode and.** t
11461 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e he current journ
11462 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a al-mode (Pager.j
11463 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 ournalMode value
11464 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a ), as follows:.*
11465 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f *.** journalMo
11466 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 de==MEMORY.**
11467 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 Journal file d
11468 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69 6d escriptor is sim
11469 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 ply closed. This
1146a 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a destroys an .**
1146b 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a in-memory j
1146c 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 ournal..**.**
1146d 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 journalMode==TRU
1146e 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 NCATE.** Jou
1146f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 75 rnal file is tru
11470 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 62 ncated to zero b
11471 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a ytes in size..**
11472 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 .** journalMod
11473 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 e==PERSIST.**
11474 20 20 54 68 65 20 66 69 72 73 74 20 32 38 20 62 The first 28 b
11475 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ytes of the jour
11476 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 72 nal file are zer
11477 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 6c 69 oed. This invali
11478 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 65 dates.** the
11479 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 first journal h
1147a 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 69 6c eader in the fil
1147b 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 e, and hence the
1147c 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a entire journal.
1147d 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 6e 20 ** file. An
1147e 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 invalid journal
1147f 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 file cannot be r
11480 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a olled back..**.*
11481 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d * journalMode=
11482 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54 =DELETE.** T
11483 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11484 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 is closed and de
11485 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 leted using sqli
11486 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a te3OsDelete()..*
11487 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 65 20 *.** If the
11488 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 pager is running
11489 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
1148a 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 20 de, this method
1148b 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a of finalizing.**
1148c 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c the journal
1148d 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 75 file is never u
1148e 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 sed. Instead, if
1148f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 the journalMode
11490 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 is.** DELET
11491 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 E and the pager
11492 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 is in exclusive
11493 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f 64 mode, the method
11494 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65 72 described under
11495 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d .** journalM
11496 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73 20 ode==PERSIST is
11497 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a used instead..**
11498 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6a 6f .** After the jo
11499 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a urnal is finaliz
1149a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 ed, if running i
1149b 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 n non-exclusive
1149c 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67 mode, the.** pag
1149d 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 er moves to PAGE
1149e 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 20 28 R_SHARED state (
1149f 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 74 and downgrades t
114a0 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a he lock on the.*
114a1 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
114a2 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a accordingly)..**
114a3 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
114a4 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 is running in e
114a5 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e xclusive mode an
114a6 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 59 d is in PAGER_SY
114a7 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69 NCED state,.** i
114a8 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 t moves to PAGER
114a9 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c _EXCLUSIVE. No l
114aa 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 61 ocks are downgra
114ab 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 ded when running
114ac 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65 in.** exclusive
114ad 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c mode..**.** SQL
114ae 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
114af 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f ed if no error o
114b0 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 72 72 ccurs. If an err
114b1 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
114b2 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 49 .** any of the I
114b3 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 O operations to
114b4 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 finalize the jou
114b5 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c rnal file or unl
114b6 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ock the.** datab
114b7 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 4f 20 ase then the IO
114b8 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
114b9 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 turned to the us
114ba 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f er. If the .** o
114bb 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61 peration to fina
114bc 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lize the journal
114bd 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 65 file fails, the
114be 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 6c 6c n the code still
114bf 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 6e 6c .** tries to unl
114c0 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
114c1 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20 file if not in
114c2 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20 exclusive mode.
114c3 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b If the.** unlock
114c4 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 73 operation fails
114c5 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74 as well, then t
114c6 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 63 he first error c
114c7 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74 ode related.** t
114c8 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f o the first erro
114c9 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 74 r encountered (t
114ca 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c he journal final
114cb 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a ization one) is.
114cc 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a ** returned..*/.
114cd 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
114ce 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
114cf 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
114d0 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 7b 0a int hasMaster){.
114d1 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
114d2 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72 E_OK; /* Er
114d3 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f ror code from jo
114d4 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 urnal finalizati
114d5 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a on operation */.
114d6 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 int rc2 = SQLI
114d7 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 TE_OK; /* Er
114d8 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 ror code from db
114d9 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 file unlock ope
114da 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 ration */.. if(
114db 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 pPager->state<P
114dc 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b AGER_RESERVED ){
114dd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
114de 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c TE_OK;. }. rel
114df 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 easeAllSavepoint
114e0 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 73 s(pPager);.. as
114e1 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
114e2 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 ger->jfd) || pPa
114e3 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
114e4 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 =0 );. if( isOp
114e5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
114e6 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a ){.. /* TODO:
114e7 20 54 68 65 72 65 27 73 20 61 20 70 72 6f 62 6c There's a probl
114e8 65 6d 20 68 65 72 65 20 69 66 20 61 20 6a 6f 75 em here if a jou
114e9 72 6e 61 6c 2d 66 69 6c 65 20 77 61 73 20 6f 70 rnal-file was op
114ea 65 6e 65 64 20 69 6e 20 4d 45 4d 4f 52 59 0a 20 ened in MEMORY.
114eb 20 20 20 2a 2a 20 6d 6f 64 65 20 61 6e 64 20 74 ** mode and t
114ec 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d hen the journal-
114ed 6d 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 mode is changed
114ee 74 6f 20 54 52 55 4e 43 41 54 45 20 6f 72 20 50 to TRUNCATE or P
114ef 45 52 53 49 53 54 0a 20 20 20 20 2a 2a 20 64 75 ERSIST. ** du
114f0 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 ring the transac
114f1 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 64 65 20 tion. This code
114f2 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e 67 65 should be change
114f3 64 20 74 6f 20 61 73 73 75 6d 65 0a 20 20 20 20 d to assume.
114f4 2a 2a 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ** that the jour
114f5 6e 61 6c 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 nal mode has not
114f6 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74 changed since t
114f7 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 he transaction w
114f8 61 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 65 as. ** starte
114f9 64 2e 20 41 6e 64 20 74 68 65 20 73 71 6c 69 74 d. And the sqlit
114fa 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
114fb 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 68 de() function sh
114fc 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20 63 ould be. ** c
114fd 68 61 6e 67 65 64 20 74 6f 20 6d 61 6b 65 20 73 hanged to make s
114fe 75 72 65 20 74 68 61 74 20 74 68 69 73 20 69 73 ure that this is
114ff 20 74 68 65 20 63 61 73 65 20 74 6f 6f 2e 0a 20 the case too..
11500 20 20 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 */.. /* Fi
11501 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e nalize the journ
11502 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 al file. */.
11503 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
11504 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
11505 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
11506 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 Y ){. int i
11507 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 3d sMemoryJournal =
11508 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
11509 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 rnal(pPager->jfd
1150a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1150b 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
1150c 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 jfd);. if(
1150d 21 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c !isMemoryJournal
1150e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1150f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
11510 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 (pPager->pVfs, p
11511 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
11512 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
11513 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
11514 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11515 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11516 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 E_TRUNCATE ){.
11517 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
11518 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b journalOff==0 ){
11519 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1151a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
1151b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
1151c 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e = sqlite3OsTrun
1151d 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 cate(pPager->jfd
1151e 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
1151f 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
11520 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 nalOff = 0;.
11521 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11522 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 lStarted = 0;.
11523 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
11524 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
11525 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65 e . || pPage
11526 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11527 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11528 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b E_PERSIST. ){
11529 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f . rc = zero
1152a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
1152b 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20 r, hasMaster);.
1152c 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 pager_error
1152d 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
1152e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1152f 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 nalOff = 0;.
11530 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11531 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 lStarted = 0;.
11532 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
11533 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
11534 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
11535 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 R_JOURNALMODE_DE
11536 4c 45 54 45 20 7c 7c 20 72 63 20 29 3b 0a 20 20 LETE || rc );.
11537 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
11538 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
11539 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
1153a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 QLITE_OK && !pPa
1153b 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
1153c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1153d 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50 lite3OsDelete(pP
1153e 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 ager->pVfs, pPag
1153f 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 er->zJournal, 0)
11540 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
11541 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
11542 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 73 HECK_PAGES. s
11543 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
11544 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d ateDirty(pPager-
11545 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 5f >pPCache, pager_
11546 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a 23 set_pagehash);.#
11547 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 74 endif.. sqlit
11548 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c e3PcacheCleanAll
11549 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1154a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 );. sqlite3Bi
1154b 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
1154c 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b er->pInJournal);
1154d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e . pPager->pIn
1154e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 Journal = 0;.
1154f 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 pPager->nRec =
11550 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 0;. }.. if( !p
11551 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
11552 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 20 Mode ){. rc2
11553 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 = osUnlock(pPage
11554 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f r->fd, SHARED_LO
11555 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d CK);. pPager-
11556 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 >state = PAGER_S
11557 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 65 HARED;. pPage
11558 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
11559 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 ne = 0;. }else
1155a 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
1155b 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 e==PAGER_SYNCED
1155c 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 ){. pPager->s
1155d 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 tate = PAGER_EXC
1155e 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50 LUSIVE;. }. pP
1155f 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
11560 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e = 0;. pPager->n
11561 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 eedSync = 0;. p
11562 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
11563 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 d = 0;.. /* TOD
11564 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 6d O: Is this optim
11565 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 64 al? Why is the d
11566 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 74 b size invalidat
11567 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 68 ed here . ** wh
11568 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
11569 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c 6f file is not unlo
1156a 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 65 cked? */. pPage
1156b 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 r->dbOrigSize =
1156c 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 0;. sqlite3Pcac
1156d 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 heTruncate(pPage
1156e 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 67 r->pPCache, pPag
1156f 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 er->dbSize);. i
11570 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 f( !MEMDB ){.
11571 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
11572 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 alid = 0;. }..
11573 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c return (rc==SQL
11574 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a ITE_OK?rc2:rc);.
11575 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
11576 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 6f er aData must po
11577 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 int to a buffer
11578 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 of pPager->pageS
11579 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 ize bytes.** of
1157a 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 6e data. Compute an
1157b 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 6b d return a check
1157c 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 68 sum based ont th
1157d 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
1157e 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 61 e .** page of da
1157f 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 ta and the curre
11580 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 67 nt value of pPag
11581 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a er->cksumInit..*
11582 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 *.** This is not
11583 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d a real checksum
11584 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 6a . It is really j
11585 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 74 ust the sum of t
11586 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e he .** random in
11587 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 61 itial value (pPa
11588 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 20 ger->cksumInit)
11589 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 20 and every 200th
1158a 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 byte.** of the p
1158b 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 69 age data, starti
1158c 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 66 ng with byte off
1158d 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 67 set (pPager->pag
1158e 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 45 eSize%200)..** E
1158f 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 65 ach byte is inte
11590 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 2d rpreted as an 8-
11591 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 bit unsigned int
11592 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e eger..**.** Chan
11593 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c 61 ging the formula
11594 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 used to compute
11595 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 72 this checksum r
11596 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 esults in an.**
11597 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f 75 incompatible jou
11598 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 rnal file format
11599 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e ..**.** If journ
1159a 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 al corruption oc
1159b 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 6f curs due to a po
1159c 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 65 wer failure, the
1159d 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a most likely .**
1159e 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 61 scenario is tha
1159f 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 65 t one end or the
115a0 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 65 other of the re
115a1 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 61 cord will be cha
115a2 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 20 nged. .** It is
115a3 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79 much less likely
115a4 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 6e that the two en
115a5 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ds of the journa
115a6 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 l record will be
115a7 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 20 .** correct and
115a8 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 6f the middle be co
115a9 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 68 rrupt. Thus, th
115aa 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 63 is "checksum" sc
115ab 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 heme,.** though
115ac 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c fast and simple,
115ad 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f 73 catches the mos
115ae 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 tly likely kind
115af 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a of corruption..*
115b0 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 /.static u32 pag
115b1 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 2a er_cksum(Pager *
115b2 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 pPager, const u8
115b3 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 20 *aData){. u32
115b4 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e cksum = pPager->
115b5 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 cksumInit;
115b6 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 76 /* Checksum v
115b7 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
115b8 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 /. int i = pPag
115b9 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 er->pageSize-200
115ba 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f ; /* Lo
115bb 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
115bc 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 while( i>0 ){.
115bd 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 61 cksum += aData
115be 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 30 [i];. i -= 20
115bf 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
115c0 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 cksum;.}../*.**
115c1 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 61 Read a single pa
115c2 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 74 ge from either t
115c3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
115c4 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d (if isMainJrnl==
115c5 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 1) or.** from th
115c6 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 e sub-journal (i
115c7 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 f isMainJrnl==0)
115c8 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 68 and playback th
115c9 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 at page..** The
115ca 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 6f page begins at o
115cb 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 69 ffset *pOffset i
115cc 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68 nto the file. Th
115cd 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61 e *pOffset.** va
115ce 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 lue is increased
115cf 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
115d0 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
115d1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a n the journal..*
115d2 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e 4a *.** The isMainJ
115d3 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 65 rnl flag is true
115d4 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
115d5 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f main rollback jo
115d6 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c urnal and.** fal
115d7 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 se for the state
115d8 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 ment journal. T
115d9 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
115da 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a 2a journal uses.**
115db 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 65 checksums - the
115dc 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
115dd 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a al does not..**.
115de 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e ** If the page n
115df 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
115e0 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 72 e record read fr
115e1 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 om the (sub-)jou
115e2 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 rnal file.** is
115e3 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
115e4 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
115e5 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 f Pager.dbSize,
115e6 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 73 then playback is
115e7 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 20 .** skipped and
115e8 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
115e9 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
115ea 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c pDone is not NUL
115eb 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 20 L, then it is a
115ec 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 20 record of pages
115ed 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 64 that have alread
115ee 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 y.** been played
115ef 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 70 back. If the p
115f0 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 20 age at *pOffset
115f1 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
115f2 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 played back.**
115f3 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f (if the correspo
115f4 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 nding pDone bit
115f5 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b 69 is set) then ski
115f6 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a p the playback..
115f7 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 ** Make sure the
115f8 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 pDone bit corre
115f9 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
115fa 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 73 *pOffset page is
115fb 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f set.** prior to
115fc 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a returning..**.*
115fd 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 * If the page re
115fe 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 66 cord is successf
115ff 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 74 ully read from t
11600 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c he (sub-)journal
11601 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 file.** and pla
11602 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 53 yed back, then S
11603 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
11604 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
11605 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 77 rror occurs.** w
11606 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 65 hile reading the
11607 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 65 record from the
11608 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 (sub-)journal f
11609 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 69 ile or while wri
1160a 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 64 ting.** to the d
1160b 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
1160c 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
1160d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
1160e 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 20 . If data.** is
1160f 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 successfully rea
11610 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d d from the (sub-
11611 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 75 )journal file bu
11612 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 0a t appears to be.
11613 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 ** corrupted, SQ
11614 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 LITE_DONE is ret
11615 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 63 urned. Data is c
11616 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 70 onsidered corrup
11617 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 ted in.** two ci
11618 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 rcumstances:.**
11619 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72 .** * If the r
1161a 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 65 ecord page-numbe
1161b 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 20 r is illegal (0
1161c 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f or PAGER_MJ_PGNO
1161d 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 20 ), or.** * If
1161e 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 65 the record is be
1161f 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 ing rolled back
11620 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f from the main jo
11621 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 urnal file.**
11622 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 and the checks
11623 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e 6f um field does no
11624 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 6f t match the reco
11625 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a rd content..**.*
11626 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 * Neither of the
11627 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 se two scenarios
11628 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 75 are possible du
11629 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e 74 ring a savepoint
1162a 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
1162b 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 61 If this is a sa
1162c 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
1162d 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 , then memory ma
1162e 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 6e y have to be dyn
1162f 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f amically.** allo
11630 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75 cated by this fu
11631 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 nction. If this
11632 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 20 is the case and
11633 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 an allocation fa
11634 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e ils,.** SQLITE_N
11635 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
11636 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11637 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
11638 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 72 ne_page(. Pager
11639 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
1163a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1163b 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 79 pager being play
1163c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 ed back */. int
1163d 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 isMainJrnl,
1163e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
1163f 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e -> main journal.
11640 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 0 -> sub-journa
11641 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 6e l. */. int isUn
11642 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 sync,
11643 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
11644 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75 6e reading from un
11645 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75 72 synced main jour
11646 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f nal */. i64 *pO
11647 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 ffset,
11648 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
11649 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 6c of record to pl
1164a 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 ayback */. int
1164b 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20 20 isSavepnt,
1164c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1164d 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 6e e for a savepoin
1164e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 t rollback */.
1164f 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20 20 Bitvec *pDone
11650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11651 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 Bitvec of pages
11652 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 20 already played
11653 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 back */.){. int
11654 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 rc;. PgHdr *pP
11655 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
11656 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 /* An exist
11657 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20 ing page in the
11658 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 cache */. Pgno
11659 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
1165a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1165b 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 page number of a
1165c 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c page in journal
1165d 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b */. u32 cksum;
1165e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1165f 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
11660 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20 used for sanity
11661 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 checking */. u8
11662 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 *aData;
11663 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11664 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 emporary storage
11665 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a 2f for the page */
11666 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
11667 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 *jfd;
11668 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 /* The file des
11669 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 criptor for the
1166a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1166b 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d 61 . assert( (isMa
1166c 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b inJrnl&~1)==0 );
1166d 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a /* isMainJ
1166e 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a 2f rnl is 0 or 1 */
1166f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 61 . assert( (isSa
11670 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20 vepnt&~1)==0 );
11671 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 70 /* isSavep
11672 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a nt is 0 or 1 */.
11673 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 6e assert( isMain
11674 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b Jrnl || pDone );
11675 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c /* pDone al
11676 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 62 ways used on sub
11677 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 -journals */. a
11678 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 ssert( isSavepnt
11679 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 || pDone==0 );
1167a 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72 /* pDone never
1167b 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 used on non-sav
1167c 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 epoint */.. aDa
1167d 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65 72 ta = (u8*)pPager
1167e 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 61 ->pTmpSpace;. a
1167f 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b 20 ssert( aData );
11680 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 /* Temp
11681 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 76 storage must hav
11682 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 e already been a
11683 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 2f llocated */.. /
11684 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20 * Read the page
11685 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 number and page
11686 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a 6f data from the jo
11687 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 urnal or sub-jou
11688 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 rnal. ** file.
11689 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
1168a 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c code to the call
1168b 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f er if an IO erro
1168c 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 r occurs.. */.
1168d 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e jfd = isMainJrn
1168e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 l ? pPager->jfd
1168f 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0a : pPager->sjfd;.
11690 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 rc = read32bit
11691 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c s(jfd, *pOffset,
11692 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 &pgno);. if( r
11693 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
11694 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
11695 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a sqlite3OsRead(j
11696 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 fd, aData, pPage
11697 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a 70 r->pageSize, (*p
11698 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69 66 Offset)+4);. if
11699 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1169a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a ) return rc;. *
1169b 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67 65 pOffset += pPage
1169c 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 20 r->pageSize + 4
1169d 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a + isMainJrnl*4;.
1169e 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 . /* Sanity che
1169f 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 cking on the pag
116a0 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 65 e. This is more
116a1 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
116a2 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a I originally. *
116a3 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 61 * thought. If a
116a4 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f power failure o
116a5 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20 ccurs while the
116a6 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 journal is being
116a7 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 written,. ** i
116a8 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 6e t could cause in
116a9 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 65 valid data to be
116aa 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
116ab 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e e journal. We n
116ac 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65 eed to. ** dete
116ad 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 20 ct this invalid
116ae 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 20 data (with high
116af 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e 64 probability) and
116b0 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f ignore it.. */
116b1 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c . if( pgno==0 |
116b2 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a | pgno==PAGER_MJ
116b3 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b _PGNO(pPager) ){
116b4 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 . assert( !is
116b5 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 72 Savepnt );. r
116b6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
116b7 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e E;. }. if( pgn
116b8 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e o>(Pgno)pPager->
116b9 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74 65 dbSize || sqlite
116ba 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f 6e 3BitvecTest(pDon
116bb 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 e, pgno) ){.
116bc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
116bd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d 61 ;. }. if( isMa
116be 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63 inJrnl ){. rc
116bf 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 = read32bits(jf
116c0 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34 2c d, (*pOffset)-4,
116c1 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 66 &cksum);. if
116c2 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
116c3 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61 76 ;. if( !isSav
116c4 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63 6b epnt && pager_ck
116c5 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74 sum(pPager, aDat
116c6 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20 a)!=cksum ){.
116c7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
116c8 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d _DONE;. }. }
116c9 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26 .. if( pDone &&
116ca 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 (rc = sqlite3Bi
116cb 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70 tvecSet(pDone, p
116cc 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b gno))!=SQLITE_OK
116cd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
116ce 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 c;. }.. assert
116cf 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
116d0 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 =PAGER_RESERVED
116d1 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 || pPager->state
116d2 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
116d3 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 E );.. /* If th
116d4 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 e pager is in RE
116d5 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 SERVED state, th
116d6 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 en there must be
116d7 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a a copy of this.
116d8 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 ** page in the
116d9 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e pager cache. In
116da 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 this case just
116db 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 update the pager
116dc 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 cache,. ** not
116dd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
116de 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 le. The page is
116df 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 left marked dirt
116e0 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a y in this case..
116e1 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 **. ** An exc
116e2 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 eption to the ab
116e3 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 ove rule: If the
116e4 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 database is in
116e5 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a no-sync mode. *
116e6 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 * and a page is
116e7 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 moved during an
116e8 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
116e9 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 um then the page
116ea 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 may. ** not be
116eb 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 in the pager ca
116ec 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 che. Later: if a
116ed 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 malloc() or IO
116ee 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a error occurs. *
116ef 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 * during a Movep
116f0 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e age() call, then
116f1 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f the page may no
116f2 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 t be in the cach
116f3 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 e. ** either. S
116f4 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 o the condition
116f5 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 described in the
116f6 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 above paragraph
116f7 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 is not. ** ass
116f8 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a ert()able.. **.
116f9 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 ** If in EXCLU
116fa 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e SIVE state, then
116fb 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20 70 we update the p
116fc 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 74 ager cache if it
116fd 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 exists. ** and
116fe 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 the main file.
116ff 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 6e The page is then
11700 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 marked not dirt
11701 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 y.. **. ** Tic
11702 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 ket #1171: The
11703 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
11704 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 l might contain
11705 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 page content tha
11706 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 t is. ** differ
11707 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 ent from the pag
11708 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 e content at the
11709 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 start of the tr
1170a 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 ansaction.. **
1170b 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e This occurs when
1170c 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 a page is chang
1170d 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ed prior to the
1170e 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 start of a state
1170f 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 ment. ** then c
11710 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 hanged again wit
11711 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e hin the statemen
11712 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 t. When rolling
11713 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a back such a. *
11714 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d * statement we m
11715 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f ust not write to
11716 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
11717 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 tabase unless we
11718 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 know. ** for c
11719 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 ertain that orig
1171a 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e inal page conten
1171b 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e ts are synced in
1171c 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c to the main roll
1171d 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 back. ** journa
1171e 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 l. Otherwise, a
1171f 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 power loss migh
11720 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 t leave modified
11721 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a data in the. *
11722 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
11723 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 without an entry
11724 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b in the rollback
11725 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 journal that ca
11726 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 n. ** restore t
11727 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 he database to i
11728 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d ts original form
11729 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e . Two condition
1172a 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d s must be. ** m
1172b 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e et before writin
1172c 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 g to the databas
1172d 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 e files. (1) the
1172e 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 database must b
1172f 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 e. ** locked.
11730 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 (2) we know that
11731 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 the original pa
11732 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 ge content is fu
11733 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 lly synced. **
11734 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 in the main jour
11735 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 nal either becau
11736 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e se the page is n
11737 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 ot in cache or e
11738 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 lse. ** the pag
11739 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e e is marked as n
1173a 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a eedSync==0.. **
1173b 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 . ** 2008-04-14
1173c 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 : When attempti
1173d 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 ng to vacuum a c
1173e 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 orrupt database
1173f 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 file, it. ** is
11740 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 possible to fai
11741 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e l a statement on
11742 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74 a database that
11743 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 does not yet ex
11744 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 ist.. ** Do not
11745 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 attempt to writ
11746 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69 e if database fi
11747 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 le has never bee
11748 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 n opened.. */.
11749 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f pPg = pager_loo
1174a 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f kup(pPager, pgno
1174b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 );. assert( pPg
1174c 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 || !MEMDB );.
1174d 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c 41 PAGERTRACE(("PLA
1174e 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25 64 YBACK %d page %d
1174f 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c 6e hash(%08x) %s\n
11750 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
11751 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
11752 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 ), pgno, pager_d
11753 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e atahash(pPager->
11754 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 29 pageSize, aData)
11755 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
11756 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61 (isMainJrnl?"ma
11757 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 62 in-journal":"sub
11758 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 3b -journal"). ));
11759 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e . if( (pPager->
1175a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
1175b 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 70 LUSIVE). && (p
1175c 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 67 Pg==0 || 0==(pPg
1175d 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
1175e 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 20 ED_SYNC)). &&
1175f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
11760 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 79 d). && !isUnsy
11761 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 20 nc. ){. i64
11762 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a ofst = (pgno-1)*
11763 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 (i64)pPager->pag
11764 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 eSize;. rc =
11765 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
11766 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 61 Pager->fd, aData
11767 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
11768 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 69 ze, ofst);. i
11769 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e f( pgno>pPager->
1176a 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 dbFileSize ){.
1176b 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
1176c 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
1176d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1176e 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 BackupUpdate(pPa
1176f 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 ger->pBackup, pg
11770 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 7d 65 no, aData);. }e
11771 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e 4a lse if( !isMainJ
11772 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 7b rnl && pPg==0 ){
11773 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
11774 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 is a rollback of
11775 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 a savepoint and
11776 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 72 data was not wr
11777 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 itten to. **
11778 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 the database and
11779 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
1177a 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 in-memory, ther
1177b 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 6c e is a potential
1177c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e . ** problem.
1177d 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 When the page i
1177e 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 62 s next fetched b
1177f 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 y the b-tree lay
11780 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 er, it . ** w
11781 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f 6d ill be read from
11782 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11783 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f 72 le, which may or
11784 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 may not be .
11785 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 ** current. .
11786 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 **. ** Ther
11787 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 6f e are a couple o
11788 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 f different ways
11789 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e this can happen
1178a 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 0a . All are quite.
1178b 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 ** obscure.
1178c 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 When running in
1178d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 synchronous mode
1178e 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 , this can only
1178f 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 happen . ** i
11790 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 6e f the page is on
11791 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 the free-list a
11792 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
11793 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 he transaction,
11794 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 then. ** popu
11795 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 lated, then move
11796 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 50 d using sqlite3P
11797 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a agerMovepage()..
11798 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
11799 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f e solution is to
1179a 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 add an in-memor
1179b 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 61 y page to the ca
1179c 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 che containing.
1179d 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 6a ** the data j
1179e 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 68 ust read from th
1179f 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d e sub-journal. M
117a0 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
117a1 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e dirty . ** an
117a2 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 72 d if the pager r
117a3 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 equires a journa
117a4 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 l-sync, then mar
117a5 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a 20 k the page as .
117a6 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 ** requiring
117a7 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 a journal-sync b
117a8 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 74 efore it is writ
117a9 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ten.. */.
117aa 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e assert( isSavepn
117ab 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 t );. if( (rc
117ac 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 = sqlite3PagerA
117ad 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 70 cquire(pPager, p
117ae 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21 3d gno, &pPg, 1))!=
117af 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
117b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
117b1 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 }. pPg->fla
117b2 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 gs &= ~PGHDR_NEE
117b3 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 D_READ;. sqli
117b4 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
117b5 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 ty(pPg);. }. i
117b6 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a f( pPg ){. /*
117b7 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 No page should
117b8 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 ever be explicit
117b9 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 ly rolled back t
117ba 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 hat is in use, e
117bb 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 xcept. ** for
117bc 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69 73 page 1 which is
117bd 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 held in use in
117be 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 order to keep th
117bf 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 e lock on the.
117c0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 ** database ac
117c1 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 tive. However su
117c2 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 ch a page may be
117c3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 rolled back as
117c4 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 a result. **
117c5 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 of an internal e
117c6 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 rror resulting i
117c7 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 n an automatic c
117c8 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 all to. ** sq
117c9 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
117ca 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ck().. */.
117cb 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 void *pData;.
117cc 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 pData = pPg->p
117cd 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 Data;. memcpy
117ce 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c 20 70 (pData, aData, p
117cf 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
117d0 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 ;. if( pPager
117d1 2d 3e 78 52 65 69 6e 69 74 65 72 20 29 7b 0a 20 ->xReiniter ){.
117d2 20 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 pPager->xRe
117d3 69 6e 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 initer(pPg);.
117d4 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 }. if( isMai
117d5 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 nJrnl && (!isSav
117d6 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 epnt || *pOffset
117d7 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 <=pPager->journa
117d8 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f lHdr) ){. /
117d9 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 * If the content
117da 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 s of this page w
117db 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 ere just restore
117dc 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 d from the main
117dd 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 . ** journa
117de 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 l file, then its
117df 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 content must be
117e0 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68 as they were wh
117e1 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a en the . **
117e2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
117e3 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 first opened. I
117e4 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63 n this case we c
117e5 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 an mark the page
117e6 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 . ** as cle
117e7 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 an, since there
117e8 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 will be no need
117e9 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 to write it out
117ea 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a to the.. **
117eb 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 . ** There
117ec 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e is one exception
117ed 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 to this rule. I
117ee 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 f the page is be
117ef 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 ing rolled.
117f0 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 ** back as part
117f1 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 of a savepoint
117f2 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 (or statement) r
117f3 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 ollback from an
117f4 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 . ** unsync
117f5 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 ed portion of th
117f6 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
117f7 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ile, then it is
117f8 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a not safe. *
117f9 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 * to mark the pa
117fa 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 ge as clean. Thi
117fb 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 s is because mar
117fc 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 king the page as
117fd 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 . ** clean
117fe 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 will clear the P
117ff 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
11800 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 lag. Since the p
11801 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 age is. **
11802 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a already in the j
11803 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 ournal file (rec
11804 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 orded in Pager.p
11805 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 InJournal) and.
11806 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
11807 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
11808 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 is cleared, if
11809 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 the page is writ
1180a 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ten to. **
1180b 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 again within thi
1180c 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 s transaction, i
1180d 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 t will be marked
1180e 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 as dirty but.
1180f 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 ** the PGHDR
11810 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
11811 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e will not be set.
11812 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 It could then p
11813 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 otentially.
11814 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f ** be written o
11815 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ut into the data
11816 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 base file before
11817 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c its journal fil
11818 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 e. ** segme
11819 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 nt is synced. If
1181a 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 a crash occurs
1181b 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 during or follow
1181c 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 ing this,.
1181d 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 ** database corr
1181e 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 uption may ensue
1181f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
11820 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
11821 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 keClean(pPg);.
11822 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
11823 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
11824 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
11825 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
11826 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
11827 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 /* If this was
11828 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65 page 1, then re
11829 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 store the value
1182a 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 of Pager.dbFileV
1182b 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 ers.. ** Do t
1182c 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64 his before any d
1182d 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 ecoding. */.
1182e 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 if( pgno==1 ){.
1182f 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 memcpy(&pPa
11830 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c ger->dbFileVers,
11831 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32 &((u8*)pData)[2
11832 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72 4],sizeof(pPager
11833 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a ->dbFileVers));.
11834 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
11835 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75 code the page ju
11836 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 st read from dis
11837 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 k */. CODEC1(
11838 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 pPager, pData, p
11839 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20 Pg->pgno, 3);.
1183a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 sqlite3PcacheR
1183b 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d elease(pPg);. }
1183c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1183d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
1183e 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
1183f 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 (SQLITE_COVERAGE
11840 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 _TEST)./*.** Thi
11841 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 s routine looks
11842 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20 6d ahead into the m
11843 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
11844 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73 0a and determines.
11845 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
11846 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 t the next recor
11847 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74 68 d (the record th
11848 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69 6c at begins at fil
11849 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61 67 e.** offset pPag
1184a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 er->journalOff)
1184b 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 is a well-formed
1184c 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f 6e page record con
1184d 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61 20 sisting.** of a
1184e 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65 valid page numbe
1184f 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53 69 r, pPage->pageSi
11850 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74 ze bytes of cont
11851 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a ent, followed.**
11852 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65 63 by a valid chec
11853 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ksum..**.** The
11854 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65 64 pager never need
11855 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20 69 s to know this i
11856 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69 74 n order to do it
11857 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a 2a s job. This.**
11858 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 routine is only
11859 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68 20 used from with
1185a 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65 73 assert() and tes
1185b 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e 0a tcase() macros..
1185c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
1185d 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61 gerNextJournalPa
1185e 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72 20 geIsValid(Pager
1185f 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e 6f *pPager){. Pgno
11860 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
11861 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d /* The page num
11862 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 ber of the page
11863 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 */. u32 cksum;
11864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11865 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 2a page checksum *
11866 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
11867 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
11868 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65 61 rn code from rea
11869 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a d operations */.
1186a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1186b 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69 fd; /* The fi
1186c 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 72 le descriptor fr
1186d 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65 20 om which we are
1186e 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38 20 reading */. u8
1186f 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *aData;
11870 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
11871 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f the page */.. /
11872 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20 * Read the page
11873 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a 2f number header */
11874 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d 3e . fd = pPager->
11875 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 jfd;. rc = read
11876 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67 65 32bits(fd, pPage
11877 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 26 r->journalOff, &
11878 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 pgno);. if( rc!
11879 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 65 =SQLITE_OK ){ re
1187a 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 turn 0; }
1187b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1187c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1187d 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e TEST*/. if( pgn
1187e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 o==0 || pgno==PA
1187f 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
11880 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30 3b er) ){ return 0;
11881 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f } /*NO_
11882 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e TEST*/. if( pgn
11883 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e o>(Pgno)pPager->
11884 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72 6e dbSize ){ return
11885 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 0; }
11886 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
11887 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 TEST*/.. /* Rea
11888 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a d the checksum *
11889 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 /. rc = read32b
1188a 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e its(fd, pPager->
1188b 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67 65 journalOff+pPage
1188c 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26 r->pageSize+4, &
1188d 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 63 cksum);. if( rc
1188e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 !=SQLITE_OK ){ r
1188f 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 eturn 0; }
11890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11891 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
11892 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 _TEST*/.. /* Re
11893 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64 20 ad the data and
11894 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63 6b verify the check
11895 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20 3d sum */. aData =
11896 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54 (u8*)pPager->pT
11897 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 mpSpace;. rc =
11898 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64 sqlite3OsRead(fd
11899 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d , aData, pPager-
1189a 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67 65 >pageSize, pPage
1189b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34 29 r->journalOff+4)
1189c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1189d 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20 TE_OK ){ return
1189e 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 0; }
1189f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118a0 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
118a1 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63 6b /. if( pager_ck
118a2 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74 sum(pPager, aDat
118a3 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65 74 a)!=cksum ){ ret
118a4 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 urn 0; }
118a5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
118a6 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74 68 /.. /* Reach th
118a7 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69 66 is point only if
118a8 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61 6c the page is val
118a9 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 id */. return 1
118aa 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 ;.}.#endif /* !d
118ab 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
118ac 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
118ad 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 20 _COVERAGE_TEST)
118ae 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 */../*.** Parame
118af 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 ter zMaster is t
118b0 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 he name of a mas
118b1 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
118b2 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e . A single journ
118b3 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 al.** file that
118b4 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 referred to the
118b5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
118b6 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 ile has just bee
118b7 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a n rolled back..*
118b8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
118b9 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 hecks if it is p
118ba 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 ossible to delet
118bb 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
118bc 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e rnal file,.** an
118bd 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 d does so if it
118be 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 is..**.** Argume
118bf 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 nt zMaster may p
118c0 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 oint to Pager.pT
118c1 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 mpSpace. So that
118c2 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a buffer is not .
118c3 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ** available for
118c4 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 use within this
118c5 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
118c6 20 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a When a master j
118c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 ournal file is c
118c8 72 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f reated, it is po
118c9 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 pulated with the
118ca 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c names .** of al
118cb 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a l of its child j
118cc 6f 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 ournals, one aft
118cd 65 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d er another, form
118ce 61 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a atted as utf-8 .
118cf 2a 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e ** encoded text.
118d0 20 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 The end of each
118d1 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 child journal f
118d2 69 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69 ile is marked wi
118d3 74 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 th a .** nul-ter
118d4 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78 minator byte (0x
118d5 30 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 00). i.e. the en
118d6 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 tire contents of
118d7 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
118d8 6c 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 l.** file for a
118d9 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f transaction invo
118da 6c 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61 lving two databa
118db 73 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a ses might be:.**
118dc 0a 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c .** "/home/bil
118dd 6c 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 l/a.db-journal\x
118de 30 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 00/home/bill/b.d
118df 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a b-journal\x00".*
118e0 2a 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f *.** A master jo
118e1 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f urnal file may o
118e2 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f nly be deleted o
118e3 6e 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 nce all of its c
118e4 68 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c hild .** journal
118e5 73 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c s have been roll
118e6 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 ed back..**.** T
118e7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 his function rea
118e8 64 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ds the contents
118e9 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f of the master-jo
118ea 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 urnal file into
118eb 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c .** memory and l
118ec 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 oops through eac
118ed 68 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a h of the child j
118ee 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f ournal names. Fo
118ef 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 r.** each child
118f0 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 journal, it chec
118f1 6b 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a ks if:.**.** *
118f2 20 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f if the child jo
118f3 75 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e urnal exists, an
118f4 64 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 d if so.** * i
118f5 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 f the child jour
118f6 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 nal contains a r
118f7 65 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 eference to mast
118f8 65 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 er journal .**
118f9 20 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a file zMaster.
118fa 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 **.** If a child
118fb 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 journal can be
118fc 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68 found that match
118fd 65 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63 es both of the c
118fe 72 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 riteria.** above
118ff 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
11900 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 returns without
11901 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 doing anything.
11902 4f 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a Otherwise, if.**
11903 20 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a no such child j
11904 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f ournal can be fo
11905 75 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 und, file zMaste
11906 72 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f r is deleted fro
11907 6d 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 m.** the file-sy
11908 73 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 stem using sqlit
11909 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a e3OsDelete()..**
1190a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 .** If an IO err
1190b 6f 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 or within this f
1190c 75 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f unction, an erro
1190d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
1190e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 ed. This.** func
1190f 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d tion allocates m
11910 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 emory by calling
11911 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
11912 2e 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 . If an allocati
11913 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c on.** fails, SQL
11914 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
11915 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 urned. Otherwise
11916 2c 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 , if no IO or ma
11917 6c 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 lloc errors .**
11918 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b occur, SQLITE_OK
11919 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1191a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 .** TODO: This f
1191b 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 unction allocate
1191c 73 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b s a single block
1191d 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f of memory to lo
1191e 61 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 ad.** the entire
1191f 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
11920 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11921 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 file. This could
11922 20 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 be.** a couple
11923 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 of kilobytes or
11924 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 so - potentially
11925 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
11926 20 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a page .** size..
11927 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
11928 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 ger_delmaster(Pa
11929 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e ger *pPager, con
1192a 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
1192b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
1192c 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d *pVfs = pPager-
1192d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b >pVfs;. int rc;
1192e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1192f 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
11930 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
11931 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 ile *pMaster;
11932 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 /* Malloc'd mas
11933 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter-journal file
11934 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
11935 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
11936 4a 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 Journal; /* Ma
11937 6c 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 lloc'd child-jou
11938 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 rnal file descri
11939 70 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a ptor */. char *
1193a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d zMasterJournal =
1193b 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 0; /* Contents
1193c 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
1193d 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 l file */. i64
1193e 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 nMasterJournal;
1193f 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
11940 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11941 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c file */.. /* Al
11942 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
11943 20 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e both the pJourn
11944 61 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66 al and pMaster f
11945 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e ile descriptors.
11946 0a 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 . ** If success
11947 66 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 ful, open the ma
11948 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
11949 65 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 e for reading..
1194a 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 */. pMaster =
1194b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
1194c 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
1194d 6f 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 o(pVfs->szOsFile
1194e 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 * 2);. pJourna
1194f 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c l = (sqlite3_fil
11950 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73 e *)(((u8 *)pMas
11951 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f ter) + pVfs->szO
11952 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 sFile);. if( !p
11953 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63 Master ){. rc
11954 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
11955 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f . }else{. co
11956 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 nst int flags =
11957 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
11958 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 DONLY|SQLITE_OPE
11959 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
1195a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1195b 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
1195c 7a 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 zMaster, pMaster
1195d 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d , flags, 0);. }
1195e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1195f 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d E_OK ) goto delm
11960 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 aster_out;.. rc
11961 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11962 53 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e Size(pMaster, &n
11963 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a MasterJournal);.
11964 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
11965 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 _OK ) goto delma
11966 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 ster_out;.. if(
11967 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e nMasterJournal>
11968 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 0 ){. char *z
11969 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 Journal;. cha
1196a 72 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 r *zMasterPtr =
1196b 30 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 0;. int nMast
1196c 65 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 erPtr = pVfs->mx
1196d 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 Pathname+1;..
1196e 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 /* Load the ent
1196f 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ire master journ
11970 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 al file into spa
11971 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ce obtained from
11972 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f . ** sqlite3_
11973 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 malloc() and poi
11974 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 nted to by zMast
11975 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 erJournal. .
11976 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f */. zMasterJo
11977 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a 29 urnal = (char *)
11978 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 sqlite3Malloc((i
11979 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 nt)nMasterJourna
1197a 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29 3b l + nMasterPtr);
1197b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 . if( !zMaste
1197c 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 rJournal ){.
1197d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
1197e 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 MEM;. goto
1197f 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
11980 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 72 }. zMaster
11981 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a 6f Ptr = &zMasterJo
11982 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 urnal[nMasterJou
11983 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d 20 rnal];. rc =
11984 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4d sqlite3OsRead(pM
11985 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f aster, zMasterJo
11986 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 urnal, (int)nMas
11987 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a terJournal, 0);.
11988 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
11989 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c TE_OK ) goto del
1198a 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 20 master_out;..
1198b 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 zJournal = zMas
1198c 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 terJournal;.
1198d 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 6c while( (zJournal
1198e 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 -zMasterJournal)
1198f 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 <nMasterJournal
11990 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 69 ){. int exi
11991 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 sts;. rc =
11992 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
11993 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 pVfs, zJournal,
11994 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
11995 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a ISTS, &exists);.
11996 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
11997 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
11998 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 goto delmaste
11999 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 r_out;. }.
1199a 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 20 if( exists
1199b 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e ){. /* On
1199c 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
1199d 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
1199e 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1199f 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 20 al exists..
119a0 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 6e ** Open it an
119a1 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 6f d check if it po
119a2 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 74 ints at the mast
119a3 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 er journal. If.
119a4 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 65 ** so, re
119a5 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 6c turn without del
119a6 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 eting the master
119a7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
119a8 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
119a9 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 int c;.
119aa 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 int flags = (SQ
119ab 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
119ac 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d LY|SQLITE_OPEN_M
119ad 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 AIN_JOURNAL);.
119ae 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
119af 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a e3OsOpen(pVfs, z
119b0 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 Journal, pJourna
119b1 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 l, flags, 0);.
119b2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
119b3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
119b4 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 goto delmas
119b5 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 ter_out;.
119b6 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 3d }.. rc =
119b7 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e readMasterJourn
119b8 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 al(pJournal, zMa
119b9 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 72 sterPtr, nMaster
119ba 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 Ptr);. sq
119bb 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a 6f lite3OsClose(pJo
119bc 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 urnal);.
119bd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
119be 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 K ){. g
119bf 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 oto delmaster_ou
119c0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 t;. }..
119c1 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 65 c = zMaste
119c2 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 74 rPtr[0]!=0 && st
119c3 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 2c rcmp(zMasterPtr,
119c4 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 20 zMaster)==0;.
119c5 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a 20 if( c ){.
119c6 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 68 /* We h
119c7 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f 20 ave a match. Do
119c8 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d not delete the m
119c9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
119ca 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 le. */.
119cb 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
119cc 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 out;. }.
119cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a 6f }. zJo
119ce 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 65 urnal += (sqlite
119cf 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 6e 3Strlen30(zJourn
119d0 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 al)+1);. }.
119d1 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 }. . rc = sqli
119d2 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
119d3 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a , zMaster, 0);..
119d4 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20 delmaster_out:.
119d5 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 72 if( zMasterJour
119d6 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 nal ){. sqlit
119d7 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 4a e3_free(zMasterJ
119d8 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a 20 ournal);. } .
119d9 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b 0a if( pMaster ){.
119da 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
119db 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 se(pMaster);.
119dc 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e assert( !isOpen
119dd 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 20 (pJournal) );.
119de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
119df 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 (pMaster);. ret
119e0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
119e1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
119e2 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67 is used to chang
119e3 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 7a e the actual siz
119e4 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
119e5 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 68 e .** file in th
119e6 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 54 e file-system. T
119e7 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 his only happens
119e8 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 when committing
119e9 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a a transaction,.
119ea 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 ** or rolling ba
119eb 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ck a transaction
119ec 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c 6c (including roll
119ed 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d 6a ing back a hot-j
119ee 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 ournal)..**.** I
119ef 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 f the main datab
119f0 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 ase file is not
119f1 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 6c open, or an excl
119f2 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e 6f usive lock is no
119f3 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 20 t.** held, this
119f4 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
119f5 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 -op. Otherwise,
119f6 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
119f7 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e 67 file is.** chang
119f8 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 ed to nPage page
119f9 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 2d s (nPage*pPager-
119fa 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 29 >pageSize bytes)
119fb 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a . If the file.**
119fc 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 72 on disk is curr
119fd 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 ently larger tha
119fe 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 74 n nPage pages, t
119ff 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 0a hen use the VFS.
11a00 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d ** xTruncate() m
11a01 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 74 ethod to truncat
11a02 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 e it..**.** Or,
11a03 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 62 it might might b
11a04 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
11a05 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b the file on disk
11a06 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
11a07 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 73 .** nPage pages
11a08 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 . Some operating
11a09 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 6e system implemen
11a0a 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 20 tations can get
11a0b 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a 20 confused if .**
11a0c 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e 63 you try to trunc
11a0d 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 6f ate a file to so
11a0e 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 20 me size that is
11a0f 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 0a larger than it .
11a10 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 2c ** currently is,
11a11 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 20 so detect this
11a12 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 61 case and write a
11a13 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 74 single zero byt
11a14 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e 64 e to .** the end
11a15 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c 65 of the new file
11a16 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 instead..**.**
11a17 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
11a18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
11a19 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
11a1a 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f 64 occurs while mod
11a1b 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 61 ifying.** the da
11a1c 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 74 tabase file, ret
11a1d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
11a1e 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
11a1f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11a20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50 pager_truncate(P
11a21 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
11a22 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 no nPage){. int
11a23 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
11a24 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
11a25 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
11a26 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e 28 USIVE && isOpen(
11a27 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 pPager->fd) ){.
11a28 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 69 i64 currentSi
11a29 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20 ze, newSize;.
11a2a 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 20 /* TODO: Is it
11a2b 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 65 safe to use Page
11a2c 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 72 r.dbFileSize her
11a2d 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 e? */. rc = s
11a2e 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
11a2f 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 75 (pPager->fd, &cu
11a30 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 20 rrentSize);.
11a31 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 72 newSize = pPager
11a32 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 29 ->pageSize*(i64)
11a33 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 72 nPage;. if( r
11a34 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
11a35 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 77 currentSize!=new
11a36 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 66 Size ){. if
11a37 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e 65 ( currentSize>ne
11a38 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 wSize ){.
11a39 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
11a3a 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
11a3b 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 20 fd, newSize);.
11a3c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
11a3d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11a3e 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 sWrite(pPager->f
11a3f 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 7a d, "", 1, newSiz
11a40 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 e-1);. }.
11a41 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
11a42 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
11a43 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 pPager->dbFileS
11a44 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 ize = nPage;.
11a45 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
11a46 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11a47 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c *.** Set the val
11a48 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 2e ue of the Pager.
11a49 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61 sectorSize varia
11a4a 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 ble for the give
11a4b 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 64 n.** pager based
11a4c 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 on the value re
11a4d 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 53 turned by the xS
11a4e 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f 64 ectorSize method
11a4f 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 .** of the open
11a50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
11a51 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 he sector size w
11a52 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 64 ill be used used
11a53 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e .** to determin
11a54 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 61 e the size and a
11a55 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 lignment of jour
11a56 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 0a nal header and .
11a57 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 ** master journa
11a58 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 69 l pointers withi
11a59 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e 61 n created journa
11a5a 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 l files..**.** F
11a5b 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c or temporary fil
11a5c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 es the effective
11a5d 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 sector size is
11a5e 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 always 512 bytes
11a5f 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
11a60 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f e, for non-tempo
11a61 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 20 rary files, the
11a62 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f 72 effective sector
11a63 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 20 size is.** the
11a64 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
11a65 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 7a y the xSectorSiz
11a66 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e 64 e() method round
11a67 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66 0a ed up to 512 if.
11a68 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 ** it is less th
11a69 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e 64 an 512, or round
11a6a 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 ed down to MAX_S
11a6b 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 74 ECTOR_SIZE if it
11a6c 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 .** is greater t
11a6d 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 han MAX_SECTOR_S
11a6e 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 IZE..*/.static v
11a6f 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 7a oid setSectorSiz
11a70 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
11a71 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 {. assert( isOp
11a72 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
11a73 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 | pPager->tempFi
11a74 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 le );.. if( !pP
11a75 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
11a76 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 {. /* Sector
11a77 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 size doesn't mat
11a78 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 ter for temporar
11a79 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 y files. Also, t
11a7a 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6d he file. ** m
11a7b 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e ay not have been
11a7c 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20 opened yet, in
11a7d 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 4f which case the O
11a7e 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 sSectorSize().
11a7f 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 73 ** call will s
11a80 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a egfault.. */.
11a81 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 pPager->sect
11a82 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 orSize = sqlite3
11a83 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 OsSectorSize(pPa
11a84 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 20 ger->fd);. }.
11a85 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 if( pPager->sect
11a86 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 20 orSize<512 ){.
11a87 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 pPager->sector
11a88 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d 0a Size = 512;. }.
11a89 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 if( pPager->se
11a8a 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 ctorSize>MAX_SEC
11a8b 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 TOR_SIZE ){.
11a8c 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43 54 assert( MAX_SECT
11a8d 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b 0a OR_SIZE>=512 );.
11a8e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 pPager->sect
11a8f 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 43 orSize = MAX_SEC
11a90 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a TOR_SIZE;. }.}.
11a91 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 ./*.** Playback
11a92 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 the journal and
11a93 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 65 thus restore the
11a94 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
11a95 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 69 o.** the state i
11a96 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65 20 t was in before
11a97 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69 6e we started makin
11a98 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a g changes. .**.
11a99 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** The journal f
11a9a 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61 73 ile format is as
11a9b 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a follows: .**.**
11a9c 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70 72 (1) 8 byte pr
11a9d 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f 66 efix. A copy of
11a9e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d aJournalMagic[]
11a9f 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79 74 ..** (2) 4 byt
11aa0 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
11aa1 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
11aa2 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 69 e number of vali
11aa3 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a 2a d page records.*
11aa4 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 6a * in the j
11aa5 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 73 ournal. If this
11aa6 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 value is 0xffff
11aa7 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 75 ffff, then compu
11aa8 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 te the.**
11aa9 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 number of page r
11aaa 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 ecords from the
11aab 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a journal size..**
11aac 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62 69 (3) 4 byte bi
11aad 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
11aae 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e which is the in
11aaf 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 20 itial value for
11ab0 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 61 the .** sa
11ab1 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a 2a nity checksum..*
11ab2 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20 69 * (4) 4 byte i
11ab3 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
11ab4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
11ab5 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65 20 ges to truncate
11ab6 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 74 the.** dat
11ab7 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67 20 abase to during
11ab8 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 a rollback..**
11ab9 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67 2d (5) 4 byte big-
11aba 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 endian integer w
11abb 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63 74 hich is the sect
11abc 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68 65 or size. The he
11abd 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 73 ader.** is
11abe 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
11abf 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 36 in size..** (6
11ac0 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
11ac1 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
11ac2 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20 63 ch is the page c
11ac3 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34 20 ase..** (7) 4
11ac4 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69 byte integer whi
11ac5 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
11ac6 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
11ac7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a master journal.
11ac8 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20 20 ** name.
11ac9 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62 65 The value may be
11aca 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65 20 zero (indicate
11acb 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f that there is no
11acc 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 master.**
11acd 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20 28 journal.).** (
11ace 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20 74 8) N bytes of t
11acf 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
11ad0 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61 6d l name. The nam
11ad1 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74 65 e will be nul-te
11ad2 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 rminated.**
11ad3 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20 73 and might be s
11ad4 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65 20 horter than the
11ad5 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 value read from
11ad6 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69 72 (5). If the fir
11ad7 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 st byte.**
11ad8 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 of the name is
11ad9 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65 20 \000 then there
11ada 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 is no master jou
11adb 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74 65 rnal. The maste
11adc 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 6e r.** journ
11add 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 65 al name is store
11ade 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20 20 d in UTF-8..**
11adf 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 (9) Zero or mor
11ae0 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 65 e pages instance
11ae1 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f s, each as follo
11ae2 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 ws:.** +
11ae3 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 4 byte page num
11ae4 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b ber..** +
11ae5 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 pPager->pageSi
11ae6 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 ze bytes of data
11ae7 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34 ..** + 4
11ae8 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 2a byte checksum.*
11ae9 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 65 *.** When we spe
11aea 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ak of the journa
11aeb 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 61 l header, we mea
11aec 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69 74 n the first 8 it
11aed 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 61 ems above..** Ea
11aee 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 ch entry in the
11aef 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 6e journal is an in
11af0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39 74 stance of the 9t
11af1 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 h item..**.** Ca
11af2 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f ll the value fro
11af3 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 6c m the second bul
11af4 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 65 let "nRec". nRe
11af5 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 c is the number
11af6 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 65 of.** valid page
11af7 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
11af8 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f 73 journal. In mos
11af9 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 6e t cases, you can
11afa 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 compute the.**
11afb 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 72 value of nRec fr
11afc 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 om the size of t
11afd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
11afe 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 72 But if a power
11aff 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 75 .** failure occu
11b00 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 6a rred while the j
11b01 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 ournal was being
11b02 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f 75 written, it cou
11b03 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 ld be the.** cas
11b04 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 e that the size
11b05 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
11b06 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 20 ile had already
11b07 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 62 been increased b
11b08 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 20 ut.** the extra
11b09 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 20 entries had not
11b0a 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 65 yet made it safe
11b0b 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e 20 ly to disk. In
11b0c 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a 20 such a case,.**
11b0d 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 the value of nRe
11b0e 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 c computed from
11b0f 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 6f the file size wo
11b10 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 65 uld be too large
11b11 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 72 . For.** that r
11b12 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 73 eason, we always
11b13 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 61 use the nRec va
11b14 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 lue in the heade
11b15 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
11b16 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 78 nRec value is 0x
11b17 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e ffffffff it mean
11b18 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75 s that nRec shou
11b19 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a 2a ld be computed.*
11b1a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 * from the file
11b1b 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75 size. This valu
11b1c 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 e is used when t
11b1d 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 20 he user selects
11b1e 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f the.** no-sync o
11b1f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f ption for the jo
11b20 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20 urnal. A power
11b21 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 failure could le
11b22 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e ad to corruption
11b23 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 .** in this case
11b24 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e 67 . But for thing
11b25 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 79 s like temporary
11b26 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 69 table (which wi
11b27 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 ll be.** deleted
11b28 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 20 when the power
11b29 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 20 is restored) we
11b2a 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a 2a don't care. .**
11b2b 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 .** If the file
11b2c 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a 6f opened as the jo
11b2d 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f urnal file is no
11b2e 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a t a well-formed.
11b2f 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
11b30 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 75 then all pages u
11b31 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 p to the first c
11b32 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61 72 orrupted page ar
11b33 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b e rolled.** back
11b34 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 66 (or no pages if
11b35 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11b36 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 64 der is corrupted
11b37 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ). The journal f
11b38 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 64 ile.** is then d
11b39 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 eleted and SQLIT
11b3a 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 6a E_OK returned, j
11b3b 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f 72 ust as if no cor
11b3c 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 62 ruption had.** b
11b3d 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e een encountered.
11b3e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f 4f .**.** If an I/O
11b3f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 or malloc() err
11b40 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6a or occurs, the j
11b41 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e ournal-file is n
11b42 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 6e ot deleted.** an
11b43 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
11b44 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
11b45 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 72 ** The isHot par
11b46 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 ameter indicates
11b47 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 79 that we are try
11b48 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 ing to rollback
11b49 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 61 a journal.** tha
11b4a 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74 t might be a hot
11b4b 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69 journal. Or, i
11b4c 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 t could be that
11b4d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0a the journal is .
11b4e 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 63 ** preserved bec
11b4f 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d ause of JOURNALM
11b50 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a ODE_PERSIST or J
11b51 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
11b52 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6a ATE..** If the j
11b53 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73 ournal really is
11b54 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20 hot, reset the
11b55 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 6f pager cache prio
11b56 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 63 r rolling.** bac
11b57 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 20 k any content.
11b58 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 If the journal i
11b59 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 74 s merely persist
11b5a 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 73 ent, no reset is
11b5b 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 .** needed..*/.s
11b5c 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
11b5d 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 2a playback(Pager *
11b5e 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 6f pPager, int isHo
11b5f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 t){. sqlite3_vf
11b60 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 s *pVfs = pPager
11b61 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 7a ->pVfs;. i64 sz
11b62 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 J;
11b63 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
11b64 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
11b65 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 n bytes */. u32
11b66 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 nRec;
11b67 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
11b68 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 68 of Records in th
11b69 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 e journal */. u
11b6a 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 32 u;
11b6b 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 /* Unsig
11b6c 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 ned loop counter
11b6d 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20 */. Pgno mxPg
11b6e 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 0; /
11b6f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 72 * Size of the or
11b70 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70 iginal file in p
11b71 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ages */. int rc
11b72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11b73 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
11b74 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e e of a subroutin
11b75 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d e */. int res =
11b76 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
11b77 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 /* Value returne
11b78 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 63 d by sqlite3OsAc
11b79 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 cess() */. char
11b7a 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 *zMaster = 0;
11b7b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
11b7c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11b7d 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 ile if any */.
11b7e 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 73 int needPagerRes
11b7f 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 et; /* True
11b80 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 70 to reset page p
11b81 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 61 rior to first pa
11b82 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a ge rollback */..
11b83 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
11b84 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73 how many records
11b85 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 are in the jour
11b86 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c nal. Abort earl
11b87 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a 6f y if. ** the jo
11b88 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0a urnal is empty..
11b89 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
11b8a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
11b8b 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c d) );. rc = sql
11b8c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
11b8d 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a Pager->jfd, &szJ
11b8e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
11b8f 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 ITE_OK || szJ==0
11b90 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 ){. goto end
11b91 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a _playback;. }..
11b92 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d 61 /* Read the ma
11b93 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
11b94 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e e from the journ
11b95 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72 65 al, if it is pre
11b96 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 20 sent.. ** If a
11b97 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11b98 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 63 ile name is spec
11b99 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 66 ified, but the f
11b9a 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 ile is not. **
11b9b 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b 2c present on disk,
11b9c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
11b9d 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e 64 l is not hot and
11b9e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
11b9f 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 64 o be. ** played
11ba0 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a back.. **. **
11ba1 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 6c TODO: Technical
11ba2 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ly the following
11ba3 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65 63 is an error bec
11ba4 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73 20 ause it assumes
11ba5 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 that. ** buffer
11ba6 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 Pager.pTmpSpace
11ba7 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 2b is (mxPathname+
11ba8 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72 67 1) bytes or larg
11ba9 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 20 er. i.e. that.
11baa 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65 ** (pPager->page
11bab 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d 3e Size >= pPager->
11bac 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
11bad 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 6e +1). Using os_un
11bae 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 61 ix.c,. ** mxPa
11baf 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 77 thname is 512, w
11bb0 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d 65 hich is the same
11bb1 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 as the minimum
11bb2 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 0a allowable value.
11bb3 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 7a ** for pageSiz
11bb4 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 e.. */. zMaste
11bb5 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 r = pPager->pTmp
11bb6 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 65 Space;. rc = re
11bb7 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 adMasterJournal(
11bb8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 pPager->jfd, zMa
11bb9 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 ster, pPager->pV
11bba 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
11bbb 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
11bbc 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 ITE_OK && zMaste
11bbd 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d r[0] ){. rc =
11bbe 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
11bbf 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
11bc0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
11bc1 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 7d ISTS, &res);. }
11bc2 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a . zMaster = 0;.
11bc3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
11bc4 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a 20 _OK || !res ){.
11bc5 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
11bc6 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 back;. }. pPag
11bc7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
11bc8 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 52 0;. needPagerR
11bc9 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a 20 eset = isHot;..
11bca 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 /* This loop te
11bcb 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 20 rminates either
11bcc 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 6e when a readJourn
11bcd 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a 2a alHdr() or . **
11bce 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
11bcf 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c 20 one_page() call
11bd0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 returns SQLITE_D
11bd1 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 ONE or an IO err
11bd2 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 2e or . ** occurs.
11bd3 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 . */. while(
11bd4 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 55 1 ){. int isU
11bd5 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20 nsync = 0;..
11bd6 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 /* Read the next
11bd7 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11bd8 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c from the journal
11bd9 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 file. If there
11bda 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 are. ** not
11bdb 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65 66 enough bytes lef
11bdc 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c t in the journal
11bdd 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70 file for a comp
11bde 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72 0a lete header, or.
11bdf 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f 72 ** it is cor
11be0 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20 70 rupted, then a p
11be1 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20 66 rocess must of f
11be2 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74 ailed while writ
11be3 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54 ing it.. ** T
11be4 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e 6f his indicates no
11be5 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73 thing more needs
11be6 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
11be7 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 ck.. */. r
11be8 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 c = readJournalH
11be9 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a 2c 20 dr(pPager, szJ,
11bea 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a 20 &nRec, &mxPg);.
11beb 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
11bec 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 69 E_OK ){ . i
11bed 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f f( rc==SQLITE_DO
11bee 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 NE ){. rc
11bef 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
11bf0 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
11bf1 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 end_playback;.
11bf2 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
11bf3 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66 66 nRec is 0xffffff
11bf4 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a 6f ff, then this jo
11bf5 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 65 urnal was create
11bf6 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a 20 d by a process.
11bf7 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 6e ** working in
11bf8 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 54 no-sync mode. T
11bf9 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 his means that t
11bfa 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a he rest of the j
11bfb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
11bfc 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 70 le consists of p
11bfd 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 20 ages, there are
11bfe 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c 20 no more journal
11bff 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 65 headers. Compute
11c00 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 . ** the valu
11c01 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 20 e of nRec based
11c02 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 on this assumpti
11c03 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 on.. */. i
11c04 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 66 f( nRec==0xfffff
11c05 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73 fff ){. ass
11c06 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
11c07 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c rnalOff==JOURNAL
11c08 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
11c09 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 );. nRec =
11c0a 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f 55 (int)((szJ - JOU
11c0b 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11c0c 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f er))/JOURNAL_PG_
11c0d 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 SZ(pPager));.
11c0e 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 }.. /* If nR
11c0f 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 ec is 0 and this
11c10 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 rollback is of
11c11 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 a transaction cr
11c12 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 eated by this.
11c13 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 ** process and
11c14 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
11c15 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 final header in
11c16 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 the journal, the
11c17 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a n it means. *
11c18 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 * that this part
11c19 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11c1a 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 was being filled
11c1b 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 but has not yet
11c1c 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e been. ** syn
11c1d 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f ced to disk. Co
11c1e 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 mpute the number
11c1f 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 of pages based
11c20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 on the remaining
11c21 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 . ** size of
11c22 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a the file.. **
11c23 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 . ** The thir
11c24 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 d term of the te
11c25 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 st was added to
11c26 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 fix ticket #2565
11c27 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 6f .. ** When ro
11c28 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 lling back a hot
11c29 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d journal, nRec==
11c2a 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 74 0 always means t
11c2b 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 20 hat the next.
11c2c 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 65 ** chunk of the
11c2d 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e journal contain
11c2e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f 20 s zero pages to
11c2f 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
11c30 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e But. ** when
11c31 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 doing a ROLLBAC
11c32 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d 3d K and the nRec==
11c33 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 6c 0 chunk is the l
11c34 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 20 ast chunk in.
11c35 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c ** the journal,
11c36 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
11c37 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 he journal might
11c38 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f contain additio
11c39 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 73 nal. ** pages
11c3a 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 that need to be
11c3b 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 rolled back and
11c3c 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 that the number
11c3d 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 2a of pages . *
11c3e 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 * should be comp
11c3f 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 uted based on th
11c40 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 e journal file s
11c41 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ize.. */.
11c42 74 65 73 74 63 61 73 65 28 20 6e 52 65 63 3d 3d testcase( nRec==
11c43 30 20 26 26 20 21 69 73 48 6f 74 0a 20 20 20 20 0 && !isHot.
11c44 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e && pPager->
11c45 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
11c46 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11c47 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )!=pPager->journ
11c48 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26 alOff. &
11c49 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 & ((szJ - pPager
11c4a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 ->journalOff) /
11c4b 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
11c4c 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20 ager))>0.
11c4d 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f && pagerNextJo
11c4e 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64 urnalPageIsValid
11c4f 28 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a (pPager). );.
11c50 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 20 if( nRec==0
11c51 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20 20 && !isHot &&.
11c52 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
11c53 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
11c54 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
11c55 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11c56 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65 63 ff ){. nRec
11c57 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 = (int)((szJ -
11c58 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11c59 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 ff) / JOURNAL_PG
11c5a 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 _SZ(pPager));.
11c5b 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20 31 isUnsync = 1
11c5c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
11c5d 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 If this is the f
11c5e 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61 64 irst header read
11c5f 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
11c60 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a l, truncate the.
11c61 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
11c62 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73 file back to its
11c63 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a original size..
11c64 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
11c65 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11c66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 f==JOURNAL_HDR_S
11c67 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 Z(pPager) ){.
11c68 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 rc = pager_tr
11c69 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d uncate(pPager, m
11c6a 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 xPg);. if(
11c6b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
11c6c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
11c6d 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 d_playback;.
11c6e 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
11c6f 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 3b ->dbSize = mxPg;
11c70 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
11c71 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 opy original pag
11c72 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f es out of the jo
11c73 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 urnal and back i
11c74 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 nto the . **
11c75 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
11c76 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65 2e d/or page cache.
11c77 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 . */. for(
11c78 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b 2b u=0; u<nRec; u++
11c79 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65 ){. if( nee
11c7a 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a 20 dPagerReset ){.
11c7b 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 pager_res
11c7c 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
11c7d 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 73 needPagerRes
11c7e 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a et = 0;. }.
11c7f 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
11c80 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
11c81 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55 6e ge(pPager,1,isUn
11c82 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a 6f sync,&pPager->jo
11c83 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a 20 urnalOff,0,0);.
11c84 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
11c85 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
11c86 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
11c87 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 _DONE ){.
11c88 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
11c89 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 K;. pPa
11c8a 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
11c8b 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20 = szJ;.
11c8c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
11c8d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
11c8e 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75 6e /* If we are un
11c8f 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b able to rollback
11c90 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75 72 , quit and retur
11c91 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20 20 n the error.
11c92 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20 ** code.
11c93 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 This will cause
11c94 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 the pager to ent
11c95 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
11c96 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 te. **
11c97 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74 68 so that no furth
11c98 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 20 er harm will be
11c99 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20 74 done. Perhaps t
11c9a 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20 20 he next.
11c9b 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f 20 ** process to
11c9c 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c 20 come along will
11c9d 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 be able to rollb
11c9e 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ack the database
11c9f 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
11ca0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
11ca1 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 d_playback;.
11ca2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
11ca3 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 }. }. /*NOTR
11ca4 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65 72 EACHED*/. asser
11ca5 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c 61 t( 0 );..end_pla
11ca6 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c 6c yback:. /* Foll
11ca7 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b owing a rollback
11ca8 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 , the database f
11ca9 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62 61 ile should be ba
11caa 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69 6e ck in its origin
11cab 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70 72 al. ** state pr
11cac 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 ior to the start
11cad 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 of the transact
11cae 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74 ion, so invoke t
11caf 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46 he. ** SQLITE_F
11cb0 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 CNTL_DB_UNCHANGE
11cb1 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d D file-control m
11cb2 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c 65 ethod to disable
11cb3 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 the. ** assert
11cb4 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72 61 ion that the tra
11cb5 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
11cb6 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a 20 was modified..
11cb7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20 20 */. assert(.
11cb8 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d pPager->fd->pM
11cb9 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 20 ethods==0 ||.
11cba 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f sqlite3OsFileCo
11cbb 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66 64 ntrol(pPager->fd
11cbc 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 ,SQLITE_FCNTL_DB
11cbd 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d 53 _UNCHANGED,0)>=S
11cbe 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a 20 QLITE_OK. );..
11cbf 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61 79 /* If this play
11cc0 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69 6e back is happenin
11cc1 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 g automatically
11cc2 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 as a result of a
11cc3 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d 61 n IO or . ** ma
11cc4 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74 20 lloc error that
11cc5 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20 74 occurred after t
11cc6 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
11cc7 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62 75 r was updated bu
11cc8 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 t . ** before t
11cc9 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 he transaction w
11cca 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 as committed, th
11ccb 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f en the change-co
11ccc 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 69 unter . ** modi
11ccd 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75 73 fication may jus
11cce 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76 65 t have been reve
11ccf 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68 61 rted. If this ha
11cd0 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73 69 ppens in exclusi
11cd1 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 ve . ** mode, t
11cd2 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 74 hen subsequent t
11cd3 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72 66 ransactions perf
11cd4 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f 6e ormed by the con
11cd5 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 nection will not
11cd6 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68 65 . ** update the
11cd7 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
11cd8 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61 79 at all. This may
11cd9 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20 69 lead to cache i
11cda 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20 2a nconsistency. *
11cdb 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f * problems for o
11cdc 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 61 ther processes a
11cdd 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 t some point in
11cde 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c 20 the future. So,
11cdf 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61 73 just. ** in cas
11ce0 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70 65 e this has happe
11ce1 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20 63 ned, clear the c
11ce2 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 hangeCountDone f
11ce3 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 lag now.. */.
11ce4 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
11ce5 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 untDone = pPager
11ce6 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 69 ->tempFile;.. i
11ce7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11ce8 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 ){. zMaster
11ce9 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 = pPager->pTmpSp
11cea 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 ace;. rc = re
11ceb 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 adMasterJournal(
11cec 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 pPager->jfd, zMa
11ced 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 ster, pPager->pV
11cee 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
11cef 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
11cf0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11cf1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
11cf2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
11cf3 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
11cf4 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
11cf5 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d er, zMaster[0]!=
11cf6 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74 63 '\0');. testc
11cf7 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
11cf8 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OK );. }. if(
11cf9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
11cfa 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20 72 zMaster[0] && r
11cfb 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 es ){. /* If
11cfc 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73 74 there was a mast
11cfd 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 er journal and t
11cfe 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
11cff 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73 2c return success,
11d00 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 . ** see if i
11d01 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f t is possible to
11d02 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 delete the mast
11d03 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 er journal..
11d04 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 */. rc = page
11d05 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61 67 r_delmaster(pPag
11d06 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 er, zMaster);.
11d07 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
11d08 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d SQLITE_OK );. }
11d09 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65 72 .. /* The Pager
11d0a 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 .sectorSize vari
11d0b 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65 able may have be
11d0c 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c 65 en updated while
11d0d 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 61 rolling. ** ba
11d0e 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72 65 ck a journal cre
11d0f 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 ated by a proces
11d10 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 s with a differe
11d11 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a 20 nt sector size.
11d12 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65 74 ** value. Reset
11d13 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72 65 it to the corre
11d14 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69 ct value for thi
11d15 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a s process.. */.
11d16 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 setSectorSize(
11d17 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 pPager);. retur
11d18 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 n rc;.}../*.** P
11d19 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69 6e layback savepoin
11d1a 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f 72 t pSavepoint. Or
11d1b 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74 3d , if pSavepoint=
11d1c 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 79 =NULL, then play
11d1d 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 69 back.** the enti
11d1e 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 re master journa
11d1f 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73 65 l file. The case
11d20 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c pSavepoint==NUL
11d21 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a 2a L occurs when .*
11d22 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 * a ROLLBACK TO
11d23 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f 6b command is invok
11d24 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e ed on a SAVEPOIN
11d25 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61 6e T that is a tran
11d26 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 65 saction .** save
11d27 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 point..**.** Whe
11d28 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 n pSavepoint is
11d29 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e not NULL (meanin
11d2a 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 g a non-transact
11d2b 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 ion savepoint is
11d2c 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65 .** being rolle
11d2d 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 68 d back), then th
11d2e 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69 e rollback consi
11d2f 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 72 sts of up to thr
11d30 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70 65 ee stages,.** pe
11d31 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 6f rformed in the o
11d32 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a 0a rder specified:.
11d33 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 **.** * Pages
11d34 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 are played back
11d35 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f from the main jo
11d36 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 urnal starting a
11d37 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f 66 t byte.** of
11d38 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70 6f fset PagerSavepo
11d39 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 int.iOffset and
11d3a 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a 2a continuing to .*
11d3b 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 * PagerSavep
11d3c 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 2c oint.iHdrOffset,
11d3d 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f or to the end o
11d3e 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
11d3f 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 69 al.** file i
11d40 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f PagerSavepoint
11d41 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 7a .iHdrOffset is z
11d42 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 ero..**.** * I
11d43 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f PagerSavepoint
11d44 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 6e .iHdrOffset is n
11d45 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70 61 ot zero, then pa
11d46 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a 2a ges are played.*
11d47 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72 74 * back start
11d48 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ing from the jou
11d49 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d 65 rnal header imme
11d4a 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e diately followin
11d4b 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 g .** PagerS
11d4c 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
11d4d 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f set to the end o
11d4e 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
11d4f 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 al file..**.**
11d50 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68 65 * Pages are the
11d51 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 n played back fr
11d52 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e om the sub-journ
11d53 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69 6e al file, startin
11d54 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74 68 g.** with th
11d55 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
11d56 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f 6e .iSubRec and con
11d57 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 65 tinuing to the e
11d58 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68 65 nd of.** the
11d59 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
11d5a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 20 *.** Throughout
11d5b 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f the rollback pro
11d5c 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 20 cess, each time
11d5d 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 a page is rolled
11d5e 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 6f back, the.** co
11d5f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
11d60 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 76 is set in a bitv
11d61 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 61 ec structure (va
11d62 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20 riable pDone in
11d63 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 the.** implement
11d64 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 68 ation below). Th
11d65 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e is is used to en
11d66 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67 65 sure that a page
11d67 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c is only.** roll
11d68 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72 73 ed back the firs
11d69 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63 t time it is enc
11d6a 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74 68 ountered in eith
11d6b 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a er journal..**.*
11d6c 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74 20 * If pSavepoint
11d6d 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 61 is NULL, then pa
11d6e 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c 61 ges are only pla
11d6f 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 yed back from th
11d70 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 e main.** journa
11d71 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69 73 l file. There is
11d72 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 62 no need for a b
11d73 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63 61 itvec in this ca
11d74 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74 se..**.** In eit
11d75 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72 65 her case, before
11d76 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65 6e playback commen
11d77 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64 62 ces the Pager.db
11d78 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a Size variable.**
11d79 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68 65 is reset to the
11d7a 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20 68 value that it h
11d7b 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72 74 eld at the start
11d7c 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e of the savepoin
11d7d 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73 61 t .** (or transa
11d7e 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 20 ction). No page
11d7f 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d 62 with a page-numb
11d80 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 er greater than
11d81 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 this value.** is
11d82 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49 66 played back. If
11d83 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 one is encounte
11d84 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c 79 red it is simply
11d85 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 skipped..*/.sta
11d86 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c 61 tic int pagerPla
11d87 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 50 ybackSavepoint(P
11d88 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 61 ager *pPager, Pa
11d89 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 gerSavepoint *pS
11d8a 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36 34 avepoint){. i64
11d8b 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
11d8c 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74 69 /* Effecti
11d8d 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d ve size of the m
11d8e 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 ain journal */.
11d8f 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 i64 iHdrOff;
11d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 /* End
11d91 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65 6e of first segmen
11d92 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61 t of main-journa
11d93 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 l records */. i
11d94 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
11d95 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 K; /* Retur
11d96 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74 76 n code */. Bitv
11d97 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20 ec *pDone = 0;
11d98 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 74 /* Bitvec t
11d99 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 70 o ensure pages p
11d9a 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20 layed back only
11d9b 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 once */.. asser
11d9c 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
11d9d 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 >=PAGER_SHARED )
11d9e 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
11d9f 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73 65 a bitvec to use
11da0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 to store the se
11da1 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c 65 t of pages rolle
11da2 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 d back */. if(
11da3 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
11da4 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74 65 pDone = sqlite
11da5 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 53 3BitvecCreate(pS
11da6 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 avepoint->nOrig)
11da7 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e 65 ;. if( !pDone
11da8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
11da9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
11daa 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
11dab 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 et the database
11dac 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65 size back to the
11dad 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62 65 value it was be
11dae 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f 69 fore the savepoi
11daf 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72 nt . ** being r
11db0 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 6e everted was open
11db1 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 ed.. */. pPage
11db2 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61 76 r->dbSize = pSav
11db3 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70 6f epoint ? pSavepo
11db4 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 61 int->nOrig : pPa
11db5 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b ger->dbOrigSize;
11db6 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67 65 .. /* Use pPage
11db7 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 73 r->journalOff as
11db8 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73 the effective s
11db9 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 ize of the main
11dba 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f rollback. ** jo
11dbb 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74 75 urnal. The actu
11dbc 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62 65 al file might be
11dbd 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 larger than thi
11dbe 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52 5f s in. ** PAGER_
11dbf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
11dc0 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a 4f CATE or PAGER_JO
11dc1 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
11dc2 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e 67 T. But anything
11dc3 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67 65 . ** past pPage
11dc4 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 r->journalOff is
11dc5 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20 75 off-limits to u
11dc6 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d 20 s.. */. szJ =
11dc7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11dc8 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 ff;.. /* Begin
11dc9 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 by rolling back
11dca 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 records from the
11dcb 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 main journal st
11dcc 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 50 arting at. ** P
11dcd 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f agerSavepoint.iO
11dce 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e ffset and contin
11dcf 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78 74 uing to the next
11dd0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e journal header.
11dd1 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67 68 . ** There migh
11dd2 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e 20 t be records in
11dd3 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
11dd4 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61 67 that have a pag
11dd5 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67 72 e number. ** gr
11dd6 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 eater than the c
11dd7 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 urrent database
11dd8 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64 62 size (pPager->db
11dd9 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65 0a Size) but those.
11dda 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69 ** will be ski
11ddb 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c pped automatical
11ddc 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20 61 ly. Pages are a
11ddd 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61 73 dded to pDone as
11dde 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20 70 they. ** are p
11ddf 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f layed back.. */
11de0 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e . if( pSavepoin
11de1 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 t ){. iHdrOff
11de2 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 = pSavepoint->i
11de3 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61 76 HdrOffset ? pSav
11de4 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 epoint->iHdrOffs
11de5 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 et : szJ;. pP
11de6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11de7 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 = pSavepoint->i
11de8 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c Offset;. whil
11de9 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
11dea 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
11deb 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 nalOff<iHdrOff )
11dec 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
11ded 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f er_playback_one_
11dee 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 page(pPager, 1,
11def 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 0, &pPager->jour
11df0 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 nalOff, 1, pDone
11df1 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
11df2 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
11df3 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65 7b DONE );. }else{
11df4 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
11df5 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 7d rnalOff = 0;. }
11df6 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 .. /* Continue
11df7 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 rolling back rec
11df8 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65 20 ords out of the
11df9 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 main journal sta
11dfa 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74 68 rting at. ** th
11dfb 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 e first journal
11dfc 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64 20 header seen and
11dfd 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69 6c continuing until
11dfe 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 65 the effective e
11dff 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d nd. ** of the m
11e00 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
11e01 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20 73 . Continue to s
11e02 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 kip out-of-range
11e03 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a 20 pages and. **
11e04 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67 20 continue adding
11e05 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 pages rolled bac
11e06 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a 2f k to pDone.. */
11e07 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 . while( rc==SQ
11e08 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
11e09 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a r->journalOff<sz
11e0a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 3b J ){. u32 ii;
11e0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
11e0c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
11e0d 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20 30 u32 nJRec = 0
11e0e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
11e0f 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72 of Journal Recor
11e10 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 75 ds */. u32 du
11e11 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 mmy;. rc = re
11e12 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 adJournalHdr(pPa
11e13 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 ger, szJ, &nJRec
11e14 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 , &dummy);. a
11e15 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
11e16 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f E_DONE );.. /
11e17 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 *. ** The "pP
11e18 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
11e19 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
11e1a 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d pPager)==pPager-
11e1b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 >journalOff".
11e1c 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 ** test is rela
11e1d 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 ted to ticket #2
11e1e 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 565. See the di
11e1f 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a scussion in the.
11e20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 ** pager_pla
11e21 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e yback() function
11e22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
11e23 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 information..
11e24 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
11e25 21 28 6e 4a 52 65 63 3d 3d 30 0a 20 20 20 20 20 !(nJRec==0.
11e26 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a && pPager->j
11e27 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 ournalHdr+JOURNA
11e28 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
11e29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 !=pPager->journa
11e2a 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26 26 lOff. &&
11e2b 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d ((szJ - pPager-
11e2c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a >journalOff) / J
11e2d 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
11e2e 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20 20 ger))>0.
11e2f 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f 75 && pagerNextJou
11e30 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64 28 rnalPageIsValid(
11e31 70 50 61 67 65 72 29 29 0a 20 20 20 20 29 3b 0a pPager)). );.
11e32 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 if( nJRec==0
11e33 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 . && pPager
11e34 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 ->journalHdr+JOU
11e35 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11e36 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 er)==pPager->jou
11e37 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 rnalOff. ){.
11e38 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 nJRec = (u3
11e39 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2)((szJ - pPager
11e3a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f ->journalOff)/JO
11e3b 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
11e3c 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 er));. }.
11e3d 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 for(ii=0; rc==SQ
11e3e 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a LITE_OK && ii<nJ
11e3f 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a Rec && pPager->j
11e40 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 ournalOff<szJ; i
11e41 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d i++){. rc =
11e42 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
11e43 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
11e44 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 1, 0, &pPager->
11e45 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 journalOff, 1, p
11e46 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Done);. }.
11e47 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
11e48 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a ITE_DONE );. }.
11e49 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
11e4a 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 LITE_OK || pPage
11e4b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 r->journalOff==s
11e4c 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 zJ );.. /* Fina
11e4d 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 lly, rollback p
11e4e 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 ages from the su
11e4f 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 b-journal. Page
11e50 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 that were. **
11e51 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 previously rolle
11e52 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 d back out of th
11e53 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 e main journal (
11e54 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e and are hence in
11e55 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c pDone). ** wil
11e56 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f l be skipped. O
11e57 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 ut-of-range page
11e58 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 s are also skipp
11e59 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ed.. */. if( p
11e5a 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
11e5b 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 u32 ii;
11e5c 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
11e5d 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f ter */. i64 o
11e5e 66 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69 ffset = pSavepoi
11e5f 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 nt->iSubRec*(4+p
11e60 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
11e61 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61 ;. for(ii=pSa
11e62 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 vepoint->iSubRec
11e63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
11e64 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 && ii<pPager->nS
11e65 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 ubRec; ii++){.
11e66 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 assert( offs
11e67 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 et==ii*(4+pPager
11e68 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 ->pageSize) );.
11e69 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
11e6a 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
11e6b 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 e(pPager, 0, 0,
11e6c 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e &offset, 1, pDon
11e6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 e);. }. as
11e6e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
11e6f 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 _DONE );. }..
11e70 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
11e71 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 troy(pDone);. i
11e72 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11e73 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
11e74 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a journalOff = szJ
11e75 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
11e76 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e c;.}../*.** Chan
11e77 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e ge the maximum n
11e78 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f umber of in-memo
11e79 72 79 20 70 61 67 65 73 20 74 68 61 74 20 61 72 ry pages that ar
11e7a 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 e allowed..*/.SQ
11e7b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
11e7c 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 d sqlite3PagerSe
11e7d 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 tCachesize(Pager
11e7e 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 *pPager, int mx
11e7f 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 Page){. sqlite3
11e80 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 PcacheSetCachesi
11e81 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ze(pPager->pPCac
11e82 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a he, mxPage);.}..
11e83 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 /*.** Adjust the
11e84 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 robustness of t
11e85 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 he database to d
11e86 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 amage due to OS
11e87 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f crashes.** or po
11e88 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 wer failures by
11e89 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d changing the num
11e8a 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 ber of syncs()s
11e8b 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 when writing.**
11e8c 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
11e8d 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 rnal. There are
11e8e 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a three levels:.*
11e8f 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 *.** OFF
11e90 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 sqlite3OsSync(
11e91 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 ) is never calle
11e92 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 d. This is the
11e93 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 default.**
11e94 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 for temp
11e95 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 orary and transi
11e96 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a ent files..**.**
11e97 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 NORMAL Th
11e98 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e e journal is syn
11e99 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 ced once before
11e9a 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 writes begin on
11e9b 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
11e9c 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 database. T
11e9d 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 his is normally
11e9e 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 adequate protect
11e9f 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 ion, but.**
11ea0 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74 it is t
11ea1 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 heoretically pos
11ea2 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 sible, though ve
11ea3 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 ry unlikely,.**
11ea4 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61 tha
11ea5 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 t an inopertune
11ea6 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f power failure co
11ea7 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f uld leave the jo
11ea8 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 urnal.**
11ea9 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 in a state
11eaa 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 which would cau
11eab 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 se damage to the
11eac 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
11ead 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 when i
11eae 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b t is rolled back
11eaf 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 ..**.** FULL
11eb0 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c The journal
11eb1 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 is synced twice
11eb2 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 before writes b
11eb3 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 egin on the.**
11eb4 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 data
11eb5 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 base (with some
11eb6 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
11eb7 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 mation - the nRe
11eb8 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 c field.**
11eb9 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a of the j
11eba 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 ournal header -
11ebb 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e being written in
11ebc 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f between the two
11ebd 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11ebe 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 syncs). If we
11ebf 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 assume that writ
11ec0 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 ing a.**
11ec1 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 single dis
11ec2 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d k sector is atom
11ec3 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f ic, then this mo
11ec4 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 de provides.**
11ec5 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75 assu
11ec6 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a rance that the j
11ec7 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 ournal will not
11ec8 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 be corrupted to
11ec9 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
11eca 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 point of cau
11ecb 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 sing damage to t
11ecc 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69 he database duri
11ecd 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a ng rollback..**.
11ece 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 ** Numeric value
11ecf 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
11ed0 68 20 74 68 65 73 65 20 73 74 61 74 65 73 20 61 h these states a
11ed1 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 re OFF==1, NORMA
11ed2 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c L=2,.** and FULL
11ed3 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 =3..*/.#ifndef S
11ed4 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
11ed5 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f _PRAGMAS.SQLITE_
11ed6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
11ed7 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 ite3PagerSetSafe
11ed8 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 tyLevel(Pager *p
11ed9 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c Pager, int level
11eda 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 , int bFullFsync
11edb 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 ){. pPager->noS
11edc 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 ync = (level==1
11edd 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
11ede 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 File) ?1:0;. pP
11edf 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d ager->fullSync =
11ee0 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 (level==3 && !p
11ee1 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 Pager->tempFile)
11ee2 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d ?1:0;. pPager-
11ee3 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 >sync_flags = (b
11ee4 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 FullFsync?SQLITE
11ee5 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 _SYNC_FULL:SQLIT
11ee6 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a E_SYNC_NORMAL);.
11ee7 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f if( pPager->no
11ee8 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e Sync ) pPager->n
11ee9 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 eedSync = 0;.}.#
11eea 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
11eeb 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 following globa
11eec 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e l variable is in
11eed 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 cremented whenev
11eee 65 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a er the library.*
11eef 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 * attempts to op
11ef0 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 en a temporary f
11ef1 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 ile. This infor
11ef2 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 mation is used f
11ef3 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e or.** testing an
11ef4 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e d analysis only.
11ef5 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c .*/.#ifdef SQL
11ef6 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
11ef7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
11ef8 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d opentemp_count =
11ef9 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
11efa 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 * Open a tempora
11efb 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 ry file..**.** W
11efc 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 rite the file de
11efd 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 scriptor into *p
11efe 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c File. Return SQL
11eff 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
11f00 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 s .** or some ot
11f01 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 her error code i
11f02 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f f we fail. The O
11f03 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 S will automatic
11f04 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 ally .** delete
11f05 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 the temporary fi
11f06 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c le when it is cl
11f07 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
11f08 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 flags passed to
11f09 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f the VFS layer xO
11f0a 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 pen() call are t
11f0b 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a hose specified.*
11f0c 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 * by parameter v
11f0d 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 fsFlags ORed wit
11f0e 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a h the following:
11f0f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 .**.** SQLIT
11f10 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
11f11 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f .** SQLITE_O
11f12 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 PEN_CREATE.**
11f13 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
11f14 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 CLUSIVE.** S
11f15 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
11f16 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 EONCLOSE.*/.stat
11f17 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e ic int pagerOpen
11f18 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 temp(. Pager *p
11f19 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a Pager, /*
11f1a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 The pager objec
11f1b 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 t */. sqlite3_f
11f1c 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 ile *pFile, /*
11f1d 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 Write the file d
11f1e 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a escriptor here *
11f1f 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
11f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
11f21 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
11f22 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a h to the VFS */.
11f23 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
11f24 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
11f25 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 turn code */..#i
11f26 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
11f27 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 . sqlite3_opent
11f28 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a emp_count++; /*
11f29 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e Used for testin
11f2a 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
11f2b 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 nly */.#endif..
11f2c 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 vfsFlags |= SQ
11f2d 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
11f2e 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 ITE | SQLITE_OPE
11f2f 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 N_CREATE |.
11f30 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
11f31 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 EN_EXCLUSIVE | S
11f32 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
11f33 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d EONCLOSE;. rc =
11f34 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
11f35 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 Pager->pVfs, 0,
11f36 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c pFile, vfsFlags,
11f37 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 0);. assert( r
11f38 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
11f39 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b isOpen(pFile) );
11f3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11f3b 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 ./*.** Set the b
11f3c 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 usy handler func
11f3d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
11f3e 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 pager invokes th
11f3f 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 e busy-handler i
11f40 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 f sqlite3OsLock(
11f41 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 ) returns .** SQ
11f42 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 LITE_BUSY when t
11f43 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 rying to upgrade
11f44 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f from no-lock to
11f45 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a a SHARED lock,.
11f46 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e ** or when tryin
11f47 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f g to upgrade fro
11f48 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 m a RESERVED loc
11f49 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 k to an EXCLUSIV
11f4a 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 E .** lock. It d
11f4b 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 oes *not* invoke
11f4c 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
11f4d 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 r when upgrading
11f4e 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 from.** SHARED
11f4f 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 to RESERVED, or
11f50 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 when upgrading f
11f51 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 rom SHARED to EX
11f52 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 CLUSIVE.** (whic
11f53 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 h occurs during
11f54 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
11f55 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a back). Summary:.
11f56 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 **.** Transiti
11f57 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
11f58 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f | Invo
11f59 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 kes xBusyHandler
11f5a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
11f5b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11f5c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11f5d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
11f5e 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 * NO_LOCK
11f5f 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b -> SHARED_LOCK
11f60 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 | Yes.**
11f61 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d SHARED_LOCK -
11f62 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 > RESERVED_LOCK
11f63 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 | No.** SHA
11f64 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 RED_LOCK -> EX
11f65 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c CLUSIVE_LOCK |
11f66 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 No.** RESERVE
11f67 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 D_LOCK -> EXCLUS
11f68 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 IVE_LOCK | Yes
11f69 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 .**.** If the bu
11f6a 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 sy-handler callb
11f6b 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d ack returns non-
11f6c 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 zero, the lock i
11f6d 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 s .** retried. I
11f6e 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 f it returns zer
11f6f 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 o, then the SQLI
11f70 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 TE_BUSY error is
11f71 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 .** returned to
11f72 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 the caller of th
11f73 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 e pager API func
11f74 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
11f75 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
11f76 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 ite3PagerSetBusy
11f77 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 handler(. Pager
11f78 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
11f79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11f7a 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
11f7b 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 */. int (*xBusy
11f7c 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 Handler)(void *)
11f7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 , /* Poi
11f7e 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e nter to busy-han
11f7f 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f dler function */
11f80 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 . void *pBusyHa
11f81 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 20 ndlerArg
11f82 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
11f83 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 ent to pass to x
11f84 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 BusyHandler */.)
11f85 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 { . pPager->xB
11f86 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 usyHandler = xBu
11f87 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 syHandler;. pPa
11f88 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 ger->pBusyHandle
11f89 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 rArg = pBusyHand
11f8a 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lerArg;.}../*.**
11f8b 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74 69 Set the reiniti
11f8c 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73 20 alizer for this
11f8d 70 61 67 65 72 2e 20 49 66 20 6e 6f 74 20 4e 55 pager. If not NU
11f8e 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 69 61 LL, the reinitia
11f8f 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 6c 6c lizer.** is call
11f90 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 74 ed when the cont
11f91 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 69 6e ent of a page in
11f92 20 63 61 63 68 65 20 69 73 20 6d 6f 64 69 66 69 cache is modifi
11f93 65 64 20 28 72 65 73 74 6f 72 65 64 29 0a 2a 2a ed (restored).**
11f94 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 74 72 as part of a tr
11f95 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 73 61 76 ansaction or sav
11f96 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e epoint rollback.
11f97 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 69 The callback gi
11f98 76 65 73 20 0a 2a 2a 20 68 69 67 68 65 72 2d 6c ves .** higher-l
11f99 65 76 65 6c 20 63 6f 64 65 20 61 6e 20 6f 70 70 evel code an opp
11f9a 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73 74 ortunity to rest
11f9b 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73 65 ore the EXTRA se
11f9c 63 74 69 6f 6e 20 74 6f 20 0a 2a 2a 20 61 67 72 ction to .** agr
11f9d 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74 ee with the rest
11f9e 6f 72 65 64 20 70 61 67 65 20 64 61 74 61 2e 0a ored page data..
11f9f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11fa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
11fa1 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 50 gerSetReiniter(P
11fa2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76 6f ager *pPager, vo
11fa3 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62 id (*xReinit)(Db
11fa4 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67 65 Page*)){. pPage
11fa5 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78 r->xReiniter = x
11fa6 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Reinit;.}../*.**
11fa7 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 Change the page
11fa8 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 size used by th
11fa9 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 e Pager object.
11faa 54 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a The new page siz
11fab 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 e .** is passed
11fac 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a in *pPageSize..*
11fad 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
11fae 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f r is in the erro
11faf 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 r state when thi
11fb0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
11fb1 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61 lled, it.** is a
11fb2 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75 no-op. The valu
11fb3 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 e returned is th
11fb4 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 e error state er
11fb5 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a ror code (i.e. .
11fb6 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 ** one of SQLITE
11fb7 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 _IOERR, SQLITE_C
11fb8 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49 54 45 ORRUPT or SQLITE
11fb9 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 _FULL)..**.** Ot
11fba 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c 6c 20 herwise, if all
11fbb 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
11fbc 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a are true:.**.**
11fbd 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70 61 67 * the new pag
11fbe 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20 6f 66 e size (value of
11fbf 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69 73 20 *pPageSize) is
11fc0 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72 20 0a valid (a power .
11fc1 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20 62 65 ** of two be
11fc2 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 53 51 tween 512 and SQ
11fc3 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
11fc4 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c 20 ZE, inclusive),
11fc5 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 and.**.** * th
11fc6 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 ere are no outst
11fc7 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 anding page refe
11fc8 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a rences, and.**.*
11fc9 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 * * the databa
11fca 73 65 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74 se is either not
11fcb 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
11fcc 74 61 62 61 73 65 20 6f 72 20 69 74 20 69 73 0a tabase or it is.
11fcd 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 6d ** an in-mem
11fce 6f 72 79 20 64 61 74 61 62 61 73 65 20 74 68 61 ory database tha
11fcf 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 t currently cons
11fd0 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 67 ists of zero pag
11fd1 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 es..**.** then t
11fd2 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 he pager object
11fd3 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 page size is set
11fd4 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a to *pPageSize..
11fd5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
11fd6 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65 e size is change
11fd7 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e d, then this fun
11fd8 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74 ction uses sqlit
11fd9 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20 e3PagerMalloc()
11fda 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 .** to obtain a
11fdb 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70 new Pager.pTmpSp
11fdc 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 20 74 ace buffer. If t
11fdd 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 his allocation a
11fde 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c 73 ttempt .** fails
11fdf 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 , SQLITE_NOMEM i
11fe0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
11fe1 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 65 6d he page size rem
11fe2 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 ains unchanged.
11fe3 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72 .** In all other
11fe4 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f cases, SQLITE_O
11fe5 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a K is returned..*
11fe6 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
11fe7 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 61 size is not cha
11fe8 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62 65 63 nged, either bec
11fe9 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 ause one of the
11fea 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 6f enumerated.** co
11feb 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 69 nditions above i
11fec 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 20 s not true, the
11fed 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 72 72 pager was in err
11fee 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 or state when th
11fef 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 is.** function w
11ff0 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 65 as called, or be
11ff1 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 cause the memory
11ff2 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 allocation atte
11ff3 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 mpt failed, .**
11ff4 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 20 then *pPageSize
11ff5 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 6c is set to the ol
11ff6 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61 67 65 d, retained page
11ff7 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72 65 74 size before ret
11ff8 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 urning..*/.SQLIT
11ff9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11ffa 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
11ffb 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 esize(Pager *pPa
11ffc 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67 65 53 ger, u16 *pPageS
11ffd 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ize){. int rc =
11ffe 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11fff 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
12000 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31 36 TE_OK ){. u16
12001 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 61 pageSize = *pPa
12002 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73 65 geSize;. asse
12003 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30 20 rt( pageSize==0
12004 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35 31 || (pageSize>=51
12005 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 2 && pageSize<=S
12006 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
12007 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28 20 IZE) );. if(
12008 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 pageSize && page
12009 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61 Size!=pPager->pa
1200a 67 65 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20 geSize . &&
1200b 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d (pPager->memDb==
1200c 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 0 || pPager->dbS
1200d 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26 20 ize==0). &&
1200e 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
1200f 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
12010 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 29 Cache)==0 . )
12011 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 4e {. char *pN
12012 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c ew = (char *)sql
12013 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 ite3PageMalloc(p
12014 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
12015 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 if( !pNew ){.
12016 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
12017 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 _NOMEM;. }e
12018 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61 67 lse{. pag
12019 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
1201a 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ;. pPager
1201b 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 ->pageSize = pag
1201c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 73 eSize;. s
1201d 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 qlite3PageFree(p
1201e 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
1201f 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 );. pPage
12020 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 r->pTmpSpace = p
12021 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c New;. sql
12022 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 ite3PcacheSetPag
12023 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 eSize(pPager->pP
12024 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 Cache, pageSize)
12025 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
12026 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d *pPageSize =
12027 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 61 (u16)pPager->pa
12028 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 geSize;. }. re
12029 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1202a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
1202b 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f er to the "tempo
1202c 72 61 72 79 20 70 61 67 65 22 20 62 75 66 66 65 rary page" buffe
1202d 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c r held internall
1202e 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 y.** by the page
1202f 72 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 75 r. This is a bu
12030 66 66 65 72 20 74 68 61 74 20 69 73 20 62 69 67 ffer that is big
12031 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
12032 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f the.** entire co
12033 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 ntent of a datab
12034 61 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 ase page. This
12035 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20 69 buffer is used i
12036 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 nternally.** dur
12037 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 ing rollback and
12038 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 will be overwri
12039 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 tten whenever a
1203a 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 rollback.** occu
1203b 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d rs. But other m
1203c 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 odules are free
1203d 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 to use it too, a
1203e 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 s long as.** no
1203f 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 rollbacks are ha
12040 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 ppening..*/.SQLI
12041 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12042 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d *sqlite3PagerTem
12043 70 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 pSpace(Pager *pP
12044 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
12045 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
12046 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 e;.}../*.** Atte
12047 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d mpt to set the m
12048 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 aximum database
12049 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 page count if mx
1204a 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 Page is positive
1204b 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 . .** Make no ch
1204c 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 anges if mxPage
1204d 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 is zero or negat
1204e 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 ive. And never
1204f 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 reduce the.** ma
12050 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 ximum page count
12051 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 below the curre
12052 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 nt size of the d
12053 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 atabase..**.** R
12054 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 egardless of mxP
12055 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 age, return the
12056 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 current maximum
12057 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 page count..*/.S
12058 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
12059 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 t sqlite3PagerMa
1205a 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 xPageCount(Pager
1205b 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 *pPager, int mx
1205c 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 Page){. if( mxP
1205d 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 age>0 ){. pPa
1205e 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 ger->mxPgno = mx
1205f 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Page;. }. sqli
12060 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
12061 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 t(pPager, 0);.
12062 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d return pPager->m
12063 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 xPgno;.}../*.**
12064 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 The following se
12065 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 t of routines ar
12066 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c e used to disabl
12067 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a e the simulated.
12068 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 ** I/O error mec
12069 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 hanism. These r
1206a 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
1206b 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 to avoid simula
1206c 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e ted.** errors in
1206d 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 places where we
1206e 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
1206f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a ut errors..**.**
12070 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 Unless -DSQLITE
12071 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c _TEST=1 is used,
12072 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
12073 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a are all no-ops.*
12074 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e * and generate n
12075 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 o code..*/.#ifde
12076 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
12077 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 LITE_API extern
12078 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
12079 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 rror_pending;.SQ
1207a 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 LITE_API extern
1207b 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
1207c 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63 rror_hit;.static
1207d 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a int saved_cnt;.
1207e 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d void disable_sim
1207f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
12080 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f (void){. saved_
12081 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f cnt = sqlite3_io
12082 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a _error_pending;.
12083 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
12084 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b or_pending = -1;
12085 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 .}.void enable_s
12086 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12087 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 rs(void){. sqli
12088 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
12089 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74 ding = saved_cnt
1208a 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ;.}.#else.# defi
1208b 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c ne disable_simul
1208c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
1208d 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 .# define enable
1208e 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
1208f 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f rors().#endif../
12090 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69 *.** Read the fi
12091 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d rst N bytes from
12092 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
12093 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20 f the file into
12094 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70 memory.** that p
12095 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 Dest points to.
12096 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
12097 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f ger was opened o
12098 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 n a transient fi
12099 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22 le (zFilename=="
1209a 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64 "), or.** opened
1209b 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73 73 20 on a file less
1209c 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69 6e 20 than N bytes in
1209d 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70 75 74 size, the output
1209e 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a 65 buffer is.** ze
1209f 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f roed and SQLITE_
120a0 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 OK returned. The
120a1 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20 74 rationale for t
120a2 68 69 73 20 69 73 20 74 68 61 74 20 74 68 69 73 his is that this
120a3 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 .** function is
120a4 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 used to read da
120a5 74 61 62 61 73 65 20 68 65 61 64 65 72 73 2c 20 tabase headers,
120a6 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e 73 69 and a new transi
120a7 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73 ent or.** zero s
120a8 69 7a 65 64 20 64 61 74 61 62 61 73 65 20 68 61 ized database ha
120a9 73 20 61 20 68 65 61 64 65 72 20 74 68 61 6e 20 s a header than
120aa 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c consists entirel
120ab 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a y of zeroes..**.
120ac 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65 72 72 ** If any IO err
120ad 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20 53 51 or apart from SQ
120ae 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
120af 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75 6e 74 _READ is encount
120b0 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 ered,.** the err
120b1 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
120b2 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ned to the calle
120b3 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e r and the conten
120b4 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75 74 ts of the.** out
120b5 70 75 74 20 62 75 66 66 65 72 20 75 6e 64 65 66 put buffer undef
120b6 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ined..*/.SQLITE_
120b7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
120b8 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 te3PagerReadFile
120b9 68 65 61 64 65 72 28 50 61 67 65 72 20 2a 70 50 header(Pager *pP
120ba 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 ager, int N, uns
120bb 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 65 73 igned char *pDes
120bc 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
120bd 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 QLITE_OK;. mems
120be 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b et(pDest, 0, N);
120bf 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 . assert( isOpe
120c0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c n(pPager->fd) ||
120c1 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
120c2 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 e );. if( isOpe
120c3 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b n(pPager->fd) ){
120c4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 . IOTRACE(("D
120c5 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c BHDR %p 0 %d\n",
120c6 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 pPager, N)).
120c7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
120c8 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ead(pPager->fd,
120c9 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 pDest, N, 0);.
120ca 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
120cb 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
120cc 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 D ){. rc =
120cd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
120ce 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
120cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
120d0 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 n the total numb
120d1 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
120d2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
120d3 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 associated .**
120d4 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e 6f 72 with pPager. Nor
120d5 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 mally, this is c
120d6 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28 3c 64 alculated as (<d
120d7 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 b file size>/<pa
120d8 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f ge-size>)..** Ho
120d9 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 66 69 wever, if the fi
120da 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 31 20 le is between 1
120db 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 and <page-size>
120dc 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 bytes in size, t
120dd 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 20 hen .** this is
120de 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 20 70 considered a 1 p
120df 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 age file..**.**
120e0 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
120e1 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 in error state w
120e2 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
120e3 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 n is called, the
120e4 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 n the.** error s
120e5 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 tate error code
120e6 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
120e7 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75 6e 63 *pnPage left unc
120e8 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 hanged. Or,.** i
120e9 66 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 f the file syste
120ea 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75 65 72 m has to be quer
120eb 69 65 64 20 66 6f 72 20 74 68 65 20 73 69 7a 65 ied for the size
120ec 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 of the file and
120ed 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20 61 74 .** the query at
120ee 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20 61 6e tempt returns an
120ef 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65 20 49 IO error, the I
120f0 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
120f1 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 returned.** and
120f2 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66 74 20 *pnPage is left
120f3 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
120f4 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 Otherwise, if e
120f5 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
120f6 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 53 51 cessful, then SQ
120f7 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
120f8 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 ned.** and *pnPa
120f9 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ge is set to the
120fa 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
120fb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
120fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
120fd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
120fe 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 agerPagecount(Pa
120ff 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
12100 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50 67 6e *pnPage){. Pgn
12101 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 o nPage;
12102 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
12103 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70 to return via *p
12104 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 nPage */.. /* I
12105 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 f the pager is a
12106 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 lready in the er
12107 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 ror state, retur
12108 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 n the error code
12109 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 . */. if( pPage
1210a 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 r->errCode ){.
1210b 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
1210c 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 >errCode;. }..
1210d 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 /* Determine th
1210e 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
1210f 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 53 s in the file. S
12110 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e 50 61 tore this in nPa
12111 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 ge. */. if( pPa
12112 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12113 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 ){. nPage =
12114 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a pPager->dbSize;.
12115 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
12116 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12117 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 65 /* Error re
12118 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 turned by OsFile
12119 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 36 Size() */. i6
1211a 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 4 n = 0;
1211b 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 69 /* File si
1211c 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65 74 75 ze in bytes retu
1211d 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 rned by OsFileSi
1211e 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73 ze() */.. ass
1211f 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
12120 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
12121 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
12122 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
12123 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28 30 20 ager->fd) && (0
12124 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 != (rc = sqlite3
12125 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 OsFileSize(pPage
12126 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b 0a r->fd, &n))) ){.
12127 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f pager_erro
12128 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 r(pPager, rc);.
12129 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1212a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e }. if( n>
1212b 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 0 && n<pPager->p
1212c 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ageSize ){.
1212d 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 nPage = 1;.
1212e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
1212f 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f 20 ge = (Pgno)(n /
12130 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12131 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
12132 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
12133 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a PAGER_UNLOCK ){.
12134 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
12135 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
12136 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
12137 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a leSize = nPage;.
12138 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
12139 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20 SizeValid = 1;.
1213a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1213b 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 f the current nu
1213c 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1213d 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 72 65 the file is gre
1213e 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 ater than the .
1213f 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 6d ** configured m
12140 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d aximum pager num
12141 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 74 68 ber, increase th
12142 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20 e allowed limit
12143 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 so. ** that the
12144 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72 65 61 file can be rea
12145 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 d.. */. if( nP
12146 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 age>pPager->mxPg
12147 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 no ){. pPager
12148 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f ->mxPgno = (Pgno
12149 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f )nPage;. }.. /
1214a 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 * Set the output
1214b 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 72 65 variable and re
1214c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a turn SQLITE_OK *
1214d 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20 29 /. if( pnPage )
1214e 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20 {. *pnPage =
1214f 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65 74 nPage;. }. ret
12150 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
12151 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 .../*.** Try to
12152 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 obtain a lock of
12153 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f type locktype o
12154 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
12155 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d ile. If.** a sim
12156 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65 72 20 ilar or greater
12157 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 lock is already
12158 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 held, this funct
12159 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a ion is a no-op.*
1215a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53 51 4c * (returning SQL
1215b 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 ITE_OK immediate
1215c 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ly)..**.** Other
1215d 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20 74 6f wise, attempt to
1215e 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b obtain the lock
1215f 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 using sqlite3Os
12160 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a Lock(). Invoke .
12161 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c ** the busy call
12162 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b back if the lock
12163 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f is currently no
12164 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 t available. Rep
12165 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 eat .** until th
12166 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 e busy callback
12167 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 returns false or
12168 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 65 6d until the attem
12169 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e pt to .** obtain
1216a 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 the lock succee
1216b 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ds..**.** Return
1216c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
1216d 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 ccess and an err
1216e 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 or code if we ca
1216f 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 nnot obtain.** t
12170 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 he lock. If the
12171 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
12172 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 successfully, s
12173 65 74 20 74 68 65 20 50 61 67 65 72 2e 73 74 61 et the Pager.sta
12174 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 te .** variable
12175 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f to locktype befo
12176 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f re returning..*/
12177 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
12178 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 r_wait_on_lock(P
12179 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
1217a 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
1217b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1217c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1217d 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1217e 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 de */.. /* The
1217f 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d OS lock values m
12180 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 ust be the same
12181 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63 as the Pager loc
12182 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 k values */. as
12183 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41 52 sert( PAGER_SHAR
12184 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ED==SHARED_LOCK
12185 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
12186 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53 ER_RESERVED==RES
12187 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 ERVED_LOCK );.
12188 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45 58 assert( PAGER_EX
12189 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 CLUSIVE==EXCLUSI
1218a 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a VE_LOCK );.. /*
1218b 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 If the file is
1218c 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b currently unlock
1218d 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 ed then the size
1218e 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e must be unknown
1218f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
12190 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
12191 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61 ER_SHARED || pPa
12192 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12193 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 ==0 );.. /* Che
12194 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 ck that this is
12195 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28 either a no-op (
12196 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
12197 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20 ested lock is .
12198 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 ** already held
12199 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 , or one of the
1219a 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 61 transistions tha
1219b 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c t the busy-handl
1219c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 er. ** may be i
1219d 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 61 nvoked during, a
1219e 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
1219f 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20 comment above.
121a0 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 ** sqlite3PagerS
121a1 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 2e etBusyhandler().
121a2 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
121a3 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d (pPager->state>=
121a4 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 20 locktype).
121a5 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 || (pPager->sta
121a6 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
121a7 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 && locktype==PA
121a8 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 GER_SHARED).
121a9 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 || (pPager->s
121aa 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
121ab 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 65 RVED && locktype
121ac 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ==PAGER_EXCLUSIV
121ad 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 70 E). );.. if( p
121ae 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f Pager->state>=lo
121af 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63 cktype ){. rc
121b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
121b1 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a }else{. do {.
121b2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
121b3 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
121b4 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a >fd, locktype);.
121b5 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d }while( rc==
121b6 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 SQLITE_BUSY && p
121b7 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
121b8 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73 ler(pPager->pBus
121b9 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a yHandlerArg) );.
121ba 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
121bb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
121bc 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 28 Pager->state = (
121bd 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 u8)locktype;.
121be 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43 IOTRACE(("LOC
121bf 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 K %p %d\n", pPag
121c0 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20 er, locktype)).
121c1 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
121c2 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
121c3 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e 2d 6d runcate the in-m
121c4 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 66 emory database f
121c5 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 61 ile image to nPa
121c6 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 20 0a ge pages. This .
121c7 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 ** function does
121c8 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f not actually mo
121c9 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 dify the databas
121ca 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 e file on disk.
121cb 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 73 It .** just sets
121cc 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 the internal st
121cd 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 ate of the pager
121ce 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 74 20 object so that
121cf 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 the .** truncati
121d0 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 20 on will be done
121d1 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 when the current
121d2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
121d3 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51 committed..*/.SQ
121d4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
121d5 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 d sqlite3PagerTr
121d6 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 uncateImage(Page
121d7 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 r *pPager, Pgno
121d8 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 nPage){. assert
121d9 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
121da 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 72 Valid );. asser
121db 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a t( pPager->dbSiz
121dc 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 73 e>=nPage );. as
121dd 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
121de 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 ate>=PAGER_RESER
121df 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 72 2d VED );. pPager-
121e0 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b >dbSize = nPage;
121e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f .}../*.** Shutdo
121e2 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 wn the page cach
121e3 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d e. Free all mem
121e4 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c ory and close al
121e5 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 l files..**.** I
121e6 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
121e7 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 was in progress
121e8 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
121e9 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 e is called, tha
121ea 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e t.** transaction
121eb 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e is rolled back.
121ec 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e All outstandin
121ed 67 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61 g pages are inva
121ee 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 lidated.** and t
121ef 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 heir memory is f
121f0 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65 6d reed. Any attem
121f1 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 67 65 pt to use a page
121f2 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
121f3 69 74 68 20 74 68 69 73 20 70 61 67 65 20 63 61 ith this page ca
121f4 63 68 65 20 61 66 74 65 72 20 74 68 69 73 20 66 che after this f
121f5 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
121f6 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 will likely.** r
121f7 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 esult in a cored
121f8 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ump..**.** This
121f9 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 function always
121fa 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 20 74 succeeds. If a t
121fb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
121fc 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74 0a tive an attempt.
121fd 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72 6f ** is made to ro
121fe 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20 61 ll it back. If a
121ff 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 n error occurs d
12200 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 uring the rollba
12201 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 ck .** a hot jou
12202 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 rnal may be left
12203 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 in the filesyst
12204 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 em but no error
12205 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 is returned.** t
12206 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f o the caller..*/
12207 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12208 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
12209 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 Close(Pager *pPa
1220a 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f ger){. disable_
1220b 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
1220c 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 ors();. sqlite3
1220d 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
1220e 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 c();. pPager->e
1220f 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 rrCode = 0;. pP
12210 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
12211 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 ode = 0;. pager
12212 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
12213 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 if( MEMDB ){.
12214 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 pager_unlock(
12215 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 pPager);. }else
12216 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61 67 {. /* Set Pag
12217 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f er.journalHdr to
12218 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e 65 -1 for the bene
12219 66 69 74 20 6f 66 20 74 68 65 20 70 61 67 65 72 fit of the pager
1221a 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20 20 _playback() .
1221b 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20 6d ** call which m
1221c 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 ay be made from
1221d 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f within pagerUnlo
1221e 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e ckAndRollback().
1221f 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 If it. ** is
12220 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 not -1, then th
12221 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 e unsynced porti
12222 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f on of an open jo
12223 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 urnal file may.
12224 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 ** be played
12225 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 back into the da
12226 74 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f 77 tabase. If a pow
12227 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 er failure occur
12228 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 s while. ** t
12229 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 his is happening
1222a 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d , the database m
1222b 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 ay become corrup
1222c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 t.. */. pP
1222d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
1222e 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65 72 = -1;. pager
1222f 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 UnlockAndRollbac
12230 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 k(pPager);. }.
12231 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
12232 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 nMalloc();. ena
12233 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
12234 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 _errors();. PAG
12235 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 ERTRACE(("CLOSE
12236 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
12237 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 Pager)));. IOTR
12238 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e ACE(("CLOSE %p\n
12239 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71 ", pPager)). sq
1223a 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
1223b 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 ger->fd);. sqli
1223c 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67 te3PageFree(pPag
1223d 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a er->pTmpSpace);.
1223e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
1223f 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 lose(pPager->pPC
12240 61 63 68 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 ache);.. assert
12241 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76 65 ( !pPager->aSave
12242 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65 72 point && !pPager
12243 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a ->pInJournal );.
12244 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
12245 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
12246 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 & !isOpen(pPager
12247 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 71 ->sjfd) );.. sq
12248 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 lite3_free(pPage
12249 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c r);. return SQL
1224a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 ITE_OK;.}..#if !
1224b 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 defined(NDEBUG)
1224c 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
1224d 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 E_TEST)./*.** Re
1224e 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 turn the page nu
1224f 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70 50 mber for page pP
12250 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
12251 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 VATE Pgno sqlite
12252 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
12253 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
12254 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 6e return pPg->pgn
12255 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a o;.}.#endif../*.
12256 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 ** Increment the
12257 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
12258 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a for page pPg..*
12259 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1225a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
1225b 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 50 erRef(DbPage *pP
1225c 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 g){. sqlite3Pca
1225d 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a 0a cheRef(pPg);.}..
1225e 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a /*.** Sync the j
1225f 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 72 ournal. In other
12260 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 72 words, make sur
12261 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73 20 e all the pages
12262 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 that have.** bee
12263 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
12264 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 63 journal have ac
12265 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20 74 tually reached t
12266 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74 68 he surface of th
12267 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 61 e.** disk and ca
12268 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 6e n be restored in
12269 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 the event of a
1226a 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
1226b 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 back..**.** If t
1226c 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e he Pager.needSyn
1226d 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 65 c flag is not se
1226e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e t, then this fun
1226f 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f ction is a.** no
12270 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 -op. Otherwise,
12271 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 the actions requ
12272 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 74 ired depend on t
12273 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a he journal-mode.
12274 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69 63 ** and the devic
12275 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
12276 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69 6c s of the the fil
12277 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f 6c e-system, as fol
12278 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 lows:.**.** *
12279 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 If the journal f
1227a 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d ile is an in-mem
1227b 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ory journal file
1227c 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64 , no action need
1227d 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65 6e .** be taken
1227e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 ..**.** * Othe
1227f 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64 65 rwise, if the de
12280 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 vice does not su
12281 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f 41 pport the SAFE_A
12282 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c 0a PPEND property,.
12283 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 ** then the
12284 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68 nRec field of th
12285 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
12286 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 written journal
12287 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 73 header.** is
12288 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e 74 updated to cont
12289 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ain the number o
1228a 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 f journal record
1228b 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 s that have.**
1228c 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 been written
1228d 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 66 following it. If
1228e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f 70 the pager is op
1228f 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c 2d erating in full-
12290 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 sync.** mode
12291 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
12292 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 al file is synce
12293 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 69 d before this fi
12294 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e 0a eld is updated..
12295 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 **.** * If the
12296 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 device does not
12297 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45 51 support the SEQ
12298 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 UENTIAL property
12299 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 6a , then .** j
1229a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 ournal file is s
1229b 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c ynced..**.** Or,
1229c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 3a in pseudo-code:
1229d 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f 54 .**.** if( NOT
1229e 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 <in-memory jour
1229f 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 69 nal> ){.** i
122a0 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 45 f( NOT SAFE_APPE
122a1 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 69 ND ){.** i
122a2 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f f( <full-sync mo
122a3 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 de> ) xSync(<jou
122a4 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 rnal file>);.**
122a5 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e 52 <update nR
122a6 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 ec field>.**
122a7 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e } .** if( N
122a8 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29 20 OT SEQUENTIAL )
122a9 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 xSync(<journal f
122aa 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a ile>);.** }.**
122ab 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e 65 .** The Pager.ne
122ac 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e edSync flag is n
122ad 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72 20 ever be set for
122ae 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c temporary files,
122af 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 or any.** file
122b0 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f 2d operating in no-
122b1 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 72 sync mode (Pager
122b2 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 6e .noSync set to n
122b3 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 on-zero)..**.**
122b4 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 If successful, t
122b5 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 his routine clea
122b6 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 rs the PGHDR_NEE
122b7 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 65 D_SYNC flag of e
122b8 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 75 very .** page cu
122b9 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 rrently held in
122ba 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 65 memory before re
122bb 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f turning SQLITE_O
122bc 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 K. If an IO.** e
122bd 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 rror is encounte
122be 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49 4f red, then the IO
122bf 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
122c0 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
122c1 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 aller..*/.static
122c2 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c int syncJournal
122c3 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
122c4 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e . if( pPager->n
122c5 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 61 eedSync ){. a
122c6 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
122c7 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 tempFile );.
122c8 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
122c9 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
122ca 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
122cb 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 Y ){. int r
122cc 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
122cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
122ce 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
122cf 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e /. const in
122d0 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f t iDc = sqlite3O
122d1 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
122d2 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
122d3 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 d);. assert
122d4 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
122d5 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 >jfd) );..
122d6 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 if( 0==(iDc&SQLI
122d7 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
122d8 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20 PEND) ){.
122d9 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 52 /* Variable iNR
122da 65 63 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 ecOffset is set
122db 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e to the offset in
122dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
122dd 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 e. ** of
122de 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 6f the nRec field o
122df 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
122e0 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 tly written jour
122e1 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20 nal header..
122e2 20 20 20 20 2a 2a 20 54 68 69 73 20 66 69 65 6c ** This fiel
122e3 64 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 d will be update
122e4 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 d following the
122e5 78 53 79 6e 63 28 29 20 6f 70 65 72 61 74 69 6f xSync() operatio
122e6 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 n. ** on
122e7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
122e8 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 . */. i64
122e9 20 69 4e 52 65 63 4f 66 66 73 65 74 20 3d 20 70 iNRecOffset = p
122ea 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
122eb 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 r + sizeof(aJour
122ec 6e 61 6c 4d 61 67 69 63 29 3b 0a 0a 20 20 20 20 nalMagic);..
122ed 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 /* This bloc
122ee 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20 k deals with an
122ef 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e obscure problem.
122f0 20 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e If the last con
122f1 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 nection.
122f2 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f ** that wrote to
122f3 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 77 this database w
122f4 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 as operating in
122f5 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e persistent-journ
122f6 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f al. ** mo
122f7 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 de, then the jou
122f8 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 74 rnal file may at
122f9 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74 75 this point actu
122fa 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0a 20 ally be larger.
122fb 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 ** than P
122fc 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ager.journalOff
122fd 62 79 74 65 73 2e 20 49 66 20 74 68 65 20 6e 65 bytes. If the ne
122fe 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68 65 20 xt thing in the
122ff 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 journal.
12300 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e 73 20 ** file happens
12301 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c 2d to be a journal-
12302 68 65 61 64 65 72 20 28 77 72 69 74 74 65 6e 20 header (written
12303 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 as part of the.
12304 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f ** previo
12305 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 us connections t
12306 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 ransaction), and
12307 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 a crash or powe
12308 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 20 20 r-failure .
12309 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 ** occurs aft
1230a 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 er nRec is updat
1230b 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 ed but before th
1230c 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 is connection wr
1230d 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a ites . **
1230e 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 74 anything else t
1230f 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
12310 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 le (or commits/r
12311 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 0a 20 olls back its .
12312 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 ** transa
12313 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c ction), then SQL
12314 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 ite may become c
12315 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 6f 69 onfused when doi
12316 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 ng the .
12317 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 ** hot-journal r
12318 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e ollback followin
12319 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 20 6d g recovery. It m
1231a 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c ay roll back all
1231b 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 . ** of t
1231c 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 his connections
1231d 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 data, then proce
1231e 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 ed to rolling ba
1231f 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 ck the old,.
12320 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 ** out-of-da
12321 74 65 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c te data that fol
12322 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 61 73 lows it. Databas
12323 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 e corruption..
12324 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
12325 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 ** To work arou
12326 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 nd this, if the
12327 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 journal file doe
12328 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 s appear to cont
12329 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ain. ** a
1232a 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f valid header fo
1232b 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f llowing Pager.jo
1232c 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 urnalOff, then w
1232d 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 20 20 rite a 0x00.
1232e 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 ** byte to t
1232f 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 20 74 he start of it t
12330 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f o prevent it fro
12331 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a m being recogniz
12332 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 ed.. **.
12333 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61 62 ** Variab
12334 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 le iNextHdrOffse
12335 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
12336 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68 20 offset at which
12337 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 this. **
12338 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 64 problematic head
12339 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 er will occur, i
1233a 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 4d 61 f it exists. aMa
1233b 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 20 20 gic is used .
1233c 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 65 6d ** as a tem
1233d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74 6f porary buffer to
1233e 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 69 72 inspect the fir
1233f 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 st couple of byt
12340 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a es of. **
12341 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a the potential j
12342 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 ournal header..
12343 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
12344 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 4f 66 i64 iNextHdrOf
12345 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 fset = journalHd
12346 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
12347 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 67 . u8 aMag
12348 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 20 72 ic[8];. r
12349 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
1234a 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 d(pPager->jfd, a
1234b 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 48 Magic, 8, iNextH
1234c 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 drOffset);.
1234d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1234e 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 6d E_OK && 0==memcm
1234f 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e p(aMagic, aJourn
12350 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a 20 alMagic, 8) ){.
12351 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 static
12352 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 74 const u8 zerobyt
12353 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 e = 0;.
12354 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
12355 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
12356 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c 20 , &zerobyte, 1,
12357 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b iNextHdrOffset);
12358 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
12359 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1235a 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 E_OK && rc!=SQLI
1235b 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
1235c 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EAD ){.
1235d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1235e 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f }.. /
1235f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 * Write the nRec
12360 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 value into the
12361 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61 journal file hea
12362 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 20 der. If in.
12363 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 ** full-synch
12364 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 6e ronous mode, syn
12365 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 c the journal fi
12366 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72 65 rst. This ensure
12367 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a s that. *
12368 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20 72 * all data has r
12369 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64 69 eally hit the di
1236a 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20 69 sk before nRec i
1236b 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 72 s updated to mar
1236c 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 k. ** it
1236d 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 as a candidate f
1236e 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 or rollback..
1236f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
12370 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 ** This is not r
12371 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20 70 equired if the p
12372 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20 ersistent media
12373 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20 20 supports the.
12374 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 50 ** SAFE_APP
12375 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42 65 END property. Be
12376 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63 61 cause in this ca
12377 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 se it is not pos
12378 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20 2a sible . *
12379 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64 61 * for garbage da
1237a 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65 ta to be appende
1237b 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 74 d to the file, t
1237c 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 20 he nRec field.
1237d 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 75 ** is popu
1237e 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46 46 lated with 0xFFF
1237f 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20 6a FFFFF when the j
12380 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 ournal header is
12381 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20 written.
12382 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e 65 ** and never ne
12383 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 eds to be update
12384 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 d.. */.
12385 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 if( pPager
12386 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d ->fullSync && 0=
12387 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
12388 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 AP_SEQUENTIAL) )
12389 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47 45 {. PAGE
1238a 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f RTRACE(("SYNC jo
1238b 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 urnal of %d\n",
1238c 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 PAGERID(pPager))
1238d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f 54 );. IOT
1238e 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c RACE(("JSYNC %p\
1238f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 n", pPager)).
12390 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
12391 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 te3OsSync(pPager
12392 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 ->jfd, pPager->s
12393 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 ync_flags);.
12394 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12395 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
12396 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
12397 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
12398 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 "JHDR %p %lld %d
12399 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 69 4e 52 \n", pPager, iNR
1239a 65 63 4f 66 66 73 65 74 2c 20 34 29 29 3b 0a 20 ecOffset, 4));.
1239b 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 rc = writ
1239c 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
1239d 6a 66 64 2c 20 69 4e 52 65 63 4f 66 66 73 65 74 jfd, iNRecOffset
1239e 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b , pPager->nRec);
1239f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
123a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
123a1 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
123a2 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
123a3 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
123a4 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
123a5 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
123a6 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
123a7 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
123a8 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
123a9 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
123aa 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
123ab 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 er)). rc
123ac 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
123ad 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
123ae 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c ger->sync_flags|
123af 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 . (pPa
123b0 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d ger->sync_flags=
123b1 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c =SQLITE_SYNC_FUL
123b2 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 L?SQLITE_SYNC_DA
123b3 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 TAONLY:0).
123b4 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
123b5 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
123b6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
123b7 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
123b8 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
123b9 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63 le was just succ
123ba 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e essfully synced.
123bb 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53 Set Pager.needS
123bc 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a ync . ** to z
123bd 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68 ero and clear th
123be 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
123bf 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 C flag on all pa
123c0 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 gess.. */.
123c1 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
123c2 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
123c3 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
123c4 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 d = 1;. sqlit
123c5 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e e3PcacheClearSyn
123c6 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 cFlags(pPager->p
123c7 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 PCache);. }..
123c8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
123c9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 ;.}../*.** The a
123ca 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66 rgument is the f
123cb 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 irst in a linked
123cc 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
123cd 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a ages connected.*
123ce 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70 * by the PgHdr.p
123cf 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 Dirty pointer. T
123d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 his function wri
123d1 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 tes each one of
123d2 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 the.** in-memory
123d3 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 pages in the li
123d4 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 st to the databa
123d5 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67 se file. The arg
123d6 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 ument may.** be
123d7 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 NULL, representi
123d8 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 ng an empty list
123d9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
123da 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a his function is.
123db 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a ** a no-op..**.*
123dc 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74 * The pager must
123dd 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61 hold at least a
123de 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 RESERVED lock w
123df 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
123e0 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 n.** is called.
123e1 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61 Before writing a
123e2 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 nything to the d
123e3 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
123e4 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 is lock.** is up
123e5 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 graded to an EXC
123e6 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
123e7 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 the lock cannot
123e8 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 be obtained,.**
123e9 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
123ea 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
123eb 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
123ec 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
123ed 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 ile..** .** If t
123ee 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65 he pager is a te
123ef 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e mp-file pager an
123f0 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c d the actual fil
123f1 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a e-system file.**
123f2 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e is not yet open
123f3 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 , it is created
123f4 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 and opened befor
123f5 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a e any data is .*
123f6 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a * written out..*
123f7 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f *.** Once the lo
123f8 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72 ck has been upgr
123f9 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 aded and, if nec
123fa 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65 essary, the file
123fb 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 opened,.** the
123fc 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 pages are writte
123fd 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 n out to the dat
123fe 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 abase file in li
123ff 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e st order. Writin
12400 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73 g.** a page is s
12401 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65 kipped if it mee
12402 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 ts either of the
12403 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 following crite
12404 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 ria:.**.** * T
12405 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
12406 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50 s greater than P
12407 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a ager.dbSize, or.
12408 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52 ** * The PGHDR
12409 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 _DONT_WRITE flag
1240a 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70 is set on the p
1240b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 age..**.** If wr
1240c 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65 iting out a page
1240d 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 causes the data
1240e 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f base file to gro
1240f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 w, Pager.dbFileS
12410 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 ize.** is update
12411 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 d accordingly. I
12412 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74 f page 1 is writ
12413 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 ten out, then th
12414 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a e value cached.*
12415 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c * in Pager.dbFil
12416 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74 eVers[] is updat
12417 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 ed to match the
12418 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64 new value stored
12419 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 in.** the datab
1241a 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
1241b 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 If everything is
1241c 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c successful, SQL
1241d 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1241e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 ed. If an IO err
1241f 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 or .** occurs, a
12420 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
12421 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c is returned. Or,
12422 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56 if the EXCLUSIV
12423 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a E lock cannot.**
12424 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 be obtained, SQ
12425 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
12426 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
12427 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 int pager_write
12428 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 _pagelist(PgHdr
12429 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 *pList){. Pager
1242a 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 *pPager;
1242b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1242c 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
1242d 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
1242e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1242f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12430 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 urn code */.. i
12431 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
12432 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12433 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74 pPager = pList
12434 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 ->pPager;.. /*
12435 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 At this point th
12436 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 ere may be eithe
12437 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 r a RESERVED or
12438 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
12439 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 n the. ** datab
1243a 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 ase file. If the
1243b 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e re is already an
1243c 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c EXCLUSIVE lock,
1243d 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
1243e 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f ** call is a no
1243f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d -op.. **. ** M
12440 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 66 oving the lock f
12441 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 6f 20 rom RESERVED to
12442 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 61 6c EXCLUSIVE actual
12443 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 6e ly involves goin
12444 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 g. ** through a
12445 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 n intermediate s
12446 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 20 tate PENDING.
12447 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 70 A PENDING lock p
12448 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a 2a revents new. **
12449 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 61 74 readers from at
1244a 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 20 64 taching to the d
1244b 61 74 61 62 61 73 65 20 62 75 74 20 69 73 20 75 atabase but is u
1244c 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f 72 20 nsufficient for
1244d 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 65 us to. ** write
1244e 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 20 61 . The idea of a
1244f 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 PENDING lock is
12450 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 20 to prevent new
12451 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 2a readers from. *
12452 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 6c * coming in whil
12453 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 65 78 e we wait for ex
12454 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 20 74 isting readers t
12455 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 20 o clear.. **.
12456 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 61 67 ** While the pag
12457 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 45 53 er is in the RES
12458 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 ERVED state, the
12459 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 original databa
1245a 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 se file. ** is
1245b 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 65 unchanged and we
1245c 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 69 can rollback wi
1245d 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
1245e 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 20 2a playback the. *
1245f 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 74 * journal into t
12460 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
12461 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 65 base file. Once
12462 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 we transition t
12463 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 o. ** EXCLUSIVE
12464 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 64 , it means the d
12465 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
12466 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 61 6e been changed an
12467 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a 20 d any rollback.
12468 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 65 ** will require
12469 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 a journal playb
1246a 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ack.. */. asse
1246b 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
1246c 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e>=PAGER_RESERVE
1246d 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 D );. rc = page
1246e 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
1246f 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 Pager, EXCLUSIVE
12470 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 66 _LOCK);.. /* If
12471 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 74 the file is a t
12472 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74 emp-file has not
12473 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 yet been opened
12474 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49 , open it now. I
12475 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f t. ** is not po
12476 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20 74 6f ssible for rc to
12477 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 be other than S
12478 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 69 73 QLITE_OK if this
12479 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20 branch. ** is
1247a 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65 72 5f taken, as pager_
1247b 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69 wait_on_lock() i
1247c 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65 s a no-op for te
1247d 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 mp-files.. */.
1247e 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 if( !isOpen(pPa
1247f 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 ger->fd) ){.
12480 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12481 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63 3d 3d tempFile && rc==
12482 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
12483 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 rc = pagerOpent
12484 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 emp(pPager, pPag
12485 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e er->fd, pPager->
12486 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a vfsFlags);. }..
12487 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
12488 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 ITE_OK && pList
12489 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f ){. Pgno pgno
1248a 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a = pList->pgno;.
1248b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
1248c 20 61 72 65 20 64 69 72 74 79 20 70 61 67 65 73 are dirty pages
1248d 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 in the page cac
1248e 68 65 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d he with page num
1248f 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 20 20 bers greater.
12490 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 64 ** than Pager.d
12491 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e bSize, this mean
12492 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 s sqlite3PagerTr
12493 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 77 61 uncateImage() wa
12494 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 s called to.
12495 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 ** make the file
12496 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d smaller (presum
12497 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 ably by auto-vac
12498 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f uum code). Do no
12499 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 t write. ** a
1249a 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74 6f ny such pages to
1249b 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a the file.. *
1249c 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 *. ** Also, d
1249d 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20 o not write out
1249e 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 68 61 any page that ha
1249f 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 s the PGHDR_DONT
124a0 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 20 20 _WRITE flag.
124a1 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20 73 ** set (set by s
124a2 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 qlite3PagerDontW
124a3 72 69 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a rite()).. */.
124a4 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 if( pgno<=pP
124a5 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 ager->dbSize &&
124a6 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 0==(pList->flags
124a7 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 &PGHDR_DONT_WRIT
124a8 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 E) ){. i64
124a9 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 offset = (pgno-1
124aa 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 )*(i64)pPager->p
124ab 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 ageSize;
124ac 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 72 /* Offset to wr
124ad 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 ite */. cha
124ae 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 43 r *pData = CODEC
124af 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 2(pPager, pList-
124b0 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 29 >pData, pgno, 6)
124b1 3b 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 69 ; /* Data to wri
124b2 74 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 te */.. /*
124b3 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 Write out the pa
124b4 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 ge data. */.
124b5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
124b6 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
124b7 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d , pData, pPager-
124b8 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 >pageSize, offse
124b9 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 t);.. /* If
124ba 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74 page 1 was just
124bb 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
124bc 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
124bd 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 s to match.
124be 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f ** the value no
124bf 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 w stored in the
124c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
124c1 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a f writing this .
124c2 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61 ** page ca
124c3 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73 used the databas
124c4 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
124c5 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a update dbFileSiz
124c6 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 e. . */.
124c7 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 if( pgno==1 )
124c8 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
124c9 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 (&pPager->dbFile
124ca 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d Vers, &pData[24]
124cb 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d , sizeof(pPager-
124cc 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
124cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
124ce 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 pgno>pPager->db
124cf 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 FileSize ){.
124d0 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
124d1 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
124d2 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
124d3 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b Update any back
124d4 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 up objects copyi
124d5 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ng the contents
124d6 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a of this pager. *
124d7 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 /. sqlite3B
124d8 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 ackupUpdate(pPag
124d9 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e er->pBackup, pgn
124da 6f 2c 20 28 75 38 20 2a 29 70 44 61 74 61 29 3b o, (u8 *)pData);
124db 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 .. PAGERTRA
124dc 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 CE(("STORE %d pa
124dd 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 ge %d hash(%08x)
124de 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
124df 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
124e0 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 pPager), pgno, p
124e1 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c ager_pagehash(pL
124e2 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f ist)));. IO
124e3 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 TRACE(("PGOUT %p
124e4 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
124e5 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 pgno));. PA
124e6 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 GER_INCR(sqlite3
124e7 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 _pager_writedb_c
124e8 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47 ount);. PAG
124e9 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e ER_INCR(pPager->
124ea 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c nWrite);. }el
124eb 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 se{. PAGERT
124ec 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 RACE(("NOSTORE %
124ed 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 d page %d\n", PA
124ee 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
124ef 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66 gno));. }.#if
124f0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
124f1 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74 _PAGES. pList
124f2 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
124f3 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 er_pagehash(pLis
124f4 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 t);.#endif. p
124f5 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 List = pList->pD
124f6 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 irty;. }.. ret
124f7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
124f8 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 Append a record
124f9 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
124fa 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 state of page pP
124fb 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 g to the sub-jou
124fc 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 rnal. .** It is
124fd 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 the callers resp
124fe 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 onsibility to us
124ff 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 e subjRequiresPa
12500 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a ge() to check .*
12501 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61 * that it is rea
12502 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66 lly required bef
12503 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 ore calling this
12504 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
12505 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
12506 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 set the bit corr
12507 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 esponding to pPg
12508 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 ->pgno in the bi
12509 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c tvecs.** for all
1250a 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
1250b 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1250c 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 g..**.** This fu
1250d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 nction returns S
1250e 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 QLITE_OK if ever
1250f 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 ything is succes
12510 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 sful, an IO.** e
12511 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 rror code if the
12512 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 attempt to writ
12513 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 e to the sub-jou
12514 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a rnal fails, or .
12515 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ** SQLITE_NOMEM
12516 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c if a malloc fail
12517 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 s while setting
12518 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 a bit in a savep
12519 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a oint.** bitvec..
1251a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 */.static int su
1251b 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 bjournalPage(PgH
1251c 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 dr *pPg){. int
1251d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1251e 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
1251f 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 = pPg->pPager;.
12520 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
12521 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
12522 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 void *pData = p
12523 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69 Pg->pData;. i
12524 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 64 offset = pPag
12525 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70 er->nSubRec*(4+p
12526 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
12527 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 ;. char *pDat
12528 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67 a2 = CODEC2(pPag
12529 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e er, pData, pPg->
1252a 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 0a 20 20 20 pgno, 7);. .
1252b 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 PAGERTRACE(("ST
1252c 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 MT-JOURNAL %d pa
1252d 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 ge %d\n", PAGERI
1252e 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e D(pPager), pPg->
1252f 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 pgno));. . a
12530 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 ssert( pageInJou
12531 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 rnal(pPg) || pPg
12532 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 ->pgno>pPager->d
12533 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 bOrigSize );.
12534 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 rc = write32bit
12535 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 s(pPager->sjfd,
12536 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e offset, pPg->pgn
12537 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d o);. if( rc==
12538 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12539 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1253a 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 sWrite(pPager->s
1253b 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 jfd, pData2, pPa
1253c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f ger->pageSize, o
1253d 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a ffset+4);. }.
1253e 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
1253f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
12540 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b Pager->nSubRec++
12541 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
12542 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
12543 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 >0 );. rc = a
12544 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
12545 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 vecs(pPager, pPg
12546 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 74 65 73 ->pgno);. tes
12547 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
12548 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 72 65 E_OK );. }. re
12549 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
1254a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1254b 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
1254c 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 e pcache layer w
1254d 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 hen it has reach
1254e 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 ed some.** soft
1254f 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 memory limit. Th
12550 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
12551 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
12552 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a a Pager object.
12553 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f ** (cast as a vo
12554 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 id*). The pager
12555 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 is always 'purge
12556 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e able' (not an in
12557 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 -memory.** datab
12558 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 ase). The second
12559 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 argument is a r
1255a 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 eference to a pa
1255b 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 ge that is .** c
1255c 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 urrently dirty b
1255d 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 ut has no outsta
1255e 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
1255f 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 . The page.** is
12560 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 always associat
12561 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67 65 ed with the Page
12562 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 r object passed
12563 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a as the first .**
12564 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
12565 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 The job of this
12566 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 function is to
12567 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 make pPg clean b
12568 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f y writing its co
12569 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f ntents.** out to
1256a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1256b 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e le, if possible.
1256c 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 This may involv
1256d 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a e syncing the.**
1256e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a journal file. .
1256f 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 **.** If success
12570 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 ful, sqlite3Pcac
12571 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 heMakeClean() is
12572 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 called on the p
12573 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 age and.** SQLIT
12574 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 E_OK returned. I
12575 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
12576 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e curs while tryin
12577 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a g to make the.**
12578 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 page clean, the
12579 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
1257a 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 s returned. If t
1257b 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 he page cannot b
1257c 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 e.** made clean
1257d 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 for some other r
1257e 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 eason, but no er
1257f 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
12580 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 SQLITE_OK.** is
12581 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
12582 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c ite3PcacheMakeCl
12583 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c ean() is not cal
12584 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 led..*/.static i
12585 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28 76 nt pagerStress(v
12586 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 oid *p, PgHdr *p
12587 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 Pg){. Pager *pP
12588 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 ager = (Pager *)
12589 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 p;. int rc = SQ
1258a 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
1258b 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d rt( pPg->pPager=
1258c 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 =pPager );. ass
1258d 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 ert( pPg->flags&
1258e 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a PGHDR_DIRTY );..
1258f 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 /* The doNotSy
12590 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 nc flag is set b
12591 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 y the sqlite3Pag
12592 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 erWrite() functi
12593 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a on while it. **
12594 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 is journalling
12595 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 a set of two or
12596 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 more database pa
12597 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74 6f ges that are sto
12598 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 red. ** on the
12599 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 same disk sector
1259a 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f . Syncing the jo
1259b 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c urnal is not all
1259c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 owed while. **
1259d 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e this is happenin
1259e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 g as it is impor
1259f 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 tant that all me
125a0 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a mbers of such a.
125a1 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 ** set of page
125a2 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 s are synced to
125a3 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 disk together. S
125a4 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 74 o, if the page t
125a5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a his function. *
125a6 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d * is trying to m
125a7 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 ake clean will r
125a8 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c equire a journal
125a9 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f sync and the do
125aa 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 NotSync. ** fla
125ab 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e g is set, return
125ac 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 without doing a
125ad 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 nything. The pca
125ae 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 che layer will.
125af 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f ** just have to
125b0 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c go ahead and al
125b1 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 locate a new pag
125b2 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 e buffer instead
125b3 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 of. ** reusing
125b4 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 pPg.. **. **
125b5 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 Similarly, if th
125b6 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 e pager has alre
125b7 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 ady entered the
125b8 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 error state, do
125b9 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 not. ** try to
125ba 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
125bb 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 ts of pPg to dis
125bc 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 k.. */. if( pP
125bd 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 7c 7c ager->errCode ||
125be 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 (pPager->doNotS
125bf 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67 ync && pPg->flag
125c0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
125c1 43 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e C) ){. return
125c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
125c3 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a . /* Sync the j
125c4 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 72 ournal file if r
125c5 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 equired. */. if
125c6 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ( pPg->flags&PGH
125c7 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
125c8 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 rc = syncJou
125c9 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
125ca 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
125cb 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66 _OK && pPager->f
125cc 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20 ullSync && .
125cd 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 !(pPager->jour
125ce 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
125cf 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
125d0 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73 71 Y) &&. !(sq
125d1 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
125d2 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
125d3 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f ger->fd)&SQLITE_
125d4 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
125d5 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 D). ){.
125d6 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 pPager->nRec = 0
125d7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 ;. rc = wri
125d8 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 teJournalHdr(pPa
125d9 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ger);. }. }.
125da 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
125db 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 73 e number of this
125dc 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 20 page is larger
125dd 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 than the current
125de 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 68 size of. ** th
125df 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
125e0 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f , it may need to
125e1 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 be written to t
125e2 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a he sub-journal..
125e3 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63 ** This is bec
125e4 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f ause the call to
125e5 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 pager_write_pag
125e6 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69 elist() below wi
125e7 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 75 ll not. ** actu
125e8 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 20 ally write data
125e9 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74 to the file in t
125ea 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 his case.. **.
125eb 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 ** Consider the
125ec 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 65 following seque
125ed 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a 20 nce of events:.
125ee 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e **. ** BEGIN
125ef 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 ;. ** <jour
125f0 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a nal page X>. **
125f1 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 67 <modify pag
125f2 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41 e X>. ** SA
125f3 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a 2a VEPOINT sp;. **
125f4 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64 <shrink d
125f5 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
125f6 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 20 Y pages>. **
125f7 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73 28 pagerStress(
125f8 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 20 page X). **
125f9 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 3b ROLLBACK TO sp;
125fa 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 58 . **. ** If (X
125fb 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 70 >Y), then when p
125fc 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63 61 agerStress is ca
125fd 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c 6c lled page X will
125fe 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 0a not be written.
125ff 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 ** out to the
12600 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 database file, b
12601 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 70 ut will be dropp
12602 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ed from the cach
12603 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f e. Then,. ** fo
12604 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f 4c llowing the "ROL
12605 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 61 LBACK TO sp" sta
12606 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 20 tement, reading
12607 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 64 page X will read
12608 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 . ** data from
12609 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1260a 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 e. This will be
1260b 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65 the copy of page
1260c 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 61 X as it. ** wa
1260d 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 s when the trans
1260e 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 action started,
1260f 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 68 not as it was wh
12610 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70 en "SAVEPOINT sp
12611 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63 75 ". ** was execu
12612 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ted.. **. ** T
12613 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 he solution is t
12614 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 o write the curr
12615 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61 67 ent data for pag
12616 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 e X into the .
12617 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 ** sub-journal f
12618 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69 ile now (if it i
12619 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68 s not already th
1261a 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74 ere), so that it
1261b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 will. ** be re
1261c 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 75 stored to its cu
1261d 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 6e rrent value when
1261e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 the "ROLLBACK T
1261f 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 65 O sp" is . ** e
12620 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 xecuted.. */.
12621 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12622 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 K && pPg->pgno>p
12623 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 Pager->dbSize &&
12624 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
12625 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 72 63 e(pPg) ){. rc
12626 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 = subjournalPag
12627 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f e(pPg);. }.. /
12628 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 * Write the cont
12629 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
1262a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
1262b 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 base file. */.
1262c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1262d 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44 K ){. pPg->pD
1262e 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 irty = 0;. rc
1262f 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 = pager_write_p
12630 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 agelist(pPg);.
12631 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 }.. /* Mark the
12632 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 page as clean.
12633 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c */. if( rc==SQL
12634 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 ITE_OK ){. PA
12635 47 45 52 54 52 41 43 45 28 28 22 53 54 52 45 53 GERTRACE(("STRES
12636 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c S %d page %d\n",
12637 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
12638 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 , pPg->pgno));.
12639 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
1263a 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a MakeClean(pPg);.
1263b 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 }.. return pa
1263c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
1263d 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a , rc);.}.../*.**
1263e 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e Allocate and in
1263f 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 itialize a new P
12640 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 ager object and
12641 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f put a pointer to
12642 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 it.** in *ppPag
12643 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68 er. The pager sh
12644 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 ould eventually
12645 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 be freed by pass
12646 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c ing it.** to sql
12647 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29 ite3PagerClose()
12648 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c ..**.** The zFil
12649 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 ename argument i
1264a 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68 s the path to th
1264b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1264c 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a to open..** If z
1264d 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
1264e 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 then a randomly
1264f 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 -named temporary
12650 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 file is created
12651 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 .** and used as
12652 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 the file to be c
12653 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 ached. Temporary
12654 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 65 files are be de
12655 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 leted.** automat
12656 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 ically when they
12657 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 are closed. If
12658 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d zFilename is ":m
12659 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a emory:" then .**
1265a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e all information
1265b 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 is held in cach
1265c 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 e. It is never w
1265d 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 ritten to disk.
1265e 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 20 .** This can be
1265f 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
12660 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 t an in-memory d
12661 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 atabase..**.** T
12662 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d 65 he nExtra parame
12663 74 65 72 20 73 70 65 63 69 66 69 65 73 20 74 68 ter specifies th
12664 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
12665 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 s of space alloc
12666 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 ated.** along wi
12667 74 68 20 65 61 63 68 20 70 61 67 65 20 72 65 66 th each page ref
12668 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 61 erence. This spa
12669 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 ce is available
1266a 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 to the user.** v
1266b 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 ia the sqlite3Pa
1266c 67 65 72 47 65 74 45 78 74 72 61 28 29 20 41 50 gerGetExtra() AP
1266d 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 I..**.** The fla
1266e 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75 gs argument is u
1266f 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 70 sed to specify p
12670 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 61 roperties that a
12671 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 ffect the.** ope
12672 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 ration of the pa
12673 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 ger. It should b
12674 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69 e passed some bi
12675 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f twise combinatio
12676 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45 n.** of the PAGE
12677 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 R_OMIT_JOURNAL a
12678 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 nd PAGER_NO_READ
12679 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a LOCK flags..**.*
1267a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20 70 * The vfsFlags p
1267b 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 69 arameter is a bi
1267c 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74 6f tmask to pass to
1267d 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
1267e 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 eter.** of the x
1267f 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 Open() method of
12680 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 56 46 the supplied VF
12681 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 S when opening f
12682 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 iles. .**.** If
12683 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 the pager object
12684 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
12685 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 d the specified
12686 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 file opened .**
12687 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 successfully, SQ
12688 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
12689 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 72 ned and *ppPager
1268a 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f set to point to
1268b 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 .** the new page
1268c 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 r object. If an
1268d 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 error occurs, *p
1268e 70 50 61 67 65 72 20 69 73 20 73 65 74 20 74 6f pPager is set to
1268f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 NULL.** and err
12690 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
12691 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
12692 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
12693 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 E_NOMEM.** (sqli
12694 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 te3Malloc() is u
12695 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 sed to allocate
12696 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f memory), SQLITE_
12697 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 CANTOPEN or .**
12698 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 various SQLITE_I
12699 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f O_XXX errors..*/
1269a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1269b 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
1269c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
1269d 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
1269e 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 /* The virtual
1269f 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 75 file system to u
126a0 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a se */. Pager **
126a1 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 ppPager,
126a2 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 /* OUT: Return
126a3 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 the Pager struct
126a4 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f ure here */. co
126a5 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
126a6 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f ame, /* Name o
126a7 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
126a8 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 ile to open */.
126a9 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 int nExtra,
126aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
126ab 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20 ra bytes append
126ac 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 to each in-memor
126ad 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 y page */. int
126ae 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
126af 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f /* flags co
126b0 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 ntrolling this f
126b1 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 ile */. int vfs
126b2 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
126b3 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 65 /* flags passe
126b4 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c d through to sql
126b5 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 ite3_vfs.xOpen()
126b6 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 */.){. u8 *pPt
126b7 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 r;. Pager *pPag
126b8 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a er = 0; /*
126b9 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 6f Pager object to
126ba 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 allocate and re
126bb 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 turn */. int rc
126bc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
126bd 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
126be 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 e */. int tempF
126bf 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ile = 0;
126c0 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d 70 /* True for temp
126c1 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e files (incl. in
126c2 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a -memory files) *
126c3 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 /. int memDb =
126c4 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
126c5 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
126c6 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c an in-memory fil
126c7 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f e */. int readO
126c8 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 nly = 0;
126c9 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
126ca 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 is a read-only f
126cb 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 ile */. int jou
126cc 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 rnalFileSize;
126cd 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c /* Bytes to al
126ce 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 20 locate for each
126cf 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 journal fd */.
126d0 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 char *zPathname
126d1 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c = 0; /* Full
126d2 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 73 path to databas
126d3 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 e file */. int
126d4 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 nPathname = 0;
126d5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
126d6 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 68 f bytes in zPath
126d7 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 name */. int us
126d8 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 eJournal = (flag
126d9 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a s & PAGER_OMIT_J
126da 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 OURNAL)==0; /* F
126db 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 alse to omit jou
126dc 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f rnal */. int no
126dd 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 Readlock = (flag
126de 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 s & PAGER_NO_REA
126df 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 DLOCK)!=0; /* T
126e0 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 rue to omit read
126e1 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 -lock */. int p
126e2 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 cacheSize = sqli
126e3 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b te3PcacheSize();
126e4 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
126e5 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 to allocate for
126e6 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36 20 PCache */. u16
126e7 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c szPageDflt = SQL
126e8 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 ITE_DEFAULT_PAGE
126e9 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 _SIZE; /* Defau
126ea 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a lt page size */.
126eb 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
126ec 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 how much space
126ed 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 is required for
126ee 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c each journal fil
126ef 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74 e-handle. ** (t
126f0 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66 20 here are two of
126f1 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a them, the main j
126f2 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 ournal and the s
126f3 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 ub-journal). Thi
126f4 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 s. ** is the ma
126f5 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 75 ximum space requ
126f6 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d ired for an in-m
126f7 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
126f8 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20 le handle . **
126f9 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a 6f and a regular jo
126fa 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c urnal file-handl
126fb 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 22 e. Note that a "
126fc 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d regular journal-
126fd 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79 handle". ** may
126fe 20 62 65 20 61 20 77 72 61 70 70 65 72 20 63 61 be a wrapper ca
126ff 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e 67 pable of caching
12700 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 74 69 the first porti
12701 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 on of the journa
12702 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d l. ** file in m
12703 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65 emory to impleme
12704 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 nt the atomic-wr
12705 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
12706 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72 (see . ** sour
12707 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e ce file journal.
12708 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 c).. */. if( s
12709 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a qlite3JournalSiz
1270a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d e(pVfs)>sqlite3M
1270b 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 emJournalSize()
1270c 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 ){. journalFi
1270d 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 leSize = ROUND8(
1270e 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 sqlite3JournalSi
1270f 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c ze(pVfs));. }el
12710 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 se{. journalF
12711 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 ileSize = ROUND8
12712 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e (sqlite3MemJourn
12713 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a alSize());. }..
12714 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 /* Set the out
12715 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f 20 put variable to
12716 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e 20 NULL in case an
12717 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f error occurs. */
12718 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30 3b . *ppPager = 0;
12719 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 .. /* Compute a
1271a 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 75 6c nd store the ful
1271b 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e l pathname in an
1271c 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1271d 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74 r pointed. ** t
1271e 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 o by zPathname,
1271f 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 length nPathname
12720 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 73 . Or, if this is
12721 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
12722 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f e,. ** leave bo
12723 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64 th nPathname and
12724 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 74 zPathname set t
12725 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 o 0.. */. if(
12726 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 zFilename && zFi
12727 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 lename[0] ){.
12728 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 nPathname = pVf
12729 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b s->mxPathname+1;
1272a 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d . zPathname =
1272b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
1272c 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20 Pathname*2);.
1272d 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d if( zPathname==
1272e 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
1272f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
12730 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
12731 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
12732 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d DB. if( strcm
12733 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65 p(zFilename,":me
12734 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20 mory:")==0 ){.
12735 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 memDb = 1;.
12736 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 zPathname[0
12737 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 ] = 0;. }else
12738 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 .#endif. {.
12739 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d zPathname[0]
1273a 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75 = 0; /* Make su
1273b 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 65 re initialized e
1273c 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68 6e ven if FullPathn
1273d 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20 ame() fails */.
1273e 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1273f 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 3OsFullPathname(
12740 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c pVfs, zFilename,
12741 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74 nPathname, zPat
12742 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 hname);. }..
12743 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 nPathname = s
12744 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
12745 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69 Pathname);. i
12746 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12747 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e && nPathname+8>
12748 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
12749 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
1274a 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 s branch is take
1274b 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e n when the journ
1274c 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 65 64 al path required
1274d 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 by. ** the
1274e 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 database being
1274f 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d opened will be m
12750 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d ore than pVfs->m
12751 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20 20 xPathname.
12752 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 ** bytes in leng
12753 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 th. This means t
12754 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e he database cann
12755 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20 ot be opened,.
12756 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 6c ** as it wil
12757 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c l not be possibl
12758 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f e to open the jo
12759 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 76 urnal file or ev
1275a 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65 63 en. ** chec
1275b 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 k for a hot-jour
1275c 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64 69 nal before readi
1275d 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ng.. */.
1275e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1275f 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 ANTOPEN;. }.
12760 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
12761 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
12762 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 lite3_free(zPath
12763 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 name);. ret
12764 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
12765 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 }.. /* Allocate
12766 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 memory for the
12767 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c Pager structure,
12768 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 PCache object,
12769 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 the. ** three f
1276a 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c ile descriptors,
1276b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1276c 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 le name and the
1276d 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 journal . ** fi
1276e 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 le name. The lay
1276f 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 out in memory is
12770 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a as follows:. *
12771 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72 *. ** Pager
12772 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 object
12773 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65 (size
12774 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29 of(Pager) bytes)
12775 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65 . ** PCache
12776 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 object
12777 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 (sqlit
12778 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62 e3PcacheSize() b
12779 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 ytes). ** D
1277a 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e atabase file han
1277b 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28 dle (
1277c 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 pVfs->szOsFile b
1277d 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 ytes). ** S
1277e 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
1277f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28 handle (
12780 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
12781 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
12782 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c Main journal fil
12783 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 e handle
12784 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 (journalFileSize
12785 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
12786 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e Database file n
12787 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
12788 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 (nPathname+1 by
12789 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f tes). ** Jo
1278a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
1278b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e (n
1278c 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 Pathname+8+1 byt
1278d 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 es). */. pPtr
1278e 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d = (u8 *)sqlite3M
1278f 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 allocZero(. R
12790 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 OUND8(sizeof(*pP
12791 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a ager)) + /*
12792 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 Pager structure
12793 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 */. ROUND8(p
12794 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 cacheSize) +
12795 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 /* PCache
12796 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 object */. R
12797 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 OUND8(pVfs->szOs
12798 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a File) + /*
12799 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c The main db fil
1279a 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c e */. journal
1279b 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 FileSize * 2 +
1279c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
1279d 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 wo journal files
1279e 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 */ . nPathna
1279f 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 me + 1 +
127a0 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 /* zFile
127a1 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 name */. nPat
127a2 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 hname + 8 + 1
127a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a /* zJ
127a4 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 ournal */. );.
127a5 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
127a6 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 YTE_ALIGNMENT(SQ
127a7 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 LITE_INT_TO_PTR(
127a8 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 journalFileSize)
127a9 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 72 ) );. if( !pPtr
127aa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
127ab 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b free(zPathname);
127ac 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
127ad 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
127ae 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 20 pPager =
127af 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 70 (Pager*)(p
127b0 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e Ptr);. pPager->
127b1 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 43 pPCache = (PC
127b2 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 ache*)(pPtr += R
127b3 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 OUND8(sizeof(*pP
127b4 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 65 ager)));. pPage
127b5 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 74 r->fd = (sqlit
127b6 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b e3_file*)(pPtr +
127b7 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 = ROUND8(pcacheS
127b8 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d ize));. pPager-
127b9 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 >sjfd = (sqlite3
127ba 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 _file*)(pPtr +=
127bb 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f ROUND8(pVfs->szO
127bc 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 sFile));. pPage
127bd 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 r->jfd = (sqlit
127be 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b e3_file*)(pPtr +
127bf 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a = journalFileSiz
127c0 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 e);. pPager->zF
127c1 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 ilename = (ch
127c2 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 ar*)(pPtr += jou
127c3 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 rnalFileSize);.
127c4 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
127c5 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 YTE_ALIGNMENT(pP
127c6 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 ager->jfd) );..
127c7 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 /* Fill in the
127c8 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 Pager.zFilename
127c9 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e and Pager.zJourn
127ca 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 72 al buffers, if r
127cb 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 equired. */. if
127cc 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 ( zPathname ){.
127cd 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 pPager->zJour
127ce 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 nal = (char*)(
127cf 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d pPtr += nPathnam
127d0 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 e + 1);. memc
127d1 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 py(pPager->zFile
127d2 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c name, zPathname,
127d3 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 nPathname);.
127d4 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e memcpy(pPager->
127d5 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e zJournal, zPathn
127d6 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b ame, nPathname);
127d7 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 . memcpy(&pPa
127d8 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 ger->zJournal[nP
127d9 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 athname], "-jour
127da 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 73 71 nal", 8);. sq
127db 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 lite3_free(zPath
127dc 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 name);. }. pPa
127dd 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 ger->pVfs = pVfs
127de 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 ;. pPager->vfsF
127df 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b lags = vfsFlags;
127e0 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
127e1 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f pager file.. */
127e2 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 . if( zFilename
127e3 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d && zFilename[0]
127e4 20 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 && !memDb ){.
127e5 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 int fout = 0;
127e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
127e7 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 /* VFS flags
127e8 72 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 65 returned by xOpe
127e9 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 n() */. rc =
127ea 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
127eb 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c fs, pPager->zFil
127ec 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 ename, pPager->f
127ed 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66 6f d, vfsFlags, &fo
127ee 75 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c ut);. readOnl
127ef 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45 y = (fout&SQLITE
127f0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b _OPEN_READONLY);
127f1 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
127f2 66 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 file was success
127f3 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 fully opened for
127f4 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 read/write acce
127f5 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 ss,. ** choos
127f6 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 65 e a default page
127f7 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65 size in case we
127f8 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 20 have to create
127f9 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
127fa 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65 ase file. The de
127fb 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 fault page size
127fc 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f is the maximum o
127fd 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a f:. **. **
127fe 20 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 + SQLITE_DEF
127ff 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a AULT_PAGE_SIZE,.
12800 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 ** + The
12801 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
12802 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f y sqlite3OsSecto
12803 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 rSize(). **
12804 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 + The largest
12805 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 page size that c
12806 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 an be written at
12807 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f omically.. */
12808 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
12809 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f ITE_OK && !readO
1280a 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74 nly ){. set
1280b 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 SectorSize(pPage
1280c 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 r);. assert
1280d 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
1280e 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 PAGE_SIZE<=SQLIT
1280f 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
12810 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 GE_SIZE);.
12811 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 if( szPageDflt<p
12812 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
12813 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 e ){. if(
12814 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
12815 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ize>SQLITE_MAX_D
12816 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
12817 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a ){. sz
12818 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 PageDflt = SQLIT
12819 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
1281a 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 GE_SIZE;.
1281b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1281c 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 szPageDflt = (
1281d 75 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63 74 u16)pPager->sect
1281e 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 orSize;.
1281f 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 }. }.#ifdef
12820 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
12821 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 TOMIC_WRITE.
12822 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
12823 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 iDc = sqlite3OsD
12824 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
12825 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
12826 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 ;. int ii
12827 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
12828 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
12829 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 OMIC512==(512>>8
1282a 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ));. asse
1282b 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f rt(SQLITE_IOCAP_
1282c 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 ATOMIC64K==(6553
1282d 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 6>>8));.
1282e 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 assert(SQLITE_MA
1282f 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
12830 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 IZE<=65536);.
12831 20 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 for(ii=szPa
12832 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 geDflt; ii<=SQLI
12833 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
12834 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a AGE_SIZE; ii=ii*
12835 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 2){. if
12836 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f ( iDc&(SQLITE_IO
12837 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e CAP_ATOMIC|(ii>>
12838 38 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 8)) ){.
12839 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 szPageDflt =
1283a 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ii;. }.
1283b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1283c 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 }.#endif. }.
1283d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
1283e 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 f a temporary fi
1283f 6c 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c le is requested,
12840 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 it is not opene
12841 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 d immediately..
12842 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 ** In this ca
12843 73 65 20 77 65 20 61 63 63 65 70 74 20 74 68 65 se we accept the
12844 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 default page si
12845 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 ze and delay act
12846 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 ually. ** ope
12847 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e ning the file un
12848 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 til the first ca
12849 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e ll to OsWrite().
1284a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1284b 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c his branch is al
1284c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e so run for an in
1284d 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1284e 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 . An in-memory.
1284f 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 ** database i
12850 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 s the same as a
12851 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69 temp-file that i
12852 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 s never written
12853 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 out to. ** di
12854 73 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69 sk and uses an i
12855 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 n-memory rollbac
12856 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a k journal.. *
12857 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 / . tempFile
12858 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d = 1;. pPager-
12859 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 >state = PAGER_E
1285a 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 0a 20 XCLUSIVE;. }..
1285b 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1285c 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 g call to PagerS
1285d 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72 etPagesize() ser
1285e 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76 ves to set the v
1285f 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 alue of . ** Pa
12860 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 ger.pageSize and
12861 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 to allocate the
12862 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 Pager.pTmpSpace
12863 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 buffer.. */.
12864 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12865 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
12866 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d pPager->memDb==
12867 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 0 );. rc = sq
12868 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
12869 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73 esize(pPager, &s
1286a 7a 50 61 67 65 44 66 6c 74 29 3b 0a 20 20 20 20 zPageDflt);.
1286b 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 testcase( rc!=SQ
1286c 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a LITE_OK );. }..
1286d 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
1286e 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69 74 occurred in eit
1286f 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b her of the block
12870 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74 68 s above, free th
12871 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 74 e . ** Pager st
12872 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f 73 ructure and clos
12873 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f e the file.. */
12874 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
12875 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
12876 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54 6d rt( !pPager->pTm
12877 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73 71 pSpace );. sq
12878 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
12879 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71 ger->fd);. sq
1287a 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 lite3_free(pPage
1287b 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 r);. return r
1287c 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 c;. }.. /* Ini
1287d 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61 63 tialize the PCac
1287e 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 he object. */.
1287f 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 nExtra = ROUND8(
12880 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 74 nExtra);. sqlit
12881 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50 e3PcacheOpen(szP
12882 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c ageDflt, nExtra,
12883 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 20 !memDb,.
12884 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d 65 !me
12885 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 3a mDb?pagerStress:
12886 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65 0, (void *)pPage
12887 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 r, pPager->pPCac
12888 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 he);.. PAGERTRA
12889 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 5c CE(("OPEN %d %s\
1288a 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 n", FILEHANDLEID
1288b 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 (pPager->fd), pP
1288c 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 ager->zFilename)
1288d 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f );. IOTRACE(("O
1288e 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 PEN %p %s\n", pP
1288f 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 ager, pPager->zF
12890 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61 ilename)).. pPa
12891 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
12892 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c = (u8)useJournal
12893 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 ;. pPager->noRe
12894 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 64 adlock = (noRead
12895 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c 79 lock && readOnly
12896 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 61 ) ?1:0;. /* pPa
12897 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 ger->stmtOpen =
12898 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
12899 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 r->stmtInUse = 0
1289a 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
1289b 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 ->nRef = 0; */.
1289c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
1289d 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 62 alid = (u8)memDb
1289e 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 ;. /* pPager->s
1289f 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a tmtSize = 0; */.
128a0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
128a1 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 tJSize = 0; */.
128a2 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 /* pPager->nPag
128a3 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 e = 0; */. pPag
128a4 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c er->mxPgno = SQL
128a5 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 ITE_MAX_PAGE_COU
128a6 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d NT;. /* pPager-
128a7 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55 >state = PAGER_U
128a8 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 NLOCK; */. asse
128a9 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
128aa 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f e == (tempFile ?
128ab 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
128ac 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 : PAGER_UNLOCK)
128ad 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d );. /* pPager-
128ae 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f >errMask = 0; */
128af 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 . pPager->tempF
128b0 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 ile = (u8)tempFi
128b1 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 65 le;. assert( te
128b2 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f mpFile==PAGER_LO
128b3 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
128b4 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74 . || t
128b5 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c empFile==PAGER_L
128b6 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
128b7 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 SIVE );. assert
128b8 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ( PAGER_LOCKINGM
128b9 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d 31 ODE_EXCLUSIVE==1
128ba 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 );. pPager->ex
128bb 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 clusiveMode = (u
128bc 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70 8)tempFile; . p
128bd 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
128be 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d ntDone = pPager-
128bf 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 >tempFile;. pPa
128c0 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 38 ger->memDb = (u8
128c1 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 )memDb;. pPager
128c2 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 38 ->readOnly = (u8
128c3 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 )readOnly;. /*
128c4 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
128c5 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 = 0; */. pPage
128c6 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 28 70 50 61 r->noSync = (pPa
128c7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c 7c ger->tempFile ||
128c8 20 21 75 73 65 4a 6f 75 72 6e 61 6c 29 20 3f 31 !useJournal) ?1
128c9 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 :0;. pPager->fu
128ca 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d llSync = pPager-
128cb 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 >noSync ?0:1;.
128cc 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
128cd 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs = SQLITE_SYNC
128ce 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 _NORMAL;. /* pP
128cf 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 ager->pFirst = 0
128d0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
128d1 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d ->pFirstSynced =
128d2 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
128d3 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a er->pLast = 0; *
128d4 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 /. pPager->nExt
128d5 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20 20 70 ra = nExtra;. p
128d6 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 Pager->journalSi
128d7 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 zeLimit = SQLITE
128d8 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c _DEFAULT_JOURNAL
128d9 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 _SIZE_LIMIT;. a
128da 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
128db 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d ager->fd) || tem
128dc 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 pFile );. setSe
128dd 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
128de 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29 7b ;. if( memDb ){
128df 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
128e0 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 rnalMode = PAGER
128e1 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
128e2 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 ORY;. }. /* pP
128e3 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c ager->xBusyHandl
128e4 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 er = 0; */. /*
128e5 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e pPager->pBusyHan
128e6 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a dlerArg = 0; */.
128e7 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 /* memset(pPag
128e8 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 er->aHash, 0, si
128e9 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61 zeof(pPager->aHa
128ea 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 sh)); */. *ppPa
128eb 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 ger = pPager;.
128ec 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
128ed 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ;.}..../*.** Thi
128ee 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
128ef 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73 lled after trans
128f0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 itioning from PA
128f1 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a GER_UNLOCK to.**
128f2 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74 PAGER_SHARED st
128f3 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66 ate. It tests if
128f4 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 there is a hot
128f5 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 journal present
128f6 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 in.** the file-s
128f7 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69 ystem for the gi
128f8 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74 ven pager. A hot
128f9 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 journal is one
128fa 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 that .** needs t
128fb 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b o be played back
128fc 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 . According to t
128fd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 his function, a
128fe 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 hot-journal.** f
128ff 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68 ile exists if th
12900 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 e following crit
12901 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a eria are met:.**
12902 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 .** * The jour
12903 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
12904 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 in the file syst
12905 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e em, and.** * N
12906 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 o process holds
12907 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 a RESERVED or gr
12908 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 eater lock on th
12909 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1290a 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
1290b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 database file it
1290c 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20 self is greater
1290d 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20 than 0 bytes in
1290e 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a size, and.** *
1290f 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 The first byte
12910 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
12911 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69 ile exists and i
12912 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a s not 0x00..**.*
12913 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 * If the current
12914 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
12915 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20 abase file is 0
12916 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 but a journal fi
12917 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 le.** exists, th
12918 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61 at is probably a
12919 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 n old journal le
1291a 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 ft over from a p
1291b 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 rior.** database
1291c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e with the same n
1291d 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ame. In this cas
1291e 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
1291f 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 le is.** just de
12920 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65 leted using OsDe
12921 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69 lete, *pExists i
12922 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 s set to 0 and S
12923 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 QLITE_OK.** is r
12924 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
12925 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
12926 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68 not check if th
12927 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20 ere is a master
12928 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
12929 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f .** at the end o
1292a 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 f the file. If t
1292b 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61 here is, and tha
1292c 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c t master journal
1292d 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f file.** does no
1292e 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68 t exist, then th
1292f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
12930 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 s not really hot
12931 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 . In this.** cas
12932 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 e this routine w
12933 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c ill return a fal
12934 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65 se-positive. The
12935 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
12936 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c ).** routine wil
12937 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20 l discover that
12938 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12939 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 is not really h
1293a 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 ot and .** will
1293b 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b not roll it back
1293c 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f . .**.** If a ho
1293d 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 t-journal file i
1293e 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 s found to exist
1293f 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 , *pExists is se
12940 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 t to 1 and .** S
12941 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
12942 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 d. If no hot-jou
12943 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
12944 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69 sent, *pExists i
12945 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e s.** set to 0 an
12946 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
12947 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
12948 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
12949 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 e trying.** to d
1294a 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
1294b 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f or not a hot-jo
1294c 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
1294d 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a s, the IO error.
1294e 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 ** code is retur
1294f 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75 ned and the valu
12950 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73 e of *pExists is
12951 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 undefined..*/.s
12952 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74 tatic int hasHot
12953 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
12954 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 Pager, int *pExi
12955 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f sts){. sqlite3_
12956 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 vfs * const pVfs
12957 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
12958 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
12959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1295a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
1295b 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b */. int exists;
1295c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1295d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 /* True if a
1295e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
1295f 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 present */.. as
12960 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 sert( pPager!=0
12961 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
12962 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
12963 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
12964 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12965 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 );. assert( !is
12966 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12967 29 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 73 ) );.. *pExists
12968 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c = 0;. rc = sql
12969 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
1296a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
1296b 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 nal, SQLITE_ACCE
1296c 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 SS_EXISTS, &exis
1296d 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ts);. if( rc==S
1296e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 73 QLITE_OK && exis
1296f 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6f ts ){. int lo
12970 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 cked;
12971 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
12972 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 6f some process ho
12973 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20 6c lds a RESERVED l
12974 6f 63 6b 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ock */. rc =
12975 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 sqlite3OsCheckRe
12976 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67 65 servedLock(pPage
12977 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29 3b r->fd, &locked);
12978 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
12979 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b 65 ITE_OK && !locke
1297a 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e d ){. int n
1297b 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 Page;.. /*
1297c 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 6f Check the size o
1297d 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1297e 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 69 ile. If it consi
1297f 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c 0a sts of 0 pages,.
12980 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 65 ** then de
12981 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
12982 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 68 file. See the h
12983 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 eader comment ab
12984 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 2a ove for . *
12985 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 20 * the reasoning
12986 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 here.. */.
12987 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12988 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
12989 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b pPager, &nPage);
1298a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
1298b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1298c 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 if( nPage==0
1298d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
1298e 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 = sqlite3OsDele
1298f 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d te(pVfs, pPager-
12990 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 >zJournal, 0);.
12991 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
12992 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6a /* The j
12993 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 ournal file exis
12994 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 ts and no other
12995 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 connection has a
12996 20 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 20 reserved.
12997 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 ** or greate
12998 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 r lock on the da
12999 74 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 tabase file. Now
1299a 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 check that ther
1299b 65 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a e is. *
1299c 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e * at least one n
1299d 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 74 on-zero bytes at
1299e 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
1299f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
129a0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 ** If
129a1 74 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 77 there is, then w
129a2 65 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 e consider this
129a3 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f journal to be ho
129a4 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 t. If not, .
129a5 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 ** it can
129a6 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 be ignored..
129a7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
129a8 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 int f = SQLIT
129a9 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c E_OPEN_READONLY|
129aa 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
129ab 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 _JOURNAL;.
129ac 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
129ad 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 OsOpen(pVfs, pPa
129ae 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
129af 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 Pager->jfd, f, &
129b0 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 f);. if
129b1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
129b2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
129b3 38 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 8 first = 0;.
129b4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
129b5 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
129b6 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a er->jfd, (void *
129b7 29 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a )&first, 1, 0);.
129b8 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
129b9 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
129ba 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 _SHORT_READ ){.
129bb 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 rc
129bc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
129bd 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
129be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 sqlite3Os
129bf 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 Close(pPager->jf
129c0 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 d);.
129c1 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73 *pExists = (firs
129c2 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 t!=0);.
129c3 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
129c4 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
129c5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
129c6 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 /*.** Read the c
129c7 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 ontent for page
129c8 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 pPg out of the d
129c9 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
129ca 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 into .** pPg->p
129cb 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c Data. A shared l
129cc 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d ock or greater m
129cd 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 ust be held on t
129ce 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 he database.** f
129cf 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 ile before this
129d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
129d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 ed..**.** If pag
129d2 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 e 1 is read, the
129d3 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 n the value of P
129d4 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b ager.dbFileVers[
129d5 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 ] is set to.** t
129d6 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
129d7 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
129d8 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 file..**.** If a
129d9 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
129da 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 s, then the IO e
129db 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
129dc 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
129dd 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 ** Otherwise, SQ
129de 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
129df 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
129e0 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 50 67 nt readDbPage(Pg
129e1 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 Hdr *pPg){. Pag
129e2 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
129e3 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 ->pPager; /* Pag
129e4 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 er object associ
129e5 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20 70 ated with page p
129e6 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e Pg */. Pgno pgn
129e7 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 o = pPg->pgno;
129e8 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
129e9 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 ber to read */.
129ea 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
129eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
129ec 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
129ed 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 i64 iOffset;
129ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
129ef 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 * Byte offset of
129f0 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 file to read fr
129f1 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 om */.. assert(
129f2 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
129f3 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 20 PAGER_SHARED &&
129f4 21 4d 45 4d 44 42 20 29 3b 0a 0a 20 20 69 66 28 !MEMDB );.. if(
129f5 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
129f6 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 >fd) ){. asse
129f7 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 rt( pPager->temp
129f8 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 File );. mems
129f9 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 et(pPg->pData, 0
129fa 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
129fb 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ze);. return
129fc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
129fd 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f iOffset = (pgno
129fe 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d -1)*(i64)pPager-
129ff 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 >pageSize;. rc
12a00 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
12a01 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d pPager->fd, pPg-
12a02 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e >pData, pPager->
12a03 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65 pageSize, iOffse
12a04 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
12a05 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
12a06 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20 _READ ){. rc
12a07 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
12a08 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 . if( pgno==1 )
12a09 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65 {. u8 *dbFile
12a0a 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50 Vers = &((u8*)pP
12a0b 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 g->pData)[24];.
12a0c 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
12a0d 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 r->dbFileVers, d
12a0e 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f bFileVers, sizeo
12a0f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
12a10 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f Vers));. }. CO
12a11 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67 DEC1(pPager, pPg
12a12 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 ->pData, pgno, 3
12a13 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 );.. PAGER_INCR
12a14 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 (sqlite3_pager_r
12a15 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 eaddb_count);.
12a16 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
12a17 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 r->nRead);. IOT
12a18 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 RACE(("PGIN %p %
12a19 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 d\n", pPager, pg
12a1a 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41 no));. PAGERTRA
12a1b 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70 61 CE(("FETCH %d pa
12a1c 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 ge %d hash(%08x)
12a1d 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
12a1e 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
12a1f 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 er), pgno, pager
12a20 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 29 _pagehash(pPg)))
12a21 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a ;.. return rc;.
12a22 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
12a23 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12a24 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 75 70 whenever the up
12a25 70 65 72 20 6c 61 79 65 72 20 72 65 71 75 65 73 per layer reques
12a26 74 73 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a ts a database.**
12a27 20 70 61 67 65 20 69 73 20 72 65 71 75 65 73 74 page is request
12a28 65 64 2c 20 62 65 66 6f 72 65 20 74 68 65 20 63 ed, before the c
12a29 61 63 68 65 20 69 73 20 63 68 65 63 6b 65 64 20 ache is checked
12a2a 66 6f 72 20 61 20 73 75 69 74 61 62 6c 65 20 70 for a suitable p
12a2b 61 67 65 0a 2a 2a 20 6f 72 20 61 6e 79 20 64 61 age.** or any da
12a2c 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20 ta is read from
12a2d 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 74 the database. It
12a2e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 66 6f performs the fo
12a2f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20 66 llowing.** two f
12a30 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 unctions:.**.**
12a31 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65 1) If the page
12a32 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 r is currently i
12a33 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 n PAGER_UNLOCK s
12a34 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 tate (no lock he
12a35 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 ld.** on th
12a36 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 e database file)
12a37 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 , then an attemp
12a38 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 t is made to obt
12a39 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 ain a.** SH
12a3a 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ARED lock on the
12a3b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12a3c 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 Immediately afte
12a3d 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 r obtaining.**
12a3e 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c the SHARED l
12a3f 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 ock, the file-sy
12a40 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 stem is checked
12a41 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 for a hot-journa
12a42 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68 l,.** which
12a43 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 is played back
12a44 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c if present. Foll
12a45 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f owing any hot-jo
12a46 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 urnal .** r
12a47 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e ollback, the con
12a48 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 tents of the cac
12a49 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64 he are validated
12a4a 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 by checking.**
12a4b 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65 the 'change
12a4c 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20 -counter' field
12a4d 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
12a4e 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a file header and.
12a4f 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65 ** discarde
12a50 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f d if they are fo
12a51 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 und to be invali
12a52 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 d..**.** 2) If
12a53 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
12a54 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
12a55 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 ve-mode, and the
12a56 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
12a57 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 .** no outs
12a58 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
12a59 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c es to any pages,
12a5a 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65 and is in the e
12a5b 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 rror state,.**
12a5c 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65 then an atte
12a5d 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 mpt is made to c
12a5e 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 lear the error s
12a5f 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69 tate by discardi
12a60 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63 ng.** the c
12a61 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
12a62 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f age cache and ro
12a63 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f lling back any o
12a64 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 pen journal.**
12a65 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 file..**.**
12a66 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e If the operation
12a67 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32 described by (2
12a68 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 ) above is not a
12a69 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 ttempted, and if
12a6a 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 the.** pager is
12a6b 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 in an error sta
12a6c 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 te other than SQ
12a6d 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 LITE_FULL when t
12a6e 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a his is called,.*
12a6f 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 * the error stat
12a70 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
12a71 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20 returned. It is
12a72 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61 permitted to rea
12a73 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
12a74 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 e when in SQLITE
12a75 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 _FULL error stat
12a76 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
12a77 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e se, if everythin
12a78 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
12a79 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
12a7a 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a turned. If an.**
12a7b 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
12a7c 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 while locking t
12a7d 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 he database, che
12a7e 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d cking for a hot-
12a7f 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
12a80 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 or rolling back
12a81 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 a journal file,
12a82 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
12a83 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
12a84 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12a85 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 erSharedLock(Pag
12a86 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
12a87 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
12a88 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K;
12a89 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
12a8a 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f */. int isErro
12a8b 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20 rReset = 0;
12a8c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
12a8d 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66 if recovering f
12a8e 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20 rom error state
12a8f 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 */.. /* If this
12a90 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
12a91 6e 65 64 20 66 6f 72 20 65 78 63 6c 75 73 69 76 ned for exclusiv
12a92 65 20 61 63 63 65 73 73 2c 20 68 61 73 20 6e 6f e access, has no
12a93 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 20 20 outstanding .
12a94 2a 2a 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 ** page referenc
12a95 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 6e 20 es and is in an
12a96 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 69 error-state, thi
12a97 73 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f s is a chance to
12a98 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 clear. ** the
12a99 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 error. Discard t
12a9a 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
12a9b 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 he pager-cache a
12a9c 6e 64 20 74 72 65 61 74 20 61 6e 79 0a 20 20 2a nd treat any. *
12a9d 2a 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 * open journal f
12a9e 69 6c 65 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 ile as a hot-jou
12a9f 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rnal.. */. if(
12aa0 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 !MEMDB && pPage
12aa1 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
12aa2 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 . && sqlite3P
12aa3 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
12aa4 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
12aa5 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 0 && pPager->err
12aa6 43 6f 64 65 20 0a 20 20 29 7b 0a 20 20 20 20 69 Code . ){. i
12aa7 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
12aa8 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
12aa9 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 31 isErrorReset = 1
12aaa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
12aab 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 er->errCode = SQ
12aac 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 LITE_OK;. pag
12aad 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
12aae 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
12aaf 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c he pager is stil
12ab0 6c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 l in an error st
12ab1 61 74 65 2c 20 64 6f 20 6e 6f 74 20 70 72 6f 63 ate, do not proc
12ab2 65 65 64 2e 20 54 68 65 20 65 72 72 6f 72 20 0a eed. The error .
12ab3 20 20 2a 2a 20 73 74 61 74 65 20 77 69 6c 6c 20 ** state will
12ab4 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 73 6f be cleared at so
12ab5 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 me point in the
12ab6 66 75 74 75 72 65 20 77 68 65 6e 20 61 6c 6c 20 future when all
12ab7 70 61 67 65 20 0a 20 20 2a 2a 20 72 65 66 65 72 page . ** refer
12ab8 65 6e 63 65 73 20 61 72 65 20 64 72 6f 70 70 65 ences are droppe
12ab9 64 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 d and the cache
12aba 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 can be discarded
12abb 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
12abc 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 ger->errCode &&
12abd 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 pPager->errCode!
12abe 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a =SQLITE_FULL ){.
12abf 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 return pPage
12ac0 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a r->errCode;. }.
12ac1 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
12ac2 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
12ac3 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 CK || isErrorRes
12ac4 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 et ){. sqlite
12ac5 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 3_vfs * const pV
12ac6 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
12ac7 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 s;. int isHot
12ac8 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 Journal = 0;.
12ac9 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
12aca 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
12acb 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
12acc 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
12acd 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 ache)==0 );.
12ace 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52 if( !pPager->noR
12acf 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 eadlock ){.
12ad0 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
12ad1 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
12ad2 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
12ad3 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
12ad4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12ad5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12ad6 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
12ad7 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 NLOCK );.
12ad8 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
12ad9 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
12ada 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
12adb 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 se if( pPager->s
12adc 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
12add 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 CK ){. pPag
12ade 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
12adf 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a R_SHARED;. }.
12ae0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12ae1 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 er->state>=SHARE
12ae2 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f D_LOCK );.. /
12ae3 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 * If a journal f
12ae4 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 ile exists, and
12ae5 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 there is no RESE
12ae6 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 RVED lock on the
12ae7 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
12ae8 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 file, then it e
12ae9 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 ither needs to b
12aea 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 e played back or
12aeb 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f deleted.. */
12aec 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f . if( !isErro
12aed 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 rReset ){.
12aee 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e rc = hasHotJourn
12aef 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f al(pPager, &isHo
12af0 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 tJournal);.
12af1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12af2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
12af3 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
12af4 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
12af5 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c isErrorReset ||
12af6 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b isHotJournal ){
12af7 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e . /* Get an
12af8 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
12af9 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
12afa 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f file. At this po
12afb 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 int it is.
12afc 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 ** important tha
12afd 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 t a RESERVED loc
12afe 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 k is not obtaine
12aff 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 d on the way to
12b00 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 the. ** EXC
12b01 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
12b02 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 it were, another
12b03 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f process might o
12b04 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a pen the. **
12b05 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
12b06 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 52 detect the RESER
12b07 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f VED lock, and co
12b08 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a nclude that the.
12b09 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 ** databas
12b0a 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 e is safe to rea
12b0b 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f d while this pro
12b0c 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f cess is still ro
12b0d 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 lling the .
12b0e 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ** hot-journal
12b0f 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a back.. ** .
12b10 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 ** Because
12b11 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 the intermediat
12b12 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 e RESERVED lock
12b13 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 is not requested
12b14 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f , any. ** o
12b15 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 ther process att
12b16 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 empting to acces
12b17 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
12b18 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 ile will get to
12b19 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 . ** this p
12b1a 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 oint in the code
12b1b 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 and fail to obt
12b1c 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c ain its own EXCL
12b1d 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 USIVE lock .
12b1e 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 ** on the data
12b1f 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 base file..
12b20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 */. if( pP
12b21 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c ager->state<EXCL
12b22 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
12b23 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12b24 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
12b25 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c >fd, EXCLUSIVE_L
12b26 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 OCK);. if
12b27 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12b28 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
12b29 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 = pager_error(pP
12b2a 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 ager, rc);.
12b2b 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
12b2c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
12b2d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
12b2e 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e = PAGER_EXCLUS
12b2f 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 IVE;. }. .
12b30 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 /* Open the
12b31 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 journal for rea
12b32 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 d/write access.
12b33 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 This is because
12b34 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 in . ** exc
12b35 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f lusive-access mo
12b36 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 de the file desc
12b37 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b riptor will be k
12b38 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 ept open and.
12b39 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 ** possibly u
12b3a 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 sed for a transa
12b3b 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 ction later on.
12b3c 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c On some systems,
12b3d 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 the. ** Os
12b3e 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 Truncate() call
12b3f 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 used in exclusiv
12b40 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c e-access mode al
12b41 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 so requires.
12b42 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 ** a read/writ
12b43 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 e file handle..
12b44 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
12b45 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
12b46 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
12b47 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
12b48 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12b49 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 sAccess(pVfs,pPa
12b4a 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 ger->zJournal,SQ
12b4b 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
12b4c 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 TS,&res);.
12b4d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12b4e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
12b4f 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 if( res ){.
12b50 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 int fout
12b51 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
12b52 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 int f = SQLITE
12b53 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c _OPEN_READWRITE|
12b54 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
12b55 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 _JOURNAL;.
12b56 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
12b57 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
12b58 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
12b59 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
12b5a 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
12b5b 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
12b5c 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 ->jfd, f, &fout)
12b5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 ;. as
12b5e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
12b5f 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 _OK || isOpen(pP
12b60 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
12b61 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
12b62 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 ==SQLITE_OK && f
12b63 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f out&SQLITE_OPEN_
12b64 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 READONLY ){.
12b65 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
12b66 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
12b67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
12b68 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
12b69 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
12b6a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12b6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
12b6c 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
12b6d 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 journal does not
12b6e 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d 65 61 exist, that mea
12b6f 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 ns some other pr
12b70 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20 20 20 ocess.
12b71 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 ** has already
12b72 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63 6b 20 rolled it back
12b73 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 */. r
12b74 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
12b75 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
12b76 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
12b77 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
12b78 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12b79 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
12b7a 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
12b7b 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 74 TODO: Why are t
12b7c 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 72 hese cleared her
12b7d 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 61 e? Is it necessa
12b7e 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 ry? */. pPa
12b7f 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 ger->journalStar
12b80 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ted = 0;. p
12b81 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12b82 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 f = 0;. pPa
12b83 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
12b84 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
12b85 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
12b86 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 ;. . /* Pla
12b87 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 yback and delete
12b88 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 the journal. D
12b89 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 65 rop the database
12b8a 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 write. **
12b8b 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 lock and reacqui
12b8c 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b re the read lock
12b8d 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63 68 . Purge the cach
12b8e 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a e before. *
12b8f 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 * playing back t
12b90 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 he hot-journal s
12b91 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 o that we don't
12b92 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 20 end up with.
12b93 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 ** an inconsis
12b94 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 20 tent cache..
12b95 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
12b96 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
12b97 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 Pager, 1);.
12b98 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12b99 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
12b9a 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 = pager_error(p
12b9b 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
12b9c 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b goto failed;
12b9d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
12b9e 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e ssert( (pPager->
12b9f 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 state==PAGER_SHA
12ba0 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20 RED).
12ba1 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c || (pPager->excl
12ba2 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 usiveMode && pPa
12ba3 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 ger->state>PAGER
12ba4 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29 _SHARED). )
12ba5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
12ba6 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
12ba7 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 || sqlite3Pcach
12ba8 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 ePagecount(pPage
12ba9 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b r->pPCache)>0 ){
12baa 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 . /* The sh
12bab 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 ared-lock has ju
12bac 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 st been acquired
12bad 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
12bae 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 file. ** a
12baf 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 nd there are alr
12bb0 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 eady pages in th
12bb1 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 e cache (from a
12bb2 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a previous. *
12bb3 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 * read or write
12bb4 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 transaction). C
12bb5 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
12bb6 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
12bb7 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
12bb8 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 dified. If the
12bb9 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61 database has cha
12bba 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a nged, flush the.
12bbb 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a ** cache..
12bbc 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
12bbd 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 * Database chang
12bbe 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62 es is detected b
12bbf 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 y looking at 15
12bc0 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a bytes beginning.
12bc1 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 ** at offs
12bc2 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 et 24 into the f
12bc3 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ile. The first
12bc4 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 4 of these 16 by
12bc5 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a tes are. **
12bc6 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 a 32-bit counte
12bc7 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
12bc8 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20 ented with each
12bc9 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 change. The.
12bca 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 ** other byte
12bcb 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c s change randoml
12bcc 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 y with each file
12bcd 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 change when.
12bce 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 ** a codec is
12bcf 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a in use.. *
12bd0 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 * . ** Ther
12bd1 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 e is a vanishing
12bd2 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 ly small chance
12bd3 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 that a change wi
12bd4 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 ll not be .
12bd5 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 ** detected. T
12bd6 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 he chance of an
12bd7 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 undetected chang
12bd8 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 e is so small th
12bd9 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 at. ** it c
12bda 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e an be neglected.
12bdb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
12bdc 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b char dbFileVers[
12bdd 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
12bde 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 bFileVers)];.
12bdf 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 sqlite3PagerP
12be0 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
12be1 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 0);.. if(
12be2 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
12be3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
12be4 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
12be5 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
12be6 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 iled;. }..
12be7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
12be8 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12be9 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 );. if( pP
12bea 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 ager->dbSize>0 )
12beb 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 {. IOTRAC
12bec 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 E(("CKVERS %p %d
12bed 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a \n", pPager, siz
12bee 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 eof(dbFileVers))
12bef 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
12bf0 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
12bf1 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c ager->fd, &dbFil
12bf2 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 eVers, sizeof(db
12bf3 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a FileVers), 24);.
12bf4 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
12bf5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12bf6 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
12bf7 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
12bf8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12bf9 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 memset(dbFile
12bfa 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Vers, 0, sizeof(
12bfb 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
12bfc 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
12bfd 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e memcmp(pPager->
12bfe 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 dbFileVers, dbFi
12bff 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 leVers, sizeof(d
12c00 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 bFileVers))!=0 )
12c01 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f {. pager_
12c02 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
12c03 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
12c04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12c05 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c >exclusiveMode |
12c06 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d | pPager->state=
12c07 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
12c08 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 . }.. failed:.
12c09 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12c0a 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 OK ){. /* pag
12c0b 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 er_unlock() is a
12c0c 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 no-op for exclu
12c0d 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e sive mode and in
12c0e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
12c0f 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f s. */. pager_
12c10 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a unlock(pPager);.
12c11 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
12c12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
12c13 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
12c14 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 has reached zer
12c15 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 o, rollback any
12c16 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 active.** transa
12c17 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b ction and unlock
12c18 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a the pager..**.*
12c19 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 * Except, in loc
12c1a 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 king_mode=EXCLUS
12c1b 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 IVE when there i
12c1c 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a s nothing to in.
12c1d 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
12c1e 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c journal, the unl
12c1f 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f ock is not perfo
12c20 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 rmed and there i
12c21 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 s.** nothing to
12c22 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 rollback, so thi
12c23 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
12c24 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 o-op..*/ .static
12c25 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 void pagerUnloc
12c26 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 kIfUnused(Pager
12c27 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
12c28 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 (sqlite3PcacheRe
12c29 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
12c2a 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 PCache)==0). &
12c2b 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c & (!pPager->excl
12c2c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 usiveMode || pPa
12c2d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e ger->journalOff>
12c2e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 0) . ){. pag
12c2f 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
12c30 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ack(pPager);. }
12c31 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 .}../*.** Drop a
12c32 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 63 page from the c
12c33 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c 69 74 ache using sqlit
12c34 65 33 50 63 61 63 68 65 44 72 6f 70 28 29 2e 0a e3PcacheDrop()..
12c35 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 65 **.** If this me
12c36 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f ans there are no
12c37 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74 68 20 w no pages with
12c38 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
12c39 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a em, a rollback.*
12c3a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 * occurs and the
12c3b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
12c3c 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76 65 64 abase is removed
12c3d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
12c3e 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 44 pagerDropPage(D
12c3f 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 50 bPage *pPg){. P
12c40 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
12c41 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 71 Pg->pPager;. sq
12c42 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 lite3PcacheDrop(
12c43 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55 6e 6c pPg);. pagerUnl
12c44 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 ockIfUnused(pPag
12c45 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 er);.}../*.** Ac
12c46 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 quire a referenc
12c47 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 e to page number
12c48 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 pgno in pager p
12c49 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a Pager (a page.**
12c4a 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 reference has t
12c4b 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 ype DbPage*). If
12c4c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 the requested r
12c4d 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 eference is .**
12c4e 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 successfully obt
12c4f 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 ained, it is cop
12c50 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 ied to *ppPage a
12c51 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
12c52 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 urned..**.** Thi
12c53 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 s function calls
12c54 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b pagerSharedLock
12c55 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 53 () to obtain a S
12c56 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a HARED lock on.**
12c57 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12c58 6c 65 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63 le if such a loc
12c59 6b 20 6f 72 20 67 72 65 61 74 65 72 20 69 73 20 k or greater is
12c5a 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
12c5b 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 63 61 ..** This may ca
12c5c 75 73 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 use hot-journal
12c5d 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 63 61 rollback or a ca
12c5e 63 68 65 20 70 75 72 67 65 2e 20 53 65 65 20 63 che purge. See c
12c5f 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f 76 65 omments.** above
12c60 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 53 function pagerS
12c61 68 61 72 65 64 4c 6f 63 6b 28 29 20 66 6f 72 20 haredLock() for
12c62 64 65 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 details..**.** I
12c63 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
12c64 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 page is already
12c65 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 in the cache, it
12c66 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a is returned. .*
12c67 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e * Otherwise, a n
12c68 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
12c69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
12c6a 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 populated with d
12c6b 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d ata.** read from
12c6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12c6d 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 le. In some case
12c6e 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f s, the pcache mo
12c6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f dule may.** choo
12c70 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 se not to alloca
12c71 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 te a new page ob
12c72 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 ject and may reu
12c73 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a se an existing.*
12c74 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f * object with no
12c75 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
12c76 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 erences..**.** T
12c77 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61 70 he extra data ap
12c78 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 pended to a page
12c79 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 is always initi
12c7a 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 alized to zeros
12c7b 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 the .** first ti
12c7c 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 me a page is loa
12c7d 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e ded into memory.
12c7e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 71 If the page req
12c7f 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c uested is .** al
12c80 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 ready in the cac
12c81 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e he when this fun
12c82 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
12c83 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a then the extra.
12c84 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 ** data is left
12c85 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 as it was when t
12c86 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 he page object w
12c87 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a as last used..**
12c88 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 .** If the datab
12c89 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 ase image is sma
12c8a 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 ller than the re
12c8b 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 quested page or
12c8c 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 if a .** non-zer
12c8d 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 o value is passe
12c8e 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 d as the noConte
12c8f 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 nt parameter and
12c90 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 the .** request
12c91 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 ed page is not a
12c92 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e lready stored in
12c93 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e the cache, then
12c94 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 no .** actual d
12c95 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e isk read occurs.
12c96 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
12c97 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f e memory image o
12c98 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 f the .** page i
12c99 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f s initialized to
12c9a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a all zeros. .**.
12c9b 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 ** If noContent
12c9c 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e is true, it mean
12c9d 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 s that we do not
12c9e 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 care about the
12c9f 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 contents.** of t
12ca0 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 he page. This oc
12ca1 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 curs in two sepe
12ca2 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a rate scenarios:.
12ca3 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 **.** a) When
12ca4 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c reading a free-l
12ca5 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 ist leaf page fr
12ca6 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c om the database,
12ca7 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 and.**.** b)
12ca8 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 When a savepoint
12ca9 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 is being rolled
12caa 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 back and we nee
12cab 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 d to load.**
12cac 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 a new page int
12cad 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 70 o the cache to p
12cae 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68 65 opulate with the
12caf 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 data read.**
12cb0 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 from the save
12cb1 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a point journal..*
12cb2 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e *.** If noConten
12cb3 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 t is true, then
12cb4 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 the data returne
12cb5 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 d is zeroed inst
12cb6 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 ead of.** being
12cb7 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 read from the da
12cb8 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e tabase. Addition
12cb9 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 ally, the bits c
12cba 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
12cbb 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 to pgno in Pager
12cbc 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 .pInJournal (bit
12cbd 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 vec of pages alr
12cbe 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 eady written to
12cbf 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
12cc0 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 ile) and the Pag
12cc1 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 erSavepoint.pInS
12cc2 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 avepoint bitvecs
12cc3 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 of any open.**
12cc4 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 savepoints are s
12cc5 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 et. This means i
12cc6 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 f the page is ma
12cc7 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 de writable at a
12cc8 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 ny.** point in t
12cc9 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 he future, using
12cca 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
12ccb 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 e3PagerWrite(),
12ccc 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 its contents.**
12ccd 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 will not be jour
12cce 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 naled. This save
12ccf 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s IO..**.** The
12cd0 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 acquisition migh
12cd1 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 t fail for sever
12cd2 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 al reasons. In
12cd3 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e all cases,.** an
12cd4 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 appropriate err
12cd5 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12cd6 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 ned and *ppPage
12cd7 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
12cd8 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 **.** See also s
12cd9 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
12cda 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 p(). Both this
12cdb 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b routine and Look
12cdc 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 up() attempt.**
12cdd 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 to find a page i
12cde 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 n the in-memory
12cdf 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 cache first. If
12ce0 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
12ce1 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d already.** in m
12ce2 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 emory, this rout
12ce3 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b ine goes to disk
12ce4 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 to read it in w
12ce5 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a hereas Lookup().
12ce6 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 ** just returns
12ce7 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0. This routine
12ce8 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64 acquires a read
12ce9 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 -lock the first
12cea 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 time it.** has t
12ceb 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e o go to disk, an
12cec 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 d could also pla
12ced 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 yback an old jou
12cee 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 rnal if necessar
12cef 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b y..** Since Look
12cf0 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 up() never goes
12cf1 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 to disk, it neve
12cf2 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 r has to deal wi
12cf3 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a th locks.** or j
12cf4 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f ournal files..*/
12cf5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12cf6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
12cf7 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 Acquire(. Pager
12cf8 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f *pPager, /
12cf9 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e * The pager open
12cfa 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
12cfb 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 file */. Pgno
12cfc 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f pgno, /
12cfd 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f * Page number to
12cfe 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 fetch */. DbPa
12cff 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 ge **ppPage,
12d00 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 /* Write a point
12d01 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 er to the page h
12d02 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 ere */. int noC
12d03 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 ontent /*
12d04 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 Do not bother re
12d05 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 ading content fr
12d06 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 om disk if true
12d07 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 */.){. PgHdr *p
12d08 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 Pg = 0;. int rc
12d09 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 ;.. assert( ass
12d0a 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
12d0b 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 pPager) );. ass
12d0c 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12d0d 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
12d0e 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 . || sqli
12d0f 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
12d10 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
12d11 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 e)>0 . ||
12d12 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a 20 20 pgno==1. );..
12d13 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 /* The maximum p
12d14 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e age number is 2^
12d15 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 31. Return SQLIT
12d16 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 E_CORRUPT if a p
12d17 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 age. ** number
12d18 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 greater than thi
12d19 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73 20 72 s, or zero, is r
12d1a 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 equested.. */.
12d1b 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f if( pgno>PAGER_
12d1c 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f MAX_PGNO || pgno
12d1d 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 ==0 || pgno==PAG
12d1e 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
12d1f 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e r) ){. return
12d20 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
12d21 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 BKPT;. }.. /*
12d22 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 Make sure we hav
12d23 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20 63 72 e not hit any cr
12d24 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e 0a 20 itical errors..
12d25 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28 20 70 */ . assert( p
12d26 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 2a 70 Pager!=0 );. *p
12d27 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a pPage = 0;.. /*
12d28 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
12d29 66 69 72 73 74 20 70 61 67 65 20 61 63 63 65 73 first page acces
12d2a 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20 sed, then get a
12d2b 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a SHARED lock. **
12d2c 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
12d2d 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72 file. pagerShar
12d2e 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f edLock() is a no
12d2f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64 -op if . ** a d
12d30 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20 atabase lock is
12d31 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20 already held..
12d32 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 */. rc = pagerS
12d33 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 haredLock(pPager
12d34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
12d35 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
12d36 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
12d37 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12d38 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
12d39 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 CK );.. rc = sq
12d3a 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
12d3b 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12d3c 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50 67 29 , pgno, 1, &pPg)
12d3d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
12d3e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
12d3f 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
12d40 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 3d sert( pPg->pgno=
12d41 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 =pgno );. asser
12d42 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d t( pPg->pPager==
12d43 70 50 61 67 65 72 20 7c 7c 20 70 50 67 2d 3e 70 pPager || pPg->p
12d44 50 61 67 65 72 3d 3d 30 20 29 3b 0a 20 20 69 66 Pager==0 );. if
12d45 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 30 ( pPg->pPager==0
12d46 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 ){. /* The p
12d47 61 67 65 72 20 63 61 63 68 65 20 68 61 73 20 63 ager cache has c
12d48 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 reated a new pag
12d49 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e e. Its content n
12d4a 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 eeds to . **
12d4b 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a be initialized..
12d4c 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e */. int n
12d4d 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 Max;. PAGER_I
12d4e 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 NCR(pPager->nMis
12d4f 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 s);. pPg->pPa
12d50 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20 ger = pPager;..
12d51 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
12d52 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
12d53 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 ager, &nMax);.
12d54 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12d55 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
12d56 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
12d57 50 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Pg);. retur
12d58 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
12d59 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 if( nMax<(int)p
12d5a 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 gno || MEMDB ||
12d5b 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 noContent ){.
12d5c 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 if( pgno>pPag
12d5d 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 er->mxPgno ){.
12d5e 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
12d5f 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 erUnref(pPg);.
12d60 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
12d61 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 ITE_FULL;.
12d62 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f }. if( noCo
12d63 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 ntent ){.
12d64 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73 /* Failure to s
12d65 65 74 20 74 68 65 20 62 69 74 73 20 69 6e 20 74 et the bits in t
12d66 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 he InJournal bit
12d67 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69 -vectors is beni
12d68 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 gn.. ** I
12d69 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 t merely means t
12d6a 68 61 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20 hat we might do
12d6b 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20 some extra work
12d6c 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 to journal a .
12d6d 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 ** page th
12d6e 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 at does not need
12d6f 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 to be journaled
12d70 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c . Nevertheless,
12d71 20 62 65 20 73 75 72 65 20 0a 20 20 20 20 20 20 be sure .
12d72 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 ** to test the
12d73 20 63 61 73 65 20 77 68 65 72 65 20 61 20 6d 61 case where a ma
12d74 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 lloc error occur
12d75 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 s while trying t
12d76 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a o set . *
12d77 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62 69 74 * a bit in a bit
12d78 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 vector..
12d79 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 */. sqli
12d7a 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
12d7b 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 lloc();.
12d7c 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 if( pgno<=pPager
12d7d 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a ->dbOrigSize ){.
12d7e 20 20 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e TESTON
12d7f 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 LY( rc = ) sqlit
12d80 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 e3BitvecSet(pPag
12d81 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 er->pInJournal,
12d82 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 pgno);.
12d83 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
12d84 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
12d85 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12d86 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 TESTONLY( rc =
12d87 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 ) addToSavepoint
12d88 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 Bitvecs(pPager,
12d89 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 pgno);. t
12d8a 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c estcase( rc==SQL
12d8b 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
12d8c 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 sqlite3EndB
12d8d 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
12d8e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12d8f 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e memset(pPg->
12d90 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 pData, 0, pPager
12d91 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
12d92 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
12d93 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c CE(("ZERO %p %d\
12d94 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f n", pPager, pgno
12d95 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ));. }else{.
12d96 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 assert( pPg
12d97 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 ->pPager==pPager
12d98 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 );. rc = r
12d99 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a eadDbPage(pPg);.
12d9a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12d9b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12d9c 20 20 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 pagerDropPage
12d9d 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 (pPg);. r
12d9e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
12d9f 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 }. }.#ifdef S
12da0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
12da1 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 S. pPg->pageH
12da2 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 ash = pager_page
12da3 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 hash(pPg);.#endi
12da4 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f f. }else{. /
12da5 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20 * The requested
12da6 70 61 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 page is in the p
12da7 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 age cache. */.
12da8 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 PAGER_INCR(pPa
12da9 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 7d 0a ger->nHit);. }.
12daa 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 67 . *ppPage = pPg
12dab 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
12dac 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
12dad 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66 cquire a page if
12dae 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 it is already i
12daf 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 n the in-memory
12db0 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f cache. Do.** no
12db1 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20 t read the page
12db2 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 from disk. Retu
12db3 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
12db4 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 the page,.** or
12db5 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 0 if the page is
12db6 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41 not in cache. A
12db7 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66 lso, return 0 if
12db8 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 the .** pager i
12db9 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 s in PAGER_UNLOC
12dba 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 K state when thi
12dbb 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
12dbc 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 lled,.** or if t
12dbd 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 he pager is in a
12dbe 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 n error state ot
12dbf 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
12dc0 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 FULL..**.** See
12dc1 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65 also sqlite3Page
12dc2 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66 rGet(). The dif
12dc3 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
12dc4 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
12dc5 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 and sqlite3Pager
12dc6 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67 Get() is that _g
12dc7 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 et() will go to
12dc8 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61 the disk and rea
12dc9 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 d.** in the page
12dca 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
12dcb 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 not already in c
12dcc 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 ache. This rout
12dcd 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e ine.** returns N
12dce 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20 ULL if the page
12dcf 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 is not in cache
12dd0 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f or if a disk I/O
12dd1 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 error .** has e
12dd2 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f ver happened..*/
12dd3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12dd4 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50 DbPage *sqlite3P
12dd5 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 agerLookup(Pager
12dd6 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
12dd7 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 gno){. PgHdr *p
12dd8 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 Pg = 0;. assert
12dd9 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ( pPager!=0 );.
12dda 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 assert( pgno!=0
12ddb 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50 61 67 );.. if( (pPag
12ddc 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 er->state!=PAGER
12ddd 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 20 28 _UNLOCK). && (
12dde 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d pPager->errCode=
12ddf 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
12de0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 ager->errCode==S
12de1 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 29 7b QLITE_FULL). ){
12de2 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
12de3 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e heFetch(pPager->
12de4 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 pPCache, pgno, 0
12de5 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 , &pPg);. }..
12de6 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f return pPg;.}../
12de7 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 *.** Release a p
12de8 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a age reference..*
12de9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 *.** If the numb
12dea 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
12deb 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f to the page dro
12dec 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 p to zero, then
12ded 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 the.** page is a
12dee 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 dded to the LRU
12def 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 list. When all
12df0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c references to al
12df1 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 l pages.** are r
12df2 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 eleased, a rollb
12df3 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 ack occurs and t
12df4 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 he lock on the d
12df5 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 atabase is.** re
12df6 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 moved..*/.SQLITE
12df7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
12df8 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
12df9 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
12dfa 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 if( pPg ){. P
12dfb 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
12dfc 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 Pg->pPager;.
12dfd 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
12dfe 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 ease(pPg);. p
12dff 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
12e00 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ed(pPager);. }.
12e01 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
12e02 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
12e03 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 e has already be
12e04 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 en opened, ensur
12e05 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 e that the.** su
12e06 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 b-journal file i
12e07 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 s open too. If t
12e08 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
12e09 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 is not open,.**
12e0a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
12e0b 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
12e0c 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
12e0d 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 urned if everyth
12e0e 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 ing goes accordi
12e0f 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 ng to plan. .**
12e10 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f An SQLITE_IOERR_
12e11 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 XXX error code i
12e12 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 s returned if a
12e13 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 call to .** sqli
12e14 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c te3OsOpen() fail
12e15 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
12e16 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 openSubJournal(
12e17 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
12e18 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12e19 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 E_OK;. if( isOp
12e1a 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12e1b 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 && !isOpen(pPage
12e1c 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
12e1d 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
12e1e 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
12e1f 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
12e20 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 Y || pPager->sub
12e21 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 jInMemory ){.
12e22 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 sqlite3MemJou
12e23 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d rnalOpen(pPager-
12e24 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 >sjfd);. }els
12e25 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 e{. rc = pa
12e26 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 gerOpentemp(pPag
12e27 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 er, pPager->sjfd
12e28 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 , SQLITE_OPEN_SU
12e29 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d BJOURNAL);. }
12e2a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12e2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
12e2c 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
12e2d 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 ed at the start
12e2e 6f 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74 of every write t
12e2f 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 ransaction..** T
12e30 68 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 here must alread
12e31 79 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20 y be a RESERVED
12e32 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 or EXCLUSIVE loc
12e33 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
12e34 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 e .** file when
12e35 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
12e36 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 called..**.** Op
12e37 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
12e38 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 ile for pager pP
12e39 61 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 ager and write a
12e3a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a journal header.
12e3b 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 ** to the start
12e3c 6f 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 of it. If there
12e3d 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 are active savep
12e3e 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 oints, open the
12e3f 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 sub-journal.** a
12e40 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e s well. This fun
12e41 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 ction is only us
12e42 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 ed when the jour
12e43 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e nal file is bein
12e44 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 g .** opened to
12e45 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b write a rollback
12e46 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 log for a trans
12e47 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f action. It is no
12e48 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 t used .** when
12e49 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f opening a hot jo
12e4a 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f urnal file to ro
12e4b 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a ll it back..**.*
12e4c 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
12e4d 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 file is already
12e4e 20 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 open (as it may
12e4f 20 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 be in exclusive
12e50 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 mode),.** then
12e51 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 this function ju
12e52 73 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 st writes a jour
12e53 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 nal header to th
12e54 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a e start of the.*
12e55 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 * already open f
12e56 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 ile. .**.** Whet
12e57 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a her or not the j
12e58 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f ournal file is o
12e59 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 pened by this fu
12e5a 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 nction, the.** P
12e5b 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ager.pInJournal
12e5c 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 bitvec structure
12e5d 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a is allocated..*
12e5e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
12e5f 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
12e60 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 ing is successfu
12e61 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 l. Otherwise, re
12e62 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f turn .** SQLITE_
12e63 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 NOMEM if the att
12e64 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 empt to allocate
12e65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 Pager.pInJourna
12e66 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 l fails, or .**
12e67 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
12e68 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 if opening or w
12e69 72 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e riting the journ
12e6a 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a al file fails..*
12e6b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12e6c 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 er_open_journal(
12e6d 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
12e6e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12e6f 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
12e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12e71 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
12e72 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 sqlite3_vfs * c
12e73 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 onst pVfs = pPag
12e74 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c er->pVfs; /* L
12e75 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 ocal cache of vf
12e76 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 s pointer */..
12e77 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12e78 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
12e79 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 ERVED );. asser
12e7a 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f t( pPager->useJo
12e7b 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 urnal );. asser
12e7c 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f t( pPager->pInJo
12e7d 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 urnal==0 );. .
12e7e 20 2f 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 /* If already i
12e7f 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
12e80 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
12e81 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a is a no-op. */.
12e82 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 if( pPager->er
12e83 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 rCode ){. ret
12e84 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 urn pPager->errC
12e85 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 ode;. }.. /* T
12e86 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c ODO: Is it reall
12e87 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 y possible to ge
12e88 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69 t here with dbSi
12e89 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e zeValid==0? If n
12e8a 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c ot,. ** the cal
12e8b 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f l to PagerPageco
12e8c 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d unt() can be rem
12e8d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 oved.. */. tes
12e8e 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64 tcase( pPager->d
12e8f 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b bSizeValid==0 );
12e90 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 . sqlite3PagerP
12e91 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
12e92 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 0);.. pPager->
12e93 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c pInJournal = sql
12e94 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
12e95 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
12e96 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
12e97 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b pInJournal==0 ){
12e98 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
12e99 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
12e9a 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 /* Open the jou
12e9b 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 rnal file if it
12e9c 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
12e9d 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 pen. */. if( !i
12e9e 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
12e9f 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 d) ){. if( pP
12ea0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
12ea1 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
12ea2 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
12ea3 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a sqlite3MemJ
12ea4 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 ournalOpen(pPage
12ea5 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c r->jfd);. }el
12ea6 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 se{. const
12ea7 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20 int flags =
12ea8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12ea9 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 VFS flags to op
12eaa 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 en journal file
12eab 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 */. SQLIT
12eac 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
12ead 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 |SQLITE_OPEN_CRE
12eae 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50 ATE|. (pP
12eaf 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f ager->tempFile ?
12eb0 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c . (SQL
12eb1 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
12eb2 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 NCLOSE|SQLITE_OP
12eb3 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 EN_TEMP_JOURNAL)
12eb4 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c :. (SQL
12eb5 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
12eb6 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29 URNAL). )
12eb7 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
12eb8 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
12eb9 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ITE. rc = s
12eba 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
12ebb 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66 n(. pVf
12ebc 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
12ebd 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
12ebe 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 , flags, jrnlBuf
12ebf 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a ferSize(pPager).
12ec0 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 );.#else.
12ec1 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12ec2 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
12ec3 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
12ec4 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
12ec5 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 gs, 0);.#endif.
12ec6 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
12ec7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
12ec8 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
12ec9 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 >jfd) );. }...
12eca 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 /* Write the fi
12ecb 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
12ecc 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 er to the journa
12ecd 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 l file and open
12ece 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f . ** the sub-jo
12ecf 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
12ed0 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 ry.. */. if( r
12ed1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12ed2 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 /* TODO: Che
12ed3 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 ck if all of the
12ed4 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 se are really re
12ed5 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 quired. */. p
12ed6 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
12ed7 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 e = pPager->dbSi
12ed8 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ze;. pPager->
12ed9 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
12eda 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
12edb 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
12edc 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
12edd 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
12ede 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
12edf 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d pPager->setM
12ee0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 aster = 0;. p
12ee1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
12ee2 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 r = 0;. rc =
12ee3 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
12ee4 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 pPager);. }. i
12ee5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12ee6 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 && pPager->nSav
12ee7 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 epoint ){. rc
12ee8 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 = openSubJourna
12ee9 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a l(pPager);. }..
12eea 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12eeb 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
12eec 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
12eed 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
12eee 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
12eef 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
12ef0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12ef1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e ;.}../*.** Begin
12ef2 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
12ef3 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 tion on the spec
12ef4 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 ified pager obje
12ef5 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 ct. If a .** wri
12ef6 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 te-transaction h
12ef7 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
12ef8 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e opened, this fun
12ef9 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
12efa 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
12efb 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 xFlag argument i
12efc 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 s false, then ac
12efd 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 quire at least a
12efe 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 RESERVED.** loc
12eff 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
12f00 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 e file. If exFla
12f01 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
12f02 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 acquire at least
12f03 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 .** an EXCLUSIVE
12f04 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 lock. If such a
12f05 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 lock is already
12f06 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e held, no lockin
12f07 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 g .** functions
12f08 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a need be called..
12f09 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 **.** If this is
12f0a 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 not a temporary
12f0b 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 or in-memory fi
12f0c 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 le and, the jour
12f0d 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 nal file is .**
12f0e 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 opened if it has
12f0f 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 not been alread
12f10 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 y. For a tempora
12f11 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 ry file, the ope
12f12 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 ning .** of the
12f13 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12f14 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 deferred until t
12f15 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 here is an actua
12f16 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 l need to .** wr
12f17 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ite to the journ
12f18 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 al. TODO: Why ha
12f19 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 ndle temporary f
12f1a 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 iles differently
12f1b 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ?.**.** If the j
12f1c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f ournal file is o
12f1d 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 pened (or if it
12f1e 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 is already open)
12f1f 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 , then a.** jour
12f20 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 nal-header is wr
12f21 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 itten to the sta
12f22 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 rt of it..**.**
12f23 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d If the subjInMem
12f24 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ory argument is
12f25 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 non-zero, then a
12f26 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f ny sub-journal o
12f27 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 pened.** within
12f28 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
12f29 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 will be opened
12f2a 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 as an in-memory
12f2b 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 file. This.** ha
12f2c 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 s no effect if t
12f2d 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
12f2e 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 s already opened
12f2f 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 (as it may be w
12f30 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 hen.** running i
12f31 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
12f32 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e ) or if the tran
12f33 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 saction does not
12f34 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 require a.** su
12f35 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 b-journal. If th
12f36 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
12f37 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c rgument is zero,
12f38 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 then any requir
12f39 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 ed.** sub-journa
12f3a 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 l is implemented
12f3b 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 in-memory if pP
12f3c 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 ager is an in-me
12f3d 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a mory database, .
12f3e 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 ** or using a te
12f3f 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 mporary file oth
12f40 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
12f41 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12f42 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
12f43 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
12f44 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 nt exFlag, int s
12f45 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 ubjInMemory){.
12f46 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
12f47 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
12f48 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
12f49 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 ER_UNLOCK );. p
12f4a 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
12f4b 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e ory = (u8)subjIn
12f4c 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 Memory;. if( pP
12f4d 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
12f4e 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 ER_SHARED ){.
12f4f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12f50 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
12f51 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d ;. assert( !M
12f52 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d EMDB && !pPager-
12f53 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 >tempFile );..
12f54 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 /* Obtain a RE
12f55 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SERVED lock on t
12f56 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12f57 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 . If the exFlag
12f58 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a parameter. **
12f59 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 is true, then i
12f5a 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 mmediately upgra
12f5b 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 de this to an EX
12f5c 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 CLUSIVE lock. Th
12f5d 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 e. ** busy-ha
12f5e 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 ndler callback c
12f5f 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 an be used when
12f60 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 upgrading to the
12f61 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a EXCLUSIVE. *
12f62 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 * lock, but not
12f63 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 when obtaining t
12f64 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b he RESERVED lock
12f65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
12f66 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 = sqlite3OsLock(
12f67 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 pPager->fd, RESE
12f68 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 RVED_LOCK);.
12f69 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12f6a 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
12f6b 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
12f6c 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 _RESERVED;.
12f6d 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 if( exFlag ){.
12f6e 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
12f6f 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
12f70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 Pager, EXCLUSIVE
12f71 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a _LOCK);. }.
12f72 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
12f73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
12f74 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73 cks were success
12f75 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 fully obtained,
12f76 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
12f77 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 . ** file and
12f78 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74 write the first
12f79 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
12f7a 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
12f7b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12f7c 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75 _OK && pPager->u
12f7d 73 65 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 seJournal. &
12f7e 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
12f7f 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
12f80 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 RNALMODE_OFF .
12f81 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
12f82 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
12f83 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
12f84 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 }. }else if( is
12f85 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12f86 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ) && pPager->jou
12f87 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 rnalOff==0 ){.
12f88 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
12f89 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 s when the pager
12f8a 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 was in exclusiv
12f8b 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 e-access mode th
12f8c 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 e last. ** ti
12f8d 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 me a (read or wr
12f8e 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e ite) transaction
12f8f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c was successfull
12f90 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 y concluded.
12f91 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 ** by this conne
12f92 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f ction. Instead o
12f93 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a f deleting the j
12f94 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 ournal file it w
12f95 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 as . ** kept
12f96 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 open and either
12f97 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f was truncated to
12f98 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 0 bytes or its
12f99 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a header was. *
12f9a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 * overwritten wi
12f9b 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f th zeros.. */
12f9c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
12f9d 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a ger->nRec==0 );.
12f9e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12f9f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d er->dbOrigSize==
12fa0 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
12fa1 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
12fa2 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 nal==0 );. rc
12fa3 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
12fa4 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
12fa5 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
12fa6 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 (("TRANSACTION %
12fa7 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
12fa8 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 ager)));. asser
12fa9 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 t( !isOpen(pPage
12faa 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 r->jfd) || pPage
12fab 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 r->journalOff>0
12fac 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b || rc!=SQLITE_OK
12fad 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
12fae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 .}../*.** Mark a
12faf 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 single data pag
12fb0 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 e as writeable.
12fb1 54 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 The page is writ
12fb2 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a ten into the .**
12fb3 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 main journal or
12fb4 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 sub-journal as
12fb5 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 required. If the
12fb6 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
12fb7 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 into.** one of
12fb8 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 the journals, th
12fb9 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
12fba 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 bit is set in th
12fbb 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a e .** Pager.pInJ
12fbc 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e ournal bitvec an
12fbd 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 d the PagerSavep
12fbe 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
12fbf 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 t bitvecs.** of
12fc0 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 any open savepoi
12fc1 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 nts as appropria
12fc2 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e te..*/.static in
12fc3 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 t pager_write(Pg
12fc4 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 Hdr *pPg){. voi
12fc5 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e d *pData = pPg->
12fc6 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a pData;. Pager *
12fc7 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
12fc8 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d ager;. int rc =
12fc9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f SQLITE_OK;.. /
12fca 2a 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f * Check for erro
12fcb 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 rs. */. if( pP
12fcc 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
12fcd 20 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 . return pPa
12fce 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 ger->errCode;.
12fcf 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e }. if( pPager->
12fd0 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
12fd1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
12fd2 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 RM;. }.. asser
12fd3 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d t( !pPager->setM
12fd4 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 aster );.. CHEC
12fd5 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 K_PAGE(pPg);..
12fd6 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 /* Mark the page
12fd7 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 as dirty. If t
12fd8 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 he page has alre
12fd9 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
12fda 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 . ** to the jou
12fdb 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e rnal then we can
12fdc 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 return right aw
12fdd 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ay.. */. sqlit
12fde 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 e3PcacheMakeDirt
12fdf 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 y(pPg);. if( pa
12fe0 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
12fe1 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 && !subjRequire
12fe2 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 sPage(pPg) ){.
12fe3 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
12fe4 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 fied = 1;. }els
12fe5 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 e{.. /* If we
12fe6 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 get this far, i
12fe7 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
12fe8 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 page needs to b
12fe9 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e e. ** written
12fea 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 to the transact
12feb 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 ion journal or t
12fec 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f he ckeckpoint jo
12fed 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 urnal. ** or
12fee 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 both.. **.
12fef 20 2a 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 ** First check
12ff0 74 6f 20 73 65 65 20 74 68 61 74 20 74 68 65 20 to see that the
12ff1 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 transaction jour
12ff2 6e 61 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 nal exists and.
12ff3 20 20 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 ** create it
12ff4 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a if it does not..
12ff5 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
12ff6 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
12ff7 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 !=PAGER_UNLOCK )
12ff8 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
12ff9 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 e3PagerBegin(pPa
12ffa 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ger, 0, pPager->
12ffb 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 subjInMemory);.
12ffc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
12ffd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
12ffe 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
12fff 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13000 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
13001 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 20 20 RESERVED );.
13002 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 if( !isOpen(pPag
13003 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 er->jfd) && pPag
13004 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20 er->useJournal.
13005 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67 && pPag
13006 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 er->journalMode!
13007 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
13008 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 DE_OFF ){.
13009 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f rc = pager_open_
1300a 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b journal(pPager);
1300b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1300c 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
1300d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1300e 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
1300f 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f ed = 1;. . /
13010 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f * The transactio
13011 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 n journal now ex
13012 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 ists and we have
13013 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 a RESERVED or a
13014 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 n. ** EXCLUSI
13015 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d VE lock on the m
13016 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
13017 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 e. Write the cu
13018 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 rrent page to.
13019 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 ** the transac
1301a 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 tion journal if
1301b 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 it is not there
1301c 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
1301d 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a if( !pageInJ
1301e 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 ournal(pPg) && i
1301f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13020 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 d) ){. if(
13021 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 pPg->pgno<=pPage
13022 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b r->dbOrigSize ){
13023 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 . u32 cks
13024 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 um;. char
13025 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 *pData2;..
13026 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 /* We should
13027 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 never write to t
13028 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13029 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 the page that.
1302a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e ** contain
1302b 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c s the database l
1302c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f ocks. The follo
1302d 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 wing assert veri
1302e 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 fies. **
1302f 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 that we do not.
13030 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
13031 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 t( pPg->pgno!=PA
13032 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
13033 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 er) );. p
13034 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 Data2 = CODEC2(p
13035 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
13036 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 g->pgno, 7);.
13037 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 cksum = pag
13038 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c er_cksum(pPager,
13039 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 (u8*)pData2);.
1303a 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 rc = writ
1303b 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
1303c 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 jfd, pPager->jou
1303d 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 rnalOff, pPg->pg
1303e 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 no);. if(
1303f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13040 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
13041 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
13042 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 pPager->jfd, pDa
13043 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ta2, pPager->pag
13044 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 eSize,.
13045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13046 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
13047 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 rnalOff + 4);.
13048 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
13049 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 journalOff += pP
1304a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 ager->pageSize+4
1304b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1304c 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1304d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1304e 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 rc = write32b
1304f 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
13050 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13051 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 Off, cksum);.
13052 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
13053 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a ournalOff += 4;.
13054 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13055 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 IOTRACE(("JOUT
13056 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e %p %d %lld %d\n
13057 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e ", pPager, pPg->
13058 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 pgno, .
13059 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
1305a 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 journalOff, pPag
1305b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a er->pageSize));.
1305c 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e PAGER_IN
1305d 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 CR(sqlite3_pager
1305e 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a _writej_count);.
1305f 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 PAGERTRA
13060 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 CE(("JOURNAL %d
13061 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 page %d needSync
13062 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e =%d hash(%08x)\n
13063 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
13064 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
13065 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 pPg->pgno, .
13066 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
13067 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
13068 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 D_SYNC)?1:0), pa
13069 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
1306a 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a )));.. /*
1306b 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f Even if an IO o
1306c 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 r diskfull error
1306d 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 occurred while
1306e 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a journalling the.
1306f 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
13070 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f in the block abo
13071 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 ve, set the need
13072 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 -sync flag for t
13073 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 he page..
13074 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 ** Otherwise, w
13075 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 hen the transact
13076 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
13077 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e ck, the logic in
13078 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 . ** play
13079 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 back_one_page()
1307a 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 will think that
1307b 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 the page needs t
1307c 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 o be restored.
1307d 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 ** in the
1307e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 database file. A
1307f 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f nd if an IO erro
13080 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 r occurs while d
13081 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 oing so,.
13082 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 ** then corrupt
13083 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a ion may follow..
13084 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
13085 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
13086 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 noSync ){.
13087 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c pPg->flags |
13088 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e = PGHDR_NEED_SYN
13089 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 C;. pPa
1308a 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
1308b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 1;. }..
1308c 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f /* An erro
1308d 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 77 r has occurred w
1308e 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f riting to the jo
1308f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 urnal file. The
13090 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e . ** tran
13091 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 saction will be
13092 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 rolled back by t
13093 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a he layer above..
13094 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
13095 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13096 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13097 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
13098 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
13099 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a pPager->nRec++;.
1309a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1309b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
1309c 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 al!=0 );.
1309d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 rc = sqlite3Bit
1309e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
1309f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e InJournal, pPg->
130a0 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 pgno);. t
130a1 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c estcase( rc==SQL
130a2 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
130a3 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d assert( rc=
130a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 =SQLITE_OK || rc
130a5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
130a6 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 ;. rc |=
130a7 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 addToSavepointBi
130a8 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 tvecs(pPager, pP
130a9 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 g->pgno);.
130aa 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
130ab 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
130ac 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
130ad 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
130ae 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
130af 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
130b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
130b1 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f if( !pPager->jo
130b2 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 urnalStarted &&
130b3 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
130b4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 ){. pPg
130b5 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
130b6 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
130b7 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 pPager->ne
130b8 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
130b9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 }. PA
130ba 47 45 52 54 52 41 43 45 28 28 22 41 50 50 45 4e GERTRACE(("APPEN
130bb 44 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 D %d page %d nee
130bc 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 dSync=%d\n",.
130bd 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
130be 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 ERID(pPager), pP
130bf 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 g->pgno,.
130c0 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 ((pPg->f
130c1 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
130c2 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 SYNC)?1:0)));.
130c3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
130c4 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 /* If the sta
130c5 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 tement journal i
130c6 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 s open and the p
130c7 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 age is not in it
130c8 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 ,. ** then wr
130c9 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
130ca 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 page to the stat
130cb 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 ement journal.
130cc 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a Note that. **
130cd 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a the statement j
130ce 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 ournal format di
130cf 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 ffers from the s
130d0 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 tandard journal
130d1 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e format. ** in
130d2 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 that it omits t
130d3 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 he checksums and
130d4 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 the header..
130d5 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a */. if( subj
130d6 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 RequiresPage(pPg
130d7 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
130d8 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 subjournalPage(p
130d9 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a Pg);. }. }..
130da 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
130db 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e database size an
130dc 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 d return.. */.
130dd 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
130de 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 >state>=PAGER_SH
130df 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 ARED );. if( pP
130e0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 ager->dbSize<pPg
130e1 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 ->pgno ){. pP
130e2 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 ager->dbSize = p
130e3 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20 Pg->pgno;. }.
130e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
130e5 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 .** Mark a data
130e6 70 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c page as writeabl
130e7 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 e. This routine
130e8 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 must be called b
130e9 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67 efore .** making
130ea 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61 changes to a pa
130eb 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d ge. The caller m
130ec 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 ust check the re
130ed 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f turn value .** o
130ee 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 f this function
130ef 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e and be careful n
130f0 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 ot to change any
130f1 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 page data unles
130f2 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 s .** this routi
130f3 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ne returns SQLIT
130f4 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 E_OK..**.** The
130f5 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
130f6 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
130f7 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 and pager_write
130f8 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a () is that this.
130f9 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f ** function also
130fa 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 deals with the
130fb 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 special case whe
130fc 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 re 2 or more pag
130fd 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 es.** fit on a s
130fe 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f ingle disk secto
130ff 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 r. In this case
13100 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 all co-resident
13101 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 pages.** must ha
13102 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ve been written
13103 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
13104 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ile before retur
13105 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 ning..**.** If a
13106 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
13107 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 SQLITE_NOMEM or
13108 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
13109 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
1310a 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 as appropriate.
1310b 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 Otherwise, SQLIT
1310c 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
1310d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1310e 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62 te3PagerWrite(Db
1310f 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a Page *pDbPage){.
13110 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13111 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a E_OK;.. PgHdr *
13112 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 pPg = pDbPage;.
13113 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
13114 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
13115 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 Pgno nPagePerSec
13116 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 tor = (pPager->s
13117 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 ectorSize/pPager
13118 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 ->pageSize);..
13119 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 if( nPagePerSect
1311a 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f or>1 ){. Pgno
1311b 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 nPageCount;
1311c 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
1311d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
1311e 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
1311f 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b */. Pgno pg1;
13120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13121 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f /* First page o
13122 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 f the sector pPg
13123 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 is located on.
13124 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 */. int nPage
13125 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13126 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
13127 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 ges starting at
13128 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a pg1 to journal *
13129 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 /. int ii;
1312a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1312b 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1312c 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 */. int needS
1312d 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ync = 0;
1312e 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 /* True if any
1312f 70 61 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e page has PGHDR_N
13130 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 EED_SYNC */..
13131 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f /* Set the doNo
13132 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e tSync flag to 1.
13133 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
13134 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 we cannot allow
13135 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a a journal. *
13136 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 20 77 * header to be w
13137 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74 ritten between t
13138 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c he pages journal
13139 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
1313a 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
1313b 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 assert( !MEMDB )
1313c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
1313d 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d ager->doNotSync=
1313e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 =0 );. pPager
1313f 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b ->doNotSync = 1;
13140 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 .. /* This tr
13141 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 ick assumes that
13142 20 62 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 both the page-s
13143 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 ize and sector-s
13144 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 ize are. ** a
13145 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 n integer power
13146 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 of 2. It sets va
13147 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 riable pg1 to th
13148 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 e identifier.
13149 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 ** of the first
1314a 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 page of the sec
1314b 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 tor pPg is locat
1314c 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ed on.. */.
1314d 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 pg1 = ((pPg->p
1314e 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 gno-1) & ~(nPage
1314f 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 PerSector-1)) +
13150 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 1;.. sqlite3P
13151 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
13152 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 ager, (int *)&nP
13153 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69 ageCount);. i
13154 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 f( pPg->pgno>nPa
13155 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 geCount ){.
13156 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 nPage = (pPg->p
13157 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 gno - pg1)+1;.
13158 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 }else if( (pg1
13159 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d +nPagePerSector-
1315a 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 1)>nPageCount ){
1315b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e . nPage = n
1315c 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b PageCount+1-pg1;
1315d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1315e 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 nPage = nPageP
1315f 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a erSector;. }.
13160 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 assert(nPage
13161 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 >0);. assert(
13162 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b pg1<=pPg->pgno);
13163 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31 . assert((pg1
13164 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e +nPage)>pPg->pgn
13165 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d o);.. for(ii=
13166 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 0; ii<nPage && r
13167 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 c==SQLITE_OK; ii
13168 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 ++){. Pgno
13169 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 pg = pg1+ii;.
1316a 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b PgHdr *pPage;
1316b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 . if( pg==p
1316c 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c Pg->pgno || !sql
1316d 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
1316e 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1316f 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 l, pg) ){.
13170 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f if( pg!=PAGER_
13171 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
13172 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
13173 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
13174 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 t(pPager, pg, &p
13175 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
13176 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13177 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
13178 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
13179 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 te(pPage);.
1317a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
1317b 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
1317c 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 ED_SYNC ){.
1317d 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e needSyn
1317e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 c = 1;.
1317f 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 assert(pPag
13180 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 er->needSync);.
13181 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
13182 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
13183 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 PagerUnref(pPage
13184 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
13185 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
13186 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 65 20 else if( (pPage
13187 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 = pager_lookup(p
13188 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 Pager, pg))!=0 )
13189 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
1318a 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 age->flags&PGHDR
1318b 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 _NEED_SYNC ){.
1318c 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 needSync
1318d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
1318e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
1318f 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 agerUnref(pPage)
13190 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
13191 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 . /* If the P
13192 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
13193 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 lag is set for a
13194 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 ny of the nPage
13195 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 pages . ** st
13196 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 arting at pg1, t
13197 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 hen it needs to
13198 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f be set for all o
13199 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a f them. Because.
1319a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 ** writing t
1319b 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e o any of these n
1319c 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 Page pages may d
1319d 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 amage the others
1319e 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 , the. ** jou
1319f 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 rnal file must c
131a0 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 ontain sync()ed
131a1 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 copies of all of
131a2 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 them. ** bef
131a3 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 ore any of them
131a4 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f can be written o
131a5 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
131a6 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a se file.. */.
131a7 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 if( needSync
131a8 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
131a9 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 ( !MEMDB && pPag
131aa 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b er->noSync==0 );
131ab 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b . for(ii=0;
131ac 20 69 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 65 ii<nPage && nee
131ad 64 53 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 dSync; ii++){.
131ae 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 PgHdr *pPa
131af 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 ge = pager_looku
131b0 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 p(pPager, pg1+ii
131b1 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
131b2 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
131b3 20 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c pPage->flags |
131b4 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e = PGHDR_NEED_SYN
131b5 43 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c C;. sql
131b6 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
131b7 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d Page);. }
131b8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
131b9 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 ssert(pPager->ne
131ba 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a edSync);. }..
131bb 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
131bc 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 er->doNotSync==1
131bd 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e );. pPager->
131be 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 doNotSync = 0;.
131bf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
131c0 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 pager_write(pDb
131c1 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
131c2 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
131c3 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
131c4 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 the page given i
131c5 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 n the argument w
131c6 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 as previously pa
131c7 73 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ssed.** to sqlit
131c8 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 e3PagerWrite().
131c9 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
131ca 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 return TRUE if
131cb 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 it is ok.** to c
131cc 68 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e hange the conten
131cd 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a t of the page..*
131ce 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
131cf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
131d0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
131d1 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 Iswriteable(DbPa
131d2 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 ge *pPg){. retu
131d3 72 6e 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 rn pPg->flags&PG
131d4 48 44 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e HDR_DIRTY;.}.#en
131d5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c dif../*.** A cal
131d6 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e l to this routin
131d7 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65 e tells the page
131d8 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 r that it is not
131d9 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a necessary to.**
131da 20 77 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72 write the infor
131db 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 mation on page p
131dc 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 Pg back to the d
131dd 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 isk, even though
131de 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69 .** that page mi
131df 67 68 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ght be marked as
131e0 20 64 69 72 74 79 2e 20 20 54 68 69 73 20 68 61 dirty. This ha
131e1 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 ppens, for examp
131e2 6c 65 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 le, when.** the
131e3 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 61 64 page has been ad
131e4 64 65 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66 ded as a leaf of
131e5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e the freelist an
131e6 64 20 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 d so its.** cont
131e7 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 ent no longer ma
131e8 74 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tters..**.** The
131e9 20 6f 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 overlying softw
131ea 61 72 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 are layer calls
131eb 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 this routine whe
131ec 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 n all of the dat
131ed 61 0a 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 a.** on the give
131ee 6e 20 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 n page is unused
131ef 2e 20 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b . The pager mark
131f0 73 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c s the page as cl
131f1 65 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 ean so.** that i
131f2 74 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 t does not get w
131f3 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a ritten to disk..
131f4 2a 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 **.** Tests show
131f5 20 74 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d that this optim
131f6 69 7a 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 ization can quad
131f7 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 ruple the speed
131f8 6f 66 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c of large .** DEL
131f9 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a ETE operations..
131fa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
131fb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
131fc 67 65 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 gerDontWrite(PgH
131fd 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
131fe 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
131ff 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28 >pPager;. if( (
13200 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
13201 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 67 65 _DIRTY) && pPage
13202 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 r->nSavepoint==0
13203 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 ){. PAGERTRA
13204 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 CE(("DONT_WRITE
13205 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 page %d of %d\n"
13206 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 , pPg->pgno, PAG
13207 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a ERID(pPager)));.
13208 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c IOTRACE(("CL
13209 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 EAN %p %d\n", pP
1320a 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 ager, pPg->pgno)
1320b 29 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 ). pPg->flags
1320c 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 |= PGHDR_DONT_W
1320d 52 49 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c RITE;.#ifdef SQL
1320e 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
1320f 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
13210 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
13211 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
13212 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
13213 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
13214 6c 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 led to increment
13215 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
13216 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13217 0a 2a 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 .** change-count
13218 65 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 er, stored as a
13219 34 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 4-byte big-endia
1321a 6e 20 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 n integer starti
1321b 6e 67 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f ng at .** byte o
1321c 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 ffset 24 of the
1321d 70 61 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a pager file..**.*
1321e 2a 20 49 66 20 74 68 65 20 69 73 44 69 72 65 63 * If the isDirec
1321f 74 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 t flag is zero,
13220 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e then this is don
13221 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a e by calling .**
13222 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
13223 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 te() on page 1,
13224 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 then modifying t
13225 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
13226 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e he.** page data.
13227 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
13228 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 e file will be u
13229 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 pdated when the
1322a 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 current.** trans
1322b 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
1322c 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ted..**.** The i
1322d 73 44 69 72 65 63 74 20 66 6c 61 67 20 6d 61 79 sDirect flag may
1322e 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 only be non-zer
1322f 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 o if the library
13230 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a was compiled.**
13231 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 with the SQLITE
13232 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
13233 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e RITE macro defin
13234 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
13235 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 ,.** if isDirect
13236 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
13237 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
13238 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 file is updated
13239 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 directly.** by w
1323a 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 riting an update
1323b 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 d version of pag
1323c 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c e 1 using a call
1323d 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 to the .** sqli
1323e 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e te3OsWrite() fun
1323f 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
13240 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f int pager_incr_
13241 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 changecounter(Pa
13242 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
13243 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a isDirectMode){.
13244 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13245 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c E_OK;.. /* Decl
13246 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 are and initiali
13247 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 ze constant inte
13248 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 ger 'isDirect'.
13249 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d If the. ** atom
1324a 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a ic-write optimiz
1324b 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 ation is enabled
1324c 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 in this build,
1324d 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 then isDirect.
1324e 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 ** is initialize
1324f 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 d to the value p
13250 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 assed as the isD
13251 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 irectMode parame
13252 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 ter. ** to this
13253 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 function. Other
13254 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 wise, it is alwa
13255 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a ys set to zero..
13256 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 **. ** The id
13257 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 ea is that if th
13258 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f e atomic-write o
13259 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e ptimization is n
1325a 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 ot. ** enabled
1325b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c at compile time,
1325c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 the compiler ca
1325d 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 n omit the tests
1325e 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 of. ** 'isDire
1325f 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 ct' below, as we
13260 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 ll as the block
13261 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a enclosed in the.
13262 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 ** "if( isDire
13263 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e ct )" condition.
13264 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
13265 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
13266 49 43 5f 57 52 49 54 45 0a 20 20 63 6f 6e 73 74 IC_WRITE. const
13267 20 69 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20 int isDirect =
13268 30 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 0;. assert( isD
13269 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a irectMode==0 );.
1326a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1326b 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 ER(isDirectMode)
1326c 3b 0a 23 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 ;.#else. const
1326d 69 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20 69 int isDirect = i
1326e 73 44 69 72 65 63 74 4d 6f 64 65 3b 0a 23 65 6e sDirectMode;.#en
1326f 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 dif.. assert( p
13270 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
13271 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
13272 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 if( !pPager->c
13273 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 hangeCountDone &
13274 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 & pPager->dbSize
13275 3e 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 >0 ){. PgHdr
13276 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 *pPgHdr;
13277 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 /* Refer
13278 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a ence to page 1 *
13279 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 65 /. u32 change
1327a 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20 _counter;
1327b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76 /* Initial v
1327c 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63 alue of change-c
1327d 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a ounter field */.
1327e 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 . assert( !pP
1327f 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 ager->tempFile &
13280 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
13281 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 >fd) );.. /*
13282 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74 Open page 1 of t
13283 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74 he file for writ
13284 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d ing. */. rc =
13285 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
13286 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67 (pPager, 1, &pPg
13287 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Hdr);. assert
13288 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 ( pPgHdr==0 || r
13289 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
1328a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 . /* If page
1328b 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 20 one was fetched
1328c 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e successfully, an
1328d 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 d this function
1328e 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70 is not. ** op
1328f 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63 erating in direc
13290 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67 t-mode, make pag
13291 65 20 31 20 77 72 69 74 61 62 6c 65 2e 0a 20 20 e 1 writable..
13292 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
13293 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 69 =SQLITE_OK && !i
13294 73 44 69 72 65 63 74 20 29 7b 0a 20 20 20 20 20 sDirect ){.
13295 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13296 65 72 57 72 69 74 65 28 70 50 67 48 64 72 29 3b erWrite(pPgHdr);
13297 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
13298 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13299 0a 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d . /* Increm
1329a 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 ent the value ju
1329b 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 st read and writ
1329c 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 e it back to byt
1329d 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 e 24. */. c
1329e 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 hange_counter =
1329f 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
132a0 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 (u8*)pPager->dbF
132a1 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20 ileVers);.
132a2 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b change_counter++
132a3 3b 0a 20 20 20 20 20 20 70 75 74 33 32 62 69 74 ;. put32bit
132a4 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 s(((char*)pPgHdr
132a5 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 ->pData)+24, cha
132a6 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 nge_counter);..
132a7 20 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 /* If runni
132a8 6e 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 ng in direct mod
132a9 65 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e e, write the con
132aa 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20 tents of page 1
132ab 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a to the file. */.
132ac 20 20 20 20 20 20 69 66 28 20 69 73 44 69 72 65 if( isDire
132ad 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f ct ){. co
132ae 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d nst void *zBuf =
132af 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a pPgHdr->pData;.
132b0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
132b1 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
132b2 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 ze>0 );.
132b3 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
132b4 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
132b5 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 zBuf, pPager->pa
132b6 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 geSize, 0);.
132b7 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 }.. /* If
132b8 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
132b9 65 64 2c 20 73 65 74 20 74 68 65 20 63 68 61 6e ed, set the chan
132ba 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
132bb 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 . */. if( r
132bc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
132bd 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
132be 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
132bf 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 1;. }.
132c0 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 }.. /* Relea
132c1 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66 65 se the page refe
132c2 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 rence. */. sq
132c3 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
132c4 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 pPgHdr);. }. r
132c5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
132c6 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 ** Sync the page
132c7 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 r file to disk.
132c8 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
132c9 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 for in-memory fi
132ca 6c 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 les.** or pages
132cb 77 69 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e with the Pager.n
132cc 6f 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a oSync flag set..
132cd 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 **.** If success
132ce 66 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f ful, or called o
132cf 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 n a pager for wh
132d0 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f ich it is a no-o
132d1 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 p, this.** funct
132d2 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ion returns SQLI
132d3 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 TE_OK. Otherwise
132d4 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f , an IO error co
132d5 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
132d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
132d7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
132d8 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 erSync(Pager *pP
132d9 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b ager){. int rc;
132da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
132dc 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
132dd 20 20 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 70 if( MEMDB || p
132de 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b Pager->noSync ){
132df 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
132e0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
132e1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
132e2 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c Sync(pPager->fd,
132e3 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
132e4 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ags);. }. retu
132e5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
132e6 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 Sync the databas
132e7 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 e file for the p
132e8 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 ager pPager. zMa
132e9 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ster points to t
132ea 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 he name.** of a
132eb 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
132ec 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ile that should
132ed 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
132ee 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a the individual.*
132ef 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 * journal file.
132f0 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e zMaster may be N
132f1 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e ULL, which is in
132f2 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 terpreted as no
132f3 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 master.** journa
132f4 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 l (a single data
132f5 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
132f6 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f )..**.** This ro
132f7 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 utine ensures th
132f8 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 at:.**.** * Th
132f9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
132fa 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 change-counter i
132fb 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20 s updated,.**
132fc 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 * the journal is
132fd 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20 synced (unless
132fe 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 the atomic-write
132ff 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
13300 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 used),.** * a
13301 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 61 ll dirty pages a
13302 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 re written to th
13303 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
13304 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 .** * the dat
13305 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 72 abase file is tr
13306 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 71 75 uncated (if requ
13307 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 ired), and.**
13308 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
13309 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a ile synced. .**.
1330a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e ** The only thin
1330b 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 g that remains t
1330c 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 o commit the tra
1330d 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66 nsaction is to f
1330e 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c inalize .** (del
1330f 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72 ete, truncate or
13310 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 74 20 zero the first
13311 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75 part of) the jou
13312 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a rnal file (or .*
13313 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 * delete the mas
13314 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
13315 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a if specified)..
13316 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
13317 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c if zMaster==NULL
13318 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 , this does not
13319 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 76 overwrite a prev
1331a 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61 ious value.** pa
1331b 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74 ssed to an sqlit
1331c 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
1331d 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a seOne() call..**
1331e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c .** If the final
1331f 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 parameter - noS
13320 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 ync - is true, t
13321 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
13322 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 file itself.**
13323 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 is not synced. T
13324 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 he caller must c
13325 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 all sqlite3Pager
13326 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 Sync() directly
13327 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 to.** sync the d
13328 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 atabase file bef
13329 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d ore calling Comm
1332a 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 itPhaseTwo() to
1332b 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f delete the.** jo
1332c 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 urnal file in th
1332d 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 is case..*/.SQLI
1332e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1332f 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
13330 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 tPhaseOne(. Pag
13331 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
13332 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13333 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
13334 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d const char *zM
13335 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 aster,
13336 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c /* If not NULL
13337 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 , the master jou
13338 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 rnal name */. i
13339 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20 nt noSync
1333a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1333b 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74 * True to omit t
1333c 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20 he xSync on the
1333d 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 db file */.){.
1333e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1333f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
13340 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
13341 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d /.. if( pPager-
13342 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
13343 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
13344 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 50 rrCode;. }.. P
13345 41 47 45 52 54 52 41 43 45 28 28 22 44 41 54 41 AGERTRACE(("DATA
13346 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d BASE SYNC: File=
13347 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 %s zMaster=%s nS
13348 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 ize=%d\n", .
13349 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e pPager->zFilen
1334a 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 ame, zMaster, pP
1334b 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a ager->dbSize));.
1334c 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 . /* If this is
1334d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 an in-memory db
1334e 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 , or no pages ha
1334f 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ve been written
13350 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a to, or this. **
13351 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c function has al
13352 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 ready been calle
13353 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 d, it is a no-op
13354 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d .. */. if( MEM
13355 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 DB && pPager->db
13356 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 Modified ){.
13357 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
13358 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 tart(pPager->pBa
13359 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 ckup);. }else i
1335a 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
1335b 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 !=PAGER_SYNCED &
1335c 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 & pPager->dbModi
1335d 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 fied ){.. /*
1335e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
1335f 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 ock updates the
13360 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 change-counter.
13361 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 Exactly how it.
13362 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 ** does this
13363 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 depends on wheth
13364 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 er or not the at
13365 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 omic-update opti
13366 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 mization. **
13367 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 was enabled at c
13368 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 ompile time, and
13369 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 if this transac
1336a 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a tion meets the .
1336b 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 ** runtime c
1336c 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 riteria to use t
1336d 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 he operation: .
1336e 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
1336f 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 * The file-syste
13370 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 m supports the a
13371 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 tomic-write prop
13372 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 erty for. **
13373 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 blocks of s
13374 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 ize page-size, a
13375 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 nd . ** *
13376 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e This commit is n
13377 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c ot part of a mul
13378 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 ti-file transact
13379 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 ion, and. **
1337a 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 * Exactly one
1337b 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d page has been m
1337c 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 odified and stor
1337d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
1337e 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 file.. **.
1337f 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 ** If the opti
13380 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 mization was not
13381 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
13382 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 ile time, then t
13383 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f he. ** pager_
13384 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13385 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 er() function is
13386 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 called to updat
13387 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 e the change.
13388 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 ** counter in '
13389 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 indirect-mode'.
1338a 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 If the optimizat
1338b 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ion is compiled
1338c 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 in but. ** is
1338d 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 not applicable
1338e 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 to this transact
1338f 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 ion, call sqlite
13390 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 3JournalCreate()
13391 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 . ** to make
13392 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c sure the journal
13393 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c file has actual
13394 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c ly been created,
13395 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a then call. *
13396 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 * pager_incr_cha
13397 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
13398 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
13399 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 e-counter in ind
1339a 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 irect. ** mod
1339b 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a e. . **. *
1339c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
1339d 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
1339e 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 is both enabled
1339f 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c and applicable,
133a0 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c . ** then cal
133a1 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 l pager_incr_cha
133a2 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
133a3 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
133a4 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a e-counter. **
133a5 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 in 'direct' mod
133a6 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
133a7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
133a8 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 will never be.
133a9 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f ** created fo
133aa 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 r this transacti
133ab 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 on.. */.#ifde
133ac 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
133ad 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
133ae 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 PgHdr *pPg;.
133af 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
133b0 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
133b1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
133b2 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
133b3 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 ALMODE_OFF );.
133b4 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 if( !zMaster &
133b5 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
133b6 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 >jfd) . && p
133b7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
133b8 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a f==jrnlBufferSiz
133b9 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 e(pPager) .
133ba 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a && pPager->dbSiz
133bb 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c e>=pPager->dbFil
133bc 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 eSize. && (0
133bd 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 ==(pPg = sqlite3
133be 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
133bf 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
133c0 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 ) || 0==pPg->pDi
133c1 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 rty). ){.
133c2 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
133c3 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 db file change c
133c4 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 ounter via the d
133c5 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 irect-write meth
133c6 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a od. The . *
133c7 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c * following call
133c8 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 will modify the
133c9 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
133ca 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 sentation of pag
133cb 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f e 1 . ** to
133cc 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 include the upd
133cd 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e ated change coun
133ce 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 ter and then wri
133cf 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 te page 1 .
133d0 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ** directly to
133d1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
133d2 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 e. Because of th
133d3 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a e atomic-write .
133d4 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 ** propert
133d5 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 y of the host fi
133d6 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 le-system, this
133d7 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a is safe.. *
133d8 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 /. rc = pag
133d9 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
133da 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 unter(pPager, 1)
133db 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
133dc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a rc = sqlite3J
133dd 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 ournalCreate(pPa
133de 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
133df 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
133e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
133e1 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
133e2 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
133e3 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a er, 0);. }.
133e4 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
133e5 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
133e6 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
133e7 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 ager, 0);.#endif
133e8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
133e9 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
133ea 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
133eb 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 xit;.. /* If
133ec 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
133ed 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 has made the da
133ee 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 tabase smaller,
133ef 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 then all pages.
133f0 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 ** being disc
133f1 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75 arded by the tru
133f2 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 ncation must be
133f3 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
133f4 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
133f5 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c le. This can onl
133f6 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f y happen in auto
133f7 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 -vacuum mode..
133f8 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f **. ** Befo
133f9 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 re reading the p
133fa 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e ages with page n
133fb 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 umbers larger th
133fc 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 an the . ** c
133fd 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
133fe 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 Pager.dbSize, se
133ff 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f t dbSize back to
13400 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a the value. *
13401 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 * that it took a
13402 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
13403 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
13404 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 Otherwise, the.
13405 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 ** calls to s
13406 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
13407 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 return zeroed p
13408 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 ages instead of
13409 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
1340a 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
1340b 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
1340c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
1340d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1340e 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 UM. if( pPage
1340f 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72 r->dbSize<pPager
13410 2d 3e 64 62 4f 72 69 67 53 69 7a 65 0a 20 20 20 ->dbOrigSize.
13411 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 && pPager->jou
13412 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
13413 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
13414 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67 . ){. Pg
13415 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 no i;
13416 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13417 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
13418 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a tor variable */.
13419 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f const Pgno
1341a 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d iSkip = PAGER_M
1341b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20 J_PGNO(pPager);
1341c 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 /* Pending lock
1341d 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f page */. co
1341e 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 nst Pgno dbSize
1341f 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 = pPager->dbSize
13420 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 ; /* Datab
13421 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a ase image size *
13422 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d / . pPager-
13423 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 >dbSize = pPager
13424 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 ->dbOrigSize;.
13425 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a for( i=dbSiz
13426 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e e+1; i<=pPager->
13427 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 dbOrigSize; i++
13428 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 ){. if( !
13429 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
1342a 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 t(pPager->pInJou
1342b 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 rnal, i) && i!=i
1342c 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 Skip ){.
1342d 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20 PgHdr *pPage;
1342e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1342f 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a age to journal *
13430 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
13431 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
13432 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 (pPager, i, &pPa
13433 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
13434 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13435 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
13436 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 hase_one_exit;.
13437 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
13438 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13439 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 pPage);.
1343a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
1343b 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ref(pPage);.
1343c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1343d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
1343e 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
1343f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a exit;. }.
13440 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70 } . p
13441 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
13442 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65 dbSize;. }.#e
13443 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69 ndif.. /* Wri
13444 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
13445 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 urnal name into
13446 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13447 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20 . If a master .
13448 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ** journal fi
13449 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 le name has alre
1344a 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
1344b 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
1344c 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72 file, . ** or
1344d 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e if zMaster is N
1344e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a ULL (no master j
1344f 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 ournal), then th
13450 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
13451 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 op.. */. r
13452 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a c = writeMasterJ
13453 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a ournal(pPager, z
13454 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 Master);. if(
13455 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13456 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
13457 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 se_one_exit;..
13458 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f /* Sync the jo
13459 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 urnal file. If t
1345a 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 he atomic-update
1345b 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
1345c 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 being. ** us
1345d 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69 ed, this call wi
1345e 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68 ll not create th
1345f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
13460 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20 r perform any.
13461 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20 ** real IO..
13462 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79 */. rc = sy
13463 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 ncJournal(pPager
13464 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
13465 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
13466 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 commit_phase_one
13467 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 _exit;.. /* W
13468 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 rite all dirty p
13469 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 ages to the data
1346a 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 base file. */.
1346b 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
1346c 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69 te_pagelist(sqli
1346d 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 te3PcacheDirtyLi
1346e 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 st(pPager->pPCac
1346f 68 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 he));. if( rc
13470 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
13471 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 assert( rc!
13472 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c =SQLITE_IOERR_BL
13473 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67 OCKED );. g
13474 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 oto commit_phase
13475 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d _one_exit;. }
13476 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
13477 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 heCleanAll(pPage
13478 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 r->pPCache);..
13479 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
1347a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 on disk is not
1347b 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 the same size as
1347c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
1347d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e age,. ** then
1347e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 use pager_trunc
1347f 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 ate to grow or s
13480 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 hrink the file h
13481 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ere.. */.
13482 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
13483 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 ze!=pPager->dbFi
13484 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
13485 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67 Pgno nNew = pPag
13486 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50 er->dbSize - (pP
13487 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41 ager->dbSize==PA
13488 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
13489 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 er));. asse
1348a 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
1348b 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e>=PAGER_EXCLUSI
1348c 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d VE );. rc =
1348d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
1348e 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 pPager, nNew);.
1348f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13490 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
13491 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
13492 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 xit;. }..
13493 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 /* Finally, sync
13494 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13495 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 le. */. if( !
13496 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 pPager->noSync &
13497 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 & !noSync ){.
13498 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
13499 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 sSync(pPager->fd
1349a 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 , pPager->sync_f
1349b 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 lags);. }.
1349c 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e IOTRACE(("DBSYN
1349d 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 C %p\n", pPager)
1349e 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 ).. pPager->s
1349f 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e tate = PAGER_SYN
134a0 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74 CED;. }..commit
134a1 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a _phase_one_exit:
134a2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
134a3 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 E_IOERR_BLOCKED
134a4 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f ){. /* pager_
134a5 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
134a6 65 72 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 er() may attempt
134a7 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 to obtain an ex
134a8 63 6c 75 73 69 76 65 0a 20 20 20 20 2a 2a 20 6c clusive. ** l
134a9 6f 63 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 ock to spill the
134aa 20 63 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 cache and retur
134ab 6e 20 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e n IOERR_BLOCKED.
134ac 20 42 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 But since .
134ad 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 ** there is no c
134ae 68 61 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 hance the cache
134af 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c is inconsistent,
134b0 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 62 65 it is. ** be
134b1 74 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 tter to return S
134b2 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 QLITE_BUSY..
134b3 2a 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c **/. rc = SQL
134b4 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 ITE_BUSY;. }.
134b5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
134b6 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 *.** When this f
134b7 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
134b8 64 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 d, the database
134b9 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f file has been co
134ba 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61 mpletely.** upda
134bb 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74 ted to reflect t
134bc 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 he changes made
134bd 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 by the current t
134be 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a ransaction and.*
134bf 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b * synced to disk
134c0 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 . The journal fi
134c1 6c 65 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 le still exists
134c2 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
134c3 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61 em .** though, a
134c4 6e 64 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 nd if a failure
134c5 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 20 70 occurs at this p
134c6 6f 69 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65 oint it will eve
134c7 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 ntually.** be us
134c8 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 ed as a hot-jour
134c9 6e 61 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72 nal and the curr
134ca 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
134cb 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
134cc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
134cd 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a finalizes the j
134ce 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74 ournal file, eit
134cf 68 65 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c her by deleting,
134d0 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20 .** truncating
134d1 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72 or partially zer
134d2 6f 69 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74 oing it, so that
134d3 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 it cannot be us
134d4 65 64 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a ed .** for hot-j
134d5 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e ournal rollback.
134d6 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f Once this is do
134d7 6e 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ne the transacti
134d8 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63 on is.** irrevoc
134d9 61 62 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a ably committed..
134da 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
134db 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 r occurs, an IO
134dc 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
134dd 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 turned and the p
134de 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e ager.** moves in
134df 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 to the error sta
134e0 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 te. Otherwise, S
134e1 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
134e2 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
134e3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
134e4 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
134e5 61 73 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 aseTwo(Pager *pP
134e6 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
134e7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
134e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
134e9 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
134ea 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 70 72 6f . /* Do not pro
134eb 63 65 65 64 20 69 66 20 74 68 65 20 70 61 67 65 ceed if the page
134ec 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 r is already in
134ed 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e the error state.
134ee 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
134ef 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 ->errCode ){.
134f0 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
134f1 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 errCode;. }..
134f2 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
134f3 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 should not be c
134f4 61 6c 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 alled if the pag
134f5 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 er is not in at
134f6 6c 65 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 least. ** PAGER
134f7 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2e _RESERVED state.
134f8 20 41 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 And indeed SQLi
134f9 74 65 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 te never does th
134fa 69 73 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 is. But it is.
134fb 2a 2a 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 ** nice to have
134fc 74 68 69 73 20 64 65 66 65 6e 73 69 76 65 20 62 this defensive b
134fd 6c 6f 63 6b 20 68 65 72 65 20 61 6e 79 77 61 79 lock here anyway
134fe 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 .. */. if( NEV
134ff 45 52 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 ER(pPager->state
13500 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 29 <PAGER_RESERVED)
13501 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
13502 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
13503 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 .. /* An optimi
13504 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 zation. If the d
13505 61 74 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 atabase was not
13506 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 actually modifie
13507 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 d during. ** th
13508 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 is transaction,
13509 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e the pager is run
1350a 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 ning in exclusiv
1350b 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 e-mode and is.
1350c 2a 2a 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 ** using persist
1350d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 ent journals, th
1350e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1350f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a is a no-op.. *
13510 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 *. ** The start
13511 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
13512 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 file currently c
13513 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 ontains a single
13514 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 journal . ** h
13515 65 61 64 65 72 20 77 69 74 68 20 74 68 65 20 6e eader with the n
13516 52 65 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f Rec field set to
13517 20 30 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 0. If such a jo
13518 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 urnal is used as
13519 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 . ** a hot-jour
1351a 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a nal during hot-j
1351b 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c ournal rollback,
1351c 20 30 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 0 changes will
1351d 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 be made. ** to
1351e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1351f 65 2e 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e e. So there is n
13520 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 o need to zero t
13521 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a he journal . **
13522 20 68 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 header. Since t
13523 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 he pager is in e
13524 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 xclusive mode, t
13525 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a here is no need.
13526 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 ** to drop any
13527 20 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 locks either..
13528 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
13529 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 ->dbModified==0
1352a 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 && pPager->exclu
1352b 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 siveMode . &&
1352c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
1352d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
1352e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 ALMODE_PERSIST.
1352f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
13530 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13531 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
13532 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
13533 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13534 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 OK;. }.. PAGER
13535 54 52 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 TRACE(("COMMIT %
13536 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
13537 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 ager)));. asser
13538 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
13539 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c ==PAGER_SYNCED |
1353a 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 | MEMDB || !pPag
1353b 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 er->dbModified )
1353c 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 ;. rc = pager_e
1353d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
1353e 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 Pager, pPager->s
1353f 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 etMaster);. ret
13540 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 urn pager_error(
13541 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a pPager, rc);.}..
13542 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 /*.** Rollback a
13543 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 ll changes. The
13544 64 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 database falls b
13545 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 ack to PAGER_SHA
13546 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 RED mode..**.**
13547 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 This function pe
13548 72 66 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 rforms two tasks
13549 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 :.**.** 1) It
1354a 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a rolls back the j
1354b 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 ournal file, res
1354c 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 toring all datab
1354d 61 73 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a ase file and .**
1354e 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 in-memory
1354f 63 61 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 cache pages to t
13550 68 65 20 73 74 61 74 65 20 74 68 65 79 20 77 65 he state they we
13551 72 65 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 re in when the t
13552 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 ransaction.**
13553 20 20 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 was opened, a
13554 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 nd.** 2) It fi
13555 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 nalizes the jour
13556 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 nal file, so tha
13557 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 t it is not used
13558 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 for hot.**
13559 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 rollback at any
1355a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 point in the fu
1355b 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a ture..**.** subj
1355c 65 63 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ect to the follo
1355d 77 69 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 wing qualificati
1355e 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 ons:.**.** * If
1355f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13560 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e is not yet open
13561 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
13562 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a ion is called,.*
13563 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 * then only (2
13564 29 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 ) is performed.
13565 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
13566 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c re is no journal
13567 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f file.** to ro
13568 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a ll back..**.** *
13569 20 49 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 If in an error
1356a 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e state other than
1356b 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 SQLITE_FULL, th
1356c 65 6e 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a en task (1) is .
1356d 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 ** performed.
1356e 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 If successful, t
1356f 61 73 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c ask (2). Regardl
13570 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f ess of the outco
13571 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 me.** of eithe
13572 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 r, the error sta
13573 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
13574 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
13575 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e caller.** (i.
13576 65 2e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 e. either SQLITE
13577 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 _IOERR or SQLITE
13578 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a _CORRUPT)..**.**
13579 20 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 * If the pager
1357a 69 73 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 is in PAGER_RESE
1357b 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e RVED state, then
1357c 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 attempt (1). Wh
1357d 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f ether.** or no
1357e 74 20 28 31 29 20 69 73 20 73 75 63 63 75 73 73 t (1) is succuss
1357f 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 ful, also attemp
13580 74 20 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 t (2). If succes
13581 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 sful, return.**
13582 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 SQLITE_OK. Oth
13583 65 72 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 erwise, enter th
13584 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e e error state an
13585 64 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 d return the fir
13586 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 st .** error c
13587 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e ode encountered.
13588 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 .**.** In thi
13589 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 s case there is
1358a 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 no chance that t
1358b 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1358c 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 written to. .**
1358d 20 20 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 So is safe to
1358e 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 finalize the jou
1358f 72 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 rnal file even i
13590 66 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a f the playback .
13591 2a 2a 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 ** (operation
13592 31 29 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 1) failed. Howev
13593 65 72 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 er the pager mus
13594 74 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f t enter the erro
13595 72 20 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 r state.** as
13596 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
13597 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 the in-memory ca
13598 63 68 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 che are now susp
13599 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e ect..**.** * Fin
1359a 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 ally, if in PAGE
1359b 52 5f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 R_EXCLUSIVE stat
1359c 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 e, then attempt
1359d 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 (1). Only.** a
1359e 74 74 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 ttempt (2) if (1
1359f 29 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e ) is successful.
135a0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
135a1 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c K if successful,
135a2 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 .** otherwise
135a3 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 enter the error
135a4 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e state and return
135a5 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
135a6 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 from the .** f
135a7 61 69 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ailing operation
135a8 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 ..**.** In thi
135a9 73 20 63 61 73 65 20 74 68 65 20 64 61 74 61 62 s case the datab
135aa 61 73 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 ase file may hav
135ab 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 e been written t
135ac 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 o. So if the.**
135ad 20 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 playback opera
135ae 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 tion did not suc
135af 63 65 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f ceed it would no
135b0 74 20 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e t be safe to fin
135b1 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a alize.** the j
135b2 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 ournal file. It
135b3 6e 65 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 needs to be left
135b4 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 in the file-sys
135b5 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 tem so that.**
135b6 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
135b7 65 73 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 ess can use it t
135b8 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 o restore the da
135b9 74 61 62 61 73 65 20 73 74 61 74 65 20 28 62 79 tabase state (by
135ba 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 .** hot-journa
135bb 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a l rollback)..*/.
135bc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
135bd 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
135be 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 ollback(Pager *p
135bf 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
135c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
135c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
135c2 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
135c3 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
135c4 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 ROLLBACK %d\n",
135c5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 PAGERID(pPager))
135c6 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 );. if( !pPager
135c7 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 ->dbModified ||
135c8 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
135c9 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d jfd) ){. rc =
135ca 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 pager_end_trans
135cb 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 action(pPager, p
135cc 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
135cd 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
135ce 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 Pager->errCode &
135cf 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 & pPager->errCod
135d0 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 e!=SQLITE_FULL )
135d1 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
135d2 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
135d3 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 XCLUSIVE ){.
135d4 20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b pager_playback
135d5 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
135d6 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 }. rc = pPag
135d7 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
135d8 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 else{. if( pP
135d9 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
135da 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 ER_RESERVED ){.
135db 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 int rc2;.
135dc 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
135dd 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 layback(pPager,
135de 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 0);. rc2 =
135df 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
135e0 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 ction(pPager, pP
135e1 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 ager->setMaster)
135e2 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
135e3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
135e4 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 rc = rc2;.
135e5 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
135e6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
135e7 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
135e8 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 er, 0);. }..
135e9 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b if( !MEMDB ){
135ea 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
135eb 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a bSizeValid = 0;.
135ec 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
135ed 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
135ee 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 during a ROLLBA
135ef 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f CK, we can no lo
135f0 6e 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 nger trust the p
135f1 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 ager. ** cach
135f2 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 e. So call pager
135f3 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 _error() on the
135f4 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 way out to make
135f5 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a any error . *
135f6 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 * persistent..
135f7 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 */. rc = pa
135f8 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
135f9 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 , rc);. }. ret
135fa 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
135fb 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
135fc 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
135fd 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 e is opened read
135fe 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 -only. Return F
135ff 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 ALSE.** if the d
13600 61 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 atabase is (in t
13601 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e heory) writable.
13602 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13603 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 TE u8 sqlite3Pag
13604 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 erIsreadonly(Pag
13605 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
13606 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 eturn pPager->re
13607 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a adOnly;.}../*.**
13608 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
13609 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
1360a 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a to the pager..*
1360b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1360c 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
1360d 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 rRefcount(Pager
1360e 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
1360f 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 rn sqlite3Pcache
13610 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
13611 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a >pPCache);.}../*
13612 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
13613 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
13614 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 ces to the speci
13615 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 fied page..*/.SQ
13616 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13617 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
13618 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 eRefcount(DbPage
13619 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 *pPage){. retu
1361a 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 rn sqlite3Pcache
1361b 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 PageRefcount(pPa
1361c 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 ge);.}..#ifdef S
1361d 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
1361e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1361f 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
13620 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
13621 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly..*/.SQLITE_P
13622 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 RIVATE int *sqli
13623 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 te3PagerStats(Pa
13624 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13625 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d static int a[11]
13626 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 ;. a[0] = sqlit
13627 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
13628 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
13629 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 );. a[1] = sqli
1362a 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
1362b 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
1362c 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 he);. a[2] = sq
1362d 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 lite3PcacheGetCa
1362e 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e chesize(pPager->
1362f 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d pPCache);. a[3]
13630 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
13631 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 eValid ? (int) p
13632 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 Pager->dbSize :
13633 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 -1;. a[4] = pPa
13634 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b ger->state;. a[
13635 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 5] = pPager->err
13636 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 Code;. a[6] = p
13637 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 Pager->nHit;. a
13638 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d [7] = pPager->nM
13639 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b iss;. a[8] = 0;
1363a 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 /* Used to be
1363b 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f pPager->nOvfl */
1363c 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 . a[9] = pPager
1363d 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d ->nRead;. a[10]
1363e 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 = pPager->nWrit
1363f 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d e;. return a;.}
13640 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
13641 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
13642 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f is is an in-memo
13643 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c ry pager..*/.SQL
13644 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13645 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 sqlite3PagerIsMe
13646 6d 64 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 mdb(Pager *pPage
13647 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d r){. return MEM
13648 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 DB;.}../*.** Che
13649 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 61 72 ck that there ar
1364a 65 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 e at least nSave
1364b 70 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 point savepoints
1364c 20 6f 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20 open. If there
1364d 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 are.** currently
1364e 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65 less than nSave
1364f 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 points open, the
13650 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f n open one or mo
13651 72 65 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a re savepoints.**
13652 20 74 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20 to make up the
13653 64 69 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74 difference. If t
13654 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 he number of sav
13655 65 70 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61 epoints is alrea
13656 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e dy.** equal to n
13657 53 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 Savepoint, then
13658 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
13659 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
1365a 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
1365b 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 cation fails, SQ
1365c 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 LITE_NOMEM is re
1365d 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 turned. If an er
1365e 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 ror .** occurs w
1365f 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 hile opening the
13660 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-journal fil
13661 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 e, then an IO er
13662 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 ror code is.** r
13663 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 eturned. Otherwi
13664 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a se, SQLITE_OK..*
13665 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13666 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13667 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 rOpenSavepoint(P
13668 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
13669 74 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 t nSavepoint){.
1366a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1366b 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1366c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1366d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
1366e 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 nt nCurrent = pP
1366f 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
13670 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 ; /* Curr
13671 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 ent number of sa
13672 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 vepoints */.. i
13673 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 f( nSavepoint>nC
13674 75 72 72 65 6e 74 20 26 26 20 70 50 61 67 65 72 urrent && pPager
13675 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a ->useJournal ){.
13676 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
13677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13678 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
13679 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 terator variable
1367a 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76 */. PagerSav
1367b 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 epoint *aNew;
1367c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1367d 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61 /* New Pager.aSa
1367e 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f vepoint array */
1367f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 .. /* Either
13680 74 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 there is no acti
13681 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 ve journal or th
13682 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 e sub-journal is
13683 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a open or . **
13684 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
13685 61 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e always stored in
13686 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 memory */. a
13687 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
13688 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 Savepoint==0 ||
13689 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 isOpen(pPager->s
1368a 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 jfd) ||.
1368b 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1368c 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
1368d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
1368e 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f Y );.. /* Gro
1368f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 w the Pager.aSav
13690 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 epoint array usi
13691 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 ng realloc(). Re
13692 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
13693 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 M. ** if the
13694 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
13695 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 . Otherwise, zer
13696 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f o the new portio
13697 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 n in case a .
13698 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 ** malloc failu
13699 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 re occurs while
1369a 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e populating it in
1369b 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f the for(...) lo
1369c 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f op below.. */
1369d 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 . aNew = (Pag
1369e 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 erSavepoint *)sq
1369f 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 lite3Realloc(.
136a0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 pPager->aS
136a1 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 avepoint, sizeof
136a2 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 (PagerSavepoint)
136a3 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 *nSavepoint.
136a4 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 );. if( !aNew
136a5 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
136a6 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
136a7 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
136a8 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c &aNew[nCurrent],
136a9 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 0, (nSavepoint-
136aa 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 nCurrent) * size
136ab 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e of(PagerSavepoin
136ac 74 29 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d t));. pPager-
136ad 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e >aSavepoint = aN
136ae 65 77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ew;. pPager->
136af 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 nSavepoint = nSa
136b0 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a vepoint;.. /*
136b1 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61 Populate the Pa
136b2 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72 gerSavepoint str
136b3 75 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c uctures just all
136b4 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 ocated. */. f
136b5 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 or(ii=nCurrent;
136b6 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 ii<nSavepoint; i
136b7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 i++){. asse
136b8 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
136b9 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 zeValid );.
136ba 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 aNew[ii].nOrig
136bb 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 = pPager->dbSize
136bc 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 ;. if( isOp
136bd 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
136be 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
136bf 61 6c 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 20 alOff>0 ){.
136c0 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 aNew[ii].iOff
136c1 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f set = pPager->jo
136c2 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 urnalOff;.
136c3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
136c4 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 New[ii].iOffset
136c5 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a = JOURNAL_HDR_SZ
136c6 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
136c7 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d }. aNew[ii]
136c8 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 65 .iSubRec = pPage
136c9 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 r->nSubRec;.
136ca 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 aNew[ii].pInSa
136cb 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 vepoint = sqlite
136cc 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50 3BitvecCreate(pP
136cd 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 ager->dbSize);.
136ce 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 5b 69 if( !aNew[i
136cf 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 i].pInSavepoint
136d0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
136d1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
136d2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
136d3 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 /* Open the s
136d4 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 ub-journal, if i
136d5 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 t is not already
136d6 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 opened. */.
136d7 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 rc = openSubJour
136d8 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d nal(pPager);. }
136d9 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
136da 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
136db 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
136dc 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 to rollback or r
136dd 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 elease (commit)
136de 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 a savepoint..**
136df 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f The savepoint to
136e0 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c release or roll
136e1 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 back need not be
136e2 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
136e3 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 ly .** created s
136e4 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 avepoint..**.**
136e5 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20 Parameter op is
136e6 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 41 always either SA
136e7 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
136e8 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 or SAVEPOINT_RE
136e9 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 LEASE..** If it
136ea 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c is SAVEPOINT_REL
136eb 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 EASE, then relea
136ec 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74 se and destroy t
136ed 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 he savepoint wit
136ee 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65 h.** index iSave
136ef 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20 point. If it is
136f0 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
136f1 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 CK, then rollbac
136f2 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a k all changes.**
136f3 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 that have occur
136f4 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 70 red since the sp
136f5 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e ecified savepoin
136f6 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a t was created..*
136f7 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 *.** The savepoi
136f8 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f nt to rollback o
136f9 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 65 r release is ide
136fa 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ntified by param
136fb 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f eter .** iSavepo
136fc 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 int. A value of
136fd 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 0 means to opera
136fe 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d te on the outerm
136ff 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a ost savepoint.**
13700 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 61 (the first crea
13701 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 ted). A value of
13702 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 (Pager.nSavepoi
13703 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 nt-1) means oper
13704 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f ate.** on the mo
13705 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61 st recently crea
13706 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 ted savepoint. I
13707 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 f iSavepoint is
13708 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 greater than.**
13709 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e (Pager.nSavepoin
1370a 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20 t-1), then this
1370b 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
1370c 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 -op..**.** If a
1370d 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 negative value i
1370e 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 s passed to this
1370f 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 function, then
13710 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 the current.** t
13711 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
13712 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 lled back. This
13713 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 is different to
13714 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
13715 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
13716 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 20 () because this
13717 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
13718 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 t terminate.** t
13719 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f he transaction o
1371a 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 r unlock the dat
1371b 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72 abase, it just r
1371c 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 estores the .**
1371d 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
1371e 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 database to its
1371f 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 original state.
13720 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 .**.** In any ca
13721 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e se, all savepoin
13722 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 ts with an index
13723 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 53 greater than iS
13724 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 avepoint .** are
13725 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74 destroyed. If t
13726 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 his is a release
13727 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d operation (op==
13728 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
13729 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 E),.** then save
1372a 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 point iSavepoint
1372b 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 is also destroy
1372c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
1372d 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
1372e 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 rn SQLITE_NOMEM
1372f 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f if a memory allo
13730 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a cation fails,.**
13731 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 or an IO error
13732 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72 code if an IO er
13733 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
13734 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
13735 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 .** savepoint. I
13736 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 f no errors occu
13737 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 r, SQLITE_OK is
13738 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 returned..*/ .SQ
13739 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1373a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
1373b 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
1373c 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e ager, int op, in
1373d 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 t iSavepoint){.
1373e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1373f 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
13740 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 op==SAVEPOINT_RE
13741 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 LEASE || op==SAV
13742 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
13743 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 );. assert( iSa
13744 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 vepoint>=0 || op
13745 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
13746 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 BACK );.. if( i
13747 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 Savepoint<pPager
13748 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a ->nSavepoint ){.
13749 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
1374a 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
1374b 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
1374c 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 int nNew;
1374d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1374e 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 of remaining sav
1374f 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68 epoints after th
13750 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f is op. */.. /
13751 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 * Figure out how
13752 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 many savepoints
13753 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 will still be a
13754 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73 ctive after this
13755 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f . ** operatio
13756 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61 n. Store this va
13757 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 lue in nNew. The
13758 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73 n free resources
13759 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20 associated .
1375a 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76 ** with any sav
1375b 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65 epoints that are
1375c 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68 destroyed by th
1375d 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 is operation..
1375e 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 */. nNew =
1375f 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 iSavepoint + (op
13760 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
13761 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 BACK);. for(i
13762 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 i=nNew; ii<pPage
13763 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 r->nSavepoint; i
13764 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
13765 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
13766 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f (pPager->aSavepo
13767 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 int[ii].pInSavep
13768 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 oint);. }.
13769 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
1376a 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 int = nNew;..
1376b 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
1376c 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 rollback operat
1376d 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 ion, playback th
1376e 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65 e specified save
1376f 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 point.. ** If
13770 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d this is a temp-
13771 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73 file, it is poss
13772 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f ible that the jo
13773 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 urnal file has.
13774 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 ** not yet be
13775 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 en opened. In th
13776 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68 61 is case there ha
13777 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 ve been no chang
13778 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 es to. ** the
13779 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
1377a 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 so the playback
1377b 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 operation can be
1377c 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f skipped.. */
1377d 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 . if( op==SAV
1377e 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1377f 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
13780 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
13781 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
13782 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e pSavepoint = (nN
13783 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 ew==0)?0:&pPager
13784 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 ->aSavepoint[nNe
13785 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d w-1];. rc =
13786 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 pagerPlaybackSa
13787 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 vepoint(pPager,
13788 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 pSavepoint);.
13789 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 51 assert(rc!=SQ
1378a 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 LITE_DONE);.
1378b 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 }. . /* If t
1378c 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 his is a release
1378d 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 of the outermos
1378e 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 t savepoint, tru
1378f 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 ncate . ** th
13790 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f e sub-journal to
13791 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 zero bytes in s
13792 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ize. */. if(
13793 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 nNew==0 && op==S
13794 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
13795 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
13796 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
13797 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
13798 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
13799 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
1379a 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
1379b 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 sjfd, 0);.
1379c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 pPager->nSubRec
1379d 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
1379e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1379f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
137a0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 full pathname of
137a1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
137a2 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
137a3 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
137a4 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
137a5 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 lename(Pager *pP
137a6 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
137a7 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
137a8 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 e;.}../*.** Retu
137a9 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 63 rn the VFS struc
137aa 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 ture for the pag
137ab 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
137ac 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 IVATE const sqli
137ad 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 te3_vfs *sqlite3
137ae 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20 2a PagerVfs(Pager *
137af 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
137b0 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a n pPager->pVfs;.
137b1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
137b2 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 the file handle
137b3 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
137b4 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 file associated
137b5 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 .** with the pag
137b6 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 er. This might
137b7 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 return NULL if t
137b8 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e he file has.** n
137b9 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
137ba 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
137bb 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 IVATE sqlite3_fi
137bc 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 le *sqlite3Pager
137bd 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 File(Pager *pPag
137be 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
137bf 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a ager->fd;.}../*.
137c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 ** Return the fu
137c1 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 ll pathname of t
137c2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
137c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
137c4 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
137c5 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
137c6 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 alname(Pager *pP
137c7 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
137c8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
137c9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
137ca 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 n true if fsync(
137cb 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 ) calls are disa
137cc 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 bled for this pa
137cd 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c ger. Return FAL
137ce 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 SE.** if fsync()
137cf 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 6e s are executed n
137d0 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 ormally..*/.SQLI
137d1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
137d2 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e qlite3PagerNosyn
137d3 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 c(Pager *pPager)
137d4 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
137d5 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 r->noSync;.}..#i
137d6 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
137d7 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 CODEC./*.** Set
137d8 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68 the codec for th
137d9 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49 is pager.*/.SQLI
137da 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
137db 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
137dc 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70 odec(. Pager *p
137dd 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 Pager,. void *(
137de 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 *xCodec)(void*,v
137df 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a oid*,Pgno,int),.
137e0 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 void *pCodecAr
137e1 67 0a 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 g.){. pPager->x
137e2 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a Codec = xCodec;.
137e3 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 pPager->pCodec
137e4 41 72 67 20 3d 20 70 43 6f 64 65 63 41 72 67 3b Arg = pCodecArg;
137e5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
137e6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
137e7 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 UTOVACUUM./*.**
137e8 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 Move the page pP
137e9 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 g to location pg
137ea 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a no in the file..
137eb 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 **.** There must
137ec 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 be no reference
137ed 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 s to the page pr
137ee 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 eviously located
137ef 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 at.** pgno (whi
137f0 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c ch we call pPgOl
137f1 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 d) though that p
137f2 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 age is allowed t
137f3 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 o be.** in cache
137f4 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70 . If the page p
137f5 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 reviously locate
137f6 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 d at pgno is not
137f7 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 already.** in t
137f8 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
137f9 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 nal, it is not p
137fa 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 ut there by by t
137fb 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a his routine..**.
137fc 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f ** References to
137fd 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 the page pPg re
137fe 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 main valid. Upda
137ff 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 ting any.** meta
13800 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 -data associated
13801 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 with pPg (i.e.
13802 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 data stored in t
13803 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a he nExtra bytes.
13804 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f ** allocated alo
13805 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 ng with the page
13806 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 ) is the respons
13807 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 ibility of the c
13808 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 aller..**.** A t
13809 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 ransaction must
1380a 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 be active when t
1380b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
1380c 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 alled. It used t
1380d 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 o be.** required
1380e 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e that a statemen
1380f 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 t transaction wa
13810 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 s not active, bu
13811 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 t this restricti
13812 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 on.** has been r
13813 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 emoved (CREATE I
13814 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f NDEX needs to mo
13815 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 ve a page when a
13816 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 statement.** tr
13817 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
13818 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ive)..**.** If t
13819 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 he fourth argume
1381a 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 nt, isCommit, is
1381b 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
1381c 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69 this page is bei
1381d 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 ng.** moved as p
1381e 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 art of a databas
1381f 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e e reorganization
13820 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 just before the
13821 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a transaction .**
13822 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 is being commit
13823 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ted. In this cas
13824 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 e, it is guarant
13825 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 eed that the dat
13826 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 abase page .** p
13827 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c Pg refers to wil
13828 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e l not be written
13829 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e to again within
1382a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
1382b 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 n..**.** This fu
1382c 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 nction may retur
1382d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f n SQLITE_NOMEM o
1382e 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f r an IO error co
1382f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a de if an error.*
13830 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 * occurs. Otherw
13831 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 ise, it returns
13832 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 SQLITE_OK..*/.SQ
13833 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13834 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
13835 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 epage(Pager *pPa
13836 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 ger, DbPage *pPg
13837 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 , Pgno pgno, int
13838 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 isCommit){. Pg
13839 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 Hdr *pPgOld;
1383a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1383b 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 e page being ove
1383c 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 rwritten. */. P
1383d 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f gno needSyncPgno
1383e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f = 0; /* O
1383f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d ld value of pPg-
13840 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 >pgno, if sync i
13841 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 s required */.
13842 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
13843 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13844 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
13845 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 Pgno origPgno;
13846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13847 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 The original pa
13848 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 ge number */..
13849 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 assert( pPg->nRe
1384a 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 f>0 );.. /* If
1384b 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d the page being m
1384c 6f 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e oved is dirty an
1384d 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 d has not been s
1384e 61 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 aved by the late
1384f 73 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e st. ** savepoin
13850 74 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 t, then save the
13851 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 current content
13852 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e s of the page in
13853 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 to the . ** sub
13854 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 -journal now. Th
13855 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 is is required t
13856 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c o handle the fol
13857 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a lowing scenario:
13858 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 . **. ** BEG
13859 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f IN;. ** <jo
1385a 75 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 urnal page X, th
1385b 65 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 en modify it in
1385c 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 memory>. **
1385d 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a SAVEPOINT one;.
1385e 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 ** <Move
1385f 20 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 page X to locat
13860 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 ion Y>. **
13861 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b ROLLBACK TO one;
13862 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 . **. ** If pa
13863 67 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 ge X were not wr
13864 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 itten to the sub
13865 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 -journal here, i
13866 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a t would not. **
13867 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 be possible to
13868 72 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 restore its cont
13869 65 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 ents when the "R
1386a 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a OLLBACK TO one".
1386b 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 ** statement w
1386c 65 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 ere is processed
1386d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a .. **. ** subj
1386e 6f 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 ournalPage() may
1386f 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 need to allocat
13870 65 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 e space to store
13871 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a pPg->pgno into.
13872 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 ** one or more
13873 20 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 savepoint bitve
13874 63 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 cs. This is the
13875 72 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 reason this func
13876 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 tion. ** may re
13877 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
13878 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 M.. */. if( pP
13879 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
1387a 49 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a IRTY . && subj
1387b 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 RequiresPage(pPg
1387c 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f ). && SQLITE_O
1387d 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 K!=(rc = subjour
1387e 6e 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 nalPage(pPg)).
1387f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
13880 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 ;. }.. PAGERTR
13881 41 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 ACE(("MOVE %d pa
13882 67 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d ge %d (needSync=
13883 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c %d) moves to %d\
13884 6e 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 n", . PAGER
13885 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
13886 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 >pgno, (pPg->fla
13887 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13888 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b NC)?1:0, pgno));
13889 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 . IOTRACE(("MOV
1388a 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 E %p %d %d\n", p
1388b 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
1388c 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 , pgno)).. /* I
1388d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 f the journal ne
1388e 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 eds to be sync()
1388f 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 ed before page p
13890 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a Pg->pgno can. *
13891 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c * be written to,
13892 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
13893 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
13894 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e le needSyncPgno.
13895 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
13896 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 e isCommit flag
13897 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 is set, there is
13898 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 no need to reme
13899 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 mber that. ** t
1389a 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 he journal needs
1389b 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 to be sync()ed
1389c 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 before database
1389d 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a page pPg->pgno .
1389e 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 ** can be writ
1389f 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c ten to. The call
138a0 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 er has already p
138a1 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 romised not to w
138a2 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f rite to it.. */
138a3 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 . if( (pPg->fla
138a4 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
138a5 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 NC) && !isCommit
138a6 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 ){. needSync
138a7 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f Pgno = pPg->pgno
138a8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
138a9 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
138aa 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 || pPg->pgno>pP
138ab 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
138ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
138ad 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
138ae 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 _DIRTY );. as
138af 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 sert( pPager->ne
138b0 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 edSync );. }..
138b1 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 /* If the cache
138b2 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 contains a page
138b3 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 with page-numbe
138b4 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 r pgno, remove i
138b5 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 t. ** from its
138b6 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f hash chain. Also
138b7 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e , if the PgHdr.n
138b8 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 eedSync was set
138b9 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 for . ** page p
138ba 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 gno before the '
138bb 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c move' operation,
138bc 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
138bd 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 retained . ** f
138be 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 or the page move
138bf 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 d there.. */.
138c0 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 pPg->flags &= ~P
138c1 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
138c2 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 pPgOld = pager
138c3 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
138c4 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 pgno);. assert(
138c5 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f !pPgOld || pPgO
138c6 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 ld->nRef==1 );.
138c7 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 if( pPgOld ){.
138c8 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
138c9 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 (pPgOld->flags&
138ca 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
138cb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
138cc 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b cheDrop(pPgOld);
138cd 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f . }.. origPgno
138ce 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 = pPg->pgno;.
138cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 sqlite3PcacheMov
138d0 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 e(pPg, pgno);.
138d1 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
138d2 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 eDirty(pPg);. p
138d3 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
138d4 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 d = 1;.. if( ne
138d5 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 edSyncPgno ){.
138d6 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 /* If needSync
138d7 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f Pgno is non-zero
138d8 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
138d9 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f al file needs to
138da 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 be . ** sync
138db 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 ()ed before any
138dc 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 data is written
138dd 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 to database file
138de 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 page needSyncPg
138df 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 no.. ** Curre
138e0 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 ntly, no such pa
138e1 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 ge exists in the
138e2 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 page-cache and
138e3 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 the . ** "is
138e4 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 journaled" bitve
138e5 63 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 c flag has been
138e6 73 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 set. This needs
138e7 74 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 to be remedied b
138e8 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 y. ** loading
138e9 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 the page into t
138ea 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 he pager-cache a
138eb 6e 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 nd setting the P
138ec 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 gHdr.needSync .
138ed 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 ** flag..
138ee 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
138ef 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 attempt to load
138f0 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 the page into t
138f1 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 he page-cache fa
138f2 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a ils, (due. **
138f3 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f to a malloc() o
138f4 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 r IO failure), c
138f5 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 lear the bit in
138f6 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d the pInJournal[]
138f7 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f . ** array. O
138f8 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 therwise, if the
138f9 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 page is loaded
138fa 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 and written agai
138fb 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 n in. ** this
138fc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 transaction, it
138fd 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 may be written
138fe 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
138ff 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 file before.
13900 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 ** it is synced
13901 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
13902 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c file. This way,
13903 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 it may end up i
13904 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 n. ** the jou
13905 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c rnal file twice,
13906 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 but that is not
13907 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 a problem..
13908 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 **. ** The sq
13909 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
1390a 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 call may cause t
1390b 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 he journal to sy
1390c 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 nc. So make.
1390d 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 ** sure the Page
1390e 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 r.needSync flag
1390f 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 is set too..
13910 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 */. PgHdr *pP
13911 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 gHdr;. assert
13912 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ( pPager->needSy
13913 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 nc );. rc = s
13914 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
13915 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 Pager, needSyncP
13916 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 gno, &pPgHdr);.
13917 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13918 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 E_OK ){. if
13919 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
1391a 72 6e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63 rnal && needSync
1391b 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 Pgno<=pPager->db
1391c 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 OrigSize ){.
1391d 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
1391e 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 cClear(pPager->p
1391f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 InJournal, needS
13920 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 yncPgno);.
13921 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 }. return r
13922 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 c;. }. pPa
13923 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
13924 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 1;. assert( p
13925 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 Pager->noSync==0
13926 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 && !MEMDB );.
13927 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 pPgHdr->flags
13928 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 |= PGHDR_NEED_SY
13929 4e 43 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 NC;. sqlite3P
1392a 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 cacheMakeDirty(p
1392b 50 67 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 PgHdr);. sqli
1392c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
1392d 67 48 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a gHdr);. }.. /*
1392e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d . ** For an in-
1392f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
13930 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f make sure the o
13931 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e riginal page con
13932 74 69 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 tinues. ** to e
13933 78 69 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 xist, in case th
13934 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 e transaction ne
13935 65 64 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b eds to roll back
13936 2e 20 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20 . We allocate.
13937 20 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77 ** the page now
13938 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20 , instead of at
13939 72 6f 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73 rollback, becaus
1393a 65 20 77 65 20 63 61 6e 20 62 65 74 74 65 72 20 e we can better
1393b 64 65 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61 deal. ** with a
1393c 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 n out-of-memory
1393d 65 72 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b error now. Tick
1393e 65 74 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20 et #3761.. */.
1393f 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 if( MEMDB ){.
13940 20 20 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a DbPage *pNew;.
13941 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13942 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50 61 PagerAcquire(pPa
13943 67 65 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26 ger, origPgno, &
13944 70 4e 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66 pNew, 1);. if
13945 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13946 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
13947 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
13948 65 66 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 ef(pNew);. }..
13949 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1394a 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
1394b 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
1394c 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 ter to the data
1394d 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
1394e 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 d page..*/.SQLIT
1394f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
13950 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
13951 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 ata(DbPage *pPg)
13952 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d {. assert( pPg-
13953 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e >nRef>0 || pPg->
13954 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b pPager->memDb );
13955 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 . return pPg->p
13956 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Data;.}../*.** R
13957 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
13958 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 to the Pager.nEx
13959 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 tra bytes of "ex
1395a 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 tra" space .** a
1395b 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 llocated along w
1395c 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65 ith the specifie
1395d 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 d page..*/.SQLIT
1395e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
1395f 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
13960 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 xtra(DbPage *pPg
13961 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
13962 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13963 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 ;. return (pPag
13964 65 72 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30 er?pPg->pExtra:0
13965 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f );.}../*.** Get/
13966 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d set the locking-
13967 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 mode for this pa
13968 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 ger. Parameter e
13969 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 Mode must be one
1396a 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 .** of PAGER_LOC
1396b 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 KINGMODE_QUERY,
1396c 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
1396d 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 E_NORMAL or .**
1396e 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
1396f 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 E_EXCLUSIVE. If
13970 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 the parameter is
13971 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 not _QUERY, the
13972 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 n.** the locking
13973 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 -mode is set to
13974 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 the value specif
13975 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ied..**.** The r
13976 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 eturned value is
13977 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f either PAGER_LO
13978 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
13979 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 or.** PAGER_LOC
1397a 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
1397b 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 VE, indicating t
1397c 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 he current (poss
1397d 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a ibly updated).**
1397e 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a locking-mode..*
1397f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13980 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13981 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 rLockingMode(Pag
13982 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13983 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 eMode){. assert
13984 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c ( eMode==PAGER_L
13985 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 OCKINGMODE_QUERY
13986 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
13987 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
13988 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a KINGMODE_NORMAL.
13989 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
1398a 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b Mode==PAGER_LOCK
1398b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
1398c 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 E );. assert( P
1398d 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
1398e 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 _QUERY<0 );. as
1398f 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b sert( PAGER_LOCK
13990 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d INGMODE_NORMAL>=
13991 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 0 && PAGER_LOCKI
13992 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
13993 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f >=0 );. if( eMo
13994 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 de>=0 && !pPager
13995 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 ->tempFile ){.
13996 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 pPager->exclus
13997 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d iveMode = (u8)eM
13998 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ode;. }. retur
13999 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 n (int)pPager->e
1399a 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a xclusiveMode;.}.
1399b 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 ./*.** Get/set t
1399c 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 he journal-mode
1399d 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 for this pager.
1399e 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 Parameter eMode
1399f 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a must be one of:.
139a0 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a **.** PAGER_J
139a1 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 OURNALMODE_QUERY
139a2 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
139a3 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a RNALMODE_DELETE.
139a4 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
139a5 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
139a6 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
139a7 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
139a8 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
139a9 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 RNALMODE_OFF.**
139aa 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
139ab 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a MODE_MEMORY.**.*
139ac 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 * If the paramet
139ad 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 er is not _QUERY
139ae 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
139af 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 al_mode is set t
139b0 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 o the.** value s
139b1 70 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 pecified if the
139b2 63 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 change is allowe
139b3 64 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 d. The change i
139b4 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 s disallowed.**
139b5 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e for the followin
139b6 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a g reasons:.**.**
139b7 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f * An in-memo
139b8 72 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 ry database can
139b9 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f only have its jo
139ba 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 urnal_mode set t
139bb 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f o _OFF.** o
139bc 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a r _MEMORY..**.**
139bd 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 * The journa
139be 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 l mode may not b
139bf 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 e changed while
139c0 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
139c1 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 active..**.** T
139c2 68 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 he returned indi
139c3 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 cate the current
139c4 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 (possibly updat
139c5 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 ed) journal-mode
139c6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
139c7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
139c8 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 agerJournalMode(
139c9 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
139ca 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 nt eMode){. ass
139cb 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 ert( eMode==PAGE
139cc 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
139cd 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 ERY.
139ce 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
139cf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 JOURNALMODE_DELE
139d0 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c TE. |
139d1 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
139d2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
139d3 41 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 ATE.
139d4 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
139d5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
139d6 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 IST.
139d7 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
139d8 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
139d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
139da 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
139db 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
139dc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
139dd 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 ER_JOURNALMODE_Q
139de 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 UERY<0 );. if(
139df 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 eMode>=0. && (
139e0 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d !MEMDB || eMode=
139e1 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
139e2 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 DE_MEMORY .
139e3 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 || eMod
139e4 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
139e5 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 MODE_OFF). &&
139e6 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 !pPager->dbModif
139e7 69 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 ied. && (!isOp
139e8 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
139e9 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f || 0==pPager->jo
139ea 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 urnalOff). ){.
139eb 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
139ec 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 ager->jfd) ){.
139ed 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
139ee 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
139ef 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
139f0 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d r->journalMode =
139f1 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a (u8)eMode;. }.
139f2 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 return (int)pP
139f3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
139f4 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f e;.}../*.** Get/
139f5 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d set the size-lim
139f6 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 it used for pers
139f7 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 istent journal f
139f8 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 iles..**.** Sett
139f9 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d ing the size lim
139fa 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e it to -1 means n
139fb 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 o limit is enfor
139fc 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d ced..** An attem
139fd 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 pt to set a limi
139fe 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d t smaller than -
139ff 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 1 is a no-op..*/
13a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13a01 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 i64 sqlite3Pager
13a02 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 JournalSizeLimit
13a03 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13a04 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 i64 iLimit){. i
13a05 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b f( iLimit>=-1 ){
13a06 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
13a07 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 rnalSizeLimit =
13a08 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 iLimit;. }. re
13a09 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 turn pPager->jou
13a0a 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d rnalSizeLimit;.}
13a0b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
13a0c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
13a0d 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 pPager->pBackup
13a0e 76 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 variable. The ba
13a0f 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 ckup module.** i
13a10 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 n backup.c maint
13a11 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 ains the content
13a12 20 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c of this variabl
13a13 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a e. This module.*
13a14 2a 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 * uses it opaque
13a15 6c 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ly as an argumen
13a16 74 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b t to sqlite3Back
13a17 75 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a upRestart() and.
13a18 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 ** sqlite3Backup
13a19 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a Update() only..*
13a1a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13a1b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
13a1c 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 **sqlite3PagerBa
13a1d 63 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 ckupPtr(Pager *p
13a1e 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
13a1f 20 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 &pPager->pBacku
13a20 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 p;.}..#endif /*
13a21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
13a22 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a IO */../********
13a23 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 ****** End of pa
13a24 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ger.c **********
13a25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a27 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
13a28 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
13a29 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a e btmutex.c ****
13a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a2c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
13a2d 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 7 August 27.**.*
13a2e 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
13a2f 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
13a30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
13a31 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
13a32 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
13a33 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
13a34 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
13a35 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
13a36 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
13a37 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
13a38 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
13a39 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
13a3a 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
13a3b 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
13a3c 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
13a3d 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
13a3e 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
13a3f 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
13a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
13a44 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c $Id: btmutex.c,
13a45 76 20 31 2e 31 35 20 32 30 30 39 2f 30 34 2f 31 v 1.15 2009/04/1
13a46 30 20 31 32 3a 35 35 3a 31 37 20 64 61 6e 69 65 0 12:55:17 danie
13a47 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a lk1977 Exp $.**.
13a48 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
13a49 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 tains code used
13a4a 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 to implement mut
13a4b 65 78 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 exes on Btree ob
13a4c 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 jects..** This c
13a4d 6f 64 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e ode really belon
13a4e 67 73 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 gs in btree.c.
13a4f 42 75 74 20 62 74 72 65 65 2e 63 20 69 73 20 67 But btree.c is g
13a50 65 74 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 etting too.** bi
13a51 67 20 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f g and we want to
13a52 20 62 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 break it down s
13a53 6f 6d 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61 ome. This packa
13a54 67 65 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a ged seemed like.
13a55 2a 2a 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f ** a good breako
13a56 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ut..*/./********
13a57 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 ****** Include b
13a58 74 72 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 treeInt.h in the
13a59 20 6d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 middle of btmut
13a5a 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
13a5b 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
13a5c 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
13a5d 65 20 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a e btreeInt.h ***
13a5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a60 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
13a61 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 4 April 6.**.**
13a62 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
13a63 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
13a64 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
13a65 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
13a66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
13a67 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
13a68 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
13a69 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
13a6a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
13a6b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
13a6c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
13a6d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
13a6e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
13a6f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
13a70 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
13a71 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
13a72 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
13a73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a77 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a ********.** $Id:
13a78 20 62 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e btreeInt.h,v 1.
13a79 34 36 20 32 30 30 39 2f 30 33 2f 32 30 20 31 34 46 2009/03/20 14
13a7a 3a 31 38 3a 35 32 20 64 61 6e 69 65 6c 6b 31 39 :18:52 danielk19
13a7b 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 77 Exp $.**.** T
13a7c 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
13a7d 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 nts a external (
13a7e 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 disk-based) data
13a7f 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 base using BTree
13a80 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 s..** For a deta
13a81 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 iled discussion
13a82 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 of BTrees, refer
13a83 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f to.**.** Do
13a84 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 nald E. Knuth, T
13a85 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 HE ART OF COMPUT
13a86 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 ER PROGRAMMING,
13a87 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 Volume 3:.**
13a88 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 "Sorting And Se
13a89 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 arching", pages
13a8a 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 473-480. Addison
13a8b 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 -Wesley.** P
13a8c 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e ublishing Compan
13a8d 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 y, Reading, Mass
13a8e 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a achusetts..**.**
13a8f 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 The basic idea
13a90 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 is that each pag
13a91 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f e of the file co
13a92 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 ntains N databas
13a93 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 e.** entries and
13a94 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f N+1 pointers to
13a95 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a subpages..**.**
13a96 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------
13a97 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a98 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a99 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a9a 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 ---.** | Ptr(
13a9b 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 0) | Key(0) | Pt
13a9c 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 r(1) | Key(1) |
13a9d 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c ... | Key(N-1) |
13a9e 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d Ptr(N) |.** -
13a9f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13aa0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13aa1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13aa2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
13aa3 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 **.** All of the
13aa4 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 keys on the pag
13aa5 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f e that Ptr(0) po
13aa6 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c ints to have val
13aa7 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e ues less.** than
13aa8 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 Key(0). All of
13aa9 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 the keys on pag
13aaa 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 e Ptr(1) and its
13aab 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a subpages have.*
13aac 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 * values greater
13aad 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 than Key(0) and
13aae 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 less than Key(1
13aaf 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b ). All of the k
13ab0 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 eys.** on Ptr(N)
13ab1 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 and its subpage
13ab2 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 s have values gr
13ab3 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e eater than Key(N
13ab4 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 -1). And.** so
13ab5 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e forth..**.** Fin
13ab6 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 ding a particula
13ab7 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 r key requires r
13ab8 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 eading O(log(M))
13ab9 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
13aba 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d .** disk where M
13abb 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
13abc 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 f entries in the
13abd 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 tree..**.** In
13abe 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 this implementat
13abf 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 ion, a single fi
13ac0 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 le can hold one
13ac1 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 or more separate
13ac2 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 .** BTrees. Ea
13ac3 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e ch BTree is iden
13ac4 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e tified by the in
13ac5 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 dex of its root
13ac6 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 page. The.** ke
13ac7 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 y and data for a
13ac8 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d ny entry are com
13ac9 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 bined to form th
13aca 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a e "payload". A.
13acb 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 ** fixed amount
13acc 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 of payload can b
13acd 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 e carried direct
13ace 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ly on the databa
13acf 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 se.** page. If
13ad0 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c the payload is l
13ad1 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 arger than the p
13ad2 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 reset amount the
13ad3 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 n surplus.** byt
13ad4 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e es are stored on
13ad5 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
13ad6 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f The payload fo
13ad7 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e r an entry.** an
13ad8 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 d the preceding
13ad9 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 pointer are comb
13ada 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 ined to form a "
13adb 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a Cell". Each .**
13adc 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c page has a smal
13add 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 l header which c
13ade 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 ontains the Ptr(
13adf 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f N) pointer and o
13ae0 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 ther.** informat
13ae1 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 ion such as the
13ae2 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 size of key and
13ae3 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d data..**.** FORM
13ae4 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a AT DETAILS.**.**
13ae5 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 The file is div
13ae6 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e ided into pages.
13ae7 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 The first page
13ae8 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 is called page
13ae9 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 1,.** the second
13aea 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 is page 2, and
13aeb 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 so forth. A pag
13aec 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f e number of zero
13aed 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e indicates.** "n
13aee 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 o such page". T
13aef 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e he page size can
13af0 20 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 be anything bet
13af1 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 ween 512 and 655
13af2 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 36..** Each page
13af3 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 can be either a
13af4 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 btree page, a f
13af5 72 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 reelist page or
13af6 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 an overflow.** p
13af7 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 age..**.** The f
13af8 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 irst page is alw
13af9 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 ays a btree page
13afa 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 . The first 100
13afb 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 bytes of the fi
13afc 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 rst.** page cont
13afd 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 ain a special he
13afe 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 ader (the "file
13aff 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 header") that de
13b00 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 scribes the file
13b01 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 ..** The format
13b02 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 of the file head
13b03 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 er is as follows
13b04 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 :.**.** OFFSET
13b05 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
13b06 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 IPTION.** 0
13b07 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 16 Hea
13b08 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c der string: "SQL
13b09 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 ite format 3\000
13b0a 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 ".** 16
13b0b 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 2 Page siz
13b0c 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a e in bytes. .**
13b0d 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 18 1
13b0e 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 File format
13b0f 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a write version.**
13b10 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 19 1
13b11 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 File format
13b12 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 read version.**
13b13 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 1
13b14 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 Bytes of unus
13b15 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 ed space at the
13b16 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 end of each page
13b17 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 .** 21
13b18 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 1 Max embed
13b19 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
13b1a 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 tion.** 22
13b1b 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 1 Min e
13b1c 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
13b1d 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 fraction.**
13b1e 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 23 1 M
13b1f 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 in leaf payload
13b20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 fraction.**
13b21 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 24 4 F
13b22 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 ile change count
13b23 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 er.** 28
13b24 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 4 Reserve
13b25 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 d for future use
13b26 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 .** 32
13b27 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 4 First fre
13b28 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 elist page.**
13b29 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 36 4
13b2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
13b2b 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 ist pages in the
13b2c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 file.** 40
13b2d 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 60 15 4
13b2e 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 -byte meta value
13b2f 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 s passed to high
13b30 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 er layers.**.**
13b31 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 All of the integ
13b32 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 er values are bi
13b33 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 g-endian (most s
13b34 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 ignificant byte
13b35 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 first)..**.** Th
13b36 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f e file change co
13b37 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 unter is increme
13b38 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 nted when the da
13b39 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 tabase is change
13b3a 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 d.** This counte
13b3b 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 r allows other p
13b3c 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 rocesses to know
13b3d 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 when the file h
13b3e 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e as changed.** an
13b3f 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 d thus when they
13b40 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 need to flush t
13b41 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a heir cache..**.*
13b42 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 * The max embedd
13b43 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
13b44 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e ion is the amoun
13b45 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 t of the total u
13b46 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 sable.** space i
13b47 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61 n a page that ca
13b48 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 n be consumed by
13b49 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 a single cell f
13b4a 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 or standard.** B
13b4b 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 -tree (non-LEAFD
13b4c 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 ATA) tables. A
13b4d 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 value of 255 mea
13b4e 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 ns 100%. The de
13b4f 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c fault.** is to l
13b50 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d imit the maximum
13b51 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 cell size so th
13b52 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 at at least 4 ce
13b53 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 lls will fit.**
13b54 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 on one page. Th
13b55 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d us the default m
13b56 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c ax embedded payl
13b57 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
13b58 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 64..**.** If the
13b59 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 payload for a c
13b5a 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 ell is larger th
13b5b 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f an the max paylo
13b5c 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a ad, then extra.*
13b5d 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 * payload is spi
13b5e 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 lled to overflow
13b5f 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e pages. Once an
13b60 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
13b61 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 s allocated,.**
13b62 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 as many bytes as
13b63 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f possible are mo
13b64 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 ved into the ove
13b65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 rflow pages with
13b66 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 out letting.** t
13b67 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f he cell size dro
13b68 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 p below the min
13b69 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
13b6a 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a fraction..**.**
13b6b 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 The min leaf pa
13b6c 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 yload fraction i
13b6d 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 s like the min e
13b6e 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
13b6f 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 fraction.** exce
13b70 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 pt that it appli
13b71 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 es to leaf nodes
13b72 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 in a LEAFDATA t
13b73 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 ree. The maximu
13b74 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 m.** payload fra
13b75 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 ction for a LEAF
13b76 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 DATA tree is alw
13b77 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 ays 100% (or 255
13b78 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 ) and it.** not
13b79 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
13b7a 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 header..**.** E
13b7b 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20 ach btree pages
13b7c 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 is divided into
13b7d 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 three sections:
13b7e 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 The header, the
13b7f 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 .** cell pointer
13b80 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 array, and the
13b81 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
13b82 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 a. Page 1 also
13b83 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a has a 100-byte.*
13b84 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 * file header th
13b85 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 at occurs before
13b86 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
13b87 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d ..**.** |--
13b88 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
13b89 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 ** | file h
13b8a 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 eader | 100
13b8b 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 bytes. Page 1
13b8c 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d only..** |-
13b8d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
13b8e 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 .** | page
13b8f 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 header | 8
13b90 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 bytes for leaves
13b91 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 . 12 bytes for
13b92 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a interior nodes.*
13b93 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
13b94 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
13b95 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 | cell pointer
13b96 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 | | 2 byte
13b97 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 s per cell. Sor
13b98 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 ted order..**
13b99 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 | array
13b9a 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 | | Grows
13b9b 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 downward.**
13b9c 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 |
13b9d 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 | v.**
13b9e 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
13b9f 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e --|.** | un
13ba0 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a allocated |.*
13ba1 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 * | space
13ba2 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 |.**
13ba3 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
13ba4 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 ---| ^ Grows
13ba5 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 upwards.**
13ba6 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 | cell content
13ba7 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 | | Arbitrar
13ba8 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 y order interspe
13ba9 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c rsed with freebl
13baa 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 ocks..** |
13bab 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c area |
13bac 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 | and free s
13bad 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a pace fragments..
13bae 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
13baf 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a ---------|.**.**
13bb0 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72 The page header
13bb1 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 s looks like thi
13bb2 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 s:.**.** OFFSE
13bb3 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 T SIZE DES
13bb4 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
13bb5 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20 0 1
13bb6 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 Flags. 1: intkey
13bb7 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 , 2: zerodata, 4
13bb8 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c : leafdata, 8: l
13bb9 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 eaf.** 1
13bba 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20 2 byte
13bbb 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 offset to the fi
13bbc 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a rst freeblock.**
13bbd 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20 3 2
13bbe 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 number of c
13bbf 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ells on this pag
13bc0 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 e.** 5
13bc1 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62 2 first b
13bc2 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 yte of the cell
13bc3 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 content area.**
13bc4 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20 7 1
13bc5 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 number of fr
13bc6 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 agmented free by
13bc7 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 tes.** 8
13bc8 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74 4 Right
13bc9 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 child (the Ptr(
13bca 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 N) value). Omit
13bcb 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a ted on leaves..*
13bcc 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 *.** The flags d
13bcd 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 efine the format
13bce 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70 of this btree p
13bcf 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 age. The leaf f
13bd0 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a lag means that.*
13bd1 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20 * this page has
13bd2 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 no children. Th
13bd3 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 e zerodata flag
13bd4 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 means that this
13bd5 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 page carries.**
13bd6 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f only keys and no
13bd7 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b data. The intk
13bd8 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 ey flag means th
13bd9 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 at the key is a
13bda 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 integer.** which
13bdb 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 is stored in th
13bdc 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 e key size entry
13bdd 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 of the cell hea
13bde 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 der rather than
13bdf 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 in.** the payloa
13be0 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 d area..**.** Th
13be1 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
13be2 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 rray begins on t
13be3 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66 he first byte af
13be4 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61 ter the page hea
13be5 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c der..** The cell
13be6 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 pointer array c
13be7 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 ontains zero or
13be8 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 more 2-byte numb
13be9 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a ers which are.**
13bea 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 offsets from th
13beb 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
13bec 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 he page to the c
13bed 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 ell content in t
13bee 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 he cell.** conte
13bef 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 nt area. The ce
13bf0 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 ll pointers occu
13bf1 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 r in sorted orde
13bf2 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 r. The system s
13bf3 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 trives.** to kee
13bf4 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74 p free space aft
13bf5 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c er the last cell
13bf6 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 pointer so that
13bf7 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a new cells can.*
13bf8 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 * be easily adde
13bf9 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 d without having
13bfa 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 to defragment t
13bfb 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 he page..**.** C
13bfc 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 ell content is s
13bfd 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72 tored at the ver
13bfe 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 y end of the pag
13bff 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 e and grows towa
13c00 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e rd the.** beginn
13c01 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e ing of the page.
13c02 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 .**.** Unused sp
13c03 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 ace within the c
13c04 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
13c05 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e is collected in
13c06 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 to a linked list
13c07 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b of.** freeblock
13c08 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f s. Each freeblo
13c09 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 ck is at least 4
13c0a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 bytes in size.
13c0b 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 The byte offset
13c0c 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
13c0d 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 freeblock is gi
13c0e 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 ven in the heade
13c0f 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f r. Freeblocks o
13c10 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 ccur in.** incre
13c11 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 asing order. Be
13c12 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 cause a freebloc
13c13 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 k must be at lea
13c14 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 st 4 bytes in si
13c15 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 ze,.** any group
13c16 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 of 3 or fewer u
13c17 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 nused bytes in t
13c18 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
13c19 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 area cannot.** e
13c1a 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 xist on the free
13c1b 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 block chain. A
13c1c 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 group of 3 or fe
13c1d 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69 wer free bytes i
13c1e 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 s called.** a fr
13c1f 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 agment. The tot
13c20 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
13c21 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 es in all fragme
13c22 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e nts is recorded.
13c23 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 .** in the page
13c24 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 header at offset
13c25 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 7..**.** SIZ
13c26 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e E DESCRIPTION
13c27 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
13c28 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 yte offset of th
13c29 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b e next freeblock
13c2a 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
13c2b 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 ytes in this fre
13c2c 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c eblock.**.** Cel
13c2d 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 ls are of variab
13c2e 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c le length. Cell
13c2f 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
13c30 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
13c31 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 area at.** the
13c32 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e end of the page.
13c33 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 Pointers to th
13c34 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 e cells are in t
13c35 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
13c36 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d array.** that im
13c37 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
13c38 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 s the page heade
13c39 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 r. Cells is not
13c3a 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 necessarily.**
13c3b 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e contiguous or in
13c3c 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c order, but cell
13c3d 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f pointers are co
13c3e 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 ntiguous and in
13c3f 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c order..**.** Cel
13c40 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 l content makes
13c41 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 use of variable
13c42 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e length integers.
13c43 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 A variable.**
13c44 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 length integer i
13c45 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 s 1 to 9 bytes w
13c46 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 here the lower 7
13c47 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a bits of each .*
13c48 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e * byte are used.
13c49 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f The integer co
13c4a 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 nsists of all by
13c4b 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69 tes that have bi
13c4c 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 t 8 set and.** t
13c4d 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69 he first byte wi
13c4e 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 th bit 8 clear.
13c4f 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 The most signif
13c50 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 icant byte of th
13c51 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 e integer.** app
13c52 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 ears first. A v
13c53 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
13c54 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 nteger may not b
13c55 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 e more than 9 by
13c56 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 tes long..** As
13c57 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 a special case,
13c58 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 all 8 bytes of t
13c59 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20 he 9th byte are
13c5a 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 used as data. T
13c5b 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 his.** allows a
13c5c 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 64-bit integer t
13c5d 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 o be encoded in
13c5e 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 9 bytes..**.**
13c5f 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 0x00
13c60 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
13c61 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a mes 0x00000000.
13c62 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20 ** 0x7f
13c63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c64 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
13c65 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 07f.** 0x81 0
13c66 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 x00
13c67 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
13c68 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 0000080.** 0x
13c69 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20 82 0x00
13c6a 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
13c6b 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 0x00000100.**
13c6c 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20 0x80 0x7f
13c6d 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
13c6e 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a mes 0x0000007f.
13c6f 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20 ** 0x8a 0x91
13c70 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20 0xd1 0xac 0x78
13c71 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 becomes 0x12345
13c72 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 678.** 0x81 0
13c73 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 x81 0x81 0x81 0x
13c74 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 01 becomes 0x1
13c75 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 0204081.**.** Va
13c76 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
13c77 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20 tegers are used
13c78 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 for rowids and t
13c79 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 o hold the numbe
13c7a 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 r of.** bytes of
13c7b 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e key and data in
13c7c 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a a btree cell..*
13c7d 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
13c7e 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 of a cell looks
13c7f 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
13c80 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
13c81 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
13c82 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
13c83 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 er of the left c
13c84 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 hild. Omitted if
13c85 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 leaf flag is se
13c86 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 t..** var
13c87 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
13c88 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 of data. Omitte
13c89 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 d if the zerodat
13c8a 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a a flag is set..*
13c8b 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d * var Num
13c8c 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
13c8d 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 key. Or the key
13c8e 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 itself if intkey
13c8f 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a flag is set..**
13c90 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c * Payl
13c91 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 oad.** 4
13c92 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 First page of
13c93 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 the overflow cha
13c94 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 in. Omitted if
13c95 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a no overflow.**.*
13c96 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 * Overflow pages
13c97 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c form a linked l
13c98 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 ist. Each page
13c99 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 except the last
13c9a 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a is completely.**
13c9b 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 filled with dat
13c9c 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 a (pagesize - 4
13c9d 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 bytes). The las
13c9e 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 t page can have
13c9f 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 as little.** as
13ca0 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 1 byte of data..
13ca1 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
13ca2 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
13ca3 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
13ca4 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f number of next o
13ca5 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 verflow page.**
13ca6 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a * Data.
13ca7 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 **.** Freelist p
13ca8 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f ages come in two
13ca9 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b subtypes: trunk
13caa 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 pages and leaf
13cab 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 pages. The.** f
13cac 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 ile header point
13cad 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 s to the first i
13cae 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
13caf 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 of trunk page.
13cb0 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 Each trunk.** pa
13cb1 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c ge points to mul
13cb2 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 tiple leaf pages
13cb3 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f . The content o
13cb4 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 f a leaf page is
13cb5 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e .** unspecified.
13cb6 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c A trunk page l
13cb7 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a ooks like this:.
13cb8 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
13cb9 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
13cba 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
13cbb 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 number of next t
13cbc 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 runk page.**
13cbd 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 4 Number o
13cbe 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 f leaf pointers
13cbf 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 on this page.**
13cc0 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 * zero
13cc1 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 or more pages nu
13cc2 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a mbers of leaves.
13cc3 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c */.../* The foll
13cc4 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 owing value is t
13cc5 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 he maximum cell
13cc6 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 size assuming a
13cc7 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 maximum page.**
13cc8 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e size give above.
13cc9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 .*/.#define MX_C
13cca 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 ELL_SIZE(pBt) (
13ccb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 pBt->pageSize-8)
13ccc 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d ../* The maximum
13ccd 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
13cce 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
13ccf 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
13cd0 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 e. This.** assu
13cd1 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 mes a minimum ce
13cd2 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 ll size of 6 byt
13cd3 65 73 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 es (4 bytes for
13cd4 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 the cell itself
13cd5 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 .** plus 2 bytes
13cd6 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 for the index t
13cd7 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 o the cell in th
13cd8 65 20 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 e page header).
13cd9 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 Such.** small c
13cda 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 ells will be rar
13cdb 65 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 20 e, but they are
13cdc 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 possible..*/.#de
13cdd 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 fine MX_CELL(pBt
13cde 29 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a ) ((pBt->pageSiz
13cdf 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 e-8)/6)../* Forw
13ce0 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
13ce1 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 */.typedef stru
13ce2 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 ct MemPage MemPa
13ce3 67 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ge;.typedef stru
13ce4 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b ct BtLock BtLock
13ce5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 ;../*.** This is
13ce6 20 61 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 a magic string
13ce7 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 that appears at
13ce8 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
13ce9 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 every.** SQLite
13cea 20 64 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 database in ord
13ceb 65 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 er to identify t
13cec 68 65 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 he file as a rea
13ced 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a l database..**.*
13cee 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 * You can change
13cef 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 this value at c
13cf0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 ompile-time by s
13cf1 70 65 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d pecifying a.** -
13cf2 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 DSQLITE_FILE_HEA
13cf3 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 DER="..." on the
13cf4 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
13cf5 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 d-line. The.**
13cf6 68 65 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 header must be e
13cf7 78 61 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 xactly 16 bytes
13cf8 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 including the ze
13cf9 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f ro-terminator so
13cfa 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 .** the string i
13cfb 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 tself should be
13cfc 31 35 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 15 characters lo
13cfd 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e ng. If you chan
13cfe 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 ge.** the header
13cff 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 , then your cust
13d00 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 om library will
13d01 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 not be able to r
13d02 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 ead .** database
13d03 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 s generated by t
13d04 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c he standard tool
13d05 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 s and the standa
13d06 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c rd tools.** will
13d07 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 not be able to
13d08 72 65 61 64 20 64 61 74 61 62 61 73 65 73 20 63 read databases c
13d09 72 65 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 reated by your c
13d0a 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a ustom library..*
13d0b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
13d0c 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 _FILE_HEADER /*
13d0d 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36 123456789 123456
13d0e 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 */.# define SQ
13d0f 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 LITE_FILE_HEADER
13d10 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 "SQLite format
13d11 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 3".#endif../*.**
13d12 20 50 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 Page type flags
13d13 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 . An ORed combi
13d14 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 nation of these
13d15 66 6c 61 67 73 20 61 70 70 65 61 72 20 61 73 20 flags appear as
13d16 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 the.** first byt
13d17 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 e of on-disk ima
13d18 67 65 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 ge of every BTre
13d19 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 e page..*/.#defi
13d1a 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 ne PTF_INTKEY
13d1b 20 30 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 0x01.#define PT
13d1c 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 F_ZERODATA 0x02
13d1d 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 .#define PTF_LEA
13d1e 46 44 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 FDATA 0x04.#def
13d1f 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 ine PTF_LEAF
13d20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 0x08../*.** As
13d21 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 each page of th
13d22 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 e file is loaded
13d23 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e into memory, an
13d24 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
13d25 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
13d26 72 75 63 74 75 72 65 20 69 73 20 61 70 70 65 6e ructure is appen
13d27 64 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ded and initiali
13d28 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 zed to zero. Th
13d29 69 73 20 73 74 72 75 63 74 75 72 65 20 73 74 6f is structure sto
13d2a 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 res.** informati
13d2b 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 on about the pag
13d2c 65 20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 e that is decode
13d2d 64 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 d from the raw f
13d2e 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 ile page..**.**
13d2f 54 68 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c The pParent fiel
13d30 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f d points back to
13d31 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
13d32 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 . This allows u
13d33 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 s to.** walk up
13d34 74 68 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 the BTree from a
13d35 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 ny leaf to the r
13d36 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 oot. Care must
13d37 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 be taken to.** u
13d38 6e 72 65 66 28 29 20 74 68 65 20 70 61 72 65 6e nref() the paren
13d39 74 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 t page pointer w
13d3a 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 hen this page is
13d3b 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 no longer refer
13d3c 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 enced..** The pa
13d3d 67 65 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 geDestructor() r
13d3e 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 outine handles t
13d3f 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a hat chore..**.**
13d40 20 41 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 Access to all f
13d41 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 ields of this st
13d42 72 75 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 ructure is contr
13d43 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 olled by the mut
13d44 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 ex.** stored in
13d45 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 MemPage.pBt->mut
13d46 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 ex..*/.struct Me
13d47 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 mPage {. u8 isI
13d48 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f nit; /
13d49 2a 20 54 72 75 65 20 69 66 20 70 72 65 76 69 6f * True if previo
13d4a 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 usly initialized
13d4b 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 . MUST BE FIRST!
13d4c 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c */. u8 nOverfl
13d4d 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ow; /* Nu
13d4e 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 mber of overflow
13d4f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 cell bodies in
13d50 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 aCell[] */. u8
13d51 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 intKey;
13d52 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 /* True if int
13d53 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 key flag is set
13d54 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 */. u8 leaf;
13d55 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
13d56 65 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 e if leaf flag i
13d57 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 s set */. u8 ha
13d58 73 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 sData;
13d59 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
13d5a 70 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 page stores data
13d5b 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 */. u8 hdrOffs
13d5c 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 et; /* 10
13d5d 30 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 0 for page 1. 0
13d5e 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 otherwise */.
13d5f 75 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b u8 childPtrSize;
13d60 20 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 /* 0 if lea
13d61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 f==1. 4 if leaf
13d62 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 ==0 */. u16 max
13d63 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a Local; /*
13d64 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 Copy of BtShare
13d65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 d.maxLocal or Bt
13d66 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a Shared.maxLeaf *
13d67 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c /. u16 minLocal
13d68 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 ; /* Copy
13d69 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e of BtShared.min
13d6a 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 Local or BtShare
13d6b 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 d.minLeaf */. u
13d6c 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 16 cellOffset;
13d6d 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
13d6e 61 44 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 aData of first c
13d6f 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
13d70 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
13d71 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
13d72 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e of free bytes on
13d73 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 the page */. u
13d74 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 16 nCell;
13d75 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
13d76 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 cells on this p
13d77 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f age, local and o
13d78 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 vfl */. u16 mas
13d79 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a kPage; /*
13d7a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f Mask for page o
13d7b 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 ffset */. struc
13d7c 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 t _OvflCell {
13d7d 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 /* Cells that wi
13d7e 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 ll not fit on aD
13d7f 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 ata[] */. u8
13d80 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 *pCell;
13d81 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 /* Pointers to
13d82 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 the body of the
13d83 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f overflow cell */
13d84 0a 20 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 . u16 idx;
13d85 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 /* Inse
13d86 72 74 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 rt this cell bef
13d87 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f ore idx-th non-o
13d88 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a verflow cell */.
13d89 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 } aOvfl[5];.
13d8a 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
13d8b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
13d8c 74 6f 20 42 74 53 68 61 72 65 64 20 74 68 61 74 to BtShared that
13d8d 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61 this page is pa
13d8e 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 rt of */. u8 *a
13d8f 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
13d90 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 /* Pointer to di
13d91 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 sk image of the
13d92 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 page data */. D
13d93 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
13d94 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 /* Pager pag
13d95 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 e handle */. Pg
13d96 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 no pgno;
13d97 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
13d98 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 r for this page
13d99 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
13d9a 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 in-memory image
13d9b 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 of a disk page
13d9c 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 has the auxiliar
13d9d 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 y information ap
13d9e 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 pended.** to the
13d9f 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a end. EXTRA_SIZ
13da0 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 E is the number
13da1 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
13da2 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 e needed to hold
13da3 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 .** that extra i
13da4 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 nformation..*/.#
13da5 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a define EXTRA_SIZ
13da6 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 E sizeof(MemPage
13da7 29 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 )../* A Btree ha
13da8 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 ndle.**.** A dat
13da9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13daa 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
13dab 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
13dac 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 ce of.** this ob
13dad 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 ject for every d
13dae 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 atabase file tha
13daf 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 t it has open.
13db0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a This structure.*
13db1 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 * is opaque to t
13db2 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
13db3 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 ection. The dat
13db4 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13db5 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 cannot.** see t
13db6 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 he internals of
13db7 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
13db8 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 nd only deals wi
13db9 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a th pointers to.*
13dba 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 * this structure
13dbb 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 ..**.** For some
13dbc 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c database files,
13dbd 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c the same underl
13dbe 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 ying database ca
13dbf 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a che might be .**
13dc0 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 shared between
13dc1 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 multiple connect
13dc2 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 ions. In that c
13dc3 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63 ase, each contec
13dc4 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f tion.** has it o
13dc5 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 wn pointer to th
13dc6 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 is object. But
13dc7 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 each instance of
13dc8 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 this object.**
13dc9 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 points to the sa
13dca 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 me BtShared obje
13dcb 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 ct. The databas
13dcc 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a e cache and the.
13dcd 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 ** schema associ
13dce 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 ated with the da
13dcf 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 tabase file are
13dd0 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 all contained wi
13dd1 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 thin.** the BtSh
13dd2 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a ared object..**.
13dd3 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e ** All fields in
13dd4 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
13dd5 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 are accessed und
13dd6 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 er sqlite3.mutex
13dd7 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 ..** The pBt poi
13dd8 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 nter itself may
13dd9 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 not be changed w
13dda 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 hile there exist
13ddb 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e s cursors .** in
13ddc 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 the referenced
13ddd 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 6f BtShared that po
13dde 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 int back to this
13ddf 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f Btree since tho
13de0 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 se.** cursors ha
13de1 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f ve to do go thro
13de2 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20 74 ugh this Btree t
13de3 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 o find their BtS
13de4 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 hared and.** the
13de5 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 y often do so wi
13de6 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 thout holding sq
13de7 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a lite3.mutex..*/.
13de8 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 struct Btree {.
13de9 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
13dea 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
13deb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 ase connection h
13dec 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 olding this btre
13ded 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
13dee 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 *pBt; /* Sha
13def 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 rable content of
13df0 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 this btree */.
13df1 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 u8 inTrans;
13df2 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e /* TRANS_NON
13df3 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 E, TRANS_READ or
13df4 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a TRANS_WRITE */.
13df5 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 u8 sharable;
13df6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13df7 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 we can share pBt
13df8 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 with another db
13df9 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b */. u8 locked;
13dfa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
13dfb 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 if db currently
13dfc 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 has pBt locked
13dfd 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c */. int wantToL
13dfe 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ock; /* Numbe
13dff 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c r of nested call
13e00 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 s to sqlite3Btre
13e01 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e eEnter() */. in
13e02 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20 t nBackup;
13e03 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 /* Number of ba
13e04 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20 ckup operations
13e05 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 72 reading this btr
13e06 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 ee */. Btree *p
13e07 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 Next; /* Li
13e08 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 st of other shar
13e09 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d able Btrees from
13e0a 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a the same db */.
13e0b 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 Btree *pPrev;
13e0c 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 /* Back poi
13e0d 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 nter of the same
13e0e 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a list */.};../*.
13e0f 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 ** Btree.inTrans
13e10 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 may take one of
13e11 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 the following v
13e12 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 alues..**.** If
13e13 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 the shared-data
13e14 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 extension is ena
13e15 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 bled, there may
13e16 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 be multiple user
13e17 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 s.** of the Btre
13e18 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 e structure. At
13e19 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 most one of thes
13e1a 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 e may open a wri
13e1b 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a te transaction,.
13e1c 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 ** but any numbe
13e1d 72 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 r may have activ
13e1e 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 e read transacti
13e1f 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ons..*/.#define
13e20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 TRANS_NONE 0.#d
13e21 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 efine TRANS_READ
13e22 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 1.#define TRAN
13e23 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a S_WRITE 2../*.**
13e24 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
13e25 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 this object repr
13e26 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 esents a single
13e27 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
13e28 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 * .** A single d
13e29 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e atabase file can
13e2a 20 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 be in use as th
13e2b 65 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 e same time by t
13e2c 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 wo.** or more da
13e2d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
13e2e 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 ns. When two or
13e2f 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e more connection
13e30 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 s are.** sharing
13e31 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
13e32 73 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f se file, each co
13e33 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 nnection has it
13e34 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 own.** private B
13e35 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 tree object for
13e36 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 the file and eac
13e37 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 h of those Btree
13e38 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 s points.** to t
13e39 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 his one BtShared
13e3a 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 object. BtShar
13e3b 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e ed.nRef is the n
13e3c 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e umber of.** conn
13e3d 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c ections currentl
13e3e 79 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 y sharing this d
13e3f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
13e40 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 .** Fields in th
13e41 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 is structure are
13e42 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 accessed under
13e43 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 the BtShared.mut
13e44 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 ex.** mutex, exc
13e45 65 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 ept for nRef and
13e46 20 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 pNext which are
13e47 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 accessed under
13e48 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 the.** global SQ
13e49 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
13e4a 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 C_MASTER mutex.
13e4b 20 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c The pPager fiel
13e4c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 d.** may not be
13e4d 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 modified once it
13e4e 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 is initially se
13e4f 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 t as long as nRe
13e50 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 f>0..** The pSch
13e51 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 ema field may be
13e52 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 set once under
13e53 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 BtShared.mutex a
13e54 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 nd.** thereafter
13e55 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 is unchanged as
13e56 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e long as nRef>0.
13e57 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 .**.** isPending
13e58 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 :.**.** If a B
13e59 74 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 tShared client f
13e5a 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 ails to obtain a
13e5b 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 write-lock on a
13e5c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 database.** t
13e5d 61 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 able (because th
13e5e 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f ere exists one o
13e5f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b r more read-lock
13e60 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c s on the table),
13e61 0a 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 .** the shared
13e62 2d 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 -cache enters 'p
13e63 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 ending-lock' sta
13e64 74 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 te and isPending
13e65 20 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 is.** set to
13e66 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 true..**.** Th
13e67 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c e shared-cache l
13e68 65 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 eaves the 'pendi
13e69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 ng lock' state w
13e6a 68 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a hen either of.**
13e6b 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 the following
13e6c 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 occur:.**.**
13e6d 20 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 1) The current
13e6e 20 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65 writer (BtShare
13e6f 64 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c d.pWriter) concl
13e70 75 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 udes its transac
13e71 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 tion, OR.**
13e72 32 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 2) The number of
13e73 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f locks held by o
13e74 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ther connections
13e75 20 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a drops to zero..
13e76 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e **.** while in
13e77 20 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f the 'pending-lo
13e78 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f ck' state, no co
13e79 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 nnection may sta
13e7a 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 rt a new.** tr
13e7b 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
13e7c 20 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 This feature
13e7d 69 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 is included to h
13e7e 65 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 elp prevent writ
13e7f 65 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a er-starvation..*
13e80 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 /.struct BtShare
13e81 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 d {. Pager *pPa
13e82 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ger; /* T
13e83 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f he page cache */
13e84 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
13e85 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
13e86 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
13e87 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 currently using
13e88 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 this Btree */.
13e89 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f BtCursor *pCurso
13e8a 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 r; /* A list
13e8b 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 of all open curs
13e8c 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ors */. MemPage
13e8d 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f *pPage1; /
13e8e 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 * First page of
13e8f 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
13e90 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 u8 readOnly;
13e91 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13e92 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e if the underlyin
13e93 67 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e g file is readon
13e94 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 ly */. u8 pageS
13e95 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a izeFixed; /*
13e96 20 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67 True if the pag
13e97 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f e size can no lo
13e98 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 nger be changed
13e99 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
13e9a 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
13e9b 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 M. u8 autoVacuu
13e9c 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 m; /* Tru
13e9d 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d e if auto-vacuum
13e9e 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 is enabled */.
13e9f 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 u8 incrVacuum;
13ea0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
13ea1 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 f incr-vacuum is
13ea2 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 enabled */.#end
13ea3 69 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a if. u16 pageSiz
13ea4 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f e; /* To
13ea5 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 tal number of by
13ea6 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f tes on a page */
13ea7 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a . u16 usableSiz
13ea8 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 e; /* Numb
13ea9 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 er of usable byt
13eaa 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 es on each page
13eab 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 */. u16 maxLoca
13eac 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 l; /* Ma
13ead 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c ximum local payl
13eae 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 oad in non-LEAFD
13eaf 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 ATA tables */.
13eb0 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 u16 minLocal;
13eb1 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d /* Minimum
13eb2 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 local payload i
13eb3 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 n non-LEAFDATA t
13eb4 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d ables */. u16 m
13eb5 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 axLeaf;
13eb6 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 /* Maximum loca
13eb7 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c l payload in a L
13eb8 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f EAFDATA table */
13eb9 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 . u16 minLeaf;
13eba 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 /* Mini
13ebb 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 mum local payloa
13ebc 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 d in a LEAFDATA
13ebd 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e table */. u8 in
13ebe 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 Transaction;
13ebf 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 /* Transaction
13ec0 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e state */. int n
13ec1 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 Transaction;
13ec2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 /* Number of op
13ec3 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 en transactions
13ec4 28 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a (read + write) *
13ec5 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d /. void *pSchem
13ec6 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 a; /* Poi
13ec7 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c nter to space al
13ec8 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 located by sqlit
13ec9 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 e3BtreeSchema()
13eca 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 */. void (*xFre
13ecb 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b eSchema)(void*);
13ecc 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
13ecd 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 for BtShared.pSc
13ece 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 hema */. sqlite
13ecf 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 3_mutex *mutex;
13ed0 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 /* Non-recursive
13ed1 20 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20 mutex required
13ed2 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73 to access this s
13ed3 74 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 truct */. Bitve
13ed4 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 c *pHasContent;
13ed5 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 /* Set of pages
13ed6 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c moved to free-l
13ed7 69 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 ist this transac
13ed8 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 tion */.#ifndef
13ed9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
13eda 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e ED_CACHE. int n
13edb 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
13edc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
13edd 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 ferences to this
13ede 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
13edf 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b BtShared *pNext;
13ee0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e /* Next on
13ee1 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 a list of shara
13ee2 62 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72 ble BtShared str
13ee3 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b ucts */. BtLock
13ee4 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 *pLock;
13ee5 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 /* List of locks
13ee6 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 held on this sh
13ee7 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 ared-btree struc
13ee8 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 t */. Btree *pW
13ee9 72 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 riter; /*
13eea 42 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65 Btree with curre
13eeb 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 ntly open write
13eec 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
13eed 20 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b u8 isExclusive;
13eee 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
13eef 66 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e f pWriter has an
13ef0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
13ef1 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 on the db */. u
13ef2 38 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 8 isPending;
13ef3 20 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 /* If waiti
13ef4 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b ng for read-lock
13ef5 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 s to clear */.#e
13ef6 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 ndif. u8 *pTmpS
13ef7 70 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 pace; /*
13ef8 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a BtShared.pageSiz
13ef9 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
13efa 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a for tmp use */.
13efb 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
13efc 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
13efd 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
13efe 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 is used to hold
13eff 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
13f00 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 about a cell. T
13f01 68 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 he parseCellPtr(
13f02 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 ) function fills
13f03 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 in this structu
13f04 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 re.** based on i
13f05 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 nformation extra
13f06 63 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 ct from the raw
13f07 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 disk page..*/.ty
13f08 70 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c pedef struct Cel
13f09 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a lInfo CellInfo;.
13f0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 struct CellInfo
13f0b 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 {. u8 *pCell;
13f0c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
13f0d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 the start of ce
13f0e 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 ll content */.
13f0f 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f i64 nKey; /
13f10 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e * The key for IN
13f11 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 TKEY tables, or
13f12 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
13f13 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 in key */. u32
13f14 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 nData; /* Nu
13f15 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
13f16 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e data */. u32 n
13f17 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 Payload; /* Tot
13f18 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 al amount of pay
13f19 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 load */. u16 nH
13f1a 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 eader; /* Size
13f1b 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e of the cell con
13f1c 74 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 tent header in b
13f1d 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c ytes */. u16 nL
13f1e 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 ocal; /* Amou
13f1f 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 nt of payload he
13f20 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 ld locally */.
13f21 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f u16 iOverflow; /
13f22 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 * Offset to over
13f23 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 flow page number
13f24 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 . Zero if no ov
13f25 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 erflow */. u16
13f26 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 nSize; /* Si
13f27 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 ze of the cell c
13f28 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 ontent on the ma
13f29 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a in b-tree page *
13f2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 /.};../*.** Maxi
13f2b 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 mum depth of an
13f2c 53 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 SQLite B-Tree st
13f2d 72 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 ructure. Any B-T
13f2e 72 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a ree deeper than.
13f2f 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 ** this will be
13f30 64 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 declared corrupt
13f31 2e 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 . This value is
13f32 63 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 calculated based
13f33 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d on a.** maximum
13f34 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f database size o
13f35 66 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d f 2^31 pages a m
13f36 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 inimum fanout of
13f37 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2 for a.** root
13f38 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 -node and 3 for
13f39 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e all other intern
13f3a 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 al nodes..**.**
13f3b 49 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 If a tree that a
13f3c 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c ppears to be tal
13f3d 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 ler than this is
13f3e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 encountered, it
13f3f 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 is.** assumed t
13f40 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
13f41 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a is corrupt..*/.
13f42 23 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 #define BTCURSOR
13f43 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f _MAX_DEPTH 20../
13f44 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 *.** A cursor is
13f45 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
13f46 70 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 particular entry
13f47 20 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 within a partic
13f48 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 ular.** b-tree w
13f49 69 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 ithin a database
13f4a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 file..**.** The
13f4b 20 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 entry is identi
13f4c 66 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 fied by its MemP
13f4d 61 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 age and the inde
13f4e 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e x in.** MemPage.
13f4f 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 aCell[] of the e
13f50 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e ntry..**.** When
13f51 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
13f52 73 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 se file can shar
13f53 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 ed by two more d
13f54 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13f55 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 ons,.** but curs
13f56 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 ors cannot be sh
13f57 61 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 ared. Each curs
13f58 6f 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 or is associated
13f59 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 with a.** parti
13f5a 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
13f5b 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 onnection identi
13f5c 66 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 fied BtCursor.pB
13f5d 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 tree.db..**.** F
13f5e 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 ields in this st
13f5f 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 ructure are acce
13f60 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 ssed under the B
13f61 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a tShared.mutex.**
13f62 20 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e found at self->
13f63 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a pBt->mutex. .*/.
13f64 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 struct BtCursor
13f65 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 {. Btree *pBtre
13f66 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
13f67 20 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 The Btree to wh
13f68 69 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 ich this cursor
13f69 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 belongs */. BtS
13f6a 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 hared *pBt;
13f6b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 /* The Bt
13f6c 53 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 Shared this curs
13f6d 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a or points to */.
13f6e 20 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 BtCursor *pNex
13f6f 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 t, *pPrev; /* F
13f70 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 orms a linked li
13f71 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 st of all cursor
13f72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 s */. struct Ke
13f73 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
13f74 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 /* Argument pas
13f75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f sed to compariso
13f76 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 n function */.
13f77 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 Pgno pgnoRoot;
13f78 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
13f79 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 root page of th
13f7a 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c is tree */. sql
13f7b 69 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 ite3_int64 cache
13f7c 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 dRowid; /* Next
13f7d 72 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 rowid cache. 0
13f7e 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 means not valid
13f7f 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e */. CellInfo in
13f80 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f fo; /
13f81 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 * A parse of the
13f82 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 cell we are poi
13f83 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 nting at */. u8
13f84 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 wrFlag;
13f85 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13f86 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 if writable */.
13f87 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 u8 atLast;
13f88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
13f89 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
13f8a 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
13f8b 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 */. u8 validNKe
13f8c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
13f8d 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e * True if info.n
13f8e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a Key is valid */.
13f8f 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 u8 eState;
13f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
13f91 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 ne of the CURSOR
13f92 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 _XXX constants (
13f93 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 see below) */.
13f94 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 void *pKey;
13f95 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 /* Saved key th
13f96 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 at was cursor's
13f97 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 last known posit
13f98 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 ion */. i64 nKe
13f99 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a y; /* Siz
13f9a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 e of pKey, or la
13f9b 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a st integer key *
13f9c 2f 0a 20 20 69 6e 74 20 73 6b 69 70 3b 20 20 20 /. int skip;
13f9d 20 20 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30 29 /* (skip<0)
13f9e 20 2d 3e 20 50 72 65 76 28 29 20 69 73 20 61 20 -> Prev() is a
13f9f 6e 6f 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29 20 no-op. (skip>0)
13fa0 2d 3e 20 4e 65 78 74 28 29 20 69 73 20 2a 2f 0a -> Next() is */.
13fa1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
13fa2 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 MIT_INCRBLOB. u
13fa3 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 8 isIncrblobHand
13fa4 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 le; /* True
13fa5 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 if this cursor
13fa6 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 is an incr. io h
13fa7 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 andle */. Pgno
13fa8 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 *aOverflow;
13fa9 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 /* Cache of
13faa 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c overflow page l
13fab 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 ocations */.#end
13fac 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 if.#ifndef NDEBU
13fad 47 0a 20 20 75 38 20 70 61 67 65 73 53 68 75 66 G. u8 pagesShuf
13fae 66 6c 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a fled; /*
13faf 20 54 72 75 65 20 69 66 20 42 74 72 65 65 20 70 True if Btree p
13fb0 61 67 65 73 20 61 72 65 20 72 65 61 72 72 61 6e ages are rearran
13fb1 67 65 64 20 62 79 20 62 61 6c 61 6e 63 65 28 29 ged by balance()
13fb2 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 */.#endif. i16
13fb3 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 iPage;
13fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fb5 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 /* Index of cu
13fb6 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 rrent page in ap
13fb7 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 Page */. MemPag
13fb8 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 e *apPage[BTCURS
13fb9 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 OR_MAX_DEPTH];
13fba 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f /* Pages from ro
13fbb 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 ot to current pa
13fbc 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 ge */. u16 aiId
13fbd 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 x[BTCURSOR_MAX_D
13fbe 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a EPTH]; /*
13fbf 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 Current index i
13fc0 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d n apPage[i] */.}
13fc1 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 ;../*.** Potenti
13fc2 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 al values for Bt
13fc3 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a Cursor.eState..*
13fc4 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 *.** CURSOR_VALI
13fc5 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 D:.** Cursor p
13fc6 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 oints to a valid
13fc7 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f entry. getPaylo
13fc8 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 ad() etc. may be
13fc9 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 called..**.** C
13fca 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a URSOR_INVALID:.*
13fcb 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 * Cursor does
13fcc 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 not point to a v
13fcd 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 alid entry. This
13fce 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 can happen (for
13fcf 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 example) .**
13fd0 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c because the tabl
13fd1 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 e is empty or be
13fd2 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73 6f cause BtreeCurso
13fd3 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 rFirst() has not
13fd4 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 been.** calle
13fd5 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f d..**.** CURSOR_
13fd6 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 REQUIRESEEK:.**
13fd7 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 The table that
13fd8 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 this cursor was
13fd9 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c opened on still
13fda 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 exists, but has
13fdb 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 been .** modi
13fdc 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 fied since the c
13fdd 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 ursor was last u
13fde 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 sed. The cursor
13fdf 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 position is save
13fe0 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 d.** in variab
13fe1 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 les BtCursor.pKe
13fe2 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e y and BtCursor.n
13fe3 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 Key. When a curs
13fe4 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 or is in .** t
13fe5 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f his state, resto
13fe6 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
13fe7 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 () can be called
13fe8 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a to attempt to.*
13fe9 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 * seek the cur
13fea 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 sor to the saved
13feb 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a position..**.**
13fec 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a CURSOR_FAULT:.*
13fed 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 * A unrecovera
13fee 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f ble error (an I/
13fef 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c O error or a mal
13ff0 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 loc failure) has
13ff1 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f occurred.** o
13ff2 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f n a different co
13ff3 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 nnection that sh
13ff4 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72 65 ares the BtShare
13ff5 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68 69 d cache with thi
13ff6 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 s.** cursor.
13ff7 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 The error has le
13ff8 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 ft the cache in
13ff9 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
13ffa 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e state..** Do n
13ffb 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 othing else with
13ffc 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 this cursor. A
13ffd 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
13ffe 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 e the cursor.**
13fff 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 should return
14000 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 the error code s
14001 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f tored in BtCurso
14002 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e r.skip.*/.#defin
14003 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 e CURSOR_INVALID
14004 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 0.#de
14005 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 fine CURSOR_VALI
14006 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a D 1.
14007 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 #define CURSOR_R
14008 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 EQUIRESEEK
14009 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 2.#define CURSO
1400a 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 R_FAULT
1400b 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 3../* .** Th
1400c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
1400d 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 the PENDING_BYTE
1400e 20 6f 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 occupies. This
1400f 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 page is never us
14010 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 ed..*/.# define
14011 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
14012 45 28 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f E(pBt) PAGER_MJ_
14013 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a PGNO(pBt)../*.**
14014 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f A linked list o
14015 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
14016 73 74 72 75 63 74 75 72 65 73 20 69 73 20 73 74 structures is st
14017 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 65 64 ored at BtShared
14018 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 .pLock..** Locks
14019 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 are added (or u
1401a 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 pgraded from REA
1401b 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f D_LOCK to WRITE_
1401c 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 LOCK) when a cur
1401d 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 sor .** is opene
1401e 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 d on the table w
1401f 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 ith root page Bt
14020 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c Shared.iTable. L
14021 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 ocks are removed
14022 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 .** from this li
14023 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 st when a transa
14024 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
14025 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 ed or rolled bac
14026 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 k, or when.** a
14027 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 btree handle is
14028 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 closed..*/.struc
14029 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 t BtLock {. Btr
1402a 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 ee *pBtree;
1402b 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 /* Btree hand
1402c 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 le holding this
1402d 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 lock */. Pgno i
1402e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
1402f 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 /* Root page of
14030 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c table */. u8 eL
14031 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
14032 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 /* READ_LOCK or
14033 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 WRITE_LOCK */.
14034 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 BtLock *pNext;
14035 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
14036 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b n BtShared.pLock
14037 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 list */.};../*
14038 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 Candidate values
14039 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 for BtLock.eLoc
1403a 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 k */.#define REA
1403b 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 D_LOCK 1.#de
1403c 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 fine WRITE_LOCK
1403d 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 2../*.** Thes
1403e 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 e macros define
1403f 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 the location of
14040 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
14041 65 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 entry for a .**
14042 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 database page. T
14043 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
14044 74 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 t to each is the
14045 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c number of usabl
14046 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 e.** bytes on ea
14047 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 ch page of the d
14048 61 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 atabase (often 1
14049 30 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 024). The second
1404a 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 is the.** page
1404b 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 number to look u
1404c 70 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 p in the pointer
1404d 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d map..**.** PTRM
1404e 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e AP_PAGENO return
1404f 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 s the database p
14050 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
14051 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a e pointer-map.**
14052 20 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 page that store
14053 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 s the required p
14054 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 ointer. PTRMAP_P
14055 54 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 TROFFSET returns
14056 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f .** the offset o
14057 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
14058 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a map entry..**.**
14059 20 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 If the pgno arg
1405a 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
1405b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 PTRMAP_PAGENO is
1405c 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
1405d 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e age,.** then pgn
1405e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 o is returned. S
1405f 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f o (pgno==PTRMAP_
14060 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e PAGENO(pgsz, pgn
14061 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 o)) can be.** us
14062 65 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 ed to test if pg
14063 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d no is a pointer-
14064 6d 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 map page. PTRMAP
14065 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e _ISPAGE implemen
14066 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e ts.** this test.
14067 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d .*/.#define PTRM
14068 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 AP_PAGENO(pBt, p
14069 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e gno) ptrmapPagen
1406a 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 o(pBt, pgno).#de
1406b 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f fine PTRMAP_PTRO
1406c 46 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 FFSET(pgptrmap,
1406d 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 pgno) (5*(pgno-p
1406e 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 gptrmap-1)).#def
1406f 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 ine PTRMAP_ISPAG
14070 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 E(pBt, pgno) (PT
14071 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 RMAP_PAGENO((pBt
14072 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f ),(pgno))==(pgno
14073 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f ))../*.** The po
14074 69 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c inter map is a l
14075 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 ookup table that
14076 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14077 70 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a parent page for.
14078 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 ** each child pa
14079 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
1407a 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 se file. The pa
1407b 72 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 rent page is the
1407c 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f page that.** co
1407d 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
1407e 20 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 to the child.
1407f 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 Every page in th
14080 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 e database conta
14081 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 ins.** 0 or 1 pa
14082 72 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e rent pages. (In
14083 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 this context 'd
14084 61 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 atabase page' re
14085 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 fers.** to any p
14086 61 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 age that is not
14087 70 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e part of the poin
14088 74 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 ter map itself.)
14089 20 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d Each pointer m
1408a 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 ap.** entry cons
1408b 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 ists of a single
1408c 20 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 byte 'type' and
1408d 20 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 a 4 byte parent
1408e 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a page number..**
1408f 20 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 The PTRMAP_XXX
14090 69 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f identifiers belo
14091 77 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 w are the valid
14092 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 types..**.** The
14093 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 purpose of the
14094 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 pointer map is t
14095 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e o facility movin
14096 67 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 g pages from one
14097 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 .** position in
14098 74 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 the file to anot
14099 68 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 her as part of a
1409a 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e utovacuum. When
1409b 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f a page.** is mo
1409c 76 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 ved, the pointer
1409d 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d in its parent m
1409e 75 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 ust be updated t
1409f 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a o point to the.*
140a0 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 * new location.
140a1 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
140a2 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 is used to loca
140a3 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 te the parent pa
140a4 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a ge quickly..**.*
140a5 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 * PTRMAP_ROOTPAG
140a6 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 E: The database
140a7 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 page is a root-p
140a8 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 age. The page-nu
140a9 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 mber is not.**
140aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140ab 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 used in this cas
140ac 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f e..**.** PTRMAP_
140ad 46 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 FREEPAGE: The da
140ae 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 tabase page is a
140af 6e 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 n unused (free)
140b0 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e page. The page-n
140b1 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 umber .**
140b2 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f is no
140b3 74 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 t used in this c
140b4 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 ase..**.** PTRMA
140b5 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 P_OVERFLOW1: The
140b6 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 database page i
140b7 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 s the first page
140b8 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a in a list of .*
140b9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
140ba 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 overflow pag
140bb 65 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d es. The page num
140bc 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 ber identifies t
140bd 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 he page that.**
140be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140bf 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 contains the c
140c0 65 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 ell with a point
140c1 65 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 er to this overf
140c2 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 low page..**.**
140c3 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
140c4 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
140c5 61 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e age is the secon
140c6 64 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 d or later page
140c7 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 in a list of.**
140c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140c9 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 overflow pages
140ca 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 . The page-numbe
140cb 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 r identifies the
140cc 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 previous.**
140cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
140ce 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 age in the overf
140cf 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a low page list..*
140d0 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 *.** PTRMAP_BTRE
140d1 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 E: The database
140d2 70 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f page is a non-ro
140d3 6f 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 ot btree page. T
140d4 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a he page number.*
140d5 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
140d6 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
140d7 61 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 arent page in th
140d8 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 e btree..*/.#def
140d9 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 ine PTRMAP_ROOTP
140da 41 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 AGE 1.#define PT
140db 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a RMAP_FREEPAGE 2.
140dc 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f #define PTRMAP_O
140dd 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 VERFLOW1 3.#defi
140de 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ne PTRMAP_OVERFL
140df 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 OW2 4.#define PT
140e0 52 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a RMAP_BTREE 5../*
140e1 20 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 A bunch of asse
140e2 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
140e3 74 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 to check the tra
140e4 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 nsaction state v
140e5 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 ariables.** of h
140e6 61 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 andle p (type Bt
140e7 72 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e ree*) are intern
140e8 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e ally consistent.
140e9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 .*/.#define btre
140ea 65 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a eIntegrity(p) \.
140eb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 assert( p->pBt
140ec 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 ->inTransaction!
140ed 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 =TRANS_NONE || p
140ee 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 ->pBt->nTransact
140ef 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 ion==0 ); \. as
140f0 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e sert( p->pBt->in
140f1 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e Transaction>=p->
140f2 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a inTrans ); .../*
140f3 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 .** The ISAUTOVA
140f4 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 CUUM macro is us
140f5 65 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 ed within balanc
140f6 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 e_nonroot() to d
140f7 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 etermine.** if t
140f8 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 he database supp
140f9 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d orts auto-vacuum
140fa 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 or not. Because
140fb 20 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 it is used.** w
140fc 69 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 ithin an express
140fd 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 ion that is an a
140fe 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 rgument to anoth
140ff 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 er macro .** (sq
14100 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 liteMallocRaw),
14101 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
14102 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 le to use condit
14103 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f ional compilatio
14104 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d n..** So, this m
14105 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 acro is defined
14106 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e instead..*/.#ifn
14107 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14108 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 AUTOVACUUM.#defi
14109 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 ne ISAUTOVACUUM
1410a 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d (pBt->autoVacuum
1410b 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
1410c 49 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 ISAUTOVACUUM 0.#
1410d 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
1410e 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
1410f 70 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 passed around th
14110 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 rough all the sa
14111 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f nity checking ro
14112 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 utines.** in ord
14113 65 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b er to keep track
14114 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 of some global
14115 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f state informatio
14116 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
14117 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b ruct IntegrityCk
14118 20 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 IntegrityCk;.st
14119 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b ruct IntegrityCk
1411a 20 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 {. BtShared *p
1411b 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 Bt; /* The tr
1411c 65 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 ee being checked
1411d 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 out */. Pager
1411e 2a 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 *pPager; /* T
1411f 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 he associated pa
14120 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 ger. Also acces
14121 73 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 sible by pBt->pP
14122 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e ager */. Pgno n
14123 50 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e Page; /* N
14124 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
14125 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a n the database *
14126 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 /. int *anRef;
14127 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
14128 6f 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 of times each pa
14129 67 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 ge is referenced
1412a 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b */. int mxErr;
1412b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 /* Stop
1412c 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 accumulating err
1412d 6f 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 ors when this re
1412e 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 aches zero */.
1412f 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 int nErr;
14130 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
14131 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 essages written
14132 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 to zErrMsg so fa
14133 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f r */. int mallo
14134 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 cFailed; /* A me
14135 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
14136 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
14137 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d ed */. StrAccum
14138 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 errMsg; /* Acc
14139 75 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f umulate the erro
1413a 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 r message text h
1413b 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ere */.};../*.**
1413c 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 Read or write a
1413d 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 two- and four-b
1413e 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
1413f 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a nteger values..*
14140 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 /.#define get2by
14141 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c te(x) ((x)[0]<
14142 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 <8 | (x)[1]).#de
14143 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c fine put2byte(p,
14144 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 v) ((p)[0] = (u8
14145 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 )((v)>>8), (p)[1
14146 5d 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 ] = (u8)(v)).#de
14147 66 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 fine get4byte sq
14148 6c 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 lite3Get4byte.#d
14149 65 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 efine put4byte s
1414a 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a qlite3Put4byte..
1414b 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72 /*.** Internal r
1414c 6f 75 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f outines that sho
1414d 75 6c 64 20 62 65 20 61 63 63 65 73 73 65 64 20 uld be accessed
1414e 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 by the btree lay
1414f 65 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 er only..*/.SQLI
14150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
14151 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
14152 67 65 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67 ge(BtShared*, Pg
14153 6e 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69 no, MemPage**, i
14154 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
14155 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
14156 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d treeInitPage(Mem
14157 50 61 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51 Page *pPage);.SQ
14158 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14159 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 d sqlite3BtreePa
1415a 72 73 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61 rseCellPtr(MemPa
1415b 67 65 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e ge*, u8*, CellIn
1415c 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 fo*);.SQLITE_PRI
1415d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1415e 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 3BtreeParseCell(
1415f 4d 65 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43 MemPage*, int, C
14160 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 ellInfo*);.SQLIT
14161 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14162 6c 69 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 lite3BtreeRestor
14163 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
14164 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b BtCursor *pCur);
14165 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14166 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14167 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 eGetTempCursor(B
14168 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 tCursor *pCur, B
14169 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 tCursor *pTempCu
1416a 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
1416b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
1416c 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 treeReleaseTempC
1416d 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a ursor(BtCursor *
1416e 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pCur);.SQLITE_PR
1416f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14170 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 e3BtreeMoveToPar
14171 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ent(BtCursor *pC
14172 75 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ur);../*********
14173 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 ***** End of btr
14174 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a eeInt.h ********
14175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14177 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
14178 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
14179 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
1417a 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 ff in btmutex.c
1417b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1417c 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ****/.#ifndef SQ
1417d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1417e 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 _CACHE.#if SQLIT
1417f 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a E_THREADSAFE../*
14180 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 .** Obtain the B
14181 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 tShared mutex as
14182 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d sociated with B-
14183 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 Tree handle p. A
14184 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 lso,.** set BtSh
14185 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 ared.db to the d
14186 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 atabase handle a
14187 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
14188 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c and the.** p->l
14189 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f ocked boolean to
1418a 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
1418b 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d void lockBtreeM
1418c 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a utex(Btree *p){.
1418d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 assert( p->loc
1418e 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ked==0 );. asse
1418f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14190 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 x_notheld(p->pBt
14191 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
14192 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14193 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
14194 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c mutex) );.. sql
14195 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
14196 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
14197 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 . p->pBt->db =
14198 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b p->db;. p->lock
14199 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 1;.}../*.**
1419a 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 Release the BtS
1419b 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f hared mutex asso
1419c 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 ciated with B-Tr
1419d 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a ee handle p and.
1419e 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e ** clear the p->
1419f 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a locked boolean..
141a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
141a1 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 nlockBtreeMutex(
141a2 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
141a3 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d ert( p->locked==
141a4 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 1 );. assert( s
141a5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
141a6 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 d(p->pBt->mutex)
141a7 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
141a8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
141a9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
141aa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
141ab 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b b==p->pBt->db );
141ac 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
141ad 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e x_leave(p->pBt->
141ae 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 mutex);. p->loc
141af 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a ked = 0;.}../*.*
141b0 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20 * Enter a mutex
141b1 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 on the given BTr
141b2 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a ee object..**.**
141b3 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69 If the object i
141b4 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 s not sharable,
141b5 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 then no mutex is
141b6 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a ever required.*
141b7 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 * and this routi
141b8 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 ne is a no-op.
141b9 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d The underlying m
141ba 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 utex is non-recu
141bb 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 rsive..** But we
141bc 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e 63 keep a referenc
141bd 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 e count in Btree
141be 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 .wantToLock so t
141bf 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f he behavior.** o
141c0 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 f this interface
141c1 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a is recursive..*
141c2 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 *.** To avoid de
141c3 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c adlocks, multipl
141c4 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63 e Btrees are loc
141c5 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ked in the same
141c6 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 order.** by all
141c7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
141c8 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e ions. The p->pN
141c9 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 ext is a list of
141ca 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 other.** Btrees
141cb 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 belonging to th
141cc 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
141cd 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 connection as th
141ce 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 e p Btree.** whi
141cf 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f ch need to be lo
141d0 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 49 cked after p. I
141d1 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 f we cannot get
141d2 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 a lock on.** p,
141d3 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 then first unloc
141d4 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 k all of the oth
141d5 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c ers on p->pNext,
141d6 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f then wait.** fo
141d7 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 r the lock to be
141d8 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f come available o
141d9 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b n p, then relock
141da 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 all of the.** s
141db 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 73 ubsequent Btrees
141dc 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20 6c that desire a l
141dd 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ock..*/.SQLITE_P
141de 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
141df 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 te3BtreeEnter(Bt
141e0 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 ree *p){. Btree
141e1 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 *pLater;.. /*
141e2 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 Some basic sanit
141e3 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 y checking on th
141e4 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69 e Btree. The li
141e5 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a st of Btrees. *
141e6 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 * connected by p
141e7 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 73 Next and pPrev s
141e8 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 hould be in sort
141e9 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a ed order by. **
141ea 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65 Btree.pBt value
141eb 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f . All elements o
141ec 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c f the list shoul
141ed 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a d belong to. **
141ee 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 the same connec
141ef 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 tion. Only share
141f0 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20 d Btrees are on
141f1 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 the list. */. a
141f2 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d ssert( p->pNext=
141f3 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e =0 || p->pNext->
141f4 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 pBt>p->pBt );.
141f5 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 assert( p->pPrev
141f6 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d ==0 || p->pPrev-
141f7 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 >pBt<p->pBt );.
141f8 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 assert( p->pNex
141f9 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 t==0 || p->pNext
141fa 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 ->db==p->db );.
141fb 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 assert( p->pPre
141fc 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 v==0 || p->pPrev
141fd 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 ->db==p->db );.
141fe 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
141ff 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 able || (p->pNex
14200 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76 t==0 && p->pPrev
14201 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 ==0) );.. /* Ch
14202 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 eck for locking
14203 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 consistency */.
14204 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 assert( !p->loc
14205 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f ked || p->wantTo
14206 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 Lock>0 );. asse
14207 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
14208 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
14209 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 ==0 );.. /* We
1420a 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 should already h
1420b 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 old a lock on th
1420c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1420d 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 ction */. asser
1420e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1420f 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
14210 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c ex) );.. /* Unl
14211 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
14212 20 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64 is sharable and
14213 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 unlocked, then
14214 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a BtShared.db. **
14215 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
14216 62 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 be set correctly
14217 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 . */. assert( (
14218 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 p->locked==0 &&
14219 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 p->sharable) ||
1421a 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 p->pBt->db==p->d
1421b 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e b );.. if( !p->
1421c 73 68 61 72 61 62 6c 65 20 29 20 72 65 74 75 72 sharable ) retur
1421d 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f n;. p->wantToLo
1421e 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c ck++;. if( p->l
1421f 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a ocked ) return;.
14220 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 . /* In most ca
14221 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 ses, we should b
14222 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 e able to acquir
14223 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 e the lock we.
14224 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20 ** want without
14225 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 having to go thr
14226 6f 75 67 68 74 20 74 68 65 20 61 73 63 65 6e 64 ought the ascend
14227 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 ing lock. ** pr
14228 6f 63 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c ocedure that fol
14229 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73 lows. Just be s
1422a 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b ure not to block
1422b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c .. */. if( sql
1422c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 ite3_mutex_try(p
1422d 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 ->pBt->mutex)==S
1422e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1422f 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e p->pBt->db = p->
14230 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 db;. p->locke
14231 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 d = 1;. retur
14232 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 n;. }.. /* To
14233 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 avoid deadlock,
14234 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c first release al
14235 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c l locks with a l
14236 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 arger. ** BtSha
14237 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 68 red address. Th
14238 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c en acquire our l
14239 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71 ock. Then reacq
1423a 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 uire. ** the ot
1423b 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63 her BtShared loc
1423c 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64 20 ks that we used
1423d 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e to hold in ascen
1423e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e ding. ** order.
1423f 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 . */. for(pLat
14240 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 er=p->pNext; pLa
14241 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 ter; pLater=pLat
14242 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 er->pNext){.
14243 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e assert( pLater->
14244 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 sharable );.
14245 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e assert( pLater->
14246 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 pNext==0 || pLat
14247 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 er->pNext->pBt>p
14248 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 Later->pBt );.
14249 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 65 assert( !pLate
1424a 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 r->locked || pLa
1424b 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e ter->wantToLock>
1424c 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 0 );. if( pLa
1424d 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 ter->locked ){.
1424e 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
1424f 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 Mutex(pLater);.
14250 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 }. }. lockB
14251 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14252 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e for(pLater=p->pN
14253 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 ext; pLater; pLa
14254 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 ter=pLater->pNex
14255 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 t){. if( pLat
14256 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 er->wantToLock )
14257 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 {. lockBtre
14258 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a eMutex(pLater);.
14259 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1425a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 75 ** Exit the recu
1425b 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 rsive mutex on a
1425c 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 Btree..*/.SQLIT
1425d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1425e 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1425f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 (Btree *p){. if
14260 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b ( p->sharable ){
14261 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
14262 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
14263 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14264 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e k--;. if( p->
14265 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
14266 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
14267 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14268 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 }. }.}..#ifndef
14269 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 NDEBUG./*.** Re
1426a 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
1426b 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 BtShared mutex
1426c 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 is held on the b
1426d 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a tree, or if the.
1426e 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 ** B-Tree is not
1426f 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72 61 marked as shara
14270 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ble..**.** This
14271 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
14272 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e only from within
14273 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
14274 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
14275 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14276 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
14277 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ex(Btree *p){.
14278 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14279 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 ble==0 || p->loc
1427a 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e ked==0 || p->wan
1427b 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 tToLock>0 );. a
1427c 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
1427d 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b le==0 || p->lock
1427e 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d ed==0 || p->db==
1427f 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 p->pBt->db );.
14280 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14281 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 ble==0 || p->loc
14282 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ked==0 || sqlite
14283 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
14284 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14285 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
14286 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f able==0 || p->lo
14287 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 cked==0 || sqlit
14288 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14289 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
1428a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 return (p->sha
1428b 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c rable==0 || p->l
1428c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 ocked);.}.#endif
1428d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1428e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
1428f 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 /*.** Enter and
14290 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e leave a mutex on
14291 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 a Btree given a
14292 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 cursor owned by
14293 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 that.** Btree.
14294 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 These entry poi
14295 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20 nts are used by
14296 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 incremental I/O
14297 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d and can be.** om
14298 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f itted if that mo
14299 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 dule is not used
1429a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1429b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1429c 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 BtreeEnterCursor
1429d 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
1429e 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 {. sqlite3Btree
1429f 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 Enter(pCur->pBtr
142a0 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ee);.}.SQLITE_PR
142a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
142a2 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
142a3 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
142a4 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 r){. sqlite3Btr
142a5 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 eeLeave(pCur->pB
142a6 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 tree);.}.#endif
142a7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
142a8 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a NCRBLOB */.../*.
142a9 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 ** Enter the mut
142aa 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 ex on every Btre
142ab 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
142ac 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 h a database.**
142ad 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 connection. Thi
142ae 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 s is needed (for
142af 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 example) prior
142b0 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 to parsing.** a
142b1 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 statement since
142b2 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 we will be compa
142b3 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 ring table and c
142b4 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 olumn names.** a
142b5 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d gainst all schem
142b6 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 as and we do not
142b7 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 want those sche
142b8 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 mas being.** res
142b9 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 et out from unde
142ba 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 r us..**.** Ther
142bb 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e e is a correspon
142bc 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 ding leave-all p
142bd 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a rocedures..**.**
142be 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 Enter the mutex
142bf 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 es in accending
142c0 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65 order by BtShare
142c1 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 d pointer addres
142c2 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 s.** to avoid th
142c3 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 e possibility of
142c4 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 deadlock when t
142c5 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a wo threads with.
142c6 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 ** two or more b
142c7 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 trees in common
142c8 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b both try to lock
142c9 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 all their btree
142ca 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 s.** at the same
142cb 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c instant..*/.SQL
142cc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
142cd 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
142ce 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 erAll(sqlite3 *d
142cf 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 b){. int i;. B
142d0 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 tree *p, *pLater
142d1 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
142d2 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
142d3 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 b->mutex) );. f
142d4 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
142d5 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d b; i++){. p =
142d6 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
142d7 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 20 . assert( !p
142d8 7c 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 || (p->locked==0
142d9 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 && p->sharable)
142da 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d || p->pBt->db==
142db 70 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28 p->db );. if(
142dc 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c p && p->sharabl
142dd 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 e ){. p->wa
142de 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 ntToLock++;.
142df 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 if( !p->locked
142e0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
142e1 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
142e2 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 k==1 );.
142e3 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 while( p->pPrev
142e4 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a ) p = p->pPrev;.
142e5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
142e6 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70 ->locked && p->p
142e7 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e Next ) p = p->pN
142e8 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ext;. for
142e9 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 (pLater = p->pNe
142ea 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 xt; pLater; pLat
142eb 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 er=pLater->pNext
142ec 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
142ed 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 pLater->locked
142ee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
142ef 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 nlockBtreeMutex(
142f0 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 pLater);.
142f1 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
142f2 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 while( p
142f3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 ){. loc
142f4 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
142f5 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d p = p-
142f6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
142f7 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
142f8 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 }.}.SQLITE_PRI
142f9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
142fa 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 3BtreeLeaveAll(s
142fb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
142fc 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 nt i;. Btree *p
142fd 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
142fe 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
142ff 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 b->mutex) );. f
14300 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
14301 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d b; i++){. p =
14302 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
14303 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d . if( p && p-
14304 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
14305 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 assert( p->wa
14306 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14307 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14308 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 k--;. if( p
14309 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
1430a 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 ){. unloc
1430b 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
1430c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1430d 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 }.}..#ifndef NDE
1430e 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e BUG./*.** Return
1430f 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72 true if the cur
14310 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 rent thread hold
14311 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 s the database c
14312 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 onnection.** mut
14313 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 ex and all requi
14314 72 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 red BtShared mut
14315 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 exes..**.** This
14316 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
14317 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
14318 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
14319 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1431a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1431b 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
1431c 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 xes(sqlite3 *db)
1431d 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
1431e 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f !sqlite3_mutex_
1431f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
14320 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
14321 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
14322 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
14323 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 . Btree *p;.
14324 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 p = db->aDb[i
14325 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 ].pBt;. if( p
14326 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 && p->sharable
14327 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e &&. (p->
14328 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c wantToLock==0 ||
14329 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f !sqlite3_mutex_
1432a 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 held(p->pBt->mut
1432b 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ex)) ){. re
1432c 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
1432d 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a }. return 1;.}.
1432e 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 #endif /* NDEBUG
1432f 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 */../*.** Add a
14330 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 new Btree point
14331 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 er to a BtreeMut
14332 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 exArray. .** if
14333 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 the pointer can
14334 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 possibly be shar
14335 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 ed with.** anoth
14336 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e er database conn
14337 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ection..**.** Th
14338 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b e pointers are k
14339 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 ept in sorted or
1433a 64 65 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 der by pBtree->p
1433b 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 Bt. That.** way
1433c 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 when we go to e
1433d 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 nter all the mut
1433e 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 exes, we can ent
1433f 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 er them.** in or
14340 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 der without ever
14341 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b y having to back
14342 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 up and retry and
14343 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 without.** worr
14344 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c ying about deadl
14345 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ock..**.** The n
14346 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 umber of shared
14347 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 btrees will alwa
14348 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 ys be small (usu
14349 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 ally 0 or 1).**
1434a 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 so an insertion
1434b 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 sort is an adequ
1434c 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 ate algorithm he
1434d 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 re..*/.SQLITE_PR
1434e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1434f 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
14350 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 yInsert(BtreeMut
14351 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c exArray *pArray,
14352 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b Btree *pBtree){
14353 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 . int i, j;. B
14354 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 tShared *pBt;.
14355 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c if( pBtree==0 ||
14356 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c pBtree->sharabl
14357 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 e==0 ) return;.#
14358 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
14359 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
1435a 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
1435b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 i++){. ass
1435c 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 ert( pArray->aBt
1435d 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 ree[i]!=pBtree )
1435e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
1435f 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 if. assert( pAr
14360 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 ray->nMutex>=0 )
14361 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 ;. assert( pArr
14362 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 ay->nMutex<Array
14363 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 Size(pArray->aBt
14364 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 ree)-1 );. pBt
14365 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 = pBtree->pBt;.
14366 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 for(i=0; i<pArr
14367 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 ay->nMutex; i++)
14368 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 {. assert( pA
14369 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 rray->aBtree[i]!
1436a 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 =pBtree );. i
1436b 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 f( pArray->aBtre
1436c 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b e[i]->pBt>pBt ){
1436d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 . for(j=pAr
1436e 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 ray->nMutex; j>i
1436f 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 ; j--){.
14370 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
14371 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 ] = pArray->aBtr
14372 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d ee[j-1];. }
14373 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 . pArray->a
14374 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 Btree[i] = pBtre
14375 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d e;. pArray-
14376 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 >nMutex++;.
14377 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
14378 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 }. pArray->aBt
14379 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 ree[pArray->nMut
1437a 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a ex++] = pBtree;.
1437b 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 }../*.** Enter t
1437c 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 he mutex of ever
1437d 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 y btree in the a
1437e 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 rray. This rout
1437f 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 ine is.** called
14380 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
14381 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 g of sqlite3Vdbe
14382 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 Exec(). The mut
14383 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 exes are.** exit
14384 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ed at the end of
14385 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
14386 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
14387 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14388 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
14389 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 yEnter(BtreeMute
1438a 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b xArray *pArray){
1438b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
1438c 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e i=0; i<pArray->n
1438d 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 Mutex; i++){.
1438e 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 Btree *p = pArr
1438f 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 ay->aBtree[i];.
14390 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 /* Some basic
14391 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
14392 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
14393 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e i==0 || pArray->
14394 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 aBtree[i-1]->pBt
14395 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 <p->pBt );. a
14396 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 ssert( !p->locke
14397 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f d || p->wantToLo
14398 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 ck>0 );.. /*
14399 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 We should alread
1439a 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e y hold a lock on
1439b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
1439c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nnection */.
1439d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1439e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
1439f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 ->mutex) );..
143a0 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b p->wantToLock++
143a1 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f ;. if( !p->lo
143a2 63 6b 65 64 20 26 26 20 70 2d 3e 73 68 61 72 61 cked && p->shara
143a3 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 ble ){. loc
143a4 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
143a5 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
143a6 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 ** Leave the mut
143a7 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 ex of every btre
143a8 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a e in the group..
143a9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
143aa 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
143ab 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
143ac 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 ve(BtreeMutexArr
143ad 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
143ae 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
143af 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
143b0 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
143b1 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
143b2 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
143b3 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
143b4 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
143b5 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
143b6 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
143b7 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
143b8 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
143b9 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 t( p->locked ||
143ba 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a !p->sharable );.
143bb 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 assert( p->w
143bc 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a antToLock>0 );..
143bd 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 /* We should
143be 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 already hold a
143bf 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
143c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
143c1 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
143c2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
143c3 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
143c4 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 );.. p->wantT
143c5 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 oLock--;. if(
143c6 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
143c7 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 0 && p->locked )
143c8 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 {. unlockBt
143c9 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 reeMutex(p);.
143ca 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a }. }.}..#else.
143cb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
143cc 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
143cd 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b Enter(Btree *p){
143ce 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 . p->pBt->db =
143cf 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f p->db;.}.SQLITE_
143d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
143d1 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c ite3BtreeEnterAl
143d2 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a l(sqlite3 *db){.
143d3 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
143d4 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
143d5 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
143d6 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 p = db->aDb[i].p
143d7 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b Bt;. if( p ){
143d8 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 . p->pBt->d
143d9 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d b = p->db;. }
143da 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
143db 20 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 if SQLITE_THREA
143dc 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 DSAFE */.#endif
143dd 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /* ifndef SQLITE
143de 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
143df 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a HE */../********
143e0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 ****** End of bt
143e1 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mutex.c ********
143e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143e4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
143e5 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
143e6 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a e btree.c ******
143e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143e9 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
143ea 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 4 April 6.**.**
143eb 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
143ec 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
143ed 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
143ee 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
143ef 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
143f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
143f1 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
143f2 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
143f3 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
143f4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
143f5 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
143f6 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
143f7 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
143f8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
143f9 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
143fa 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
143fb 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
143fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14400 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a ********.** $Id:
14401 20 62 74 72 65 65 2e 63 2c 76 20 31 2e 36 30 38 btree.c,v 1.608
14402 20 32 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35 2009/05/06 18:5
14403 37 3a 31 30 20 73 68 61 6e 65 20 45 78 70 20 24 7:10 shane Exp $
14404 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
14405 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 implements a ex
14406 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 ternal (disk-bas
14407 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 ed) database usi
14408 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 ng BTrees..** Se
14409 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d e the header com
1440a 6d 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e ment on "btreeIn
1440b 74 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f t.h" for additio
1440c 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1440d 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 .** Including a
1440e 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 description of f
1440f 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 ile format and a
14410 6e 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 n overview of op
14411 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a eration..*/../*.
14412 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74 ** The header st
14413 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 ring that appear
14414 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 s at the beginni
14415 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 ng of every.** S
14416 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a QLite database..
14417 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
14418 63 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 char zMagicHeade
14419 72 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c r[] = SQLITE_FIL
1441a 45 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a E_HEADER;../*.**
1441b 20 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c Set this global
1441c 20 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 variable to 1 t
1441d 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 o enable tracing
1441e 20 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 using the TRACE
1441f 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 .** macro..*/.#i
14420 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 f 0.int sqlite3B
14421 74 72 65 65 54 72 61 63 65 3d 30 3b 20 20 2f 2a treeTrace=0; /*
14422 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 True to enable
14423 74 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 tracing */.# def
14424 69 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66 ine TRACE(X) if
14425 28 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 (sqlite3BtreeTra
14426 63 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c ce){printf X;ffl
14427 75 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 ush(stdout);}.#e
14428 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 lse.# define TRA
14429 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a CE(X).#endif....
1442a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1442b 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1442c 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 ./*.** A list of
1442d 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
1442e 73 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 s that are eligi
1442f 62 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 ble for particip
14430 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 ation.** in shar
14431 65 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 ed cache. This
14432 76 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c variable has fil
14433 65 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e e scope during n
14434 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a ormal builds,.**
14435 20 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 but the test ha
14436 72 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 rness needs to a
14437 63 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d ccess it so we m
14438 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f ake it global fo
14439 72 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 r .** test build
1443a 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 s..**.** Access
1443b 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 to this variable
1443c 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 is protected by
1443d 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
1443e 41 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a ATIC_MASTER..*/.
1443f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
14440 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
14441 45 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 E BtShared *SQLI
14442 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 TE_WSD sqlite3Sh
14443 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 aredCacheList =
14444 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 0;.#else.static
14445 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 BtShared *SQLITE
14446 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 _WSD sqlite3Shar
14447 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b edCacheList = 0;
14448 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f .#endif.#endif /
14449 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 * SQLITE_OMIT_SH
1444a 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 ARED_CACHE */..#
1444b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1444c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
1444d 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 /*.** Enable or
1444e 64 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 disable the shar
1444f 65 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 ed pager and sch
14450 65 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a ema features..**
14451 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14452 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f has no effect o
14453 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 n existing datab
14454 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
14455 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 .** The shared c
14456 61 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 ache setting eff
14457 65 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 ects only future
14458 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c calls to.** sql
14459 69 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c ite3_open(), sql
1445a 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f ite3_open16(), o
1445b 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 r sqlite3_open_v
1445c 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 2()..*/.SQLITE_A
1445d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
1445e 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 nable_shared_cac
1445f 68 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a he(int enable){.
14460 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
14461 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 onfig.sharedCach
14462 65 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c eEnabled = enabl
14463 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 e;. return SQLI
14464 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a TE_OK;.}.#endif.
14465 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 ../*.** Forward
14466 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 declaration.*/.s
14467 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 tatic int checkF
14468 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 orReadConflicts(
14469 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c 20 42 74 Btree*, Pgno, Bt
1446a 43 75 72 73 6f 72 2a 2c 20 69 36 34 29 3b 0a 0a Cursor*, i64);..
1446b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
1446c 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1446d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 . /*. ** The f
1446e 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68 unctions querySh
1446f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
14470 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 43 ck(), setSharedC
14471 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c acheTableLock(),
14472 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 . ** and clearA
14473 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
14474 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d leLocks(). ** m
14475 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 anipulate entrie
14476 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 s in the BtShare
14477 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c d.pLock linked l
14478 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 ist used to stor
14479 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 e. ** shared-ca
1447a 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 che table level
1447b 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 locks. If the li
1447c 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 brary is compile
1447d 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 d with the. **
1447e 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 shared-cache fea
1447f 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 ture disabled, t
14480 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c hen there is onl
14481 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a y ever one user.
14482 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53 ** of each BtS
14483 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 hared structure
14484 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b and so this lock
14485 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 ing is not neces
14486 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 sary. . ** So d
14487 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72 efine the lock r
14488 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73 elated functions
14489 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f as no-ops.. */
1448a 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 79 . #define query
1448b 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1448c 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 Lock(a,b,c) SQLI
1448d 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 TE_OK. #define
1448e 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
1448f 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 bleLock(a,b,c) S
14490 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 QLITE_OK. #defi
14491 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 ne clearAllShare
14492 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 dCacheTableLocks
14493 28 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e (a).#endif..#ifn
14494 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14495 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
14496 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 ** Query to see
14497 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 if btree handle
14498 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c p may obtain a l
14499 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 ock of type eLoc
1449a 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b k .** (READ_LOCK
1449b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 or WRITE_LOCK)
1449c 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 on the table wit
1449d 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 h root-page iTab
1449e 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 . Return.** SQLI
1449f 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 TE_OK if the loc
144a0 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 k may be obtaine
144a1 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a d (by calling.**
144a2 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
144a3 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 ableLock()), or
144a4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 SQLITE_LOCKED if
144a5 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 not..*/.static
144a6 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 43 int querySharedC
144a7 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 acheTableLock(Bt
144a8 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 ree *p, Pgno iTa
144a9 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 b, u8 eLock){.
144aa 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
144ab 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b p->pBt;. BtLock
144ac 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 *pIter;.. asse
144ad 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
144ae 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
144af 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
144b0 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 ==READ_LOCK || e
144b1 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
144b2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
144b3 3e 64 62 21 3d 30 20 29 3b 0a 20 20 0a 20 20 2f >db!=0 );. . /
144b4 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 20 * If requesting
144b5 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 a write-lock, th
144b6 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 73 en the Btree mus
144b7 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 t have an open w
144b8 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 rite. ** transa
144b9 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 ction on this fi
144ba 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 le. And, obvious
144bb 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20 ly, for this to
144bc 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a be so there . *
144bd 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 * must be an ope
144be 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 n write transact
144bf 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 ion on the file
144c0 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 itself.. */. a
144c1 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 ssert( eLock==RE
144c2 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 AD_LOCK || (p==p
144c3 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70 Bt->pWriter && p
144c4 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
144c5 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73 _WRITE) );. ass
144c6 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 ert( eLock==READ
144c7 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e _LOCK || pBt->in
144c8 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
144c9 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 NS_WRITE );. .
144ca 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f /* This is a no
144cb 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 65 -op if the share
144cc 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 d-cache is not e
144cd 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 nabled */. if(
144ce 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a !p->sharable ){.
144cf 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
144d0 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
144d1 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f If some other co
144d2 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 nnection is hold
144d3 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ing an exclusive
144d4 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 lock, the. **
144d5 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d requested lock m
144d6 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e ay not be obtain
144d7 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ed.. */. if( p
144d8 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 26 Bt->pWriter!=p &
144d9 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 & pBt->isExclusi
144da 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ve ){. sqlite
144db 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
144dc 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 ed(p->db, pBt->p
144dd 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 Writer->db);.
144de 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c return SQLITE_L
144df 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
144e0 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 E;. }.. /* Thi
144e1 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20 73 65 s (along with se
144e2 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
144e3 65 4c 6f 63 6b 28 29 29 20 69 73 20 77 68 65 72 eLock()) is wher
144e4 65 0a 20 20 2a 2a 20 74 68 65 20 52 65 61 64 55 e. ** the ReadU
144e5 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
144e6 69 73 20 64 65 61 6c 74 20 77 69 74 68 2e 0a 20 is dealt with..
144e7 20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 ** If the calle
144e8 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f r is querying fo
144e9 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e r a read-lock on
144ea 20 61 6e 79 20 74 61 62 6c 65 0a 20 20 2a 2a 20 any table. **
144eb 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 73 other than the s
144ec 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
144ed 6c 65 20 28 74 61 62 6c 65 20 31 29 20 61 6e 64 le (table 1) and
144ee 20 69 66 20 74 68 65 20 52 65 61 64 55 6e 63 6f if the ReadUnco
144ef 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 66 6c 61 mmitted. ** fla
144f0 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
144f1 68 65 20 6c 6f 63 6b 20 67 72 61 6e 74 65 64 20 he lock granted
144f2 65 76 65 6e 20 69 66 20 74 68 65 72 65 20 61 72 even if there ar
144f3 65 20 77 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 e write-locks.
144f4 2a 2a 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e ** on the table.
144f5 20 49 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b If a write-lock
144f6 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 is requested, t
144f7 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 he ReadUncommitt
144f8 65 64 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 ed flag. ** is
144f9 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a not considered..
144fa 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e **. ** In fun
144fb 63 74 69 6f 6e 20 73 65 74 53 68 61 72 65 64 43 ction setSharedC
144fc 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c acheTableLock(),
144fd 20 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 if a read-lock
144fe 69 73 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 is demanded and
144ff 74 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e the . ** ReadUn
14500 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 committed flag i
14501 73 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 s set, no entry
14502 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 is added to the
14503 6c 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a locks list . **
14504 20 28 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b (BtShared.pLock
14505 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 ).. **. ** To
14506 73 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 summarize: If th
14507 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 e ReadUncommitte
14508 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 d flag is set, t
14509 68 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 hen read cursors
1450a 0a 20 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73 63 68 . ** on non-sch
1450b 65 6d 61 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f ema tables do no
1450c 74 20 63 72 65 61 74 65 20 6f 72 20 72 65 73 70 t create or resp
1450d 65 63 74 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2e ect table locks.
1450e 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a The locking. *
1450f 2a 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 * procedure for
14510 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 64 a write-cursor d
14511 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 2e 0a oes not change..
14512 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20 */. if( .
14513 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 0==(p->db->flags
14514 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f &SQLITE_ReadUnco
14515 6d 6d 69 74 74 65 64 29 20 7c 7c 20 0a 20 20 20 mmitted) || .
14516 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f eLock==WRITE_LO
14517 43 4b 20 7c 7c 0a 20 20 20 20 69 54 61 62 3d 3d CK ||. iTab==
14518 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20 20 29 7b MASTER_ROOT. ){
14519 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 . for(pIter=p
1451a 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 Bt->pLock; pIter
1451b 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 ; pIter=pIter->p
1451c 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 2f 2a 20 Next){. /*
1451d 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70 The condition (p
1451e 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f Iter->eLock!=eLo
1451f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f ck) in the follo
14520 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20 wing if(...) .
14521 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
14522 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 is a simplifica
14523 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 20 20 2a tion of:. *
14524 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 65 4c *. ** (eL
14525 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
14526 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d || pIter->eLock=
14527 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 =WRITE_LOCK).
14528 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 73 **. ** s
14529 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 ince we know tha
1452a 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 t if eLock==WRIT
1452b 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 E_LOCK, then no
1452c 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
1452d 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f . ** may ho
1452e 6c 64 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 ld a WRITE_LOCK
1452f 6f 6e 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 on any table in
14530 74 68 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 this file (since
14531 20 74 68 65 72 65 20 63 61 6e 0a 20 20 20 20 20 there can.
14532 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 ** only be a si
14533 6e 67 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20 ngle writer)..
14534 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 */. ass
14535 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 ert( pIter->eLoc
14536 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
14537 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 pIter->eLock==WR
14538 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 ITE_LOCK );.
14539 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
1453a 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 =READ_LOCK || pI
1453b 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c ter->pBtree==p |
1453c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
1453d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 READ_LOCK);.
1453e 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 if( pIter->pBt
1453f 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d ree!=p && pIter-
14540 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 >iTable==iTab &&
14541 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 pIter->eLock!=e
14542 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 Lock ){.
14543 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
14544 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 nBlocked(p->db,
14545 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 pIter->pBtree->d
14546 62 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 b);. if(
14547 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
14548 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 K ){. a
14549 73 73 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 ssert( p==pBt->p
1454a 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 Writer );.
1454b 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 pBt->isPendi
1454c 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ng = 1;.
1454d 7d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e }. return
1454e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
1454f 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 HAREDCACHE;.
14550 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
14551 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
14552 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
14553 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
14554 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
14555 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14556 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
14557 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e ** Add a lock on
14558 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 the table with
14559 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 root-page iTable
1455a 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 to the shared-b
1455b 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 tree used.** by
1455c 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 Btree handle p.
1455d 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 Parameter eLock
1455e 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 52 must be either R
1455f 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 EAD_LOCK or .**
14560 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a WRITE_LOCK..**.*
14561 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
14562 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c eturned if the l
14563 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 ock is added suc
14564 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 cessfully. SQLIT
14565 45 5f 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 E_BUSY and.** SQ
14566 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 LITE_NOMEM may a
14567 6c 73 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e lso be returned.
14568 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
14569 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
1456a 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c leLock(Btree *p,
1456b 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 Pgno iTable, u8
1456c 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 eLock){. BtSha
1456d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1456e 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f t;. BtLock *pLo
1456f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b ck = 0;. BtLock
14570 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 *pIter;.. asse
14571 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
14572 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
14573 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
14574 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 ==READ_LOCK || e
14575 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
14576 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
14577 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 >db!=0 );.. /*
14578 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
14579 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 if the shared-ca
1457a 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c che is not enabl
1457b 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e ed */. if( !p->
1457c 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
1457d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1457e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
1457f 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 SQLITE_OK==quer
14580 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
14581 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c eLock(p, iTable,
14582 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a eLock) );.. /*
14583 20 49 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 If the read-unc
14584 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 ommitted flag is
14585 20 73 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d set and a read-
14586 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
14587 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d d on. ** a non-
14588 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68 schema table, th
14589 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 en the lock is a
1458a 6c 77 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20 lways granted.
1458b 52 65 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a Return early. *
1458c 2a 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 * without adding
1458d 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 an entry to the
1458e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 BtShared.pLock
1458f 6c 69 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63 list. See. ** c
14590 6f 6d 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69 omment in functi
14591 6f 6e 20 71 75 65 72 79 53 68 61 72 65 64 43 61 on querySharedCa
14592 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 cheTableLock() f
14593 6f 72 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a or more info. *
14594 2a 20 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 * on handling th
14595 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 e ReadUncommitte
14596 64 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 d flag.. */. i
14597 66 28 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e f( . (p->db->
14598 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 flags&SQLITE_Rea
14599 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 dUncommitted) &&
1459a 20 0a 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 . (eLock==RE
1459b 41 44 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 AD_LOCK) &&.
1459c 69 54 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 iTable!=MASTER_R
1459d 4f 4f 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 OOT. ){. ret
1459e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1459f 20 7d 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 }.. /* First s
145a0 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 earch the list f
145a1 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c or an existing l
145a2 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c ock on this tabl
145a3 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 e. */. for(pIte
145a4 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
145a5 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
145a6 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
145a7 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d ( pIter->iTable=
145a8 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 =iTable && pIter
145a9 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
145aa 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 pLock = pIt
145ab 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b er;. break;
145ac 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
145ad 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 If the above se
145ae 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e arch did not fin
145af 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 d a BtLock struc
145b0 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 t associating Bt
145b1 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 ree p. ** with
145b2 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c table iTable, al
145b3 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c locate one and l
145b4 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 ink it into the
145b5 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
145b6 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 !pLock ){. p
145b7 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a Lock = (BtLock *
145b8 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
145b9 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b ro(sizeof(BtLock
145ba 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f ));. if( !pLo
145bb 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ck ){. retu
145bc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
145bd 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b . }. pLock
145be 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c ->iTable = iTabl
145bf 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 e;. pLock->pB
145c0 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c tree = p;. pL
145c1 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 ock->pNext = pBt
145c2 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 ->pLock;. pBt
145c3 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b ->pLock = pLock;
145c4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 . }.. /* Set t
145c5 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 he BtLock.eLock
145c6 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 variable to the
145c7 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 maximum of the c
145c8 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a urrent lock. **
145c9 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 and the request
145ca 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 ed lock. This me
145cb 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c ans if a write-l
145cc 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 ock was already
145cd 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 held. ** and a
145ce 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 read-lock reques
145cf 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e ted, we don't in
145d0 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 correctly downgr
145d1 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 ade the lock..
145d2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 */. assert( WRI
145d3 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 TE_LOCK>READ_LOC
145d4 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b K );. if( eLock
145d5 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b >pLock->eLock ){
145d6 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 . pLock->eLoc
145d7 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a k = eLock;. }..
145d8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
145d9 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 OK;.}.#endif /*
145da 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 !SQLITE_OMIT_SHA
145db 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
145dc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
145dd 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
145de 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c *.** Release all
145df 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 the table locks
145e0 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 (locks obtained
145e1 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a via calls to.**
145e2 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61 the setSharedCa
145e3 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 cheTableLock() p
145e4 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 rocedure) held b
145e5 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 y Btree handle p
145e6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
145e7 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
145e8 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20 at handle p has
145e9 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 an open read or
145ea 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 write .** transa
145eb 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 ction. If it doe
145ec 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 s not, then the
145ed 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69 BtShared.isPendi
145ee 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d ng variable.** m
145ef 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c ay be incorrectl
145f0 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 y cleared..*/.st
145f1 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 atic void clearA
145f2 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
145f3 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
145f4 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
145f5 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
145f6 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d tLock **ppIter =
145f7 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 &pBt->pLock;..
145f8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
145f9 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
145fa 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
145fb 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 p->sharable || 0
145fc 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 ==*ppIter );. a
145fd 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
145fe 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 s>0 );.. while(
145ff 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 *ppIter ){.
14600 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
14601 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 *ppIter;. ass
14602 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c ert( pBt->isExcl
14603 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d usive==0 || pBt-
14604 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d >pWriter==pLock-
14605 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 >pBtree );. a
14606 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 ssert( pLock->pB
14607 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 tree->inTrans>=p
14608 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 Lock->eLock );.
14609 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 if( pLock->pB
1460a 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
1460b 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b *ppIter = pLock
1460c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 ->pNext;. s
1460d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 qlite3_free(pLoc
1460e 6b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 k);. }else{.
1460f 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 ppIter = &p
14610 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
14611 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 }. }.. assert
14612 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 ( pBt->isPending
14613 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 ==0 || pBt->pWri
14614 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 ter );. if( pBt
14615 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a ->pWriter==p ){.
14616 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 pBt->pWriter
14617 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 = 0;. pBt->i
14618 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a sExclusive = 0;.
14619 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 pBt->isPendi
1461a 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 ng = 0;. }else
1461b 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 if( pBt->nTransa
1461c 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 ction==2 ){.
1461d 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
1461e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
1461f 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 connection p is
14620 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a concluding its .
14621 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
14622 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 on. If there cur
14623 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 rently exists a
14624 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 writer, and p is
14625 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 not. ** that
14626 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 writer, then th
14627 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b e number of lock
14628 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 s held by connec
14629 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 tions other.
1462a 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 ** than the writ
1462b 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 er must be about
1462c 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f to drop to zero
1462d 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 . In this case.
1462e 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 ** set the is
1462f 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 Pending flag to
14630 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 0.. **. **
14631 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 If there is not
14632 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 currently a wri
14633 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 ter, then BtShar
14634 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 ed.isPending mus
14635 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f t. ** be zero
14636 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 already. So thi
14637 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 s next line is h
14638 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 armless in that
14639 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
1463a 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
1463b 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 = 0;. }.}.#endi
1463c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1463d 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f _SHARED_CACHE */
1463e 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 ..static void re
1463f 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 leasePage(MemPag
14640 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 e *pPage); /* F
14641 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 orward reference
14642 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 */../*.** Verif
14643 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
14644 72 20 68 6f 6c 64 73 20 61 20 6d 75 74 65 78 20 r holds a mutex
14645 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 0a on the BtShared.
14646 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
14647 47 0a 73 74 61 74 69 63 20 69 6e 74 20 63 75 72 G.static int cur
14648 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 sorHoldsMutex(Bt
14649 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 Cursor *p){. re
1464a 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 turn sqlite3_mut
1464b 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e ex_held(p->pBt->
1464c 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 mutex);.}.#endif
1464d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1464e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
1464f 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 /*.** Invalidate
14650 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
14651 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f ge-list cache fo
14652 72 20 63 75 72 73 6f 72 20 70 43 75 72 2c 20 69 r cursor pCur, i
14653 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 f any..*/.static
14654 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 void invalidate
14655 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 OverflowCache(Bt
14656 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
14657 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
14658 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
14659 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
1465a 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f e(pCur->aOverflo
1465b 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 w);. pCur->aOve
1465c 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a rflow = 0;.}../*
1465d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 .** Invalidate t
1465e 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
1465f 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 -list cache for
14660 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e all cursors open
14661 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 ed.** on the sha
14662 72 65 64 20 62 74 72 65 65 20 73 74 72 75 63 74 red btree struct
14663 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 ure pBt..*/.stat
14664 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 ic void invalida
14665 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
14666 68 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 he(BtShared *pBt
14667 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
14668 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14669 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1466a 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1466b 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 for(p=pBt->pCurs
1466c 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
1466d 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 t){. invalida
1466e 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
1466f 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a p);. }.}.#else.
14670 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 #define invali
14671 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
14672 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 e(x). #define i
14673 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
14674 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 23 65 6e flowCache(x).#en
14675 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 dif../*.** Set b
14676 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 it pgno of the B
14677 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
14678 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
14679 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 is called .** w
1467a 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 hen a page that
1467b 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 previously conta
1467c 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 ined data become
1467d 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 s a free-list le
1467e 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a af .** page..**.
1467f 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e ** The BtShared.
14680 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
14681 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 ec exists to wor
14682 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 k around an obsc
14683 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 ure.** bug cause
14684 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 d by the interac
14685 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 tion of two usef
14686 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 ul IO optimizati
14687 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a ons surrounding.
14688 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ** free-list lea
14689 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 f pages:.**.**
1468a 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 1) When all dat
1468b 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f a is deleted fro
1468c 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 m a page and the
1468d 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a page becomes.**
1468e 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 a free-lis
1468f 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
14690 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 page is not wri
14691 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
14692 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 base.** (as
14693 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
14694 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f pages contain no
14695 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 meaningful data
14696 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ). Sometimes.**
14697 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65 such a page
14698 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 is not even jou
14699 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 rnalled (as it w
1469a 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 ill not be modif
1469b 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 ied,.** why
1469c 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c bother journall
1469d 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 ing it?)..**.**
1469e 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2) When a free
1469f 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
146a0 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 is reused, its c
146a1 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 ontent is not re
146a2 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 ad.** from
146a3 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
146a4 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
146a5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 ournal file (why
146a6 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 should it.**
146a7 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 be, if it is
146a8 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 not at all meani
146a9 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 ngful?)..**.** B
146aa 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 y themselves, th
146ab 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ese optimization
146ac 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 s work fine and
146ad 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a provide a handy.
146ae 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ** performance b
146af 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c oost to bulk del
146b0 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 ete or insert op
146b1 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 erations. Howeve
146b2 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 r, if.** a page
146b3 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
146b4 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 free-list and th
146b5 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e en reused within
146b6 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 the same.** tra
146b7 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 nsaction, a prob
146b8 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 lem comes up. If
146b9 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
146ba 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e journalled when
146bb 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 .** it is moved
146bc 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
146bd 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 and it is also
146be 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 not journalled w
146bf 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 hen it.** is ext
146c0 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
146c1 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 free-list and re
146c2 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f used, then the o
146c3 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 riginal data.**
146c4 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 may be lost. In
146c5 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 the event of a r
146c6 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 ollback, it may
146c7 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a not be possible.
146c8 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ** to restore th
146c9 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
146ca 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 s original confi
146cb 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 guration..**.**
146cc 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
146cd 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
146ce 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
146cf 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 Whenever a page
146d0 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f is .** moved to
146d1 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c become a free-l
146d2 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
146d3 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
146d4 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 bit is.** set i
146d5 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 n the bitvec. Wh
146d6 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 enever a leaf pa
146d7 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ge is extracted
146d8 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
146d9 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 st,.** optimizat
146da 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f ion 2 above is o
146db 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 mmitted if the c
146dc 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
146dd 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 is already.** s
146de 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 et in BtShared.p
146df 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 HasContent. The
146e0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
146e1 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 bitvec are clear
146e2 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ed.** at the end
146e3 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 of every transa
146e4 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
146e5 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 int btreeSetHas
146e6 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
146e7 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
146e8 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
146e9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 LITE_OK;. if( !
146ea 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
146eb 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 ){. int nPag
146ec 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e;. rc = sqli
146ed 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
146ee 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 t(pBt->pPager, &
146ef 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 nPage);. if(
146f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
146f1 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 48 61 73 . pBt->pHas
146f2 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 Content = sqlite
146f3 33 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 3BitvecCreate((u
146f4 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 32)nPage);.
146f5 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 if( !pBt->pHasC
146f6 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 ontent ){.
146f7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
146f8 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
146f9 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d }. }. if( rc=
146fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 67 =SQLITE_OK && pg
146fb 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 74 76 65 no<=sqlite3Bitve
146fc 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 61 73 43 cSize(pBt->pHasC
146fd 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 20 20 72 ontent) ){. r
146fe 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 c = sqlite3Bitve
146ff 63 53 65 74 28 70 42 74 2d 3e 70 48 61 73 43 6f cSet(pBt->pHasCo
14700 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 ntent, pgno);.
14701 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
14702 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 ../*.** Query th
14703 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 e BtShared.pHasC
14704 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 2e 0a 2a ontent vector..*
14705 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
14706 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 on is called whe
14707 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 n a free-list le
14708 61 66 20 70 61 67 65 20 69 73 20 72 65 6d 6f 76 af page is remov
14709 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 ed from the.** f
1470a 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 72 65 75 ree-list for reu
1470b 73 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 66 se. It returns f
1470c 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 61 alse if it is sa
1470d 66 65 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 fe to retrieve t
1470e 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 6f 6d 20 he.** page from
1470f 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 the pager layer
14710 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e with the 'no-con
14711 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20 tent' flag set.
14712 54 72 75 65 20 6f 74 68 65 72 77 69 73 65 2e 0a True otherwise..
14713 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
14714 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 reeGetHasContent
14715 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
14716 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 42 69 Pgno pgno){. Bi
14717 74 76 65 63 20 2a 70 20 3d 20 70 42 74 2d 3e 70 tvec *p = pBt->p
14718 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 72 65 HasContent;. re
14719 74 75 72 6e 20 28 70 20 26 26 20 28 70 67 6e 6f turn (p && (pgno
1471a 3e 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 >sqlite3BitvecSi
1471b 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 74 65 33 ze(p) || sqlite3
1471c 42 69 74 76 65 63 54 65 73 74 28 70 2c 20 70 67 BitvecTest(p, pg
1471d 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 no)));.}../*.**
1471e 43 6c 65 61 72 20 28 64 65 73 74 72 6f 79 29 20 Clear (destroy)
1471f 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
14720 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
14721 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 0a This should be.
14722 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 ** invoked at th
14723 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 e conclusion of
14724 65 61 63 68 20 77 72 69 74 65 2d 74 72 61 6e 73 each write-trans
14725 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 action..*/.stati
14726 63 20 76 6f 69 64 20 62 74 72 65 65 43 6c 65 61 c void btreeClea
14727 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 rHasContent(BtSh
14728 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 ared *pBt){. sq
14729 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
1472a 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 oy(pBt->pHasCont
1472b 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e 70 48 61 ent);. pBt->pHa
1472c 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a sContent = 0;.}.
1472d 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 ./*.** Save the
1472e 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 current cursor p
1472f 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 osition in the v
14730 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f ariables BtCurso
14731 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 r.nKey .** and B
14732 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 tCursor.pKey. Th
14733 65 20 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 e cursor's state
14734 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f is set to CURSO
14735 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a R_REQUIRESEEK..*
14736 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 /.static int sav
14737 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
14738 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
14739 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
1473a 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c sert( CURSOR_VAL
1473b 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
1473c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d );. assert( 0=
1473d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 =pCur->pKey );.
1473e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1473f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
14740 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );.. rc = sqlit
14741 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 e3BtreeKeySize(p
14742 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 Cur, &pCur->nKey
14743 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 );.. /* If this
14744 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 is an intKey ta
14745 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 ble, then the ab
14746 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 ove call to Btre
14747 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 eKeySize(). **
14748 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 stores the integ
14749 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e er key in pCur->
1474a 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 nKey. In this ca
1474b 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 se this value is
1474c 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 . ** all that i
1474d 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 s required. Othe
1474e 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 rwise, if pCur i
1474f 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e s not open on an
14750 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 intKey. ** tab
14751 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 le, then malloc
14752 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 space for and st
14753 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b ore the pCur->nK
14754 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 ey bytes of key
14755 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f . ** data.. */
14756 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
14757 45 5f 4f 4b 20 26 26 20 30 3d 3d 70 43 75 72 2d E_OK && 0==pCur-
14758 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
14759 65 79 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 ey){. void *p
1475a 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Key = sqlite3Mal
1475b 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e loc( (int)pCur->
1475c 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 nKey );. if(
1475d 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 pKey ){. rc
1475e 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1475f 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 ey(pCur, 0, (int
14760 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 )pCur->nKey, pKe
14761 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 y);. if( rc
14762 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
14763 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 pCur->pKe
14764 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 y = pKey;.
14765 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
14766 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 qlite3_free(pKey
14767 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
14768 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
14769 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1476a 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
1476b 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 t( !pCur->apPage
1476c 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 [0]->intKey || !
1476d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 pCur->pKey );..
1476e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1476f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b OK ){. int i;
14770 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
14771 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b =pCur->iPage; i+
14772 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 +){. releas
14773 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
14774 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 ge[i]);. pC
14775 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 ur->apPage[i] =
14776 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 0;. }. pCu
14777 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 r->iPage = -1;.
14778 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
14779 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 = CURSOR_REQUIRE
1477a 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 SEEK;. }.. inv
1477b 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
1477c 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 ache(pCur);. re
1477d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1477e 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 * Save the posit
1477f 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 ions of all curs
14780 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65 ors except pExce
14781 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 pt open on the t
14782 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f able .** with ro
14783 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 ot-page iRoot. U
14784 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 sually, this is
14785 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f called just befo
14786 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 re cursor.** pEx
14787 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 cept is used to
14788 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 modify the table
14789 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 (BtreeDelete()
1478a 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 or BtreeInsert()
1478b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
1478c 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
1478d 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
1478e 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 gno iRoot, BtCur
1478f 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 sor *pExcept){.
14790 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
14791 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14792 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
14793 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
14794 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c rt( pExcept==0 |
14795 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d | pExcept->pBt==
14796 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 pBt );. for(p=p
14797 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
14798 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
14799 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 if( p!=pExcept
1479a 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 && (0==iRoot ||
1479b 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f p->pgnoRoot==iRo
1479c 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 ot) && .
1479d 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f p->eState==CURSO
1479e 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 R_VALID ){.
1479f 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 int rc = saveCu
147a0 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b rsorPosition(p);
147a1 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
147a2 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK!=rc ){.
147a3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
147a4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
147a5 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
147a6 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
147a7 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ear the current
147a8 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e cursor position.
147a9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
147aa 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
147ab 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
147ac 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
147ad 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
147ae 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
147af 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) );. sqlite3_f
147b0 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
147b1 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 . pCur->pKey =
147b2 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 0;. pCur->eStat
147b3 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
147b4 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 ID;.}../*.** Res
147b5 74 6f 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 tore the cursor
147b6 74 6f 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 to the position
147b7 69 74 20 77 61 73 20 69 6e 20 28 6f 72 20 61 73 it was in (or as
147b8 20 63 6c 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 close to as pos
147b9 73 69 62 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 sible).** when s
147ba 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f aveCursorPositio
147bb 6e 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 n() was called.
147bc 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 Note that this c
147bd 61 6c 6c 20 64 65 6c 65 74 65 73 20 74 68 65 20 all deletes the
147be 0a 2a 2a 20 73 61 76 65 64 20 70 6f 73 69 74 69 .** saved positi
147bf 6f 6e 20 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 on info stored b
147c0 79 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 y saveCursorPosi
147c1 74 69 6f 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 tion(), so there
147c2 20 63 61 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f can be.** at mo
147c3 73 74 20 6f 6e 65 20 65 66 66 65 63 74 69 76 65 st one effective
147c4 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
147c5 73 69 74 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66 sition() call af
147c6 74 65 72 20 65 61 63 68 20 0a 2a 2a 20 73 61 76 ter each .** sav
147c7 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
147c8 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
147c9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
147ca 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 BtreeRestoreCurs
147cb 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 orPosition(BtCur
147cc 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
147cd 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
147ce 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
147cf 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
147d0 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
147d1 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 >=CURSOR_REQUIRE
147d2 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 SEEK );. if( pC
147d3 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
147d4 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 OR_FAULT ){.
147d5 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 return pCur->ski
147d6 70 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 p;. }. pCur->e
147d7 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
147d8 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 73 NVALID;. rc = s
147d9 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
147da 6f 28 70 43 75 72 2c 20 70 43 75 72 2d 3e 70 4b o(pCur, pCur->pK
147db 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 ey, pCur->nKey,
147dc 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69 70 29 3b 0, &pCur->skip);
147dd 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
147de 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
147df 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 te3_free(pCur->p
147e0 4b 65 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e Key);. pCur->
147e1 70 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 pKey = 0;. as
147e2 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
147e3 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
147e4 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
147e5 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
147e6 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e );. }. return
147e7 20 72 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 rc;.}..#define
147e8 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
147e9 69 74 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d ition(p) \. (p-
147ea 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f >eState>=CURSOR_
147eb 52 45 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a REQUIRESEEK ? \.
147ec 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
147ed 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 BtreeRestoreCurs
147ee 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 orPosition(p) :
147ef 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 \. SQLIT
147f0 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 E_OK)../*.** Det
147f1 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
147f2 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 r not a cursor h
147f3 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 as moved from th
147f4 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a e position it.**
147f5 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 was last placed
147f6 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 at. Cursors ca
147f7 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 n move when the
147f8 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 row they are poi
147f9 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 nting.** at is d
147fa 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 eleted out from
147fb 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a under them..**.*
147fc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
147fd 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 eturns an error
147fe 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e code if somethin
147ff 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 g goes wrong. T
14800 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 he.** integer *p
14801 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 HasMoved is set
14802 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 to one if the cu
14803 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 rsor has moved a
14804 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a nd 0 if not..*/.
14805 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
14806 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
14807 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 ursorHasMoved(Bt
14808 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
14809 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 t *pHasMoved){.
1480a 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d int rc;.. rc =
1480b 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
1480c 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
1480d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
1480e 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 HasMoved = 1;.
1480f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
14810 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
14811 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
14812 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 21 D || pCur->skip!
14813 3d 30 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d =0 ){. *pHasM
14814 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 oved = 1;. }els
14815 65 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 e{. *pHasMove
14816 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 d = 0;. }. ret
14817 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
14818 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
14819 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1481a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 ./*.** Given a p
1481b 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 age number of a
1481c 72 65 67 75 6c 61 72 20 64 61 74 61 62 61 73 65 regular database
1481d 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 page, return th
1481e 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 e page.** number
1481f 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 for the pointer
14820 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74 20 63 -map page that c
14821 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 ontains the entr
14822 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70 y for the.** inp
14823 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a ut page number..
14824 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 */.static Pgno p
14825 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 trmapPageno(BtSh
14826 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
14827 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61 pgno){. int nPa
14828 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 gesPerMapPage;.
14829 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72 Pgno iPtrMap, r
1482a 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 et;. assert( sq
1482b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1482c 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1482d 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 nPagesPerMapPa
1482e 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c ge = (pBt->usabl
1482f 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50 eSize/5)+1;. iP
14830 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29 trMap = (pgno-2)
14831 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 /nPagesPerMapPag
14832 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72 e;. ret = (iPtr
14833 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70 Map*nPagesPerMap
14834 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66 Page) + 2; . if
14835 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 ( ret==PENDING_B
14836 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
14837 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a . ret++;. }.
14838 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a return ret;.}.
14839 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 ./*.** Write an
1483a 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 entry into the p
1483b 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a ointer map..**.*
1483c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 * This routine u
1483d 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74 pdates the point
1483e 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er map entry for
1483f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65 page number 'ke
14840 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 y'.** so that it
14841 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 27 65 maps to type 'e
14842 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74 Type' and parent
14843 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67 page number 'pg
14844 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 no'..** An error
14845 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
14846 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 d if something g
14847 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 oes wrong, other
14848 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a wise SQLITE_OK..
14849 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 */.static int pt
1484a 72 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64 rmapPut(BtShared
1484b 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c *pBt, Pgno key,
1484c 20 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 u8 eType, Pgno
1484d 70 61 72 65 6e 74 29 7b 0a 20 20 44 62 50 61 67 parent){. DbPag
1484e 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 e *pDbPage; /*
1484f 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
14850 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 page */. u8 *pP
14851 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 trmap; /* T
14852 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 he pointer map d
14853 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ata */. Pgno iP
14854 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 trmap; /* Th
14855 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
14856 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 ge number */. i
14857 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
14858 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f /* Offset in po
14859 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
1485a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 /. int rc;.. a
1485b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1485c 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1485d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 utex) );. /* Th
1485e 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c e master-journal
1485f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 page number mus
14860 74 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 t never be used
14861 61 73 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 as a pointer map
14862 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 page */. asser
14863 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 t( 0==PTRMAP_ISP
14864 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 AGE(pBt, PENDING
14865 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 _BYTE_PAGE(pBt))
14866 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
14867 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
14868 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 ;. if( key==0 )
14869 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1486a 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1486b 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20 ;. }. iPtrmap
1486c 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 = PTRMAP_PAGENO(
1486d 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 pBt, key);. rc
1486e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
1486f 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
14870 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 Ptrmap, &pDbPage
14871 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
14872 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
14873 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f turn rc;. }. o
14874 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 ffset = PTRMAP_P
14875 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 TROFFSET(iPtrmap
14876 2c 20 6b 65 79 29 3b 0a 20 20 70 50 74 72 6d 61 , key);. pPtrma
14877 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 p = (u8 *)sqlite
14878 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
14879 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 bPage);.. if( e
1487a 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 Type!=pPtrmap[of
1487b 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 fset] || get4byt
1487c 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e(&pPtrmap[offse
1487d 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b t+1])!=parent ){
1487e 0a 20 20 20 20 54 52 41 43 45 28 28 22 50 54 52 . TRACE(("PTR
1487f 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e MAP_UPDATE: %d->
14880 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c (%d,%d)\n", key,
14881 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 eType, parent))
14882 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
14883 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 e3PagerWrite(pDb
14884 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
14885 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
14886 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 pPtrmap[of
14887 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 fset] = eType;.
14888 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
14889 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
1488a 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d , parent);. }
1488b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 50 . }.. sqlite3P
1488c 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 agerUnref(pDbPag
1488d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b e);. return rc;
1488e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
1488f 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 n entry from the
14890 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a pointer map..**
14891 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14892 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 70 retrieves the p
14893 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
14894 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c for page 'key',
14895 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 writing.** the
14896 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 type and parent
14897 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a page number to *
14898 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e pEType and *pPgn
14899 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a o respectively..
1489a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** An error code
1489b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1489c 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
1489d 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 rong, otherwise
1489e 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
1489f 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 atic int ptrmapG
148a0 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 et(BtShared *pBt
148a1 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a , Pgno key, u8 *
148a2 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 pEType, Pgno *pP
148a3 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a gno){. DbPage *
148a4 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 pDbPage; /* Th
148a5 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
148a6 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 ge */. int iPtr
148a7 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f map; /* Po
148a8 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 inter map page i
148a9 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 ndex */. u8 *pP
148aa 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 trmap; /*
148ab 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 Pointer map page
148ac 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f data */. int o
148ad 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
148ae 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 Offset of entry
148af 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 in pointer map
148b0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 */. int rc;..
148b1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
148b2 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
148b3 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 mutex) );.. iPt
148b4 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 rmap = PTRMAP_PA
148b5 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a GENO(pBt, key);.
148b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
148b7 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 gerGet(pBt->pPag
148b8 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 er, iPtrmap, &pD
148b9 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
148ba 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !=0 ){. retur
148bb 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 n rc;. }. pPtr
148bc 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 map = (u8 *)sqli
148bd 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
148be 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 pDbPage);.. off
148bf 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 set = PTRMAP_PTR
148c0 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 OFFSET(iPtrmap,
148c1 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 key);. assert(
148c2 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a pEType!=0 );. *
148c3 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 pEType = pPtrmap
148c4 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 [offset];. if(
148c5 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d pPgno ) *pPgno =
148c6 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d get4byte(&pPtrm
148c7 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a ap[offset+1]);..
148c8 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
148c9 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
148ca 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c if( *pEType<1 ||
148cb 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 *pEType>5 ) ret
148cc 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
148cd 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 PT_BKPT;. retur
148ce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
148cf 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 #else /* if defi
148d0 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned SQLITE_OMIT_
148d1 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 AUTOVACUUM */.
148d2 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 #define ptrmapPu
148d3 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 t(w,x,y,z) SQLIT
148d4 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 E_OK. #define p
148d5 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a trmapGet(w,x,y,z
148d6 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
148d7 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f efine ptrmapPutO
148d8 76 66 6c 28 79 2c 7a 29 20 53 51 4c 49 54 45 5f vfl(y,z) SQLITE_
148d9 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a OK.#endif../*.**
148da 20 47 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 Given a btree p
148db 61 67 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 age and a cell i
148dc 6e 64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 ndex (0 means th
148dd 65 20 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a e first cell on.
148de 2a 2a 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d ** the page, 1 m
148df 65 61 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 eans the second
148e0 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 cell, and so for
148e1 74 68 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 th) return a poi
148e2 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 nter.** to the c
148e3 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a ell content..**.
148e4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
148e5 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 works only for p
148e6 61 67 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 ages that do not
148e7 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f contain overflo
148e8 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 w cells..*/.#def
148e9 69 6e 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 ine findCell(P,I
148ea 29 20 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 ) \. ((P)->aDat
148eb 61 20 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 a + ((P)->maskPa
148ec 67 65 20 26 20 67 65 74 32 62 79 74 65 28 26 28 ge & get2byte(&(
148ed 50 29 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 P)->aData[(P)->c
148ee 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d ellOffset+2*(I)]
148ef 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 )))../*.** This
148f0 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 a more complex v
148f1 65 72 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 ersion of findCe
148f2 6c 6c 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 ll() that works
148f3 66 6f 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 for.** pages tha
148f4 74 20 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 t do contain ove
148f5 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 20 53 65 rflow cells. Se
148f6 65 20 69 6e 73 65 72 74 0a 2a 2f 0a 73 74 61 74 e insert.*/.stat
148f7 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 ic u8 *findOverf
148f8 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 lowCell(MemPage
148f9 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c *pPage, int iCel
148fa 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 l){. int i;. a
148fb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
148fc 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
148fd 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
148fe 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e for(i=pPage->n
148ff 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 Overflow-1; i>=0
14900 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 ; i--){. int
14901 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f k;. struct _O
14902 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a vflCell *pOvfl;.
14903 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 pOvfl = &pPa
14904 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 ge->aOvfl[i];.
14905 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 k = pOvfl->idx
14906 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 ;. if( k<=iCe
14907 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
14908 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 k==iCell ){.
14909 20 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c return pOvfl
1490a 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d ->pCell;. }
1490b 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a . iCell--;.
1490c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1490d 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 rn findCell(pPag
1490e 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a e, iCell);.}../*
1490f 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c .** Parse a cell
14910 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 content block a
14911 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 nd fill in the C
14912 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ellInfo structur
14913 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 e. There.** are
14914 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 two versions of
14915 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 this function.
14916 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
14917 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61 seCell() takes a
14918 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 .** cell index
14919 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
1491a 67 75 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69 74 gument and sqlit
1491b 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
1491c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 Ptr() .** takes
1491d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1491e 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c body of the cel
1491f 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 l as its second
14920 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
14921 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 Within this file
14922 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 , the parseCell(
14923 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 ) macro can be c
14924 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 alled instead of
14925 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 .** sqlite3Btree
14926 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 ParseCellPtr().
14927 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 Using some compi
14928 6c 65 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 lers, this will
14929 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53 51 be faster..*/.SQ
1492a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1492b 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 d sqlite3BtreePa
1492c 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 rseCellPtr(. Me
1492d 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
1492e 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
1492f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c ntaining the cel
14930 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c l */. u8 *pCell
14931 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
14932 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
14933 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 cell text. */.
14934 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f CellInfo *pInfo
14935 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c /* Fill
14936 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 in this structu
14937 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e re */.){. u16 n
14938 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14939 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 /* Number byt
1493a 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 es in cell conte
1493b 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 nt header */. u
1493c 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 32 nPayload;
1493d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1493e 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c of bytes of cel
1493f 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 l payload */..
14940 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14941 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
14942 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
14943 0a 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c .. pInfo->pCell
14944 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 = pCell;. asse
14945 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d rt( pPage->leaf=
14946 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 =0 || pPage->lea
14947 66 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 f==1 );. n = pP
14948 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
14949 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d e;. assert( n==
1494a 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 4-4*pPage->leaf
1494b 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
1494c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 intKey ){. if
1494d 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
1494e 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 ){. n += g
1494f 65 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c etVarint32(&pCel
14950 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b l[n], nPayload);
14951 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
14952 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a nPayload = 0;.
14953 20 20 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 }. n += g
14954 65 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b etVarint(&pCell[
14955 6e 5d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f n], (u64*)&pInfo
14956 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e ->nKey);. pIn
14957 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 fo->nData = nPay
14958 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 load;. }else{.
14959 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 pInfo->nData
1495a 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 = 0;. n += ge
1495b 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c tVarint32(&pCell
1495c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a [n], nPayload);.
1495d 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 pInfo->nKey
1495e 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a = nPayload;. }.
1495f 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 pInfo->nPayloa
14960 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 d = nPayload;.
14961 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d pInfo->nHeader =
14962 20 6e 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 n;. if( likely
14963 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 (nPayload<=pPage
14964 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 ->maxLocal) ){.
14965 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 /* This is th
14966 65 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 e (easy) common
14967 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 case where the e
14968 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 ntire payload fi
14969 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 ts. ** on the
1496a 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f local page. No
1496b 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 overflow is req
1496c 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 uired.. */.
1496d 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 int nSize;
1496e 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 /* Total s
1496f 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 ize of cell cont
14970 65 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ent in bytes */.
14971 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 nSize = nPay
14972 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 load + n;. pI
14973 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 nfo->nLocal = (u
14974 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 16)nPayload;.
14975 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f pInfo->iOverflo
14976 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 w = 0;. if( (
14977 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 nSize & ~3)==0 )
14978 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 {. nSize =
14979 34 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 4; /* Min
1497a 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 imum cell size i
1497b 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 s 4 */. }.
1497c 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 pInfo->nSize =
1497d 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 (u16)nSize;. }e
1497e 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 lse{. /* If t
1497f 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 he payload will
14980 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 not fit complete
14981 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ly on the local
14982 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 page, we have.
14983 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 ** to decide h
14984 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 ow much to store
14985 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 locally and how
14986 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f much to spill o
14987 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 nto. ** overf
14988 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 low pages. The
14989 73 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d strategy is to m
1498a 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 inimize the amou
1498b 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 nt of unused.
1498c 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 ** space on ove
1498d 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c rflow pages whil
1498e 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d e keeping the am
1498f 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 ount of local st
14990 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 orage. ** in
14991 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c between minLocal
14992 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 and maxLocal..
14993 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 **. ** War
14994 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 ning: changing
14995 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 the way overflow
14996 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 payload is dist
14997 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 ributed in any.
14998 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 ** way will r
14999 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f esult in an inco
1499a 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f mpatible file fo
1499b 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 rmat.. */.
1499c 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 int minLocal;
1499d 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e /* Minimum amoun
1499e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c t of payload hel
1499f 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 d locally */.
149a0 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 int maxLocal;
149a1 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e /* Maximum amoun
149a2 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c t of payload hel
149a3 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 d locally */.
149a4 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 int surplus;
149a5 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c /* Overflow payl
149a6 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f oad available fo
149a7 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 r local storage
149a8 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c */.. minLocal
149a9 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 = pPage->minLoc
149aa 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c al;. maxLocal
149ab 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 = pPage->maxLoc
149ac 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 al;. surplus
149ad 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 = minLocal + (nP
149ae 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 ayload - minLoca
149af 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e l)%(pPage->pBt->
149b0 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b usableSize - 4);
149b1 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75 73 . if( surplus
149b2 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a <= maxLocal ){.
149b3 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f pInfo->nLo
149b4 63 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c cal = (u16)surpl
149b5 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 us;. }else{.
149b6 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 pInfo->nLoc
149b7 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63 al = (u16)minLoc
149b8 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 al;. }. pI
149b9 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d nfo->iOverflow =
149ba 20 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c (u16)(pInfo->nL
149bb 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 70 ocal + n);. p
149bc 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49 Info->nSize = pI
149bd 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b nfo->iOverflow +
149be 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 4;. }.}.#defin
149bf 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 e parseCell(pPag
149c0 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 e, iCell, pInfo)
149c1 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 \. sqlite3Btre
149c2 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
149c3 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
149c4 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
149c5 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49 ), (pInfo)).SQLI
149c6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
149c7 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
149c8 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 eCell(. MemPage
149c9 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
149ca 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
149cb 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
149cc 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 int iCell,
149cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
149ce 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 cell index. Fi
149cf 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f rst cell is 0 */
149d0 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e . CellInfo *pIn
149d1 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 fo /* Fi
149d2 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ll in this struc
149d3 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 ture */.){. par
149d4 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 seCell(pPage, iC
149d5 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a ell, pInfo);.}..
149d6 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
149d7 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
149d8 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43 f bytes that a C
149d9 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 ell needs in the
149da 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 cell.** data ar
149db 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d ea of the btree-
149dc 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 page. The retur
149dd 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 n number include
149de 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 s the cell.** da
149df 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 ta header and th
149e0 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c e local payload,
149e1 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 but not any ove
149e2 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a rflow page or.**
149e3 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 the space used
149e4 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e by the cell poin
149e5 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 ter..*/.static u
149e6 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 16 cellSizePtr(M
149e7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
149e8 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 8 *pCell){. u8
149e9 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b *pIter = &pCell[
149ea 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
149eb 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a ize];. u32 nSiz
149ec 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 e;..#ifdef SQLIT
149ed 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 E_DEBUG. /* The
149ee 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
149ef 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
149f0 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 should always b
149f1 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20 e the same as.
149f2 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f ** the (CellInfo
149f3 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f .nSize) value fo
149f4 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66 und by doing a f
149f5 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65 ull parse of the
149f6 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 . ** cell. If S
149f7 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 QLITE_DEBUG is d
149f8 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 efined, an asser
149f9 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f t() at the botto
149fa 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 m of. ** this f
149fb 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73 unction verifies
149fc 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 72 that this invar
149fd 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c iant is not viol
149fe 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 ated. */. CellI
149ff 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 nfo debuginfo;.
14a00 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
14a01 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
14a02 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e pCell, &debugin
14a03 66 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 fo);.#endif.. i
14a04 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
14a05 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 ){. u8 *pEnd
14a06 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d ;. if( pPage-
14a07 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 >hasData ){.
14a08 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 pIter += getVa
14a09 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 rint32(pIter, nS
14a0a 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ize);. }else{
14a0b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 . nSize = 0
14a0c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
14a0d 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 pIter now points
14a0e 20 61 74 20 74 68 65 20 36 34 2d 62 69 74 20 69 at the 64-bit i
14a0f 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 nteger key value
14a10 2c 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e , a variable len
14a11 67 74 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 gth . ** inte
14a12 67 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ger. The followi
14a13 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 ng block moves p
14a14 49 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 Iter to point at
14a15 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a the first byte.
14a16 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 ** past the
14a17 65 6e 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 end of the key v
14a18 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e alue. */. pEn
14a19 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 d = &pIter[9];.
14a1a 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 while( (*pIte
14a1b 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 r++)&0x80 && pIt
14a1c 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c er<pEnd );. }el
14a1d 73 65 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d se{. pIter +=
14a1e 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 getVarint32(pIt
14a1f 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a er, nSize);. }.
14a20 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 . if( nSize>pPa
14a21 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a ge->maxLocal ){.
14a22 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c int minLocal
14a23 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 = pPage->minLoc
14a24 61 6c 3b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 al;. nSize =
14a25 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a minLocal + (nSiz
14a26 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 e - minLocal) %
14a27 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 (pPage->pBt->usa
14a28 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 bleSize - 4);.
14a29 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 if( nSize>pPag
14a2a 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 e->maxLocal ){.
14a2b 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e nSize = min
14a2c 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 Local;. }.
14a2d 20 6e 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d nSize += 4;. }
14a2e 0a 20 20 6e 53 69 7a 65 20 2b 3d 20 28 70 49 74 . nSize += (pIt
14a2f 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 er - pCell);..
14a30 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 /* The minimum s
14a31 69 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 ize of any cell
14a32 69 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 is 4 bytes. */.
14a33 20 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a if( nSize<4 ){.
14a34 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 nSize = 4;.
14a35 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 }.. assert( nS
14a36 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e ize==debuginfo.n
14a37 53 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e Size );. return
14a38 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 (u16)nSize;.}.#
14a39 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
14a3a 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a atic u16 cellSiz
14a3b 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
14a3c 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 , int iCell){.
14a3d 72 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 return cellSizeP
14a3e 74 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 tr(pPage, findCe
14a3f 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 ll(pPage, iCell)
14a40 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 );.}.#endif..#if
14a41 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14a42 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a _AUTOVACUUM./*.*
14a43 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 * If the cell pC
14a44 65 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 ell, part of pag
14a45 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 e pPage contains
14a46 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f a pointer.** to
14a47 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
14a48 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 e, insert an ent
14a49 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e ry into the poin
14a4a 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 ter-map.** for t
14a4b 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
14a4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
14a4d 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
14a4e 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
14a4f 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 u8 *pCell){. C
14a50 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
14a51 61 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 assert( pCell!=0
14a52 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
14a53 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
14a54 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
14a55 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 fo);. assert( (
14a56 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 info.nData+(pPag
14a57 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f e->intKey?0:info
14a58 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 .nKey))==info.nP
14a59 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 ayload );. if(
14a5a 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 (info.nData+(pPa
14a5b 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 ge->intKey?0:inf
14a5c 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c o.nKey))>info.nL
14a5d 6f 63 61 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f ocal ){. Pgno
14a5e 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 ovfl = get4byte
14a5f 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
14a60 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65 erflow]);. re
14a61 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74 28 70 turn ptrmapPut(p
14a62 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c Page->pBt, ovfl,
14a63 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
14a64 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 1, pPage->pgno);
14a65 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
14a66 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a LITE_OK;.}./*.**
14a67 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 69 74 If the cell wit
14a68 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e h index iCell on
14a69 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 page pPage cont
14a6a 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a ains a pointer.*
14a6b 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 * to an overflow
14a6c 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e page, insert an
14a6d 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 entry into the
14a6e 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 pointer-map.** f
14a6f 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
14a70 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 page..*/.static
14a71 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66 int ptrmapPutOvf
14a72 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
14a73 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 , int iCell){.
14a74 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73 u8 *pCell;. ass
14a75 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14a76 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
14a77 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14a78 70 43 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 pCell = findOver
14a79 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 flowCell(pPage,
14a7a 69 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e iCell);. return
14a7b 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
14a7c 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b r(pPage, pCell);
14a7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
14a7e 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 * Defragment the
14a7f 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c page given. Al
14a80 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 l Cells are move
14a81 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 d to the.** end
14a82 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 of the page and
14a83 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 all free space i
14a84 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f s collected into
14a85 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 one.** big Free
14a86 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 Blk that occurs
14a87 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 in between the h
14a88 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a eader and cell.*
14a89 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 * pointer array
14a8a 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e and the cell con
14a8b 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 tent area..*/.st
14a8c 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d atic int defragm
14a8d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 entPage(MemPage
14a8e 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 *pPage){. int i
14a8f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14a90 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
14a91 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 unter */. int p
14a92 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
14a93 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
14a94 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 of a i-th cell
14a95 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 */. int addr;
14a96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a97 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72 /* Offset of fir
14a98 73 74 20 62 79 74 65 20 61 66 74 65 72 20 63 65 st byte after ce
14a99 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
14a9a 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 */. int hdr;
14a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a9c 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 /* Offset to th
14a9d 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f e page header */
14a9e 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 . int size;
14a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14aa0 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 Size of a cell
14aa1 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 */. int usableS
14aa2 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
14aa3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 /* Number of usa
14aa4 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 ble bytes on a p
14aa5 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c age */. int cel
14aa6 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 lOffset;
14aa7 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
14aa8 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 the cell pointe
14aa9 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 r array */. int
14aaa 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 cbrk;
14aab 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
14aac 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f t to the cell co
14aad 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 ntent area */.
14aae 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 int nCell;
14aaf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
14ab0 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e mber of cells on
14ab1 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 the page */. u
14ab2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
14ab3 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ta; /* The
14ab4 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 page data */.
14ab5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 unsigned char *t
14ab6 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 emp; /* Te
14ab7 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c mp area for cell
14ab8 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 content */.. a
14ab9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
14aba 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
14abb 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
14abc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
14abd 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 e->pBt!=0 );. a
14abe 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
14abf 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d t->usableSize <=
14ac0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
14ac1 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 _SIZE );. asser
14ac2 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
14ac3 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 low==0 );. asse
14ac4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14ac5 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
14ac6 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 t->mutex) );. t
14ac7 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 emp = sqlite3Pag
14ac8 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 erTempSpace(pPag
14ac9 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b e->pBt->pPager);
14aca 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
14acb 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 >aData;. hdr =
14acc 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
14acd 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d ;. cellOffset =
14ace 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 pPage->cellOffs
14acf 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 et;. nCell = pP
14ad0 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 age->nCell;. as
14ad1 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 sert( nCell==get
14ad2 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
14ad3 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 3]) );. usableS
14ad4 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 ize = pPage->pBt
14ad5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 ->usableSize;.
14ad6 63 62 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 cbrk = get2byte(
14ad7 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 &data[hdr+5]);.
14ad8 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 memcpy(&temp[cb
14ad9 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d rk], &data[cbrk]
14ada 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 , usableSize - c
14adb 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 brk);. cbrk = u
14adc 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 sableSize;. for
14add 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 (i=0; i<nCell; i
14ade 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 ++){. u8 *pAd
14adf 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 dr; /* The i
14ae0 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 -th cell pointer
14ae1 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 */. pAddr =
14ae2 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 &data[cellOffset
14ae3 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 + i*2];. pc
14ae4 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 72 = get2byte(pAddr
14ae5 29 3b 0a 20 20 20 20 69 66 28 20 70 63 3e 3d 75 );. if( pc>=u
14ae6 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
14ae7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
14ae8 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14ae9 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20 }. size =
14aea 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
14aeb 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 e, &temp[pc]);.
14aec 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b cbrk -= size;
14aed 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 63 65 . if( cbrk<ce
14aee 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c llOffset+2*nCell
14aef 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 || pc+size>usab
14af0 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 leSize ){.
14af1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
14af2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
14af3 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62 }. assert( cb
14af4 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 rk+size<=usableS
14af5 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 30 20 29 ize && cbrk>=0 )
14af6 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 ;. memcpy(&da
14af7 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b ta[cbrk], &temp[
14af8 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 pc], size);.
14af9 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c 20 put2byte(pAddr,
14afa 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 cbrk);. }. ass
14afb 65 72 74 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f ert( cbrk>=cellO
14afc 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b ffset+2*nCell );
14afd 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
14afe 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b a[hdr+5], cbrk);
14aff 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d . data[hdr+1] =
14b00 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 0;. data[hdr+2
14b01 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 ] = 0;. data[hd
14b02 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 r+7] = 0;. addr
14b03 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a = cellOffset+2*
14b04 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 nCell;. memset(
14b05 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 &data[addr], 0,
14b06 63 62 72 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73 cbrk-addr);. as
14b07 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
14b08 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
14b09 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
14b0a 0a 20 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72 . if( cbrk-addr
14b0b 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 !=pPage->nFree )
14b0c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
14b0d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
14b0e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
14b0f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
14b10 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
14b11 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
14b12 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 from within the
14b13 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 B-Tree page pas
14b14 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 sed.** as the fi
14b15 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 52 65 rst argument. Re
14b16 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 turn the index i
14b17 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 nto pPage->aData
14b18 5b 5d 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 69 [] of the .** fi
14b19 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f rst byte of allo
14b1a 63 61 74 65 64 20 73 70 61 63 65 2e 20 0a 2a 2a cated space. .**
14b1b 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 .** The caller g
14b1c 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 uarantees that t
14b1d 68 65 20 73 70 61 63 65 20 62 65 74 77 65 65 6e he space between
14b1e 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
14b1f 63 65 6c 6c 2d 6f 66 66 73 65 74 20 0a 2a 2a 20 cell-offset .**
14b20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20 73 74 array and the st
14b21 61 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d art of the cell-
14b22 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 content area is
14b23 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 20 62 at least nByte b
14b24 79 74 65 73 0a 2a 2a 20 69 6e 20 73 69 7a 65 2e ytes.** in size.
14b25 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 So this routine
14b26 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e can never fail.
14b27 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
14b28 61 72 65 20 61 6c 72 65 61 64 79 20 36 30 20 6f are already 60 o
14b29 72 20 6d 6f 72 65 20 62 79 74 65 73 20 6f 66 20 r more bytes of
14b2a 66 72 61 67 6d 65 6e 74 73 20 77 69 74 68 69 6e fragments within
14b2b 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 74 68 the page,.** th
14b2c 65 20 70 61 67 65 20 69 73 20 64 65 66 72 61 67 e page is defrag
14b2d 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 20 72 65 mented before re
14b2e 74 75 72 6e 69 6e 67 2e 20 49 66 20 74 68 69 73 turning. If this
14b2f 20 77 65 72 65 20 6e 6f 74 20 64 6f 6e 65 20 74 were not done t
14b30 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 63 68 61 here.** is a cha
14b31 6e 63 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d nce that the num
14b32 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 ber of fragmente
14b33 64 20 62 79 74 65 73 20 63 6f 75 6c 64 20 65 76 d bytes could ev
14b34 65 6e 74 75 61 6c 6c 79 20 0a 2a 2a 20 6f 76 65 entually .** ove
14b35 72 66 6c 6f 77 20 74 68 65 20 73 69 6e 67 6c 65 rflow the single
14b36 2d 62 79 74 65 20 66 69 65 6c 64 20 6f 66 20 74 -byte field of t
14b37 68 65 20 70 61 67 65 2d 68 65 61 64 65 72 20 69 he page-header i
14b38 6e 20 77 68 69 63 68 20 74 68 69 73 20 76 61 6c n which this val
14b39 75 65 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e ue.** is stored.
14b3a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
14b3b 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d llocateSpace(Mem
14b3c 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
14b3d 20 6e 42 79 74 65 29 7b 0a 20 20 63 6f 6e 73 74 nByte){. const
14b3e 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 int hdr = pPage
14b3f 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 ->hdrOffset;
14b40 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
14b41 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 f pPage->hdrOffs
14b42 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e et */. u8 * con
14b43 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d st data = pPage-
14b44 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 >aData; /*
14b45 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 Local cache of p
14b46 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 Page->aData */.
14b47 20 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 int nFrag;
14b48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b49 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
14b4a 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 of fragmented by
14b4b 74 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a tes on pPage */.
14b4c 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 0a 20 20 int top;. .
14b4d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
14b4e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
14b4f 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
14b50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
14b51 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 ge->pBt );. ass
14b52 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14b53 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
14b54 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14b55 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 assert( nByte>=0
14b56 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 ); /* Minimum
14b57 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a cell size is 4 *
14b58 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
14b59 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 e->nFree>=nByte
14b5a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
14b5b 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ge->nOverflow==0
14b5c 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 );.. /* Assert
14b5d 20 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20 that the space
14b5e 62 65 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c between the cell
14b5f 2d 6f 66 66 73 65 74 20 61 72 72 61 79 20 61 6e -offset array an
14b60 64 20 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c d the . ** cell
14b61 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 -content area is
14b62 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6e 42 greater than nB
14b63 79 74 65 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a yte bytes.. */.
14b64 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 20 assert( nByte
14b65 3c 3d 20 28 0a 20 20 20 20 20 20 67 65 74 32 62 <= (. get2b
14b66 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
14b67 29 2d 28 68 64 72 2b 38 2b 28 70 50 61 67 65 2d )-(hdr+8+(pPage-
14b68 3e 6c 65 61 66 3f 30 3a 34 29 2b 32 2a 67 65 74 >leaf?0:4)+2*get
14b69 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
14b6a 33 5d 29 29 0a 20 20 29 29 3b 0a 0a 20 20 70 50 3])). ));.. pP
14b6b 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 age->nFree -= (u
14b6c 31 36 29 6e 42 79 74 65 3b 0a 20 20 6e 46 72 61 16)nByte;. nFra
14b6d 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b g = data[hdr+7];
14b6e 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 . if( nFrag>=60
14b6f 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65 ){. defragme
14b70 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 ntPage(pPage);.
14b71 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 }else{. /* S
14b72 65 61 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 earch the freeli
14b73 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 st looking for a
14b74 20 66 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 free slot big e
14b75 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 nough to satisfy
14b76 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 . ** the req
14b77 75 65 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 uest. The alloca
14b78 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f tion is made fro
14b79 6d 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 m the first free
14b7a 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a slot in . **
14b7b 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 the list that i
14b7c 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 s large enough t
14b7d 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e o accomadate it.
14b7e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
14b7f 70 63 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f pc, addr;. fo
14b80 72 28 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 r(addr=hdr+1; (p
14b81 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 c = get2byte(&da
14b82 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 ta[addr]))>0; ad
14b83 64 72 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e dr=pc){. in
14b84 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 t size = get2byt
14b85 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 e(&data[pc+2]);
14b86 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 /* Size of f
14b87 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 ree slot */.
14b88 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 if( size>=nByt
14b89 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 e ){. int
14b8a 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 x = size - nByt
14b8b 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 e;. if( x
14b8c 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <4 ){.
14b8d 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c /* Remove the sl
14b8e 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 ot from the free
14b8f 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 -list. Update th
14b90 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 e number of.
14b91 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e ** fragmen
14b92 74 65 64 20 62 79 74 65 73 20 77 69 74 68 69 6e ted bytes within
14b93 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 the page. */.
14b94 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
14b95 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 data[addr], &dat
14b96 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 a[pc], 2);.
14b97 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d data[hdr+7]
14b98 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 = (u8)(nFrag +
14b99 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 x);. }els
14b9a 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
14b9b 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 The slot remains
14b9c 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
14b9d 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 69 t. Reduce its si
14b9e 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 ze to account.
14b9f 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 ** for t
14ba0 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 he portion used
14ba1 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 by the new alloc
14ba2 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 ation. */.
14ba3 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
14ba4 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 ta[pc+2], x);.
14ba5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
14ba6 72 65 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a 20 return pc + x;.
14ba7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
14ba8 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
14ba9 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 memory from the
14baa 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 gap in between t
14bab 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
14bac 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 array. ** and t
14bad 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
14bae 61 72 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 area.. */. top
14baf 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
14bb0 61 5b 68 64 72 2b 35 5d 29 20 2d 20 6e 42 79 74 a[hdr+5]) - nByt
14bb1 65 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 e;. put2byte(&d
14bb2 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 ata[hdr+5], top)
14bb3 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a ;. return top;.
14bb4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
14bb5 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 a section of the
14bb6 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f pPage->aData to
14bb7 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a the freelist..*
14bb8 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
14bb9 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 of the new free
14bba 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d block is pPage-
14bbb 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a >aDisk[start].**
14bbc 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 and the size of
14bbd 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 the block is "s
14bbe 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ize" bytes..**.*
14bbf 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 * Most of the ef
14bc0 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 fort here is inv
14bc1 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 olved in coalesi
14bc2 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 ng adjacent.** f
14bc3 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 ree blocks into
14bc4 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 a single big fre
14bc5 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 e block..*/.stat
14bc6 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 ic int freeSpace
14bc7 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
14bc8 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 int start, int
14bc9 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 size){. int add
14bca 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a r, pbegin, hdr;.
14bcb 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
14bcc 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 *data = pPage->a
14bcd 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 Data;.. assert(
14bce 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
14bcf 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14bd0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
14bd1 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
14bd2 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
14bd3 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 start>=pPage->h
14bd4 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67 drOffset+6+(pPag
14bd5 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a e->leaf?0:4) );.
14bd6 20 20 61 73 73 65 72 74 28 20 28 73 74 61 72 74 assert( (start
14bd7 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d + size)<=pPage-
14bd8 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
14bd9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
14bda 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14bdb 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
14bdc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
14bdd 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a size>=0 ); /*
14bde 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 Minimum cell si
14bdf 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 ze is 4 */..#ifd
14be0 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 ef SQLITE_SECURE
14be1 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 _DELETE. /* Ove
14be2 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 rwrite deleted i
14be3 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 nformation with
14be4 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 zeros when the S
14be5 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 ECURE_DELETE .
14be6 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 ** option is ena
14be7 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d bled at compile-
14be8 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 time */. memset
14be9 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 (&data[start], 0
14bea 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a , size);.#endif.
14beb 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 . /* Add the sp
14bec 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 ace back into th
14bed 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 e linked list of
14bee 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 freeblocks */.
14bef 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
14bf0 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 rOffset;. addr
14bf1 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 = hdr + 1;. whi
14bf2 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 le( (pbegin = ge
14bf3 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 t2byte(&data[add
14bf4 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 r]))<start && pb
14bf5 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 egin>0 ){. as
14bf6 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 sert( pbegin<=pP
14bf7 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
14bf8 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66 Size-4 );. if
14bf9 28 20 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29 ( pbegin<=addr )
14bfa 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
14bfb 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
14bfc 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 KPT;. }. a
14bfd 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 ddr = pbegin;.
14bfe 7d 0a 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e }. if ( pbegin>
14bff 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
14c00 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20 leSize-4 ) {.
14c01 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
14c02 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
14c03 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 . assert( pbegi
14c04 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e n>addr || pbegin
14c05 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74 ==0 );. put2byt
14c06 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73 e(&data[addr], s
14c07 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74 tart);. put2byt
14c08 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 e(&data[start],
14c09 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62 pbegin);. put2b
14c0a 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b yte(&data[start+
14c0b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61 2], size);. pPa
14c0c 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31 ge->nFree += (u1
14c0d 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 6)size;.. /* Co
14c0e 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 alesce adjacent
14c0f 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 free blocks */.
14c10 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 addr = pPage->h
14c11 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20 drOffset + 1;.
14c12 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d while( (pbegin =
14c13 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14c14 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 addr]))>0 ){.
14c15 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a int pnext, psiz
14c16 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74 e, x;. assert
14c17 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b ( pbegin>addr );
14c18 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 . assert( pbe
14c19 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d gin<=pPage->pBt-
14c1a 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b >usableSize-4 );
14c1b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 . pnext = get
14c1c 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
14c1d 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 in]);. psize
14c1e 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14c1f 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 [pbegin+2]);.
14c20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 if( pbegin + ps
14c21 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 ize + 3 >= pnext
14c22 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 && pnext>0 ){.
14c23 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 int frag =
14c24 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b pnext - (pbegin+
14c25 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 psize);. if
14c26 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 ( (frag<0) || (f
14c27 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50 rag>(int)data[pP
14c28 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 age->hdrOffset+7
14c29 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ]) ){. re
14c2a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14c2b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
14c2c 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 70 50 61 }. data[pPa
14c2d 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d ge->hdrOffset+7]
14c2e 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 -= (u8)frag;.
14c2f 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 x = get2byte
14c30 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a (&data[pnext]);.
14c31 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 put2byte(&
14c32 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 data[pbegin], x)
14c33 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 ;. x = pnex
14c34 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 t + get2byte(&da
14c35 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 ta[pnext+2]) - p
14c36 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 begin;. put
14c37 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
14c38 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d in+2], x);. }
14c39 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 else{. addr
14c3a 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d = pbegin;. }
14c3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
14c3c 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
14c3d 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 rea begins with
14c3e 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d a freeblock, rem
14c3f 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 ove it. */. if(
14c40 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 data[hdr+1]==da
14c41 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 ta[hdr+5] && dat
14c42 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 a[hdr+2]==data[h
14c43 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 dr+6] ){. int
14c44 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e top;. pbegin
14c45 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
14c46 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d a[hdr+1]);. m
14c47 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b emcpy(&data[hdr+
14c48 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 1], &data[pbegin
14c49 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d ], 2);. top =
14c4a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14c4b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 hdr+5]) + get2by
14c4c 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b te(&data[pbegin+
14c4d 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 2]);. put2byt
14c4e 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
14c4f 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 top);. }. asse
14c50 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
14c51 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
14c52 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
14c53 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
14c54 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f K;.}../*.** Deco
14c55 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 de the flags byt
14c56 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 e (the first byt
14c57 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 e of the header)
14c58 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 for a page.** a
14c59 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 nd initialize fi
14c5a 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 elds of the MemP
14c5b 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 age structure ac
14c5c 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a cordingly..**.**
14c5d 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 Only the follow
14c5e 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 ing combinations
14c5f 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 are supported.
14c60 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 Anything differ
14c61 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 ent.** indicates
14c62 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
14c63 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a ase files:.**.**
14c64 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 PTF_ZER
14c65 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 ODATA.**
14c66 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 PTF_ZERODATA |
14c67 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 PTF_LEAF.**
14c68 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 PTF_LEAFDATA
14c69 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a | PTF_INTKEY.**
14c6a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 PTF_LEA
14c6b 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b FDATA | PTF_INTK
14c6c 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f EY | PTF_LEAF.*/
14c6d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f .static int deco
14c6e 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 deFlags(MemPage
14c6f 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 *pPage, int flag
14c70 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 Byte){. BtShare
14c71 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 d *pBt; /* A
14c72 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e copy of pPage->
14c73 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pBt */.. assert
14c74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 ( pPage->hdrOffs
14c75 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f et==(pPage->pgno
14c76 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 ==1 ? 100 : 0) )
14c77 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14c78 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14c79 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
14c7a 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 ) );. pPage->le
14c7b 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 af = (u8)(flagBy
14c7c 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 te>>3); assert(
14c7d 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c PTF_LEAF == 1<<
14c7e 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 3 );. flagByte
14c7f 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 &= ~PTF_LEAF;.
14c80 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
14c81 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d ize = 4-4*pPage-
14c82 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 >leaf;. pBt = p
14c83 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 Page->pBt;. if(
14c84 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f flagByte==(PTF_
14c85 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
14c86 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 NTKEY) ){. pP
14c87 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b age->intKey = 1;
14c88 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 . pPage->hasD
14c89 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 ata = pPage->lea
14c8a 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 f;. pPage->ma
14c8b 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
14c8c 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 xLeaf;. pPage
14c8d 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 ->minLocal = pBt
14c8e 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c ->minLeaf;. }el
14c8f 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d se if( flagByte=
14c90 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b =PTF_ZERODATA ){
14c91 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b . pPage->intK
14c92 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ey = 0;. pPag
14c93 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a e->hasData = 0;.
14c94 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f pPage->maxLo
14c95 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f cal = pBt->maxLo
14c96 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e cal;. pPage->
14c97 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e minLocal = pBt->
14c98 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 minLocal;. }els
14c99 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 e{. return SQ
14c9a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
14c9b 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 T;. }. return
14c9c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
14c9d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
14c9e 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 he auxiliary inf
14c9f 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 ormation for a d
14ca0 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a isk block..**.**
14ca1 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
14ca2 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 K on success. I
14ca3 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 f we see that th
14ca4 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e e page does.** n
14ca5 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c ot contain a wel
14ca6 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 l-formed databas
14ca7 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 e page, then ret
14ca8 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 urn .** SQLITE_C
14ca9 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 ORRUPT. Note th
14caa 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 at a return of S
14cab 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f QLITE_OK does no
14cac 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
14cad 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
14cae 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 well-formed. It
14caf 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 only shows that
14cb0 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f .** we failed to
14cb1 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 detect any corr
14cb2 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 uption..*/.SQLIT
14cb3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14cb4 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 lite3BtreeInitPa
14cb5 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
14cb6 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 e){.. assert( p
14cb7 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
14cb8 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14cb9 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
14cba 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
14cbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
14cbc 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 ge->pgno==sqlite
14cbd 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
14cbe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
14cbf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
14cc0 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 age == sqlite3Pa
14cc1 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
14cc2 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
14cc3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
14cc4 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 aData == sqlite3
14cc5 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
14cc6 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
14cc7 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 . if( !pPage->i
14cc8 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 sInit ){. u16
14cc9 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
14cca 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 /* Address of a
14ccb 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e freeblock within
14ccc 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 pPage->aData[]
14ccd 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 */. u8 hdr;
14cce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
14ccf 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 set to beginning
14cd0 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 of page header
14cd1 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b */. u8 *data;
14cd2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 /* Equ
14cd3 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 al to pPage->aDa
14cd4 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 ta */. BtShar
14cd5 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
14cd6 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 /* The main btre
14cd7 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
14cd8 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a u16 usableSiz
14cd9 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 e; /* Amount
14cda 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 of usable space
14cdb 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a on each page */.
14cdc 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 u16 cellOffs
14cdd 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 et; /* Offset
14cde 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 from start of p
14cdf 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c age to first cel
14ce0 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 l pointer */.
14ce1 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
14ce2 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14ce3 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e unused bytes on
14ce4 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
14ce5 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 u16 top;
14ce6 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
14ce7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
14ce8 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20 ntent area */..
14ce9 20 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e pBt = pPage->
14cea 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 pBt;.. hdr =
14ceb 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
14cec 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 ;. data = pPa
14ced 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 ge->aData;. i
14cee 66 28 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 f( decodeFlags(p
14cef 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 Page, data[hdr])
14cf0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
14cf1 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14cf2 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
14cf3 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 pageSize>=512 &&
14cf4 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d pBt->pageSize<=
14cf5 33 32 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 32768 );. pPa
14cf6 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 ge->maskPage = p
14cf7 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 Bt->pageSize - 1
14cf8 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 ;. pPage->nOv
14cf9 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 erflow = 0;.
14cfa 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 usableSize = pBt
14cfb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 ->usableSize;.
14cfc 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 pPage->cellOff
14cfd 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 set = cellOffset
14cfe 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a = hdr + 12 - 4*
14cff 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 pPage->leaf;.
14d00 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 top = get2byte(
14d01 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 &data[hdr+5]);.
14d02 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 pPage->nCell
14d03 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14d04 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 [hdr+3]);. if
14d05 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d ( pPage->nCell>M
14d06 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 X_CELL(pBt) ){.
14d07 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 /* To many
14d08 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 cells for a sing
14d09 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 le page. The pa
14d0a 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 ge must be corru
14d0b 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 pt */. retu
14d0c 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
14d0d 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
14d0e 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 . /* Compute
14d0f 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 the total free s
14d10 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 pace on the page
14d11 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74 */. pc = get
14d12 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
14d13 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 1]);. nFree =
14d14 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 data[hdr+7] + t
14d15 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74 op - (cellOffset
14d16 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c + 2*pPage->nCel
14d17 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 l);. while( p
14d18 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36 c>0 ){. u16
14d19 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 next, size;.
14d1a 20 20 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 if( pc>usable
14d1b 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 Size-4 ){.
14d1c 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20 /* Free block
14d1d 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 is off the page
14d1e 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
14d1f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
14d20 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
14d21 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
14d22 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 2byte(&data[pc])
14d23 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 ;. size = g
14d24 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
14d25 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
14d26 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d next>0 && next<=
14d27 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20 pc+size+3 ){.
14d28 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f /* Free blo
14d29 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61 cks must be in a
14d2a 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a ccending order *
14d2b 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
14d2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
14d2d 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 BKPT; . }.
14d2e 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69 nFree += si
14d2f 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e ze;. pc = n
14d30 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ext;. }. p
14d31 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 Page->nFree = (u
14d32 31 36 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 16)nFree;. if
14d33 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 ( nFree>=usableS
14d34 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
14d35 46 72 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f Free space canno
14d36 74 20 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 t exceed total p
14d37 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 age size */.
14d38 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14d39 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 CORRUPT_BKPT; .
14d3a 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a }..#if 0. /*
14d3b 20 43 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 Check that all
14d3c 74 68 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 the offsets in t
14d3d 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 he cell offset a
14d3e 72 72 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 rray are within
14d3f 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 range. . ** .
14d40 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 ** Omitting this
14d41 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 consistency che
14d42 63 6b 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 ck and using the
14d43 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 pPage->maskPage
14d44 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 mask. ** to pr
14d45 65 76 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e event overrunnin
14d46 67 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65 g the page buffe
14d47 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 r in findCell()
14d48 72 65 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a results in a. *
14d49 2a 20 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e * 2.5% performan
14d4a 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 ce gain.. */.
14d4b 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 {. u8 *pOff;
14d4c 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
14d4d 6f 72 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b or used to check
14d4e 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 all cell offset
14d4f 73 20 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a s are in range *
14d50 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 /. u8 *pEnd;
14d51 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
14d52 72 20 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c r to end of cell
14d53 20 6f 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f offset array */
14d54 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 . u8 mask;
14d55 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 /* Mask of
14d56 20 62 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 bits that must
14d57 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f be zero in MSB o
14d58 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a f cell offsets *
14d59 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 /. mask = ~((
14d5a 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 (u8)(pBt->pageSi
14d5b 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 ze>>8))-1);.
14d5c 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c pEnd = &data[cel
14d5d 6c 4f 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d lOffset + pPage-
14d5e 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 >nCell*2];. f
14d5f 6f 72 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 or(pOff=&data[ce
14d60 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 llOffset]; pOff!
14d61 3d 70 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 =pEnd && !((*pOf
14d62 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d f)&mask); pOff+=
14d63 32 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 2);. if( pOff
14d64 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 !=pEnd ){.
14d65 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
14d66 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
14d67 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
14d68 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
14d69 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
14d6a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
14d6b 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 /*.** Set up a r
14d6c 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 aw page so that
14d6d 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 it looks like a
14d6e 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 6f database page ho
14d6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 lding.** no entr
14d70 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ies..*/.static v
14d71 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d oid zeroPage(Mem
14d72 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
14d73 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 flags){. unsig
14d74 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d ned char *data =
14d75 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
14d76 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
14d77 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 pPage->pBt;. u
14d78 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 8 hdr = pPage->h
14d79 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 drOffset;. u16
14d7a 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 first;.. assert
14d7b 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 ( sqlite3PagerPa
14d7c 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e genumber(pPage->
14d7d 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d pDbPage)==pPage-
14d7e 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 >pgno );. asser
14d7f 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
14d80 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
14d81 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 DbPage) == (void
14d82 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 *)pPage );. ass
14d83 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
14d84 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e rGetData(pPage->
14d85 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 pDbPage) == data
14d86 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
14d87 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
14d88 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
14d89 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
14d8a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14d8b 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
14d8c 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 ) );. /*memset(
14d8d 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 &data[hdr], 0, p
14d8e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
14d8f 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b hdr);*/. data[
14d90 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 hdr] = (char)fla
14d91 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64 gs;. first = hd
14d92 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 r + 8 + 4*((flag
14d93 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f s&PTF_LEAF)==0 ?
14d94 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 1:0);. memset(&
14d95 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 data[hdr+1], 0,
14d96 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 4);. data[hdr+7
14d97 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 ] = 0;. put2byt
14d98 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
14d99 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 pBt->usableSize)
14d9a 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
14d9b 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
14d9c 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 ze - first;. de
14d9d 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c codeFlags(pPage,
14d9e 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 flags);. pPage
14d9f 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 ->hdrOffset = hd
14da0 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c r;. pPage->cell
14da1 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a Offset = first;.
14da2 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
14da3 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ow = 0;. assert
14da4 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e ( pBt->pageSize>
14da5 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 =512 && pBt->pag
14da6 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a eSize<=32768 );.
14da7 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 pPage->maskPag
14da8 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
14da9 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e e - 1;. pPage->
14daa 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 nCell = 0;. pPa
14dab 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a ge->isInit = 1;.
14dac 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 }.../*.** Conver
14dad 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 t a DbPage obtai
14dae 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 ned from the pag
14daf 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 er into a MemPag
14db0 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 e used by.** the
14db1 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f btree layer..*/
14db2 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 .static MemPage
14db3 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 *btreePageFromDb
14db4 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 62 Page(DbPage *pDb
14db5 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c Page, Pgno pgno,
14db6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b BtShared *pBt){
14db7 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
14db8 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 e = (MemPage*)sq
14db9 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
14dba 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ra(pDbPage);. p
14dbb 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 Page->aData = sq
14dbc 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
14dbd 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 a(pDbPage);. pP
14dbe 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 age->pDbPage = p
14dbf 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d DbPage;. pPage-
14dc0 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 >pBt = pBt;. pP
14dc1 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f age->pgno = pgno
14dc2 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 ;. pPage->hdrOf
14dc3 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 fset = pPage->pg
14dc4 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b no==1 ? 100 : 0;
14dc5 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b . return pPage;
14dc6 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 .}../*.** Get a
14dc7 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
14dc8 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a ager. Initializ
14dc9 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
14dca 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
14dcb 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
14dcc 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a if needed..**.**
14dcd 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e If the noConten
14dce 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 t flag is set, i
14dcf 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 t means that we
14dd0 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 do not care abou
14dd1 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 t.** the content
14dd2 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20 of the page at
14dd3 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 this time. So d
14dd4 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 o not go to the
14dd5 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 disk.** to fetch
14dd6 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a the content. J
14dd7 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 ust fill in the
14dd8 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 content with zer
14dd9 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 os for now..** I
14dda 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 f in the future
14ddb 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 we call sqlite3P
14ddc 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 agerWrite() on t
14ddd 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a his page, that.*
14dde 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 * means we have
14ddf 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f started to be co
14de0 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f ncerned about co
14de1 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 ntent and the di
14de2 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c sk.** read shoul
14de3 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 d occur at that
14de4 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 point..*/.SQLITE
14de5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
14de6 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
14de7 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
14de8 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 t, /* The
14de9 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 btree */. Pgno
14dea 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
14deb 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 /* Number of the
14dec 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a page to fetch *
14ded 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
14dee 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 Page, /* Retu
14def 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 rn the page in t
14df0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f his parameter */
14df1 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
14df2 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
14df3 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 t load page cont
14df4 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 ent if true */.)
14df5 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 {. int rc;. Db
14df6 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a Page *pDbPage;..
14df7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14df8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
14df9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 ->mutex) );. rc
14dfa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 = sqlite3PagerA
14dfb 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 cquire(pBt->pPag
14dfc 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 er, pgno, (DbPag
14dfd 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f e**)&pDbPage, no
14dfe 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 Content);. if(
14dff 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
14e00 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 *ppPage = btre
14e01 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 ePageFromDbPage(
14e02 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 pDbPage, pgno, p
14e03 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 Bt);. return SQ
14e04 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
14e05 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 61 67 * Retrieve a pag
14e06 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 e from the pager
14e07 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 cache. If the r
14e08 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
14e09 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not.** already
14e0a 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 in the pager cac
14e0b 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 he return NULL.
14e0c 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d Initialize the M
14e0d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a emPage.pBt and.*
14e0e 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 * MemPage.aData
14e0f 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 elements if need
14e10 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 ed..*/.static Me
14e11 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 mPage *btreePage
14e12 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 Lookup(BtShared
14e13 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
14e14 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
14e15 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 age;. assert( s
14e16 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14e17 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
14e18 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c . pDbPage = sql
14e19 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
14e1a 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e pBt->pPager, pgn
14e1b 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 o);. if( pDbPag
14e1c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
14e1d 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 btreePageFromDbP
14e1e 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e age(pDbPage, pgn
14e1f 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 o, pBt);. }. r
14e20 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
14e21 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
14e22 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
14e23 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e e file in pages.
14e24 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 If there is any
14e25 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f kind of.** erro
14e26 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 r, return ((unsi
14e27 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f gned int)-1)..*/
14e28 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 .static Pgno pag
14e29 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 erPagecount(BtSh
14e2a 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e ared *pBt){. in
14e2b 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 t nPage = -1;.
14e2c 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
14e2d 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b ( pBt->pPage1 );
14e2e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
14e2f 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
14e30 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 t->pPager, &nPag
14e31 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 e);. assert( rc
14e32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e ==SQLITE_OK || n
14e33 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 Page==-1 );. re
14e34 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 turn (Pgno)nPage
14e35 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 ;.}../*.** Get a
14e36 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
14e37 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c ager and initial
14e38 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f ize it. This ro
14e39 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 utine.** is just
14e3a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 a convenience w
14e3b 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 rapper around se
14e3c 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a parate calls to.
14e3d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 ** sqlite3BtreeG
14e3e 65 74 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c etPage() and sql
14e3f 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
14e40 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 e()..*/.static i
14e41 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 nt getAndInitPag
14e42 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
14e43 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt, /*
14e44 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
14e45 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
14e46 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
14e47 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
14e48 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 e to get */. Me
14e49 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 mPage **ppPage
14e4a 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
14e4b 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 page pointer her
14e4c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
14e4d 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
14e4e 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ge;.. assert( s
14e4f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14e50 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
14e51 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
14e52 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
14e53 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
14e54 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 ; . }.. /* It
14e55 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73 is often the cas
14e56 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
14e57 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61 we want is alrea
14e58 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a dy in cache.. *
14e59 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20 * If so, get it
14e5a 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 directly. This
14e5b 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61 saves us from ha
14e5c 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a ving to call. *
14e5d 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 * pagerPagecount
14e5e 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 () to make sure
14e5f 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c pgno is within l
14e60 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73 imits, which res
14e61 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d ults. ** in a m
14e62 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f easureable perfo
14e63 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 rmance improveme
14e64 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50 nts.. */. *ppP
14e65 61 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 age = pPage = bt
14e66 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 reePageLookup(pB
14e67 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 t, pgno);. if(
14e68 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 pPage ){. /*
14e69 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 Page is already
14e6a 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20 in cache */.
14e6b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
14e6c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
14e6d 50 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 Page not in cach
14e6e 65 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 e. Acquire it.
14e6f 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e */. if( pgno>
14e70 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
14e71 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Bt) ){. ret
14e72 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
14e73 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a PT_BKPT; . }.
14e74 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14e75 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
14e76 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 , pgno, ppPage,
14e77 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 0);. if( rc )
14e78 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
14e79 70 50 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b pPage = *ppPage;
14e7a 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 . }. if( !pPag
14e7b 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 e->isInit ){.
14e7c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
14e7d 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
14e7e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 );. }. if( rc!
14e7f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14e80 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
14e81 61 67 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67 age);. *ppPag
14e82 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
14e83 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
14e84 20 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 Release a MemPa
14e85 67 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 ge. This should
14e86 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 be called once
14e87 66 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a for each prior.*
14e88 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 * call to sqlite
14e89 33 42 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 3BtreeGetPage..*
14e8a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
14e8b 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 leasePage(MemPag
14e8c 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 e *pPage){. if(
14e8d 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 pPage ){. as
14e8e 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
14e8f 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c erflow==0 || sql
14e90 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
14e91 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 count(pPage->pDb
14e92 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 Page)>1 );. a
14e93 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 ssert( pPage->aD
14e94 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ata );. asser
14e95 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b t( pPage->pBt );
14e96 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
14e97 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
14e98 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
14e99 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 ) == (void*)pPag
14e9a 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
14e9b 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
14e9c 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 Data(pPage->pDbP
14e9d 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 age)==pPage->aDa
14e9e 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ta );. assert
14e9f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14ea0 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
14ea1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 >mutex) );. s
14ea2 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
14ea3 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
14ea4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
14ea5 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b uring a rollback
14ea6 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 , when the pager
14ea7 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 reloads informa
14ea8 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 tion into the ca
14ea9 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 che.** so that t
14eaa 68 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 he cache is rest
14eab 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
14eac 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 inal state at th
14ead 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 e start of.** th
14eae 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 e transaction, f
14eaf 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 or each page res
14eb0 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 tored this routi
14eb1 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
14eb2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14eb3 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 needs to reset
14eb4 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 the extra data s
14eb5 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e ection at the en
14eb6 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 d of the.** page
14eb7 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 to agree with t
14eb8 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 he restored data
14eb9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14eba 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 pageReinit(DbPa
14ebb 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 ge *pData){. Me
14ebc 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
14ebd 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 pPage = (MemPage
14ebe 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
14ebf 65 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a etExtra(pData);.
14ec0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14ec1 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
14ec2 6e 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 nt(pData)>0 );.
14ec3 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e if( pPage->isIn
14ec4 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 it ){. assert
14ec5 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14ec6 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
14ec7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 >mutex) );. p
14ec8 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
14ec9 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
14eca 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
14ecb 6e 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 nt(pData)>1 ){.
14ecc 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 /* pPage mi
14ecd 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 ght not be a btr
14ece 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 ee page; it mig
14ecf 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f ht be an overflo
14ed0 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 w page. **
14ed1 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f or ptrmap page o
14ed2 72 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 r a free page.
14ed3 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 In those cases,
14ed4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
14ed5 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 ** call to s
14ed6 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
14ed7 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c age() will likel
14ed8 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f y return SQLITE_
14ed9 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a CORRUPT.. *
14eda 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 * But no harm is
14edb 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 done by this.
14edc 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69 And it is very i
14edd 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 mportant that.
14ede 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 ** sqlite3Bt
14edf 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 reeInitPage() be
14ee0 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 called on every
14ee1 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 btree page so w
14ee2 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 e make. **
14ee3 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 the call for eve
14ee4 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d ry page that com
14ee5 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 es in for re-ini
14ee6 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 73 ting. */. s
14ee7 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
14ee8 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
14ee9 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 }. }.}../*.** I
14eea 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
14eeb 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 andler for a btr
14eec 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ee..*/.static in
14eed 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 t btreeInvokeBus
14eee 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 yHandler(void *p
14eef 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 Arg){. BtShared
14ef0 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 *pBt = (BtShare
14ef1 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 d*)pArg;. asser
14ef2 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 t( pBt->db );.
14ef3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14ef4 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
14ef5 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
14ef6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e return sqlite3In
14ef7 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
14ef8 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 &pBt->db->busyHa
14ef9 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ndler);.}../*.**
14efa 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 Open a database
14efb 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 file..** .** zF
14efc 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e ilename is the n
14efd 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
14efe 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 ase file. If zF
14eff 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a ilename is NULL.
14f00 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 ** a new databas
14f01 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 e with a random
14f02 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e name is created.
14f03 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 This randomly
14f04 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 named.** databas
14f05 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 e file will be d
14f06 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 eleted when sqli
14f07 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 te3BtreeClose()
14f08 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 is called..** If
14f09 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a zFilename is ":
14f0a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e memory:" then an
14f0b 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
14f0c 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a ase is created.*
14f0d 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 * that is automa
14f0e 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 tically destroye
14f0f 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f d when it is clo
14f10 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 sed..**.** If th
14f11 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c e database is al
14f12 72 65 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 ready opened in
14f13 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
14f14 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
14f15 61 6e 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 and we are in sh
14f16 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c ared cache mode,
14f17 20 74 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 then the open w
14f18 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
14f19 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 .** SQLITE_CONST
14f1a 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 RAINT error. We
14f1b 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 cannot allow tw
14f1c 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 o or more BtShar
14f1d 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e ed.** objects in
14f1e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
14f1f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 se connection si
14f20 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c nce doing so wil
14f21 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f l lead.** to pro
14f22 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 blems with locki
14f23 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
14f24 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14f25 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 3BtreeOpen(. co
14f26 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
14f27 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 ame, /* Name of
14f28 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 the file contai
14f29 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 ning the BTree d
14f2a 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c atabase */. sql
14f2b 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
14f2c 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 /* Associat
14f2d 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 ed database hand
14f2e 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a le */. Btree **
14f2f 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 ppBtree,
14f30 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 /* Pointer to ne
14f31 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 w Btree object w
14f32 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 ritten here */.
14f33 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
14f34 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 /* Opti
14f35 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 ons */. int vfs
14f36 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
14f37 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 /* Flags passed
14f38 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 through to sqli
14f39 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 te3_vfs.xOpen()
14f3a 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f */.){. sqlite3_
14f3b 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 vfs *pVfs;
14f3c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 /* The VF
14f3d 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 S to use for thi
14f3e 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 s btree */. BtS
14f3f 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 hared *pBt = 0;
14f40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
14f41 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 hared part of bt
14f42 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f ree structure */
14f43 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 . Btree *p;
14f44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f45 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 /* Handle to r
14f46 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 eturn */. sqlit
14f47 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f e3_mutex *mutexO
14f48 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 pen = 0; /* Pre
14f49 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e vents a race con
14f4a 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 dition. Ticket #
14f4b 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3537 */. int rc
14f4c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
14f4d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
14f4e 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 lt code from thi
14f4f 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 s function */.
14f50 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 u8 nReserve;
14f51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14f52 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 * Byte of unused
14f53 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 space on each p
14f54 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 age */. unsigne
14f55 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 d char zDbHeader
14f56 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 [100]; /* Datab
14f57 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 ase header conte
14f58 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 nt */.. /* Set
14f59 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d the variable isM
14f5a 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 emdb to true for
14f5b 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
14f5c 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a tabase, or . **
14f5d 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c false for a fil
14f5e 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 e-based database
14f5f 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 . This symbol is
14f60 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 only required i
14f61 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 f. ** either of
14f62 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 the shared-data
14f63 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 or autovacuum f
14f64 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 eatures are comp
14f65 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 iled . ** into
14f66 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a the library.. *
14f67 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
14f68 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
14f69 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 D_CACHE) || !def
14f6a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
14f6b 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 _AUTOVACUUM). #
14f6c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
14f6d 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 T_MEMORYDB. c
14f6e 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 onst int isMemdb
14f6f 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 = 0;. #else.
14f70 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 const int isMe
14f71 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 mdb = zFilename
14f72 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 && !strcmp(zFile
14f73 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 name, ":memory:"
14f74 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 );. #endif.#end
14f75 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 if.. assert( db
14f76 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
14f77 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14f78 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
14f79 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e ;.. pVfs = db->
14f7a 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 pVfs;. p = sqli
14f7b 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
14f7c 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 zeof(Btree));.
14f7d 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 if( !p ){. re
14f7e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
14f7f 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 M;. }. p->inTr
14f80 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 ans = TRANS_NONE
14f81 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a ;. p->db = db;.
14f82 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
14f83 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
14f84 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
14f85 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
14f86 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a DISKIO). /*. *
14f87 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20 * If this Btree
14f88 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 is a candidate f
14f89 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c or shared cache,
14f8a 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a try to find an.
14f8b 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 ** existing Bt
14f8c 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 Shared object th
14f8d 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 at we can share
14f8e 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 with. */. if(
14f8f 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 isMemdb==0 && zF
14f90 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 ilename && zFile
14f91 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 name[0] ){. i
14f92 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
14f93 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 Config.sharedCac
14f94 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 heEnabled ){.
14f95 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 int nFullPath
14f96 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 name = pVfs->mxP
14f97 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 athname+1;.
14f98 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 char *zFullPath
14f99 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 name = sqlite3Ma
14f9a 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 lloc(nFullPathna
14f9b 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 me);. sqlit
14f9c 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 e3_mutex *mutexS
14f9d 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e hared;. p->
14f9e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 sharable = 1;.
14f9f 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d db->flags |=
14fa0 20 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 SQLITE_SharedCa
14fa1 63 68 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 che;. if( !
14fa2 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b zFullPathname ){
14fa3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
14fa4 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 _free(p);.
14fa5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14fa6 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
14fa7 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75 sqlite3OsFu
14fa8 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
14fa9 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c zFilename, nFul
14faa 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c lPathname, zFull
14fab 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
14fac 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c mutexOpen = sql
14fad 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
14fae 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
14faf 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20 IC_OPEN);.
14fb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
14fb1 74 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a ter(mutexOpen);.
14fb2 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 mutexShare
14fb3 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 d = sqlite3Mutex
14fb4 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
14fb5 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
14fb6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
14fb7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
14fb8 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 exShared);.
14fb9 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c 28 for(pBt=GLOBAL(
14fba 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
14fbb 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
14fbc 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74 2d ); pBt; pBt=pBt-
14fbd 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 >pNext){.
14fbe 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 52 assert( pBt->nR
14fbf 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 ef>0 );.
14fc0 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a 46 if( 0==strcmp(zF
14fc1 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 6c ullPathname, sql
14fc2 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d ite3PagerFilenam
14fc3 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 0a e(pBt->pPager)).
14fc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14fc5 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 && sqlite3Pager
14fc6 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29 Vfs(pBt->pPager)
14fc7 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 ==pVfs ){.
14fc8 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 int iDb;.
14fc9 20 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64 for(iDb=d
14fca 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 b->nDb-1; iDb>=0
14fcb 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 ; iDb--){.
14fcc 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 45 78 Btree *pEx
14fcd 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 isting = db->aDb
14fce 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20 [iDb].pBt;.
14fcf 20 20 20 20 20 20 20 69 66 28 20 70 45 78 69 73 if( pExis
14fd0 74 69 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e ting && pExistin
14fd1 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20 g->pBt==pBt ){.
14fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
14fd3 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
14fd4 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
14fd5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
14fd6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
14fd7 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 (mutexOpen);.
14fd8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
14fd9 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 e3_free(zFullPat
14fda 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 hname);.
14fdb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
14fdc 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 ee(p);.
14fdd 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
14fde 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 TE_CONSTRAINT;.
14fdf 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
14fe0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14fe1 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b p->pBt = pBt;
14fe2 0a 20 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e . pBt->
14fe3 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nRef++;.
14fe4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
14fe5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
14fe6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14fe7 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 eave(mutexShared
14fe8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
14fe9 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e _free(zFullPathn
14fea 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 ame);. }.#ifd
14feb 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
14fec 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
14fed 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65 /* In debug mode
14fee 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65 , we mark all pe
14fef 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 73 rsistent databas
14ff0 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20 es as sharable.
14ff1 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 ** even whe
14ff2 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 n they are not.
14ff3 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73 20 This exercises
14ff4 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 the locking code
14ff5 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69 and. ** gi
14ff6 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75 ves more opportu
14ff7 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72 74 73 nity for asserts
14ff8 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 (sqlite3_mutex_h
14ff9 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20 eld()). **
14ffa 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69 statements to fi
14ffb 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c nd locking probl
14ffc 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ems.. */.
14ffd 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 p->sharable
14ffe 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 1;. }.#endi
14fff 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 f. }.#endif. i
15000 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 f( pBt==0 ){.
15001 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 /*. ** The f
15002 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 ollowing asserts
15003 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
15004 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 structures used
15005 62 79 20 74 68 65 20 62 74 72 65 65 20 61 72 65 by the btree are
15006 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68 . ** the righ
15007 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73 t size. This is
15008 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 to guard agains
15009 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74 t size changes t
1500a 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a hat result. *
1500b 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 * when compiling
1500c 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 on a different
1500d 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20 architecture..
1500e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1500f 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 sizeof(i64)==8
15010 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d || sizeof(i64)==
15011 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 4 );. assert(
15012 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20 sizeof(u64)==8
15013 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d || sizeof(u64)==
15014 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 4 );. assert(
15015 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 sizeof(u32)==4
15016 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15017 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b izeof(u16)==2 );
15018 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
15019 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a eof(Pgno)==4 );.
1501a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c . pBt = sql
1501b 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
1501c 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a sizeof(*pBt) );.
1501d 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 if( pBt==0 )
1501e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1501f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
15020 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e goto btree_open
15021 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
15022 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15023 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42 74 rOpen(pVfs, &pBt
15024 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 6e ->pPager, zFilen
15025 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ame,.
15026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
15027 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 73 XTRA_SIZE, flags
15028 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 20 , vfsFlags);.
15029 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1502a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
1502b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 sqlite3PagerRea
1502c 64 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d dFileheader(pBt-
1502d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a >pPager,sizeof(z
1502e 44 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 DbHeader),zDbHea
1502f 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 der);. }.
15030 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15031 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 K ){. goto
15032 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15033 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 }. pBt->d
15034 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 b = db;. sqli
15035 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 te3PagerSetBusyh
15036 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 andler(pBt->pPag
15037 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 er, btreeInvokeB
15038 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 usyHandler, pBt)
15039 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 ;. p->pBt = p
1503a 42 74 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69 74 Bt;. . sqlit
1503b 65 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 e3PagerSetReinit
1503c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 er(pBt->pPager,
1503d 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 pageReinit);.
1503e 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 pBt->pCursor =
1503f 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 0;. pBt->pPag
15040 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d e1 = 0;. pBt-
15041 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 >readOnly = sqli
15042 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
15043 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b ly(pBt->pPager);
15044 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 . pBt->pageSi
15045 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a ze = get2byte(&z
15046 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 DbHeader[16]);.
15047 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 if( pBt->page
15048 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d Size<512 || pBt-
15049 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 >pageSize>SQLITE
1504a 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 _MAX_PAGE_SIZE.
1504b 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 || ((pBt
1504c 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 ->pageSize-1)&pB
1504d 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 t->pageSize)!=0
1504e 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 ){. pBt->pa
1504f 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e geSize = 0;.#ifn
15050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15051 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
15052 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 /* If the magic
15053 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 name ":memory:"
15054 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 will create an
15055 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
15056 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a se, then. *
15057 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f * leave the auto
15058 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 Vacuum mode at 0
15059 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 (do not auto-va
1505a 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 cuum), even if.
1505b 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 ** SQLITE_D
1505c 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
1505d 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 M is true. On th
1505e 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 e other hand, if
1505f 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 . ** SQLITE
15060 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 _OMIT_MEMORYDB h
15061 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c as been defined,
15062 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 then ":memory:"
15063 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 is just a.
15064 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 ** regular file
15065 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 -name. In this c
15066 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 ase the auto-vac
15067 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 uum applies as p
15068 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 er normal..
15069 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 */. if( zF
1506a 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 ilename && !isMe
1506b 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 mdb ){. p
1506c 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d Bt->autoVacuum =
1506d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 (SQLITE_DEFAULT
1506e 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 _AUTOVACUUM ? 1
1506f 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 : 0);. pB
15070 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
15071 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
15072 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 AUTOVACUUM==2 ?
15073 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 1 : 0);. }.
15074 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 #endif. nRe
15075 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d serve = 0;. }
15076 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 else{. nRes
15077 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 erve = zDbHeader
15078 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d [20];. pBt-
15079 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
1507a 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 1;.#ifndef SQLI
1507b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1507c 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75 UM. pBt->au
1507d 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 toVacuum = (get4
1507e 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b byte(&zDbHeader[
1507f 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 36 + 4*4])?1:0);
15080 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 . pBt->incr
15081 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 Vacuum = (get4by
15082 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 te(&zDbHeader[36
15083 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 + 7*4])?1:0);.#
15084 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
15085 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15086 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 rSetPagesize(pBt
15087 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e ->pPager, &pBt->
15088 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 pageSize);. i
15089 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 74 72 f( rc ) goto btr
1508a 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
1508b 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
1508c 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 = pBt->pageSize
1508d 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 - nReserve;.
1508e 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70 assert( (pBt->p
1508f 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 ageSize & 7)==0
15090 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c ); /* 8-byte al
15091 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53 ignment of pageS
15092 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 21 ize */. .#if !
15093 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
15094 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
15095 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
15096 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f LITE_OMIT_DISKIO
15097 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 ). /* Add the
15098 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 new BtShared ob
15099 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b ject to the link
1509a 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 ed list sharable
1509b 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 BtShareds..
1509c 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 */. if( p->sh
1509d 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 arable ){.
1509e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
1509f 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 utexShared;.
150a0 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b pBt->nRef = 1;
150a1 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 . mutexShar
150a2 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ed = sqlite3Mute
150a3 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
150a4 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
150a5 52 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 R);. if( SQ
150a6 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
150a7 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c && sqlite3Global
150a8 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 Config.bCoreMute
150a9 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 x ){. pBt
150aa 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 ->mutex = sqlite
150ab 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
150ac 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a TE_MUTEX_FAST);.
150ad 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d if( pBt-
150ae 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 >mutex==0 ){.
150af 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
150b0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
150b1 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
150b2 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 iled = 0;.
150b3 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
150b4 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 pen_out;.
150b5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
150b6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
150b7 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 nter(mutexShared
150b8 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e );. pBt->pN
150b9 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 ext = GLOBAL(BtS
150ba 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 hared*,sqlite3Sh
150bb 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a aredCacheList);.
150bc 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 GLOBAL(BtS
150bd 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 hared*,sqlite3Sh
150be 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d aredCacheList) =
150bf 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 pBt;. sqli
150c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
150c1 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
150c2 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a }.#endif. }..
150c3 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
150c4 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
150c5 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e CACHE) && !defin
150c6 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ed(SQLITE_OMIT_D
150c7 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 ISKIO). /* If t
150c8 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 he new Btree use
150c9 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 s a sharable pBt
150ca 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e Shared, then lin
150cb 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 k the new. ** B
150cc 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 tree into the li
150cd 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 st of all sharab
150ce 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 le Btrees for th
150cf 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f e same connectio
150d0 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 n.. ** The list
150d1 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 is kept in asce
150d2 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 nding order by p
150d3 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f Bt address.. */
150d4 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 . if( p->sharab
150d5 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b le ){. int i;
150d6 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 . Btree *pSib
150d7 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
150d8 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
150d9 20 20 20 20 20 20 69 66 28 20 28 70 53 69 62 20 if( (pSib
150da 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
150db 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 )!=0 && pSib->sh
150dc 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 arable ){.
150dd 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 while( pSib->p
150de 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 Prev ){ pSib = p
150df 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 Sib->pPrev; }.
150e0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 if( p->pBt
150e1 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 <pSib->pBt ){.
150e2 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
150e3 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 = pSib;.
150e4 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b p->pPrev = 0;
150e5 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62 2d . pSib-
150e6 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 >pPrev = p;.
150e7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
150e8 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 while( pSib
150e9 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d ->pNext && pSib-
150ea 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 >pNext->pBt<p->p
150eb 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Bt ){.
150ec 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 pSib = pSib->p
150ed 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 Next;.
150ee 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 }. p->p
150ef 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 Next = pSib->pNe
150f0 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d xt;. p-
150f1 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 >pPrev = pSib;.
150f2 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e if( p->
150f3 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 pNext ){.
150f4 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 p->pNext->p
150f5 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 Prev = p;.
150f6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
150f7 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b pSib->pNext = p;
150f8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
150f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
150fa 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
150fb 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 if. *ppBtree =
150fc 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f p;..btree_open_o
150fd 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 ut:. if( rc!=SQ
150fe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
150ff 66 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 f( pBt && pBt->p
15100 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 Pager ){. s
15101 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
15102 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
15103 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
15104 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 _free(pBt);.
15105 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
15106 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 . *ppBtree =
15107 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 0;. }. if( mut
15108 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 exOpen ){. as
15109 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1510a 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 tex_held(mutexOp
1510b 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 en) );. sqlit
1510c 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
1510d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 utexOpen);. }.
1510e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1510f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
15110 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 he BtShared.nRef
15111 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 counter. When
15112 69 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c it reaches zero,
15113 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 .** remove the B
15114 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 tShared structur
15115 65 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 e from the shari
15116 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e ng list. Return
15117 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 .** true if the
15118 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f BtShared.nRef co
15119 75 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 unter reaches ze
1511a 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a ro and return.**
1511b 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 false if it is
1511c 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a still positive..
1511d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1511e 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c moveFromSharingL
1511f 69 73 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 ist(BtShared *pB
15120 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t){.#ifndef SQLI
15121 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
15122 41 43 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d ACHE. sqlite3_m
15123 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 utex *pMaster;.
15124 20 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 BtShared *pList
15125 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 ;. int removed
15126 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
15127 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
15128 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 theld(pBt->mutex
15129 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d ) );. pMaster =
1512a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
1512b 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
1512c 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
1512d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1512e 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a enter(pMaster);.
1512f 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 pBt->nRef--;.
15130 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d if( pBt->nRef<=
15131 30 20 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 0 ){. if( GLO
15132 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 BAL(BtShared*,sq
15133 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
15134 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 List)==pBt ){.
15135 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 GLOBAL(BtSha
15136 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
15137 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 edCacheList) = p
15138 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d Bt->pNext;. }
15139 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 else{. pLis
1513a 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 t = GLOBAL(BtSha
1513b 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
1513c 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 edCacheList);.
1513d 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 while( ALWAY
1513e 53 28 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 S(pList) && pLis
1513f 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b t->pNext!=pBt ){
15140 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 . pList=p
15141 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 List->pNext;.
15142 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 }. if( A
15143 4c 57 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a LWAYS(pList) ){.
15144 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 pList->p
15145 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 Next = pBt->pNex
15146 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d t;. }. }
15147 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
15148 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 THREADSAFE ){.
15149 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1514a 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 x_free(pBt->mute
1514b 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 x);. }. re
1514c 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 moved = 1;. }.
1514d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1514e 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 eave(pMaster);.
1514f 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b return removed;
15150 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 .#else. return
15151 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 1;.#endif.}../*.
15152 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 ** Make sure pBt
15153 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e ->pTmpSpace poin
15154 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 ts to an allocat
15155 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 ion of .** MX_CE
15156 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 LL_SIZE(pBt) byt
15157 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
15158 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 id allocateTempS
15159 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 pace(BtShared *p
1515a 42 74 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d Bt){. if( !pBt-
1515b 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 >pTmpSpace ){.
1515c 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 pBt->pTmpSpace
1515d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
1515e 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 lloc( pBt->pageS
1515f 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ize );. }.}../*
15160 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 .** Free the pBt
15161 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f ->pTmpSpace allo
15162 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 cation.*/.static
15163 20 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 void freeTempSp
15164 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 ace(BtShared *pB
15165 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 t){. sqlite3Pag
15166 65 46 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 eFree( pBt->pTmp
15167 53 70 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 Space);. pBt->p
15168 54 6d 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a TmpSpace = 0;.}.
15169 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 ./*.** Close an
1516a 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e open database an
1516b 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c d invalidate all
1516c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c cursors..*/.SQL
1516d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1516e 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
1516f 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 e(Btree *p){. B
15170 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
15171 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f ->pBt;. BtCurso
15172 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 r *pCur;.. /* C
15173 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 lose all cursors
15174 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 opened via this
15175 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 handle. */. a
15176 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15177 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
15178 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
15179 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1517a 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 2d );. pCur = pBt-
1517b 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 6c >pCursor;. whil
1517c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 42 e( pCur ){. B
1517d 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d 20 tCursor *pTmp =
1517e 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 3d pCur;. pCur =
1517f 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pCur->pNext;.
15180 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 72 if( pTmp->pBtr
15181 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 73 ee==p ){. s
15182 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
15183 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 20 Cursor(pTmp);.
15184 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f }. }.. /* Ro
15185 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76 llback any activ
15186 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e e transaction an
15187 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 6c d free the handl
15188 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a e structure.. *
15189 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 * The call to sq
1518a 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
1518b 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 74 ck() drops any t
1518c 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 able-locks held
1518d 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 6e by. ** this han
1518e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 dle.. */. sqli
1518f 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
15190 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 (p);. sqlite3Bt
15191 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 reeLeave(p);..
15192 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
15193 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 still other outs
15194 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
15195 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 es to the shared
15196 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 -btree. ** stru
15197 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f cture, return no
15198 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 w. The remainder
15199 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 of this procedu
1519a 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 re cleans . **
1519b 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 up the shared-bt
1519c 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ree.. */. asse
1519d 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
1519e 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 k==0 && p->locke
1519f 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 d==0 );. if( !p
151a0 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 ->sharable || re
151a1 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c moveFromSharingL
151a2 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 ist(pBt) ){.
151a3 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f /* The pBt is no
151a4 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 longer on the s
151a5 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 haring list, so
151a6 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 we can access.
151a7 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 ** it without
151a8 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 having to hold t
151a9 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a he mutex.. **
151aa 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 . ** Clean ou
151ab 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 t and delete the
151ac 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
151ad 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
151ae 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 ert( !pBt->pCurs
151af 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 or );. sqlite
151b0 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3PagerClose(pBt-
151b1 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 >pPager);. if
151b2 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 ( pBt->xFreeSche
151b3 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 ma && pBt->pSche
151b4 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d ma ){. pBt-
151b5 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 >xFreeSchema(pBt
151b6 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
151b7 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
151b8 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 ee(pBt->pSchema)
151b9 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 ;. freeTempSp
151ba 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 ace(pBt);. sq
151bb 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b lite3_free(pBt);
151bc 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
151bd 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
151be 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 28 _CACHE. assert(
151bf 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
151c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
151c1 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 ->locked==0 );.
151c2 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 20 if( p->pPrev )
151c3 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 p->pPrev->pNext
151c4 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 66 = p->pNext;. if
151c5 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d 3e ( p->pNext ) p->
151c6 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
151c7 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 0a ->pPrev;.#endif.
151c8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
151c9 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
151ca 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
151cb 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69 Change the limi
151cc 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 t on the number
151cd 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64 of pages allowed
151ce 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a in the cache..*
151cf 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
151d0 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 number of cache
151d1 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 6f pages is set to
151d2 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a 2a the absolute.**
151d3 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 value of mxPage
151d4 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 20 . If mxPage is
151d5 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 61 negative, the pa
151d6 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 ger will.** oper
151d7 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 ate asynchronous
151d8 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f 74 ly - it will not
151d9 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 6e stop to do fsyn
151da 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 c()s.** to insur
151db 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 e data is writte
151dc 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 75 n to the disk su
151dd 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 rface before.**
151de 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 61 continuing. Tra
151df 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c 20 nsactions still
151e0 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f 6e work if synchron
151e1 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 ous is off,.** a
151e2 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nd the database
151e3 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 cannot be corrup
151e4 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f 67 ted if this prog
151e5 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e 20 ram.** crashes.
151e6 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 72 But if the oper
151e7 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61 ating system cra
151e8 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 73 shes or there is
151e9 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 6f .** an abrupt po
151ea 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 6e wer failure when
151eb 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 synchronous is
151ec 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 73 off, the databas
151ed 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c 65 e.** could be le
151ee 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 ft in an inconsi
151ef 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 6f stent and unreco
151f0 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a 2a verable state..*
151f1 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 * Synchronous is
151f2 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 73 on by default s
151f3 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
151f4 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 ption is not.**
151f5 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 79 normally a worry
151f6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
151f7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
151f8 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 treeSetCacheSize
151f9 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d (Btree *p, int m
151fa 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 xPage){. BtShar
151fb 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
151fc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
151fd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
151fe 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
151ff 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15200 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ter(p);. sqlite
15201 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69 3PagerSetCachesi
15202 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
15203 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 mxPage);. sqlit
15204 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
15205 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15206 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 _OK;.}../*.** Ch
15207 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 74 ange the way dat
15208 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64 a is synced to d
15209 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 isk in order to
1520a 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 72 increase or decr
1520b 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c ease.** how well
1520c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 the database re
1520d 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 65 sists damage due
1520e 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 61 to OS crashes a
1520f 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c nd power.** fail
15210 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 69 ures. Level 1 i
15211 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 73 s the same as as
15212 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 ynchronous (no s
15213 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e 64 yncs() occur and
15214 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 .** there is a h
15215 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 20 igh probability
15216 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 65 of damage) Leve
15217 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61 75 l 2 is the defau
15218 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 73 lt. There.** is
15219 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 20 a very low but
1521a 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 non-zero probabi
1521b 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e 20 lity of damage.
1521c 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 73 Level 3 reduces
1521d 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 6c the.** probabil
1521e 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 6f ity of damage to
1521f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 77 near zero but w
15220 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72 66 ith a write perf
15221 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 6f ormance reductio
15222 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 n..*/.#ifndef SQ
15223 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
15224 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 PRAGMAS.SQLITE_P
15225 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15226 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 e3BtreeSetSafety
15227 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c 20 Level(Btree *p,
15228 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 int level, int f
15229 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 68 ullSync){. BtSh
1522a 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1522b 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
1522c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1522d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
1522e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1522f 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 Enter(p);. sqli
15230 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 te3PagerSetSafet
15231 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 yLevel(pBt->pPag
15232 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 er, level, fullS
15233 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ync);. sqlite3B
15234 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
15235 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15236 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
15237 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
15238 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 65 the given btree
15239 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 74 is set to safet
1523a 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f y level 1. In o
1523b 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 ther.** words, r
1523c 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f eturn TRUE if no
1523d 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f sync() occurs o
1523e 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73 n the disk files
1523f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
15240 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15241 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 treeSyncDisabled
15242 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 (Btree *p){. Bt
15243 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
15244 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a >pBt;. int rc;.
15245 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15246 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
15247 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a db->mutex) ); .
15248 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15249 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
1524a 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 ( pBt && pBt->pP
1524b 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 ager );. rc = s
1524c 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e qlite3PagerNosyn
1524d 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a c(pBt->pPager);.
1524e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1524f 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
15250 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 rc;.}..#if !def
15251 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15252 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 _PAGER_PRAGMAS)
15253 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
15254 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a TE_OMIT_VACUUM).
15255 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
15256 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 default pages s
15257 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 ize and the numb
15258 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 er of reserved b
15259 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a ytes per page..*
1525a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67 * Or, if the pag
1525b 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 e size has alrea
1525c 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72 dy been fixed, r
1525d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
1525e 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 DONLY .** withou
1525f 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 t changing anyth
15260 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ing..**.** The p
15261 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 age size must be
15262 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 a power of 2 be
15263 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 tween 512 and 65
15264 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 67 536. If the pag
15265 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 e.** size suppli
15266 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 ed does not meet
15267 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
15268 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 73 then the page s
15269 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 ize is not.** ch
1526a 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 anged..**.** Pag
1526b 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 e sizes are cons
1526c 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20 trained to be a
1526d 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 power of two so
1526e 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a that the region.
1526f 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ** of the databa
15270 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 se file used for
15271 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e locking (beginn
15272 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 ing at PENDING_B
15273 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 YTE,.** the firs
15274 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 t byte past the
15275 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 1GB boundary, 0x
15276 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 20 40000000) needs
15277 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 to occur.** at t
15278 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
15279 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 a page..**.** If
1527a 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 parameter nRese
1527b 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e rve is less than
1527c 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
1527d 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 number of reserv
1527e 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20 ed.** bytes per
1527f 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 page is left unc
15280 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 hanged..**.** If
15281 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68 65 the iFix!=0 the
15282 6e 20 74 68 65 20 70 61 67 65 53 69 7a 65 46 69 n the pageSizeFi
15283 78 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 xed flag is set
15284 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67 65 so that the page
15285 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 74 size.** and aut
15286 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 63 61 6e ovacuum mode can
15287 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 no longer be ch
15288 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 anged..*/.SQLITE
15289 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1528a 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 ite3BtreeSetPage
1528b 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 Size(Btree *p, i
1528c 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69 6e 74 nt pageSize, int
1528d 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 69 nReserve, int i
1528e 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Fix){. int rc =
1528f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 SQLITE_OK;. Bt
15290 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
15291 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 >pBt;. assert(
15292 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 20 nReserve>=-1 &&
15293 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b nReserve<=255 );
15294 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
15295 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 nter(p);. if( p
15296 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
15297 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 d ){. sqlite3
15298 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
15299 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1529a 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 _READONLY;. }.
1529b 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 if( nReserve<0
1529c 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 ){. nReserve
1529d 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
1529e 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a - pBt->usableSiz
1529f 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 e;. }. assert(
152a0 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 nReserve>=0 &&
152a1 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b nReserve<=255 );
152a2 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 3e . if( pageSize>
152a3 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 =512 && pageSize
152a4 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 <=SQLITE_MAX_PAG
152a5 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20 20 20 E_SIZE &&.
152a6 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 ((pageSize-1)&
152a7 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a pageSize)==0 ){.
152a8 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67 assert( (pag
152a9 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b eSize & 7)==0 );
152aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 . assert( !pB
152ab 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21 70 42 t->pPage1 && !pB
152ac 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 t->pCursor );.
152ad 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
152ae 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b = (u16)pageSize;
152af 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 . freeTempSpa
152b0 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 ce(pBt);. rc
152b1 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
152b2 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
152b3 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
152b4 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42 eSize);. }. pB
152b5 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 t->usableSize =
152b6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
152b7 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 (u16)nReserve;.
152b8 20 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d if( iFix ) pBt-
152b9 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
152ba 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 1;. sqlite3Btr
152bb 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
152bc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
152bd 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 * Return the cur
152be 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 rently defined p
152bf 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 age size.*/.SQLI
152c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
152c1 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
152c2 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 geSize(Btree *p)
152c3 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 {. return p->pB
152c4 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a t->pageSize;.}..
152c5 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
152c6 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
152c7 20 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 of space at the
152c8 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 end of every pa
152c9 67 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 ge that.** are i
152ca 6e 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 ntentually left
152cb 75 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 unused. This is
152cc 20 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 the "reserved"
152cd 73 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a space that is.**
152ce 20 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 sometimes used
152cf 62 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a by extensions..*
152d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
152d1 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
152d2 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 eGetReserve(Btre
152d3 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a e *p){. int n;.
152d4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
152d5 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d ter(p);. n = p-
152d6 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d >pBt->pageSize -
152d7 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 p->pBt->usableS
152d8 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ize;. sqlite3Bt
152d9 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
152da 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
152db 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 * Set the maximu
152dc 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 m page count for
152dd 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d a database if m
152de 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 xPage is positiv
152df 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 e..** No changes
152e0 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 are made if mxP
152e1 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 age is 0 or nega
152e2 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c tive..** Regardl
152e3 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ess of the value
152e4 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 of mxPage, retu
152e5 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 rn the maximum p
152e6 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 age count..*/.SQ
152e7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
152e8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 sqlite3BtreeMax
152e9 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 PageCount(Btree
152ea 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b *p, int mxPage){
152eb 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
152ec 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
152ed 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 ;. n = sqlite3P
152ee 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
152ef 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c (p->pBt->pPager,
152f0 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 mxPage);. sqli
152f1 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
152f2 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a ;. return n;.}.
152f3 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
152f4 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ed(SQLITE_OMIT_P
152f5 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c AGER_PRAGMAS) ||
152f6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
152f7 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f _OMIT_VACUUM) */
152f8 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
152f9 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 he 'auto-vacuum'
152fa 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 property of the
152fb 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 database. If th
152fc 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a e 'autoVacuum'.*
152fd 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e * parameter is n
152fe 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 on-zero, then au
152ff 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 to-vacuum mode i
15300 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 s enabled. If ze
15301 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 ro, it.** is dis
15302 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 abled. The defau
15303 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
15304 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f auto-vacuum pro
15305 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 perty is .** det
15306 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 ermined by the S
15307 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
15308 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a TOVACUUM macro..
15309 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1530a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1530b 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeSetAutoVacuum(
1530c 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 Btree *p, int au
1530d 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 toVacuum){.#ifde
1530e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1530f 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 TOVACUUM. retur
15310 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
15311 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 Y;.#else. BtSha
15312 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
15313 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
15314 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 LITE_OK;. u8 av
15315 20 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 = (u8)autoVacuu
15316 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 m;.. sqlite3Btr
15317 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
15318 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 ( pBt->pageSizeF
15319 69 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 ixed && (av ?1:0
1531a 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 )!=pBt->autoVacu
1531b 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 um ){. rc = S
1531c 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
1531d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 }else{. pBt
1531e 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 ->autoVacuum = a
1531f 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d v ?1:0;. pBt-
15320 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 >incrVacuum = av
15321 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 ==2 ?1:0;. }.
15322 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
15323 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
15324 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a c;.#endif.}../*.
15325 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 ** Return the va
15326 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f lue of the 'auto
15327 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 -vacuum' propert
15328 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 y. If auto-vacuu
15329 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 m is .** enabled
1532a 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 1 is returned.
1532b 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a Otherwise 0..*/.
1532c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1532d 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
1532e 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
1532f 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 ee *p){.#ifdef S
15330 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15331 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 ACUUM. return B
15332 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
15333 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e NONE;.#else. in
15334 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 t rc;. sqlite3B
15335 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
15336 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e rc = (. (!p->
15337 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
15338 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
15339 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d M_NONE:. (!p-
1533a 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d >pBt->incrVacuum
1533b 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 )?BTREE_AUTOVACU
1533c 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 UM_FULL:. BTR
1533d 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e EE_AUTOVACUUM_IN
1533e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 CR. );. sqlite
1533f 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15340 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
15341 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 dif.}.../*.** Ge
15342 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f t a reference to
15343 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 pPage1 of the d
15344 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
15345 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f his will.** also
15346 20 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c acquire a readl
15347 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 ock on that file
15348 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
15349 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e K is returned on
1534a 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 success. If th
1534b 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a e file is not a.
1534c 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 ** well-formed d
1534d 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
1534e 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 en SQLITE_CORRUP
1534f 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a T is returned..*
15350 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 * SQLITE_BUSY is
15351 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
15352 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 database is loc
15353 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d ked. SQLITE_NOM
15354 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 EM.** is returne
15355 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 d if we run out
15356 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 of memory. .*/.s
15357 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 tatic int lockBt
15358 72 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 ree(BtShared *pB
15359 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
1535a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
1535b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 . int nPage;..
1535c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1535d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1535e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1535f 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
15360 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ==0 );. rc = sq
15361 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
15362 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 e(pBt, 1, &pPage
15363 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 1, 0);. if( rc!
15364 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
15365 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f urn rc;.. /* Do
15366 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 some checking t
15367 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 o help insure th
15368 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 e file we opened
15369 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 really is. **
1536a 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 a valid database
1536b 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 file. . */. r
1536c 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1536d 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
1536e 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
1536f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15370 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 _OK ){. goto
15371 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
15372 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e d;. }else if( n
15373 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e Page>0 ){. in
15374 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 t pageSize;.
15375 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a int usableSize;.
15376 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 u8 *page1 =
15377 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 pPage1->aData;.
15378 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
15379 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d OTADB;. if( m
1537a 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 emcmp(page1, zMa
1537b 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d gicHeader, 16)!=
1537c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1537d 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
1537e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 d;. }. if(
1537f 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a page1[18]>1 ){.
15380 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f pBt->readO
15381 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 nly = 1;. }.
15382 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d if( page1[19]
15383 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f >1 ){. goto
15384 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
15385 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ed;. }.. /
15386 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d * The maximum em
15387 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 bedded fraction
15388 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 must be exactly
15389 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 25%. And the mi
1538a 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 nimum. ** emb
1538b 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d edded fraction m
1538c 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72 ust be 12.5% for
1538d 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20 both leaf-data
1538e 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 and non-leaf-dat
1538f 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 a.. ** The or
15390 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c iginal design al
15391 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75 lowed these amou
15392 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74 nts to vary, but
15393 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 as of. ** ve
15394 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 rsion 3.6.0, we
15395 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 require them to
15396 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f be fixed.. */
15397 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 . if( memcmp(
15398 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 &page1[21], "\10
15399 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 0\040\040",3)!=0
1539a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
1539b 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
1539c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 ;. }. page
1539d 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 Size = get2byte(
1539e 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 &page1[16]);.
1539f 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d if( ((pageSize-
153a0 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20 1)&pageSize)!=0
153a1 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20 || pageSize<512
153a2 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49 ||. (SQLI
153a3 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
153a4 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53 69 <32768 && pageSi
153a5 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
153a6 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a GE_SIZE). ){.
153a7 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
153a8 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
153a9 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
153aa 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d (pageSize & 7)==
153ab 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 0 );. usableS
153ac 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d ize = pageSize -
153ad 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 page1[20];.
153ae 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42 if( pageSize!=pB
153af 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 t->pageSize ){.
153b0 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 /* After re
153b1 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 ading the first
153b2 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
153b3 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 base assuming a
153b4 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 page size.
153b5 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 ** of BtShared.p
153b6 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 ageSize, we have
153b7 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 discovered that
153b8 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 the page-size i
153b9 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 s. ** actua
153ba 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e lly pageSize. Un
153bb 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
153bc 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 e, leave pBt->pP
153bd 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a age1 at. **
153be 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e zero and return
153bf 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 SQLITE_OK. The
153c0 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c caller will call
153c1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 this function.
153c2 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 ** again wi
153c3 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 th the correct p
153c4 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 age-size..
153c5 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 */. release
153c6 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 Page(pPage1);.
153c7 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 pBt->usableS
153c8 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c ize = (u16)usabl
153c9 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 eSize;. pBt
153ca 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 ->pageSize = (u1
153cb 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 6)pageSize;.
153cc 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 freeTempSpace(
153cd 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d pBt);. rc =
153ce 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
153cf 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 Pagesize(pBt->pP
153d0 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 ager, &pBt->page
153d1 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Size);. if(
153d2 20 72 63 20 29 20 67 6f 74 6f 20 70 61 67 65 31 rc ) goto page1
153d3 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
153d4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
153d5 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
153d6 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 35 if( usableSize<5
153d7 30 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 00 ){. goto
153d8 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
153d9 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ed;. }. pB
153da 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 t->pageSize = (u
153db 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 16)pageSize;.
153dc 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
153dd 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 = (u16)usableSi
153de 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ze;.#ifndef SQLI
153df 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
153e0 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f UM. pBt->auto
153e1 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 Vacuum = (get4by
153e2 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 te(&page1[36 + 4
153e3 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 *4])?1:0);. p
153e4 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d Bt->incrVacuum =
153e5 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 (get4byte(&page
153e6 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 1[36 + 7*4])?1:0
153e7 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 );.#endif. }..
153e8 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 /* maxLocal is
153e9 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 the maximum amou
153ea 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f nt of payload to
153eb 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 store locally f
153ec 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 or. ** a cell.
153ed 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 Make sure it is
153ee 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f small enough so
153ef 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d that at least m
153f0 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 inFanout. ** ce
153f1 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 lls can will fit
153f2 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 on one page. W
153f3 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 e assume a 10-by
153f4 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a te page header..
153f5 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 ** Besides the
153f6 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 payload, the ce
153f7 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 ll must store:.
153f8 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 ** 2-byte p
153f9 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 ointer to the ce
153fa 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 ll. ** 4-by
153fb 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 te child pointer
153fc 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 . ** 9-byte
153fd 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a nKey value. **
153fe 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 4-byte nDat
153ff 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 a value. **
15400 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 4-byte overflow
15401 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 page pointer.
15402 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e ** So a cell con
15403 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 sists of a 2-byt
15404 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 e poiner, a head
15405 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d er which is as m
15406 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 uch as. ** 17 b
15407 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 ytes long, 0 to
15408 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f N bytes of paylo
15409 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f ad, and an optio
1540a 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 nal 4 byte overf
1540b 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f low. ** page po
1540c 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 inter.. */. pB
1540d 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 t->maxLocal = (p
1540e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 Bt->usableSize-1
1540f 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 2)*64/255 - 23;.
15410 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 pBt->minLocal
15411 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = (pBt->usableSi
15412 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 ze-12)*32/255 -
15413 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 23;. pBt->maxLe
15414 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 af = pBt->usable
15415 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 Size - 35;. pBt
15416 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 ->minLeaf = (pBt
15417 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 ->usableSize-12)
15418 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 *32/255 - 23;.
15419 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 assert( pBt->max
1541a 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f Leaf + 23 <= MX_
1541b 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 CELL_SIZE(pBt) )
1541c 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 ;. pBt->pPage1
1541d 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 = pPage1;. retu
1541e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 rn SQLITE_OK;..p
1541f 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
15420 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 :. releasePage(
15421 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e pPage1);. pBt->
15422 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 pPage1 = 0;. re
15423 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15424 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
15425 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 74 orks like lockBt
15426 72 65 65 28 29 20 65 78 63 65 70 74 20 74 68 61 ree() except tha
15427 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b 65 t it also invoke
15428 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 s the.** busy ca
15429 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65 20 llback if there
1542a 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 is lock contenti
1542b 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
1542c 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 t lockBtreeWithR
1542d 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 65 66 etry(Btree *pRef
1542e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1542f 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
15430 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
15431 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65 66 29 HoldsMutex(pRef)
15432 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66 2d 3e );. if( pRef->
15433 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e inTrans==TRANS_N
15434 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20 69 6e ONE ){. u8 in
15435 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 52 Transaction = pR
15436 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 ef->pBt->inTrans
15437 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72 65 action;. btre
15438 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66 29 eIntegrity(pRef)
15439 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1543a 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e e3BtreeBeginTran
1543b 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20 20 20 s(pRef, 0);.
1543c 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 pRef->pBt->inTra
1543d 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72 61 nsaction = inTra
1543e 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70 52 nsaction;. pR
1543f 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 ef->inTrans = TR
15440 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69 66 ANS_NONE;. if
15441 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15442 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d 3e 70 ){. pRef->p
15443 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e Bt->nTransaction
15444 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 74 --;. }. bt
15445 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 reeIntegrity(pRe
15446 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e f);. }. return
15447 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a 0a rc;.}. ..
15448 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 /*.** If there a
15449 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
1544a 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 g cursors and we
1544b 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 are not in the
1544c 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 middle.** of a t
1544d 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 ransaction but t
1544e 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 6c here is a read l
1544f 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
15450 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 ase, then.** thi
15451 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 s routine unrefs
15452 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
15453 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
15454 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 file which .** h
15455 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 as the effect of
15456 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 releasing the r
15457 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 ead lock..**.**
15458 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 If there are any
15459 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 outstanding cur
1545a 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 sors, this routi
1545b 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
1545c 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
1545d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
1545e 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 n progress, this
1545f 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
15460 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 -op..*/.static v
15461 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 oid unlockBtreeI
15462 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 fUnused(BtShared
15463 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 *pBt){. assert
15464 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15465 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
15466 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 );. if( pBt->i
15467 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
15468 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d ANS_NONE && pBt-
15469 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26 20 70 >pCursor==0 && p
1546a 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b Bt->pPage1!=0 ){
1546b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1546c 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 PagerRefcount(pB
1546d 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20 29 7b t->pPager)>=1 ){
1546e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1546f 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
15470 61 20 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 a );. relea
15471 73 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 sePage(pBt->pPag
15472 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 e1);. }. p
15473 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a Bt->pPage1 = 0;.
15474 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 }.}../*.** Cre
15475 61 74 65 20 61 20 6e 65 77 20 64 61 74 61 62 61 ate a new databa
15476 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 se by initializi
15477 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ng the first pag
15478 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 e of the.** file
15479 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1547a 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68 newDatabase(BtSh
1547b 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 ared *pBt){. Me
1547c 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e mPage *pP1;. un
1547d 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
1547e 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 a;. int rc;. i
1547f 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 nt nPage;.. ass
15480 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15481 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
15482 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ex) );. rc = sq
15483 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
15484 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c unt(pBt->pPager,
15485 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
15486 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
15487 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 nPage>0 ){.
15488 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
15489 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 pP1 = pBt->pPag
1548a 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 e1;. assert( pP
1548b 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 1!=0 );. data =
1548c 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 pP1->aData;. r
1548d 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1548e 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 Write(pP1->pDbPa
1548f 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 ge);. if( rc )
15490 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d return rc;. mem
15491 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 cpy(data, zMagic
15492 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a Header, sizeof(z
15493 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 MagicHeader));.
15494 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
15495 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 zMagicHeader)==1
15496 36 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 6 );. put2byte(
15497 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e &data[16], pBt->
15498 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 pageSize);. dat
15499 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 a[18] = 1;. dat
1549a 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 a[19] = 1;. ass
1549b 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 ert( pBt->usable
1549c 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 Size<=pBt->pageS
1549d 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 ize && pBt->usab
1549e 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d leSize+255>=pBt-
1549f 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 >pageSize);. da
154a0 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 ta[20] = (u8)(pB
154a1 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 t->pageSize - pB
154a2 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a t->usableSize);.
154a3 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b data[21] = 64;
154a4 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 . data[22] = 32
154a5 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 ;. data[23] = 3
154a6 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 2;. memset(&dat
154a7 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 a[24], 0, 100-24
154a8 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 );. zeroPage(pP
154a9 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 1, PTF_INTKEY|PT
154aa 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 F_LEAF|PTF_LEAFD
154ab 41 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 ATA );. pBt->pa
154ac 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b geSizeFixed = 1;
154ad 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
154ae 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
154af 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 assert( pBt->a
154b0 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 utoVacuum==1 ||
154b1 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d pBt->autoVacuum=
154b2 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
154b3 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d pBt->incrVacuum=
154b4 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 =1 || pBt->incrV
154b5 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 acuum==0 );. pu
154b6 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 t4byte(&data[36
154b7 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 + 4*4], pBt->aut
154b8 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 oVacuum);. put4
154b9 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 byte(&data[36 +
154ba 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 7*4], pBt->incrV
154bb 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 acuum);.#endif.
154bc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
154bd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 K;.}../*.** Atte
154be 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e mpt to start a n
154bf 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ew transaction.
154c0 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 A write-transact
154c1 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 ion.** is starte
154c2 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 d if the second
154c3 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a argument is nonz
154c4 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ero, otherwise a
154c5 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 read-.** transa
154c6 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 ction. If the s
154c7 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
154c8 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 s 2 or more and
154c9 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 exclusive.** tra
154ca 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
154cb 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 ted, meaning tha
154cc 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 t no other proce
154cd 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a ss is allowed.**
154ce 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 to access the d
154cf 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 atabase. A pree
154d0 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 xisting transact
154d1 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a ion may not be.*
154d2 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 * upgraded to ex
154d3 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 clusive by calli
154d4 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
154d5 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 a second time -
154d6 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 the.** exclusivi
154d7 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 ty flag only wor
154d8 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 ks for a new tra
154d9 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
154da 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 A write-transact
154db 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 ion must be star
154dc 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d ted before attem
154dd 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 pting any .** ch
154de 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 anges to the dat
154df 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 abase. None of
154e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f the following ro
154e1 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 utines .** will
154e2 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 work unless a tr
154e3 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
154e4 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a rted first:.**.*
154e5 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 * sqlite3Bt
154e6 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 reeCreateTable()
154e7 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
154e8 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 BtreeCreateIndex
154e9 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
154ea 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
154eb 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
154ec 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
154ed 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
154ee 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 te3BtreeInsert()
154ef 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
154f0 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a BtreeDelete().**
154f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
154f2 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a eeUpdateMeta().*
154f3 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 *.** If an initi
154f4 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 al attempt to ac
154f5 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 quire the lock f
154f6 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 ails because of
154f7 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a lock contention.
154f8 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 ** and the datab
154f9 61 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 ase was previous
154fa 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 ly unlocked, the
154fb 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 n invoke the bus
154fc 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 y handler.** if
154fd 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 there is one. B
154fe 75 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 ut if there was
154ff 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 previously a rea
15500 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a d-lock, do not.*
15501 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 * invoke the bus
15502 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 y handler - just
15503 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
15504 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 USY. SQLITE_BUS
15505 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 Y is .** returne
15506 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 d when there is
15507 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c already a read-l
15508 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ock in order to
15509 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b avoid a deadlock
1550a 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 ..**.** Suppose
1550b 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 there are two pr
1550c 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e ocesses A and B.
1550d 20 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c A has a read l
1550e 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a ock and B has.**
1550f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
15510 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 . B tries to pr
15511 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 omote to exclusi
15512 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 ve but is blocke
15513 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 d because.** of
15514 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 A's read lock.
15515 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f A tries to promo
15516 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 te to reserved b
15517 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 ut is blocked by
15518 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 B..** One or th
15519 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 e other of the t
1551a 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 wo processes mus
1551b 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 t give way or th
1551c 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f ere can be.** no
1551d 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 progress. By r
1551e 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
1551f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 BUSY and not inv
15520 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 oking the busy c
15521 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 allback.** when
15522 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 A already has a
15523 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e read lock, we en
15524 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 courage A to giv
15525 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a e up and let B.*
15526 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 * proceed..*/.SQ
15527 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15528 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
15529 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 inTrans(Btree *p
1552a 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 , int wrflag){.
1552b 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b sqlite3 *pBlock
1552c 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 = 0;. BtShared
1552d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1552e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1552f 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 E_OK;.. sqlite3
15530 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
15531 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
15532 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
15533 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 btree is alread
15534 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 y in a write-tra
15535 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a nsaction, or it.
15536 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 ** is already
15537 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 in a read-transa
15538 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 ction and a read
15539 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a -transaction. *
1553a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 * is requested,
1553b 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e this is a no-op.
1553c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1553d 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1553e 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 ITE || (p->inTra
1553f 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 ns==TRANS_READ &
15540 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 & !wrflag) ){.
15541 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 goto trans_beg
15542 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 un;. }.. /* Wr
15543 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite transactions
15544 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c are not possibl
15545 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 e on a read-only
15546 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 database */. i
15547 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 f( pBt->readOnly
15548 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 && wrflag ){.
15549 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 rc = SQLITE_RE
1554a 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f ADONLY;. goto
1554b 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 trans_begun;.
1554c 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1554d 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1554e 43 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 CHE. /* If anot
1554f 68 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e her database han
15550 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 dle has already
15551 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 opened a write t
15552 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a ransaction . **
15553 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d on this shared-
15554 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 btree structure
15555 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 and a second wri
15556 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te transaction i
15557 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 s. ** requested
15558 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
15559 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 LOCKED.. */. i
1555a 66 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 f( (wrflag && pB
1555b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1555c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c ==TRANS_WRITE) |
1555d 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 | pBt->isPending
1555e 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d ){. pBlock =
1555f 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 pBt->pWriter->d
15560 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 b;. }else if( w
15561 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 rflag>1 ){. B
15562 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 tLock *pIter;.
15563 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d for(pIter=pBt-
15564 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 >pLock; pIter; p
15565 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 Iter=pIter->pNex
15566 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 t){. if( pI
15567 74 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 ter->pBtree!=p )
15568 7b 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b {. pBlock
15569 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 = pIter->pBtree
1556a 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 ->db;. br
1556b 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1556c 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c }. }. if( pBl
1556d 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ock ){. sqlit
1556e 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
1556f 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 ked(p->db, pBloc
15570 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c k);. rc = SQL
15571 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
15572 44 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f DCACHE;. goto
15573 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 trans_begun;.
15574 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b }.#endif.. do {
15575 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 . /* Call loc
15576 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 kBtree() until e
15577 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 ither pBt->pPage
15578 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 1 is populated o
15579 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 r. ** lockBtr
1557a 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d ee() returns som
1557b 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ething other tha
1557c 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 n SQLITE_OK. loc
1557d 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 kBtree(). **
1557e 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
1557f 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 E_OK but leave p
15580 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 Bt->pPage1 set t
15581 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 o 0 if after.
15582 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 ** reading page
15583 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 1 it discovers
15584 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 that the page-si
15585 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
15586 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 se . ** file
15587 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 is not pBt->page
15588 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 Size. In this ca
15589 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 se lockBtree() w
1558a 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a ill update. *
1558b 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 * pBt->pageSize
1558c 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 to the page-size
1558d 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 of the file on
1558e 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 disk.. */.
1558f 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 while( pBt->pPa
15590 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 ge1==0 && SQLITE
15591 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 _OK==(rc = lockB
15592 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 tree(pBt)) );..
15593 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15594 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 E_OK && wrflag )
15595 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d {. if( pBt-
15596 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
15597 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
15598 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 _READONLY;.
15599 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1559a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1559b 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 rBegin(pBt->pPag
1559c 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 er,wrflag>1,sqli
1559d 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 te3TempInMemory(
1559e 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 p->db));.
1559f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
155a0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
155a1 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 rc = newDatabase
155a2 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d (pBt);. }
155a3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
155a4 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 . if( rc!=SQ
155a5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
155a6 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
155a7 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d used(pBt);. }
155a8 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 . }while( rc==S
155a9 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 QLITE_BUSY && pB
155aa 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
155ab 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a ==TRANS_NONE &&.
155ac 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 btreeI
155ad 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
155ae 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 (pBt) );.. if(
155af 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
155b0 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
155b1 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ans==TRANS_NONE
155b2 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 ){. pBt->nT
155b3 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 ransaction++;.
155b4 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 }. p->inTra
155b5 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 ns = (wrflag?TRA
155b6 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 NS_WRITE:TRANS_R
155b7 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d EAD);. if( p-
155b8 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e >inTrans>pBt->in
155b9 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
155ba 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e pBt->inTran
155bb 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 saction = p->inT
155bc 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e rans;. }.#ifn
155bd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
155be 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 SHARED_CACHE.
155bf 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 if( wrflag ){.
155c0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 assert( !pB
155c1 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 t->pWriter );.
155c2 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 pBt->pWriter
155c3 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d = p;. pBt-
155c4 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 28 >isExclusive = (
155c5 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 u8)(wrflag>1);.
155c6 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
155c7 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 ..trans_begun:.
155c8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
155c9 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a OK && wrflag ){.
155ca 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c /* This call
155cb 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 makes sure that
155cc 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 74 the pager has t
155cd 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 he correct numbe
155ce 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e r of. ** open
155cf 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 savepoints. If
155d0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
155d1 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72 20 eter is greater
155d2 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a than 0 and. *
155d3 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 * the sub-journa
155d4 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 l is not already
155d5 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 open, then it w
155d6 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 ill be opened he
155d7 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 re.. */. r
155d8 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
155d9 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 OpenSavepoint(pB
155da 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 t->pPager, p->db
155db 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 ->nSavepoint);.
155dc 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 }.. btreeInteg
155dd 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 rity(p);. sqlit
155de 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
155df 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
155e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
155e1 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
155e2 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 ./*.** Set the p
155e3 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
155e4 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 es for all child
155e5 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 ren of page pPag
155e6 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 e. Also, if.** p
155e7 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 Page contains ce
155e8 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 lls that point t
155e9 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
155ea 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 , set the pointe
155eb 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 r.** map entries
155ec 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f for the overflo
155ed 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e w pages as well.
155ee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
155ef 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d etChildPtrmaps(M
155f0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
155f1 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
155f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155f3 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 /* Counter
155f4 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e variable */. in
155f5 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 t nCell;
155f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155f7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 /* Number of ce
155f8 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 lls in page pPag
155f9 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
155fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
155fc 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 urn code */. Bt
155fd 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
155fe 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 age->pBt;. u8 i
155ff 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 sInitOrig = pPag
15600 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e e->isInit;. Pgn
15601 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e o pgno = pPage->
15602 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 pgno;.. assert(
15603 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15604 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15605 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
15606 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
15607 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 tPage(pPage);.
15608 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15609 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 K ){. goto se
1560a 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f t_child_ptrmaps_
1560b 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c out;. }. nCell
1560c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
1560d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e .. for(i=0; i<n
1560e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
1560f 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 u8 *pCell = find
15610 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a Cell(pPage, i);.
15611 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 . rc = ptrmap
15612 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 PutOvflPtr(pPage
15613 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 , pCell);. if
15614 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15615 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 ){. goto se
15616 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f t_child_ptrmaps_
15617 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 out;. }..
15618 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
15619 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 ){. Pgno c
1561a 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 hildPgno = get4b
1561b 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 yte(pCell);.
1561c 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
1561d 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c (pBt, childPgno,
1561e 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 PTRMAP_BTREE, p
1561f 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
15620 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
15621 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 goto set_child_p
15622 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 trmaps_out;.
15623 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 }. }.. if( !pP
15624 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
15625 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 Pgno childPgno
15626 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
15627 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
15628 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
15629 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
1562a 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f t(pBt, childPgno
1562b 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
1562c 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f pgno);. }..set_
1562d 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 child_ptrmaps_ou
1562e 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e t:. pPage->isIn
1562f 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b it = isInitOrig;
15630 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15631 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 ./*.** Somewhere
15632 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 69 63 68 on pPage, which
15633 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
15634 6f 20 62 65 20 61 20 62 74 72 65 65 20 70 61 67 o be a btree pag
15635 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66 6c e, not an overfl
15636 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20 61 ow.** page, is a
15637 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 pointer to page
15638 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 iFrom. Modify t
15639 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 his pointer so t
1563a 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f hat it points to
1563b 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 .** iTo. Paramet
1563c 65 72 20 65 54 79 70 65 20 64 65 73 63 72 69 62 er eType describ
1563d 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 es the type of p
1563e 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f 64 ointer to be mod
1563f 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66 6f ified, as .** fo
15640 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 llows:.**.** PTR
15641 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 MAP_BTREE: p
15642 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d Page is a btree-
15643 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 page. The pointe
15644 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 r points at a ch
15645 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ild .**
15646 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 6f page o
15647 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 f pPage..**.** P
15648 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a TRMAP_OVERFLOW1:
15649 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 pPage is a btre
1564a 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e e-page. The poin
1564b 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e ter points at an
1564c 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 overflow.**
1564d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1564e 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 age pointed to b
1564f 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c y one of the cel
15650 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a ls on pPage..**.
15651 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
15652 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e OW2: pPage is an
15653 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 overflow-page.
15654 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e The pointer poin
15655 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a ts at the next.*
15656 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
15657 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 overflow pag
15658 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a e in the list..*
15659 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 /.static int mod
1565a 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d ifyPagePointer(M
1565b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 emPage *pPage, P
1565c 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 gno iFrom, Pgno
1565d 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a iTo, u8 eType){.
1565e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1565f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15660 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15661 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15662 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
15663 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
15664 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 age) );. if( eT
15665 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
15666 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 FLOW2 ){. /*
15667 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 The pointer is a
15668 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 lways the first
15669 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 4 bytes of the p
1566a 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 age in this case
1566b 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 . */. if( ge
1566c 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
1566d 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 ata)!=iFrom ){.
1566e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1566f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15670 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 . }. put4b
15671 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 yte(pPage->aData
15672 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b , iTo);. }else{
15673 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 . u8 isInitOr
15674 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e ig = pPage->isIn
15675 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 it;. int i;.
15676 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 int nCell;..
15677 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 sqlite3BtreeI
15678 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
15679 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 nCell = pPag
1567a 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 e->nCell;.. f
1567b 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
1567c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 i++){. u8
1567d 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c *pCell = findCel
1567e 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 l(pPage, i);.
1567f 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 if( eType==PT
15680 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 RMAP_OVERFLOW1 )
15681 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e {. CellIn
15682 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 fo info;.
15683 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
15684 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
15685 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
15686 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
15687 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
15688 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f if( iFro
15689 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 m==get4byte(&pCe
1568a 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
1568b 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 w]) ){.
1568c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65 put4byte(&pCe
1568d 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
1568e 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 w], iTo);.
1568f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
15690 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15691 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
15692 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34 if( get4
15693 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 byte(pCell)==iFr
15694 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 om ){.
15695 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 put4byte(pCell,
15696 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 iTo);.
15697 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
15698 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
15699 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 . if( i==nCe
1569a 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
1569b 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 eType!=PTRMAP_BT
1569c 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 REE || .
1569d 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 get4byte(&pPag
1569e 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
1569f 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 hdrOffset+8])!=i
156a0 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 From ){.
156a1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
156a2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
156a3 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 }. put4by
156a4 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
156a5 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
156a6 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 t+8], iTo);.
156a7 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 }.. pPage->is
156a8 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 Init = isInitOri
156a9 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 g;. }. return
156aa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
156ab 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 *.** Move the op
156ac 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 en database page
156ad 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 pDbPage to loca
156ae 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 tion iFreePage i
156af 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 n the .** databa
156b0 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 se. The pDbPage
156b1 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e reference remain
156b2 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 s valid..*/.stat
156b3 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 ic int relocateP
156b4 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
156b5 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 *pBt,
156b6 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 /* Btree */. Me
156b7 6d 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 mPage *pDbPage,
156b8 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 /* Open p
156b9 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 age to move */.
156ba 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 u8 eType,
156bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
156bc 6e 74 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 nter map 'type'
156bd 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 entry for pDbPag
156be 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 e */. Pgno iPtr
156bf 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
156c0 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 /* Pointer map '
156c1 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 page-no' entry f
156c2 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 or pDbPage */.
156c3 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 Pgno iFreePage,
156c4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
156c5 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 location to move
156c6 20 70 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 pDbPage to */.
156c7 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b int isCommit.){
156c8 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 . MemPage *pPtr
156c9 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 Page; /* The p
156ca 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e age that contain
156cb 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 s a pointer to p
156cc 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
156cd 20 69 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 iDbPage = pDbPa
156ce 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 ge->pgno;. Page
156cf 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d r *pPager = pBt-
156d0 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 >pPager;. int r
156d1 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54 c;.. assert( eT
156d2 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
156d3 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d FLOW2 || eType==
156d4 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
156d5 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65 || . eType
156d6 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c ==PTRMAP_BTREE |
156d7 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
156d8 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73 ROOTPAGE );. as
156d9 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
156da 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
156db 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
156dc 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d ( pDbPage->pBt==
156dd 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 pBt );.. /* Mov
156de 65 20 70 61 67 65 20 69 44 62 50 61 67 65 20 66 e page iDbPage f
156df 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20 rom its current
156e0 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 location to page
156e1 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 61 67 number iFreePag
156e2 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41 e */. TRACE(("A
156e3 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e UTOVACUUM: Movin
156e4 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 61 67 g %d to free pag
156e5 65 20 25 64 20 28 70 74 72 20 70 61 67 65 20 25 e %d (ptr page %
156e6 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a d type %d)\n", .
156e7 20 20 20 20 20 20 69 44 62 50 61 67 65 2c 20 69 iDbPage, i
156e8 46 72 65 65 50 61 67 65 2c 20 69 50 74 72 50 61 FreePage, iPtrPa
156e9 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72 ge, eType));. r
156ea 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
156eb 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65 72 2c Movepage(pPager,
156ec 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67 pDbPage->pDbPag
156ed 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 73 e, iFreePage, is
156ee 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 Commit);. if( r
156ef 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
156f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
156f1 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67 }. pDbPage->pg
156f2 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a no = iFreePage;.
156f3 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65 . /* If pDbPage
156f4 20 77 61 73 20 61 20 62 74 72 65 65 2d 70 61 67 was a btree-pag
156f5 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68 e, then it may h
156f6 61 76 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 ave child pages
156f7 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a and/or cells. *
156f8 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 * that point to
156f9 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
156fa 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
156fb 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 entries for all
156fc 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 these. ** pages
156fd 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e need to be chan
156fe 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ged.. **. ** I
156ff 66 20 70 44 62 50 61 67 65 20 69 73 20 61 6e 20 f pDbPage is an
15700 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 overflow page, t
15701 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 34 20 hen the first 4
15702 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20 bytes may store
15703 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 a. ** pointer t
15704 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f o a subsequent o
15705 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 verflow page. If
15706 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 this is the cas
15707 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 e, then. ** the
15708 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 pointer map nee
15709 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 ds to be updated
1570a 20 66 6f 72 20 74 68 65 20 73 75 62 73 65 71 75 for the subsequ
1570b 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ent overflow pag
1570c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 e.. */. if( eT
1570d 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 ype==PTRMAP_BTRE
1570e 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d E || eType==PTRM
1570f 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
15710 20 20 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64 rc = setChild
15711 50 74 72 6d 61 70 73 28 70 44 62 50 61 67 65 29 Ptrmaps(pDbPage)
15712 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
15713 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15714 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
15715 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 }. }else{. P
15716 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 gno nextOvfl = g
15717 65 74 34 62 79 74 65 28 70 44 62 50 61 67 65 2d et4byte(pDbPage-
15718 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 >aData);. if(
15719 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a nextOvfl!=0 ){.
1571a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
1571b 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 pPut(pBt, nextOv
1571c 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 fl, PTRMAP_OVERF
1571d 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 29 LOW2, iFreePage)
1571e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1571f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15720 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15721 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
15722 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20 }.. /* Fix the
15723 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 database pointer
15724 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 61 67 on page iPtrPag
15725 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61 e that pointed a
15726 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a t iDbPage so. *
15727 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 * that it points
15728 20 61 74 20 69 46 72 65 65 50 61 67 65 2e 20 41 at iFreePage. A
15729 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e lso fix the poin
1572a 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f ter map entry fo
1572b 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e r. ** iPtrPage.
1572c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 . */. if( eTyp
1572d 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 e!=PTRMAP_ROOTPA
1572e 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 GE ){. rc = s
1572f 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
15730 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 ge(pBt, iPtrPage
15731 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b , &pPtrPage, 0);
15732 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
15733 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
15734 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
15735 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
15736 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72 3PagerWrite(pPtr
15737 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
15738 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15739 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1573a 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
1573b 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 age);. retu
1573c 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
1573d 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 rc = modifyPage
1573e 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 Pointer(pPtrPage
1573f 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 , iDbPage, iFree
15740 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 Page, eType);.
15741 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
15742 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 trPage);. if(
15743 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
15744 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 {. rc = ptr
15745 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 mapPut(pBt, iFre
15746 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 ePage, eType, iP
15747 74 72 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 trPage);. }.
15748 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15749 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 }../* Forward de
1574a 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 claration requir
1574b 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d ed by incrVacuum
1574c 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 Step(). */.stati
1574d 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 c int allocateBt
1574e 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64 reePage(BtShared
1574f 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 *, MemPage **,
15750 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 Pgno *, Pgno, u8
15751 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 );../*.** Perfor
15752 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 m a single step
15753 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 of an incrementa
15754 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 l-vacuum. If suc
15755 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 cessful,.** retu
15756 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 rn SQLITE_OK. If
15757 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 there is no wor
15758 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 k to do (and the
15759 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 refore no.** poi
1575a 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 nt in calling th
1575b 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 is function agai
1575c 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 n), return SQLIT
1575d 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f E_DONE..**.** Mo
1575e 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 re specificly, t
1575f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 his function att
15760 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 empts to re-orga
15761 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 nize the .** dat
15762 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 abase so that th
15763 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 e last page of t
15764 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c he file currentl
15765 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e y in use.** is n
15766 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e o longer in use.
15767 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 .**.** If the nF
15768 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 in parameter is
15769 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d non-zero, the im
1576a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 plementation ass
1576b 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 umes.** that the
1576c 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 caller will kee
1576d 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 p calling incrVa
1576e 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c cuumStep() until
1576f 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 .** it returns S
15770 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e QLITE_DONE or an
15771 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 error, and that
15772 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 nFin is the.**
15773 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
15774 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
15775 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 e will contain a
15776 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 fter this .** pr
15777 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 ocess is complet
15778 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
15779 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
1577a 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
1577b 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 gno nFin, Pgno i
1577c 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f 20 LastPg){. Pgno
1577d 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 nFreeList;
1577e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1577f 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e f pages still on
15780 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a the free-list *
15781 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
15782 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15783 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15784 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 67 assert( iLastPg
15785 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20 >nFin );.. if(
15786 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 !PTRMAP_ISPAGE(p
15787 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 Bt, iLastPg) &&
15788 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 iLastPg!=PENDING
15789 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
1578a 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 ){. int rc;.
1578b 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 u8 eType;.
1578c 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a Pgno iPtrPage;.
1578d 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d . nFreeList =
1578e 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
1578f 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
15790 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65 ]);. if( nFre
15791 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 eList==0 ){.
15792 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15793 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 DONE;. }..
15794 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
15795 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 65 pBt, iLastPg, &e
15796 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65 29 Type, &iPtrPage)
15797 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
15798 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15799 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1579a 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 3d }. if( eType=
1579b 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
1579c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1579d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1579e 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 BKPT;. }..
1579f 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
157a0 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 AP_FREEPAGE ){.
157a1 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 if( nFin==0
157a2 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 ){. /* R
157a3 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 66 emove the page f
157a4 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72 rom the files fr
157a5 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73 ee-list. This is
157a6 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20 not required.
157a7 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 6e ** if nFin
157a8 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e is non-zero. In
157a9 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 that case, the
157aa 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 free-list will b
157ab 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 75 e. ** tru
157ac 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 61 ncated to zero a
157ad 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 fter this functi
157ae 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 69 on returns, so i
157af 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 20 t doesn't .
157b0 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 20 ** matter if
157b1 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e it still contain
157b2 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 65 s some garbage e
157b3 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20 ntries..
157b4 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 */. Pgno
157b5 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 iFreePg;.
157b6 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 MemPage *pFreeP
157b7 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 g;. rc =
157b8 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
157b9 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c e(pBt, &pFreePg,
157ba 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 74 &iFreePg, iLast
157bb 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 Pg, 1);.
157bc 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
157bd 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
157be 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
157bf 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 }. asse
157c0 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 rt( iFreePg==iLa
157c1 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20 20 stPg );.
157c2 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
157c3 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ePg);. }.
157c4 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
157c5 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 Pgno iFreePg;
157c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
157c7 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67 65 dex of free page
157c8 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 to move pLastPg
157c9 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d to */. Mem
157ca 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a 0a Page *pLastPg;..
157cb 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
157cc 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
157cd 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c Bt, iLastPg, &pL
157ce 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 astPg, 0);.
157cf 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
157d0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
157d1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
157d2 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46 .. /* If nF
157d3 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 in is zero, this
157d4 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 loop runs exact
157d5 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 ly once and page
157d6 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a pLastPg. *
157d7 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74 * is swapped wit
157d8 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 h the first free
157d9 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 page pulled off
157da 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a the free list..
157db 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
157dc 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
157dd 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20 and, if nFin is
157de 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
157df 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20 o, then keep.
157e0 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e ** looping un
157e1 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20 til a free-page
157e2 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74 located within t
157e3 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61 he first nFin pa
157e4 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ges. ** of
157e5 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e the file is foun
157e6 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
157e7 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d do {. M
157e8 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b emPage *pFreePg;
157e9 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c . rc = al
157ea 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
157eb 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 pBt, &pFreePg, &
157ec 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a iFreePg, 0, 0);.
157ed 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
157ee 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
157ef 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
157f0 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 ge(pLastPg);.
157f1 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
157f2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
157f3 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
157f4 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 pFreePg);.
157f5 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20 }while( nFin!=0
157f6 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 && iFreePg>nFin
157f7 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
157f8 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67 iFreePg<iLastPg
157f9 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 );. .
157fa 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
157fb 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d erWrite(pLastPg-
157fc 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
157fd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
157fe 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
157ff 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
15800 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 pBt, pLastPg, eT
15801 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 ype, iPtrPage, i
15802 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29 FreePg, nFin!=0)
15803 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
15804 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 releasePage(pLas
15805 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 tPg);. if(
15806 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15807 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
15808 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
15809 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69 }. }.. if( nFi
1580a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 n==0 ){. iLas
1580b 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 tPg--;. while
1580c 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 ( iLastPg==PENDI
1580d 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1580e 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 )||PTRMAP_ISPAGE
1580f 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 (pBt, iLastPg) )
15810 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d {. if( PTRM
15811 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 AP_ISPAGE(pBt, i
15812 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20 LastPg) ){.
15813 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b MemPage *pPg;
15814 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 . int rc
15815 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
15816 74 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 tPage(pBt, iLast
15817 50 67 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 Pg, &pPg, 0);.
15818 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
15819 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1581a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1581b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1581c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1581d 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 gerWrite(pPg->pD
1581e 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
1581f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 releasePage(pPg)
15820 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
15821 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
15822 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
15823 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
15824 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73 }. iLas
15825 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 tPg--;. }.
15826 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 sqlite3PagerTru
15827 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e ncateImage(pBt->
15828 70 50 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29 pPager, iLastPg)
15829 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
1582a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1582b 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 ** A write-trans
1582c 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f action must be o
1582d 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c pened before cal
1582e 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 ling this functi
1582f 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 on..** It perfor
15830 6d 73 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 ms a single unit
15831 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 of work towards
15832 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 an incremental
15833 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 vacuum..**.** If
15834 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c the incremental
15835 20 76 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 vacuum is finis
15836 68 65 64 20 61 66 74 65 72 20 74 68 69 73 20 66 hed after this f
15837 75 6e 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c unction has run,
15838 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 .** SQLITE_DONE
15839 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
1583a 69 74 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 it is not finish
1583b 65 64 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 ed, but no error
1583c 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 occurred,.** SQ
1583d 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
1583e 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 ned. Otherwise a
1583f 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
15840 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f ode. .*/.SQLITE_
15841 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15842 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 te3BtreeIncrVacu
15843 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 um(Btree *p){.
15844 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
15845 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
15846 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
15847 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
15848 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
15849 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1584a 52 49 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 RITE && p->inTra
1584b 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
1584c 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 );. if( !pBt->a
1584d 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
1584e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e rc = SQLITE_DON
1584f 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 E;. }else{.
15850 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
15851 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b rflowCache(pBt);
15852 0a 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 . rc = incrVa
15853 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c cuumStep(pBt, 0,
15854 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 pagerPagecount(
15855 70 42 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c pBt));. }. sql
15856 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
15857 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
15858 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
15859 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
1585a 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33 prior to sqlite3
1585b 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e PagerCommit when
1585c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a a transaction.*
1585d 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f * is commited fo
1585e 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d r an auto-vacuum
1585f 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
15860 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 If SQLITE_OK is
15861 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 returned, then
15862 2a 70 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 *pnTrunc is set
15863 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
15864 20 70 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 pages.** the da
15865 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 tabase file shou
15866 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 ld be truncated
15867 74 6f 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f to during the co
15868 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a mmit process. .*
15869 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62 * i.e. the datab
1586a 61 73 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f ase has been reo
1586b 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 rganized so that
1586c 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 only the first
1586d 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 *pnTrunc.** page
1586e 73 20 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f s are in use..*/
1586f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f .static int auto
15870 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 VacuumCommit(BtS
15871 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 hared *pBt){. i
15872 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
15873 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 K;. Pager *pPag
15874 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 er = pBt->pPager
15875 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e ;. VVA_ONLY( in
15876 74 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 t nRef = sqlite3
15877 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 PagerRefcount(pP
15878 61 67 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 ager) );.. asse
15879 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1587a 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
1587b 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 x) );. invalida
1587c 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
1587d 68 65 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 he(pBt);. asser
1587e 74 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 t(pBt->autoVacuu
1587f 6d 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e m);. if( !pBt->
15880 69 6e 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 incrVacuum ){.
15881 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 0a 20 20 20 Pgno nFin;.
15882 20 50 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 Pgno nFree;.
15883 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20 Pgno nPtrmap;.
15884 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 0a 20 Pgno iFree;.
15885 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 const int pgs
15886 7a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a z = pBt->pageSiz
15887 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 e;. Pgno nOri
15888 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 g = pagerPagecou
15889 6e 74 28 70 42 74 29 3b 0a 0a 20 20 20 20 69 66 nt(pBt);.. if
1588a 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 ( PTRMAP_ISPAGE(
1588b 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e pBt, nOrig) || n
1588c 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Orig==PENDING_BY
1588d 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a TE_PAGE(pBt) ){.
1588e 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e /* It is n
1588f 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 ot possible to c
15890 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65 reate a database
15891 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 for which the f
15892 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 inal page.
15893 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70 ** is either a p
15894 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 ointer-map page
15895 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 or the pending-b
15896 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 yte page. If one
15897 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 . ** is enc
15898 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 ountered, this i
15899 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 ndicates corrupt
1589a 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
1589b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1589c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1589d 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 }.. nFree
1589e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
1589f 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
158a0 33 36 5d 29 3b 0a 20 20 20 20 6e 50 74 72 6d 61 36]);. nPtrma
158a1 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 p = (nFree-nOrig
158a2 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 +PTRMAP_PAGENO(p
158a3 42 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f Bt, nOrig)+pgsz/
158a4 35 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 5)/(pgsz/5);.
158a5 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 nFin = nOrig -
158a6 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b nFree - nPtrmap;
158a7 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 . if( nOrig>P
158a8 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
158a9 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 (pBt) && nFin<PE
158aa 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
158ab 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 pBt) ){. nF
158ac 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 in--;. }.
158ad 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 while( PTRMAP_IS
158ae 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 PAGE(pBt, nFin)
158af 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 || nFin==PENDING
158b0 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
158b1 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b ){. nFin--;
158b2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 . }.. for(
158b3 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 iFree=nOrig; iFr
158b4 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 ee>nFin && rc==S
158b5 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d QLITE_OK; iFree-
158b6 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 -){. rc = i
158b7 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 ncrVacuumStep(pB
158b8 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b t, nFin, iFree);
158b9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 . }. if( (
158ba 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
158bb 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b || rc==SQLITE_OK
158bc 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a ) && nFree>0 ){.
158bd 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
158be 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d E_OK;. rc =
158bf 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
158c0 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e te(pBt->pPage1->
158c1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
158c2 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
158c3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
158c4 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 , 0);. put4
158c5 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
158c6 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 1->aData[36], 0)
158c7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 ;. sqlite3P
158c8 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 agerTruncateImag
158c9 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e e(pBt->pPager, n
158ca 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Fin);. }.
158cb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
158cc 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
158cd 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
158ce 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pPager);. }.
158cf 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 }.. assert( nR
158d0 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 ef==sqlite3Pager
158d1 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 Refcount(pPager)
158d2 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
158d3 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 .}..#endif /* if
158d4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
158d5 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a _AUTOVACUUM */..
158d6 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
158d7 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 ne does the firs
158d8 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f t phase of a two
158d9 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 -phase commit.
158da 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 This routine.**
158db 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 causes a rollbac
158dc 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 k journal to be
158dd 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20 64 created (if it d
158de 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 oes not already
158df 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f exist).** and po
158e0 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f pulated with eno
158e1 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ugh information
158e2 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 so that if a pow
158e3 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a er loss occurs.*
158e4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
158e5 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 an be restored t
158e6 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 o its original s
158e7 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 tate by playing
158e8 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 back.** the jour
158e9 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 nal. Then the c
158ea 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a ontents of the j
158eb 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 ournal are flush
158ec 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 ed out to.** the
158ed 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 disk. After th
158ee 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 e journal is saf
158ef 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 ely on oxide, th
158f0 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 e changes to the
158f1 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 .** database are
158f2 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
158f3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
158f4 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f and flushed to o
158f5 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 xide..** At the
158f6 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c end of this call
158f7 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a , the rollback j
158f8 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 ournal still exi
158f9 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 sts on the.** di
158fa 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 sk and we are st
158fb 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 ill holding all
158fc 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 locks, so the tr
158fd 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f ansaction has no
158fe 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 t.** committed.
158ff 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 See sqlite3Btre
15900 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
15901 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 ) for the second
15902 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a phase of the.**
15903 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e commit process.
15904 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c .**.** This call
15905 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e is a no-op if n
15906 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 o write-transact
15907 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ion is currently
15908 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a active on pBt..
15909 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1590a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 sync the databa
1590b 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 se file for the
1590c 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 btree pBt. zMast
1590d 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 er points to.**
1590e 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 the name of a ma
1590f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
15910 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 e that should be
15911 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
15912 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 e.** individual
15913 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 journal file, or
15914 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 is NULL, indica
15915 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a ting no master j
15916 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 ournal file .**
15917 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 (single database
15918 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a transaction)..*
15919 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 *.** When this i
1591a 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 s called, the ma
1591b 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f ster journal sho
1591c 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 uld already have
1591d 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 been.** created
1591e 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 , populated with
1591f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f this journal po
15920 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 inter and synced
15921 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 to disk..**.**
15922 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 Once this is rou
15923 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 tine has returne
15924 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e d, the only thin
15925 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f g required to co
15926 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 mmit.** the writ
15927 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f e-transaction fo
15928 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 r this database
15929 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 file is to delet
1592a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a e the journal..*
1592b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1592c 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1592d 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
1592e 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 Btree *p, const
1592f 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a char *zMaster){.
15930 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
15931 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 E_OK;. if( p->i
15932 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
15933 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 ITE ){. BtSha
15934 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
15935 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 t;. sqlite3Bt
15936 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 reeEnter(p);.#if
15937 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15938 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
15939 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
1593a 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 uum ){. rc
1593b 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d = autoVacuumComm
1593c 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 it(pBt);. i
1593d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1593e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1593f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15940 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
15941 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
15942 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63 }.#endif. rc
15943 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 = sqlite3PagerC
15944 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 ommitPhaseOne(pB
15945 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 t->pPager, zMast
15946 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 er, 0);. sqli
15947 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15948 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
15949 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d c;.}../*.** Comm
1594a 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 it the transacti
1594b 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 on currently in
1594c 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 progress..**.**
1594d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 This routine imp
1594e 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f lements the seco
1594f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d nd phase of a 2-
15950 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 phase commit. T
15951 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 he.** sqlite3Btr
15952 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
15953 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 () routine does
15954 74 68 65 20 66 69 72 73 74 20 70 68 61 73 65 20 the first phase
15955 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 and should.** be
15956 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 invoked prior t
15957 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 o calling this r
15958 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c outine. The sql
15959 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
1595a 68 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 haseOne().** rou
1595b 74 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65 tine did all the
1595c 20 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 work of writing
1595d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 information out
1595e 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 to disk and flu
1595f 73 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e shing the.** con
15960 74 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 tents so that th
15961 65 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f ey are written o
15962 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 nto the disk pla
15963 74 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a tter. All this.
15964 2a 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 ** routine has t
15965 6f 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f o do is delete o
15966 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 r truncate or ze
15967 72 6f 20 74 68 65 20 68 65 61 64 65 72 20 69 6e ro the header in
15968 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c the.** the roll
15969 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 back journal (wh
1596a 69 63 68 20 63 61 75 73 65 73 20 74 68 65 20 74 ich causes the t
1596b 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f ransaction to co
1596c 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f mmit) and.** dro
1596d 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 p locks..**.** T
1596e 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 his will release
1596f 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 the write lock
15970 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
15971 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a file. If there.
15972 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 ** are no active
15973 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 cursors, it als
15974 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 o releases the r
15975 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c ead lock..*/.SQL
15976 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15977 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
15978 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 itPhaseTwo(Btree
15979 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 *p){. BtShared
1597a 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1597b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1597c 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 nter(p);. btree
1597d 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 Integrity(p);..
1597e 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
1597f 65 20 68 61 73 20 61 20 77 72 69 74 65 2d 74 72 e has a write-tr
15980 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 ansaction open,
15981 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65 commit the share
15982 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 d-btrees . ** t
15983 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 ransaction and s
15984 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73 74 et the shared st
15985 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 ate to TRANS_REA
15986 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d D.. */. if( p-
15987 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
15988 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 WRITE ){. int
15989 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28 rc;. assert(
1598a 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1598b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1598c 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1598d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
1598e 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 n>0 );. rc =
1598f 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
15990 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e itPhaseTwo(pBt->
15991 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 pPager);. if(
15992 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15993 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
15994 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
15995 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
15996 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e }. pBt->in
15997 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 Transaction = TR
15998 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 ANS_READ;. }..
15999 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
1599a 65 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f e has any kind o
1599b 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 f transaction op
1599c 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 en, decrement th
1599d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 e transaction.
1599e 2a 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 ** count of the
1599f 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 shared btree. If
159a0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
159a1 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 count reaches 0
159a2 2c 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 , set. ** the s
159a3 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
159a4 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 RANS_NONE. The u
159a5 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
159a6 65 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a ed() call below.
159a7 20 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b ** will unlock
159a8 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f the pager.. */
159a9 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
159aa 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b s!=TRANS_NONE ){
159ab 0a 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 . clearAllSha
159ac 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
159ad 6b 73 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e ks(p);. pBt->
159ae 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a nTransaction--;.
159af 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e if( 0==pBt->
159b0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a nTransaction ){.
159b1 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 pBt->inTra
159b2 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 nsaction = TRANS
159b3 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d _NONE;. }. }
159b4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 .. /* Set the c
159b5 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
159b6 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e on state to TRAN
159b7 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 S_NONE and unloc
159b8 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 k. ** the pager
159b9 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c if this call cl
159ba 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 osed the only re
159bb 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e ad or write tran
159bc 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 saction.. */.
159bd 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e btreeClearHasCon
159be 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e tent(pBt);. p->
159bf 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f inTrans = TRANS_
159c0 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 NONE;. unlockBt
159c1 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
159c2 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 ;.. btreeIntegr
159c3 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ity(p);. sqlite
159c4 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
159c5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
159c6 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 OK;.}../*.** Do
159c7 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 both phases of a
159c8 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 commit..*/.SQLI
159c9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
159ca 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
159cb 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 t(Btree *p){. i
159cc 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 nt rc;. sqlite3
159cd 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
159ce 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
159cf 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
159d0 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 (p, 0);. if( rc
159d1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
159d2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
159d3 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
159d4 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c wo(p);. }. sql
159d5 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
159d6 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
159d7 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
159d8 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 G./*.** Return t
159d9 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69 he number of wri
159da 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 te-cursors open
159db 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 on this handle.
159dc 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a This is for use.
159dd 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 ** in assert() e
159de 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 xpressions, so i
159df 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c t is only compil
159e0 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 ed if NDEBUG is
159e1 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a not.** defined..
159e2 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 **.** For the pu
159e3 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 rposes of this r
159e4 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d outine, a write-
159e5 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 cursor is any cu
159e6 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 rsor that.** is
159e7 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69 capable of writi
159e8 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 73 ng to the databs
159e9 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 e. That means t
159ea 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a he cursor was.**
159eb 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e originally open
159ec 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 ed for writing a
159ed 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 nd the cursor ha
159ee 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65 s not be disable
159ef 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69 d.** by having i
159f0 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 64 ts state changed
159f1 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 to CURSOR_FAULT
159f2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
159f3 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 countWriteCursor
159f4 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 s(BtShared *pBt)
159f5 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 {. BtCursor *pC
159f6 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b ur;. int r = 0;
159f7 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d . for(pCur=pBt-
159f8 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 >pCursor; pCur;
159f9 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 pCur=pCur->pNext
159fa 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
159fb 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d >wrFlag && pCur-
159fc 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
159fd 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 FAULT ) r++; .
159fe 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a }. return r;.}.
159ff 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
15a00 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
15a01 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 the state to CUR
15a02 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 SOR_FAULT and th
15a03 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
15a04 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 to errCode for e
15a05 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42 very cursor on B
15a06 74 53 68 61 72 65 64 20 74 68 61 74 20 70 42 74 tShared that pBt
15a07 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 ree.** reference
15a08 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 s..**.** Every c
15a09 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 ursor is tripped
15a0a 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 , including curs
15a0b 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a ors that belong.
15a0c 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 ** to other data
15a0d 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
15a0e 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 that happen to
15a0f 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68 be sharing.** th
15a10 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42 74 e cache with pBt
15a11 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ree..**.** This
15a12 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c routine gets cal
15a13 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 led when a rollb
15a14 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 ack occurs..** A
15a15 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67 ll cursors using
15a16 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 20 the same cache
15a17 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 64 0a must be tripped.
15a18 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 ** to prevent th
15a19 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 em from trying t
15a1a 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 65 20 o use the btree
15a1b 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c after.** the rol
15a1c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c lback. The roll
15a1d 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 back may have de
15a1e 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20 leted tables.**
15a1f 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61 or moved root pa
15a20 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f ges, so it is no
15a21 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a t sufficient to.
15a22 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 61 74 ** save the stat
15a23 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e e of the cursor.
15a24 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 The cursor mus
15a25 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 t be.** invalida
15a26 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
15a27 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
15a28 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
15a29 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42 ursors(Btree *pB
15a2a 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64 tree, int errCod
15a2b 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a e){. BtCursor *
15a2c 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 p;. sqlite3Btre
15a2d 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a eEnter(pBtree);.
15a2e 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e for(p=pBtree->
15a2f 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
15a30 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
15a31 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c int i;. sql
15a32 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
15a33 72 73 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e rsor(p);. p->
15a34 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
15a35 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b FAULT;. p->sk
15a36 69 70 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 ip = errCode;.
15a37 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d for(i=0; i<=p-
15a38 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
15a39 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
15a3a 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 p->apPage[i]);.
15a3b 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 p->apPage[i
15a3c 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d ] = 0;. }. }
15a3d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
15a3e 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a eave(pBtree);.}.
15a3f 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 ./*.** Rollback
15a40 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
15a41 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c in progress. Al
15a42 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 l cursors will b
15a43 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 e.** invalided b
15a44 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e y this operation
15a45 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
15a46 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a o use a cursor.*
15a47 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 * that was open
15a48 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
15a49 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 of this operati
15a4a 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a on will result.*
15a4b 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a * in an error..*
15a4c 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 *.** This will r
15a4d 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 elease the write
15a4e 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
15a4f 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 abase file. If
15a50 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
15a51 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 active cursors,
15a52 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 it also releases
15a53 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a the read lock..
15a54 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15a55 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
15a56 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
15a57 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
15a58 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15a59 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 = p->pBt;. MemP
15a5a 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 age *pPage1;..
15a5b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
15a5c 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 r(p);. rc = sav
15a5d 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
15a5e 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 0, 0);.#ifndef
15a5f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15a60 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 ED_CACHE. if( r
15a61 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15a62 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 /* This is a
15a63 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 horrible situat
15a64 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 ion. An IO or ma
15a65 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 lloc() error occ
15a66 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 urred whilst.
15a67 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 ** trying to sa
15a68 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 ve cursor positi
15a69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 ons. If this is
15a6a 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c an automatic rol
15a6b 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a lback (as. **
15a6c 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 the result of a
15a6d 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c constraint, mal
15a6e 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 loc() failure or
15a6f 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 IO error) then
15a70 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 . ** the cach
15a71 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 e may be interna
15a72 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 lly inconsistent
15a73 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 (not contain va
15a74 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 lid trees) so.
15a75 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 ** we cannot s
15a76 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 imply return the
15a77 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 error to the ca
15a78 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 ller. Instead, a
15a79 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c bort . ** all
15a7a 20 71 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 queries that ma
15a7b 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f y be using any o
15a7c 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 f the cursors th
15a7d 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 at failed to sav
15a7e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 e.. */. sq
15a7f 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c lite3BtreeTripAl
15a80 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b lCursors(p, rc);
15a81 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 . }.#endif. bt
15a82 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
15a83 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
15a84 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
15a85 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a ){. int rc2;.
15a86 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41 . assert( TRA
15a87 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 NS_WRITE==pBt->i
15a88 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a nTransaction );.
15a89 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 rc2 = sqlite
15a8a 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
15a8b 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
15a8c 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 if( rc2!=SQLITE
15a8d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
15a8e 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 = rc2;. }..
15a8f 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 /* The rollbac
15a90 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 k may have destr
15a91 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d oyed the pPage1-
15a92 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 >aData value. S
15a93 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71 o. ** call sq
15a94 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
15a95 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 e() on page 1 ag
15a96 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 ain to make.
15a97 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e ** sure pPage1->
15a98 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72 aData is set cor
15a99 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 rectly. */. i
15a9a 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 f( sqlite3BtreeG
15a9b 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 etPage(pBt, 1, &
15a9c 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 pPage1, 0)==SQLI
15a9d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15a9e 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
15a9f 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 1);. }. as
15aa0 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 sert( countWrite
15aa1 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 Cursors(pBt)==0
15aa2 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 );. pBt->inTr
15aa3 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
15aa4 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 S_READ;. }.. i
15aa5 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 f( p->inTrans!=T
15aa6 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 RANS_NONE ){.
15aa7 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 clearAllSharedC
15aa8 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 acheTableLocks(p
15aa9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
15aaa 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e Bt->nTransaction
15aab 3e 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e >0 );. pBt->n
15aac 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 Transaction--;.
15aad 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e if( 0==pBt->n
15aae 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
15aaf 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e pBt->inTran
15ab0 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f saction = TRANS_
15ab1 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a NONE;. }. }.
15ab2 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 . btreeClearHas
15ab3 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 Content(pBt);.
15ab4 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 p->inTrans = TRA
15ab5 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 NS_NONE;. unloc
15ab6 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 kBtreeIfUnused(p
15ab7 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 Bt);.. btreeInt
15ab8 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c egrity(p);. sql
15ab9 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
15aba 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
15abb 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 }../*.** Start a
15abc 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 statement subtr
15abd 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 ansaction. The s
15abe 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 ubtransaction ca
15abf 6e 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a n can be rolled.
15ac0 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 ** back independ
15ac1 65 6e 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 ently of the mai
15ac2 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 n transaction. Y
15ac3 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 ou must start a
15ac4 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
15ac5 62 65 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 before starting
15ac6 61 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e a subtransaction
15ac7 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 . The subtransac
15ac8 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 tion is ended au
15ac9 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
15aca 69 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e if the main tran
15acb 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 saction commits
15acc 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a or rolls back..*
15acd 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 *.** Statement s
15ace 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 ubtransactions a
15acf 72 65 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 re used around i
15ad0 6e 64 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 ndividual SQL st
15ad1 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 atements.** that
15ad2 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 are contained w
15ad3 69 74 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e ithin a BEGIN...
15ad4 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 COMMIT block. I
15ad5 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a f a constraint.*
15ad6 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 * error occurs w
15ad7 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d ithin the statem
15ad8 65 6e 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 ent, the effect
15ad9 6f 66 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 of that one stat
15ada 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 ement.** can be
15adb 72 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 rolled back with
15adc 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f out having to ro
15add 6c 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 llback the entir
15ade 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
15adf 2a 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 *.** A statement
15ae0 20 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e sub-transaction
15ae1 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
15ae2 61 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 as an anonymous
15ae3 73 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a savepoint. The.*
15ae4 2a 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 * value passed a
15ae5 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 s the second par
15ae6 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 74 6f ameter is the to
15ae7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 tal number of sa
15ae8 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 vepoints,.** inc
15ae9 6c 75 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 luding the new a
15aea 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 nonymous savepoi
15aeb 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 nt, open on the
15aec 42 2d 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 B-Tree. i.e. if
15aed 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
15aee 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 active savepoint
15aef 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 s and no other s
15af0 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
15af1 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 tions open,.** i
15af2 53 74 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 Statement is 1.
15af3 54 68 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 This anonymous s
15af4 61 76 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 avepoint can be
15af5 72 65 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c released or roll
15af6 65 64 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 ed back.** using
15af7 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 the sqlite3Btre
15af8 65 53 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e eSavepoint() fun
15af9 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
15afa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15afb 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 ite3BtreeBeginSt
15afc 6d 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 mt(Btree *p, int
15afd 20 69 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 iStatement){.
15afe 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
15aff 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
15b00 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15b01 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 Enter(p);. asse
15b02 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
15b03 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
15b04 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 assert( pBt->re
15b05 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 adOnly==0 );. a
15b06 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e ssert( iStatemen
15b07 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 t>0 );. assert(
15b08 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 iStatement>p->d
15b09 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b b->nSavepoint );
15b0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e . if( NEVER(p->
15b0b 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 inTrans!=TRANS_W
15b0c 52 49 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 RITE || pBt->rea
15b0d 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 dOnly) ){. rc
15b0e 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e = SQLITE_INTERN
15b0f 41 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 AL;. }else{.
15b10 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
15b11 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
15b12 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 NS_WRITE );.
15b13 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 72 20 /* At the pager
15b14 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 level, a stateme
15b15 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
15b16 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 s a savepoint wi
15b17 74 68 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 th. ** an ind
15b18 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ex greater than
15b19 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 all savepoints c
15b1a 72 65 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c reated explicitl
15b1b 79 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 y using. ** S
15b1c 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 QL statements. I
15b1d 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
15b1e 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 open, release or
15b1f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 rollback any.
15b20 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f ** such savepo
15b21 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 ints while the s
15b22 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
15b23 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 tion savepoint i
15b24 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f s active.. */
15b25 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
15b26 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
15b27 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c int(pBt->pPager,
15b28 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 iStatement);.
15b29 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
15b2a 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
15b2b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15b2c 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
15b2d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
15b2e 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 tion, op, is alw
15b2f 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f ays SAVEPOINT_RO
15b30 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 LLBACK.** or SAV
15b31 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 EPOINT_RELEASE.
15b32 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 This function ei
15b33 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 ther releases or
15b34 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a rolls back the.
15b35 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 ** savepoint ide
15b36 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ntified by param
15b37 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c eter iSavepoint,
15b38 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
15b39 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f e value .** of o
15b3a 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c p..**.** Normall
15b3b 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 y, iSavepoint is
15b3c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
15b3d 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 equal to zero.
15b3e 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 However, if op i
15b3f 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 s.** SAVEPOINT_R
15b40 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 OLLBACK, then iS
15b41 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 avepoint may als
15b42 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 o be -1. In this
15b43 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f case the .** co
15b44 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e ntents of the en
15b45 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tire transaction
15b46 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b are rolled back
15b47 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 . This is differ
15b48 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f ent.** from a no
15b49 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e rmal transaction
15b4a 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f rollback, as no
15b4b 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 locks are relea
15b4c 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 sed and the.** t
15b4d 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 ransaction remai
15b4e 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 ns open..*/.SQLI
15b4f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
15b50 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 qlite3BtreeSavep
15b51 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 oint(Btree *p, i
15b52 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 nt op, int iSave
15b53 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
15b54 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
15b55 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 if( p && p->inTr
15b56 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
15b57 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
15b58 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15b59 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d assert( op==
15b5a 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
15b5b 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 E || op==SAVEPOI
15b5c 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 NT_ROLLBACK );.
15b5d 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 assert( iSave
15b5e 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 point>=0 || (iSa
15b5f 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f vepoint==-1 && o
15b60 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
15b61 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 LBACK) );. sq
15b62 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
15b63 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c p);. rc = sql
15b64 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 ite3PagerSavepoi
15b65 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
15b66 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b op, iSavepoint);
15b67 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
15b68 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
15b69 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 rc = newDatabase
15b6a 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 (pBt);. }.
15b6b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15b6c 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ve(p);. }. ret
15b6d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15b6e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 Create a new cu
15b6f 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 rsor for the BTr
15b70 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 ee whose root is
15b71 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 on the page.**
15b72 69 54 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74 iTable. The act
15b73 20 6f 66 20 61 63 71 75 69 72 69 6e 67 20 61 20 of acquiring a
15b74 63 75 72 73 6f 72 20 67 65 74 73 20 61 20 72 65 cursor gets a re
15b75 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 ad lock on .** t
15b76 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
15b77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 ..**.** If wrFla
15b78 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 g==0, then the c
15b79 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 ursor can only b
15b7a 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 e used for readi
15b7b 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 ng..** If wrFlag
15b7c 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 ==1, then the cu
15b7d 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 rsor can be used
15b7e 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 for reading or
15b7f 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 for.** writing i
15b80 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f f other conditio
15b81 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 ns for writing a
15b82 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 re also met. Th
15b83 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 ese.** are the c
15b84 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d onditions that m
15b85 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 ust be met in or
15b86 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 der for writing
15b87 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 to.** be allowed
15b88 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 :.**.** 1: The
15b89 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 cursor must have
15b8a 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 been opened wit
15b8b 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a h wrFlag==1.**.*
15b8c 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 * 2: Other data
15b8d 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
15b8e 20 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20 that share the
15b8f 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 same pager cache
15b90 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 .** but whic
15b91 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 h are not in the
15b92 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 READ_UNCOMMITTE
15b93 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 D state may not
15b94 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 have.** curs
15b95 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 ors open with wr
15b96 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 Flag==0 on the s
15b97 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 ame table. Othe
15b98 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 rwise.** the
15b99 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
15b9a 20 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73 this write curs
15b9b 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 or would be visi
15b9c 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 ble to.** th
15b9d 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 e read cursors i
15b9e 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 n the other data
15b9f 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e base connection.
15ba0 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 .**.** 3: The d
15ba1 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 atabase must be
15ba2 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e writable (not on
15ba3 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 read-only media
15ba4 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 ).**.** 4: Ther
15ba5 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 e must be an act
15ba6 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ive transaction.
15ba7 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 .**.** No checki
15ba8 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 ng is done to ma
15ba9 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67 ke sure that pag
15baa 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 e iTable really
15bab 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 is the.** root p
15bac 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e age of a b-tree.
15bad 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 If it is not,
15bae 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
15baf 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c acquired.** will
15bb0 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 not work correc
15bb1 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 tly..**.** It is
15bb2 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
15bb3 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 e sqlite3BtreeCu
15bb4 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 rsorSize() bytes
15bb5 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 of memory .** p
15bb6 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 ointed to by pCu
15bb7 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f r have been zero
15bb8 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
15bb9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15bba 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 btreeCursor(. B
15bbb 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 tree *p,
15bbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15bbd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 /* The btr
15bbe 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 ee */. int iTab
15bbf 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 le,
15bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15bc1 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 * Root page of t
15bc2 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a able to open */.
15bc3 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 int wrFlag,
15bc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15bc5 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f /* 1 to
15bc6 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f write. 0 read-o
15bc7 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 nly */. struct
15bc8 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 KeyInfo *pKeyInf
15bc9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o,
15bca 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 /* First arg to
15bcb 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 comparison funct
15bcc 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ion */. BtCurso
15bcd 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 r *pCur
15bce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15bcf 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 /* Space for new
15bd0 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 cursor */.){.
15bd1 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e int rc;. Pgno n
15bd2 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 Page;. BtShared
15bd3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15bd4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15bd5 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
15bd6 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
15bd7 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 ( wrFlag==0 || w
15bd8 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 20 20 69 66 rFlag==1 );. if
15bd9 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 ( wrFlag ){.
15bda 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 assert( !pBt->re
15bdb 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 69 66 adOnly );. if
15bdc 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 72 65 61 ( NEVER(pBt->rea
15bdd 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 dOnly) ){.
15bde 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 return SQLITE_RE
15bdf 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 ADONLY;. }.
15be0 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 rc = checkForR
15be1 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 eadConflicts(p,
15be2 69 54 61 62 6c 65 2c 20 30 2c 20 30 29 3b 0a 20 iTable, 0, 0);.
15be3 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
15be4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
15be5 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
15be6 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
15be7 43 48 45 20 29 3b 0a 20 20 20 20 20 20 72 65 74 CHE );. ret
15be8 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
15be9 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50 }.. if( pBt->pP
15bea 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 age1==0 ){. r
15beb 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 c = lockBtreeWit
15bec 68 52 65 74 72 79 28 70 29 3b 0a 20 20 20 20 69 hRetry(p);. i
15bed 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15bee 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
15bef 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rc;. }. }.
15bf0 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 pCur->pgnoRoot
15bf1 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a = (Pgno)iTable;.
15bf2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15bf3 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
15bf4 2d 3e 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a ->pPager, (int *
15bf5 29 26 6e 50 61 67 65 29 3b 20 0a 20 20 69 66 28 )&nPage); . if(
15bf6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15bf7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
15bf8 0a 20 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c . }. if( iTabl
15bf9 65 3d 3d 31 20 26 26 20 6e 50 61 67 65 3d 3d 30 e==1 && nPage==0
15bfa 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
15bfb 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67 ITE_EMPTY;. g
15bfc 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f oto create_curso
15bfd 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d r_exception;. }
15bfe 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
15bff 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 itPage(pBt, pCur
15c00 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 ->pgnoRoot, &pCu
15c01 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 r->apPage[0]);.
15c02 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15c03 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63 OK ){. goto c
15c04 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 reate_cursor_exc
15c05 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f eption;. }.. /
15c06 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 * Now that no ot
15c07 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f her errors can o
15c08 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c ccur, finish fil
15c09 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 ling in the BtCu
15c0a 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 rsor. ** variab
15c0b 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75 les, link the cu
15c0c 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 rsor into the Bt
15c0d 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20 Shared list and
15c0e 73 65 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a set *ppCur (the.
15c0f 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75 ** output argu
15c10 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
15c11 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70 ction).. */. p
15c12 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 Cur->pKeyInfo =
15c13 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 pKeyInfo;. pCur
15c14 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 ->pBtree = p;.
15c15 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b pCur->pBt = pBt;
15c16 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 . pCur->wrFlag
15c17 3d 20 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 = (u8)wrFlag;.
15c18 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 pCur->pNext = pB
15c19 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 t->pCursor;. if
15c1a 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b ( pCur->pNext ){
15c1b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 . pCur->pNext
15c1c 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a ->pPrev = pCur;.
15c1d 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 }. pBt->pCurs
15c1e 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 or = pCur;. pCu
15c1f 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
15c20 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 OR_INVALID;. pC
15c21 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 ur->cachedRowid
15c22 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 = 0;.. return S
15c23 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 QLITE_OK;..creat
15c24 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69 e_cursor_excepti
15c25 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 on:. releasePag
15c26 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 e(pCur->apPage[0
15c27 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 ]);. unlockBtre
15c28 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
15c29 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
15c2a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
15c2b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
15c2c 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 rsor(. Btree *p
15c2d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c2f 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
15c30 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
15c31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15c32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c33 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
15c34 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 f table to open
15c35 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
15c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c38 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 /* 1 to write.
15c39 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 0 read-only */.
15c3a 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
15c3b 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 *pKeyInfo,
15c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15c3d 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f First arg to xCo
15c3e 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 mpare() */. BtC
15c3f 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 ursor *pCur
15c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c41 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
15c42 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 e new cursor her
15c43 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15c44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15c45 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d Enter(p);. rc =
15c46 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 btreeCursor(p,
15c47 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 iTable, wrFlag,
15c48 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b pKeyInfo, pCur);
15c49 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
15c4a 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
15c4b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
15c4c 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
15c4d 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a f a BtCursor obj
15c4e 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a ect in bytes..**
15c4f 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
15c50 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f ces is needed so
15c51 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 that users of c
15c52 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c ursors can preal
15c53 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 locate.** suffic
15c54 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 ient storage to
15c55 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 hold a cursor.
15c56 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a The BtCursor obj
15c57 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a ect is opaque.**
15c58 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 to users so the
15c59 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 y cannot do the
15c5a 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c sizeof() themsel
15c5b 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 ves - they must
15c5c 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 call.** this rou
15c5d 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tine..*/.SQLITE_
15c5e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15c5f 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 te3BtreeCursorSi
15c60 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 ze(void){. retu
15c61 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73 rn sizeof(BtCurs
15c62 6f 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 or);.}../*.** Se
15c63 74 20 74 68 65 20 63 61 63 68 65 64 20 72 6f 77 t the cached row
15c64 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 id value of ever
15c65 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68 65 20 y cursor in the
15c66 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 same database fi
15c67 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20 61 6e le.** as pCur an
15c68 64 20 68 61 76 69 6e 67 20 74 68 65 20 73 61 6d d having the sam
15c69 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 e root page numb
15c6a 65 72 20 61 73 20 70 43 75 72 2e 20 20 54 68 65 er as pCur. The
15c6b 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 value is.** set
15c6c 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a to iRowid..**.*
15c6d 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65 20 * Only positive
15c6e 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61 72 65 rowid values are
15c6f 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69 considered vali
15c70 64 20 66 6f 72 20 74 68 69 73 20 63 61 63 68 65 d for this cache
15c71 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 ..** The cache i
15c72 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f s initialized to
15c73 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e zero, indicatin
15c74 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63 61 63 g an invalid cac
15c75 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20 77 he..** A btree w
15c76 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77 69 ill work fine wi
15c77 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 th zero or negat
15c78 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57 65 20 ive rowids. We
15c79 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63 just cannot.** c
15c7a 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 ache zero or neg
15c7b 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20 77 68 ative rowids, wh
15c7c 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 ich means tables
15c7d 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f 20 6f that use zero o
15c7e 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 6f r.** negative ro
15c7f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e 20 61 wids might run a
15c80 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 little slower.
15c81 20 42 75 74 20 69 6e 20 70 72 61 63 74 69 63 65 But in practice
15c82 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67 , zero.** or neg
15c83 61 74 69 76 65 20 72 6f 77 69 64 73 20 61 72 65 ative rowids are
15c84 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 very uncommon s
15c85 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f o this should no
15c86 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a t be a problem..
15c87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15c88 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
15c89 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 reeSetCachedRowi
15c8a 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
15c8b 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
15c8c 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43 75 72 iRowid){. BtCur
15c8d 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d sor *p;. for(p=
15c8e 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73 pCur->pBt->pCurs
15c8f 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
15c90 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 t){. if( p->p
15c91 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70 gnoRoot==pCur->p
15c92 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63 gnoRoot ) p->cac
15c93 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f 77 69 hedRowid = iRowi
15c94 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 d;. }. assert(
15c95 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 pCur->cachedRow
15c96 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a id==iRowid );.}.
15c97 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
15c98 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20 66 e cached rowid f
15c99 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 or the given cur
15c9a 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69 76 65 sor. A negative
15c9b 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75 or zero.** retu
15c9c 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 rn value indicat
15c9d 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 es that the rowi
15c9e 64 20 63 61 63 68 65 20 69 73 20 69 6e 76 61 6c d cache is inval
15c9f 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 id and should be
15ca0 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 .** ignored. If
15ca1 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 the rowid cache
15ca2 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 has never befor
15ca3 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e e been set, then
15ca4 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 a.** zero is re
15ca5 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
15ca6 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
15ca7 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 3_int64 sqlite3B
15ca8 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 treeGetCachedRow
15ca9 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 id(BtCursor *pCu
15caa 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 r){. return pCu
15cab 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3b 0a r->cachedRowid;.
15cac 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
15cad 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 72 65 cursor. The re
15cae 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ad lock on the d
15caf 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
15cb0 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e released.** when
15cb1 20 74 68 65 20 6c 61 73 74 20 63 75 72 73 6f 72 the last cursor
15cb2 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 is closed..*/.S
15cb3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
15cb4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
15cb5 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 oseCursor(BtCurs
15cb6 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 or *pCur){. Btr
15cb7 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70 43 75 ee *pBtree = pCu
15cb8 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 28 r->pBtree;. if(
15cb9 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20 69 pBtree ){. i
15cba 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68 61 72 nt i;. BtShar
15cbb 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e ed *pBt = pCur->
15cbc 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pBt;. sqlite3
15cbd 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 BtreeEnter(pBtre
15cbe 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 e);. sqlite3B
15cbf 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
15cc0 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 pCur);. if( p
15cc1 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 Cur->pPrev ){.
15cc2 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d pCur->pPrev-
15cc3 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 >pNext = pCur->p
15cc4 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Next;. }else{
15cc5 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 . pBt->pCur
15cc6 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 sor = pCur->pNex
15cc7 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
15cc8 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a pCur->pNext ){.
15cc9 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 pCur->pNex
15cca 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d t->pPrev = pCur-
15ccb 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 >pPrev;. }.
15ccc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 for(i=0; i<=pC
15ccd 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b ur->iPage; i++){
15cce 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
15ccf 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ge(pCur->apPage[
15cd0 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 i]);. }. u
15cd1 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
15cd2 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 ed(pBt);. inv
15cd3 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
15cd4 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20 ache(pCur);.
15cd5 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 /* sqlite3_free(
15cd6 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 pCur); */. sq
15cd7 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15cd8 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 pBtree);. }. r
15cd9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15cda 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 .}../*.** Make a
15cdb 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f temporary curso
15cdc 72 20 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 r by filling in
15cdd 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 the fields of pT
15cde 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 empCur..** The t
15cdf 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 emporary cursor
15ce0 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 is not on the cu
15ce1 72 73 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 rsor list for th
15ce2 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 e Btree..*/.SQLI
15ce3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
15ce4 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 sqlite3BtreeGetT
15ce5 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 empCursor(BtCurs
15ce6 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 or *pCur, BtCurs
15ce7 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 or *pTempCur){.
15ce8 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
15ce9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15cea 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 ex(pCur) );. me
15ceb 6d 63 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 mcpy(pTempCur, p
15cec 43 75 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75 Cur, sizeof(BtCu
15ced 72 73 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 rsor));. pTempC
15cee 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 ur->pNext = 0;.
15cef 20 70 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 pTempCur->pPrev
15cf0 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = 0;. for(i=0;
15cf1 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50 i<=pTempCur->iP
15cf2 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 age; i++){. s
15cf3 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 qlite3PagerRef(p
15cf4 54 65 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b TempCur->apPage[
15cf5 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 i]->pDbPage);.
15cf6 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 65 6d }. assert( pTem
15cf7 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30 20 29 3b pCur->pKey==0 );
15cf8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
15cf9 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 a temporary cur
15cfa 73 6f 72 20 73 75 63 68 20 61 73 20 77 61 73 20 sor such as was
15cfb 6d 61 64 65 20 62 79 20 74 68 65 20 43 72 65 61 made by the Crea
15cfc 74 65 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f teTemporaryCurso
15cfd 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 r().** function
15cfe 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 above..*/.SQLITE
15cff 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
15d00 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 lite3BtreeReleas
15d01 65 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 eTempCursor(BtCu
15d02 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 rsor *pCur){. i
15d03 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
15d04 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15d05 28 70 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28 (pCur) );. for(
15d06 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 i=0; i<=pCur->iP
15d07 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 age; i++){. s
15d08 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
15d09 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
15d0a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a ->pDbPage);. }.
15d0b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
15d0c 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a 0a 0a Cur->pKey);.}...
15d0d 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
15d0e 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 the BtCursor* g
15d0f 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 iven in the argu
15d10 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 ment has a valid
15d11 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
15d12 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 o structure. If
15d13 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
15d14 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a dy valid, call.*
15d15 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 * sqlite3BtreePa
15d16 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c rseCell() to fil
15d17 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 l it in..**.** B
15d18 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 tCursor.info is
15d19 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 a cache of the i
15d1a 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 nformation in th
15d1b 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a e current cell..
15d1c 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 ** Using this ca
15d1d 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65 20 che reduces the
15d1e 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 number of calls
15d1f 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 to sqlite3BtreeP
15d20 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a arseCell()..**.*
15d21 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 * 2007-06-25: T
15d22 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e here is a bug in
15d23 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f some versions o
15d24 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 f MSVC that caus
15d25 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 e the.** compile
15d26 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 r to crash when
15d27 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 getCellInfo() is
15d28 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
15d29 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 a macro..** But
15d2a 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 there is a measu
15d2b 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 reable speed adv
15d2c 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 antage to using
15d2d 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 the macro on gcc
15d2e 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 .** (when less c
15d2f 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 ompiler optimiza
15d30 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f tions like -Os o
15d31 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 r -O0 are used a
15d32 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c nd the.** compil
15d33 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 er is not doing
15d34 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 agressive inlini
15d35 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 ng.) So we use
15d36 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a a real function.
15d37 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 ** for MSVC and
15d38 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 a macro for ever
15d39 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 ything else. Ti
15d3a 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 cket #2457..*/.#
15d3b 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
15d3c 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 static void asse
15d3d 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 rtCellInfo(BtCur
15d3e 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 sor *pCur){.
15d3f 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
15d40 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
15d41 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 Cur->iPage;.
15d42 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c memset(&info, 0,
15d43 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a sizeof(info));.
15d44 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15d45 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
15d46 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 apPage[iPage], p
15d47 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 Cur->aiIdx[iPage
15d48 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 ], &info);. a
15d49 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 ssert( memcmp(&i
15d4a 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f nfo, &pCur->info
15d4b 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d , sizeof(info))=
15d4c 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a =0 );. }.#else.
15d4d 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 #define assert
15d4e 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 CellInfo(x).#end
15d4f 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 if.#ifdef _MSC_V
15d50 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 ER. /* Use a re
15d51 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d al function in M
15d52 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 SVC to work arou
15d53 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 nd bugs in that
15d54 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 compiler. */. s
15d55 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 tatic void getCe
15d56 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 llInfo(BtCursor
15d57 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 *pCur){. if(
15d58 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
15d59 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ==0 ){. int
15d5a 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
15d5b 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 Page;. sqli
15d5c 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
15d5d 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
15d5e 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 Page],pCur->aiId
15d5f 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e x[iPage],&pCur->
15d60 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 info);. pCu
15d61 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
15d62 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
15d63 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 assertCellInf
15d64 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 o(pCur);. }.
15d65 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e }.#else /* if n
15d66 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 ot _MSC_VER */.
15d67 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 /* Use a macro
15d68 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d in all other com
15d69 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 pilers so that t
15d6a 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 he function is i
15d6b 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e nlined */.#defin
15d6c 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 e getCellInfo(pC
15d6d 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 ur)
15d6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d70 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
15d71 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
15d72 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 e==0 ){
15d73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d75 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
15d76 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
15d77 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 ->iPage;
15d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d7a 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
15d7b 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
15d7c 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 seCell(pCur->apP
15d7d 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d age[iPage],pCur-
15d7e 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 >aiIdx[iPage],&p
15d7f 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 Cur->info); \.
15d80 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
15d81 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 y = 1;
15d82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d84 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
15d85 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 }else{
15d86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
15d8a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e assertCellIn
15d8b 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 fo(pCur);
15d8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
15d8f 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f . }.#endif /* _
15d90 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a MSC_VER */../*.*
15d91 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 * Set *pSize to
15d92 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
15d93 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f buffer needed to
15d94 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 hold the value
15d95 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f of.** the key fo
15d96 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e r the current en
15d97 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 try. If the cur
15d98 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 sor is not point
15d99 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 ing.** to a vali
15d9a 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 d entry, *pSize
15d9b 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a is set to 0. .**
15d9c 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 .** For a table
15d9d 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 with the INTKEY
15d9e 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 flag set, this r
15d9f 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
15da0 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 he key.** itself
15da1 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 , not the number
15da2 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
15da3 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f key..*/.SQLITE_
15da4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15da5 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
15da6 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
15da7 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 i64 *pSize){. i
15da8 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
15da9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15daa 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
15dab 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
15dac 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
15dad 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
15dae 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
15daf 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
15db0 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
15db1 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
15db2 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
15db3 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 . if( pCur->e
15db4 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
15db5 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a VALID ){. *
15db6 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d pSize = 0;. }
15db7 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43 else{. getC
15db8 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
15db9 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 *pSize = pC
15dba 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 ur->info.nKey;.
15dbb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
15dbc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
15dbd 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 et *pSize to the
15dbe 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
15dbf 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 of data in the
15dc0 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 entry the.** cur
15dc1 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f sor currently po
15dc2 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73 ints to. Always
15dc3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15dc4 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 K..** Failure is
15dc5 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 not possible.
15dc6 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
15dc7 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a not currently.*
15dc8 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e * pointing to an
15dc9 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 61 entry (which ca
15dca 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 n happen, for ex
15dcb 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65 ample, if.** the
15dcc 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 database is emp
15dcd 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20 ty) then *pSize
15dce 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a is set to 0..*/.
15dcf 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15dd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
15dd1 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 ataSize(BtCursor
15dd2 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 *pCur, u32 *pSi
15dd3 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ze){. int rc;..
15dd4 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
15dd5 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
15dd6 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
15dd7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
15dd8 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
15dd9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15dda 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
15ddb 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15ddc 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d INVALID || pCur-
15ddd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15dde 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28 VALID );. if(
15ddf 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15de0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
15de1 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f . /* Not po
15de2 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 inting at a vali
15de3 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70 d entry - set *p
15de4 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20 Size to 0. */.
15de5 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a *pSize = 0;.
15de6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15de7 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 getCellInfo(pCu
15de8 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 r);. *pSize
15de9 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 = pCur->info.nD
15dea 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ata;. }. }.
15deb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15dec 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 *.** Given the p
15ded 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e age number of an
15dee 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
15def 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 n the database (
15df0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 parameter.** ovf
15df1 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f l), this functio
15df2 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 n finds the page
15df3 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
15df4 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ext page in the
15df5 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 .** linked list
15df6 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 of overflow page
15df7 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 s. If possible,
15df8 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f it uses the auto
15df9 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 -vacuum.** point
15dfa 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 er-map data inst
15dfb 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 ead of reading t
15dfc 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 he content of pa
15dfd 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f ge ovfl to do so
15dfe 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 . .**.** If an e
15dff 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 rror occurs an S
15e00 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
15e01 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
15e02 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 herwise:.**.** T
15e03 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
15e04 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66 f the next overf
15e05 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
15e06 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a linked list is .
15e07 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 ** written to *p
15e08 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 PgnoNext. If pag
15e09 65 20 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61 e ovfl is the la
15e0a 73 74 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c st page in its l
15e0b 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 inked .** list,
15e0c 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 *pPgnoNext is se
15e0d 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a t to zero. .**.*
15e0e 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20 6e * If ppPage is n
15e0f 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 ot NULL, and a r
15e10 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
15e11 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63 MemPage object c
15e12 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
15e13 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 to page number p
15e14 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 Ovfl was obtaine
15e15 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20 d, then *ppPage
15e16 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 is set to point
15e17 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 to that.** refer
15e18 65 6e 63 65 2e 20 49 74 20 69 73 20 74 68 65 20 ence. It is the
15e19 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f responsibility o
15e1a 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 f the caller to
15e1b 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 call releasePage
15e1c 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 ().** on *ppPage
15e1d 20 74 6f 20 66 72 65 65 20 74 68 65 20 72 65 66 to free the ref
15e1e 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 erence. In no re
15e1f 66 65 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61 ference was obta
15e20 69 6e 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a ined (because.**
15e21 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
15e22 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74 was used to obt
15e23 61 69 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f ain the value fo
15e24 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 r *pPgnoNext), t
15e25 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 hen.** *ppPage i
15e26 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a s set to zero..*
15e27 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
15e28 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 OverflowPage(.
15e29 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a BtShared *pBt, .
15e2a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 Pgno ovfl,
15e2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15e2c 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 * Overflow page
15e2d 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
15e2e 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
15e2f 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 /* OUT: MemPag
15e30 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 e handle (may be
15e31 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f NULL) */. Pgno
15e32 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 *pPgnoNext
15e33 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
15e34 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 Next overflow p
15e35 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b age number */.){
15e36 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 . Pgno next = 0
15e37 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
15e38 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 ge = 0;. int rc
15e39 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
15e3a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15e3b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
15e3c 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
15e3d 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a ert(pPgnoNext);.
15e3e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15e3f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
15e40 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 /* Try to find
15e41 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
15e42 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c n the overflow l
15e43 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 ist using the.
15e44 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f ** autovacuum po
15e45 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e inter-map pages.
15e46 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 Guess that the
15e47 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 next page in .
15e48 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 ** the overflow
15e49 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d list is page num
15e4a 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 ber (ovfl+1). If
15e4b 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e that guess turn
15e4c 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 s . ** out to b
15e4d 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 e wrong, fall ba
15e4e 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 ck to loading th
15e4f 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a e data of page .
15e50 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c ** number ovfl
15e51 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
15e52 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 e next page numb
15e53 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 er.. */. if( p
15e54 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
15e55 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b {. Pgno pgno;
15e56 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 . Pgno iGuess
15e57 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 = ovfl+1;. u
15e58 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 8 eType;.. wh
15e59 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
15e5a 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 GE(pBt, iGuess)
15e5b 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 || iGuess==PENDI
15e5c 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
15e5d 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 ) ){. iGues
15e5e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 s++;. }..
15e5f 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 if( iGuess<=page
15e60 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
15e61 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 ){. rc = pt
15e62 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 rmapGet(pBt, iGu
15e63 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 ess, &eType, &pg
15e64 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 no);. if( r
15e65 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
15e66 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
15e67 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d ERFLOW2 && pgno=
15e68 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 =ovfl ){.
15e69 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a next = iGuess;.
15e6a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
15e6b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 ITE_DONE;.
15e6c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
15e6d 69 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 if.. if( rc==SQ
15e6e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
15e6f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
15e70 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 GetPage(pBt, ovf
15e71 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 l, &pPage, 0);.
15e72 20 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 assert(rc==SQ
15e73 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 LITE_OK || pPage
15e74 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 ==0);. if( ne
15e75 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c xt==0 && rc==SQL
15e76 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
15e77 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65 28 next = get4byte(
15e78 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 pPage->aData);.
15e79 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 }. }.. *pPg
15e7a 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 noNext = next;.
15e7b 20 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 if( ppPage ){.
15e7c 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 *ppPage = pPa
15e7d 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ge;. }else{.
15e7e 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
15e7f 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ge);. }. retur
15e80 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f n (rc==SQLITE_DO
15e81 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a NE ? SQLITE_OK :
15e82 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rc);.}../*.** C
15e83 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20 opy data from a
15e84 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 65 buffer to a page
15e85 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65 , or from a page
15e86 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a to a buffer..**
15e87 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20 .** pPayload is
15e88 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 a pointer to dat
15e89 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 a stored on data
15e8a 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 base page pDbPag
15e8b 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e e..** If argumen
15e8c 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 t eOp is false,
15e8d 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73 then nByte bytes
15e8e 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 of data are cop
15e8f 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 ied.** from pPay
15e90 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 66 load to the buff
15e91 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79 er pointed at by
15e92 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73 pBuf. If eOp is
15e93 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 true,.** then s
15e94 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
15e95 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 () is called on
15e96 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74 pDbPage and nByt
15e97 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 e bytes.** of da
15e98 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 ta are copied fr
15e99 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 om the buffer pB
15e9a 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a uf to pPayload..
15e9b 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
15e9c 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
15e9d 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 uccess, otherwis
15e9e 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e e an error code.
15e9f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
15ea0 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f opyPayload(. vo
15ea1 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 id *pPayload,
15ea2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
15ea3 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20 er to page data
15ea4 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c */. void *pBuf,
15ea5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15ea6 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 * Pointer to buf
15ea7 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 fer */. int nBy
15ea8 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
15ea9 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
15eaa 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f bytes to copy */
15eab 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 . int eOp,
15eac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15ead 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 0 -> copy from p
15eae 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 age, 1 -> copy t
15eaf 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 o page */. DbPa
15eb0 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 20 ge *pDbPage
15eb1 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
15eb2 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 ntaining pPayloa
15eb3 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f d */.){. if( eO
15eb4 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 p ){. /* Copy
15eb5 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 data from buffe
15eb6 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 69 r to page (a wri
15eb7 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f te operation) */
15eb8 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
15eb9 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
15eba 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
15ebb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15ebc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
15ebd 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 rc;. }. me
15ebe 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 mcpy(pPayload, p
15ebf 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d Buf, nByte);. }
15ec0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 else{. /* Cop
15ec1 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 y data from page
15ec2 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 to buffer (a re
15ec3 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f ad operation) */
15ec4 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 . memcpy(pBuf
15ec5 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 , pPayload, nByt
15ec6 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
15ec7 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
15ec8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15ec9 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 on is used to re
15eca 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20 ad or overwrite
15ecb 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 payload informat
15ecc 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 ion.** for the e
15ecd 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 43 ntry that the pC
15ece 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 ur cursor is poi
15ecf 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 nting to. If the
15ed0 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 eOp.** paramete
15ed1 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 20 r is 0, this is
15ed2 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e a read operation
15ed3 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e (data copied in
15ed4 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 to.** buffer pBu
15ed5 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e f). If it is non
15ed6 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28 -zero, a write (
15ed7 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d data copied from
15ed8 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 .** buffer pBuf)
15ed9 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 ..**.** A total
15eda 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 of "amt" bytes a
15edb 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 re read or writt
15edc 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 en beginning at
15edd 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 "offset"..** Dat
15ede 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 a is read to or
15edf 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
15ee0 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 pBuf..**.** This
15ee1 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
15ee2 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 t make a distinc
15ee3 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79 tion between key
15ee4 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74 and data..** It
15ee5 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20 77 just reads or w
15ee6 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d rites bytes from
15ee7 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 the payload are
15ee8 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a a. Data might .
15ee9 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 ** appear on the
15eea 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65 main page or be
15eeb 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f scattered out o
15eec 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 n multiple overf
15eed 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a low .** pages..*
15eee 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75 *.** If the BtCu
15eef 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 rsor.isIncrblobH
15ef0 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 andle flag is se
15ef1 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 t, and the curre
15ef2 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 nt.** cursor ent
15ef3 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d ry uses one or m
15ef4 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ore overflow pag
15ef5 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f es, this functio
15ef6 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 n.** allocates s
15ef7 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a pace for and laz
15ef8 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68 ily popluates th
15ef9 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
15efa 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 list .** cache a
15efb 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61 rray (BtCursor.a
15efc 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 Overflow). Subse
15efd 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 quent calls use
15efe 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f this.** cache to
15eff 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f make seeking to
15f00 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 the supplied of
15f01 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69 fset more effici
15f02 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 ent..**.** Once
15f03 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
15f04 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20 -list cache has
15f05 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 been allocated,
15f06 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 it may be.** inv
15f07 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65 alidated if some
15f08 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 72 other cursor wr
15f09 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65 ites to the same
15f0a 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a table, or if.**
15f0b 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d the cursor is m
15f0c 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 oved to a differ
15f0d 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f ent row. Additio
15f0e 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 nally, in auto-v
15f0f 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 acuum.** mode, t
15f10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 he following eve
15f11 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 nts may invalida
15f12 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 te an overflow p
15f13 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a age-list cache..
15f14 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 **.** * An inc
15f15 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c remental vacuum,
15f16 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 .** * A commit
15f17 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d in auto_vacuum=
15f18 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 "full" mode,.**
15f19 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74 * Creating a t
15f1a 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72 able (may requir
15f1b 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 e moving an over
15f1c 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 flow page)..*/.s
15f1d 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 73 tatic int access
15f1e 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 Payload(. BtCur
15f1f 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
15f20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 /* Cursor pointi
15f21 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 ng to entry to r
15f22 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 ead from */. u3
15f23 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 2 offset,
15f24 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 /* Begin read
15f25 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e 74 ing this far int
15f26 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 o payload */. u
15f27 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 32 amt,
15f28 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73 /* Read this
15f29 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 many bytes */.
15f2a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
15f2b 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 pBuf, /* Write t
15f2c 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68 he bytes into th
15f2d 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 is buffer */ .
15f2e 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20 int skipKey,
15f2f 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62 /* offset b
15f30 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69 66 egins at data if
15f31 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f this is true */
15f32 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 . int eOp
15f33 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 /* zero
15f34 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 to read. non-zer
15f35 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 o to write. */.)
15f36 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 {. unsigned cha
15f37 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 r *aPayload;. i
15f38 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
15f39 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 K;. u32 nKey;.
15f3a 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 int iIdx = 0;.
15f3b 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
15f3c 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
15f3d 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 Cur->iPage]; /*
15f3e 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 Btree page of cu
15f3f 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 rrent entry */.
15f40 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15f41 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 pCur->pBt;
15f42 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15f43 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f Btree this curso
15f44 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a r belongs to */.
15f45 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
15f46 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
15f47 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
15f48 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
15f49 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
15f4a 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
15f4b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
15f4c 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
15f4d 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
15f4e 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 );.. getCellInf
15f4f 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c o(pCur);. aPayl
15f50 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f oad = pCur->info
15f51 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 .pCell + pCur->i
15f52 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e nfo.nHeader;. n
15f53 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e Key = (pPage->in
15f54 74 4b 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 tKey ? 0 : (int)
15f55 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 pCur->info.nKey)
15f56 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 ;.. if( skipKey
15f57 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b ){. offset +
15f58 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 = nKey;. }. if
15f59 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e ( offset+amt > n
15f5a 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e Key+pCur->info.n
15f5b 44 61 74 61 20 0a 20 20 20 7c 7c 20 26 61 50 61 Data . || &aPa
15f5c 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f yload[pCur->info
15f5d 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 .nLocal] > &pPag
15f5e 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 e->aData[pBt->us
15f5f 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 ableSize]. ){.
15f60 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 /* Trying to
15f61 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 70 61 read or write pa
15f62 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 st the end of th
15f63 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 72 72 e data is an err
15f64 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e or */. return
15f65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15f66 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 BKPT;. }.. /*
15f67 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 Check if data mu
15f68 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 st be read/writt
15f69 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 en to/from the b
15f6a 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 tree page itself
15f6b 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 . */. if( offse
15f6c 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f t<pCur->info.nLo
15f6d 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 cal ){. int a
15f6e 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 = amt;. if(
15f6f 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 a+offset>pCur->i
15f70 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 nfo.nLocal ){.
15f71 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e a = pCur->in
15f72 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 fo.nLocal - offs
15f73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 et;. }. rc
15f74 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 = copyPayload(&
15f75 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d aPayload[offset]
15f76 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 , pBuf, a, eOp,
15f77 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
15f78 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b . offset = 0;
15f79 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a . pBuf += a;.
15f7a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 amt -= a;.
15f7b 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 }else{. offse
15f7c 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e t -= pCur->info.
15f7d 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 nLocal;. }.. i
15f7e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15f7f 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 && amt>0 ){.
15f80 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 const u32 ovflS
15f81 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ize = pBt->usabl
15f82 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 eSize - 4; /* B
15f83 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 ytes content per
15f84 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 ovfl page */.
15f85 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b Pgno nextPage;
15f86 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d .. nextPage =
15f87 20 67 65 74 34 62 79 74 65 28 26 61 50 61 79 6c get4byte(&aPayl
15f88 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e oad[pCur->info.n
15f89 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 Local]);..#ifnde
15f8a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
15f8b 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 CRBLOB. /* If
15f8c 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 the isIncrblobH
15f8d 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 andle flag is se
15f8e 74 20 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 t and the BtCurs
15f8f 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 or.aOverflow[].
15f90 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 ** has not be
15f91 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c en allocated, al
15f92 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 locate it now. T
15f93 68 65 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 he array is size
15f94 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 d at. ** one
15f95 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f entry for each o
15f96 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 verflow page in
15f97 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 the overflow cha
15f98 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 in. The. ** p
15f99 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
15f9a 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 e first overflow
15f9b 20 70 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 page is stored
15f9c 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c in aOverflow[0],
15f9d 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 . ** etc. A v
15f9e 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 alue of 0 in the
15f9f 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 aOverflow[] arr
15fa0 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 ay means "not ye
15fa1 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 t known". **
15fa2 28 74 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 (the cache is la
15fa3 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e zily populated).
15fa4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
15fa5 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 pCur->isIncrblob
15fa6 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d Handle && !pCur-
15fa7 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 >aOverflow ){.
15fa8 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 int nOvfl =
15fa9 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 (pCur->info.nPay
15faa 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e load-pCur->info.
15fab 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d nLocal+ovflSize-
15fac 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 1)/ovflSize;.
15fad 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c pCur->aOverfl
15fae 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c ow = (Pgno *)sql
15faf 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
15fb0 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 izeof(Pgno)*nOvf
15fb1 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f l);. if( nO
15fb2 76 66 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f vfl && !pCur->aO
15fb3 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
15fb4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
15fb5 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OMEM;. }.
15fb6 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
15fb7 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
15fb8 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20 -list cache has
15fb9 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 been allocated a
15fba 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e nd the. ** en
15fbb 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 try for the firs
15fbc 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 t required overf
15fbd 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 low page is vali
15fbe 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 d, skip. ** d
15fbf 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 irectly to it..
15fc0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 */. if( pC
15fc1 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 ur->aOverflow &&
15fc2 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
15fc3 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 [offset/ovflSize
15fc4 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 ] ){. iIdx
15fc5 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 = (offset/ovflSi
15fc6 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 ze);. nextP
15fc7 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 age = pCur->aOve
15fc8 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 rflow[iIdx];.
15fc9 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 offset = (off
15fca 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 set%ovflSize);.
15fcb 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
15fcc 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 for( ; rc==SQLI
15fcd 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 TE_OK && amt>0 &
15fce 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 & nextPage; iIdx
15fcf 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 ++){..#ifndef SQ
15fd0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
15fd1 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 OB. /* If r
15fd2 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 equired, populat
15fd3 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 e the overflow p
15fd4 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 age-list cache.
15fd5 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 */. if( pCu
15fd6 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a r->aOverflow ){.
15fd7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 21 assert(!
15fd8 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
15fd9 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 iIdx] || pCur->a
15fda 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d Overflow[iIdx]==
15fdb 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 nextPage);.
15fdc 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c pCur->aOverfl
15fdd 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 ow[iIdx] = nextP
15fde 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e age;. }.#en
15fdf 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f dif.. if( o
15fe0 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 ffset>=ovflSize
15fe1 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
15fe2 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f e only reason to
15fe3 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 read this page
15fe4 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 is to obtain the
15fe5 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
15fe6 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
15fe7 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
15fe8 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e overflow chain.
15fe9 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 The page.
15fea 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 ** data is not
15feb 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 required. So fi
15fec 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 rst try to looku
15fed 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 p the overflow.
15fee 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c ** page-l
15fef 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e ist cache, if an
15ff0 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 y, then fall bac
15ff1 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 k to the getOver
15ff2 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 flowPage().
15ff3 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a ** function..
15ff4 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 */.#ifnd
15ff5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
15ff6 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 NCRBLOB.
15ff7 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 if( pCur->aOverf
15ff8 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 low && pCur->aOv
15ff9 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 erflow[iIdx+1] )
15ffa 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 {. next
15ffb 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 Page = pCur->aOv
15ffc 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a erflow[iIdx+1];.
15ffd 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a } else .
15ffe 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 #endif.
15fff 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f rc = getOverflo
16000 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 wPage(pBt, nextP
16001 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 age, 0, &nextPag
16002 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 e);. offs
16003 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a et -= ovflSize;.
16004 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
16005 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 /* Need to
16006 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70 read this page p
16007 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 roperly. It cont
16008 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 ains some of the
16009 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 . ** rang
1600a 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 e of data that i
1600b 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f s being read (eO
1600c 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e p==0) or written
1600d 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 (eOp!=0)..
1600e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 */. Db
1600f 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 Page *pDbPage;.
16010 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 int a = a
16011 6d 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d mt;. rc =
16012 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
16013 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 (pBt->pPager, ne
16014 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 xtPage, &pDbPage
16015 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
16016 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16017 20 20 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f aPaylo
16018 61 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ad = sqlite3Page
16019 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
1601a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 );. nex
1601b 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 tPage = get4byte
1601c 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 (aPayload);.
1601d 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 if( a + of
1601e 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 fset > ovflSize
1601f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 ){. a
16020 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 = ovflSize - of
16021 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 fset;.
16022 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
16023 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 copyPayload(&aP
16024 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d ayload[offset+4]
16025 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 , pBuf, a, eOp,
16026 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
16027 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
16028 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a Unref(pDbPage);.
16029 20 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 offset
1602a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
1602b 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 amt -= a;.
1602c 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 pBuf += a;.
1602d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1602e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
1602f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16030 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 && amt>0 ){.
16031 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
16032 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
16033 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
16034 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 /*.** Read part
16035 6f 66 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 of the key assoc
16036 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f iated with curso
16037 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 r pCur. Exactly
16038 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 .** "amt" bytes
16039 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 will be transfer
1603a 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 ed into pBuf[].
1603b 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a The transfer.**
1603c 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 begins at "offs
1603d 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 et"..**.** Retur
1603e 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
1603f 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 uccess or an err
16040 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 or code if anyth
16041 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e ing goes.** wron
16042 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 g. An error is
16043 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 returned if "off
16044 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 set+amt" is larg
16045 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 er than.** the a
16046 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 vailable payload
16047 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16048 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16049 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 treeKey(BtCursor
1604a 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 *pCur, u32 offs
1604b 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
1604c 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 d *pBuf){. int
1604d 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
1604e 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1604f 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 pCur) );. rc =
16050 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
16051 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 ition(pCur);. i
16052 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16053 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
16054 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16055 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
16056 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16057 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
16058 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16059 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28 Page] );. if(
1605a 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
1605b 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
1605c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1605d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1605e 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1605f 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16060 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 ->iPage]<pCur->a
16061 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16062 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 e]->nCell );.
16063 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c rc = accessPayl
16064 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 oad(pCur, offset
16065 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 , amt, (unsigned
16066 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 char*)pBuf, 0,
16067 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0);. }. return
16068 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
16069 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 ad part of the d
1606a 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
1606b 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e ith cursor pCur.
1606c 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d Exactly.** "am
1606d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 t" bytes will be
1606e 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f transfered into
1606f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 pBuf[]. The tr
16070 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 ansfer.** begins
16071 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a at "offset"..**
16072 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
16073 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
16074 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
16075 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
16076 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 s.** wrong. An
16077 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
16078 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 d if "offset+amt
16079 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e " is larger than
1607a 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c .** the availabl
1607b 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 e payload..*/.SQ
1607c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1607d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
1607e 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 a(BtCursor *pCur
1607f 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
16080 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 2 amt, void *pBu
16081 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 f){. int rc;..#
16082 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16083 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 IT_INCRBLOB. if
16084 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d ( pCur->eState=
16085 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16086 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16087 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a LITE_ABORT;. }.
16088 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 #endif.. assert
16089 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
1608a 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
1608b 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
1608c 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
1608d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1608e 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
1608f 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16090 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16091 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
16092 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 r->iPage>=0 && p
16093 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16094 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 ->iPage] );.
16095 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 assert( pCur->ai
16096 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16097 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 <pCur->apPage[pC
16098 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
16099 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 l );. rc = ac
1609a 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 cessPayload(pCur
1609b 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
1609c 42 75 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a Buf, 1, 0);. }.
1609d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1609e 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
1609f 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 ointer to payloa
160a0 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 d information fr
160a1 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 om the entry tha
160a2 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 t the .** pCur c
160a3 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
160a4 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 g to. The point
160a5 65 72 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 er is to the beg
160a6 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 inning of.** the
160a7 20 6b 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d key if skipKey=
160a8 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 =0 and it points
160a9 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e to the beginnin
160aa 67 20 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 g of data if.**
160ab 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 skipKey==1. The
160ac 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
160ad 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 of available ke
160ae 79 2f 64 61 74 61 20 69 73 20 77 72 69 74 74 65 y/data is writte
160af 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e n.** into *pAmt.
160b0 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 If *pAmt==0, t
160b1 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
160b2 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 turned will not
160b3 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f be.** a valid po
160b4 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 inter..**.** Thi
160b5 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 s routine is an
160b6 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 optimization. I
160b7 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 t is common for
160b8 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a the entire key.*
160b9 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 * and data to fi
160ba 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 t on the local p
160bb 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 age and for ther
160bc 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 e to be no overf
160bd 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 low.** pages. W
160be 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 hen that is so,
160bf 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e this routine can
160c0 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 be used to acce
160c1 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e ss the.** key an
160c2 64 20 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d d data without m
160c3 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 aking a copy. I
160c4 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 f the key and/or
160c5 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 data spills.**
160c6 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 onto overflow pa
160c7 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 ges, then access
160c8 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 Payload() must b
160c9 65 20 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 e used to reasse
160ca 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f mble.** the key/
160cb 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 data and copy it
160cc 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 into a prealloc
160cd 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a ated buffer..**.
160ce 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 ** The pointer r
160cf 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 eturned by this
160d0 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 routine looks di
160d1 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 rectly into the
160d2 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f cached.** page o
160d3 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 f the database.
160d4 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 The data might
160d5 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 change or move t
160d6 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 he next time.**
160d7 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e any btree routin
160d8 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a e is called..*/.
160d9 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
160da 69 67 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 igned char *fetc
160db 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 hPayload(. BtCu
160dc 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
160dd 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 /* Cursor point
160de 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 ing to entry to
160df 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 read from */. i
160e0 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 nt *pAmt,
160e1 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
160e2 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c number of avail
160e3 61 62 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 able bytes here
160e4 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 */. int skipKey
160e5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 /* rea
160e6 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 d beginning at d
160e7 61 74 61 20 69 66 20 74 68 69 73 20 69 73 20 74 ata if this is t
160e8 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 rue */.){. unsi
160e9 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c gned char *aPayl
160ea 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a oad;. MemPage *
160eb 70 50 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 pPage;. u32 nKe
160ec 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b y;. u32 nLocal;
160ed 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 .. assert( pCur
160ee 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 !=0 && pCur->iPa
160ef 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 ge>=0 && pCur->a
160f0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
160f1 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e]);. assert( p
160f2 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
160f3 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
160f4 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
160f5 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
160f6 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
160f7 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
160f8 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 age];. assert(
160f9 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
160fa 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e ->iPage]<pPage->
160fb 6e 43 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 nCell );. getCe
160fc 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 llInfo(pCur);.
160fd 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d aPayload = pCur-
160fe 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 >info.pCell;. a
160ff 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d Payload += pCur-
16100 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 >info.nHeader;.
16101 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
16102 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d ey ){. nKey =
16103 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
16104 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 70 43 75 nKey = (int)pCu
16105 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 r->info.nKey;.
16106 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 }. if( skipKey
16107 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f 61 64 20 ){. aPayload
16108 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f += nKey;. nLo
16109 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f cal = pCur->info
1610a 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a .nLocal - nKey;.
1610b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f }else{. nLo
1610c 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f cal = pCur->info
1610d 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 69 66 28 .nLocal;. if(
1610e 20 6e 4c 6f 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a nLocal>nKey ){.
1610f 20 20 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e nLocal = n
16110 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 Key;. }. }.
16111 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b *pAmt = nLocal;
16112 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f . return aPaylo
16113 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f ad;.}.../*.** Fo
16114 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 r the entry that
16115 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
16116 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e point to, return
16117 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 as.** many byte
16118 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 s of the key or
16119 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69 data as are avai
1611a 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 lable on the loc
1611b 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 al.** b-tree pag
1611c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 e. Write the nu
1611d 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
1611e 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 e bytes into *pA
1611f 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f mt..**.** The po
16120 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 inter returned i
16121 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 s ephemeral. Th
16122 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d e key/data may m
16123 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 ove.** or be des
16124 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 troyed on the ne
16125 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 xt call to any B
16126 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a tree routine,.**
16127 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 including calls
16128 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 from other thre
16129 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 ads against the
1612a 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 same cache..** H
1612b 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e ence, a mutex on
1612c 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68 the BtShared sh
1612d 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 ould be held pri
1612e 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a or to calling.**
1612f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
16130 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
16131 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 nes is used to g
16132 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 et quick access
16133 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a to key and data.
16134 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e ** in the common
16135 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f case where no o
16136 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 verflow pages ar
16137 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 e used..*/.SQLIT
16138 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
16139 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
1613a 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 eeKeyFetch(BtCur
1613b 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
1613c 70 41 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 pAmt){. assert(
1613d 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1613e 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 x(pCur) );. if(
1613f 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16140 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
16141 20 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 return (const
16142 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c void*)fetchPayl
16143 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 oad(pCur, pAmt,
16144 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0);. }. return
16145 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 0;.}.SQLITE_PRI
16146 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 VATE const void
16147 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 *sqlite3BtreeDat
16148 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 aFetch(BtCursor
16149 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 *pCur, int *pAmt
1614a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
1614b 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1614c 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75 ur) );. if( pCu
1614d 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1614e 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 R_VALID ){. r
1614f 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 eturn (const voi
16150 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 d*)fetchPayload(
16151 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a pCur, pAmt, 1);.
16152 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
16153 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 }.../*.** Move t
16154 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
16155 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 o a new child pa
16156 67 65 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f ge. The newPgno
16157 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
16158 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 .** page number
16159 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 of the child pag
1615a 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f e to move to..*/
1615b 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
1615c 54 6f 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72 ToChild(BtCursor
1615d 20 2a 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50 *pCur, u32 newP
1615e 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a gno){. int rc;.
1615f 20 20 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e int i = pCur->
16160 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 iPage;. MemPage
16161 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 *pNewPage;. Bt
16162 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 Shared *pBt = pC
16163 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 ur->pBt;.. asse
16164 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
16165 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
16166 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
16167 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
16168 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ID );. assert(
16169 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 pCur->iPage<BTCU
1616a 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 RSOR_MAX_DEPTH )
1616b 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 ;. if( pCur->iP
1616c 61 67 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d age>=(BTCURSOR_M
1616d 41 58 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 AX_DEPTH-1) ){.
1616e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1616f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
16170 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 }. rc = getAnd
16171 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 InitPage(pBt, ne
16172 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 wPgno, &pNewPage
16173 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
16174 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d turn rc;. pCur-
16175 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70 >apPage[i+1] = p
16176 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d NewPage;. pCur-
16177 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b >aiIdx[i+1] = 0;
16178 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b . pCur->iPage++
16179 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ;.. pCur->info.
1617a 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 nSize = 0;. pCu
1617b 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
1617c 3b 0a 20 20 69 66 28 20 70 4e 65 77 50 61 67 65 ;. if( pNewPage
1617d 2d 3e 6e 43 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 ->nCell<1 ){.
1617e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1617f 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
16180 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
16181 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 _OK;.}..#ifndef
16182 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 NDEBUG./*.** Pag
16183 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20 e pParent is an
16184 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 internal (non-le
16185 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 54 af) tree page. T
16186 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a his function .**
16187 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 61 asserts that pa
16188 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 ge number iChild
16189 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 is the left-chi
1618a 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 74 ld if the iIdx't
1618b 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 h.** cell in pag
1618c 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 e pParent. Or, i
1618d 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 f iIdx is equal
1618e 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d to the total num
1618f 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 ber of.** cells
16190 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74 in pParent, that
16191 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 page number iCh
16192 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74 ild is the right
16193 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 -child of.** the
16194 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
16195 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 65 void assertPare
16196 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20 ntIndex(MemPage
16197 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49 *pParent, int iI
16198 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29 dx, Pgno iChild)
16199 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78 {. assert( iIdx
1619a 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c <=pParent->nCell
1619b 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d );. if( iIdx==
1619c 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 pParent->nCell )
1619d 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 {. assert( ge
1619e 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d t4byte(&pParent-
1619f 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e >aData[pParent->
161a0 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69 hdrOffset+8])==i
161a1 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65 Child );. }else
161a2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65 {. assert( ge
161a3 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
161a4 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d pParent, iIdx))=
161a5 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d =iChild );. }.}
161a6 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 .#else.# define
161a7 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 assertParentInd
161a8 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 ex(x,y,z) .#endi
161a9 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 f../*.** Move th
161aa 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74 e cursor up to t
161ab 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a he parent page..
161ac 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20 **.** pCur->idx
161ad 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 65 is set to the ce
161ae 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f ll index that co
161af 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 ntains the point
161b0 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 er.** to the pag
161b1 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 e we are coming
161b2 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65 from. If we are
161b3 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65 coming from the
161b4 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 .** right-most c
161b5 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70 hild page then p
161b6 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 Cur->idx is set
161b7 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e to one more than
161b8 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 .** the largest
161b9 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 cell index..*/.S
161ba 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
161bb 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
161bc 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 oveToParent(BtCu
161bd 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 rsor *pCur){. a
161be 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
161bf 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
161c0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
161c1 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
161c2 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
161c3 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 t( pCur->iPage>0
161c4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
161c5 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
161c6 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
161c7 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a ertParentIndex(.
161c8 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 pCur->apPage
161c9 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c [pCur->iPage-1],
161ca 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 . pCur->aiId
161cb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d x[pCur->iPage-1]
161cc 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 , . pCur->apP
161cd 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
161ce 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 ->pgno. );. re
161cf 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e leasePage(pCur->
161d0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
161d1 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 ge]);. pCur->iP
161d2 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 age--;. pCur->i
161d3 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
161d4 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
161d5 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d = 0;.}../*.** M
161d6 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 ove the cursor t
161d7 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 0a o the root page.
161d8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
161d9 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f veToRoot(BtCurso
161da 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 r *pCur){. MemP
161db 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e age *pRoot;. in
161dc 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
161dd 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 ;. Btree *p = p
161de 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 Cur->pBtree;. B
161df 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
161e0 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 ->pBt;.. assert
161e1 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
161e2 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
161e3 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 sert( CURSOR_INV
161e4 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 ALID < CURSOR_RE
161e5 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 QUIRESEEK );. a
161e6 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 ssert( CURSOR_VA
161e7 4c 49 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 LID < CURSOR_R
161e8 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 EQUIRESEEK );.
161e9 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 assert( CURSOR_F
161ea 41 55 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f AULT > CURSOR_
161eb 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 REQUIRESEEK );.
161ec 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
161ed 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 e>=CURSOR_REQUIR
161ee 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 ESEEK ){. if(
161ef 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
161f0 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 URSOR_FAULT ){.
161f1 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 return pCur
161f2 2d 3e 73 6b 69 70 3b 0a 20 20 20 20 7d 0a 20 20 ->skip;. }.
161f3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
161f4 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b earCursor(pCur);
161f5 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 . }.. if( pCur
161f6 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 ->iPage>=0 ){.
161f7 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
161f8 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 (i=1; i<=pCur->i
161f9 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
161fa 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
161fb 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a ur->apPage[i]);.
161fc 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
161fd 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 if( . SQ
161fe 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 67 LITE_OK!=(rc = g
161ff 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 etAndInitPage(pB
16200 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t, pCur->pgnoRoo
16201 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 t, &pCur->apPage
16202 5b 30 5d 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 [0])). ){.
16203 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
16204 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
16205 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
16206 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 c;. }. }..
16207 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 pRoot = pCur->ap
16208 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 Page[0];. asser
16209 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d t( pRoot->pgno==
1620a 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 pCur->pgnoRoot )
1620b 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 ;. pCur->iPage
1620c 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 = 0;. pCur->aiI
1620d 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75 dx[0] = 0;. pCu
1620e 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
1620f 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 0;. pCur->atLas
16210 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 t = 0;. pCur->v
16211 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 alidNKey = 0;..
16212 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c if( pRoot->nCel
16213 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e l==0 && !pRoot->
16214 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f leaf ){. Pgno
16215 20 73 75 62 70 61 67 65 3b 0a 20 20 20 20 61 73 subpage;. as
16216 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e sert( pRoot->pgn
16217 6f 3d 3d 31 20 29 3b 0a 20 20 20 20 73 75 62 70 o==1 );. subp
16218 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
16219 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f pRoot->aData[pRo
1621a 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ot->hdrOffset+8]
1621b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
1621c 75 62 70 61 67 65 3e 30 20 29 3b 0a 20 20 20 20 ubpage>0 );.
1621d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
1621e 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 URSOR_VALID;.
1621f 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
16220 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 29 d(pCur, subpage)
16221 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
16222 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28 Cur->eState = ((
16223 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f pRoot->nCell>0)?
16224 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52 CURSOR_VALID:CUR
16225 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20 SOR_INVALID);.
16226 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
16227 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
16228 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 cursor down to
16229 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 the left-most le
1622a 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 af entry beneath
1622b 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f the.** entry to
1622c 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 which it is cur
1622d 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e rently pointing.
1622e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d .**.** The left-
1622f 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68 65 most leaf is the
16230 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d one with the sm
16231 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65 allest key - the
16232 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63 first.** in asc
16233 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f ending order..*/
16234 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
16235 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72 ToLeftmost(BtCur
16236 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 sor *pCur){. Pg
16237 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 no pgno;. int r
16238 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
16239 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
1623a 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1623b 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1623c 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1623d 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1623e 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
1623f 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
16240 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 E_OK && !(pPage
16241 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
16242 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 Cur->iPage])->le
16243 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 af ){. assert
16244 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
16245 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 ur->iPage]<pPage
16246 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70 ->nCell );. p
16247 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66 gno = get4byte(f
16248 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 indCell(pPage, p
16249 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1624a 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72 >iPage]));. r
1624b 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
1624c 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d pCur, pgno);. }
1624d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1624e 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
1624f 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 cursor down to t
16250 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 he right-most le
16251 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 af entry beneath
16252 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 the.** page to
16253 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72 which it is curr
16254 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 ently pointing.
16255 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 66 Notice the diff
16256 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65 erence.** betwee
16257 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 n moveToLeftmost
16258 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67 () and moveToRig
16259 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54 htmost(). moveT
1625a 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 oLeftmost().** f
1625b 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f inds the left-mo
1625c 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 st entry beneath
1625d 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65 the *entry* whe
1625e 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74 reas moveToRight
1625f 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 most().** finds
16260 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 the right-most e
16261 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
16262 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 *page*..**.** T
16263 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e he right-most en
16264 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77 try is the one w
16265 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 ith the largest
16266 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a key - the last.*
16267 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69 * key in ascendi
16268 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 ng order..*/.sta
16269 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69 tic int moveToRi
1626a 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 ghtmost(BtCursor
1626b 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 *pCur){. Pgno
1626c 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d pgno;. int rc =
1626d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 SQLITE_OK;. Me
1626e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
1626f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
16270 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16271 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
16272 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16273 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16274 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 while( rc==SQLI
16275 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 TE_OK && !(pPage
16276 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
16277 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c pCur->iPage])->l
16278 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 eaf ){. pgno
16279 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
1627a 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
1627b 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
1627c 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
1627d 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 Cur->iPage] = pP
1627e 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 age->nCell;.
1627f 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 rc = moveToChild
16280 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 (pCur, pgno);.
16281 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
16282 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75 TE_OK ){. pCu
16283 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16284 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e Page] = pPage->n
16285 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72 Cell-1;. pCur
16286 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
16287 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 ;. pCur->vali
16288 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 dNKey = 0;. }.
16289 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1628a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1628b 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 r to the first e
1628c 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
1628d 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
1628e 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 E_OK.** on succe
1628f 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 ss. Set *pRes t
16290 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f o 0 if the curso
16291 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 r actually point
16292 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a s to something.*
16293 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 * or set *pRes t
16294 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 o 1 if the table
16295 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 is empty..*/.SQ
16296 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16297 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 sqlite3BtreeFir
16298 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st(BtCursor *pCu
16299 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
1629a 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
1629b 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1629c 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1629d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1629e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
1629f 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
162a0 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f ex) );. rc = mo
162a1 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a veToRoot(pCur);.
162a2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
162a3 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 _OK ){. if( p
162a4 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
162a5 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 SOR_INVALID ){.
162a6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
162a7 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
162a8 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 iPage]->nCell==0
162a9 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
162aa 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 1;. rc =
162ab 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
162ac 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
162ad 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
162ae 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
162af 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 Cell>0 );.
162b0 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
162b1 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 rc = moveToLeft
162b2 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
162b3 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
162b4 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 c;.}../* Move th
162b5 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
162b6 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 last entry in th
162b7 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e e table. Return
162b8 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e SQLITE_OK.** on
162b9 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a success. Set *
162ba 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 pRes to 0 if the
162bb 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 cursor actually
162bc 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 points to somet
162bd 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a hing.** or set *
162be 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 pRes to 1 if the
162bf 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e table is empty.
162c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
162c1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
162c2 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 reeLast(BtCursor
162c3 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
162c4 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a s){. int rc;. .
162c5 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
162c6 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
162c7 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
162c8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
162c9 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 (pCur->pBtree->d
162ca 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 b->mutex) );..
162cb 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 /* If the cursor
162cc 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20 already points
162cd 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 to the last entr
162ce 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d y, this is a no-
162cf 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52 op. */. if( CUR
162d0 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d SOR_VALID==pCur-
162d1 3e 65 53 74 61 74 65 20 26 26 20 70 43 75 72 2d >eState && pCur-
162d2 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65 >atLast ){.#ifde
162d3 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
162d4 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
162d5 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 serves to asser
162d6 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72 t() that the cur
162d7 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 sor really does
162d8 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f point . ** to
162d9 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
162da 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a in the b-tree. *
162db 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 /. int ii;.
162dc 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 for(ii=0; ii<p
162dd 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b Cur->iPage; ii++
162de 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
162df 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d pCur->aiIdx[ii]
162e0 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 ==pCur->apPage[i
162e1 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 i]->nCell );.
162e2 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
162e3 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
162e4 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 >iPage]==pCur->a
162e5 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
162e6 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 e]->nCell-1 );.
162e7 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
162e8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
162e9 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 age]->leaf );.#e
162ea 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 ndif. return
162eb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
162ec 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
162ed 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
162ee 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
162ef 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
162f0 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
162f1 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 tate ){. as
162f2 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
162f3 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
162f4 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 >nCell==0 );.
162f5 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
162f6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
162f7 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
162f8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
162f9 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 D );. *pRes
162fa 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d = 0;. rc =
162fb 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
162fc 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 43 (pCur);. pC
162fd 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 3d ur->atLast = rc=
162fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b =SQLITE_OK ?1:0;
162ff 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
16300 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f urn rc;.}../* Mo
16301 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f ve the cursor so
16302 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
16303 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 to an entry near
16304 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65 the key .** spe
16305 63 69 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 cified by pIdxKe
16306 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 y or intKey. R
16307 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 eturn a success
16308 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 code..**.** For
16309 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74 INTKEY tables, t
1630a 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65 he intKey parame
1630b 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 70 49 ter is used. pI
1630c 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62 dxKey .** must b
1630d 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 e NULL. For ind
1630e 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b ex tables, pIdxK
1630f 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 20 69 ey is used and i
16310 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f ntKey.** is igno
16311 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e red..**.** If an
16312 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 exact match is
16313 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 not found, then
16314 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c the cursor is al
16315 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 ways.** left poi
16316 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 nting at a leaf
16317 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 page which would
16318 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 hold the entry
16319 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 if it.** were pr
1631a 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 esent. The curs
1631b 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 or might point t
1631c 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 o an entry that
1631d 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 comes.** before
1631e 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 or after the key
1631f 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 ..**.** An integ
16320 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e er is written in
16321 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20 69 to *pRes which i
16322 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a s the result of.
16323 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 ** comparing the
16324 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 65 6e key with the en
16325 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 65 try to which the
16326 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70 cursor is .** p
16327 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65 ointing. The me
16328 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 aning of the int
16329 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74 eger written int
1632a 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73 o.** *pRes is as
1632b 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
1632c 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 *pRes<0
1632d 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c The cursor is l
1632e 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
1632f 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a an entry that.**
16330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16331 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 is smaller tha
16332 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 n intKey/pIdxKey
16333 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 or if the table
16334 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 is empty.**
16335 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e an
16336 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 d the cursor is
16337 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 therefore left p
16338 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e oint to nothing.
16339 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 .**.** *pRes
1633a 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73 ==0 The curs
1633b 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
1633c 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 ing at an entry
1633d 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 that.**
1633e 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 exactly
1633f 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f matches intKey/
16340 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 pIdxKey..**.**
16341 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 *pRes>0
16342 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
16343 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
16344 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 n entry that.**
16345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16346 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
16347 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a intKey/pIdxKey..
16348 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 **.*/.SQLITE_PRI
16349 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1634a 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1634b 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 ked(. BtCursor
1634c 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 *pCur,
1634d 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f /* The cursor to
1634e 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 be moved */. U
1634f 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
16350 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63 IdxKey, /* Unpac
16351 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f ked index key */
16352 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 . i64 intKey,
16353 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
16354 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a he table key */.
16355 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c int biasRight,
16356 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
16357 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 true, bias the
16358 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 search to the hi
16359 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 gh end */. int
1635a 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 *pRes
1635b 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
1635c 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
1635d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
1635e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
1635f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16360 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
16361 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16362 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
16363 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
16364 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 . /* If the cur
16365 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 sor is already p
16366 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 68 65 ositioned at the
16367 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 74 72 point we are tr
16368 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 ying. ** to mov
16369 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 e to, then just
1636a 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 return without d
1636b 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f oing any work */
1636c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
1636d 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
1636e 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c 69 64 D && pCur->valid
1636f 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43 75 72 NKey . && pCur
16370 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
16371 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 Key . ){. if
16372 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 ( pCur->info.nKe
16373 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 y==intKey ){.
16374 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
16375 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16376 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
16377 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 if( pCur->atLast
16378 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e && pCur->info.n
16379 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 Key<intKey ){.
1637a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a *pRes = -1;.
1637b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1637c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
1637d 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f }.. rc = moveTo
1637e 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 Root(pCur);. if
1637f 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
16380 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
16381 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
16382 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 e[pCur->iPage] )
16383 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16384 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16385 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b Page]->isInit );
16386 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
16387 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
16388 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 LID ){. *pRes
16389 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 = -1;. asser
1638a 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
1638b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 pCur->iPage]->nC
1638c 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 ell==0 );. re
1638d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1638e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
1638f 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
16390 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 ntKey || pIdxKey
16391 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 );. for(;;){.
16392 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b int lwr, upr;
16393 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64 50 67 . Pgno chldPg
16394 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 ;. MemPage *p
16395 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
16396 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16397 3b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 2d 31 ;. int c = -1
16398 3b 20 20 2f 2a 20 70 52 65 73 20 72 65 74 75 72 ; /* pRes retur
16399 6e 20 69 66 20 74 61 62 6c 65 20 69 73 20 65 6d n if table is em
1639a 70 74 79 20 6d 75 73 74 20 62 65 20 2d 31 20 2a pty must be -1 *
1639b 2f 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 /. lwr = 0;.
1639c 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e upr = pPage->
1639d 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 nCell-1;. if(
1639e 20 28 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 (!pPage->intKey
1639f 20 26 26 20 70 49 64 78 4b 65 79 3d 3d 30 29 20 && pIdxKey==0)
163a0 7c 7c 20 75 70 72 3c 30 20 29 7b 0a 20 20 20 20 || upr<0 ){.
163a1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
163a2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
163a3 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 goto moveto_fi
163a4 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 nish;. }.
163a5 69 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b if( biasRight ){
163a6 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 . pCur->aiI
163a7 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
163a8 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 = (u16)upr;.
163a9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 }else{. pCu
163aa 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
163ab 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 Page] = (u16)((u
163ac 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 pr+lwr)/2);.
163ad 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 }. for(;;){.
163ae 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 int idx = p
163af 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
163b0 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 >iPage]; /* Inde
163b1 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c x of current cel
163b2 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 l in pPage */.
163b3 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 u8 *pCell;
163b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
163b5 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
163b6 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 er to current ce
163b7 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a ll in pPage */..
163b8 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
163b9 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
163ba 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
163bb 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b ll(pPage, idx) +
163bc 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
163bd 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Size;. if(
163be 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
163bf 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 . i64 nCe
163c0 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 llKey;. i
163c1 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 f( pPage->hasDat
163c2 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 a ){. u
163c3 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 32 dummy;.
163c4 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 pCell += get
163c5 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 Varint32(pCell,
163c6 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 dummy);.
163c7 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 }. getVar
163c8 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a int(pCell, (u64*
163c9 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 )&nCellKey);.
163ca 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 if( nCellKe
163cb 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 y==intKey ){.
163cc 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 c = 0;.
163cd 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
163ce 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 nCellKey<intKey
163cf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d ){. c =
163d0 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c -1;. }el
163d1 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 se{. as
163d2 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 sert( nCellKey>i
163d3 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 ntKey );.
163d4 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 c = +1;.
163d5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 }. pCu
163d6 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
163d7 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
163d8 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c info.nKey = nCel
163d9 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 lKey;. }els
163da 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e{. /* Th
163db 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 e maximum suppor
163dc 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 ted page-size is
163dd 20 33 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 32768 bytes. Th
163de 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 is means that.
163df 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 ** the max
163e0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 imum number of r
163e1 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 ecord bytes stor
163e2 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 ed on an index B
163e3 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a -Tree. **
163e4 20 70 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 page is at most
163e5 20 38 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 8198 bytes, whi
163e6 63 68 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 ch may be stored
163e7 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 as a 2-byte.
163e8 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 ** varint.
163e9 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e This information
163ea 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 is used to atte
163eb 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 mpt to avoid par
163ec 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a sing . **
163ed 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c the entire cell
163ee 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 by checking for
163ef 20 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65 the cases where
163f0 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a the record is .
163f1 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 ** store
163f2 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 d entirely withi
163f3 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 n the b-tree pag
163f4 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 e by inspecting
163f5 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 the first .
163f6 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 ** 2 bytes of
163f7 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 the cell..
163f8 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e */. in
163f9 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b t nCell = pCell[
163fa 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0];. if(
163fb 21 28 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 !(nCell & 0x80)
163fc 26 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d && nCell<=pPage-
163fd 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 >maxLocal ){.
163fe 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 /* This b
163ff 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 ranch runs if th
16400 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 e record-size fi
16401 65 6c 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 eld of the cell
16402 69 73 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a is a. *
16403 2a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 * single byte va
16404 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 rint and the rec
16405 6f 72 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c ord fits entirel
16406 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 y on the main.
16407 20 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 ** b-tre
16408 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 e page. */.
16409 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 c = sqlite
1640a 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
1640b 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a re(nCell, (void*
1640c 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 )&pCell[1], pIdx
1640d 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 Key);. }e
1640e 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b lse if( !(pCell[
1640f 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 1] & 0x80) .
16410 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 && (nCell
16411 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c = ((nCell&0x7f)<
16412 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c <7) + pCell[1])<
16413 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
16414 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
16415 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 /* The rec
16416 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 ord-size field i
16417 73 20 61 20 32 20 62 79 74 65 20 76 61 72 69 6e s a 2 byte varin
16418 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 t and the record
16419 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 . ** f
1641a 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 its entirely on
1641b 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 the main b-tree
1641c 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 page. */.
1641d 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
1641e 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
1641f 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
16420 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 pCell[2], pIdxKe
16421 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
16422 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
16423 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 The record flows
16424 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f over onto one o
16425 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 r more overflow
16426 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 pages. In.
16427 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 ** this case
16428 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 the whole cell
16429 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 needs to be pars
1642a 65 64 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c ed, a buffer all
1642b 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 ocated.
1642c 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 ** and accessPa
1642d 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 yload() used to
1642e 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65 63 retrieve the rec
1642f 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 ord into the.
16430 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 ** buffer
16431 20 62 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f before VdbeReco
16432 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 rdCompare() can
16433 62 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 be called. */.
16434 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 void *pC
16435 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 ellKey;.
16436 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 u8 * const pCe
16437 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d llBody = pCell -
16438 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
16439 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 Size;.
1643a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
1643b 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
1643c 70 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 pCellBody, &pCur
1643d 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 ->info);.
1643e 20 20 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 nCell = (int)
1643f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b pCur->info.nKey;
16440 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c . pCell
16441 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Key = sqlite3Mal
16442 6c 6f 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 loc( nCell );.
16443 20 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c if( pCel
16444 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lKey==0 ){.
16445 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
16446 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
16447 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
16448 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 o_finish;.
16449 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1644a 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f rc = accessPaylo
1644b 61 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c ad(pCur, 0, nCel
1644c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 l, (unsigned cha
1644d 72 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 30 2c 20 r*)pCellKey, 0,
1644e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 0);. c
1644f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
16450 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c ordCompare(nCell
16451 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 , pCellKey, pIdx
16452 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Key);.
16453 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 sqlite3_free(pCe
16454 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
16455 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
16456 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
16457 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
16458 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 . if( c==0
16459 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1645a 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 Page->intKey &&
1645b 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1645c 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 lwr =
1645d 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 idx;. u
1645e 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 pr = lwr - 1;.
1645f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
16460 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
16461 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
16462 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 0;. rc
16463 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
16464 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 goto move
16465 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 to_finish;.
16466 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
16467 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 if( c<0 ){.
16468 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b lwr = idx+
16469 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
1646a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 upr = id
1646b 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 x-1;. }.
1646c 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 if( lwr>upr )
1646d 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
1646e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1646f 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
16470 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 >iPage] = (u16)(
16471 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 (lwr+upr)/2);.
16472 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
16473 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 lwr==upr+1 );.
16474 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
16475 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 >isInit );. i
16476 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
16477 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d {. chldPg =
16478 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
16479 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 ( lwr>=pPage->nC
1647a 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c ell ){. chl
1647b 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 dPg = get4byte(&
1647c 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
1647d 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
1647e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1647f 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 chldPg = get
16480 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 4byte(findCell(p
16481 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 Page, lwr));.
16482 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 }. if( chldP
16483 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 g==0 ){. as
16484 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
16485 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
16486 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16487 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 ->iPage]->nCell
16488 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 );. if( pRe
16489 73 20 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 s ) *pRes = c;.
1648a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1648b 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 _OK;. goto
1648c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
1648d 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 }. pCur->a
1648e 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1648f 5d 20 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 ] = (u16)lwr;.
16490 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
16491 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 ze = 0;. pCur
16492 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
16493 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
16494 43 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 Child(pCur, chld
16495 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 Pg);. if( rc
16496 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 ) goto moveto_fi
16497 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f nish;. }.moveto
16498 5f 66 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 _finish:. retur
16499 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 n rc;.}../*.** I
1649a 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f n this version o
1649b 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 f BtreeMoveto, p
1649c 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 Key is a packed
1649d 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 index record.**
1649e 73 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 such as is gener
1649f 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d ated by the OP_M
164a0 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 akeRecord opcode
164a1 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a . Unpack the.**
164a2 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e record and then
164a3 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 call BtreeMovet
164a4 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 oUnpacked() to d
164a5 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 o the work..*/.S
164a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
164a7 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f t sqlite3BtreeMo
164a8 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 veto(. BtCursor
164a9 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 *pCur, /* C
164aa 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 ursor open on th
164ab 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 e btree to be se
164ac 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 arched */. cons
164ad 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 t void *pKey,
164ae 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 /* Packed key if
164af 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e the btree is an
164b0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 index */. i64
164b1 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 nKey,
164b2 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 /* Integer key f
164b3 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 or tables. Size
164b4 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 of pKey for ind
164b5 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 ices */. int bi
164b6 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a as, /*
164b7 20 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 Bias search to
164b8 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a the high end */.
164b9 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 int *pRes
164ba 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 /* Write s
164bb 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 earch results he
164bc 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 re */.){. int r
164bd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
164be 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 /* Status
164bf 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b code */. Unpack
164c0 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 edRecord *pIdxKe
164c1 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 y; /* Unpacked
164c2 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 index key */.
164c3 63 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d char aSpace[150]
164c4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 ; /* Te
164c5 6d 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 mp space for pId
164c6 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 xKey - to avoid
164c7 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 0a 20 20 a malloc */...
164c8 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
164c9 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 assert( nKey==(i
164ca 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 64)(int)nKey );.
164cb 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 pIdxKey = sq
164cc 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
164cd 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 npack(pCur->pKey
164ce 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c Info, (int)nKey,
164cf 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 pKey,.
164d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164d1 20 20 20 20 20 20 20 20 20 20 20 20 20 61 53 70 aSp
164d2 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 ace, sizeof(aSpa
164d3 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 ce));. if( pI
164d4 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 dxKey==0 ) retur
164d5 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
164d6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 }else{. pId
164d7 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 xKey = 0;. }.
164d8 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
164d9 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
164da 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e pCur, pIdxKey, n
164db 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 Key, bias, pRes)
164dc 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a ;. if( pKey ){.
164dd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 sqlite3VdbeD
164de 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
164df 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 ord(pIdxKey);.
164e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
164e1 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .../*.** Return
164e2 54 52 55 45 20 69 66 20 74 68 65 20 63 75 72 73 TRUE if the curs
164e3 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 or is not pointi
164e4 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f ng at an entry o
164e5 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a f the table..**.
164e6 2a 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 ** TRUE will be
164e7 72 65 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 returned after a
164e8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
164e9 42 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 BtreeNext() move
164ea 73 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 s.** past the la
164eb 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
164ec 74 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 table or sqlite3
164ed 42 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 BtreePrev() move
164ee 73 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 s past.** the fi
164ef 72 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 rst entry. TRUE
164f0 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 is also returne
164f1 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 d if the table i
164f2 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 s empty..*/.SQLI
164f3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
164f4 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 qlite3BtreeEof(B
164f5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
164f6 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 /* TODO: What
164f7 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 if the cursor is
164f8 20 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 in CURSOR_REQUI
164f9 52 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 RESEEK but all t
164fa 61 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a able entries. *
164fb 2a 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 * have been dele
164fc 74 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 ted? This API wi
164fd 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 ll need to chang
164fe 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 e to return an e
164ff 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 rror code. ** a
16500 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f s well as the bo
16501 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c olean result val
16502 75 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 ue.. */. retur
16503 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 n (CURSOR_VALID!
16504 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a =pCur->eState);.
16505 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
16506 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
16507 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 nection handle f
16508 6f 72 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a or a cursor..*/.
16509 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
1650a 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 qlite3 *sqlite3B
1650b 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e treeCursorDb(con
1650c 73 74 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st BtCursor *pCu
1650d 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 r){. assert( sq
1650e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1650f 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 (pCur->pBtree->d
16510 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 b->mutex) );. r
16511 65 74 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 eturn pCur->pBtr
16512 65 65 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ee->db;.}../*.**
16513 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 Advance the cur
16514 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 sor to the next
16515 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 entry in the dat
16516 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 abase. If.** su
16517 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 ccessful then se
16518 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 t *pRes=0. If t
16519 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 he cursor.** was
1651a 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e already pointin
1651b 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e g to the last en
1651c 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1651d 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 ase before.** th
1651e 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 is routine was c
1651f 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 alled, then set
16520 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 *pRes=1..*/.SQLI
16521 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16522 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 qlite3BtreeNext(
16523 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
16524 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e int *pRes){. in
16525 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b t rc;. int idx;
16526 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
16527 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 e;.. assert( cu
16528 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16529 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 Cur) );. rc = r
1652a 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
1652b 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
1652c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1652d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1652e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1652f 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 pRes!=0 );. if(
16530 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d CURSOR_INVALID=
16531 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
16532 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a . *pRes = 1;.
16533 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16534 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
16535 70 43 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a pCur->skip>0 ){.
16536 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d pCur->skip =
16537 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 0;. *pRes =
16538 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
16539 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 LITE_OK;. }. p
1653a 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a Cur->skip = 0;..
1653b 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
1653c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1653d 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 ge];. idx = ++p
1653e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1653f 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 >iPage];. asser
16540 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
16541 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 );. assert( id
16542 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x<=pPage->nCell
16543 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f );.. pCur->info
16544 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 .nSize = 0;. pC
16545 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
16546 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 0;. if( idx>=pP
16547 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 age->nCell ){.
16548 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
16549 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d af ){. rc =
1654a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
1654b 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 r, get4byte(&pPa
1654c 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
1654d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b >hdrOffset+8]));
1654e 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1654f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
16550 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 rc = moveToLeft
16551 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
16552 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
16553 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
16554 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 }. do{.
16555 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
16556 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
16557 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 *pRes = 1;.
16558 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
16559 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1655a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1655b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1655c 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1655d 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 3BtreeMoveToPare
1655e 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 nt(pCur);.
1655f 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16560 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16561 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 ];. }while( p
16562 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
16563 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e >iPage]>=pPage->
16564 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 nCell );. *pR
16565 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 es = 0;. if(
16566 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
16567 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
16568 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 te3BtreeNext(pCu
16569 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 r, pRes);. }e
1656a 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1656b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
1656c 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1656d 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b }. *pRes = 0;
1656e 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 . if( pPage->le
1656f 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e af ){. return
16570 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
16571 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 rc = moveToLef
16572 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72 tmost(pCur);. r
16573 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
16574 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 .** Step the cur
16575 73 6f 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 sor to the back
16576 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 to the previous
16577 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 entry in the dat
16578 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 abase. If.** su
16579 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 ccessful then se
1657a 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 t *pRes=0. If t
1657b 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 he cursor.** was
1657c 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e already pointin
1657d 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 g to the first e
1657e 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 ntry in the data
1657f 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 base before.** t
16580 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 his routine was
16581 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 called, then set
16582 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c *pRes=1..*/.SQL
16583 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16584 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
16585 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ious(BtCursor *p
16586 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
16587 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
16588 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
16589 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1658a 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1658b 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 ;. rc = restore
1658c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
1658d 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d Cur);. if( rc!=
1658e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1658f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
16590 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d pCur->atLast =
16591 20 30 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 0;. if( CURSOR
16592 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e _INVALID==pCur->
16593 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 eState ){. *p
16594 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 Res = 1;. ret
16595 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
16596 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 }. if( pCur->s
16597 6b 69 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 kip<0 ){. pCu
16598 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 r->skip = 0;.
16599 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
1659a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1659b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b ;. }. pCur->sk
1659c 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 ip = 0;.. pPage
1659d 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
1659e 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
1659f 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
165a0 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 sInit );. if( !
165a1 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
165a2 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
165a3 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
165a4 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 Page];. rc =
165a5 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
165a6 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 , get4byte(findC
165a7 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 ell(pPage, idx))
165a8 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
165a9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
165aa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
165ab 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
165ac 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b (pCur);. }else{
165ad 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 . while( pCur
165ae 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
165af 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 age]==0 ){.
165b0 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
165b1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
165b2 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
165b3 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
165b4 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
165b5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
165b6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
165b7 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
165b8 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e BtreeMoveToParen
165b9 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 t(pCur);. }.
165ba 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 pCur->info.nS
165bb 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 ize = 0;. pCu
165bc 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
165bd 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 ;.. pCur->aiI
165be 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d dx[pCur->iPage]-
165bf 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 -;. pPage = p
165c0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
165c1 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 ->iPage];. if
165c2 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
165c3 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 && !pPage->leaf
165c4 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
165c5 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
165c6 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a us(pCur, pRes);.
165c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
165c8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
165c9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 . }. }. *pR
165ca 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e es = 0;. return
165cb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c rc;.}../*.** Al
165cc 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 locate a new pag
165cd 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 e from the datab
165ce 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
165cf 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 The new page is
165d0 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e marked as dirty.
165d1 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 (In other word
165d2 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 s, sqlite3PagerW
165d3 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c rite().** has al
165d4 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 ready been calle
165d5 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 d on the new pag
165d6 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 e.) The new pag
165d7 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 e has also.** be
165d8 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e en referenced an
165d9 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f d the calling ro
165da 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 utine is respons
165db 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 ible for calling
165dc 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
165dd 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e Unref() on the n
165de 65 77 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 ew page when it
165df 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 is done..**.** S
165e0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
165e1 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e rned on success.
165e2 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 Any other retu
165e3 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 rn value indicat
165e4 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 es.** an error.
165e5 20 2a 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 *ppPage and *pP
165e6 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 gno are undefine
165e7 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f d in the event o
165e8 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 f an error..** D
165e9 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c o not invoke sql
165ea 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
165eb 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 on *ppPage if a
165ec 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
165ed 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
165ee 65 20 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d e "nearby" param
165ef 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 eter is not 0, t
165f0 68 65 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65 hen a (feeble) e
165f1 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f ffort is made to
165f2 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 .** locate a pa
165f3 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 ge close to the
165f4 70 61 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 page number "nea
165f5 72 62 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 rby". This can
165f6 62 65 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a be used in an.**
165f7 20 61 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 attempt to keep
165f8 20 72 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 related pages c
165f9 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 lose to each oth
165fa 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 er in the databa
165fb 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 se file,.** whic
165fc 68 20 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 h in turn can ma
165fd 6b 65 20 64 61 74 61 62 61 73 65 20 61 63 63 65 ke database acce
165fe 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a ss faster..**.**
165ff 20 49 66 20 74 68 65 20 22 65 78 61 63 74 22 20 If the "exact"
16600 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 parameter is not
16601 20 30 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 0, and the page
16602 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 -number nearby e
16603 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 xists .** anywhe
16604 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c re on the free-l
16605 69 73 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ist, then it is
16606 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 guarenteed to be
16607 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a returned. This.
16608 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 ** is only used
16609 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 by auto-vacuum d
1660a 61 74 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c atabases when al
1660b 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74 locating a new t
1660c 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 able..*/.static
1660d 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 int allocateBtre
1660e 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 ePage(. BtShare
1660f 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 d *pBt, . MemPa
16610 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 ge **ppPage, .
16611 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 Pgno *pPgno, .
16612 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 Pgno nearby,. u
16613 38 20 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 8 exact.){. Mem
16614 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 Page *pPage1;.
16615 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b int rc;. int n;
16616 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
16617 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 f pages on the f
16618 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 reelist */. int
16619 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 k; /* Numbe
1661a 72 20 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 r of leaves on t
1661b 68 65 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 he trunk of the
1661c 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 freelist */. Me
1661d 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 mPage *pTrunk =
1661e 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 0;. MemPage *pP
1661f 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 revTrunk = 0;..
16620 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16621 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
16622 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 >mutex) );. pPa
16623 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 ge1 = pBt->pPage
16624 31 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 1;. n = get4byt
16625 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
16626 5b 33 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 [36]);. if( n>0
16627 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 ){. /* There
16628 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 are pages on th
16629 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 e freelist. Reu
1662a 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 se one of those
1662b 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 pages. */. Pg
1662c 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 no iTrunk;. u
1662d 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 8 searchList = 0
1662e 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 ; /* If the free
1662f 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 -list must be se
16630 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 arched for 'near
16631 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 by' */. .
16632 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74 /* If the 'exact
16633 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 ' parameter was
16634 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 true and a query
16635 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d of the pointer-
16636 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 map. ** shows
16637 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27 that the page '
16638 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 nearby' is somew
16639 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 here on the free
1663a 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 -list, then.
1663b 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 ** the entire-li
1663c 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 st will be searc
1663d 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 hed for that pag
1663e 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 e.. */.#ifnde
1663f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16640 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
16641 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 exact && nearby
16642 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 <=pagerPagecount
16643 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75 (pBt) ){. u
16644 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 8 eType;. a
16645 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 ssert( nearby>0
16646 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
16647 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16648 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 );. rc = p
16649 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 trmapGet(pBt, ne
1664a 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 arby, &eType, 0)
1664b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
1664c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1664d 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
1664e 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a MAP_FREEPAGE ){.
1664f 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 searchLi
16650 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a st = 1;. }.
16651 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e *pPgno = n
16652 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e earby;. }.#en
16653 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 dif.. /* Decr
16654 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c ement the free-l
16655 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 ist count by 1.
16656 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 Set iTrunk to th
16657 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 e index of the.
16658 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 ** first free
16659 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
1665a 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 . iPrevTrunk is
1665b 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 initially 1..
1665c 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
1665d 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
1665e 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
1665f 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
16660 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 turn rc;. put
16661 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
16662 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a Data[36], n-1);.
16663 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 . /* The code
16664 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f within this loo
16665 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e p is run only on
16666 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 63 ce if the 'searc
16667 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a hList' variable.
16668 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 ** is not tr
16669 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ue. Otherwise, i
1666a 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 t runs once for
1666b 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 each trunk-page
1666c 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 on the. ** fr
1666d 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 ee-list until th
1666e 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
1666f 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 is located..
16670 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 */. do {.
16671 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 pPrevTrunk = p
16672 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 Trunk;. if(
16673 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 pPrevTrunk ){.
16674 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 iTrunk =
16675 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54 get4byte(&pPrevT
16676 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b runk->aData[0]);
16677 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
16678 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 iTrunk = g
16679 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d et4byte(&pPage1-
1667a 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 >aData[32]);.
1667b 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1667c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
1667d 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c age(pBt, iTrunk,
1667e 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 &pTrunk, 0);.
1667f 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
16680 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 pTrunk = 0
16681 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 ;. goto e
16682 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
16683 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
16684 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 k = get4byte(&p
16685 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 Trunk->aData[4])
16686 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 ;. if( k==0
16687 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 && !searchList
16688 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
16689 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c e trunk has no l
1668a 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 eaves and the li
1668b 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 st is not being
1668c 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 searched. .
1668d 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 ** So extract
1668e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 the trunk page
1668f 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 itself and use i
16690 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a t as the newly .
16691 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 ** alloc
16692 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 ated page */.
16693 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 assert( pPr
16694 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 evTrunk==0 );.
16695 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16696 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
16697 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
16698 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
16699 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1669a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
1669b 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
1669c 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 *pPgno = iT
1669d 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 runk;. me
1669e 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 mcpy(&pPage1->aD
1669f 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b ata[32], &pTrunk
166a0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a ->aData[0], 4);.
166a1 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 *ppPage
166a2 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = pTrunk;.
166a3 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 pTrunk = 0;.
166a4 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
166a5 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b LOCATE: %d trunk
166a6 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 - %d free pages
166a7 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f left\n", *pPgno
166a8 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d , n-1));. }
166a9 65 6c 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e else if( k>pBt->
166aa 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 usableSize/4 - 2
166ab 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 ){. /* V
166ac 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 alue of k is out
166ad 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61 of range. Data
166ae 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
166af 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
166b0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
166b1 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 KPT;. got
166b2 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
166b3 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c age;.#ifndef SQL
166b4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
166b5 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 UUM. }else
166b6 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20 26 if( searchList &
166b7 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b & nearby==iTrunk
166b8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
166b9 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 he list is being
166ba 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 searched and th
166bb 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 is trunk page is
166bc 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 the page.
166bd 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 ** to allocate
166be 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 , regardless of
166bf 77 68 65 74 68 65 72 20 69 74 20 68 61 73 20 6c whether it has l
166c0 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a eaves.. *
166c1 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
166c2 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b ( *pPgno==iTrunk
166c3 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 );. *ppP
166c4 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 age = pTrunk;.
166c5 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 searchList
166c6 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 = 0;. rc
166c7 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
166c8 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 rite(pTrunk->pDb
166c9 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Page);. i
166ca 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
166cb 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
166cc 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
166cd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
166ce 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 k==0 ){.
166cf 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 if( !pPrevTru
166d0 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 nk ){.
166d1 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 memcpy(&pPage1
166d2 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 ->aData[32], &pT
166d3 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
166d4 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 4);. }e
166d5 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
166d6 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 memcpy(&pPrevTr
166d7 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 unk->aData[0], &
166d8 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
166d9 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
166da 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
166db 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
166dc 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 e trunk page is
166dd 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
166de 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f caller but it co
166df 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 ntains .
166e0 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f ** pointers to
166e1 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 free-list leave
166e2 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 s. The first lea
166e3 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e f becomes a trun
166e4 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 k. ** p
166e5 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 age in this case
166e6 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
166e7 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 MemPage
166e8 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 *pNewTrunk;.
166e9 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 Pgno iNew
166ea 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
166eb 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
166ec 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 8]);. r
166ed 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
166ee 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 GetPage(pBt, iNe
166ef 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 wTrunk, &pNewTru
166f0 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 nk, 0);.
166f1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
166f2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
166f3 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
166f4 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
166f5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
166f6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
166f7 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e erWrite(pNewTrun
166f8 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
166f9 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
166fa 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
166fb 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
166fc 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a age(pNewTrunk);.
166fd 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
166fe 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
166ff 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ge;. }.
16700 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
16701 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 (&pNewTrunk->aDa
16702 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[0], &pTrunk->
16703 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
16704 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
16705 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 (&pNewTrunk->aDa
16706 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 ta[4], k-1);.
16707 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 memcpy(&p
16708 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b NewTrunk->aData[
16709 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 8], &pTrunk->aDa
1670a 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 ta[12], (k-1)*4)
1670b 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 ;. rele
1670c 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e asePage(pNewTrun
1670d 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 k);. if
1670e 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b ( !pPrevTrunk ){
1670f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 . ass
16710 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
16711 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
16712 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ge1->pDbPage) );
16713 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 . put
16714 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
16715 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 Data[32], iNewTr
16716 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 unk);.
16717 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
16718 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
16719 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54 agerWrite(pPrevT
1671a 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
1671b 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1671c 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 rc ){.
1671d 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
1671e 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
1671f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16720 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
16721 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 pPrevTrunk->aDat
16722 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 a[0], iNewTrunk)
16723 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
16724 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16725 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
16726 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
16727 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d CATE: %d trunk -
16728 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c %d free pages l
16729 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 eft\n", *pPgno,
1672a 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 n-1));.#endif.
1672b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1672c 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 /* Extract a
1672d 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 leaf from the tr
1672e 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 unk */. i
1672f 6e 74 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 nt closest;.
16730 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a Pgno iPage;.
16731 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 unsigned
16732 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 char *aData = p
16733 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 Trunk->aData;.
16734 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16735 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
16736 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
16737 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
16738 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
16739 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
1673a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
1673b 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e if( nearby>
1673c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 0 ){. i
1673d 6e 74 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20 nt i, dist;.
1673e 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
1673f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 0;. dis
16740 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 t = get4byte(&aD
16741 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 ata[8]) - nearby
16742 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
16743 64 69 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 dist<0 ) dist =
16744 2d 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 -dist;.
16745 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 for(i=1; i<k; i
16746 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
16747 20 69 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 int d2 = get4by
16748 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d te(&aData[8+i*4]
16749 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 ) - nearby;.
1674a 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 if( d2<0
1674b 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 ) d2 = -d2;.
1674c 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c if( d2<
1674d 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 dist ){.
1674e 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
1674f 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 i;.
16750 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 dist = d2;.
16751 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16752 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
16753 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 lse{. c
16754 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 losest = 0;.
16755 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 }.. i
16756 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
16757 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
16758 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 *4]);. if
16759 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c ( !searchList ||
1675a 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 iPage==nearby )
1675b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
1675c 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 noContent;.
1675d 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b Pgno nPage;
1675e 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e . *pPgn
1675f 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 o = iPage;.
16760 20 20 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 nPage = pag
16761 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
16762 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
16763 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a *pPgno>nPage ){.
16764 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
16765 72 65 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 ree page off the
16766 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
16767 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
16768 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
16769 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1676a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
1676b 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
1676c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1676d 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
1676e 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 CATE: %d was lea
1676f 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 f %d of %d on tr
16770 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 unk %d".
16771 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d ": %d m
16772 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e ore free pages\n
16773 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
16774 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 *pPgno, clos
16775 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b est+1, k, pTrunk
16776 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 ->pgno, n-1));.
16777 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f if( clo
16778 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 sest<k-1 ){.
16779 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1677a 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a aData[8+closest*
1677b 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 4], &aData[4+k*4
1677c 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 ], 4);.
1677d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 }. put
1677e 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 4byte(&aData[4],
1677f 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 k-1);.
16780 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16781 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
16782 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 (pTrunk->pDbPage
16783 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e ) );. n
16784 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 oContent = !btre
16785 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 eGetHasContent(p
16786 42 74 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 Bt, *pPgno);.
16787 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
16788 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
16789 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 pBt, *pPgno, ppP
1678a 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b age, noContent);
1678b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
1678c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1678d 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
1678e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1678f 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 te((*ppPage)->pD
16790 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16791 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
16792 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
16793 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
16794 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
16795 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
16796 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
16797 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b searchList = 0;
16798 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
16799 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 }. release
1679a 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 Page(pPrevTrunk)
1679b 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 ;. pPrevTru
1679c 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 nk = 0;. }whi
1679d 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 le( searchList )
1679e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
1679f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 * There are no p
167a0 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
167a1 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 list, so create
167a2 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 a new page at th
167a3 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 e. ** end of
167a4 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 the file */.
167a5 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 int nPage = page
167a6 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b rPagecount(pBt);
167a7 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 . *pPgno = nP
167a8 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 age + 1;.. if
167a9 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e ( *pPgno==PENDIN
167aa 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
167ab 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e ){. (*pPgn
167ac 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 o)++;. }..#if
167ad 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
167ae 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
167af 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
167b0 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 uum && PTRMAP_IS
167b1 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f PAGE(pBt, *pPgno
167b2 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 ) ){. /* If
167b3 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 *pPgno refers t
167b4 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 o a pointer-map
167b5 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 page, allocate t
167b6 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 wo new pages.
167b7 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ** at the end
167b8 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 of the file ins
167b9 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 tead of one. The
167ba 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 first allocated
167bb 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 page. ** b
167bc 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 ecomes a new poi
167bd 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 nter-map page, t
167be 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 he second is use
167bf 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e d by the caller.
167c0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
167c1 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 MemPage *pPg = 0
167c2 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 ;. TRACE(("
167c3 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f ALLOCATE: %d fro
167c4 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 m end of file (p
167c5 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 ointer-map page)
167c6 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 \n", *pPgno));.
167c7 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 assert( *pP
167c8 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 gno!=PENDING_BYT
167c9 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 E_PAGE(pBt) );.
167ca 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
167cb 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
167cc 74 2c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c t, *pPgno, &pPg,
167cd 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
167ce 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
167cf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
167d0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
167d1 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 Pg->pDbPage);.
167d2 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
167d3 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a e(pPg);. }.
167d4 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
167d5 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
167d6 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 (*pPgno)++;.
167d7 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 if( *pPgno==PE
167d8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
167d9 70 42 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 pBt) ){ (*pPgno)
167da 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 ++; }. }.#end
167db 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 if.. assert(
167dc 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f *pPgno!=PENDING_
167dd 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
167de 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
167df 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
167e0 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 Bt, *pPgno, ppPa
167e1 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ge, 0);. if(
167e2 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
167e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
167e4 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 PagerWrite((*ppP
167e5 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a age)->pDbPage);.
167e6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
167e7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
167e8 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
167e9 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 ge);. }. T
167ea 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a RACE(("ALLOCATE:
167eb 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 %d from end of
167ec 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 file\n", *pPgno)
167ed 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 );. }.. assert
167ee 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e ( *pPgno!=PENDIN
167ef 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
167f0 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 );..end_allocat
167f1 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 e_page:. releas
167f2 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 ePage(pTrunk);.
167f3 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 releasePage(pPr
167f4 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 evTrunk);. if(
167f5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
167f6 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
167f7 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
167f8 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 t((*ppPage)->pDb
167f9 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20 Page)>1 ){.
167fa 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 releasePage(*pp
167fb 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 Page);. ret
167fc 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
167fd 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 PT_BKPT;. }.
167fe 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 (*ppPage)->is
167ff 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Init = 0;. }.
16800 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
16801 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
16802 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 n is used to add
16803 20 70 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 page iPage to t
16804 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
16805 20 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 free-list. .**
16806 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
16807 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 6e at the page is n
16808 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 ot already a par
16809 74 20 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 t of the free-li
1680a 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 st..**.** The va
1680b 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 lue passed as th
1680c 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
1680d 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
1680e 6f 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a on is optional..
1680f 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 ** If the caller
16810 20 68 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 happens to have
16811 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
16812 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 e MemPage object
16813 20 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 .** correspondi
16814 6e 67 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 ng to page iPage
16815 20 68 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 handy, it may p
16816 61 73 73 20 69 74 20 61 73 20 74 68 65 20 73 65 ass it as the se
16817 63 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 cond value. .**
16818 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 Otherwise, it ma
16819 79 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a y pass NULL..**.
1681a 2a 2a 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 ** If a pointer
1681b 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a to a MemPage obj
1681c 65 63 74 20 69 73 20 70 61 73 73 65 64 20 61 73 ect is passed as
1681d 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
1681e 6d 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 ment,.** its ref
1681f 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 erence count is
16820 6e 6f 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 not altered by t
16821 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f his function..*/
16822 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 .static int free
16823 50 61 67 65 32 28 42 74 53 68 61 72 65 64 20 2a Page2(BtShared *
16824 70 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d pBt, MemPage *pM
16825 65 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 emPage, Pgno iPa
16826 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ge){. MemPage *
16827 70 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 pTrunk = 0;
16828 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 /* Fr
16829 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 ee-list trunk pa
1682a 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 ge */. Pgno iTr
1682b 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 unk = 0;
1682c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1682d 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 age number of fr
1682e 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 ee-list trunk pa
1682f 67 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 ge */ . MemPage
16830 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e *pPage1 = pBt->
16831 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 pPage1; /*
16832 4c 6f 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 Local reference
16833 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d to page 1 */. M
16834 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 emPage *pPage;
16835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16836 20 20 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 /* Page being
16837 20 66 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e freed. May be N
16838 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ULL. */. int rc
16839 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1683a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1683b 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a Return Code */.
1683c 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 int nFree;
1683d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1683e 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c /* Initial
1683f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
16840 20 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f on free-list */
16841 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
16842 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16843 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
16844 61 73 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 assert( iPage>1
16845 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d );. assert( !pM
16846 65 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 emPage || pMemPa
16847 67 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 ge->pgno==iPage
16848 29 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 );.. if( pMemPa
16849 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 ge ){. pPage
1684a 3d 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 = pMemPage;.
1684b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 sqlite3PagerRef(
1684c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
1684d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 . }else{. pP
1684e 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c age = btreePageL
1684f 6f 6f 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 ookup(pBt, iPage
16850 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 );. }.. /* Inc
16851 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 rement the free
16852 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 page count on pP
16853 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 age1 */. rc = s
16854 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16855 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 (pPage1->pDbPage
16856 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f );. if( rc ) go
16857 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
16858 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 . nFree = get4b
16859 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
1685a 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 ta[36]);. put4b
1685b 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
1685c 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 ta[36], nFree+1)
1685d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1685e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 _SECURE_DELETE.
1685f 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 /* If the SQLIT
16860 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 E_SECURE_DELETE
16861 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 compile-time opt
16862 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 ion is enabled,
16863 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 then. ** always
16864 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 fully overwrite
16865 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 deleted informa
16866 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e tion with zeros.
16867 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 . */. if( (!pP
16868 61 67 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c age && (rc = sql
16869 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
1686a 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 (pBt, iPage, &pP
1686b 61 67 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 age, 0))). ||
1686c 20 20 20 20 20 20 20 20 20 20 20 28 72 63 20 3d (rc =
1686d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1686e 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
1686f 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 e)). ){. got
16870 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
16871 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 }. memset(pPa
16872 67 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 ge->aData, 0, pP
16873 61 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 age->pBt->pageSi
16874 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f ze);.#endif.. /
16875 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
16876 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
16877 76 61 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e vacuum, write an
16878 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f entry in the po
16879 69 6e 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 inter-map. ** t
1687a 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 o indicate that
1687b 74 68 65 20 70 61 67 65 20 69 73 20 66 72 65 65 the page is free
1687c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 .. */. if( ISA
1687d 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
1687e 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 rc = ptrmapPut(
1687f 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d pBt, iPage, PTRM
16880 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b AP_FREEPAGE, 0);
16881 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
16882 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
16883 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d . }.. /* Now m
16884 61 6e 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 anipulate the ac
16885 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 tual database fr
16886 65 65 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 ee-list structur
16887 65 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f e. There are two
16888 0a 20 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 . ** possibilit
16889 69 65 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 ies. If the free
1688a 2d 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 -list is current
1688b 6c 79 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 ly empty, or if
1688c 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 the first. ** t
1688d 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 runk page in the
1688e 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 free-list is fu
1688f 6c 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 ll, then this pa
16890 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 ge will become a
16891 0a 20 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c . ** new free-l
16892 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
16893 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 Otherwise, it wi
16894 6c 6c 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 ll become a leaf
16895 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 of the. ** fir
16896 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e st trunk page in
16897 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 the current fre
16898 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f e-list. This blo
16899 63 6b 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 ck tests if it.
1689a 20 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 ** is possible
1689b 74 6f 20 61 64 64 20 74 68 65 20 70 61 67 65 20 to add the page
1689c 61 73 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 as a new free-li
1689d 73 74 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 st leaf.. */.
1689e 69 66 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a if( nFree!=0 ){.
1689f 20 20 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20 int nLeaf;
168a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
168a1 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 Initial number
168a2 6f 66 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e of leaf cells on
168a3 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a trunk page */..
168a4 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 iTrunk = get
168a5 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
168a6 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 Data[32]);. r
168a7 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
168a8 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
168a9 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
168aa 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
168ab 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
168ac 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
168ad 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e ut;. }.. n
168ae 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 Leaf = get4byte(
168af 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
168b0 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 ]);. if( nLea
168b1 66 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 f<0 ){. rc
168b2 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
168b3 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
168b4 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
168b5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c }. if( nL
168b6 65 61 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53 eaf<pBt->usableS
168b7 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 ize/4 - 8 ){.
168b8 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 /* In this ca
168b9 73 65 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d se there is room
168ba 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 on the trunk pa
168bb 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 ge to insert the
168bc 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 page. ** b
168bd 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 eing freed as a
168be 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 new leaf..
168bf 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 **. ** Note
168c0 20 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 that the trunk
168c1 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c page is not real
168c2 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 ly full until it
168c3 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 contains.
168c4 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 ** usableSize/4
168c5 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 - 2 entries, not
168c6 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
168c7 38 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 8 entries as we
168c8 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f have. ** co
168c9 64 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f ded. But due to
168ca 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 a coding error
168cb 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 in versions of S
168cc 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 QLite prior to.
168cd 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 ** 3.6.0, d
168ce 61 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72 atabases with fr
168cf 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 eelist trunk pag
168d0 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 es holding more
168d1 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 than. ** us
168d2 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 ableSize/4 - 8 e
168d3 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 ntries will be r
168d4 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 eported as corru
168d5 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 pt. In order.
168d6 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 ** to mainta
168d7 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d in backwards com
168d8 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 patibility with
168d9 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f older versions o
168da 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 f SQLite,.
168db 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 ** we will conta
168dc 69 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 in to restrict t
168dd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 he number of ent
168de 72 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 ries to usableSi
168df 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a ze/4 - 8. *
168e0 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 * for now. At s
168e1 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 ome point in the
168e2 20 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 future (once ev
168e3 65 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 eryone has upgra
168e4 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ded. ** to
168e5 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 3.6.0 or later)
168e6 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 we should consid
168e7 65 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f er fixing the co
168e8 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a nditional above.
168e9 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 ** to read
168ea 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 "usableSize/4-2
168eb 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 " instead of "us
168ec 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 ableSize/4-8"..
168ed 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
168ee 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
168ef 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 rite(pTrunk->pDb
168f0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
168f1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
168f2 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 {. put4by
168f3 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 te(&pTrunk->aDat
168f4 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a a[4], nLeaf+1);.
168f5 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
168f6 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
168f7 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 8+nLeaf*4], iPag
168f8 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e);.#ifndef SQLI
168f9 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TE_SECURE_DELETE
168fa 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
168fb 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ge ){.
168fc 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
168fd 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
168fe 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d Page);. }
168ff 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
16900 72 63 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 rc = btreeSetHas
16901 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 Content(pBt, iPa
16902 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ge);. }.
16903 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 2d TRACE(("FREE-
16904 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e PAGE: %d leaf on
16905 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e trunk page %d\n
16906 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 ",pPage->pgno,pT
16907 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 runk->pgno));.
16908 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 goto freepag
16909 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d e_out;. }. }
1690a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f .. /* If contro
1690b 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 l flows to this
1690c 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 point, then it w
1690d 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 as not possible
1690e 74 6f 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 to add the. **
1690f 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 the page being f
16910 72 65 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 reed as a leaf p
16911 61 67 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 age of the first
16912 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 trunk in the fr
16913 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f ee-list.. ** Po
16914 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 ssibly because t
16915 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 he free-list is
16916 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 empty, or possib
16917 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20 0a ly because the .
16918 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b ** first trunk
16919 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
1691a 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 t is full. Eithe
1691b 72 20 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 r way, the page
1691c 62 65 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a being freed. **
1691d 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 will become the
1691e 20 6e 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b new first trunk
1691f 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 page in the fre
16920 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 e-list.. */. i
16921 66 28 20 20 20 28 28 21 70 50 61 67 65 29 20 26 f( ((!pPage) &
16922 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 & (0 != (rc = sq
16923 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
16924 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 e(pBt, iPage, &p
16925 50 61 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20 Page, 0)))).
16926 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 || (0 != (rc =
16927 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16928 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16929 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 ))). ){. got
1692a 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
1692b 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 }. put4byte(p
1692c 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 Page->aData, iTr
1692d 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 unk);. put4byte
1692e 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 (&pPage->aData[4
1692f 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 ], 0);. put4byt
16930 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
16931 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 [32], iPage);.
16932 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
16933 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 E: %d new trunk
16934 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 page replacing %
16935 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e d\n", pPage->pgn
16936 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 o, iTrunk));..fr
16937 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 eepage_out:. if
16938 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 ( pPage ){. p
16939 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
1693a 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 ;. }. releaseP
1693b 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
1693c 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b leasePage(pTrunk
1693d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1693e 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 }.static int fre
1693f 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 ePage(MemPage *p
16940 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 Page){. return
16941 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 2d freePage2(pPage-
16942 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 >pBt, pPage, pPa
16943 67 65 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a ge->pgno);.}../*
16944 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 .** Free any ove
16945 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f rflow pages asso
16946 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
16947 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 given Cell..*/.s
16948 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 tatic int clearC
16949 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ell(MemPage *pPa
1694a 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ge, unsigned cha
1694b 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 r *pCell){. BtS
1694c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 hared *pBt = pPa
1694d 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 ge->pBt;. CellI
1694e 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f nfo info;. Pgno
1694f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 ovflPgno;. int
16950 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c rc;. int nOvfl
16951 3b 0a 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 ;. u16 ovflPage
16952 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Size;.. assert(
16953 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16954 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
16955 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
16956 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
16957 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
16958 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 l, &info);. if(
16959 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d info.iOverflow=
1695a 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1695b 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 SQLITE_OK; /*
1695c 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 No overflow page
1695d 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 s. Return withou
1695e 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 t doing anything
1695f 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 */. }. ovflPg
16960 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
16961 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
16962 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 low]);. assert(
16963 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16964 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 > 4 );. ovflPa
16965 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 geSize = pBt->us
16966 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 ableSize - 4;.
16967 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 nOvfl = (info.nP
16968 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c ayload - info.nL
16969 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 ocal + ovflPageS
1696a 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 ize - 1)/ovflPag
1696b 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 eSize;. assert(
1696c 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 ovflPgno==0 ||
1696d 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 nOvfl>0 );. whi
1696e 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 le( nOvfl-- ){.
1696f 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 Pgno iNext =
16970 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 0;. MemPage *
16971 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 pOvfl = 0;. i
16972 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c f( ovflPgno<2 ||
16973 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 ovflPgno>pagerP
16974 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b agecount(pBt) ){
16975 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e . /* 0 is n
16976 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20 ot a legal page
16977 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 number and page
16978 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 1 cannot be an .
16979 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f ** overflo
1697a 77 20 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72 w page. Therefor
1697b 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 e if ovflPgno<2
1697c 6f 72 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 or past the end
1697d 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a of the . **
1697e 20 66 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 file the databa
1697f 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 se must be corru
16980 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 pt. */. ret
16981 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16982 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 PT_BKPT;. }.
16983 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a if( nOvfl ){.
16984 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 rc = getOv
16985 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 erflowPage(pBt,
16986 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c ovflPgno, &pOvfl
16987 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 , &iNext);.
16988 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
16989 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rc;. }. r
1698a 63 20 3d 20 66 72 65 65 50 61 67 65 32 28 70 42 c = freePage2(pB
1698b 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 t, pOvfl, ovflPg
1698c 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 76 no);. if( pOv
1698d 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 fl ){. sqli
1698e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f te3PagerUnref(pO
1698f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 vfl->pDbPage);.
16990 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 }. if( rc
16991 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
16992 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 ovflPgno = iNex
16993 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
16994 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
16995 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 62 .** Create the b
16996 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 73 65 yte sequence use
16997 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 d to represent a
16998 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 cell on page pP
16999 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 age.** and write
1699a 20 74 68 61 74 20 62 79 74 65 20 73 65 71 75 65 that byte seque
1699b 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d nce into pCell[]
1699c 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 . Overflow page
1699d 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 s are.** allocat
1699e 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e ed and filled in
1699f 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 20 as necessary.
169a0 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 The calling proc
169a1 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 edure.** is resp
169a2 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 onsible for maki
169a3 6e 67 20 73 75 72 65 20 73 75 66 66 69 63 69 65 ng sure sufficie
169a4 6e 74 20 73 70 61 63 65 20 68 61 73 20 62 65 65 nt space has bee
169a5 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 n allocated.** f
169a6 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a or pCell[]..**.*
169a7 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43 65 6c * Note that pCel
169a8 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 l does not neces
169a9 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 sary need to poi
169aa 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67 65 2d nt to the pPage-
169ab 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 >aData.** area.
169ac 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 pCell might poi
169ad 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f nt to some tempo
169ae 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20 20 54 rary storage. T
169af 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 he cell will.**
169b0 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 be constructed i
169b1 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 n this temporary
169b2 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70 69 65 area then copie
169b3 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 d into pPage->aD
169b4 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f ata.** later..*/
169b5 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c .static int fill
169b6 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 InCell(. MemPag
169b7 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
169b8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
169b9 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 page that contai
169ba 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 ns the cell */.
169bb 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
169bc 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 pCell,
169bd 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
169be 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a of the cell */.
169bf 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
169c0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 ey, i64 nKey,
169c1 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 /* The key */.
169c2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 const void *pDa
169c3 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 ta,int nData,
169c4 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 /* The data */.
169c5 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 int nZero,
169c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169c7 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79 /* Extra zero by
169c8 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f tes to append to
169c9 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 pData */. int
169ca 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20 *pnSize
169cb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
169cc 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 ite cell size he
169cd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e re */.){. int n
169ce 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 Payload;. const
169cf 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 u8 *pSrc;. int
169d0 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 nSrc, n, rc;.
169d1 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 int spaceLeft;.
169d2 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 MemPage *pOvfl
169d3 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
169d4 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a pToRelease = 0;.
169d5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
169d6 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 *pPrior;. unsig
169d7 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79 6c 6f ned char *pPaylo
169d8 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ad;. BtShared *
169d9 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 pBt = pPage->pBt
169da 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 ;. Pgno pgnoOvf
169db 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 l = 0;. int nHe
169dc 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f ader;. CellInfo
169dd 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 info;.. assert
169de 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
169df 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
169e0 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a >mutex) );.. /*
169e1 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 pPage is not ne
169e2 63 65 73 73 61 72 69 6c 79 20 77 72 69 74 65 61 cessarily writea
169e3 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 ble since pCell
169e4 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c 69 61 might be auxilia
169e5 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 ry. ** buffer s
169e6 70 61 63 65 20 74 68 61 74 20 69 73 20 73 65 70 pace that is sep
169e7 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70 arate from the p
169e8 50 61 67 65 20 62 75 66 66 65 72 20 61 72 65 61 Page buffer area
169e9 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 */. assert( pC
169ea 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 ell<pPage->aData
169eb 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 || pCell>=&pPag
169ec 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 e->aData[pBt->pa
169ed 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 geSize].
169ee 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 || sqlite3Pa
169ef 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
169f0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
169f1 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 ;.. /* Fill in
169f2 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 the header. */.
169f3 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 nHeader = 0;.
169f4 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
169f5 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 ){. nHeader
169f6 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 += 4;. }. if(
169f7 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
169f8 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d {. nHeader +=
169f9 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c putVarint(&pCel
169fa 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 l[nHeader], nDat
169fb 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 a+nZero);. }els
169fc 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e e{. nData = n
169fd 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Zero = 0;. }.
169fe 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 nHeader += putVa
169ff 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 rint(&pCell[nHea
16a00 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b der], *(u64*)&nK
16a01 65 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ey);. sqlite3Bt
16a02 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
16a03 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
16a04 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nfo);. assert(
16a05 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 info.nHeader==nH
16a06 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 eader );. asser
16a07 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b t( info.nKey==nK
16a08 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
16a09 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 info.nData==(u32
16a0a 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 )(nData+nZero) )
16a0b 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 ;. . /* Fill i
16a0c 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f n the payload */
16a0d 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 . nPayload = nD
16a0e 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 ata + nZero;. i
16a0f 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
16a10 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 ){. pSrc = p
16a11 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d Data;. nSrc =
16a12 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 nData;. nDat
16a13 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 a = 0;. }else{
16a14 0a 20 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 78 . if( nKey>0x
16a15 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 79 7fffffff || pKey
16a16 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
16a17 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16a18 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 PT;. }. nP
16a19 61 79 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e ayload += (int)n
16a1a 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 Key;. pSrc =
16a1b 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d pKey;. nSrc =
16a1c 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a (int)nKey;. }.
16a1d 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f *pnSize = info
16a1e 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c .nSize;. spaceL
16a1f 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 eft = info.nLoca
16a20 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 l;. pPayload =
16a21 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b &pCell[nHeader];
16a22 0a 20 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65 . pPrior = &pCe
16a23 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
16a24 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50 w];.. while( nP
16a25 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 ayload>0 ){.
16a26 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 if( spaceLeft==0
16a27 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
16a28 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
16a29 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 UM. Pgno pg
16a2a 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f noPtrmap = pgnoO
16a2b 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 vfl; /* Overflow
16a2c 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 page pointer-ma
16a2d 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a p entry page */.
16a2e 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 if( pBt->a
16a2f 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
16a30 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 do{.
16a31 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 pgnoOvfl++;.
16a32 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 } while(
16a33 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 . PTRMA
16a34 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 P_ISPAGE(pBt, pg
16a35 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f noOvfl) || pgnoO
16a36 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 vfl==PENDING_BYT
16a37 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 E_PAGE(pBt) .
16a38 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a );. }.
16a39 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 #endif. rc
16a3a 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
16a3b 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c age(pBt, &pOvfl,
16a3c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f &pgnoOvfl, pgno
16a3d 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 Ovfl, 0);.#ifnde
16a3e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16a3f 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f TOVACUUM. /
16a40 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
16a41 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
16a42 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 vacuum, and the
16a43 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 second or subseq
16a44 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 uent. ** ov
16a45 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 erflow page is b
16a46 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 eing allocated,
16a47 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 add an entry to
16a48 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
16a49 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 ** for tha
16a4a 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 t page now. .
16a4b 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 **. ** I
16a4c 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
16a4d 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 rst overflow pag
16a4e 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 e, then write a
16a4f 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 partial entry .
16a50 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 ** to the p
16a51 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 ointer-map. If w
16a52 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 e write nothing
16a53 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d to this pointer-
16a54 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 map slot,.
16a55 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 ** then the opti
16a56 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 mistic overflow
16a57 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 chain processing
16a58 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a in clearCell().
16a59 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 ** may mis
16a5a 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75 6e interpret the un
16a5b 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75 initialised valu
16a5c 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 es and delete th
16a5d 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 e. ** wrong
16a5e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
16a5f 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 database..
16a60 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 */. if( pBt
16a61 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
16a62 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
16a63 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79 70 . u8 eTyp
16a64 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f e = (pgnoPtrmap?
16a65 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
16a66 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 :PTRMAP_OVERFLOW
16a67 31 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 1);. rc =
16a68 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
16a69 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c pgnoOvfl, eType,
16a6a 20 70 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20 20 pgnoPtrmap);.
16a6b 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
16a6c 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
16a6d 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 ePage(pOvfl);.
16a6e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
16a6f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 #endif. if(
16a70 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 rc ){. r
16a71 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 eleasePage(pToRe
16a72 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 lease);.
16a73 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
16a74 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
16a75 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f pToRelease is no
16a76 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 t zero than pPri
16a77 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 or points into t
16a78 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 he data area.
16a79 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 ** of pToRele
16a7a 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ase. Make sure
16a7b 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 pToRelease is st
16a7c 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a ill writeable. *
16a7d 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
16a7e 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c pToRelease==0 ||
16a7f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
16a80 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 riteable(pToRele
16a81 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ase->pDbPage) );
16a82 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 .. /* If pP
16a83 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 rior is part of
16a84 74 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 the data area of
16a85 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b pPage, then mak
16a86 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 e sure pPage.
16a87 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 ** is still w
16a88 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 riteable */.
16a89 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 assert( pPrior
16a8a 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c <pPage->aData ||
16a8b 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d pPrior>=&pPage-
16a8c 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 >aData[pBt->page
16a8d 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 Size].
16a8e 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 || sqlite3Page
16a8f 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
16a90 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
16a91 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
16a92 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c pPrior, pgnoOvfl
16a93 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
16a94 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
16a95 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 ;. pToRelea
16a96 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 se = pOvfl;.
16a97 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c pPrior = pOvfl
16a98 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 ->aData;. p
16a99 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 ut4byte(pPrior,
16a9a 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 0);. pPaylo
16a9b 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 ad = &pOvfl->aDa
16a9c 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 ta[4];. spa
16a9d 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 ceLeft = pBt->us
16a9e 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 ableSize - 4;.
16a9f 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 }. n = nPay
16aa0 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e load;. if( n>
16aa1 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 spaceLeft ) n =
16aa2 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 spaceLeft;..
16aa3 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 /* If pToRelease
16aa4 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 is not zero tha
16aa5 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 n pPayload point
16aa6 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 s into the data
16aa7 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 area. ** of p
16aa8 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 ToRelease. Make
16aa9 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 sure pToRelease
16aaa 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
16aab 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 ble. */. asse
16aac 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d rt( pToRelease==
16aad 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 0 || sqlite3Page
16aae 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f rIswriteable(pTo
16aaf 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 Release->pDbPage
16ab0 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 ) );.. /* If
16ab1 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 pPayload is part
16ab2 20 6f 66 20 74 68 65 20 64 61 74 61 20 61 72 65 of the data are
16ab3 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e a of pPage, then
16ab4 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 make sure pPage
16ab5 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c . ** is still
16ab6 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 writeable */.
16ab7 20 20 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f assert( pPaylo
16ab8 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 ad<pPage->aData
16ab9 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 || pPayload>=&pP
16aba 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
16abb 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 pageSize].
16abc 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
16abd 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
16abe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
16abf 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 );.. if( nSr
16ac0 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 c>0 ){. if(
16ac1 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 n>nSrc ) n = nS
16ac2 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 rc;. assert
16ac3 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 ( pSrc );.
16ac4 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c memcpy(pPayload,
16ac5 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d pSrc, n);. }
16ac6 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 else{. mems
16ac7 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 et(pPayload, 0,
16ac8 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 n);. }. nP
16ac9 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 ayload -= n;.
16aca 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a pPayload += n;.
16acb 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 pSrc += n;.
16acc 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 nSrc -= n;.
16acd 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e spaceLeft -= n
16ace 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d ;. if( nSrc==
16acf 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 0 ){. nSrc
16ad0 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 = nData;. p
16ad1 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 Src = pData;.
16ad2 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 }. }. release
16ad3 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
16ad4 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
16ad5 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
16ad6 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 emove the i-th c
16ad7 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 ell from pPage.
16ad8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 This routine ef
16ad9 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 fects pPage only
16ada 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f ..** The cell co
16adb 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 ntent is not fre
16adc 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 ed or deallocate
16add 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 d. It is assume
16ade 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 d that.** the ce
16adf 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 ll content has b
16ae0 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 een copied somep
16ae1 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 lace else. This
16ae2 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a routine just.**
16ae3 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 removes the ref
16ae4 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 erence to the ce
16ae5 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a ll from pPage..*
16ae6 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 *.** "sz" must b
16ae7 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
16ae8 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
16ae9 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
16aea 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 dropCell(MemPag
16aeb 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 e *pPage, int id
16aec 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e x, int sz){. in
16aed 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t i; /*
16aee 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
16aef 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 . int pc;
16af0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 /* Offset to
16af1 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 cell content of
16af2 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 cell being delet
16af3 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 ed */. u8 *data
16af4 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 ; /* pPage
16af5 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 ->aData */. u8
16af6 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 *ptr; /*
16af7 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 Used to move byt
16af8 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e es around within
16af9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 data[] */. int
16afa 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 rc; /*
16afb 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 The return code
16afc 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 */.. assert( id
16afd 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 x>=0 && idx<pPag
16afe 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 e->nCell );. as
16aff 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 sert( sz==cellSi
16b00 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 ze(pPage, idx) )
16b01 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
16b02 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
16b03 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
16b04 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
16b05 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16b06 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
16b07 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 mutex) );. data
16b08 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
16b09 0a 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 . ptr = &data[p
16b0a 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
16b0b 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 + 2*idx];. pc
16b0c 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b = get2byte(ptr);
16b0d 0a 20 20 69 66 28 20 28 70 63 3c 70 50 61 67 65 . if( (pc<pPage
16b0e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 ->hdrOffset+6+(p
16b0f 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 29 Page->leaf?0:4))
16b10 0a 20 20 20 20 20 7c 7c 20 28 70 63 2b 73 7a 3e . || (pc+sz>
16b11 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
16b12 6c 65 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 72 leSize) ){. r
16b13 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
16b14 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
16b15 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 rc = freeSpace(
16b16 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a pPage, pc, sz);.
16b17 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16b18 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
16b19 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 n rc;. }. for(
16b1a 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 i=idx+1; i<pPage
16b1b 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 ->nCell; i++, pt
16b1c 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 r+=2){. ptr[0
16b1d 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 ] = ptr[2];.
16b1e 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b ptr[1] = ptr[3];
16b1f 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 . }. pPage->nC
16b20 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 ell--;. put2byt
16b21 65 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 e(&data[pPage->h
16b22 64 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 drOffset+3], pPa
16b23 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 ge->nCell);. pP
16b24 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b age->nFree += 2;
16b25 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
16b26 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e _OK;.}../*.** In
16b27 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 sert a new cell
16b28 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c on pPage at cell
16b29 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 index "i". pCe
16b2a 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 ll points to the
16b2b 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 .** content of t
16b2c 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 he cell..**.** I
16b2d 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 f the cell conte
16b2e 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 nt will fit on t
16b2f 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 he page, then pu
16b30 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 t it there. If
16b31 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 it.** will not f
16b32 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 it, then make a
16b33 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c copy of the cell
16b34 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 content into pT
16b35 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 emp if.** pTemp
16b36 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 is not null. Re
16b37 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d gardless of pTem
16b38 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 p, allocate a ne
16b39 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 w entry.** in pP
16b3a 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 age->aOvfl[] and
16b3b 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 make it point t
16b3c 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
16b3d 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e nt (either.** in
16b3e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 pTemp or the or
16b3f 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e iginal pCell) an
16b40 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 d also record it
16b41 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c s index. .** All
16b42 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e ocating a new en
16b43 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 try in pPage->aC
16b44 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 ell[] implies th
16b45 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f at .** pPage->nO
16b46 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 verflow is incre
16b47 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mented..**.** If
16b48 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 nSkip is non-ze
16b49 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 ro, then do not
16b4a 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e copy the first n
16b4b 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 Skip bytes of th
16b4c 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 e.** cell. The c
16b4d 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 aller will overw
16b4e 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 rite them after
16b4f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 this function re
16b50 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b turns. If.** nSk
16b51 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 ip is non-zero,
16b52 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e then pCell may n
16b53 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 ot point to an i
16b54 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f nvalid memory lo
16b55 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 cation .** (but
16b56 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 pCell+nSkip is a
16b57 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f lways valid)..*/
16b58 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65 .static int inse
16b59 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 rtCell(. MemPag
16b5a 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 e *pPage, /* P
16b5b 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 age into which w
16b5c 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f e are copying */
16b5d 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 . int i,
16b5e 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c /* New cell
16b5f 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 becomes the i-t
16b60 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 h cell of the pa
16b61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c ge */. u8 *pCel
16b62 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e l, /* Con
16b63 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 tent of the new
16b64 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a cell */. int sz
16b65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 , /* B
16b66 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 ytes of content
16b67 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 in pCell */. u8
16b68 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 *pTemp,
16b69 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 /* Temp storage
16b6a 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c space for pCell,
16b6b 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 if needed */.
16b6c 75 38 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20 u8 nSkip
16b6d 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 /* Do not writ
16b6e 65 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 e the first nSki
16b6f 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 p bytes of the c
16b70 65 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ell */.){. int
16b71 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a idx; /*
16b72 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 Where to write
16b73 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 new cell content
16b74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 in data[] */.
16b75 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 int j;
16b76 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
16b77 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 r */. int top;
16b78 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
16b79 74 20 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e t byte of conten
16b7a 74 20 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69 t for any cell i
16b7b 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e n data[] */. in
16b7c 74 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 t end;
16b7d 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 /* First byte pa
16b7e 73 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c st the last cell
16b7f 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 pointer in data
16b80 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b [] */. int ins;
16b81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
16b82 65 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 ex in data[] whe
16b83 72 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e re new cell poin
16b84 74 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 ter is inserted
16b85 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 */. int hdr;
16b86 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
16b87 20 69 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20 into data[] of
16b88 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 the page header
16b89 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 */. int cellOff
16b8a 73 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 set; /* Addres
16b8b 73 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 s of first cell
16b8c 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b pointer in data[
16b8d 5d 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b ] */. u8 *data;
16b8e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
16b8f 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 content of the w
16b90 68 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 hole page */. u
16b91 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 8 *ptr;
16b92 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 /* Used for mov
16b93 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ing information
16b94 61 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d around in data[]
16b95 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
16b96 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d >=0 && i<=pPage-
16b97 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f >nCell+pPage->nO
16b98 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 verflow );. ass
16b99 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ert( pPage->nCel
16b9a 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 l<=MX_CELL(pPage
16b9b 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c ->pBt) && MX_CEL
16b9c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 L(pPage->pBt)<=5
16b9d 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 460 );. assert(
16b9e 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
16b9f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 70 50 61 w<=ArraySize(pPa
16ba0 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20 ge->aOvfl) );.
16ba1 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c assert( sz==cell
16ba2 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 SizePtr(pPage, p
16ba3 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Cell) );. asser
16ba4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16ba5 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
16ba6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
16ba7 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
16ba8 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65 ow || sz+2>pPage
16ba9 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 69 ->nFree ){. i
16baa 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20 20 20 f( pTemp ){.
16bab 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e memcpy(pTemp+n
16bac 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 Skip, pCell+nSki
16bad 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 p, sz-nSkip);.
16bae 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d pCell = pTem
16baf 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d p;. }. j =
16bb0 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
16bb1 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 w++;. assert(
16bb2 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 j<(int)(sizeof(
16bb3 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 pPage->aOvfl)/si
16bb4 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 zeof(pPage->aOvf
16bb5 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 l[0])) );. pP
16bb6 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 age->aOvfl[j].pC
16bb7 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 ell = pCell;.
16bb8 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d pPage->aOvfl[j]
16bb9 2e 69 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 .idx = (u16)i;.
16bba 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 pPage->nFree
16bbb 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
16bbc 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
16bbd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
16bbe 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
16bbf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16bc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
16bc1 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
16bc2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16bc3 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
16bc4 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16bc5 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 ) );. data =
16bc6 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 pPage->aData;.
16bc7 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 hdr = pPage->h
16bc8 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 74 6f drOffset;. to
16bc9 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
16bca 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 ta[hdr+5]);.
16bcb 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 cellOffset = pPa
16bcc 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a ge->cellOffset;.
16bcd 20 20 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 end = cellOf
16bce 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e fset + 2*pPage->
16bcf 6e 43 65 6c 6c 20 2b 20 32 3b 0a 20 20 20 20 69 nCell + 2;. i
16bd0 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 ns = cellOffset
16bd1 2b 20 32 2a 69 3b 0a 20 20 20 20 69 66 28 20 65 + 2*i;. if( e
16bd2 6e 64 20 3e 20 74 6f 70 20 2d 20 73 7a 20 29 7b nd > top - sz ){
16bd3 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 66 72 . rc = defr
16bd4 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 agmentPage(pPage
16bd5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
16bd6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16bd7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16bd8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 . }. t
16bd9 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 op = get2byte(&d
16bda 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 ata[hdr+5]);.
16bdb 20 20 20 61 73 73 65 72 74 28 20 65 6e 64 20 2b assert( end +
16bdc 20 73 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20 sz <= top );.
16bdd 20 20 7d 0a 20 20 20 20 69 64 78 20 3d 20 61 6c }. idx = al
16bde 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 locateSpace(pPag
16bdf 65 2c 20 73 7a 29 3b 0a 20 20 20 20 61 73 73 65 e, sz);. asse
16be0 72 74 28 20 69 64 78 3e 30 20 29 3b 0a 20 20 20 rt( idx>0 );.
16be1 20 61 73 73 65 72 74 28 20 65 6e 64 20 3c 3d 20 assert( end <=
16be2 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
16be3 64 72 2b 35 5d 29 20 29 3b 0a 20 20 20 20 69 66 dr+5]) );. if
16be4 20 28 69 64 78 2b 73 7a 20 3e 20 70 50 61 67 65 (idx+sz > pPage
16be5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
16be6 65 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e) {. retur
16be7 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
16be8 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
16be9 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b pPage->nCell++;
16bea 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 . pPage->nFre
16beb 65 20 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63 e -= 2;. memc
16bec 70 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b py(&data[idx+nSk
16bed 69 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 ip], pCell+nSkip
16bee 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 , sz-nSkip);.
16bef 20 66 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74 for(j=end-2, pt
16bf0 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e r=&data[j]; j>in
16bf1 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 s; j-=2, ptr-=2)
16bf2 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d {. ptr[0] =
16bf3 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 ptr[-2];.
16bf4 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d ptr[1] = ptr[-1]
16bf5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32 ;. }. put2
16bf6 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c byte(&data[ins],
16bf7 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62 idx);. put2b
16bf8 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
16bf9 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
16bfa 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
16bfb 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
16bfc 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 if( pPage->p
16bfd 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
16bfe 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 {. /* The c
16bff 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 ell may contain
16c00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
16c01 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 overflow page. I
16c02 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 f so, write.
16c03 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 ** the entry f
16c04 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
16c05 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f page into the po
16c06 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 inter map..
16c07 20 2a 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e */. CellIn
16c08 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 fo info;. s
16c09 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
16c0a 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
16c0b 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
16c0c 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 assert( (inf
16c0d 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e o.nData+(pPage->
16c0e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b intKey?0:info.nK
16c0f 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c ey))==info.nPayl
16c10 6f 61 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 oad );. if(
16c11 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 info.iOverflow
16c12 29 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 ){. Pgno
16c13 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 pgnoOvfl = get4b
16c14 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e yte(&pCell[info.
16c15 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 iOverflow]);.
16c16 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
16c17 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 Put(pPage->pBt,
16c18 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 pgnoOvfl, PTRMAP
16c19 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 _OVERFLOW1, pPag
16c1a 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 e->pgno);.
16c1b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16c1c 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
16c1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
16c1e 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 endif. }.. ret
16c1f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16c20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 ../*.** Add a li
16c21 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 st of cells to a
16c22 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 page. The page
16c23 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 should be initi
16c24 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 ally empty..** T
16c25 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 he cells are gua
16c26 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f ranteed to fit o
16c27 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 n the page..*/.s
16c28 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d tatic void assem
16c29 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 blePage(. MemPa
16c2a 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 ge *pPage, /*
16c2b 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 The page to be a
16c2c 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 ssemblied */. i
16c2d 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 nt nCell,
16c2e 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f /* The number o
16c2f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 f cells to add t
16c30 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 o this page */.
16c31 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 u8 **apCell,
16c32 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 /* Pointers t
16c33 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f o cell bodies */
16c34 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 . u16 *aSize
16c35 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 /* Sizes of
16c36 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b the cells */.){
16c37 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
16c38 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
16c39 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43 nter */. u8 *pC
16c3a 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41 ellptr; /* A
16c3b 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 ddress of next c
16c3c 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
16c3d 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 int cellbody;
16c3e 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
16c3f 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 next cell body
16c40 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 */. u8 * const
16c41 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
16c42 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
16c43 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 /* Pointer to d
16c44 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f ata for pPage */
16c45 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 . const int hdr
16c46 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
16c47 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f set; /
16c48 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 * Offset of head
16c49 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 er on pPage */.
16c4a 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62 const int nUsab
16c4b 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d le = pPage->pBt-
16c4c 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 >usableSize; /*
16c4d 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70 Usable size of p
16c4e 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 age */.. assert
16c4f 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
16c50 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ow==0 );. asser
16c51 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16c52 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
16c53 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
16c54 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 sert( nCell>=0 &
16c55 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c & nCell<=MX_CELL
16c56 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 (pPage->pBt) &&
16c57 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
16c58 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 Bt)<=5460 );. a
16c59 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
16c5a 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
16c5b 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
16c5c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
16c5d 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20 at the page has
16c5e 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 just been zeroed
16c5f 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a by zeroPage() *
16c60 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
16c61 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 e->nCell==0 );.
16c62 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74 assert( get2byt
16c63 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d e(&data[hdr+5])=
16c64 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 =nUsable );.. p
16c65 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b Cellptr = &data[
16c66 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
16c67 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 t + nCell*2];.
16c68 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 cellbody = nUsab
16c69 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c le;. for(i=nCel
16c6a 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b l-1; i>=0; i--){
16c6b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d . pCellptr -=
16c6c 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79 2;. cellbody
16c6d 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 -= aSize[i];.
16c6e 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c put2byte(pCell
16c6f 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a ptr, cellbody);.
16c70 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
16c71 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 [cellbody], apCe
16c72 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 ll[i], aSize[i])
16c73 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 ;. }. put2byte
16c74 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e (&data[hdr+3], n
16c75 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 Cell);. put2byt
16c76 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
16c77 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 cellbody);. pPa
16c78 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 ge->nFree -= (nC
16c79 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 ell*2 + nUsable
16c7a 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 - cellbody);. p
16c7b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 Page->nCell = (u
16c7c 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 16)nCell;.}../*.
16c7d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
16c7e 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 parameters dete
16c7f 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 rmine how many a
16c80 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 djacent pages ge
16c81 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e t involved.** in
16c82 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 a balancing ope
16c83 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 ration. NN is t
16c84 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 he number of nei
16c85 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
16c86 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 side.** of the
16c87 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 page that partic
16c88 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
16c89 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
16c8a 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 . NB is the.**
16c8b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
16c8c 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 pages that parti
16c8d 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e cipate, includin
16c8e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 g the target pag
16c8f 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 e and.** NN neig
16c90 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
16c91 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 side..**.** The
16c92 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 minimum value of
16c93 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 NN is 1 (of cou
16c94 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e rse). Increasin
16c95 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 g NN above 1.**
16c96 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 (to 2 or 3) give
16c97 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f s a modest impro
16c98 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 vement in SELECT
16c99 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 and DELETE perf
16c9a 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 ormance.** in ex
16c9b 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 change for a lar
16c9c 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 ger degradation
16c9d 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 in INSERT and UP
16c9e 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 DATE performance
16c9f 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
16ca0 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 f NN appears to
16ca1 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 give the best re
16ca2 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a sults overall..*
16ca3 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 /.#define NN 1
16ca4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
16ca5 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
16ca6 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
16ca7 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 of pPage */.#de
16ca8 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 fine NB (NN*2+1)
16ca9 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 /* Total p
16caa 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e ages involved in
16cab 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a the balance */.
16cac 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 ./* Forward refe
16cad 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 rence */.static
16cae 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 int balance(BtCu
16caf 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 rsor*, int);..#i
16cb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16cb1 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f T_QUICKBALANCE./
16cb2 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
16cb3 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 n of balance() h
16cb4 61 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f andles the commo
16cb5 6e 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 n special case w
16cb6 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e here.** a new en
16cb7 74 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 try is being ins
16cb8 65 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 erted on the ext
16cb9 72 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f reme right-end o
16cba 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 f the.** tree, i
16cbb 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 n other words, w
16cbc 68 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 hen the new entr
16cbd 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 y will become th
16cbe 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 e largest.** ent
16cbf 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a ry in the tree..
16cc0 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 **.** Instead of
16cc1 20 74 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 trying balance
16cc2 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 the 3 right-most
16cc3 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 leaf pages, jus
16cc4 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 t add.** a new p
16cc5 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 age to the right
16cc6 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 -hand side and p
16cc7 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 ut the one new e
16cc8 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 ntry in.** that
16cc9 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 page. This leav
16cca 65 73 20 74 68 65 20 72 69 67 68 74 20 73 69 64 es the right sid
16ccb 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f e of the tree so
16ccc 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 mewhat.** unbala
16ccd 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 nced. But odds
16cce 61 72 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c are that we will
16ccf 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 be inserting ne
16cd0 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 w entries.** at
16cd1 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 the end soon aft
16cd2 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e erwards so the n
16cd3 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 early empty page
16cd4 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a will quickly.**
16cd5 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 fill up. On av
16cd6 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 erage..**.** pPa
16cd7 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 ge is the leaf p
16cd8 61 67 65 20 77 68 69 63 68 20 69 73 20 74 68 65 age which is the
16cd9 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 right-most page
16cda 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a in the tree..**
16cdb 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 pParent is its
16cdc 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d parent. pPage m
16cdd 75 73 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c ust have a singl
16cde 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 e overflow entry
16cdf 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 .** which is als
16ce0 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 o the right-most
16ce1 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 entry on the pa
16ce2 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ge..*/.static in
16ce3 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 t balance_quick(
16ce4 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
16ce5 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
16ce6 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a Page *pNew = 0;.
16ce7 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a Pgno pgnoNew;.
16ce8 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 u8 *pCell;. u
16ce9 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 16 szCell;. Cel
16cea 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65 lInfo info;. Me
16ceb 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 mPage *pPage = p
16cec 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16ced 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50 ->iPage];. MemP
16cee 61 67 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 age *pParent = p
16cef 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16cf0 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74 ->iPage-1];. Bt
16cf1 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
16cf2 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 age->pBt;. int
16cf3 70 61 72 65 6e 74 49 64 78 20 3d 20 70 50 61 72 parentIdx = pPar
16cf4 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a ent->nCell; /*
16cf5 20 70 50 61 72 65 6e 74 20 6e 65 77 20 64 69 76 pParent new div
16cf6 69 64 65 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 ider cell index
16cf7 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65 6e 74 53 */. int parentS
16cf8 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
16cf9 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
16cfa 66 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 f new divider ce
16cfb 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e ll */. u8 paren
16cfc 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 tCell[64];
16cfd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
16cfe 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 ce for the new d
16cff 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a ivider cell */..
16d00 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16d01 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
16d02 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
16d03 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 );.. /* Allocat
16d04 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 6e e a new page. In
16d05 73 65 72 74 20 74 68 65 20 6f 76 65 72 66 6c 6f sert the overflo
16d06 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 w cell from pPag
16d07 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 e. ** into it.
16d08 54 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 Then remove the
16d09 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 overflow cell fr
16d0a 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 om pPage.. */.
16d0b 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
16d0c 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e reePage(pBt, &pN
16d0d 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c ew, &pgnoNew, 0,
16d0e 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 0);. if( rc==S
16d0f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16d10 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 pCell = pPage->a
16d11 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 Ovfl[0].pCell;.
16d12 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c szCell = cell
16d13 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 SizePtr(pPage, p
16d14 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 72 Cell);. asser
16d15 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
16d16 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 2d swriteable(pNew-
16d17 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 >pDbPage) );.
16d18 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 zeroPage(pNew,
16d19 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29 pPage->aData[0])
16d1a 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 ;. assemblePa
16d1b 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65 ge(pNew, 1, &pCe
16d1c 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20 ll, &szCell);.
16d1d 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
16d1e 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f ow = 0;. . /
16d1f 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72 65 * pPage is curre
16d20 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63 ntly the right-c
16d21 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e hild of pParent.
16d22 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20 Change this.
16d23 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 ** so that the
16d24 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74 right-child is t
16d25 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f he new page allo
16d26 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a cated above and.
16d27 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20 ** pPage is
16d28 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68 the next-to-righ
16d29 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a t child. . **
16d2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74 . ** Ignore t
16d2b 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
16d2c 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66 of the call to f
16d2d 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c illInCell(). fil
16d2e 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a lInCell(). **
16d2f 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e may only return
16d30 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
16d31 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72 TE_OK if it is r
16d32 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63 equired to alloc
16d33 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f ate. ** one o
16d34 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 r more overflow
16d35 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20 pages. Since an
16d36 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42 internal table B
16d37 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20 -Tree cell .
16d38 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69 ** may never spi
16d39 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20 ll over onto an
16d3a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69 overflow page (i
16d3b 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f t is a maximum o
16d3c 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74 f . ** 13 byt
16d3d 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20 es in size), it
16d3e 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72 is not neccessar
16d3f 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 y to check the r
16d40 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20 eturn code..
16d41 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 **. ** Simila
16d42 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43 rly, the insertC
16d43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 ell() function c
16d44 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68 annot fail if th
16d45 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65 e page. ** be
16d46 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 ing inserted int
16d47 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72 69 o is already wri
16d48 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65 table and the ce
16d49 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20 ll does not .
16d4a 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f ** contain an o
16d4b 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e verflow pointer.
16d4c 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20 So ignore this
16d4d 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e return code too.
16d4e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
16d4f 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
16d50 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 >0 );. pCell
16d51 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
16d52 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 , pPage->nCell-1
16d53 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
16d54 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
16d55 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
16d56 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e nfo);. fillIn
16d57 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 Cell(pParent, pa
16d58 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 rentCell, 0, inf
16d59 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c o.nKey, 0, 0, 0,
16d5a 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20 &parentSize);.
16d5b 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65 6e assert( paren
16d5c 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20 tSize<64 );.
16d5d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
16d5e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
16d5f 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
16d60 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43 ) );. insertC
16d61 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 ell(pParent, par
16d62 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65 entIdx, parentCe
16d63 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20 ll, parentSize,
16d64 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62 0, 4);. put4b
16d65 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 yte(findOverflow
16d66 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72 Cell(pParent,par
16d67 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e entIdx), pPage->
16d68 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62 pgno);. put4b
16d69 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 yte(&pParent->aD
16d6a 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 ata[pParent->hdr
16d6b 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e Offset+8], pgnoN
16d6c 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 ew);. . /* I
16d6d 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
16d6e 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
16d6f 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f e, update the po
16d70 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a inter map. **
16d71 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f with entries fo
16d72 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 r the new page,
16d73 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 and any pointer
16d74 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a from the . **
16d75 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 cell on the pag
16d76 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e to an overflow
16d77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
16d78 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
16d79 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d UM ){. rc =
16d7a 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
16d7b 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f pgnoNew, PTRMAP_
16d7c 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e BTREE, pParent->
16d7d 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 pgno);. if(
16d7e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16d7f 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
16d80 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 trmapPutOvfl(pNe
16d81 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 w, 0);. }.
16d82 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c }.. /* Rel
16d83 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e ease the referen
16d84 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 ce to the new pa
16d85 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 ge. */. relea
16d86 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 sePage(pNew);.
16d87 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 }.. /* At this
16d88 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d point the pPage-
16d89 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65 20 >nFree variable
16d8a 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65 is not set corre
16d8b 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72 ctly with. ** r
16d8c 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f espect to the co
16d8d 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 ntent of the pag
16d8e 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77 61 e (because it wa
16d8f 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20 s set to 0 by .
16d90 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e ** insertCell).
16d91 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 So call sqlite3
16d92 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 BtreeInitPage()
16d93 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
16d94 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e is. ** correct.
16d95 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
16d96 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65 has to be done e
16d97 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ven if an error
16d98 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
16d99 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20 . Normally, if.
16d9a 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 ** an error occ
16d9b 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65 20 urs during tree
16d9c 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63 balancing, the c
16d9d 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61 ontents of MemPa
16d9e 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20 ge are. ** not
16d9f 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68 important, as th
16da0 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c ey will be recal
16da1 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65 culated when the
16da2 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a page is rolled.
16da3 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68 ** back. But h
16da4 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f ere, in balance_
16da5 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70 quick(), it is p
16da6 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50 61 ossible that pPa
16da7 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74 ge has . ** not
16da8 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64 yet been marked
16da9 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74 65 dirty or writte
16daa 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
16dab 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f al file. Therefo
16dac 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20 re. ** it will
16dad 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 not be rolled ba
16dae 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 ck and so it is
16daf 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b important to mak
16db0 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a e sure that. **
16db1 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61 the page data a
16db2 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d nd contents of M
16db3 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69 emPage are consi
16db4 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50 stent.. */. pP
16db5 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
16db6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 . sqlite3BtreeI
16db7 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
16db8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
16db9 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
16dba 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 .. /* If everyt
16dbb 68 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65 hing else succee
16dbc 64 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65 ded, balance the
16dbd 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e parent page, in
16dbe 20 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 . ** case the
16dbf 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 divider cell ins
16dc0 65 72 74 65 64 20 63 61 75 73 65 64 20 69 74 20 erted caused it
16dc1 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 to become overfu
16dc2 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 ll.. */. if( r
16dc3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16dc4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
16dc5 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 pPage);. pCur
16dc6 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72 ->iPage--;. r
16dc7 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 c = balance(pCur
16dc8 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 0);. }. retu
16dc9 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 rn rc;.}.#endif
16dca 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 /* SQLITE_OMIT_Q
16dcb 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a UICKBALANCE */..
16dcc 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
16dcd 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73 ne redistributes
16dce 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 Cells on pPage
16dcf 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 and up to NN*2 s
16dd0 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 iblings.** of pP
16dd1 61 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 age so that all
16dd2 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 pages have about
16dd3 20 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 the same amount
16dd4 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a of free space..
16dd5 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 ** Usually NN si
16dd6 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 blings on either
16dd7 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 side of pPage i
16dd8 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 s used in the ba
16dd9 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 lancing,.** thou
16dda 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 gh more siblings
16ddb 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d might come from
16ddc 20 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 one side if pPa
16ddd 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 0a ge is the first.
16dde 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 ** or last child
16ddf 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 of its parent.
16de0 20 49 66 20 70 50 61 67 65 20 68 61 73 20 66 65 If pPage has fe
16de1 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 wer than 2*NN si
16de2 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 blings.** (somet
16de3 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f hing which can o
16de4 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 nly happen if pP
16de5 61 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 age is the root
16de6 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 page or a .** ch
16de7 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 ild of root) the
16de8 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 n all available
16de9 73 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 siblings partici
16dea 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 pate in the bala
16deb 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ncing..**.** The
16dec 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 number of sibli
16ded 6e 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 ngs of pPage mig
16dee 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 ht be increased
16def 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79 20 or decreased by
16df0 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e one or.** two in
16df1 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 an effort to ke
16df2 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 ep pages nearly
16df3 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 full but not ove
16df4 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 r full. The root
16df5 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 page.** is spec
16df6 69 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 ial and is allow
16df7 65 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 ed to be nearly
16df8 65 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 empty. If pPage
16df9 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 is .** the root
16dfa 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 page, then the d
16dfb 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 epth of the tree
16dfc 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 might be increa
16dfd 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 sed.** or decrea
16dfe 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e sed by one, as n
16dff 65 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 ecessary, to kee
16e00 70 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 p the root page
16e01 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 from being.** ov
16e02 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 erfull or comple
16e03 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a tely empty..**.*
16e04 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e * Note that when
16e05 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
16e06 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 called, some of
16e07 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 the Cells on pP
16e08 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 age.** might not
16e09 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f actually be sto
16e0a 72 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 red in pPage->aD
16e0b 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e ata[]. This can
16e0c 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 happen.** if th
16e0d 65 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 e page is overfu
16e0e 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 ll. Part of the
16e0f 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 job of this rou
16e10 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 tine is to.** ma
16e11 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c ke sure all Cell
16e12 73 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 s for pPage once
16e13 20 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 again fit in pP
16e14 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a age->aData[]..**
16e15 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 .** In the cours
16e16 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 e of balancing t
16e17 68 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 he siblings of p
16e18 50 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 Page, the parent
16e19 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 of pPage.** mig
16e1a 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 ht become overfu
16e1b 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e ll or underfull.
16e1c 20 20 49 66 20 74 68 61 74 20 68 61 70 70 65 6e If that happen
16e1d 73 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 s, then this rou
16e1e 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 tine.** is calle
16e1f 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e d recursively on
16e20 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a the parent..**.
16e21 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 ** If this routi
16e22 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 ne fails for any
16e23 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 reason, it migh
16e24 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 t leave the data
16e25 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 base.** in a cor
16e26 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 rupted state. S
16e27 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e o if this routin
16e28 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 e fails, the dat
16e29 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 abase should.**
16e2a 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a be rolled back..
16e2b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 */.static int ba
16e2c 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74 lance_nonroot(Bt
16e2d 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
16e2e 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
16e2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16e30 20 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64 The over or und
16e31 65 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62 erfull page to b
16e32 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50 alance */. MemP
16e33 61 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 age *pParent;
16e34 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
16e35 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20 parent of pPage
16e36 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
16e37 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Bt;
16e38 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 /* The whole d
16e39 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 atabase */. int
16e3a 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 nCell = 0;
16e3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
16e3c 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 ber of cells in
16e3d 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e apCell[] */. in
16e3e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b t nMaxCells = 0;
16e3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c /* Al
16e40 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 located size of
16e41 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 apCell, szCell,
16e42 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 aFrom. */. int
16e43 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 nOld = 0;
16e44 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16e45 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 er of pages in a
16e46 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 pOld[] */. int
16e47 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 nNew = 0;
16e48 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16e49 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 er of pages in a
16e4a 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 pNew[] */. int
16e4b 6e 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 nDiv;
16e4c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16e4d 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 er of cells in a
16e4e 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 pDiv[] */. int
16e4f 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 i, j, k;
16e50 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
16e51 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
16e52 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 nt idx;
16e53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
16e54 6e 64 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e ndex of pPage in
16e55 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b pParent->aCell[
16e56 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 ] */. int nxDiv
16e57 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16e58 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 /* Next divi
16e59 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 der slot in pPar
16e5a 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a ent->aCell[] */.
16e5b 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
16e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16e5d 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 * The return cod
16e5e 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43 e */. int leafC
16e5f 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 orrection;
16e60 20 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 /* 4 if pPag
16e61 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 e is a leaf. 0
16e62 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 if not */. int
16e63 6c 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 leafData;
16e64 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
16e65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c if pPage is a l
16e66 65 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 eaf of a LEAFDAT
16e67 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 A tree */. int
16e68 75 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 usableSpace;
16e69 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
16e6a 73 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e s in pPage beyon
16e6b 64 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a d the header */.
16e6c 20 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b int pageFlags;
16e6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16e6e 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 * Value of pPage
16e6f 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 ->aData[0] */.
16e70 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 int subtotal;
16e71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16e72 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 Subtotal of byte
16e73 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e s in cells on on
16e74 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 e page */. int
16e75 69 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 iSpace1 = 0;
16e76 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
16e77 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 t unused byte of
16e78 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 aSpace1[] */.
16e79 69 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b int iSpace2 = 0;
16e7a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16e7b 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 First unused byt
16e7c 65 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a e of aSpace2[] *
16e7d 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 /. int szScratc
16e7e 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
16e7f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 /* Size of scra
16e80 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 tch memory reque
16e81 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 sted */. MemPag
16e82 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 e *apOld[NB];
16e83 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
16e84 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 and up to two si
16e85 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f blings */. Pgno
16e86 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 pgnoOld[NB];
16e87 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
16e88 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 numbers for eac
16e89 68 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b h page in apOld[
16e8a 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a ] */. MemPage *
16e8b 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 apCopy[NB];
16e8c 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 /* Private c
16e8d 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d opies of apOld[]
16e8e 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 pages */. MemP
16e8f 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d age *apNew[NB+2]
16e90 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 ; /* pPag
16e91 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 e and up to NB s
16e92 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 iblings after ba
16e93 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e lancing */. Pgn
16e94 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b o pgnoNew[NB+2];
16e95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
16e96 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 e numbers for ea
16e97 63 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 ch page in apNew
16e98 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 [] */. u8 *apDi
16e99 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 v[NB];
16e9a 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 /* Divider
16e9b 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 cells in pParent
16e9c 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 */. int cntNew
16e9d 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 [NB+2];
16e9e 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 /* Index in a
16e9f 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 Cell[] of cell a
16ea0 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a fter i-th page *
16ea1 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 /. int szNew[NB
16ea2 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 +2];
16ea3 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a /* Combined siz
16ea4 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 e of cells place
16ea5 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f on i-th page */
16ea6 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d . u8 **apCell =
16ea7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
16ea8 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 /* All cells beg
16ea9 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 in balanced */.
16eaa 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 u16 *szCell;
16eab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16eac 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 Local size of a
16ead 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 ll cells in apCe
16eae 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 ll[] */. u8 *aC
16eaf 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 opy[NB];
16eb0 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f /* Space for ho
16eb1 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 lding data of ap
16eb2 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a Copy[] */. u8 *
16eb3 61 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 aSpace1;
16eb4 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 /* Space for
16eb5 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 copies of divide
16eb6 72 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 rs cells before
16eb7 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 balance */. u8
16eb8 2a 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20 *aSpace2 = 0;
16eb9 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 /* Space for
16eba 20 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65 overflow divide
16ebb 72 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 rs cells after b
16ebc 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a alance */. u8 *
16ebd 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50 aFrom = 0;.. pP
16ebe 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
16ebf 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
16ec0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16ec1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
16ec2 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
16ec3 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 );. VVA_ONLY(
16ec4 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 pCur->pagesShuff
16ec5 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a led = 1 );.. /*
16ec6 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 . ** Find the
16ec7 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a parent page.. *
16ec8 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 /. assert( pCur
16ec9 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 ->iPage>0 );. a
16eca 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 ssert( pPage->is
16ecb 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 Init );. assert
16ecc 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
16ecd 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
16ece 3e 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 >pDbPage) || pPa
16ecf 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 ge->nOverflow==1
16ed0 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 );. pBt = pPag
16ed1 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e e->pBt;. pParen
16ed2 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 t = pCur->apPage
16ed3 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b [pCur->iPage-1];
16ed4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 . assert( pPare
16ed5 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 nt );. if( SQLI
16ed6 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
16ed7 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
16ed8 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 Parent->pDbPage)
16ed9 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 61 ) ){. goto ba
16eda 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
16edb 20 7d 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 }.. TRACE(("BA
16edc 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 LANCE: begin pag
16edd 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 e %d child of %d
16ede 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f \n", pPage->pgno
16edf 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 , pParent->pgno)
16ee0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
16ee1 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c TE_OMIT_QUICKBAL
16ee2 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 ANCE. /*. ** A
16ee3 20 73 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 special case:
16ee4 49 66 20 61 20 6e 65 77 20 65 6e 74 72 79 20 68 If a new entry h
16ee5 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 as just been ins
16ee6 65 72 74 65 64 20 69 6e 74 6f 20 61 0a 20 20 2a erted into a. *
16ee7 2a 20 74 61 62 6c 65 20 28 74 68 61 74 20 69 73 * table (that is
16ee8 2c 20 61 20 62 74 72 65 65 20 77 69 74 68 20 69 , a btree with i
16ee9 6e 74 65 67 65 72 20 6b 65 79 73 20 61 6e 64 20 nteger keys and
16eea 61 6c 6c 20 64 61 74 61 20 61 74 20 74 68 65 20 all data at the
16eeb 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e 64 leaves). ** and
16eec 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 the new entry i
16eed 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 s the right-most
16eee 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 entry in the tr
16eef 65 65 20 28 69 74 20 68 61 73 20 74 68 65 0a 20 ee (it has the.
16ef0 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 29 ** largest key)
16ef1 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 73 70 then use the sp
16ef2 65 63 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71 75 ecial balance_qu
16ef3 69 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 66 6f ick() routine fo
16ef4 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 r. ** balancing
16ef5 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b . balance_quick
16ef6 28 29 20 69 73 20 6d 75 63 68 20 66 61 73 74 65 () is much faste
16ef7 72 20 61 6e 64 20 72 65 73 75 6c 74 73 20 69 6e r and results in
16ef8 20 61 20 74 69 67 68 74 65 72 0a 20 20 2a 2a 20 a tighter. **
16ef9 70 61 63 6b 69 6e 67 20 6f 66 20 64 61 74 61 20 packing of data
16efa 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 in the common ca
16efb 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 se.. */. if( p
16efc 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20 20 Page->leaf &&.
16efd 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 pPage->intKe
16efe 79 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 y &&. pPage
16eff 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 ->nOverflow==1 &
16f00 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 &. pPage->a
16f01 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 Ovfl[0].idx==pPa
16f02 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 ge->nCell &&.
16f03 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f pParent->pgno
16f04 21 3d 31 20 26 26 0a 20 20 20 20 20 20 67 65 74 !=1 &&. get
16f05 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 4byte(&pParent->
16f06 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 aData[pParent->h
16f07 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70 50 drOffset+8])==pP
16f08 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a 20 age->pgno. ){.
16f09 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
16f0a 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 ->intKey );.
16f0b 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a 20 /*. ** TODO:
16f0c 43 68 65 63 6b 20 74 68 65 20 73 69 62 6c 69 6e Check the siblin
16f0d 67 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f gs to the left o
16f0e 66 20 70 50 61 67 65 2e 20 49 74 20 6d 61 79 20 f pPage. It may
16f0f 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 be that. ** t
16f10 68 65 79 20 61 72 65 20 6e 6f 74 20 66 75 6c 6c hey are not full
16f11 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67 65 and no new page
16f12 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 is required..
16f13 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 */. return
16f14 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 43 balance_quick(pC
16f15 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ur);. }.#endif.
16f16 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
16f17 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 !=(rc = sqlite3P
16f18 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
16f19 3e 70 44 62 50 61 67 65 29 29 20 29 7b 0a 20 20 >pDbPage)) ){.
16f1a 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
16f1b 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f leanup;. }.. /
16f1c 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 *. ** Find the
16f1d 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65 cell in the pare
16f1e 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65 nt page whose le
16f1f 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20 ft child points
16f20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61 back. ** to pPa
16f21 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20 76 ge. The "idx" v
16f22 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 69 ariable is the i
16f23 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c ndex of that cel
16f24 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a l. If pPage. *
16f25 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f * is the rightmo
16f26 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 st child of pPar
16f27 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64 78 ent then set idx
16f28 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 to pParent->nCe
16f29 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d ll . */. idx =
16f2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
16f2b 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61 r->iPage-1];. a
16f2c 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 ssertParentIndex
16f2d 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70 (pParent, idx, p
16f2e 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 Page->pgno);..
16f2f 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 62 /*. ** Find sib
16f30 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70 50 ling pages to pP
16f31 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c age and the cell
16f32 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68 61 s in pParent tha
16f33 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 68 t divide. ** th
16f34 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e 20 e siblings. An
16f35 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
16f36 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 to find NN sibli
16f37 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 20 ngs on either.
16f38 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 ** side of pPage
16f39 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 . More siblings
16f3a 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 are taken from
16f3b 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 one side, howeve
16f3c 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 65 r, if. ** pPage
16f3d 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72 there are fewer
16f3e 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 than NN sibling
16f3f 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73 s on the other s
16f40 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e 74 ide. If pParent
16f41 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 . ** has NB or
16f42 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 fewer children t
16f43 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e hen all children
16f44 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 of pParent are
16f45 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 78 taken.. */. nx
16f46 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b 0a Div = idx - NN;.
16f47 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e 42 if( nxDiv + NB
16f48 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c > pParent->nCel
16f49 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d l ){. nxDiv =
16f4a 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 pParent->nCell
16f4b 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 - NB + 1;. }.
16f4c 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a 20 if( nxDiv<0 ){.
16f4d 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 nxDiv = 0;.
16f4e 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 20 }. nDiv = 0;.
16f4f 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 76 for(i=0, k=nxDiv
16f50 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b 2b ; i<NB; i++, k++
16f51 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 61 ){. if( k<pPa
16f52 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 rent->nCell ){.
16f53 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 apDiv[i] =
16f54 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 findCell(pParent
16f55 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 76 , k);. nDiv
16f56 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ++;. assert
16f57 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 66 ( !pParent->leaf
16f58 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c );. pgnoOl
16f59 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65 28 d[i] = get4byte(
16f5a 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 7d apDiv[i]);. }
16f5b 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 72 else if( k==pPar
16f5c 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 ent->nCell ){.
16f5d 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d pgnoOld[i] =
16f5e 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 get4byte(&pPare
16f5f 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
16f60 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 t->hdrOffset+8])
16f61 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
16f62 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
16f63 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 rc = getAndI
16f64 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e nitPage(pBt, pgn
16f65 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 5b oOld[i], &apOld[
16f66 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 i]);. if( rc
16f67 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 ) goto balance_c
16f68 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 61 leanup;. /* a
16f69 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65 pOld[i]->idxPare
16f6a 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 61 nt = k; */. a
16f6b 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 pCopy[i] = 0;.
16f6c 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c assert( i==nOl
16f6d 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b d );. nOld++;
16f6e 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b . nMaxCells +
16f6f 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 = 1+apOld[i]->nC
16f70 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f ell+apOld[i]->nO
16f71 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 verflow;. }..
16f72 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c /* Make nMaxCell
16f73 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 s a multiple of
16f74 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 4 in order to pr
16f75 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 eserve 8-byte.
16f76 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a ** alignment */.
16f77 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e nMaxCells = (n
16f78 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 MaxCells + 3)&~3
16f79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c ;.. /*. ** All
16f7a 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
16f7b 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
16f7c 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 74 s. */. szScrat
16f7d 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 ch =. nMax
16f7e 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a Cells*sizeof(u8*
16f7f 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
16f80 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c /* apCel
16f81 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 l */. + nMax
16f82 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 Cells*sizeof(u16
16f83 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
16f84 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c /* szCel
16f85 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f 55 l */. + (ROU
16f86 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 ND8(sizeof(MemPa
16f87 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 69 ge))+pBt->pageSi
16f88 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 79 ze)*NB /* aCopy
16f89 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e */. + pBt->
16f8a 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 pageSize
16f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f8c 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 /* aSpace
16f8d 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 41 1 */. + (ISA
16f8e 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 78 UTOVACUUM ? nMax
16f8f 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 20 Cells : 0);
16f90 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d /* aFrom
16f91 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 */. apCell = s
16f92 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
16f93 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 loc( szScratch )
16f94 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d ; . if( apCell=
16f95 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
16f96 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
16f97 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
16f98 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 eanup;. }. szC
16f99 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 ell = (u16*)&apC
16f9a 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a ell[nMaxCells];.
16f9b 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 38 aCopy[0] = (u8
16f9c 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 *)&szCell[nMaxCe
16f9d 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 lls];. assert(
16f9e 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
16f9f 4d 45 4e 54 28 61 43 6f 70 79 5b 30 5d 29 20 29 MENT(aCopy[0]) )
16fa0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e ;. for(i=1; i<N
16fa1 42 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f B; i++){. aCo
16fa2 70 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69 py[i] = &aCopy[i
16fa3 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a -1][pBt->pageSiz
16fa4 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 e+ROUND8(sizeof(
16fa5 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20 MemPage))];.
16fa6 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b assert( ((aCopy[
16fa7 69 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 i] - (u8*)0) & 7
16fa8 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 )==0 ); /* 8-byt
16fa9 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 e alignment requ
16faa 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 ired */. }. aS
16fab 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e pace1 = &aCopy[N
16fac 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 B-1][pBt->pageSi
16fad 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 ze+ROUND8(sizeof
16fae 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 (MemPage))];. a
16faf 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
16fb0 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 E_ALIGNMENT(aSpa
16fb1 63 65 31 29 20 29 3b 0a 20 20 69 66 28 20 49 53 ce1) );. if( IS
16fb2 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 AUTOVACUUM ){.
16fb3 20 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 aFrom = &aSpac
16fb4 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e1[pBt->pageSize
16fb5 5d 3b 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32 ];. }. aSpace2
16fb6 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
16fb7 6c 6c 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69 lloc(pBt->pageSi
16fb8 7a 65 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63 ze);. if( aSpac
16fb9 65 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 e2==0 ){. rc
16fba 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
16fbb 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
16fbc 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 _cleanup;. }.
16fbd 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 . /*. ** Make
16fbe 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f copies of the co
16fbf 6e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 ntent of pPage a
16fc0 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 nd its siblings
16fc1 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a into aOld[].. *
16fc2 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 * The rest of th
16fc3 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c is function will
16fc4 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 use data from t
16fc5 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 he copies rather
16fc6 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f . ** that the o
16fc7 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 riginal pages si
16fc8 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c nce the original
16fc9 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 pages will be i
16fca 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 n the. ** proce
16fcb 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 ss of being over
16fcc 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 written.. */.
16fcd 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b for(i=0; i<nOld;
16fce 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 i++){. MemPa
16fcf 67 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 ge *p = apCopy[i
16fd0 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 ] = (MemPage*)aC
16fd1 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 opy[i];. memc
16fd2 70 79 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 py(p, apOld[i],
16fd3 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 sizeof(MemPage))
16fd4 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d ;. p->aData =
16fd5 20 28 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 (void*)&p[1];.
16fd6 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 memcpy(p->aDa
16fd7 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 ta, apOld[i]->aD
16fd8 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 ata, pBt->pageSi
16fd9 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 ze);. }.. /*.
16fda 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 ** Load pointer
16fdb 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f s to all cells o
16fdc 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 n sibling pages
16fdd 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20 and the divider
16fde 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 cells. ** into
16fdf 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c the local apCell
16fe0 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 [] array. Make
16fe1 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 copies of the di
16fe2 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a vider cells. **
16fe3 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 into space obta
16fe4 69 6e 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 ined form aSpace
16fe5 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 1[] and remove t
16fe6 68 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43 he the divider C
16fe7 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 ells. ** from p
16fe8 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a Parent.. **. *
16fe9 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 * If the sibling
16fea 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 s are on leaf pa
16feb 67 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 ges, then the ch
16fec 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 ild pointers of
16fed 74 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 the. ** divider
16fee 20 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 cells are strip
16fef 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c ped from the cel
16ff0 6c 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 ls before they a
16ff1 72 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 re copied. ** i
16ff2 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 nto aSpace1[].
16ff3 49 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c In this way, all
16ff4 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c cells in apCell
16ff5 5b 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 [] are without.
16ff6 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 ** child pointe
16ff7 72 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 rs. If siblings
16ff8 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c are not leaves,
16ff9 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 then all cell i
16ffa 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 n. ** apCell[]
16ffb 69 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f include child po
16ffc 69 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 inters. Either
16ffd 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 way, all cells i
16ffe 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 n apCell[]. **
16fff 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a are alike.. **.
17000 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 ** leafCorrect
17001 69 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 ion: 4 if pPage
17002 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 is a leaf. 0 i
17003 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 f pPage is not a
17004 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 leaf.. **
17005 20 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 leafData: 1 i
17006 66 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 f pPage holds ke
17007 79 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 y+data and pPare
17008 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 nt holds only ke
17009 79 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c ys.. */. nCell
1700a 20 3d 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 = 0;. leafCorr
1700b 65 63 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e ection = pPage->
1700c 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 leaf*4;. leafDa
1700d 74 61 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 ta = pPage->hasD
1700e 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ata;. for(i=0;
1700f 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 i<nOld; i++){.
17010 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 MemPage *pOld
17011 3d 20 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 = apCopy[i];.
17012 20 69 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c int limit = pOl
17013 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e d->nCell+pOld->n
17014 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f Overflow;. fo
17015 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 r(j=0; j<limit;
17016 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 j++){. asse
17017 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 rt( nCell<nMaxCe
17018 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 lls );. apC
17019 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e ell[nCell] = fin
1701a 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f dOverflowCell(pO
1701b 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a ld, j);. sz
1701c 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 Cell[nCell] = ce
1701d 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 llSizePtr(pOld,
1701e 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a apCell[nCell]);.
1701f 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f if( ISAUTO
17020 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
17021 20 20 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 int a;.
17022 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 aFrom[nCell] =
17023 28 75 38 29 69 3b 20 20 20 61 73 73 65 72 74 28 (u8)i; assert(
17024 20 69 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a i>=0 && i<6 );.
17025 20 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b for(a=0;
17026 20 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c a<pOld->nOverfl
17027 6f 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 ow; a++){.
17028 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f if( pOld->aO
17029 76 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 vfl[a].pCell==ap
1702a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 Cell[nCell] ){.
1702b 20 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d aFrom
1702c 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a [nCell] = 0xFF;.
1702d 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
1702e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 k;. }.
1702f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
17030 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a . nCell++;.
17031 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c }. if( i<
17032 6e 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 nOld-1 ){.
17033 75 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a u16 sz = cellSiz
17034 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 ePtr(pParent, ap
17035 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 Div[i]);. i
17036 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 f( leafData ){.
17037 20 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 /* With t
17038 68 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 he LEAFDATA flag
17039 2c 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 , pParent cells
1703a 68 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 hold only INTKEY
1703b 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a s that. *
1703c 2a 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 * are duplicates
1703d 20 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 of keys on the
1703e 63 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 child pages. We
1703f 20 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a need to remove.
17040 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 ** the d
17041 69 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f ivider cells fro
17042 6d 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 m pParent, but t
17043 68 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c he dividers cell
17044 73 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 s are not.
17045 20 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 ** added to ap
17046 43 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 Cell[] because t
17047 68 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 hey are duplicat
17048 65 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c es of child cell
17049 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 s.. */.
1704a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 dropCell(p
1704b 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 Parent, nxDiv, s
1704c 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b z);. }else{
1704d 0a 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 . u8 *pTe
1704e 6d 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 mp;. asse
1704f 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 rt( nCell<nMaxCe
17050 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 lls );. s
17051 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 zCell[nCell] = s
17052 7a 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 z;. pTemp
17053 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 = &aSpace1[iSpa
17054 63 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 ce1];. iS
17055 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 pace1 += sz;.
17056 20 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c assert( sz<
17057 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 =pBt->pageSize/4
17058 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 );. asse
17059 72 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 rt( iSpace1<=pBt
1705a 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 ->pageSize );.
1705b 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 memcpy(pTe
1705c 6d 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a mp, apDiv[i], sz
1705d 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c );. apCel
1705e 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 l[nCell] = pTemp
1705f 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b +leafCorrection;
17060 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41 . if( ISA
17061 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
17062 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 aFrom[nCe
17063 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 ll] = 0xFF;.
17064 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 }. dr
17065 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 opCell(pParent,
17066 6e 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 nxDiv, sz);.
17067 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 assert( leaf
17068 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c Correction==0 ||
17069 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d leafCorrection=
1706a 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a =4 );. sz
1706b 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 Cell[nCell] -= (
1706c 75 31 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69 u16)leafCorrecti
1706d 6f 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 on;. asse
1706e 72 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65 rt( get4byte(pTe
1706f 6d 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 mp)==pgnoOld[i]
17070 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 );. if( !
17071 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 pOld->leaf ){.
17072 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
17073 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
17074 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 0 );. /
17075 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e * The right poin
17076 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 ter of the child
17077 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d page pOld becom
17078 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 es the left.
17079 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 ** pointer
1707a 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
1707b 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 cell */.
1707c 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b memcpy(apCell[
1707d 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 nCell], &pOld->a
1707e 44 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 Data[pOld->hdrOf
1707f 66 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 fset+8], 4);.
17080 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17081 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
17082 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 afCorrection==4
17083 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
17084 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 szCell[nCell]<4
17085 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
17086 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 /* Do not allow
17087 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 any cells smalle
17088 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 r than 4 bytes.
17089 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 */. s
1708a 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 zCell[nCell] = 4
1708b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1708c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1708d 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d nCell++;. }
1708e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1708f 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 . ** Figure out
17090 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
17091 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 ages needed to h
17092 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 old all nCell ce
17093 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 lls.. ** Store
17094 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 this number in "
17095 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 k". Also comput
17096 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 e szNew[] which
17097 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a is the total. *
17098 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 * size of all ce
17099 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 lls on the i-th
1709a 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b page and cntNew[
1709b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 ] which is the i
1709c 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 ndex. ** in apC
1709d 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c ell[] of the cel
1709e 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70 l that divides p
1709f 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 age i from page
170a0 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e i+1. . ** cntN
170a1 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 ew[k] should equ
170a2 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 al nCell.. **.
170a3 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 ** Values compu
170a4 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 ted by this bloc
170a5 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 k:. **. **
170a6 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f k: The to
170a7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 tal number of si
170a8 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a bling pages. **
170a9 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 szNew[i]: Sp
170aa 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 aced used on the
170ab 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 i-th sibling pa
170ac 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 ge.. ** cntNe
170ad 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 w[i]: Index in a
170ae 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 pCell[] and szCe
170af 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 ll[] for the fir
170b0 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 st cell to. **
170b1 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
170b2 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d right of the i-
170b3 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e th sibling page.
170b4 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 . ** usableSpac
170b5 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 e: Number of byt
170b6 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 es of space avai
170b7 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 lable on each si
170b8 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a bling.. ** . *
170b9 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 /. usableSpace
170ba 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
170bb 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 e - 12 + leafCor
170bc 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 rection;. for(s
170bd 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 ubtotal=k=i=0; i
170be 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
170bf 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 assert( i<nMax
170c0 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 Cells );. sub
170c1 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b total += szCell[
170c2 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 i] + 2;. if(
170c3 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c subtotal > usabl
170c4 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 eSpace ){.
170c5 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f szNew[k] = subto
170c6 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b tal - szCell[i];
170c7 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d . cntNew[k]
170c8 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 = i;. if(
170c9 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b leafData ){ i--;
170ca 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 }. subtota
170cb 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b l = 0;. k++
170cc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a ;. }. }. sz
170cd 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 New[k] = subtota
170ce 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d l;. cntNew[k] =
170cf 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a nCell;. k++;..
170d0 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 /*. ** The pa
170d1 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 cking computed b
170d2 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 y the previous b
170d3 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 lock is biased t
170d4 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e oward the siblin
170d5 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c gs. ** on the l
170d6 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c eft side. The l
170d7 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 eft siblings are
170d8 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 always nearly f
170d9 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 ull, while the.
170da 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 ** right-most s
170db 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 ibling might be
170dc 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 nearly empty. T
170dd 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 his block of cod
170de 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 e attempts. **
170df 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 to adjust the pa
170e0 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 cking of sibling
170e1 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 s to get a bette
170e2 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a r balance.. **.
170e3 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 ** This adjust
170e4 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 ment is more tha
170e5 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f n an optimizatio
170e6 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 n. The packing
170e7 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a above might. **
170e8 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 be so out of ba
170e9 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 lance as to be i
170ea 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 llegal. For exa
170eb 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d mple, the right-
170ec 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e most. ** siblin
170ed 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c g might be compl
170ee 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 etely empty. Th
170ef 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 is adjustment is
170f0 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 not optional..
170f1 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b */. for(i=k-1;
170f2 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 i>0; i--){.
170f3 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a int szRight = sz
170f4 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 New[i]; /* Size
170f5 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 of sibling on t
170f6 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 he right */.
170f7 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e int szLeft = szN
170f8 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 ew[i-1]; /* Size
170f9 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 of sibling on t
170fa 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 he left */. i
170fb 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 nt r;
170fc 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 /* Index of r
170fd 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 ight-most cell i
170fe 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a n left sibling *
170ff 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 /. int d;
17100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
17101 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c ex of first cell
17102 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 to the left of
17103 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f right sibling */
17104 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 .. r = cntNew
17105 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 [i-1] - 1;. d
17106 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 = r + 1 - leafD
17107 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ata;. assert(
17108 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a d<nMaxCells );.
17109 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d assert( r<nM
1710a 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 axCells );. w
1710b 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 hile( szRight==0
1710c 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 || szRight+szCe
1710d 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d ll[d]+2<=szLeft-
1710e 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b (szCell[r]+2) ){
1710f 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b . szRight +
17110 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b = szCell[d] + 2;
17111 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d . szLeft -=
17112 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a szCell[r] + 2;.
17113 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 cntNew[i-1
17114 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 ]--;. r = c
17115 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a ntNew[i-1] - 1;.
17116 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 d = r + 1
17117 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 - leafData;.
17118 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d }. szNew[i] =
17119 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a szRight;. sz
1711a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 New[i-1] = szLef
1711b 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 t;. }.. /* Eit
1711c 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 her we found one
1711d 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 or more cells (
1711e 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 cntnew[0])>0) or
1711f 20 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a we are the. **
17120 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 a virtual root
17121 70 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c page. A virtual
17122 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 root page is wh
17123 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 en the real root
17124 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 . ** page is pa
17125 67 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 ge 1 and we are
17126 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f the only child o
17127 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a f that page.. *
17128 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e /. assert( cntN
17129 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 ew[0]>0 || (pPar
1712a 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 ent->pgno==1 &&
1712b 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d pParent->nCell==
1712c 30 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 0) );.. /*. **
1712d 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 Allocate k new
1712e 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c pages. Reuse ol
1712f 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f d pages where po
17130 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 ssible.. */. a
17131 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 ssert( pPage->pg
17132 6e 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c no>1 );. pageFl
17133 61 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ags = pPage->aDa
17134 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 ta[0];. for(i=0
17135 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 ; i<k; i++){.
17136 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a MemPage *pNew;.
17137 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 if( i<nOld )
17138 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 {. pNew = a
17139 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b pNew[i] = apOld[
1713a 69 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 i];. pgnoNe
1713b 77 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 w[i] = pgnoOld[i
1713c 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 ];. apOld[i
1713d 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 ] = 0;. rc
1713e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1713f 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 ite(pNew->pDbPag
17140 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b e);. nNew++
17141 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
17142 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
17143 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 eanup;. }else
17144 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
17145 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 i>0 );. rc
17146 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
17147 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 age(pBt, &pNew,
17148 26 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e &pgnoNew[i], pgn
17149 6f 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 oNew[i-1], 0);.
1714a 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
1714b 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
1714c 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b up;. apNew[
1714d 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 i] = pNew;.
1714e 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nNew++;. }.
1714f 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e }.. /* Free an
17150 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 y old pages that
17151 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 were not reused
17152 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 as new pages..
17153 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e */. while( i<n
17154 4f 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 Old ){. rc =
17155 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 freePage(apOld[i
17156 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 ]);. if( rc )
17157 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
17158 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 eanup;. relea
17159 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 sePage(apOld[i])
1715a 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d ;. apOld[i] =
1715b 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0;. i++;. }
1715c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 .. /*. ** Put
1715d 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e the new pages in
1715e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
1715f 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f . This helps to
17160 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 . ** keep entri
17161 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 es in the disk f
17162 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 ile in order so
17163 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a that a scan. **
17164 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 of the table is
17165 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 a linear scan t
17166 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e hrough the file.
17167 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 That. ** in t
17168 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 urn helps the op
17169 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 erating system t
1716a 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a o deliver pages.
1716b 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 ** from the di
1716c 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e sk more rapidly.
1716d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 . **. ** An O(
1716e 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 n^2) insertion s
1716f 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 ort algorithm is
17170 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 used, but since
17171 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 . ** n is never
17172 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 more than NB (a
17173 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 small constant)
17174 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 , that should.
17175 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 ** not be a prob
17176 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 lem.. **. ** W
17177 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 hen NB==3, this
17178 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e one optimization
17179 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 makes the datab
1717a 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 ase. ** about 2
1717b 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 5% faster for la
1717c 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 rge insertions a
1717d 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 nd deletions..
1717e 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c */. for(i=0; i<
1717f 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 k-1; i++){. i
17180 6e 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 nt minV = pgnoNe
17181 77 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 w[i];. int mi
17182 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 nI = i;. for(
17183 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 j=i+1; j<k; j++)
17184 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f {. if( pgno
17185 4e 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 New[j]<(unsigned
17186 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 )minV ){.
17187 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 minI = j;.
17188 20 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 minV = pgnoNe
17189 77 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 w[j];. }.
1718a 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 }. if( minI
1718b 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 >i ){. int
1718c 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 t;. MemPage
1718d 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 *pT;. t =
1718e 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 pgnoNew[i];.
1718f 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b pT = apNew[i];
17190 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 . pgnoNew[i
17191 5d 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 ] = pgnoNew[minI
17192 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 ];. apNew[i
17193 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b ] = apNew[minI];
17194 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d . pgnoNew[m
17195 69 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 inI] = t;.
17196 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 apNew[minI] = pT
17197 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 ;. }. }. TR
17198 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f ACE(("BALANCE: o
17199 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 ld: %d %d %d ne
1719a 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29 w: %d(%d) %d(%d)
1719b 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 %d(%d) %d(%d) %
1719c 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 d(%d)\n",. pg
1719d 6e 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e noOld[0], . n
1719e 4f 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 Old>=2 ? pgnoOld
1719f 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c [1] : 0,. nOl
171a0 64 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 d>=3 ? pgnoOld[2
171a1 5d 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e ] : 0,. pgnoN
171a2 65 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c ew[0], szNew[0],
171a3 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 . nNew>=2 ? p
171a4 67 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e gnoNew[1] : 0, n
171a5 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 New>=2 ? szNew[1
171a6 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e ] : 0,. nNew>
171a7 3d 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 =3 ? pgnoNew[2]
171a8 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 : 0, nNew>=3 ? s
171a9 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 zNew[2] : 0,.
171aa 20 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e nNew>=4 ? pgnoN
171ab 65 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e ew[3] : 0, nNew>
171ac 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 =4 ? szNew[3] :
171ad 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 0,. nNew>=5 ?
171ae 20 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c pgnoNew[4] : 0,
171af 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 nNew>=5 ? szNew
171b0 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a [4] : 0));.. /*
171b1 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 . ** Evenly dis
171b2 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61 tribute the data
171b3 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 in apCell[] acr
171b4 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 oss the new page
171b5 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 s.. ** Insert d
171b6 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 ivider cells int
171b7 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 o pParent as nec
171b8 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a essary.. */. j
171b9 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = 0;. for(i=0;
171ba 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 i<nNew; i++){.
171bb 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 /* Assemble t
171bc 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 he new sibling p
171bd 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 age. */. MemP
171be 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 age *pNew = apNe
171bf 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 w[i];. assert
171c0 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( j<nMaxCells );
171c1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 . assert( pNe
171c2 77 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 w->pgno==pgnoNew
171c3 5b 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 [i] );. zeroP
171c4 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c age(pNew, pageFl
171c5 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 ags);. assemb
171c6 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 lePage(pNew, cnt
171c7 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c New[i]-j, &apCel
171c8 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d l[j], &szCell[j]
171c9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
171ca 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 New->nCell>0 ||
171cb 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e (nNew==1 && cntN
171cc 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 ew[0]==0) );.
171cd 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e assert( pNew->n
171ce 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a Overflow==0 );..
171cf 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
171d0 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
171d1 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 database, updat
171d2 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 e the pointer ma
171d3 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a p entries. **
171d4 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 that point to t
171d5 68 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 he siblings that
171d6 20 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 were rearranged
171d7 2e 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 . These can be:
171d8 6c 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c left. ** chil
171d9 64 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 dren of cells, t
171da 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f he right-child o
171db 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f f the page, or o
171dc 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 verflow pages.
171dd 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 ** pointed to
171de 62 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f by cells.. */
171df 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
171e0 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 ACUUM ){. f
171e1 6f 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 or(k=j; k<cntNew
171e2 5b 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 [i]; k++){.
171e3 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 assert( k<nMa
171e4 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
171e5 20 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d if( aFrom[k]==
171e6 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 0xFF || apCopy[a
171e7 46 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d From[k]]->pgno!=
171e8 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 pNew->pgno ){.
171e9 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 rc = ptr
171ea 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c mapPutOvfl(pNew,
171eb 20 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 k-j);.
171ec 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
171ed 4f 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 OK && leafCorrec
171ee 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 tion==0 ){.
171ef 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d rc = ptrm
171f0 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 apPut(pBt, get4b
171f1 79 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 yte(apCell[k]),
171f2 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e PTRMAP_BTREE, pN
171f3 65 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 ew->pgno);.
171f4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
171f5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
171f6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
171f7 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
171f8 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 leanup;.
171f9 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
171fa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
171fb 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a j = cntNew[i];.
171fc 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
171fd 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 ibling page asse
171fe 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 mbled above was
171ff 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f not the right-mo
17200 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 st sibling,.
17201 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 ** insert a divi
17202 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 der cell into th
17203 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 e parent page..
17204 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c */. if( i<
17205 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c nNew-1 && j<nCel
17206 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 l ){. u8 *p
17207 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a Cell;. u8 *
17208 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 pTemp;. int
17209 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 sz;.. asse
1720a 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 rt( j<nMaxCells
1720b 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
1720c 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 apCell[j];.
1720d 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d sz = szCell[j]
1720e 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f + leafCorrectio
1720f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d n;. pTemp =
17210 20 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 &aSpace2[iSpace
17211 32 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 2];. if( !p
17212 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 New->leaf ){.
17213 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
17214 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 w->aData[8], pCe
17215 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 ll, 4);.
17216 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
17217 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61 . && (a
17218 46 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c From[j]==0xFF ||
17219 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d apCopy[aFrom[j]
1721a 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 ]->pgno!=pNew->p
1721b 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a gno). ){.
1721c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
1721d 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 trmapPut(pBt, ge
1721e 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 t4byte(pCell), P
1721f 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 TRMAP_BTREE, pNe
17220 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 w->pgno);.
17221 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
17222 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
17223 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 goto balanc
17224 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 e_cleanup;.
17225 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
17226 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
17227 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 leafData ){.
17228 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 /* If the t
17229 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 ree is a leaf-da
1722a 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 ta tree, and the
1722b 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 siblings are le
1722c 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a aves, . *
1722d 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 * then there is
1722e 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 no divider cell
1722f 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 in apCell[]. Ins
17230 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 tead, the divide
17231 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 r . ** ce
17232 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 ll consists of t
17233 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 he integer key f
17234 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 or the right-mos
17235 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 t cell of .
17236 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e ** the siblin
17237 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g-page assembled
17238 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 above only..
17239 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1723a 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
1723b 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 j--;.
1723c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1723d 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 ParseCellPtr(pNe
1723e 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 w, apCell[j], &i
1723f 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 nfo);. pC
17240 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 ell = pTemp;.
17241 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e rc = fillIn
17242 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43 Cell(pParent, pC
17243 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 ell, 0, info.nKe
17244 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29 y, 0, 0, 0, &sz)
17245 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17246 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17247 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 goto ba
17248 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17249 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1724a 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 pTemp = 0;.
1724b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1724c 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 pCell -= 4;.
1724d 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 /* Obscure
1724e 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 case for non-lea
1724f 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 f-data trees: If
17250 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 the cell at pCe
17251 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a ll was. *
17252 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f * previously sto
17253 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f red on a leaf no
17254 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f de, and its repo
17255 72 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a rted size was 4.
17256 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 ** bytes
17257 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 , then it may ac
17258 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 tually be smalle
17259 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 r than this .
1725a 20 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c ** (see sql
1725b 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
1725c 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 llPtr(), 4 bytes
1725d 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 is the minimum
1725e 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 size of.
1725f 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 ** any cell). Bu
17260 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e t it is importan
17261 74 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f t to pass the co
17262 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 rrect size to .
17263 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 ** insert
17264 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 Cell(), so repar
17265 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e se the cell now.
17266 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
17267 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 ** Note that
17268 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 this can never
17269 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c happen in an SQL
1726a 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 ite data file, a
1726b 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a s all. **
1726c 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 cells are at le
1726d 61 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 ast 4 bytes. It
1726e 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 only happens in
1726f 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 b-trees used.
17270 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 ** to evalu
17271 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 ate "IN (SELECT
17272 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 ...)" and simila
17273 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 r clauses..
17274 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 */. if
17275 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 ( szCell[j]==4 )
17276 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
17277 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f rt(leafCorrectio
17278 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 n==4);.
17279 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 sz = cellSizePt
1727a 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c r(pParent, pCell
1727b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1727c 20 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63 }. iSpac
1727d 65 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 e2 += sz;.
1727e 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d assert( sz<=pBt-
1727f 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 >pageSize/4 );.
17280 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 assert( iSp
17281 61 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53 ace2<=pBt->pageS
17282 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 ize );. rc
17283 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 = insertCell(pPa
17284 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 rent, nxDiv, pCe
17285 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34 ll, sz, pTemp, 4
17286 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
17287 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
17288 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
17289 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 p;. assert(
1728a 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
1728b 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 riteable(pParent
1728c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
1728d 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e put4byte(fin
1728e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 dOverflowCell(pP
1728f 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e arent,nxDiv), pN
17290 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 ew->pgno);..
17291 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
17292 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
17293 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 atabase, and not
17294 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 a leaf-data tre
17295 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e e,. ** then
17296 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e update the poin
17297 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 ter map with an
17298 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 entry for the ov
17299 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 erflow page.
1729a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 ** that the ce
1729b 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 ll just inserted
1729c 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 points to (if a
1729d 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ny).. */.
1729e 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
1729f 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 CUUM && !leafDat
172a0 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 a ){. rc
172a1 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 = ptrmapPutOvfl(
172a2 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b pParent, nxDiv);
172a3 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
172a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
172a5 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c goto bal
172a6 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
172a7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
172a8 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 j++;.
172a9 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a nxDiv++;. }.
172aa 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
172ab 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
172ac 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 y for the new si
172ad 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 bling page. */.
172ae 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
172af 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 UUM ){. rc
172b0 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c = ptrmapPut(pBt,
172b1 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 pNew->pgno, PTR
172b2 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 MAP_BTREE, pPare
172b3 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 nt->pgno);.
172b4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
172b5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
172b6 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
172b7 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 up;. }.
172b8 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
172b9 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 j==nCell );. as
172ba 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a sert( nOld>0 );.
172bb 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 assert( nNew>0
172bc 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 );. if( (pageF
172bd 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 lags & PTF_LEAF)
172be 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a ==0 ){. u8 *z
172bf 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b Child = &apCopy[
172c0 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 nOld-1]->aData[8
172c1 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 ];. memcpy(&a
172c2 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 pNew[nNew-1]->aD
172c3 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 ata[8], zChild,
172c4 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 4);. if( ISAU
172c5 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
172c6 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
172c7 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a (pBt, get4byte(z
172c8 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 Child), PTRMAP_B
172c9 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 TREE, apNew[nNew
172ca 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 -1]->pgno);.
172cb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
172cc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
172cd 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
172ce 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nup;. }.
172cf 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
172d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
172d1 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 riteable(pParent
172d2 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
172d3 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 if( nxDiv==pPare
172d4 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e nt->nCell+pParen
172d5 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a t->nOverflow ){.
172d6 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 /* Right-mos
172d7 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 t sibling is the
172d8 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c right-most chil
172d9 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a d of pParent */.
172da 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
172db 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 arent->aData[pPa
172dc 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b rent->hdrOffset+
172dd 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 8], pgnoNew[nNew
172de 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 -1]);. }else{.
172df 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 /* Right-most
172e0 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 sibling is the
172e1 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 left child of th
172e2 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e e first entry in
172e3 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 pParent. **
172e4 70 61 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d past the right-m
172e5 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 ost divider entr
172e6 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 y */. put4byt
172e7 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 e(findOverflowCe
172e8 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 ll(pParent, nxDi
172e9 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 v), pgnoNew[nNew
172ea 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a -1]);. }.. /*.
172eb 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 ** Balance the
172ec 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e parent page. N
172ed 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 75 72 ote that the cur
172ee 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 67 65 rent page (pPage
172ef 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76 ) might. ** hav
172f0 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20 e been added to
172f1 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20 the freelist so
172f2 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67 it might no long
172f3 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 er be initialize
172f4 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 d.. ** But the
172f5 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c parent page will
172f6 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 always be initi
172f7 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 alized.. */. a
172f8 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e ssert( pParent->
172f9 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69 isInit );. sqli
172fa 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 te3ScratchFree(a
172fb 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c pCell);. apCell
172fc 20 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22 = 0;. TRACE(("
172fd 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 BALANCE: finishe
172fe 64 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 d with %d: old=%
172ff 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 d new=%d cells=%
17300 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
17301 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c pPage->pgno, nOl
17302 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 d, nNew, nCell))
17303 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 ;. pPage->nOver
17304 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65 flow = 0;. rele
17305 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
17306 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b pCur->iPage--;
17307 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 . rc = balance(
17308 70 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f pCur, 0);. . /
17309 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 *. ** Cleanup b
1730a 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
1730b 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c . */.balance_cl
1730c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 eanup:. sqlite3
1730d 50 61 67 65 46 72 65 65 28 61 53 70 61 63 65 32 PageFree(aSpace2
1730e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 );. sqlite3Scra
1730f 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b tchFree(apCell);
17310 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f . for(i=0; i<nO
17311 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 ld; i++){. re
17312 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b leasePage(apOld[
17313 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 i]);. }. for(i
17314 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 =0; i<nNew; i++)
17315 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 {. releasePag
17316 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d e(apNew[i]);. }
17317 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b . pCur->apPage[
17318 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f pCur->iPage]->nO
17319 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 verflow = 0;..
1731a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1731b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1731c 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 is called for t
1731d 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
1731e 61 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 65 a btree when the
1731f 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f root.** page co
17320 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e ntains no cells.
17321 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70 This is an opp
17322 6f 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 ortunity to make
17323 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 the tree.** sha
17324 6c 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 llower by one le
17325 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
17326 6e 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c nt balance_shall
17327 6f 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70 ower(BtCursor *p
17328 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 Cur){. MemPage
17329 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
1732a 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
1732b 65 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 e of B-Tree */.
1732c 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 MemPage *pChild
1732d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1732e 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 The only child
1732f 70 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f page of pPage */
17330 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c . Pgno pgnoChil
17331 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
17332 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 /* Page number f
17333 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 or pChild */. i
17334 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
17335 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 K; /* R
17336 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
17337 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f subprocedures */
17338 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
17339 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1733a 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 /* The main B
1733b 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a Tree structure *
1733c 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 /. int mxCellPe
1733d 72 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 rPage;
1733e 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 /* Maximum numb
1733f 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 er of cells per
17340 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 page */. u8 **a
17341 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 pCell;
17342 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 /* All ce
17343 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62 lls from pages b
17344 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f eing balanced */
17345 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 . u16 *szCell;
17346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17347 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 /* Local size of
17348 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 all cells */..
17349 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
1734a 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 Page==0 );. pPa
1734b 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1734c 65 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 e[0];.. assert(
1734d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 pPage->nCell==0
1734e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1734f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17350 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
17351 65 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 ex) );. pBt = p
17352 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 Page->pBt;. mxC
17353 65 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f ellPerPage = MX_
17354 43 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 CELL(pBt);. apC
17355 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ell = sqlite3Mal
17356 6c 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 loc( mxCellPerPa
17357 67 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b ge*(sizeof(u8*)+
17358 73 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a sizeof(u16)) );.
17359 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 if( apCell==0
1735a 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1735b 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 NOMEM;. szCell
1735c 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b = (u16*)&apCell[
1735d 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a mxCellPerPage];.
1735e 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
1735f 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 f ){. /* The
17360 74 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 table is complet
17361 65 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 ely empty */.
17362 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
17363 3a 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64 : empty table %d
17364 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f \n", pPage->pgno
17365 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ));. }else{.
17366 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 /* The root pag
17367 65 20 69 73 20 65 6d 70 74 79 20 62 75 74 20 68 e is empty but h
17368 61 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 as one child. T
17369 72 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20 ransfer the.
1736a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ** information f
1736b 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69 rom that one chi
1736c 6c 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 ld into the root
1736d 20 70 61 67 65 20 69 66 20 69 74 20 0a 20 20 20 page if it .
1736e 20 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 ** will fit. T
1736f 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20 his reduces the
17370 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 depth of the tre
17371 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a e by one.. **
17372 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 . ** If the r
17373 6f 6f 74 20 70 61 67 65 20 69 73 20 70 61 67 65 oot page is page
17374 20 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20 1, it has less
17375 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 space available
17376 74 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 than. ** its
17377 63 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68 child (due to th
17378 65 20 31 30 30 20 62 79 74 65 20 68 65 61 64 65 e 100 byte heade
17379 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 r that occurs at
1737a 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 the beginning.
1737b 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 ** of the dat
1737c 61 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 abase fle), so i
1737d 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 t might not be a
1737e 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 ble to hold all
1737f 6f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 of the . ** i
17380 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 nformation curre
17381 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 ntly contained i
17382 6e 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66 n the child. If
17383 20 74 68 69 73 20 69 73 20 74 68 65 20 0a 20 20 this is the .
17384 20 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 ** case, then
17385 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 do not do the tr
17386 61 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70 ansfer. Leave p
17387 61 67 65 20 31 20 65 6d 70 74 79 20 65 78 63 65 age 1 empty exce
17388 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 pt. ** for th
17389 65 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 e right-pointer
1738a 74 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 to the child pag
1738b 65 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70 61 e. The child pa
1738c 67 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a ge becomes. *
1738d 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f * the virtual ro
1738e 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a ot of the tree..
1738f 20 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f */. VVA_O
17390 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 NLY( pCur->pages
17391 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a Shuffled = 1 );.
17392 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 pgnoChild =
17393 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
17394 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
17395 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
17396 20 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 assert( pgnoChi
17397 6c 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 ld>0 );. asse
17398 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 rt( pgnoChild<=p
17399 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
1739a 61 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 age->pBt) );.
1739b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1739c 65 65 47 65 74 50 61 67 65 28 70 50 61 67 65 2d eeGetPage(pPage-
1739d 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c >pBt, pgnoChild,
1739e 20 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 &pChild, 0);.
1739f 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
173a0 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 end_shallow_bala
173a1 6e 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 nce;. if( pPa
173a2 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 ge->pgno==1 ){.
173a3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
173a4 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 3BtreeInitPage(p
173a5 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 Child);. if
173a6 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f ( rc ) goto end_
173a7 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b shallow_balance;
173a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
173a9 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 Child->nOverflow
173aa 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==0 );. if(
173ab 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d pChild->nFree>=
173ac 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 100 ){. /
173ad 2a 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f * The child info
173ae 72 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 rmation will fit
173af 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 on the root pag
173b0 65 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 e, so do the.
173b1 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a ** copy */.
173b2 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 int i;.
173b3 20 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28 zeroPage(
173b4 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 pPage, pChild->a
173b5 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 Data[0]);.
173b6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 for(i=0; i<pCh
173b7 69 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 ild->nCell; i++)
173b8 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 43 65 {. apCe
173b9 6c 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c ll[i] = findCell
173ba 28 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 (pChild,i);.
173bb 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 szCell[i]
173bc 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 = cellSizePtr(pC
173bd 68 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 hild, apCell[i])
173be 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
173bf 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 assemblePage
173c0 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e (pPage, pChild->
173c1 6e 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 nCell, apCell, s
173c2 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 zCell);.
173c3 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67 68 /* Copy the righ
173c4 74 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 t-pointer of the
173c5 20 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61 child to the pa
173c6 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 rent. */.
173c7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
173c8 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
173c9 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
173ca 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 );. put4
173cb 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
173cc 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
173cd 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 set+8], .
173ce 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 get4byte(&p
173cf 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 Child->aData[pCh
173d0 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 ild->hdrOffset+8
173d1 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ]));. rc
173d2 3d 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c = freePage(pChil
173d3 64 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 d);. TRAC
173d4 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 E(("BALANCE: chi
173d5 6c 64 20 25 64 20 74 72 61 6e 73 66 65 72 20 74 ld %d transfer t
173d6 6f 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 o page 1\n", pCh
173d7 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 ild->pgno));.
173d8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
173d9 20 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68 /* The child h
173da 61 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 as more informat
173db 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 ion that will fi
173dc 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 t on the root..
173dd 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72 ** The tr
173de 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 62 61 ee is already ba
173df 6c 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 lanced. Do noth
173e0 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ing. */.
173e1 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
173e2 20 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e child %d will n
173e3 6f 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31 ot fit on page 1
173e4 5c 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e \n", pChild->pgn
173e5 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 o));. }.
173e6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
173e7 6d 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74 mcpy(pPage->aDat
173e8 61 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 a, pChild->aData
173e9 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 , pPage->pBt->us
173ea 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 ableSize);.
173eb 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
173ec 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
173ed 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
173ee 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
173ef 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
173f0 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
173f1 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 freePage(pChild
173f2 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 );. TRACE((
173f3 22 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 "BALANCE: transf
173f4 65 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f er child %d into
173f5 20 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 root %d\n",.
173f6 20 20 20 20 20 20 20 20 20 20 20 70 43 68 69 6c pChil
173f7 64 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e d->pgno, pPage->
173f8 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 pgno));. }.
173f9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
173fa 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
173fb 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
173fc 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
173fd 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
173fe 43 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49 CUUM && rc==SQLI
173ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
17400 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d c = setChildPtrm
17401 61 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20 aps(pPage);.
17402 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c }.#endif. rel
17403 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 easePage(pChild)
17404 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f ;. }.end_shallo
17405 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c w_balance:. sql
17406 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c ite3_free(apCell
17407 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
17408 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f }.../*.** The ro
17409 6f 74 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 ot page is overf
1740a 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 ull.**.** When t
1740b 68 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65 his happens, Cre
1740c 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 ate a new child
1740d 70 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68 page and copy th
1740e 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 e.** contents of
1740f 20 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74 the root into t
17410 68 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20 he child. Then
17411 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a make the root.**
17412 20 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70 page an empty p
17413 61 67 65 20 77 69 74 68 20 72 69 67 68 74 43 68 age with rightCh
17414 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 ild pointing to
17415 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 the new.** child
17416 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c . Finally, cal
17417 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e l balance_intern
17418 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 al() on the new
17419 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73 child.** to caus
1741a 65 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a e it to split..*
1741b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c /.static int bal
1741c 61 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43 75 ance_deeper(BtCu
1741d 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 rsor *pCur){. i
1741e 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1741f 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c /* Return val
17420 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 ue from subproce
17421 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 dures */. MemPa
17422 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f ge *pPage; /
17423 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
17424 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 root page */.
17425 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b MemPage *pChild;
17426 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
17427 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 o a new child pa
17428 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e ge */. Pgno pgn
17429 6f 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 oChild; /* P
1742a 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
1742b 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 e new child page
1742c 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
1742d 70 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 pBt; /*
1742e 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 The BTree */. i
1742f 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 nt usableSize;
17430 20 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 /* Total usab
17431 6c 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 le size of a pag
17432 65 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b e */. u8 *data;
17433 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
17434 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 ntent of the par
17435 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 ent page */. u8
17436 20 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 *cdata;
17437 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
17438 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a the child page *
17439 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 /. int hdr;
1743a 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1743b 74 20 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 t to page header
1743c 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 in parent */.
1743d 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 int cbrk;
1743e 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
1743f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 content of firs
17440 74 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 t cell in parent
17441 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
17442 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b Cur->iPage==0 );
17443 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
17444 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 >apPage[0]->nOve
17445 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56 rflow>0 );.. VV
17446 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 A_ONLY( pCur->pa
17447 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 gesShuffled = 1
17448 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 );. pPage = pCu
17449 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 r->apPage[0];.
1744a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 pBt = pPage->pBt
1744b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1744c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1744d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1744e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1744f 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
17450 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
17451 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 );. rc = alloca
17452 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
17453 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 &pChild, &pgnoC
17454 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e hild, pPage->pgn
17455 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 o, 0);. if( rc
17456 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 ) return rc;. a
17457 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17458 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17459 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 Child->pDbPage)
1745a 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 );. usableSize
1745b 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
1745c 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 e;. data = pPag
1745d 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 e->aData;. hdr
1745e 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
1745f 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 et;. cbrk = get
17460 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
17461 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 5]);. cdata = p
17462 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 Child->aData;.
17463 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 memcpy(cdata, &d
17464 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d ata[hdr], pPage-
17465 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 >cellOffset+2*pP
17466 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b age->nCell-hdr);
17467 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 . memcpy(&cdata
17468 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 [cbrk], &data[cb
17469 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d rk], usableSize-
1746a 63 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74 cbrk);.. assert
1746b 28 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74 ( pChild->isInit
1746c 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ==0 );. rc = sq
1746d 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 lite3BtreeInitPa
1746e 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 ge(pChild);. if
1746f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
17470 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 ){. int nCopy
17471 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 = pPage->nOverf
17472 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65 low*sizeof(pPage
17473 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20 ->aOvfl[0]);.
17474 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e memcpy(pChild->
17475 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f aOvfl, pPage->aO
17476 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 vfl, nCopy);.
17477 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c pChild->nOverfl
17478 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ow = pPage->nOve
17479 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 rflow;. if( p
1747a 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 Child->nOverflow
1747b 20 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64 ){. pChild
1747c 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 ->nFree = 0;.
1747d 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1747e 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50 Child->nCell==pP
1747f 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
17480 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17481 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
17482 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
17483 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 ) );. zeroPag
17484 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d e(pPage, pChild-
17485 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 >aData[0] & ~PTF
17486 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 _LEAF);. put4
17487 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
17488 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
17489 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c set+8], pgnoChil
1748a 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 d);. TRACE(("
1748b 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f BALANCE: copy ro
1748c 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 ot %d into %d\n"
1748d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 , pPage->pgno, p
1748e 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 Child->pgno));.
1748f 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
17490 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 UUM ){. rc
17491 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c = ptrmapPut(pBt,
17492 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 pChild->pgno, P
17493 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
17494 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 ge->pgno);.#ifnd
17495 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
17496 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
17497 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17498 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
17499 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 = setChildPtrmap
1749a 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 s(pChild);.
1749b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 }. if( rc
1749c 29 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c ){. pChil
1749d 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 d->nOverflow = 0
1749e 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
1749f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
174a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
174a1 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 ){. pCur->iPa
174a2 67 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e ge++;. pCur->
174a3 61 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 apPage[1] = pChi
174a4 6c 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 ld;. pCur->ai
174a5 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 Idx[0] = 0;.
174a6 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e rc = balance_non
174a7 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 root(pCur);. }e
174a8 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 lse{. release
174a9 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 Page(pChild);.
174aa 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
174ab 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 }../*.** The pag
174ac 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 e that pCur curr
174ad 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ently points to
174ae 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f has just been mo
174af 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d dified in.** som
174b0 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 e way. This func
174b1 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 tion figures out
174b2 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 if this modific
174b3 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a ation means the.
174b4 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f ** tree needs to
174b5 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e be balanced, an
174b6 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 d if so calls th
174b7 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 e appropriate ba
174b8 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 lancing .** rout
174b9 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 ine..** .** Para
174ba 6d 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 meter isInsert i
174bb 73 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 s true if a new
174bc 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e cell was just in
174bd 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a serted into the.
174be 2a 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 ** page, or fals
174bf 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a e otherwise..*/.
174c0 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
174c1 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ce(BtCursor *pCu
174c2 72 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 r, int isInsert)
174c3 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
174c4 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
174c5 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d e *pPage = pCur-
174c6 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
174c7 61 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 age];.. assert(
174c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
174c9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
174ca 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
174cb 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 pCur->iPage==0 )
174cc 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
174cd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
174ce 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
174cf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
174d0 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f _OK && pPage->nO
174d1 76 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 verflow>0 ){.
174d2 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f rc = balance_
174d3 64 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 deeper(pCur);.
174d4 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
174d5 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61 ->apPage[0]==pPa
174d6 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ge );. asse
174d7 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
174d8 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53 flow==0 || rc!=S
174d9 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
174da 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
174db 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
174dc 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 ->nCell==0 ){.
174dd 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
174de 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29 _shallower(pCur)
174df 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
174e0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d pCur->apPage[0]=
174e1 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 =pPage );.
174e2 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
174e3 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 Overflow==0 || r
174e4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
174e5 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
174e6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f if( pPage->nO
174e7 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 verflow>0 || .
174e8 20 20 20 20 20 20 28 21 69 73 49 6e 73 65 72 74 (!isInsert
174e9 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 && pPage->nFree
174ea 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 >pPage->pBt->usa
174eb 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a bleSize*2/3) ){.
174ec 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e rc = balan
174ed 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 ce_nonroot(pCur)
174ee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
174ef 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
174f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
174f1 68 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 hecks all cursor
174f2 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 s that point to
174f3 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a table pgnoRoot..
174f4 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f ** If any of tho
174f5 73 65 20 63 75 72 73 6f 72 73 20 77 65 72 65 20 se cursors were
174f6 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c opened with wrFl
174f7 61 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65 ag==0 in a diffe
174f8 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 rent.** database
174f9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 connection (a d
174fa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
174fb 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 on that shares t
174fc 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 he pager.** cach
174fd 65 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 e with the curre
174fe 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 nt connection) a
174ff 6e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f nd that other co
17500 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 nnection .** is
17501 6e 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64 55 not in the ReadU
17502 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 ncommmitted stat
17503 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 e, then this rou
17504 74 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a tine returns .**
17505 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
17506 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 **.** As well as
17507 20 63 75 72 73 6f 72 73 20 77 69 74 68 20 77 72 cursors with wr
17508 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 Flag==0, cursors
17509 20 77 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72 with .** isIncr
1750a 62 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 blobHandle==1 ar
1750b 65 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 e also considere
1750c 64 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 d 'read' cursors
1750d 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72 because.** incr
1750e 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 emental blob cur
1750f 73 6f 72 73 20 61 72 65 20 75 73 65 64 20 66 6f sors are used fo
17510 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 r both reading a
17511 6e 64 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a nd writing..**.*
17512 2a 20 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20 * When pgnoRoot
17513 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 is the root page
17514 20 6f 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 of an intkey ta
17515 62 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ble, this functi
17516 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 on is also.** re
17517 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e sponsible for in
17518 76 61 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65 validating incre
17519 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 mental blob curs
1751a 6f 72 73 20 77 68 65 6e 20 74 68 65 20 74 61 62 ors when the tab
1751b 6c 65 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69 le row.** on whi
1751c 63 68 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e ch they are open
1751d 65 64 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 ed is deleted or
1751e 20 6d 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f modified. Curso
1751f 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 rs are invalidat
17520 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 ed.** according
17521 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
17522 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 rules:.**.**
17523 31 29 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65 1) When BtreeCle
17524 61 72 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c arTable() is cal
17525 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c led to completel
17526 79 20 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e y delete the con
17527 74 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66 tents.** of
17528 20 61 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c a B-Tree table,
17529 20 70 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 pExclude is set
1752a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72 to zero and par
1752b 61 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a ameter iRow is .
1752c 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e ** set to n
1752d 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 on-zero. In this
1752e 20 63 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d case all increm
1752f 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f ental blob curso
17530 72 73 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20 rs open.**
17531 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f on the table roo
17532 74 65 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 ted at pgnoRoot
17533 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e are invalidated.
17534 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e .**.** 2) When
17535 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20 BtreeInsert(),
17536 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 BtreeDelete() or
17537 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 20 BtreePutData()
17538 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a is called to .**
17539 20 20 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74 modify a t
1753a 61 62 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20 able row via an
1753b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 SQL statement, p
1753c 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 Exclude is set t
1753d 6f 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77 o the .** w
1753e 72 69 74 65 20 63 75 72 73 6f 72 20 75 73 65 64 rite cursor used
1753f 20 74 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66 to do the modif
17540 69 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61 ication and para
17541 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 73 65 meter iRow is se
17542 74 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 t.** to the
17543 20 69 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20 integer row id
17544 6f 66 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e of the B-Tree en
17545 74 72 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 try being modifi
17546 65 64 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20 ed. Unless.**
17547 20 20 20 70 45 78 63 6c 75 64 65 20 69 73 20 69 pExclude is i
17548 74 73 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65 tself an increme
17549 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 ntal blob cursor
1754a 2c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65 , then all incre
1754b 6d 65 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62 mental.** b
1754c 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e lob cursors open
1754d 20 6f 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20 on row iRow of
1754e 74 68 65 20 42 2d 54 72 65 65 20 61 72 65 20 69 the B-Tree are i
1754f 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a nvalidated..**.*
17550 2a 20 20 20 33 29 20 49 66 20 62 6f 74 68 20 70 * 3) If both p
17551 45 78 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77 Exclude and iRow
17552 20 61 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f are set to zero
17553 2c 20 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c , no incremental
17554 20 62 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63 blob .** c
17555 75 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c ursors are inval
17556 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 idated..*/.stati
17557 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65 c int checkForRe
17558 61 64 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42 adConflicts(. B
17559 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 tree *pBtree,
1755a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
1755b 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 tabase file to c
1755c 68 65 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 heck */. Pgno p
1755d 67 6e 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 gnoRoot,
1755e 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65 /* Look for re
1755f 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 ad cursors on th
17560 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 is btree */. Bt
17561 43 75 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65 Cursor *pExclude
17562 2c 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 , /* Ignore
17563 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 this cursor */.
17564 20 69 36 34 20 69 52 6f 77 20 20 20 20 20 20 20 i64 iRow
17565 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
17566 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 rowid that might
17567 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a be changing */.
17568 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
17569 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
1756a 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b t = pBtree->pBt;
1756b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1756c 20 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 pBtree->db;. a
1756d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1756e 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 reeHoldsMutex(pB
1756f 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 tree) );. for(p
17570 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 =pBt->pCursor; p
17571 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 ; p=p->pNext){.
17572 20 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 if( p==pExclu
17573 64 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 de ) continue;.
17574 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f if( p->pgnoRo
17575 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 ot!=pgnoRoot ) c
17576 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 ontinue;.#ifndef
17577 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
17578 52 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d RBLOB. if( p-
17579 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c >isIncrblobHandl
1757a 65 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 e && ( .
1757b 20 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 (!pExclude && i
1757c 52 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 Row). || (p
1757d 45 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 Exclude && !pExc
1757e 6c 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 lude->isIncrblob
1757f 48 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 Handle && p->inf
17580 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 o.nKey==iRow).
17581 20 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 )){. p->e
17582 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
17583 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 NVALID;. }.#e
17584 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e ndif. if( p->
17585 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
17586 41 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b ALID ) continue;
17587 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c . if( p->wrFl
17588 61 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 ag==0 .#ifndef S
17589 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1758a 4c 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 LOB. || p->i
1758b 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a sIncrblobHandle.
1758c 23 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 #endif. ){.
1758d 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f sqlite3 *dbO
1758e 74 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 ther = p->pBtree
1758f 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65 ->db;. asse
17590 72 74 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20 rt(dbOther);.
17591 20 20 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d if( dbOther!=
17592 64 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e db && (dbOther->
17593 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 flags & SQLITE_R
17594 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d eadUncommitted)=
17595 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 =0 ){. sq
17596 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
17597 6c 6f 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68 locked(db, dbOth
17598 65 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 er);. ret
17599 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
1759a 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 D_SHAREDCACHE;.
1759b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1759c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1759d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e _OK;.}../*.** In
1759e 73 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 sert a new recor
1759f 64 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 d into the BTree
175a0 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 . The key is gi
175a1 76 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 ven by (pKey,nKe
175a2 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 y).** and the da
175a3 74 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 ta is given by (
175a4 70 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 pData,nData). T
175a5 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 he cursor is use
175a6 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 d only to.** def
175a7 69 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 ine what table t
175a8 68 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 he record should
175a9 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 be inserted int
175aa 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a o. The cursor.*
175ab 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 * is left pointi
175ac 6e 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c ng at a random l
175ad 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 ocation..**.** F
175ae 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 or an INTKEY tab
175af 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 le, only the nKe
175b0 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b y value of the k
175b1 65 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 ey is used. pKe
175b2 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e y is.** ignored.
175b3 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 For a ZERODATA
175b4 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 table, the pDat
175b5 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 a and nData are
175b6 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a both ignored..**
175b7 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 .** If the seekR
175b8 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 esult parameter
175b9 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
175ba 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 n a successful c
175bb 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
175bc 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 74 3BtreeMoveto() t
175bd 6f 20 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43 o seek cursor pC
175be 75 72 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 ur to (pKey, nKe
175bf 79 29 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a y) has already.*
175c0 2a 20 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 * been performed
175c1 2e 20 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20 . seekResult is
175c2 74 68 65 20 73 65 61 72 63 68 20 72 65 73 75 6c the search resul
175c3 74 20 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65 t returned (a ne
175c4 67 61 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 gative.** number
175c5 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 if pCur points
175c6 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
175c7 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
175c8 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f (pKey, nKey), o
175c9 72 0a 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20 r.** a positive
175ca 76 61 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f value if pCur po
175cb 69 6e 74 73 20 61 74 20 61 6e 20 65 74 72 79 20 ints at an etry
175cc 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 that is larger t
175cd 68 61 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e han .** (pKey, n
175ce 4b 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 Key)). .**.** If
175cf 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 the seekResult
175d0 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 parameter is 0,
175d1 74 68 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72 then cursor pCur
175d2 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 6e may point to an
175d3 79 20 0a 2a 2a 20 65 6e 74 72 79 20 6f 72 20 74 y .** entry or t
175d4 6f 20 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c o no entry at al
175d5 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 l. In this case
175d6 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 this function ha
175d7 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65 s to seek.** the
175d8 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74 cursor before t
175d9 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62 he new key can b
175da 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 e inserted..*/.S
175db 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
175dc 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e t sqlite3BtreeIn
175dd 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 sert(. BtCursor
175de 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 *pCur,
175df 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 /* Insert
175e0 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 data into the t
175e1 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 able of this cur
175e2 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 sor */. const v
175e3 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e oid *pKey, i64 n
175e4 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b Key, /* The k
175e5 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 ey of the new re
175e6 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 cord */. const
175e7 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 void *pData, int
175e8 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 nData, /* The
175e9 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 data of the new
175ea 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 record */. int
175eb 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 nZero,
175ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
175ed 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 mber of extra 0
175ee 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 bytes to append
175ef 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 to data */. int
175f0 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20 appendBias,
175f1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
175f2 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c rue if this is l
175f3 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 ikely an append
175f4 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 */. int seekRes
175f5 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ult
175f6 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
175f7 20 70 72 69 6f 72 20 73 71 6c 69 74 65 33 42 74 prior sqlite3Bt
175f8 72 65 65 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c reeMoveto() call
175f9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
175fa 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 . int loc = see
175fb 6b 52 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 73 kResult;. int s
175fc 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b zNew;. int idx;
175fd 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
175fe 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 e;. Btree *p =
175ff 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 pCur->pBtree;.
17600 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
17601 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e p->pBt;. unsign
17602 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c ed char *oldCell
17603 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
17604 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a r *newCell = 0;.
17605 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
17606 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
17607 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
17608 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
17609 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
1760a 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74 ;. assert( !pBt
1760b 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 ->readOnly );.
1760c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 assert( pCur->wr
1760d 46 6c 61 67 20 29 3b 0a 20 20 72 63 20 3d 20 63 Flag );. rc = c
1760e 68 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c heckForReadConfl
1760f 69 63 74 73 28 70 43 75 72 2d 3e 70 42 74 72 65 icts(pCur->pBtre
17610 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f e, pCur->pgnoRoo
17611 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79 29 3b 0a t, pCur, nKey);.
17612 20 20 69 66 28 20 72 63 20 29 7b 20 20 20 20 20 if( rc ){
17613 20 20 20 20 20 20 20 20 0a 20 20 20 20 2f 2a 20 . /*
17614 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 70 The table pCur p
17615 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 72 oints to has a r
17616 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ead lock */.
17617 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
17618 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
17619 43 41 43 48 45 20 29 3b 0a 20 20 20 20 72 65 74 CACHE );. ret
1761a 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 urn rc;. }. if
1761b 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
1761c 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a CURSOR_FAULT ){.
1761d 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d return pCur-
1761e 3e 73 6b 69 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a >skip;. }.. /*
1761f 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 Save the positi
17620 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 ons of any other
17621 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
17622 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20 20 2a this table.. *
17623 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20 63 *. ** In some c
17624 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c 20 74 ases, the call t
17625 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f o sqlite3BtreeMo
17626 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 veto() below is
17627 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a a no-op. For. *
17628 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 * example, when
17629 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 inserting data i
1762a 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 nto a table with
1762b 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 auto-generated
1762c 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 integer. ** key
1762d 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 s, the VDBE laye
1762e 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 r invokes sqlite
1762f 33 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 3BtreeLast() to
17630 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a figure out the .
17631 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 ** integer key
17632 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e to use. It then
17633 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 calls this func
17634 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 tion to actually
17635 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a insert the . *
17636 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 * data into the
17637 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 intkey B-Tree. I
17638 6e 20 74 68 69 73 20 63 61 73 65 20 73 71 6c 69 n this case sqli
17639 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
1763a 20 72 65 63 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a recognizes. **
1763b 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
1763c 20 69 73 20 61 6c 72 65 61 64 79 20 77 68 65 72 is already wher
1763d 65 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 e it needs to be
1763e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 77 69 74 and returns wit
1763f 68 6f 75 74 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 hout. ** doing
17640 61 6e 79 20 77 6f 72 6b 2e 20 54 6f 20 61 76 6f any work. To avo
17641 69 64 20 74 68 77 61 72 74 69 6e 67 20 74 68 65 id thwarting the
17642 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 se optimizations
17643 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e , it is importan
17644 74 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c t. ** not to cl
17645 65 61 72 20 74 68 65 20 63 75 72 73 6f 72 20 68 ear the cursor h
17646 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 0a ere.. */. if(.
17647 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 SQLITE_OK!=(
17648 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 rc = saveAllCurs
17649 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 ors(pBt, pCur->p
1764a 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 20 gnoRoot, pCur))
1764b 7c 7c 20 28 21 6c 6f 63 20 26 26 0a 20 20 20 20 || (!loc &&.
1764c 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
1764d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1764e 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 eto(pCur, pKey,
1764f 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73 nKey, appendBias
17650 2c 20 26 6c 6f 63 29 29 0a 20 20 29 29 7b 0a 20 , &loc)). )){.
17651 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
17652 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 }.. pPage = pCu
17653 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
17654 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
17655 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
17656 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 || nKey>=0 );.
17657 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c assert( pPage->l
17658 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 eaf || !pPage->i
17659 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45 ntKey );. TRACE
1765a 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 (("INSERT: table
1765b 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 =%d nkey=%lld nd
1765c 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 ata=%d page=%d %
1765d 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
1765e 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
1765f 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 nKey, nData, pPa
17660 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 ge->pgno,.
17661 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 loc==0 ? "ov
17662 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 erwrite" : "new
17663 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 entry"));. asse
17664 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
17665 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 t );. allocateT
17666 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
17667 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e newCell = pBt->
17668 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 pTmpSpace;. if(
17669 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 newCell==0 ) re
1766a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1766b 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e M;. rc = fillIn
1766c 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 Cell(pPage, newC
1766d 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c ell, pKey, nKey,
1766e 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e pData, nData, n
1766f 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 Zero, &szNew);.
17670 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 if( rc ) goto e
17671 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 nd_insert;. ass
17672 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c ert( szNew==cell
17673 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e SizePtr(pPage, n
17674 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 ewCell) );. ass
17675 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 ert( szNew<=MX_C
17676 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b ELL_SIZE(pBt) );
17677 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 . idx = pCur->a
17678 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
17679 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 ];. if( loc==0
1767a 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d && CURSOR_VALID=
1767b 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b =pCur->eState ){
1767c 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a . u16 szOld;.
1767d 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c assert( idx<
1767e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
1767f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17680 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
17681 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
17682 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
17683 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b goto end_insert;
17684 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 . }. oldCe
17685 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
17686 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 age, idx);. i
17687 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
17688 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
17689 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c newCell, oldCell
1768a 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 4);. }.
1768b 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 szOld = cellSize
1768c 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 Ptr(pPage, oldCe
1768d 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c ll);. rc = cl
1768e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f earCell(pPage, o
1768f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 ldCell);. if(
17690 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 rc ) goto end_i
17691 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20 nsert;. rc =
17692 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 dropCell(pPage,
17693 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20 idx, szOld);.
17694 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17695 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 OK ) {. got
17696 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 o end_insert;.
17697 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
17698 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e loc<0 && pPage->
17699 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 nCell>0 ){. a
1769a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 ssert( pPage->le
1769b 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 af );. idx =
1769c 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ++pCur->aiIdx[pC
1769d 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
1769e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1769f 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
176a0 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
176a1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
176a2 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 rt( pPage->leaf
176a3 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e );. }. rc = in
176a4 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 sertCell(pPage,
176a5 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a idx, newCell, sz
176a6 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 61 73 New, 0, 0);. as
176a7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
176a8 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43 _OK || pPage->nC
176a9 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e ell>0 || pPage->
176aa 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a nOverflow>0 );..
176ab 20 20 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 /* If no error
176ac 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20 63 61 has occured, ca
176ad 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 ll balance() to
176ae 64 65 61 6c 20 77 69 74 68 20 61 6e 79 20 6f 76 deal with any ov
176af 65 72 66 6c 6f 77 20 61 6e 64 0a 20 20 2a 2a 20 erflow and. **
176b0 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 move the cursor
176b1 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 to point at the
176b2 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
176b3 65 20 28 73 69 6e 63 65 20 62 61 6c 61 6e 63 65 e (since balance
176b4 20 6d 61 79 0a 20 20 2a 2a 20 68 61 76 65 20 72 may. ** have r
176b5 65 61 72 72 61 6e 67 65 64 20 74 68 65 20 74 61 earranged the ta
176b6 62 6c 65 20 69 6e 20 73 75 63 68 20 61 20 77 61 ble in such a wa
176b7 79 20 61 73 20 74 6f 20 69 6e 76 61 6c 69 64 61 y as to invalida
176b8 74 65 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 te BtCursor.apPa
176b9 67 65 5b 5d 0a 20 20 2a 2a 20 6f 72 20 42 74 43 ge[]. ** or BtC
176ba 75 72 73 6f 72 2e 61 69 49 64 78 5b 5d 29 2e 0a ursor.aiIdx[])..
176bb 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 **. ** Except
176bc 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 , if all of the
176bd 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
176be 75 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 3a 0a ue, do nothing:.
176bf 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 49 6e **. ** * In
176c0 73 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 serting the new
176c1 63 65 6c 6c 20 64 69 64 20 6e 6f 74 20 63 61 75 cell did not cau
176c2 73 65 20 6f 76 65 72 66 6c 6f 77 2c 0a 20 20 2a se overflow,. *
176c3 2a 0a 20 20 2a 2a 20 20 20 2a 20 42 65 66 6f 72 *. ** * Befor
176c4 65 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20 e inserting the
176c5 6e 65 77 20 63 65 6c 6c 20 74 68 65 20 63 75 72 new cell the cur
176c6 73 6f 72 20 77 61 73 20 70 6f 69 6e 74 69 6e 67 sor was pointing
176c7 20 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 20 20 at the . **
176c8 20 20 6c 61 72 67 65 73 74 20 6b 65 79 20 69 6e largest key in
176c9 20 61 6e 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 an intkey B-Tre
176ca 65 2c 20 61 6e 64 0a 20 20 2a 2a 0a 20 20 2a 2a e, and. **. **
176cb 20 20 20 2a 20 54 68 65 20 6b 65 79 20 76 61 6c * The key val
176cc 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ue associated wi
176cd 74 68 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 th the new cell
176ce 69 73 20 6e 6f 77 20 74 68 65 20 6c 61 72 67 65 is now the large
176cf 73 74 20 0a 20 20 2a 2a 20 20 20 20 20 69 6e 20 st . ** in
176d0 74 68 65 20 42 2d 54 72 65 65 2e 0a 20 20 2a 2a the B-Tree.. **
176d1 0a 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 . ** In this ca
176d2 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 se the cursor ca
176d3 6e 20 62 65 20 73 61 66 65 6c 79 20 6c 65 66 74 n be safely left
176d4 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 pointing at the
176d5 20 28 6e 65 77 29 20 0a 20 20 2a 2a 20 6c 61 72 (new) . ** lar
176d6 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20 69 gest key value i
176d7 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 44 6f n the B-Tree. Do
176d8 69 6e 67 20 73 6f 20 73 70 65 65 64 73 20 75 70 ing so speeds up
176d9 20 69 6e 73 65 72 74 69 6e 67 20 61 20 73 65 74 inserting a set
176da 0a 20 20 2a 2a 20 6f 66 20 65 6e 74 72 69 65 73 . ** of entries
176db 20 77 69 74 68 20 69 6e 63 72 65 61 73 69 6e 67 with increasing
176dc 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c integer key val
176dd 75 65 73 20 76 69 61 20 61 20 73 69 6e 67 6c 65 ues via a single
176de 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 28 63 6f cursor. ** (co
176df 6d 65 73 20 75 70 20 77 69 74 68 20 22 49 4e 53 mes up with "INS
176e0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c ERT INTO ... SEL
176e1 45 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 ECT ..." stateme
176e2 6e 74 73 29 2c 20 61 73 20 0a 20 20 2a 2a 20 74 nts), as . ** t
176e3 68 65 20 6e 65 78 74 20 69 6e 73 65 72 74 20 6f he next insert o
176e4 70 65 72 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 peration is not
176e5 72 65 71 75 69 72 65 64 20 74 6f 20 73 65 65 6b required to seek
176e6 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a the cursor.. *
176e7 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
176e8 54 45 5f 4f 4b 20 0a 20 20 20 26 26 20 28 70 50 TE_OK . && (pP
176e9 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c age->nOverflow |
176ea 7c 20 21 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 | !pCur->atLast
176eb 7c 7c 20 6c 6f 63 3e 3d 30 20 7c 7c 20 21 70 43 || loc>=0 || !pC
176ec 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
176ed 6e 74 4b 65 79 29 0a 20 20 29 7b 0a 20 20 20 20 ntKey). ){.
176ee 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
176ef 72 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 r, 1);. if( r
176f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
176f1 20 20 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 moveToRoot
176f2 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (pCur);. }.
176f3 7d 0a 20 20 0a 20 20 2f 2a 20 4d 75 73 74 20 6d }. . /* Must m
176f4 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c ake sure nOverfl
176f5 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a ow is reset to z
176f6 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 ero even if the
176f7 62 61 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66 balance(). ** f
176f8 61 69 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20 ails. Internal
176f9 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 data structure c
176fa 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 orruption will r
176fb 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e esult otherwise.
176fc 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 */. pCur->apPa
176fd 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
176fe 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >nOverflow = 0;.
176ff 0a 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 .end_insert:. r
17700 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17701 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e ** Delete the en
17702 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 try that the cur
17703 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
17704 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a to. The cursor.
17705 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 ** is left point
17706 69 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61 ing at a arbitra
17707 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a ry location..*/.
17708 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17709 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
1770a 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a elete(BtCursor *
1770b 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 pCur){. MemPage
1770c 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
1770d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1770e 67 65 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a ge];. int idx;.
1770f 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
17710 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 *pCell;. int rc
17711 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 ;. Pgno pgnoChi
17712 6c 64 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 ld = 0;. Btree
17713 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 *p = pCur->pBtre
17714 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e;. BtShared *p
17715 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 Bt = p->pBt;..
17716 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
17717 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
17718 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
17719 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 e->isInit );. a
1771a 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
1771b 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
1771c 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
1771d 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e rt( !pBt->readOn
1771e 6c 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 ly );. if( pCur
1771f 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17720 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
17721 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b turn pCur->skip;
17722 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 . }. if( NEVER
17723 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 (pCur->aiIdx[pCu
17724 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 r->iPage]>=pPage
17725 2d 3e 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 ->nCell) ){.
17726 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
17727 52 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 ROR; /* The cur
17728 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 sor is not point
17729 69 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 ing to anything
1772a 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 */. }. assert(
1772b 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b pCur->wrFlag );
1772c 0a 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 . rc = checkFor
1772d 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c ReadConflicts(p,
1772e 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
1772f 20 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66 pCur, pCur->inf
17730 6f 2e 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72 o.nKey);. if( r
17731 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
17732 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 /* The table
17733 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 pCur points to
17734 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 has a read lock
17735 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 */. assert( r
17736 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 c==SQLITE_LOCKED
17737 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a _SHAREDCACHE );.
17738 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17739 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 }.. /* Restore
1773a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 the current cur
1773b 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 sor position (a
1773c 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 no-op if the cur
1773d 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 sor is not in .
1773e 20 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 ** CURSOR_REQUI
1773f 52 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e RESEEK state) an
17740 64 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 d save the posit
17741 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 ions of any othe
17742 72 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 r cursors . **
17743 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
17744 20 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c table. Then cal
17745 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 l sqlite3PagerWr
17746 69 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 ite() on the pag
17747 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 e. ** that the
17748 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 entry will be de
17749 6c 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f leted from.. */
1774a 0a 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 . if( . (rc
1774b 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
1774c 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d osition(pCur))!=
1774d 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 0 ||. (rc = s
1774e 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
1774f 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t, pCur->pgnoRoo
17750 74 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a t, pCur))!=0 ||.
17751 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 (rc = sqlite
17752 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
17753 65 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a e->pDbPage))!=0.
17754 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ){. return
17755 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f rc;. }.. /* Lo
17756 63 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 cate the cell wi
17757 74 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e thin its page an
17758 64 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f d leave pCell po
17759 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 inting to the.
1775a 2a 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 ** data. The cle
1775b 61 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 arCell() call fr
1775c 65 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 ees any overflow
1775d 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 pages associate
1775e 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 d with the. **
1775f 63 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 cell. The cell i
17760 74 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 tself is still i
17761 6e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 ntact.. */. id
17762 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
17763 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
17764 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
17765 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 (pPage, idx);.
17766 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
17767 20 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c ){. pgnoChil
17768 64 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 d = get4byte(pCe
17769 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 ll);. }. rc =
1776a 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c clearCell(pPage,
1776b 20 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 pCell);. if( r
1776c 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
1776d 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 rc;. }.. if( !
1776e 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
1776f 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 /*. ** The
17770 20 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 entry we are ab
17771 6f 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 out to delete is
17772 20 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 not a leaf so i
17773 66 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 f we do not.
17774 2a 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 ** do something
17775 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 we will leave a
17776 68 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 hole on an inter
17777 6e 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a nal page.. **
17778 20 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c We have to fill
17779 20 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 the hole by mov
1777a 69 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 ing in a cell fr
1777b 6f 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a om a leaf. The.
1777c 20 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c ** next Cell
1777d 20 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 after the one t
1777e 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 o be deleted is
1777f 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 guaranteed to ex
17780 69 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 ist and. ** t
17781 6f 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 o be a leaf so w
17782 65 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 e can use it..
17783 20 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f */. BtCurso
17784 72 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d r leafCur;. M
17785 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 emPage *pLeafPag
17786 65 20 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 e = 0;.. unsi
17787 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 gned char *pNext
17788 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 ;. int notUse
17789 64 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 d;. unsigned
1778a 63 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d char *tempCell =
1778b 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0;. assert(
1778c 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 !pPage->intKey )
1778d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1778e 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 eeGetTempCursor(
1778f 70 43 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b pCur, &leafCur);
17790 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
17791 33 42 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 3BtreeNext(&leaf
17792 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a Cur, ¬Used);.
17793 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
17794 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 TE_OK ){. a
17795 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 ssert( leafCur.a
17796 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 iIdx[leafCur.iPa
17797 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ge]==0 );.
17798 70 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 pLeafPage = leaf
17799 43 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 Cur.apPage[leafC
1779a 75 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 ur.iPage];.
1779b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1779c 65 72 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 erWrite(pLeafPag
1779d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
1779e 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1779f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
177a0 20 20 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 int leafCursor
177a1 49 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 Invalid = 0;.
177a2 20 20 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 u16 szNext;.
177a3 20 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c TRACE(("DEL
177a4 45 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 ETE: table=%d de
177a5 6c 65 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 lete internal fr
177a6 6f 6d 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 om %d replace fr
177a7 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 om leaf %d\n",.
177a8 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 pCur->pg
177a9 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 noRoot, pPage->p
177aa 67 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e gno, pLeafPage->
177ab 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 pgno));. dr
177ac 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 opCell(pPage, id
177ad 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 x, cellSizePtr(p
177ae 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 Page, pCell));.
177af 20 20 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e pNext = fin
177b0 64 43 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c dCell(pLeafPage,
177b1 20 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 0);. szNex
177b2 74 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 t = cellSizePtr(
177b3 70 4c 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 pLeafPage, pNext
177b4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
177b5 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 MX_CELL_SIZE(pB
177b6 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a t)>=szNext+4 );.
177b7 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 allocateTe
177b8 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 mpSpace(pBt);.
177b9 20 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 tempCell = p
177ba 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 Bt->pTmpSpace;.
177bb 20 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c if( tempCel
177bc 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 l==0 ){.
177bd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
177be 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 M;. }.
177bf 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
177c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
177c1 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 = insertCell(pP
177c2 61 67 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d age, idx, pNext-
177c3 34 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 4, szNext+4, tem
177c4 70 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 pCell, 0);.
177c5 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 }... /* Th
177c6 65 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 e "if" statement
177c7 20 69 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 in the next cod
177c8 65 20 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 e block is criti
177c9 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 cal. The.
177ca 2a 2a 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 ** slightest err
177cb 6f 72 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 or in that state
177cc 6d 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 ment would allow
177cd 20 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 SQLite to opera
177ce 74 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 te. ** corr
177cf 65 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 ectly most of th
177d0 65 20 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 e time but produ
177d1 63 65 20 76 65 72 79 20 72 61 72 65 20 66 61 69 ce very rare fai
177d2 6c 75 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 lures. To.
177d3 20 2a 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 ** guard agains
177d4 74 20 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c t this, the foll
177d5 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c owing macros hel
177d6 70 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 p to verify that
177d7 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 . ** the "i
177d8 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 f" statement is
177d9 77 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 well tested..
177da 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 */. test
177db 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 case( pPage->nOv
177dc 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 erflow==0 && pPa
177dd 67 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 ge->nFree<pBt->u
177de 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 sableSize*2/3 .
177df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177e0 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 && pLeafPage->nF
177e1 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 ree+2+szNext > p
177e2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
177e3 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 /3 );. test
177e4 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 case( pPage->nOv
177e5 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 erflow==0 && pPa
177e6 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e ge->nFree==pBt->
177e7 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a usableSize*2/3 .
177e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177e9 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e && pLeafPage->n
177ea 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 Free+2+szNext >
177eb 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
177ec 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 2/3 );. tes
177ed 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f tcase( pPage->nO
177ee 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 verflow==0 && pP
177ef 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d age->nFree==pBt-
177f0 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b >usableSize*2/3+
177f1 31 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1 .
177f2 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 && pLeafPage
177f3 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 ->nFree+2+szNext
177f4 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 > pBt->usableSi
177f5 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 ze*2/3 );.
177f6 74 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d testcase( pPage-
177f7 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 >nOverflow>0 &&
177f8 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 pPage->nFree<=pB
177f9 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
177fa 33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3.
177fb 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d && pLeafPage-
177fc 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 >nFree+2+szNext
177fd 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a > pBt->usableSiz
177fe 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 e*2/3 );. t
177ff 65 73 74 63 61 73 65 28 20 28 70 50 61 67 65 2d estcase( (pPage-
17800 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 >nOverflow>0 ||
17801 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 (pPage->nFree >
17802 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
17803 32 2f 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 2/3)).
17804 20 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 && pLeafP
17805 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e age->nFree+2+szN
17806 65 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 ext == pBt->usab
17807 6c 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a leSize*2/3 );...
17808 20 20 20 20 20 20 69 66 28 20 28 70 50 61 67 65 if( (pPage
17809 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c ->nOverflow>0 ||
1780a 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e (pPage->nFree >
1780b 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
1780c 2a 32 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 *2/3)) &&.
1780d 20 20 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e (pLeafPage->
1780e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e nFree+2+szNext >
1780f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
17810 2a 32 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 *2/3). ){.
17811 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 /* This b
17812 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 ranch is taken i
17813 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e f the internal n
17814 6f 64 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 ode is now eithe
17815 72 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 r overflowing.
17816 20 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 ** or unde
17817 72 66 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 rfull and the le
17818 61 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 af node will be
17819 75 6e 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 underfull after
1781a 74 68 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 the just cell .
1781b 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 ** copied
1781c 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c to the internal
1781d 20 6e 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 node is deleted
1781e 20 66 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 from it. This i
1781f 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 s a special.
17820 20 20 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 ** case beca
17821 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 use the call to
17822 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 balance() to cor
17823 72 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 rect the interna
17824 6c 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a l node. *
17825 2a 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 * may change the
17826 20 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 tree structure
17827 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 and invalidate t
17828 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 he contents of.
17829 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 ** the le
1782a 61 66 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 afCur.apPage[] a
1782b 6e 64 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 nd leafCur.aiIdx
1782c 5b 5d 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 [] arrays, which
1782d 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 will be.
1782e 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 ** used by the
1782f 62 61 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 balance() requir
17830 65 64 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 ed to correct th
17831 65 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 e underfull leaf
17832 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 . ** node
17833 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
17834 20 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d ** The form
17835 75 6c 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 ula used in the
17836 65 78 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 expression above
17837 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 are based on fa
17838 63 65 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 cets of.
17839 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 ** the SQLite fi
1783a 6c 65 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 le-format that d
1783b 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 o not change ove
1783c 72 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 r time..
1783d 2a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 */. testc
1783e 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 ase( pPage->nFre
1783f 65 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e==pBt->usableSi
17840 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 ze*2/3+1 );.
17841 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c testcase( pL
17842 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 eafPage->nFree+2
17843 2b 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 +szNext==pBt->us
17844 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 ableSize*2/3+1 )
17845 3b 0a 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 ;. leafCu
17846 72 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b rsorInvalid = 1;
17847 0a 20 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 . }
17848 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d .. if( rc==
17849 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1784a 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
1784b 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
1784c 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
1784d 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 age) );.
1784e 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 put4byte(findOve
1784f 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c rflowCell(pPage,
17850 20 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 idx), pgnoChild
17851 29 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f );. VVA_O
17852 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 NLY( pCur->pages
17853 53 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a Shuffled = 0 );.
17854 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c rc = bal
17855 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 ance(pCur, 0);.
17856 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
17857 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
17858 26 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 && leafCursorInv
17859 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 alid ){.
1785a 2f 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 /* The leaf-node
1785b 20 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c is now underful
1785c 6c 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 l and so the tre
1785d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 e needs to be .
1785e 20 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 ** rebala
1785f 6e 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 nced. However, t
17860 68 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 he balance() ope
17861 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e ration on the in
17862 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a ternal. *
17863 2a 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 * node above may
17864 20 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 have modified t
17865 68 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 he structure of
17866 74 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 the B-Tree and.
17867 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 ** so the
17868 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 current content
17869 73 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 s of leafCur.apP
1786a 61 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 age[] and leafCu
1786b 72 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 r.aiIdx[].
1786c 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 ** may not be
1786d 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 trusted..
1786e 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 **. ** I
1786f 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
17870 65 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e e to copy the an
17871 63 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 cestry from pCur
17872 2c 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 , as the same.
17873 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 ** balance
17874 28 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 () call has inva
17875 6c 69 64 61 74 65 64 20 74 68 65 20 70 43 75 72 lidated the pCur
17876 2d 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 ->apPage[] and a
17877 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a iIdx[]. *
17878 2a 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 * arrays. .
17879 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
1787a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 The call to sav
1787b 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
1787c 29 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c ) below internal
1787d 6c 79 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 ly saves the .
1787e 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 ** key tha
1787f 74 20 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 t leafCur is cur
17880 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
17881 74 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 to. Currently, t
17882 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 here. **
17883 61 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f are two copies o
17884 66 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 f that key in th
17885 65 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 e tree - one her
17886 65 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 e on the leaf.
17887 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e ** page an
17888 64 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e d one on some in
17889 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 ternal node in t
1788a 68 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 he tree. The cop
1788b 79 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 y on. **
1788c 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 the leaf node is
1788d 20 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 always the next
1788e 20 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 key in tree-ord
1788f 65 72 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 er after the .
17890 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e ** copy on
17891 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f the internal no
17892 64 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c de. So, the call
17893 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
17894 4e 65 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a Next(). *
17895 2a 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 * calls restoreC
17896 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
17897 74 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 to point the cur
17898 73 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a sor to the copy.
17899 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 ** store
1789a 64 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 d on the interna
1789b 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 l node, then adv
1789c 61 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 ances to the nex
1789d 74 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 t entry,.
1789e 20 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e ** which happen
1789f 73 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 s to be the copy
178a0 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 of the key on t
178a1 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
178a2 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 .. ** Net
178a3 20 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 effect: leafCur
178a4 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 is pointing bac
178a5 6b 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 k to the duplica
178a6 74 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 te cell.
178a7 2a 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f ** that needs to
178a8 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 be removed, and
178a9 20 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 the leafCur.apP
178aa 61 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 age[] and.
178ab 20 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 ** leafCur.aiI
178ac 64 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 dx[] arrays are
178ad 63 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 correct..
178ae 20 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f */. VVA_
178af 4f 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 ONLY( Pgno leafP
178b0 67 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d gno = pLeafPage-
178b1 3e 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 >pgno );.
178b2 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 rc = saveCursor
178b3 50 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 Position(&leafCu
178b4 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 r);. if(
178b5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
178b6 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
178b7 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
178b8 28 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 (&leafCur, ¬U
178b9 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a sed);. }.
178ba 20 20 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 pLeafPag
178bb 65 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 e = leafCur.apPa
178bc 67 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 ge[leafCur.iPage
178bd 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ];. asser
178be 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b t( rc!=SQLITE_OK
178bf 20 7c 7c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 || pLeafPage->p
178c0 67 6e 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b gno==leafPgno );
178c1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
178c2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
178c3 7c 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b | leafCur.aiIdx[
178c4 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d leafCur.iPage]==
178c5 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 0 );. }..
178c6 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
178c7 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20 K==rc. &&
178c8 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
178c9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
178ca 74 65 28 70 4c 65 61 66 50 61 67 65 2d 3e 70 44 te(pLeafPage->pD
178cb 62 50 61 67 65 29 29 20 0a 20 20 20 20 20 20 29 bPage)) . )
178cc 7b 0a 20 20 20 20 20 20 20 20 64 72 6f 70 43 65 {. dropCe
178cd 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 2c ll(pLeafPage, 0,
178ce 20 73 7a 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 szNext);.
178cf 20 20 56 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 VVA_ONLY( leaf
178d0 43 75 72 2e 70 61 67 65 73 53 68 75 66 66 6c 65 Cur.pagesShuffle
178d1 64 20 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20 d = 0 );.
178d2 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c rc = balance(&l
178d3 65 61 66 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 eafCur, 0);.
178d4 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 assert( leaf
178d5 43 75 72 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c CursorInvalid ||
178d6 20 21 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53 !leafCur.pagesS
178d7 68 75 66 66 6c 65 64 0a 20 20 20 20 20 20 20 20 huffled.
178d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178d9 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 21 70 || !p
178da 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c Cur->pagesShuffl
178db 65 64 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ed );. }.
178dc 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
178dd 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 treeReleaseTempC
178de 75 72 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b ursor(&leafCur);
178df 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 . }else{. TR
178e0 41 43 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61 ACE(("DELETE: ta
178e1 62 6c 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72 ble=%d delete fr
178e2 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 om leaf %d\n",.
178e3 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f pCur->pgno
178e4 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e Root, pPage->pgn
178e5 6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 o));. rc = dr
178e6 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 opCell(pPage, id
178e7 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 x, cellSizePtr(p
178e8 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 Page, pCell));.
178e9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
178ea 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
178eb 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c = balance(pCur,
178ec 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 0);. }. }.
178ed 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
178ee 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f OK ){. moveTo
178ef 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a Root(pCur);. }.
178f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
178f1 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
178f2 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 ew BTree table.
178f3 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 Write into *piT
178f4 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a able the page.**
178f5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
178f6 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
178f7 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a new table..**.*
178f8 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 * The type of ty
178f9 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 pe is determined
178fa 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 by the flags pa
178fb 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 rameter. Only t
178fc 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 he.** following
178fd 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 values of flags
178fe 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e are currently in
178ff 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c use. Other val
17900 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 ues for.** flags
17901 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a might not work:
17902 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 .**.** BTREE
17903 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 _INTKEY|BTREE_LE
17904 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 AFDATA Used
17905 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 for SQL tables w
17906 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a ith rowid keys.*
17907 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f * BTREE_ZERO
17908 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 DATA
17909 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 Used for S
1790a 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 QL indices.*/.st
1790b 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 atic int btreeCr
1790c 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 eateTable(Btree
1790d 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 *p, int *piTable
1790e 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1790f 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
17910 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
17911 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f e *pRoot;. Pgno
17912 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 pgnoRoot;. int
17913 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
17914 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
17915 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
17916 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
17917 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
17918 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
17919 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e rt( !pBt->readOn
1791a 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 ly );..#ifdef SQ
1791b 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1791c 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f CUUM. rc = allo
1791d 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
1791e 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f t, &pRoot, &pgno
1791f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 Root, 1, 0);. i
17920 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
17921 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 urn rc;. }.#els
17922 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 e. if( pBt->aut
17923 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 oVacuum ){. P
17924 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 gno pgnoMove;
17925 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 /* Move a pag
17926 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 e here to make r
17927 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 oom for the root
17928 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d -page */. Mem
17929 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b Page *pPageMove;
1792a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 /* The page to
1792b 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 move to. */..
1792c 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e /* Creating a n
1792d 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f ew table may pro
1792e 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f bably require mo
1792f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 ving an existing
17930 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a database. **
17931 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f to make room fo
17932 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 r the new tables
17933 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 root page. In c
17934 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75 ase this page tu
17935 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 rns. ** out t
17936 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 o be an overflow
17937 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c page, delete al
17938 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d l overflow page-
17939 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a map caches. *
1793a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 * held by open c
1793b 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 ursors.. */.
1793c 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c invalidateAll
1793d 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 OverflowCache(pB
1793e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 t);.. /* Read
1793f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 the value of me
17940 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 ta[3] from the d
17941 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 atabase to deter
17942 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 mine where the.
17943 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 ** root page
17944 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
17945 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 should go. meta
17946 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 [3] is the large
17947 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
17948 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 ** created so f
17949 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 ar, so the new r
1794a 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 oot-page is (met
1794b 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a a[3]+1).. */.
1794c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1794d 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 BtreeGetMeta(p,
1794e 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 4, &pgnoRoot);.
1794f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
17950 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
17951 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
17952 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a pgnoRoot++;..
17953 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 /* The new r
17954 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 oot-page may not
17955 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e be allocated on
17956 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
17957 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 age, or the.
17958 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 ** PENDING_BYTE
17959 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
1795a 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 while( pgnoRoot
1795b 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 ==PTRMAP_PAGENO(
1795c 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c pBt, pgnoRoot) |
1795d 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f |. pgnoRo
1795e 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ot==PENDING_BYTE
1795f 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
17960 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a pgnoRoot++;.
17961 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
17962 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b ( pgnoRoot>=3 );
17963 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 .. /* Allocat
17964 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 e a page. The pa
17965 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c ge that currentl
17966 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e y resides at pgn
17967 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a oRoot will. *
17968 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 * be moved to th
17969 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 e allocated page
1796a 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c (unless the all
1796b 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 ocated page happ
1796c 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 ens. ** to re
1796d 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 side at pgnoRoot
1796e 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 ).. */. rc
1796f 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
17970 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 Page(pBt, &pPage
17971 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c Move, &pgnoMove,
17972 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 pgnoRoot, 1);.
17973 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
17974 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
17975 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a turn rc;. }..
17976 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 if( pgnoMove
17977 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 !=pgnoRoot ){.
17978 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 /* pgnoRoot
17979 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 is the page that
1797a 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f will be used fo
1797b 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 r the root-page
1797c 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
1797d 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d new table (assum
1797e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 ing an error did
1797f 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 not occur). But
17980 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a we were. *
17981 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f * allocated pgno
17982 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 Move. If require
17983 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 d (i.e. if it wa
17984 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a s not allocated.
17985 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 ** by exte
17986 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c nding the file),
17987 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 the current pag
17988 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 e at position pg
17989 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 noMove. **
1798a 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e is already journ
1798b 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 aled.. */.
1798c 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 u8 eType;.
1798d 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 Pgno iPtrPa
1798e 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 ge;.. relea
1798f 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f 76 65 sePage(pPageMove
17990 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 );.. /* Mov
17991 65 20 74 68 65 20 70 61 67 65 20 63 75 72 72 65 e the page curre
17992 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 ntly at pgnoRoot
17993 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f to pgnoMove. */
17994 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
17995 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
17996 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 pBt, pgnoRoot, &
17997 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 pRoot, 0);.
17998 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17999 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
1799a 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
1799b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d . rc = ptrm
1799c 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 apGet(pBt, pgnoR
1799d 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 oot, &eType, &iP
1799e 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 trPage);. i
1799f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
179a0 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 || eType==PTRMA
179a1 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 P_ROOTPAGE || eT
179a2 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 ype==PTRMAP_FREE
179a3 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 PAGE ){.
179a4 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f releasePage(pRoo
179a5 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 t);. retu
179a6 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
179a7 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 assert( eTy
179a8 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe!=PTRMAP_ROOTP
179a9 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 AGE );. ass
179aa 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d ert( eType!=PTRM
179ab 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 AP_FREEPAGE );.
179ac 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 rc = reloca
179ad 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f tePage(pBt, pRoo
179ae 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 t, eType, iPtrPa
179af 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 ge, pgnoMove, 0)
179b0 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 ;. releaseP
179b1 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 age(pRoot);..
179b2 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 /* Obtain the
179b3 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f page at pgnoRoo
179b4 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 t */. if( r
179b5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
179b6 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
179b7 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
179b8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
179b9 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 eeGetPage(pBt, p
179ba 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c gnoRoot, &pRoot,
179bb 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
179bc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
179bd 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
179be 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
179bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
179c0 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 erWrite(pRoot->p
179c1 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
179c2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
179c3 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
179c4 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
179c5 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
179c6 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
179c7 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f else{. pRoo
179c8 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 t = pPageMove;.
179c9 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 } .. /* Up
179ca 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 date the pointer
179cb 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 -map and meta-da
179cc 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 ta with the new
179cd 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 root-page number
179ce 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 . */. rc = pt
179cf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e rmapPut(pBt, pgn
179d0 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f oRoot, PTRMAP_RO
179d1 4f 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 OTPAGE, 0);.
179d2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
179d3 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f releasePage(pRoo
179d4 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e t);. return
179d5 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rc;. }. r
179d6 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
179d7 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c UpdateMeta(p, 4,
179d8 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 pgnoRoot);.
179d9 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
179da 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f releasePage(pRoo
179db 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e t);. return
179dc 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 rc;. }.. }e
179dd 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c lse{. rc = al
179de 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
179df 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 pBt, &pRoot, &pg
179e0 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 noRoot, 1, 0);.
179e1 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
179e2 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 rn rc;. }.#endi
179e3 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 f. assert( sqli
179e4 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
179e5 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 ble(pRoot->pDbPa
179e6 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 ge) );. zeroPag
179e7 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c e(pRoot, flags |
179e8 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 PTF_LEAF);. sq
179e9 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
179ea 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b pRoot->pDbPage);
179eb 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 . *piTable = (i
179ec 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 nt)pgnoRoot;. r
179ed 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
179ee 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
179ef 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
179f0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 eeCreateTable(Bt
179f1 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 ree *p, int *piT
179f2 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 able, int flags)
179f3 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
179f4 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
179f5 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 p);. rc = btree
179f6 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 CreateTable(p, p
179f7 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a iTable, flags);.
179f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
179f9 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
179fa 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 rc;.}../*.** Er
179fb 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 ase the given da
179fc 74 61 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 tabase page and
179fd 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e all its children
179fe 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 . Return.** the
179ff 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 page to the fre
17a00 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 elist..*/.static
17a01 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 int clearDataba
17a02 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 sePage(. BtShar
17a03 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 ed *pBt,
17a04 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 /* The BTree
17a05 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
17a06 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e e table */. Pgn
17a07 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
17a08 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
17a09 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 r to clear */.
17a0a 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 int freePageFlag
17a0b 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 , /* Dealloc
17a0c 61 74 65 20 70 61 67 65 20 69 66 20 74 72 75 65 ate page if true
17a0d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 */. int *pnCha
17a0e 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 nge.){. MemPage
17a0f 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 *pPage = 0;. i
17a10 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 nt rc;. unsigne
17a11 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 d char *pCell;.
17a12 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 int i;.. asser
17a13 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
17a14 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
17a15 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e ) );. if( pgno>
17a16 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
17a17 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Bt) ){. retur
17a18 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
17a19 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 _BKPT;. }.. rc
17a1a 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
17a1b 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 e(pBt, pgno, &pP
17a1c 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
17a1d 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 goto cleardatab
17a1e 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66 asepage_out;. f
17a1f 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d or(i=0; i<pPage-
17a20 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 >nCell; i++){.
17a21 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
17a22 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 ll(pPage, i);.
17a23 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
17a24 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d af ){. rc =
17a25 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 clearDatabasePa
17a26 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 ge(pBt, get4byte
17a27 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 (pCell), 1, pnCh
17a28 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 ange);. if(
17a29 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 rc ) goto clear
17a2a 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 databasepage_out
17a2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
17a2c 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 clearCell(pPage
17a2d 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 , pCell);. if
17a2e 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 ( rc ) goto clea
17a2f 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 rdatabasepage_ou
17a30 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 t;. }. if( !pP
17a31 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
17a32 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 rc = clearDatab
17a33 61 73 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 asePage(pBt, get
17a34 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
17a35 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68 ata[8]), 1, pnCh
17a36 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 ange);. if( r
17a37 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 c ) goto clearda
17a38 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a tabasepage_out;.
17a39 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6e 43 68 }else if( pnCh
17a3a 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 ange ){. asse
17a3b 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 rt( pPage->intKe
17a3c 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e y );. *pnChan
17a3d 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ge += pPage->nCe
17a3e 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 ll;. }. if( fr
17a3f 65 65 50 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 eePageFlag ){.
17a40 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 rc = freePage(
17a41 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 20 pPage);. }else
17a42 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 if( (rc = sqlite
17a43 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
17a44 65 2d 3e 70 44 62 50 61 67 65 29 29 3d 3d 30 20 e->pDbPage))==0
17a45 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 ){. zeroPage(
17a46 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 pPage, pPage->aD
17a47 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 ata[0] | PTF_LEA
17a48 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 F);. }..clearda
17a49 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a tabasepage_out:.
17a4a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
17a4b 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 age);. return r
17a4c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 c;.}../*.** Dele
17a4d 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 te all informati
17a4e 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 on from a single
17a4f 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 table in the da
17a50 74 61 62 61 73 65 2e 20 20 69 54 61 62 6c 65 20 tabase. iTable
17a51 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e is.** the page n
17a52 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 6f umber of the roo
17a53 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 t of the table.
17a54 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 After this rout
17a55 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 ine returns,.**
17a56 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 the root page is
17a57 20 65 6d 70 74 79 2c 20 62 75 74 20 73 74 69 6c empty, but stil
17a58 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 l exists..**.**
17a59 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
17a5a 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 l fail with SQLI
17a5b 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 TE_LOCKED if the
17a5c 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a re are any open.
17a5d 2a 2a 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 ** read cursors
17a5e 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 4f on the table. O
17a5f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 pen write cursor
17a60 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 s are moved to t
17a61 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 he.** root of th
17a62 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 e table..**.** I
17a63 66 20 70 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f f pnChange is no
17a64 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 t NULL, then tab
17a65 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 74 20 62 le iTable must b
17a66 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c e an intkey tabl
17a67 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 e. The.** intege
17a68 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 r value pointed
17a69 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 65 20 69 to by pnChange i
17a6a 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 s incremented by
17a6b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
17a6c 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 * entries in the
17a6d 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 table..*/.SQLIT
17a6e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17a6f 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
17a70 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 able(Btree *p, i
17a71 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a nt iTable, int *
17a72 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74 pnChange){. int
17a73 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 rc;. BtShared
17a74 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
17a75 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
17a76 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 er(p);. assert(
17a77 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
17a78 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 NS_WRITE );. if
17a79 28 20 28 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 ( (rc = checkFor
17a7a 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c ReadConflicts(p,
17a7b 20 69 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21 iTable, 0, 1))!
17a7c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17a7d 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 /* nothing to
17a7e 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 do */. }else if
17a7f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
17a80 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 = saveAllCursor
17a81 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30 s(pBt, iTable, 0
17a82 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 )) ){. /* not
17a83 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 hing to do */.
17a84 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
17a85 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 clearDatabasePag
17a86 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 e(pBt, (Pgno)iTa
17a87 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 ble, 0, pnChange
17a88 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
17a89 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
17a8a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
17a8b 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 *.** Erase all i
17a8c 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 nformation in a
17a8d 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 table and add th
17a8e 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 e root of the ta
17a8f 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 ble to.** the fr
17a90 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c eelist. Except,
17a91 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
17a92 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 principle table
17a93 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 (the one on.**
17a94 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 page 1) is never
17a95 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 added to the fr
17a96 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 eelist..**.** Th
17a97 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
17a98 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 fail with SQLITE
17a99 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 _LOCKED if there
17a9a 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a are any open.**
17a9b 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
17a9c 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 table..**.** If
17a9d 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e AUTOVACUUM is en
17a9e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 abled and the pa
17a9f 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 ge at iTable is
17aa0 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 not the last.**
17aa1 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
17aa2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
17aa3 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f then the last ro
17aa4 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 ot page .** in t
17aa5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
17aa6 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 is moved into t
17aa7 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 he slot formerly
17aa8 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 occupied by.**
17aa9 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 iTable and that
17aaa 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 last slot former
17aab 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 ly occupied by t
17aac 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
17aad 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f e.** is added to
17aae 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
17aaf 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e stead of iTable.
17ab0 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 In this say, a
17ab1 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 ll.** root pages
17ab2 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 are kept at the
17ab3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
17ab4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
17ab5 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 which.** is nec
17ab6 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 essary for AUTOV
17ab7 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 ACUUM to work ri
17ab8 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 ght. *piMoved i
17ab9 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a s set to the .**
17aba 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 page number tha
17abb 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 t used to be the
17abc 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 last root page
17abd 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f in the file befo
17abe 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 re.** the move.
17abf 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 If no page gets
17ac0 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 moved, *piMoved
17ac1 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a is set to 0..**
17ac2 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 The last root p
17ac3 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 age is recorded
17ac4 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 in meta[3] and t
17ac5 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d he value of.** m
17ac6 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 eta[3] is update
17ac7 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 d by this proced
17ac8 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
17ac9 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c nt btreeDropTabl
17aca 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f e(Btree *p, Pgno
17acb 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 iTable, int *pi
17acc 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 Moved){. int rc
17acd 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
17ace 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 ge = 0;. BtShar
17acf 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
17ad0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
17ad1 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
17ad2 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
17ad3 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
17ad4 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a TRANS_WRITE );..
17ad5 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 /* It is illeg
17ad6 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 al to drop a tab
17ad7 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 le if any cursor
17ad8 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 s are open on th
17ad9 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e e. ** database.
17ada 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
17adb 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
17adc 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 mode the backend
17add 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 may. ** need t
17ade 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 o move another r
17adf 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c oot-page to fill
17ae0 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 a gap left by t
17ae1 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 he deleted. **
17ae2 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e root page. If an
17ae3 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 open cursor was
17ae4 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 using this page
17ae5 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 a problem would
17ae6 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 . ** occur..
17ae7 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 */. if( pBt->pC
17ae8 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c ursor ){. sql
17ae9 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
17aea 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 ocked(p->db, pBt
17aeb 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 ->pCursor->pBtre
17aec 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 e->db);. retu
17aed 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
17aee 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
17aef 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }.. rc = sqlite
17af0 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
17af1 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c t, (Pgno)iTable,
17af2 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 &pPage, 0);. i
17af3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
17af4 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 c;. rc = sqlite
17af5 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
17af6 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a (p, iTable, 0);.
17af7 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
17af8 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
17af9 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 e);. return r
17afa 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 c;. }.. *piMov
17afb 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69 ed = 0;.. if( i
17afc 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 65 Table>1 ){.#ifde
17afd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
17afe 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 72 63 20 TOVACUUM. rc
17aff 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65 = freePage(pPage
17b00 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
17b01 67 65 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 65 ge(pPage);.#else
17b02 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 . if( pBt->au
17b03 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
17b04 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 Pgno maxRootPg
17b05 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 no;. rc = s
17b06 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 qlite3BtreeGetMe
17b07 74 61 28 70 2c 20 34 2c 20 26 6d 61 78 52 6f 6f ta(p, 4, &maxRoo
17b08 74 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 tPgno);. if
17b09 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17b0a 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 ){. relea
17b0b 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
17b0c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
17b0d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
17b0e 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 if( iTable==max
17b0f 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20 RootPgno ){.
17b10 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 /* If the ta
17b11 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 ble being droppe
17b12 64 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 d is the table w
17b13 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 ith the largest
17b14 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 root-page.
17b15 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 ** number in t
17b16 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 75 74 he database, put
17b17 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
17b18 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e n the free list.
17b19 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 . */.
17b1a 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 rc = freePa
17b1b 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
17b1c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
17b1d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Page);. i
17b1e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
17b1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
17b20 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
17b21 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
17b22 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
17b23 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 able being dropp
17b24 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 ed does not have
17b25 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f the largest roo
17b26 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a t-page. *
17b27 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 * number in the
17b28 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76 database. So mov
17b29 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 e the page that
17b2a 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20 does into the .
17b2b 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65 ** gap le
17b2c 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 ft by the delete
17b2d 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20 d root-page..
17b2e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
17b2f 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a MemPage *pMove;.
17b30 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
17b31 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
17b32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17b33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
17b34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 , maxRootPgno, &
17b35 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 pMove, 0);.
17b36 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
17b37 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
17b38 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
17b39 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 }. r
17b3a 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 c = relocatePage
17b3b 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 (pBt, pMove, PTR
17b3c 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c MAP_ROOTPAGE, 0,
17b3d 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 iTable, 0);.
17b3e 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
17b3f 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 (pMove);.
17b40 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17b41 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
17b42 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
17b43 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 }. rc
17b44 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
17b45 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f tPage(pBt, maxRo
17b46 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 otPgno, &pMove,
17b47 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
17b48 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
17b49 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
17b4a 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
17b4b 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 rc = fre
17b4c 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 ePage(pMove);.
17b4d 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
17b4e 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 e(pMove);.
17b4f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
17b50 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
17b51 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
17b52 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 }. *p
17b53 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 iMoved = maxRoot
17b54 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 Pgno;. }..
17b55 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 /* Set the
17b56 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 new 'max-root-pa
17b57 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 ge' value in the
17b58 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 database header
17b59 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 . This. **
17b5a 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 is the old value
17b5b 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 less one, less
17b5c 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 one more if that
17b5d 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 happens to.
17b5e 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 ** be a root-p
17b5f 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 age number, less
17b60 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 one again if th
17b61 61 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 at is the.
17b62 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ** PENDING_BYTE_
17b63 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 PAGE.. */.
17b64 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f maxRootPgno
17b65 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 61 --;. if( ma
17b66 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 xRootPgno==PENDI
17b67 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
17b68 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 ) ){. max
17b69 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 RootPgno--;.
17b6a 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 61 }. if( ma
17b6b 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 54 52 4d 41 xRootPgno==PTRMA
17b6c 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6d 61 P_PAGENO(pBt, ma
17b6d 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 xRootPgno) ){.
17b6e 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e maxRootPgn
17b6f 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 o--;. }.
17b70 20 20 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f assert( maxRo
17b71 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f otPgno!=PENDING_
17b72 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
17b73 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ;.. rc = sq
17b74 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
17b75 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f Meta(p, 4, maxRo
17b76 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c otPgno);. }el
17b77 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 se{. rc = f
17b78 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a reePage(pPage);.
17b79 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
17b7a 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a e(pPage);. }.
17b7b 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a #endif. }else{.
17b7c 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 /* If sqlite
17b7d 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20 3BtreeDropTable
17b7e 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 was called on pa
17b7f 67 65 20 31 2e 20 2a 2f 0a 20 20 20 20 7a 65 72 ge 1. */. zer
17b80 6f 50 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 oPage(pPage, PTF
17b81 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 _INTKEY|PTF_LEAF
17b82 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 );. releaseP
17b83 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a age(pPage);. }.
17b84 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d return rc; .}
17b85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
17b86 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
17b87 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 DropTable(Btree
17b88 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 *p, int iTable,
17b89 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 int *piMoved){.
17b8a 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
17b8b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
17b8c 0a 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f . rc = btreeDro
17b8d 70 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 pTable(p, iTable
17b8e 2c 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 , piMoved);. sq
17b8f 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
17b90 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
17b91 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .}.../*.** Read
17b92 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 the meta-informa
17b93 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 tion out of a da
17b94 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 tabase file. Me
17b95 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 ta[0].** is the
17b96 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 number of free p
17b97 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 ages currently i
17b98 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
17b99 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f Meta[1].** thro
17b9a 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 ugh meta[15] are
17b9b 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 available for u
17b9c 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 se by higher lay
17b9d 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a ers. Meta[0].**
17b9e 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 is read-only, t
17b9f 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 he others are re
17ba0 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a ad/write..** .**
17ba1 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 The schema laye
17ba2 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 r numbers meta v
17ba3 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c alues differentl
17ba4 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d y. At the schem
17ba5 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 a.** layer (and
17ba6 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e the SetCookie an
17ba7 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 d ReadCookie opc
17ba8 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 odes) the number
17ba9 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 of.** free page
17baa 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 s is not visible
17bab 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 . So Cookie[0]
17bac 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d is the same as M
17bad 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 eta[1]..*/.SQLIT
17bae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17baf 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
17bb0 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 a(Btree *p, int
17bb1 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 idx, u32 *pMeta)
17bb2 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
17bb3 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 age = 0;. int r
17bb4 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 c;. unsigned ch
17bb5 61 72 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61 ar *pP1;. BtSha
17bb6 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
17bb7 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 t;.. sqlite3Btr
17bb8 65 65 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 2f eeEnter(p);.. /
17bb9 2a 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 * Reading a meta
17bba 2d 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 -data value requ
17bbb 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b ires a read-lock
17bbc 20 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 on page 1 (and
17bbd 68 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 hence. ** the s
17bbe 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
17bbf 6c 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 le. We grab this
17bc0 20 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 lock regardless
17bc1 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 of whether or.
17bc2 20 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 ** not the SQLI
17bc3 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
17bc4 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 ed flag is set (
17bc5 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 the table rooted
17bc6 20 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 at page. ** 1
17bc7 69 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20 is treated as a
17bc8 73 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 special case by
17bc9 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
17bca 54 61 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a TableLock(). **
17bcb 20 61 6e 64 20 73 65 74 53 68 61 72 65 64 43 61 and setSharedCa
17bcc 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e cheTableLock()).
17bcd 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 . */. rc = que
17bce 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
17bcf 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 leLock(p, 1, REA
17bd0 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 D_LOCK);. if( r
17bd1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
17bd2 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
17bd3 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 Leave(p);. re
17bd4 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
17bd5 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 assert( idx>=0 &
17bd6 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 & idx<=15 );. i
17bd7 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 f( pBt->pPage1 )
17bd8 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 {. /* The b-t
17bd9 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 ree is already h
17bda 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e olding a referen
17bdb 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 ce to page 1 of
17bdc 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 the database.
17bdd 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 ** file. In thi
17bde 73 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69 s case the requi
17bdf 72 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 red meta-data va
17be0 6c 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 lue can be read
17be1 64 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 directly. **
17be2 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 from the page da
17be3 74 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 ta of this refer
17be4 65 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c ence. This is sl
17be5 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 ightly faster th
17be6 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 an. ** reques
17be7 74 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 ting a new refer
17be8 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 ence from the pa
17be9 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a ger layer.. *
17bea 2f 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 /. pP1 = (uns
17beb 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 igned char *)pBt
17bec 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b ->pPage1->aData;
17bed 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
17bee 20 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 The b-tree does
17bef 20 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 not have a refe
17bf0 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
17bf1 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
17bf2 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 file.. ** Obt
17bf3 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 ain one from the
17bf4 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 pager layer..
17bf5 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
17bf6 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 lite3PagerGet(pB
17bf7 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 t->pPager, 1, &p
17bf8 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
17bf9 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c rc ){. sql
17bfa 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
17bfb 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
17bfc 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
17bfd 31 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 1 = (unsigned ch
17bfe 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 ar *)sqlite3Page
17bff 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
17c00 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 );. }. *pMeta
17c01 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b = get4byte(&pP1[
17c02 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 36 + idx*4]);..
17c03 20 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 /* If the b-tre
17c04 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 e is not holding
17c05 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
17c06 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 page 1, then one
17c07 20 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 was . ** reque
17c08 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 sted from the pa
17c09 67 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 ger layer in the
17c0a 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 above block. Re
17c0b 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 lease it now..
17c0c 2a 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 */. if( !pBt->p
17c0d 50 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c Page1 ){. sql
17c0e 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
17c0f 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 DbPage);. }..
17c10 2f 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d /* If autovacuum
17c11 65 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 ed is disabled i
17c12 6e 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 n this build but
17c13 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
17c14 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 o . ** access a
17c15 6e 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 n autovacuumed d
17c16 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 atabase, then ma
17c17 6b 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ke the database
17c18 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a readonly. . */.
17c19 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
17c1a 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
17c1b 69 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 if( idx==4 && *p
17c1c 4d 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 Meta>0 ) pBt->re
17c1d 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 adOnly = 1;.#end
17c1e 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 if.. /* If ther
17c1f 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 e is currently a
17c20 6e 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 n open transacti
17c21 6f 6e 2c 20 67 72 61 62 20 61 20 72 65 61 64 2d on, grab a read-
17c22 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 lock . ** on pa
17c23 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 ge 1 of the data
17c24 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 base file. This
17c25 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 is done to make
17c26 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 6e sure that. ** n
17c27 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 o other connecti
17c28 6f 6e 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 on can modify th
17c29 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6a 75 73 e meta value jus
17c2a 74 20 72 65 61 64 20 66 72 6f 6d 0a 20 20 2a 2a t read from. **
17c2b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75 6e the database un
17c2c 74 69 6c 20 74 68 65 20 74 72 61 6e 73 61 63 74 til the transact
17c2d 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 ion is concluded
17c2e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
17c2f 69 6e 54 72 61 6e 73 3e 30 20 29 7b 0a 20 20 20 inTrans>0 ){.
17c30 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 rc = setSharedC
17c31 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
17c32 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 1, READ_LOCK);.
17c33 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
17c34 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
17c35 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
17c36 2a 20 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66 * Write meta-inf
17c37 6f 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e ormation back in
17c38 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e to the database.
17c39 20 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 Meta[0] is.**
17c3a 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 read-only and ma
17c3b 79 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e y not be written
17c3c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
17c3d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
17c3e 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 treeUpdateMeta(B
17c3f 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 tree *p, int idx
17c40 2c 20 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20 , u32 iMeta){.
17c41 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
17c42 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e p->pBt;. unsign
17c43 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20 ed char *pP1;.
17c44 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
17c45 28 20 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c ( idx>=1 && idx<
17c46 3d 31 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 =15 );. sqlite3
17c47 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
17c48 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
17c49 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
17c4a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
17c4b 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a t->pPage1!=0 );.
17c4c 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 pP1 = pBt->pPa
17c4d 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 ge1->aData;. rc
17c4e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
17c4f 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 rite(pBt->pPage1
17c50 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 ->pDbPage);. if
17c51 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
17c52 29 7b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 ){. put4byte(
17c53 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d &pP1[36 + idx*4]
17c54 2c 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 , iMeta);.#ifnde
17c55 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
17c56 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
17c57 20 69 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20 idx==7 ){.
17c58 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
17c59 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 toVacuum || iMet
17c5a 61 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 a==0 );. as
17c5b 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c sert( iMeta==0 |
17c5c 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 | iMeta==1 );.
17c5d 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
17c5e 75 75 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b uum = (u8)iMeta;
17c5f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
17c60 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
17c61 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
17c62 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
17c63 52 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 Return the flag
17c64 62 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69 byte at the begi
17c65 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
17c66 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
17c67 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c r.** is currentl
17c68 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a y pointing to..*
17c69 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17c6a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17c6b 65 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 eFlags(BtCursor
17c6c 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 *pCur){. /* TOD
17c6d 4f 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 O: What about CU
17c6e 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
17c6f 20 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 state? Probably
17c70 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 need to call.
17c71 2a 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 ** restoreCursor
17c72 50 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e Position() here.
17c73 0a 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 . */. MemPage
17c74 2a 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 *pPage;. restor
17c75 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
17c76 70 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d pCur);. pPage =
17c77 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
17c78 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 ur->iPage];. as
17c79 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
17c7a 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
17c7b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 21 assert( pPage!
17c7c 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
17c7d 70 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 pPage->pBt==pCur
17c7e 2d 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 ->pBt );. retur
17c7f 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 n pPage->aData[p
17c80 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d Page->hdrOffset]
17c81 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
17c82 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f ITE_OMIT_BTREECO
17c83 55 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 UNT./*.** The fi
17c84 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 rst argument, pC
17c85 75 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 ur, is a cursor
17c86 6f 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 opened on some b
17c87 2d 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 -tree. Count the
17c88 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e .** number of en
17c89 74 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 tries in the b-t
17c8a 72 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 ree and write th
17c8b 65 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 e result to *pnE
17c8c 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 ntry..**.** SQLI
17c8d 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
17c8e 64 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 d if the operati
17c8f 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c on is successful
17c90 6c 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a ly executed. .**
17c91 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
17c92 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 n error is encou
17c93 6e 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 ntered (i.e. an
17c94 49 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 IO error or data
17c95 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 base.** corrupti
17c96 6f 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 on) an SQLite er
17c97 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
17c98 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
17c99 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17c9a 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 te3BtreeCount(Bt
17c9b 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 Cursor *pCur, i6
17c9c 34 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 4 *pnEntry){. i
17c9d 36 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 64 nEntry = 0;
17c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c9f 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
17ca0 72 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 return in *pnEnt
17ca1 72 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ry */. int rc;
17ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17ca3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17ca4 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
17ca5 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
17ca6 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e (pCur);.. /* Un
17ca7 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 less an error oc
17ca8 63 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 curs, the follow
17ca9 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e ing loop runs on
17caa 65 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 e iteration for
17cab 65 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 each. ** page i
17cac 6e 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 n the B-Tree str
17cad 75 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c ucture (not incl
17cae 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 uding overflow p
17caf 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 ages). . */. w
17cb0 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
17cb1 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 _OK ){. int i
17cb2 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Idx;
17cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17cb4 20 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 Index of child
17cb5 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a node in parent *
17cb6 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 /. MemPage *p
17cb7 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
17cb8 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
17cb9 65 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 ent page of the
17cba 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f b-tree */.. /
17cbb 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c * If this is a l
17cbc 65 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 eaf page or the
17cbd 74 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 tree is not an i
17cbe 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 nt-key tree, the
17cbf 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 n . ** this p
17cc0 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 age contains cou
17cc1 6e 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 ntable entries.
17cc2 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e Increment the en
17cc3 74 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 try counter.
17cc4 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a ** accordingly..
17cc5 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 */. pPage
17cc6 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
17cc7 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
17cc8 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
17cc9 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 f || !pPage->int
17cca 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e Key ){. nEn
17ccb 74 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 try += pPage->nC
17ccc 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ell;. }..
17ccd 2f 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 /* pPage is a le
17cce 61 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f af node. This lo
17ccf 6f 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 op navigates the
17cd0 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 cursor so that
17cd1 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 it . ** point
17cd2 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 s to the first i
17cd3 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 nterior cell tha
17cd4 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
17cd5 68 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 he parent of.
17cd6 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 ** the next pag
17cd7 65 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 e in the tree th
17cd8 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 at has not yet b
17cd9 65 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 een visited. The
17cda 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 . ** pCur->ai
17cdb 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
17cdc 20 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f value is set to
17cdd 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
17cde 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 e parent cell.
17cdf 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 ** of the page
17ce0 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 , or to the numb
17ce1 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 er of cells in t
17ce2 68 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e he page if the n
17ce3 65 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 ext page. **
17ce4 74 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20 to visit is the
17ce5 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 right-child of i
17ce6 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a ts parent.. *
17ce7 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 *. ** If all
17ce8 70 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 pages in the tre
17ce9 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 e have been visi
17cea 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ted, return SQLI
17ceb 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 TE_OK to the.
17cec 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 ** caller..
17ced 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
17cee 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
17cef 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 do {. if(
17cf0 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 pCur->iPage==0
17cf1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
17cf2 41 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 All pages of the
17cf3 20 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 b-tree have bee
17cf4 6e 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 n visited. Retur
17cf5 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 n successfully.
17cf6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e */. *pn
17cf7 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a Entry = nEntry;.
17cf8 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
17cf9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
17cfa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
17cfb 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f lite3BtreeMoveTo
17cfc 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 Parent(pCur);.
17cfd 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 }while ( pCu
17cfe 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
17cff 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 Page]>=pCur->apP
17d00 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17d01 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 ->nCell );..
17d02 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
17d03 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 ur->iPage]++;.
17d04 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 pPage = pCur
17d05 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
17d06 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 Page];. }..
17d07 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 /* Descend to
17d08 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f the child node o
17d09 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 f the cell that
17d0a 74 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 the cursor curre
17d0b 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 ntly . ** poi
17d0c 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 nts at. This is
17d0d 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
17d0e 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d if (iIdx==pPage-
17d0f 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a >nCell).. */.
17d10 20 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d iIdx = pCur-
17d11 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17d12 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 ge];. if( iId
17d13 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x==pPage->nCell
17d14 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f ){. rc = mo
17d15 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
17d16 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
17d17 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
17d18 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 rOffset+8]));.
17d19 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
17d1a 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
17d1b 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 pCur, get4byte(f
17d1c 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
17d1d 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 Idx)));. }.
17d1e 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 }.. /* An error
17d1f 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 has occurred. R
17d20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
17d21 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e ode. */. return
17d22 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f rc;.}.#endif../
17d23 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
17d24 70 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 pager associated
17d25 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 with a BTree.
17d26 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
17d27 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 used for.** test
17d28 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e ing and debuggin
17d29 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 g only..*/.SQLIT
17d2a 45 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 E_PRIVATE Pager
17d2b 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 *sqlite3BtreePag
17d2c 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 er(Btree *p){.
17d2d 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 return p->pBt->p
17d2e 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 Pager;.}..#ifnde
17d2f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
17d30 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
17d31 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 .** Append a mes
17d32 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f sage to the erro
17d33 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 r message string
17d34 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
17d35 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
17d36 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
17d37 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a pCheck,. char *
17d38 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 zMsg1,. const c
17d39 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 har *zFormat,.
17d3a 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ....){. va_list
17d3b 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 ap;. if( !pChe
17d3c 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 ck->mxErr ) retu
17d3d 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 rn;. pCheck->mx
17d3e 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d Err--;. pCheck-
17d3f 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 >nErr++;. va_st
17d40 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
17d41 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e ;. if( pCheck->
17d42 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a errMsg.nChar ){.
17d43 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
17d44 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 cumAppend(&pChec
17d45 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c k->errMsg, "\n",
17d46 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 1);. }. if( z
17d47 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 Msg1 ){. sqli
17d48 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
17d49 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 d(&pCheck->errMs
17d4a 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 g, zMsg1, -1);.
17d4b 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 }. sqlite3VXPr
17d4c 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 intf(&pCheck->er
17d4d 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 rMsg, 1, zFormat
17d4e 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
17d4f 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 ap);. if( pChec
17d50 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 k->errMsg.malloc
17d51 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 Failed ){. pC
17d52 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c heck->mallocFail
17d53 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 ed = 1;. }.}.#e
17d54 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
17d55 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
17d56 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ECK */..#ifndef
17d57 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
17d58 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a GRITY_CHECK./*.*
17d59 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 * Add 1 to the r
17d5a 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 eference count f
17d5b 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 or page iPage.
17d5c 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 If this is the s
17d5d 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e econd.** referen
17d5e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 ce to the page,
17d5f 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 add an error mes
17d60 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e sage to pCheck->
17d61 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 zErrMsg..** Retu
17d62 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 rn 1 if there ar
17d63 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 e 2 ore more ref
17d64 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 erences to the p
17d65 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 age and 0 if.**
17d66 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 if this is the f
17d67 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 irst reference t
17d68 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a o the page..**.*
17d69 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 * Also check tha
17d6a 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 t the page numbe
17d6b 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a r is in bounds..
17d6c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 */.static int ch
17d6d 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 eckRef(Integrity
17d6e 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f Ck *pCheck, Pgno
17d6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 iPage, char *zC
17d70 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 ontext){. if( i
17d71 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e Page==0 ) return
17d72 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 1;. if( iPage>
17d73 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b pCheck->nPage ){
17d74 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
17d75 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
17d76 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 text, "invalid p
17d77 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 age number %d",
17d78 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 iPage);. retu
17d79 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 1;. }. if(
17d7a 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 pCheck->anRef[iP
17d7b 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 age]==1 ){. c
17d7c 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
17d7d 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
17d7e 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 "2nd reference t
17d7f 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 o page %d", iPag
17d80 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 e);. return 1
17d81 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 ;. }. return
17d82 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 (pCheck->anRef[i
17d83 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 Page]++)>1;.}..#
17d84 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17d85 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a IT_AUTOVACUUM./*
17d86 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 .** Check that t
17d87 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 he entry in the
17d88 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 pointer-map for
17d89 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 page iChild maps
17d8a 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 to .** page iPa
17d8b 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 rent, pointer ty
17d8c 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e pe ptrType. If n
17d8d 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 ot, append an er
17d8e 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 ror message.** t
17d8f 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 o pCheck..*/.sta
17d90 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 tic void checkPt
17d91 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 rmap(. Integrit
17d92 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f yCk *pCheck, /
17d93 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 * Integrity chec
17d94 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 k context */. P
17d95 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 gno iChild,
17d96 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 /* Child p
17d97 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
17d98 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 u8 eType,
17d99 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
17d9a 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 ed pointer map t
17d9b 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ype */. Pgno iP
17d9c 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 arent,
17d9d 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e /* Expected poin
17d9e 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 ter map parent p
17d9f 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
17da0 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 char *zContext
17da1 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 /* Contex
17da2 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 t description (u
17da3 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 sed for error ms
17da4 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 g) */.){. int r
17da5 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 c;. u8 ePtrmapT
17da6 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 ype;. Pgno iPtr
17da7 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 mapParent;.. rc
17da8 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 = ptrmapGet(pCh
17da9 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 eck->pBt, iChild
17daa 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 , &ePtrmapType,
17dab 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b &iPtrmapParent);
17dac 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
17dad 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
17dae 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
17daf 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
17db0 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 OERR_NOMEM ) pCh
17db1 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
17db2 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b d = 1;. check
17db3 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
17db4 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 , zContext, "Fai
17db5 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d led to read ptrm
17db6 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 ap key=%d", iChi
17db7 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ld);. return;
17db8 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 . }.. if( ePtr
17db9 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c mapType!=eType |
17dba 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 | iPtrmapParent!
17dbb 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 =iParent ){.
17dbc 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
17dbd 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
17dbe 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 . "Bad ptr
17dbf 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 map entry key=%
17dc0 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 d expected=(%d,%
17dc1 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c d) got=(%d,%d)",
17dc2 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 . iChild,
17dc3 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 eType, iParent,
17dc4 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 ePtrmapType, iPt
17dc5 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d rmapParent);. }
17dc6 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
17dc7 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
17dc8 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 rity of the free
17dc9 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 list or of an ov
17dca 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 erflow page list
17dcb 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 ..** Verify that
17dcc 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
17dcd 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 ages on the list
17dce 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 is N..*/.static
17dcf 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 void checkList(
17dd0 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
17dd1 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 pCheck, /* Inte
17dd2 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 grity checking c
17dd3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
17dd4 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 isFreeList,
17dd5 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
17dd6 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 freelist. False
17dd7 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 for overflow pa
17dd8 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 ge list */. int
17dd9 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 iPage,
17dda 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
17ddb 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 r for first page
17ddc 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a in the list */.
17ddd 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 int N,
17dde 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 /* Expec
17ddf 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 ted number of pa
17de0 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ges in the list
17de1 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 */. char *zCont
17de2 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ext /* Co
17de3 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 ntext for error
17de4 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 messages */.){.
17de5 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 int i;. int ex
17de6 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e pected = N;. in
17de7 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 t iFirst = iPage
17de8 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e ;. while( N-- >
17de9 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 0 && pCheck->mx
17dea 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 Err ){. DbPag
17deb 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 e *pOvflPage;.
17dec 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
17ded 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 *pOvflData;.
17dee 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 if( iPage<1 ){.
17def 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
17df0 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
17df1 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 text,. "
17df2 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d %d of %d pages m
17df3 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 issing from over
17df4 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 flow list starti
17df5 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 ng at %d",.
17df6 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 N+1, expect
17df7 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 ed, iFirst);.
17df8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
17df9 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 if( checkRef
17dfa 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 (pCheck, iPage,
17dfb 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 zContext) ) brea
17dfc 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 k;. if( sqlit
17dfd 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 e3PagerGet(pChec
17dfe 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f k->pPager, (Pgno
17dff 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 )iPage, &pOvflPa
17e00 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 ge) ){. che
17e01 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
17e02 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 ck, zContext, "f
17e03 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 ailed to get pag
17e04 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 e %d", iPage);.
17e05 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
17e06 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 }. pOvflData
17e07 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
17e08 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
17e09 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 etData(pOvflPage
17e0a 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 );. if( isFre
17e0b 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 eList ){. i
17e0c 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 nt n = get4byte(
17e0d 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a &pOvflData[4]);.
17e0e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17e0f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17e10 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d if( pCheck-
17e11 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
17e12 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
17e13 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
17e14 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 iPage, PTRMAP_FR
17e15 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 EEPAGE, 0, zCont
17e16 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
17e17 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e ndif. if( n
17e18 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 >pCheck->pBt->us
17e19 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a ableSize/4-2 ){.
17e1a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
17e1b 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
17e1c 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
17e1d 20 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 "freelist le
17e1e 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 af count too big
17e1f 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 on page %d", iP
17e20 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d age);. N-
17e21 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a -;. }else{.
17e22 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
17e23 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<n; i++){.
17e24 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 Pgno iFree
17e25 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
17e26 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 &pOvflData[8+i*4
17e27 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ]);.#ifndef SQLI
17e28 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
17e29 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 UM. if(
17e2a 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 pCheck->pBt->au
17e2b 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
17e2c 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 checkPtr
17e2d 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 map(pCheck, iFre
17e2e 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 ePage, PTRMAP_FR
17e2f 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 EEPAGE, 0, zCont
17e30 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ext);.
17e31 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 }.#endif.
17e32 20 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 checkRef(pChe
17e33 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a ck, iFreePage, z
17e34 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
17e35 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d }. N -=
17e36 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n;. }.
17e37 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
17e38 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
17e39 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 . else{.
17e3a 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 /* If this data
17e3b 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
17e3c 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 to-vacuum and iP
17e3d 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c age is not the l
17e3e 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 ast. ** pag
17e3f 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c e in this overfl
17e40 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 ow list, check t
17e41 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d hat the pointer-
17e42 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 map entry for.
17e43 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f ** the follo
17e44 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 wing page matche
17e45 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a s iPage.. *
17e46 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 /. if( pChe
17e47 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 ck->pBt->autoVac
17e48 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 uum && N>0 ){.
17e49 20 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 i = get4by
17e4a 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 te(pOvflData);.
17e4b 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
17e4c 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 ap(pCheck, i, PT
17e4d 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 RMAP_OVERFLOW2,
17e4e 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 iPage, zContext)
17e4f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
17e50 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 #endif. iPage
17e51 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 = get4byte(pOvf
17e52 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 lData);. sqli
17e53 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f te3PagerUnref(pO
17e54 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a vflPage);. }.}.
17e55 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
17e56 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
17e57 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
17e58 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
17e59 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
17e5a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 .** Do various s
17e5b 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 anity checks on
17e5c 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 a single page of
17e5d 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e a tree. Return
17e5e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 .** the tree dep
17e5f 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 th. Root pages
17e60 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e return 0. Paren
17e61 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 ts of root pages
17e62 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e .** return 1, an
17e63 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a d so forth..** .
17e64 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 ** These checks
17e65 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 are done:.**.**
17e66 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 1. Make su
17e67 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e re that cells an
17e68 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 d freeblocks do
17e69 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 not overlap.**
17e6a 20 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 but comb
17e6b 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c ine to completel
17e6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 y cover the page
17e6d 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 ..** NO 2. Ma
17e6e 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 ke sure cell key
17e6f 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a s are in order..
17e70 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 ** NO 3. Make
17e71 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 sure no key is
17e72 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 less than or equ
17e73 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e al to zLowerBoun
17e74 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d d..** NO 4. M
17e75 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 ake sure no key
17e76 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
17e77 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 or equal to zUpp
17e78 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 erBound..**
17e79 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 5. Check the i
17e7a 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 ntegrity of over
17e7b 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 flow pages..**
17e7c 20 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 6. Recursiv
17e7d 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 ely call checkTr
17e7e 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 eePage on all ch
17e7f 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 ildren..**
17e80 37 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 7. Verify that
17e81 74 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c the depth of all
17e82 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 children is the
17e83 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 same..** 8
17e84 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 . Make sure thi
17e85 73 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 s page is at lea
17e86 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 st 33% full or e
17e87 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 lse it is.**
17e88 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f the root o
17e89 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 f the tree..*/.s
17e8a 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 tatic int checkT
17e8b 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 reePage(. Integ
17e8c 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
17e8d 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 /* Context for
17e8e 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b the sanity check
17e8f 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c */. int iPage,
17e90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
17e91 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
17e92 65 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 e page to check
17e93 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 */. char *zPare
17e94 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 ntContext /* Pa
17e95 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a rent context */.
17e96 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 ){. MemPage *pP
17e97 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 age;. int i, rc
17e98 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e , depth, d2, pgn
17e99 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 o, cnt;. int hd
17e9a 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 r, cellStart;.
17e9b 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 int nCell;. u8
17e9c 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 *data;. BtShare
17e9d 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 d *pBt;. int us
17e9e 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 ableSize;. char
17e9f 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a zContext[100];.
17ea0 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b char *hit = 0;
17ea1 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 .. sqlite3_snpr
17ea2 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e intf(sizeof(zCon
17ea3 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c text), zContext,
17ea4 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 "Page %d: ", iP
17ea5 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 age);.. /* Chec
17ea6 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 k that the page
17ea7 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 exists. */. pB
17ea8 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b t = pCheck->pBt;
17ea9 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 . usableSize =
17eaa 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
17eab 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 . if( iPage==0
17eac 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
17ead 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 ( checkRef(pChec
17eae 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e k, iPage, zParen
17eaf 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 tContext) ) retu
17eb0 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 rn 0;. if( (rc
17eb1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
17eb2 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f tPage(pBt, (Pgno
17eb3 29 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 )iPage, &pPage,
17eb4 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 0))!=0 ){. if
17eb5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
17eb6 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 EM || rc==SQLITE
17eb7 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 _IOERR_NOMEM ) p
17eb8 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 Check->mallocFai
17eb9 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 led = 1;. che
17eba 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
17ebb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 ck, zContext,.
17ebc 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 "unable to
17ebd 67 65 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 get the page. er
17ebe 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 ror code=%d", rc
17ebf 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
17ec0 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d . }. if( (rc =
17ec1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
17ec2 74 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 tPage(pPage))!=0
17ec3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
17ec4 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 rc==SQLITE_CORRU
17ec5 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e PT ); /* The on
17ec6 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f ly possible erro
17ec7 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20 r from InitPage
17ec8 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 */. checkAppe
17ec9 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
17eca 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 ontext, .
17ecb 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c "sql
17ecc 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
17ecd 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f e() returns erro
17ece 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b r code %d", rc);
17ecf 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
17ed0 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 (pPage);. ret
17ed1 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
17ed2 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 Check out all t
17ed3 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 he cells.. */.
17ed4 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f depth = 0;. fo
17ed5 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e r(i=0; i<pPage->
17ed6 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d nCell && pCheck-
17ed7 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 >mxErr; i++){.
17ed8 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 u8 *pCell;.
17ed9 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c u32 sz;. Cel
17eda 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 lInfo info;..
17edb 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 /* Check payloa
17edc 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 d overflow pages
17edd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
17ede 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
17edf 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a eof(zContext), z
17ee0 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
17ee1 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 "On tree p
17ee2 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 age %d cell %d:
17ee3 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 ", iPage, i);.
17ee4 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
17ee5 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 ll(pPage,i);.
17ee6 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
17ee7 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
17ee8 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
17ee9 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 sz = info.nD
17eea 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50 ata;. if( !pP
17eeb 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a age->intKey ) sz
17eec 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b += (int)info.nK
17eed 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ey;. assert(
17eee 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 sz==info.nPayloa
17eef 64 20 29 3b 0a 20 20 20 20 69 66 28 20 28 73 7a d );. if( (sz
17ef0 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 >info.nLocal) .
17ef1 20 20 20 20 26 26 20 28 26 70 43 65 6c 6c 5b 69 && (&pCell[i
17ef2 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d nfo.iOverflow]<=
17ef3 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
17ef4 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a t->usableSize]).
17ef5 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ){. int
17ef6 20 6e 50 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 nPage = (sz - i
17ef7 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 nfo.nLocal + usa
17ef8 62 6c 65 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 bleSize - 5)/(us
17ef9 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 ableSize - 4);.
17efa 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 Pgno pgnoOv
17efb 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 fl = get4byte(&p
17efc 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
17efd 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 low]);.#ifndef S
17efe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
17eff 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 ACUUM. if(
17f00 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
17f01 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
17f02 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
17f03 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f gnoOvfl, PTRMAP_
17f04 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 OVERFLOW1, iPage
17f05 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 , zContext);.
17f06 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
17f07 20 20 63 68 65 63 6b 4c 69 73 74 28 70 43 68 65 checkList(pChe
17f08 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c ck, 0, pgnoOvfl,
17f09 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 nPage, zContext
17f0a 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
17f0b 20 43 68 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 Check sanity of
17f0c 20 6c 65 66 74 20 63 68 69 6c 64 20 70 61 67 65 left child page
17f0d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
17f0e 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17f0f 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 . pgno = ge
17f10 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 t4byte(pCell);.#
17f11 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
17f12 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
17f13 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
17f14 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
17f15 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
17f16 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 Check, pgno, PTR
17f17 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 MAP_BTREE, iPage
17f18 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 , zContext);.
17f19 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
17f1a 20 20 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65 d2 = checkTree
17f1b 50 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e Page(pCheck, pgn
17f1c 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 o, zContext);.
17f1d 20 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 64 if( i>0 && d
17f1e 32 21 3d 64 65 70 74 68 20 29 7b 0a 20 20 20 20 2!=depth ){.
17f1f 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
17f20 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
17f21 65 78 74 2c 20 22 43 68 69 6c 64 20 70 61 67 65 ext, "Child page
17f22 20 64 65 70 74 68 20 64 69 66 66 65 72 73 22 29 depth differs")
17f23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17f24 64 65 70 74 68 20 3d 20 64 32 3b 0a 20 20 20 20 depth = d2;.
17f25 7d 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 }. }. if( !pPa
17f26 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
17f27 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
17f28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
17f29 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
17f2a 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ]);. sqlite3_
17f2b 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
17f2c 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 zContext), zCont
17f2d 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 ext, .
17f2e 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 70 "On p
17f2f 61 67 65 20 25 64 20 61 74 20 72 69 67 68 74 20 age %d at right
17f30 63 68 69 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 child: ", iPage)
17f31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
17f32 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
17f33 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 . if( pBt->au
17f34 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
17f35 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 checkPtrmap(pC
17f36 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d heck, pgno, PTRM
17f37 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c AP_BTREE, iPage,
17f38 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 0);. }.#endi
17f39 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 50 f. checkTreeP
17f3a 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f age(pCheck, pgno
17f3b 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d , zContext);. }
17f3c 0a 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f . . /* Check fo
17f3d 72 20 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 r complete cover
17f3e 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a age of the page.
17f3f 20 20 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 */. data = pP
17f40 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 age->aData;. hd
17f41 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
17f42 66 73 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 fset;. hit = sq
17f43 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
17f44 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 pBt->pageSize )
17f45 3b 0a 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29 ;. if( hit==0 )
17f46 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 {. pCheck->ma
17f47 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
17f48 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 31 36 }else{. u16
17f49 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d contentOffset =
17f4a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
17f4b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 69 66 20 hdr+5]);. if
17f4c 28 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3e (contentOffset >
17f4d 20 75 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 usableSize) {.
17f4e 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
17f4f 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a Msg(pCheck, 0, .
17f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f51 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e "Corruption
17f52 20 64 65 74 65 63 74 65 64 20 69 6e 20 68 65 61 detected in hea
17f53 64 65 72 20 6f 6e 20 70 61 67 65 20 25 64 22 2c der on page %d",
17f54 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 iPage,0);.
17f55 67 6f 74 6f 20 63 68 65 63 6b 5f 70 61 67 65 5f goto check_page_
17f56 61 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 abort;. }.
17f57 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 memset(hit+cont
17f58 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 entOffset, 0, us
17f59 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 ableSize-content
17f5a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d Offset);. mem
17f5b 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 set(hit, 1, cont
17f5c 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 entOffset);.
17f5d 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 nCell = get2byte
17f5e 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a (&data[hdr+3]);.
17f5f 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 cellStart =
17f60 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 hdr + 12 - 4*pPa
17f61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f ge->leaf;. fo
17f62 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
17f63 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
17f64 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
17f65 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a ata[cellStart+i*
17f66 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 2]);. u16 s
17f67 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 ize = 1024;.
17f68 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 int j;. i
17f69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a f( pc<=usableSiz
17f6a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a e ){. siz
17f6b 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 e = cellSizePtr(
17f6c 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d pPage, &data[pc]
17f6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
17f6e 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 if( (pc+size-1)
17f6f 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 >=usableSize ||
17f70 70 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 pc<0 ){.
17f71 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
17f72 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 Check, 0, .
17f73 20 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 "Corrupti
17f74 6f 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 on detected in c
17f75 65 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 ell %d on page %
17f76 64 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 d",i,iPage,0);.
17f77 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17f78 20 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a for(j=pc+siz
17f79 65 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 e-1; j>=pc; j--)
17f7a 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 hit[j]++;.
17f7b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 }. }. for
17f7c 28 63 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 (cnt=0, i=get2by
17f7d 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 te(&data[hdr+1])
17f7e 3b 20 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c ; i>0 && i<usabl
17f7f 65 53 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 eSize && cnt<100
17f80 30 30 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 00; .
17f81 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e cnt++){. in
17f82 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 t size = get2byt
17f83 65 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 e(&data[i+2]);.
17f84 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
17f85 20 20 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 if( (i+size-1)
17f86 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 >=usableSize ||
17f87 69 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 i<0 ){. c
17f88 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
17f89 68 65 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 heck, 0, .
17f8a 20 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 "Corrupti
17f8b 6f 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 on detected in c
17f8c 65 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 ell %d on page %
17f8d 64 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 d",i,iPage,0);.
17f8e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17f8f 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 for(j=i+size
17f90 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 -1; j>=i; j--) h
17f91 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d it[j]++;. }
17f92 0a 20 20 20 20 20 20 69 20 3d 20 67 65 74 32 62 . i = get2b
17f93 79 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 yte(&data[i]);.
17f94 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 }. for(i=c
17f95 6e 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 nt=0; i<usableSi
17f96 7a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ze; i++){.
17f97 69 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b if( hit[i]==0 ){
17f98 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a . cnt++;.
17f99 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
17f9a 68 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 hit[i]>1 ){.
17f9b 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
17f9c 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 sg(pCheck, 0,.
17f9d 20 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c "Multipl
17f9e 65 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 e uses for byte
17f9f 25 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 %d of page %d",
17fa0 69 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 i, iPage);.
17fa1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
17fa2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
17fa3 63 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d cnt!=data[hdr+7]
17fa4 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ){. checkA
17fa5 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
17fa6 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 0, . "
17fa7 46 72 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65 Fragmented space
17fa8 20 69 73 20 25 64 20 62 79 74 65 20 72 65 70 6f is %d byte repo
17fa9 72 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 rted as %d on pa
17faa 67 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 ge %d",.
17fab 20 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b cnt, data[hdr+
17fac 37 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7], iPage);.
17fad 7d 0a 20 20 7d 0a 63 68 65 63 6b 5f 70 61 67 65 }. }.check_page
17fae 5f 61 62 6f 72 74 3a 0a 20 20 69 66 20 28 68 69 _abort:. if (hi
17faf 74 29 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 t) sqlite3PageFr
17fb0 65 65 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 ee(hit);.. rele
17fb1 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
17fb2 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 return depth+1
17fb3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
17fb4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
17fb5 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 ITY_CHECK */..#i
17fb6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17fb7 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
17fb8 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 K./*.** This rou
17fb9 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 tine does a comp
17fba 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 lete check of th
17fbb 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 e given BTree fi
17fbc 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a le. aRoot[] is.
17fbd 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 ** an array of p
17fbe 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 ages numbers wer
17fbf 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 e each page numb
17fc0 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 er is the root p
17fc1 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c age of.** a tabl
17fc2 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 e. nRoot is the
17fc3 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
17fc4 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a es in aRoot..**.
17fc5 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d ** Write the num
17fc6 62 65 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65 ber of error see
17fc7 6e 20 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 n in *pnErr. Ex
17fc8 63 65 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 cept for some me
17fc9 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
17fca 6f 6e 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 on errors, an e
17fcb 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c rror message hel
17fcc 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 d in memory obta
17fcd 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c ined from.** mal
17fce 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20 loc is returned
17fcf 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e if *pnErr is non
17fd0 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 -zero. If *pnEr
17fd1 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 r==0 then NULL i
17fd2 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 s.** returned.
17fd3 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
17fd4 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
17fd5 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 urs, NULL is ret
17fd6 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
17fd7 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
17fd8 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 qlite3BtreeInteg
17fd9 72 69 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72 rityCheck(. Btr
17fda 65 65 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 ee *p, /* Th
17fdb 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 63 68 e btree to be ch
17fdc 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a ecked */. int *
17fdd 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 aRoot, /* An a
17fde 72 72 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 rray of root pag
17fdf 65 73 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 es numbers for i
17fe0 6e 64 69 76 69 64 75 61 6c 20 74 72 65 65 73 20 ndividual trees
17fe1 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 */. int nRoot,
17fe2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
17fe3 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 entries in aRoot
17fe4 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 [] */. int mxEr
17fe5 72 2c 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 r, /* Stop re
17fe6 70 6f 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61 porting errors a
17fe7 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a fter this many *
17fe8 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 /. int *pnErr
17fe9 20 20 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 /* Write numbe
17fea 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e r of errors seen
17feb 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c to this variabl
17fec 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 e */.){. Pgno i
17fed 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 ;. int nRef;.
17fee 49 6e 74 65 67 72 69 74 79 43 6b 20 73 43 68 65 IntegrityCk sChe
17fef 63 6b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ck;. BtShared *
17ff0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
17ff1 63 68 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a char zErr[100];.
17ff2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
17ff3 6e 74 65 72 28 70 29 3b 0a 20 20 6e 52 65 66 20 nter(p);. nRef
17ff4 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 = sqlite3PagerRe
17ff5 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 fcount(pBt->pPag
17ff6 65 72 29 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42 er);. if( lockB
17ff7 74 72 65 65 57 69 74 68 52 65 74 72 79 28 70 29 treeWithRetry(p)
17ff8 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17ff9 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 *pnErr = 1;.
17ffa 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
17ffb 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
17ffc 75 72 6e 20 73 71 6c 69 74 65 33 44 62 53 74 72 urn sqlite3DbStr
17ffd 44 75 70 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61 Dup(0, "cannot a
17ffe 63 71 75 69 72 65 20 61 20 72 65 61 64 20 6c 6f cquire a read lo
17fff 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
18000 73 65 22 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 se");. }. sChe
18001 63 6b 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 ck.pBt = pBt;.
18002 73 43 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 sCheck.pPager =
18003 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 pBt->pPager;. s
18004 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61 Check.nPage = pa
18005 67 65 72 50 61 67 65 63 6f 75 6e 74 28 73 43 68 gerPagecount(sCh
18006 65 63 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 eck.pBt);. sChe
18007 63 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 ck.mxErr = mxErr
18008 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 ;. sCheck.nErr
18009 3d 20 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 = 0;. sCheck.ma
1800a 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
1800b 20 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 *pnErr = 0;.
1800c 69 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 if( sCheck.nPage
1800d 3d 3d 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 ==0 ){. unloc
1800e 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 kBtreeIfUnused(p
1800f 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Bt);. sqlite3
18010 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18011 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
18012 0a 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 . sCheck.anRef
18013 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
18014 20 28 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 (sCheck.nPage+1
18015 29 2a 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e )*sizeof(sCheck.
18016 61 6e 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 anRef[0]) );. i
18017 66 28 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 f( !sCheck.anRef
18018 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 ){. unlockBt
18019 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
1801a 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 ;. *pnErr = 1
1801b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1801c 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
1801d 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1801e 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 for(i=0; i<=sChe
1801f 63 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 ck.nPage; i++){
18020 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 sCheck.anRef[i]
18021 3d 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e = 0; }. i = PEN
18022 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
18023 42 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 Bt);. if( i<=sC
18024 68 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 heck.nPage ){.
18025 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 sCheck.anRef[i
18026 5d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c ] = 1;. }. sql
18027 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
18028 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c (&sCheck.errMsg,
18029 20 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 zErr, sizeof(zE
1802a 72 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 rr), 20000);..
1802b 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 /* Check the int
1802c 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 egrity of the fr
1802d 65 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 eelist. */. ch
1802e 65 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c eckList(&sCheck,
1802f 20 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 1, get4byte(&pB
18030 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
18031 5b 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 [32]),.
18032 20 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 get4byte(&pBt
18033 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
18034 33 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 36]), "Main free
18035 6c 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 list: ");.. /*
18036 43 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 Check all the ta
18037 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 bles.. */. for
18038 28 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f (i=0; (int)i<nRo
18039 6f 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 ot && sCheck.mxE
1803a 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 rr; i++){. if
1803b 28 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 ( aRoot[i]==0 )
1803c 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 continue;.#ifnde
1803d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1803e 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
1803f 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18040 20 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 && aRoot[i]>1 )
18041 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 {. checkPtr
18042 6d 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f map(&sCheck, aRo
18043 6f 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f ot[i], PTRMAP_RO
18044 4f 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 OTPAGE, 0, 0);.
18045 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
18046 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 checkTreePage(&s
18047 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c Check, aRoot[i],
18048 20 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 "List of tree r
18049 6f 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 oots: ");. }..
1804a 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 /* Make sure ev
1804b 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
1804c 66 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 file is referenc
1804d 65 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d ed. */. for(i=
1804e 31 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 1; i<=sCheck.nPa
1804f 67 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 ge && sCheck.mxE
18050 72 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 rr; i++){.#ifdef
18051 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
18052 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
18053 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d sCheck.anRef[i]=
18054 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 =0 ){. chec
18055 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 kAppendMsg(&sChe
18056 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 ck, 0, "Page %d
18057 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 is never used",
18058 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a i);. }.#else.
18059 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 /* If the da
1805a 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
1805b 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b auto-vacuum, mak
1805c 65 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 e sure no tables
1805d 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 contain. **
1805e 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f references to po
1805f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e inter-map pages.
18060 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
18061 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d sCheck.anRef[i]=
18062 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 =0 && . (P
18063 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
18064 2c 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d , i)!=i || !pBt-
18065 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a >autoVacuum) ){.
18066 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e checkAppen
18067 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c dMsg(&sCheck, 0,
18068 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 "Page %d is nev
18069 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 er used", i);.
1806a 20 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 }. if( sChe
1806b 63 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 ck.anRef[i]!=0 &
1806c 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 & . (PTRMA
1806d 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 P_PAGENO(pBt, i)
1806e 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f ==i && pBt->auto
1806f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 Vacuum) ){.
18070 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18071 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 &sCheck, 0, "Poi
18072 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 nter map page %d
18073 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c is referenced",
18074 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 i);. }.#endi
18075 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 f. }.. /* Make
18076 20 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 sure this analy
18077 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 sis did not leav
18078 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 e any unref() pa
18079 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 ges.. ** This i
1807a 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f s an internal co
1807b 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b nsistency check;
1807c 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 an integrity ch
1807d 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 eck. ** of the
1807e 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e integrity check.
1807f 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 . */. unlockBt
18080 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
18081 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 ;. if( NEVER(nR
18082 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 ef != sqlite3Pag
18083 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e erRefcount(pBt->
18084 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 pPager)) ){.
18085 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 checkAppendMsg(&
18086 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 sCheck, 0, .
18087 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 "Outstanding p
18088 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 age count goes f
18089 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 rom %d to %d dur
1808a 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 ing this analysi
1808b 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 s",. nRef,
1808c 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
1808d 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
1808e 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 ). );. }..
1808f 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 /* Clean up and
18090 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a report errors..
18091 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
18092 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 reeLeave(p);. s
18093 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 qlite3_free(sChe
18094 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 ck.anRef);. if(
18095 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 sCheck.mallocFa
18096 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 iled ){. sqli
18097 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 te3StrAccumReset
18098 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 (&sCheck.errMsg)
18099 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 ;. *pnErr = s
1809a 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 Check.nErr+1;.
1809b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1809c 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 *pnErr = sChec
1809d 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 k.nErr;. if( sC
1809e 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 heck.nErr==0 ) s
1809f 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
180a0 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d set(&sCheck.errM
180a1 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 sg);. return sq
180a2 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
180a3 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d ish(&sCheck.errM
180a4 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a sg);.}.#endif /*
180a5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
180a6 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a EGRITY_CHECK */.
180a7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
180a8 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 e full pathname
180a9 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
180aa 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e g database file.
180ab 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
180ac 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 filename is inv
180ad 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 ariant as long a
180ae 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a s the pager is.*
180af 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 * open so it is
180b0 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 safe to access w
180b1 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 ithout the BtSha
180b2 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 red mutex..*/.SQ
180b3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
180b4 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
180b5 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
180b6 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
180b7 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 sert( p->pBt->pP
180b8 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 ager!=0 );. ret
180b9 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 urn sqlite3Pager
180ba 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d Filename(p->pBt-
180bb 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a >pPager);.}../*.
180bc 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 ** Return the pa
180bd 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f thname of the jo
180be 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 urnal file for t
180bf 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 his database. Th
180c0 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 e return.** valu
180c1 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e e of this routin
180c2 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 e is the same re
180c3 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
180c4 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 her the journal
180c5 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e file.** has been
180c6 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e created or not.
180c7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
180c8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d journal filenam
180c9 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 e is invariant a
180ca 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 s long as the pa
180cb 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 ger is.** open s
180cc 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 o it is safe to
180cd 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 access without t
180ce 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 he BtShared mute
180cf 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 x..*/.SQLITE_PRI
180d0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
180d1 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 *sqlite3BtreeGet
180d2 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 Journalname(Btre
180d3 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
180d4 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 p->pBt->pPager!
180d5 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 =0 );. return s
180d6 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
180d7 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 alname(p->pBt->p
180d8 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Pager);.}../*.**
180d9 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
180da 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f if a transactio
180db 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a n is active..*/.
180dc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
180dd 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
180de 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a sInTrans(Btree *
180df 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d p){. assert( p=
180e0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
180e1 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
180e2 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 mutex) );. retu
180e3 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 rn (p && (p->inT
180e4 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
180e5 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 E));.}../*.** Re
180e6 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 turn non-zero if
180e7 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 a read (or writ
180e8 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 e) transaction i
180e9 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c s active..*/.SQL
180ea 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
180eb 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
180ec 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 ReadTrans(Btree
180ed 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
180ee 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
180ef 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
180f0 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
180f1 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e ;. return p->in
180f2 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e Trans!=TRANS_NON
180f3 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 E;.}..SQLITE_PRI
180f4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
180f5 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 BtreeIsInBackup(
180f6 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
180f7 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 ert( p );. asse
180f8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
180f9 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
180fa 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
180fb 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a p->nBackup!=0;.
180fc 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
180fd 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 nction returns a
180fe 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c pointer to a bl
180ff 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 ob of memory ass
18100 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 ociated with.**
18101 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d a single shared-
18102 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 btree. The memor
18103 79 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 y is used by cli
18104 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 ent code for its
18105 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 own.** purposes
18106 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 (for example, t
18107 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c o store a high-l
18108 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f evel schema asso
18109 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 ciated with .**
1810a 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
1810b 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 ). The btree lay
1810c 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 er manages refer
1810d 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 ence counting is
1810e 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sues..**.** The
1810f 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
18110 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 is called on a s
18111 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 hared-btree, nBy
18112 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d tes bytes of mem
18113 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 ory.** are alloc
18114 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e ated, zeroed, an
18115 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 d returned to th
18116 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 e caller. For ea
18117 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a ch subsequent .*
18118 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 * call the nByte
18119 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 s parameter is i
1811a 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 gnored and a poi
1811b 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 nter to the same
1811c 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f blob.** of memo
1811d 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a ry returned. .**
1811e 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 .** If the nByte
1811f 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 s parameter is 0
18120 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 and the blob of
18121 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 memory has not
18122 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f yet been.** allo
18123 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f cated, a null po
18124 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
18125 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 d. If the blob h
18126 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a as already been.
18127 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 ** allocated, it
18128 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 is returned as
18129 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 normal..**.** Ju
1812a 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 st before the sh
1812b 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c ared-btree is cl
1812c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 osed, the functi
1812d 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 on passed as the
1812e 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d .** xFree argum
1812f 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d ent when the mem
18130 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 ory allocation w
18131 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b as made is invok
18132 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c ed on the .** bl
18133 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 ob of allocated
18134 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e memory. This fun
18135 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ction should not
18136 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 call sqlite3_fr
18137 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d ee().** on the m
18138 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 emory, the btree
18139 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 layer does that
1813a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1813b 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
1813c 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 3BtreeSchema(Btr
1813d 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 ee *p, int nByte
1813e 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 s, void(*xFree)(
1813f 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 void *)){. BtSh
18140 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
18141 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 Bt;. sqlite3Btr
18142 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
18143 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 ( !pBt->pSchema
18144 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 && nBytes ){.
18145 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 pBt->pSchema =
18146 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
18147 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 o(nBytes);. p
18148 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 Bt->xFreeSchema
18149 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 = xFree;. }. s
1814a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1814b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 (p);. return pB
1814c 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f t->pSchema;.}../
1814d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
1814e 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
1814f 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 CACHE if another
18150 20 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d user of the sam
18151 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 e shared .** btr
18152 65 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 ee as the argume
18153 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 nt handle holds
18154 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
18155 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c k on the .** sql
18156 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
18157 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 . Otherwise SQLI
18158 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
18159 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1815a 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c ite3BtreeSchemaL
1815b 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b ocked(Btree *p){
1815c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 . int rc;. ass
1815d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1815e 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
1815f 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
18160 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18161 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 . rc = querySha
18162 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
18163 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 k(p, MASTER_ROOT
18164 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 , READ_LOCK);.
18165 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
18166 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c TE_OK || rc==SQL
18167 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
18168 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 DCACHE );. sqli
18169 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1816a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1816b 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1816c 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1816d 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e CHE./*.** Obtain
1816e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
1816f 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 able whose root
18170 70 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 page is iTab. T
18171 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 he.** lock is a
18172 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 write lock if is
18173 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 Writelock is tru
18174 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b e or a read lock
18175 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c .** if it is fal
18176 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
18177 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18178 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 3BtreeLockTable(
18179 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
1817a 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f ab, u8 isWriteLo
1817b 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ck){. int rc =
1817c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
1817d 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
1817e 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 u8 lockType
1817f 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 = READ_LOCK + is
18180 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 WriteLock;. a
18181 73 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b ssert( READ_LOCK
18182 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 +1==WRITE_LOCK )
18183 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 ;. assert( is
18184 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 WriteLock==0 ||
18185 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 isWriteLock==1 )
18186 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
18187 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 eeEnter(p);.
18188 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 rc = queryShared
18189 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
1818a 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 , iTab, lockType
1818b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1818c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1818d 20 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 rc = setShared
1818e 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
1818f 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 , iTab, lockType
18190 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
18191 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18192 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
18193 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 rc;.}.#endif..#i
18194 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18195 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a T_INCRBLOB./*.**
18196 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d Argument pCsr m
18197 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 20 ust be a cursor
18198 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 opened for writi
18199 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 ng on an .** INT
1819a 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e KEY table curren
1819b 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 tly pointing at
1819c 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e a valid table en
1819d 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 try. .** This fu
1819e 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 nction modifies
1819f 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 20 the data stored
181a0 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 20 as part of that
181a1 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 entry..** Only t
181a2 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20 he data content
181a3 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 may only be modi
181a4 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 fied, it is not
181a5 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 63 possible.** to c
181a6 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 68 hange the length
181a7 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74 6f of the data sto
181a8 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
181a9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
181aa 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 e3BtreePutData(B
181ab 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 tCursor *pCsr, u
181ac 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
181ad 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 mt, void *z){.
181ae 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
181af 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
181b0 74 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 tex(pCsr) );. a
181b1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
181b2 75 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e utex_held(pCsr->
181b3 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 pBtree->db->mute
181b4 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 x) );. assert(p
181b5 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 Csr->isIncrblobH
181b6 61 6e 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f andle);.. resto
181b7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
181b8 28 70 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74 (pCsr);. assert
181b9 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d ( pCsr->eState!=
181ba 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
181bb 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 EK );. if( pCsr
181bc 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
181bd 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 _VALID ){. re
181be 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 turn SQLITE_ABOR
181bf 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 T;. }.. /* Che
181c0 63 6b 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69 ck some precondi
181c1 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 tions: . ** (
181c2 61 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 a) the cursor is
181c3 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e open for writin
181c4 67 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 g,. ** (b) th
181c5 65 72 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c ere is no read-l
181c6 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ock on the table
181c7 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 being modified
181c8 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 and. ** (c) t
181c9 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 he cursor points
181ca 20 61 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 at a valid row
181cb 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 of an intKey tab
181cc 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 le.. */. if( !
181cd 70 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a pCsr->wrFlag ){.
181ce 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
181cf 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a E_READONLY;. }.
181d0 20 20 61 73 73 65 72 74 28 20 21 70 43 73 72 2d assert( !pCsr-
181d1 3e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a >pBt->readOnly .
181d2 20 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73 && pCs
181d3 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 r->pBt->inTransa
181d4 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
181d5 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65 TE );. rc = che
181d6 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 ckForReadConflic
181d7 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c ts(pCsr->pBtree,
181d8 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCsr->pgnoRoot,
181d9 20 70 43 73 72 2c 20 30 29 3b 0a 20 20 69 66 28 pCsr, 0);. if(
181da 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
181db 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 {. /* The tab
181dc 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 le pCur points t
181dd 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 o has a read loc
181de 6b 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 k */. assert(
181df 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc==SQLITE_LOCK
181e0 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 ED_SHAREDCACHE )
181e1 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
181e2 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 73 72 2d . }. if( pCsr-
181e3 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
181e4 49 4e 56 41 4c 49 44 20 7c 7c 20 21 70 43 73 72 INVALID || !pCsr
181e5 2d 3e 61 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 ->apPage[pCsr->i
181e6 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b Page]->intKey ){
181e7 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
181e8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 TE_ERROR;. }..
181e9 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 return accessPa
181ea 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 yload(pCsr, offs
181eb 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e et, amt, (unsign
181ec 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 30 2c 20 ed char *)z, 0,
181ed 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 1);.}../* .** Se
181ee 74 20 61 20 66 6c 61 67 20 6f 6e 20 74 68 69 73 t a flag on this
181ef 20 63 75 72 73 6f 72 20 74 6f 20 63 61 63 68 65 cursor to cache
181f0 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f the locations o
181f1 66 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 f pages from the
181f2 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69 .** overflow li
181f3 73 74 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 st for the curre
181f4 6e 74 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20 nt row. This is
181f5 75 73 65 64 20 62 79 20 63 75 72 73 6f 72 73 20 used by cursors
181f6 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e opened.** for in
181f7 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 cremental blob I
181f8 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 O only..**.** Th
181f9 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 is function sets
181fa 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 a flag only. Th
181fb 65 20 61 63 74 75 61 6c 20 70 61 67 65 20 6c 6f e actual page lo
181fc 63 61 74 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 cation cache.**
181fd 28 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 (stored in BtCur
181fe 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 sor.aOverflow[])
181ff 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
18200 64 20 75 73 65 64 20 62 79 20 66 75 6e 63 74 69 d used by functi
18201 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50 61 79 6c on.** accessPayl
18202 6f 61 64 28 29 20 28 74 68 65 20 77 6f 72 6b 65 oad() (the worke
18203 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73 r function for s
18204 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
18205 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ) and.** sqlite3
18206 42 74 72 65 65 50 75 74 44 61 74 61 28 29 29 2e BtreePutData()).
18207 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18208 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
18209 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f treeCacheOverflo
1820a 77 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 w(BtCursor *pCur
1820b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
1820c 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1820d 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
1820e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1820f 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
18210 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
18211 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e assert(!pCur->
18212 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 isIncrblobHandle
18213 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43 75 );. assert(!pCu
18214 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 r->aOverflow);.
18215 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f pCur->isIncrblo
18216 62 48 61 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 bHandle = 1;.}.#
18217 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
18218 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 ****** End of bt
18219 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ree.c **********
1821a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1821b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1821c 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1821d 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1821e 65 20 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a e backup.c *****
1821f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18221 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
18222 39 20 4a 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 9 January 28.**.
18223 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
18224 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
18225 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
18226 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
18227 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
18228 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
18229 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1822a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1822b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1822c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1822d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1822e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1822f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
18230 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
18231 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
18232 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
18233 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
18234 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
18235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18237 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18238 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
18239 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
1823a 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
1823b 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
1823c 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 te3_backup_XXX()
1823d 20 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f .** API functio
1823e 6e 73 20 61 6e 64 20 74 68 65 20 72 65 6c 61 74 ns and the relat
1823f 65 64 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a ed features..**.
18240 2a 2a 20 24 49 64 3a 20 62 61 63 6b 75 70 2e 63 ** $Id: backup.c
18241 2c 76 20 31 2e 31 33 2e 32 2e 31 20 32 30 30 39 ,v 1.13.2.1 2009
18242 2f 30 35 2f 31 38 20 31 37 3a 31 31 3a 33 31 20 /05/18 17:11:31
18243 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
18244 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 Macro to find t
18245 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 he minimum of tw
18246 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 o numeric values
18247 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e ..*/.#ifndef MIN
18248 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c .# define MIN(x,
18249 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a y) ((x)<(y)?(x):
1824a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a (y)).#endif../*.
1824b 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c ** Structure all
1824c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
1824d 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
1824e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
1824f 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73 te3_backup {. s
18250 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b qlite3* pDestDb;
18251 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 /* Desti
18252 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
18253 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 handle */. Btre
18254 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 e *pDest;
18255 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 /* Destinat
18256 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20 ion b-tree file
18257 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63 */. u32 iDestSc
18258 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a hema; /*
18259 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61 Original schema
1825a 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69 cookie in desti
1825b 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
1825c 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20 bDestLocked;
1825d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 /* True onc
1825e 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 e a write-transa
1825f 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e ction is open on
18260 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e pDest */.. Pgn
18261 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 o iNext;
18262 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
18263 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
18264 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20 source page to
18265 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 copy */. sqlite
18266 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20 3* pSrcDb;
18267 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 /* Source dat
18268 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
18269 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20 Btree *pSrc;
1826a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
1826b 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65 urce b-tree file
1826c 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20 */.. int rc;
1826d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1826e 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73 /* Backup proces
1826f 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a s error code */.
18270 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 . /* These two
18271 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 variables are se
18272 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 t by every call
18273 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 to backup_step()
18274 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20 . They are. **
18275 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f read by calls to
18276 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
18277 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 g() and backup_p
18278 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f agecount().. */
18279 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 . Pgno nRemaini
1827a 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ng; /* N
1827b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c umber of pages l
1827c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1827d 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74 Pgno nPagecount
1827e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
1827f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
18280 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 es to copy */..
18281 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b int isAttached;
18282 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
18283 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61 e once backup ha
18284 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 s been registere
18285 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a d with pager */.
18286 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
18287 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 *pNext; /* Ne
18288 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69 xt backup associ
18289 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65 ated with source
1828a 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pager */.};../*
1828b 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54 .** THREAD SAFET
1828c 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 Y NOTES:.**.**
1828d 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65 Once it has bee
1828e 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 n created using
1828f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61 backup_init(), a
18290 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f single sqlite3_
18291 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75 backup.** stru
18292 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63 cture may be acc
18293 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72 essed via two gr
18294 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73 oups of thread-s
18295 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 afe entry points
18296 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 :.**.** * Vi
18297 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 a the sqlite3_ba
18298 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 ckup_XXX() API f
18299 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 unction backup_s
1829a 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20 tep() and .**
1829b 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 backup_finis
1829c 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 h(). Both these
1829d 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e functions obtain
1829e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1829f 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61 base.** ha
182a0 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74 ndle mutex and t
182a1 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 he mutex associa
182a2 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 ted with the sou
182a3 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a rce BtShared .**
182a4 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65 structure
182a5 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e , in that order.
182a6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 .**.** * Via
182a7 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74 the BackupUpdat
182a8 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65 e() and BackupRe
182a9 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e start() function
182aa 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 s, which are.**
182ab 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79 invoked by
182ac 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
182ad 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f to report vario
182ae 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73 us state changes
182af 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 in.** the
182b0 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f page cache asso
182b1 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
182b2 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e source database.
182b3 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 The mutex.**
182b4 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 associated w
182b5 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 ith the source d
182b6 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64 atabase BtShared
182b7 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 structure will
182b8 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20 always .**
182b9 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69 be held when ei
182ba 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75 ther of these fu
182bb 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f nctions are invo
182bc 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 ked..**.** The
182bd 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62 other sqlite3_b
182be 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 ackup_XXX() API
182bf 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75 functions, backu
182c0 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e p_remaining() an
182c1 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61 d.** backup_pa
182c2 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f gecount() are no
182c3 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75 t thread-safe fu
182c4 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79 nctions. If they
182c5 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20 are called.**
182c6 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65 while some othe
182c7 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c r thread is call
182c8 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28 ing backup_step(
182c9 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69 ) or backup_fini
182ca 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76 sh(),.** the v
182cb 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d alues returned m
182cc 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54 ay be invalid. T
182cd 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 here is no way f
182ce 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 or a call to.**
182cf 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 BackupUpdate()
182d0 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72 or BackupRestar
182d1 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65 t() to interfere
182d2 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d with backup_rem
182d3 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72 aining().** or
182d4 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e backup_pagecoun
182d5 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70 t()..**.** Dep
182d6 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51 ending on the SQ
182d7 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69 Lite configurati
182d8 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 on, the database
182d9 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a handles and/or.
182da 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f ** the Btree o
182db 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20 bjects may have
182dc 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65 their own mutexe
182dd 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c s that require l
182de 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e ocking..** Non
182df 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 -sharable Btrees
182e0 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 (in-memory data
182e1 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c bases for exampl
182e2 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a e), do not have.
182e3 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 ** associated
182e4 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a mutexes..*/../*.
182e5 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
182e6 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ter correspondin
182e7 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44 g to database zD
182e8 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 b (i.e. "main",
182e9 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f "temp").** in co
182ea 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
182eb 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64 pDb. If such a d
182ec 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 atabase cannot b
182ed 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a e found, return.
182ee 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ** a NULL pointe
182ef 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65 r and write an e
182f0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
182f1 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 pErrorDb..**.**
182f2 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61 If the "temp" da
182f3 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73 tabase is reques
182f4 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 ted, it may need
182f5 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79 to be opened by
182f6 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
182f7 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 on. If an error
182f8 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 occurs while doi
182f9 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 ng so, return 0
182fa 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a and write an .**
182fb 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
182fc 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73 o pErrorDb..*/.s
182fd 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e tatic Btree *fin
182fe 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a dBtree(sqlite3 *
182ff 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 pErrorDb, sqlite
18300 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 3 *pDb, const ch
18301 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20 ar *zDb){. int
18302 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 i = sqlite3FindD
18303 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b bName(pDb, zDb);
18304 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a .. if( i==1 ){.
18305 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65 Parse sParse
18306 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 50 ;. memset(&sP
18307 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 arse, 0, sizeof(
18308 73 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 50 sParse));. sP
18309 61 72 73 65 2e 64 62 20 3d 20 70 44 62 3b 0a 20 arse.db = pDb;.
1830a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 if( sqlite3Op
1830b 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 26 enTempDatabase(&
1830c 73 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 sParse) ){.
1830d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
1830e 61 72 28 26 73 50 61 72 73 65 29 3b 0a 20 20 20 ar(&sParse);.
1830f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
18310 70 45 72 72 6f 72 44 62 2c 20 73 50 61 72 73 65 pErrorDb, sParse
18311 2e 72 63 2c 20 22 25 73 22 2c 20 73 50 61 72 73 .rc, "%s", sPars
18312 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 e.zErrMsg);.
18313 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
18314 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 50 }. assert( sP
18315 61 72 73 65 2e 7a 45 72 72 4d 73 67 3d 3d 30 20 arse.zErrMsg==0
18316 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c );. }.. if( i<
18317 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
18318 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 Error(pErrorDb,
18319 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 SQLITE_ERROR, "u
1831a 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 nknown database
1831b 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72 %s", zDb);. r
1831c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
1831d 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b return pDb->aDb[
1831e 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a i].pBt;.}../*.**
1831f 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 Create an sqlit
18320 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65 73 e3_backup proces
18321 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f s to copy the co
18322 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44 62 ntents of zSrcDb
18323 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 from.** connect
18324 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63 44 ion handle pSrcD
18325 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e 20 b to zDestDb in
18326 70 44 65 73 74 44 62 2e 20 49 66 20 73 75 63 63 pDestDb. If succ
18327 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a essful, return.*
18328 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 * a pointer to t
18329 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 he new sqlite3_b
1832a 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a ackup object..**
1832b 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
1832c 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 occurs, NULL is
1832d 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 returned and an
1832e 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 error code and e
1832f 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
18330 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 stored in databa
18331 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74 44 se handle pDestD
18332 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 b..*/.SQLITE_API
18333 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
18334 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f *sqlite3_backup_
18335 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 2a init(. sqlite3*
18336 20 70 44 65 73 74 44 62 2c 20 20 20 20 20 20 20 pDestDb,
18337 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18338 20 44 61 74 61 62 61 73 65 20 74 6f 20 77 72 69 Database to wri
18339 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 te to */. const
1833a 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c 20 char *zDestDb,
1833b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1833c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 /* Name of data
1833d 62 61 73 65 20 77 69 74 68 69 6e 20 70 44 65 73 base within pDes
1833e 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 tDb */. sqlite3
1833f 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 20 * pSrcDb,
18340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18341 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
18342 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 72 ction to read fr
18343 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 om */. const ch
18344 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 20 ar *zSrcDb
18345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18346 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 Name of databas
18347 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 20 e within pSrcDb
18348 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f */.){. sqlite3_
18349 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 backup *p;
1834a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1834b 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e Value to return
1834c 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 */.. /* Lock t
1834d 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
1834e 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 se handle. The d
1834f 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
18350 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 ase. ** handle
18351 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e is not locked in
18352 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 62 this routine, b
18353 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64 20 ut it is locked
18354 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f in. ** sqlite3_
18355 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 backup_step(). T
18356 68 65 20 75 73 65 72 20 69 73 20 72 65 71 75 69 he user is requi
18357 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 red to ensure th
18358 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65 72 at no. ** other
18359 20 74 68 72 65 61 64 20 61 63 63 65 73 73 65 73 thread accesses
1835a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
1835b 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 handle for the
1835c 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66 duration. ** of
1835d 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
1835e 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 ation. Any atte
1835f 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 64 mpt to use the d
18360 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 estination. **
18361 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
18362 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b ion while a back
18363 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 up is in progres
18364 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a 2a s may cause. **
18365 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 6f a malfunction o
18366 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 r a deadlock..
18367 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
18368 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62 2d ex_enter(pSrcDb-
18369 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 >mutex);. sqlit
1836a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1836b 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a DestDb->mutex);.
1836c 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d 70 . if( pSrcDb==p
1836d 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 DestDb ){. sq
1836e 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20 20 lite3Error(.
1836f 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 51 4c pDestDb, SQL
18370 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75 72 ITE_ERROR, "sour
18371 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 ce and destinati
18372 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74 69 on must be disti
18373 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 nct". );.
18374 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 7b p = 0;. }else {
18375 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
18376 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 space for a new
18377 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
18378 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 20 object */. p
18379 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 = (sqlite3_backu
1837a 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c p *)sqlite3_mall
1837b 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 oc(sizeof(sqlite
1837c 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20 3_backup));.
1837d 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 if( !p ){.
1837e 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44 65 sqlite3Error(pDe
1837f 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d stDb, SQLITE_NOM
18380 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 EM, 0);. }.
18381 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 }.. /* If the a
18382 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 65 llocation succee
18383 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 ded, populate th
18384 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a 2f e new object. */
18385 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
18386 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a memset(p, 0, siz
18387 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 6b eof(sqlite3_back
18388 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72 up));. p->pSr
18389 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 c = findBtree(pD
1838a 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 7a estDb, pSrcDb, z
1838b 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 SrcDb);. p->p
1838c 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 65 Dest = findBtree
1838d 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 44 (pDestDb, pDestD
1838e 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 20 b, zDestDb);.
1838f 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 44 p->pDestDb = pD
18390 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 53 estDb;. p->pS
18391 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a 20 rcDb = pSrcDb;.
18392 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b p->iNext = 1;
18393 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63 68 . p->isAttach
18394 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 ed = 0;.. if(
18395 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20 30 0==p->pSrc || 0
18396 3d 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a 20 20 ==p->pDest ){.
18397 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 /* One (or b
18398 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d 65 oth) of the name
18399 64 20 64 61 74 61 62 61 73 65 73 20 64 69 64 20 d databases did
1839a 6e 6f 74 20 65 78 69 73 74 2e 20 41 6e 20 65 72 not exist. An er
1839b 72 6f 72 20 68 61 73 0a 20 20 20 20 20 20 2a 2a ror has. **
1839c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 already been wr
1839d 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 70 itten into the p
1839e 44 65 73 74 44 62 20 68 61 6e 64 6c 65 2e 20 41 DestDb handle. A
1839f 6c 6c 20 74 68 61 74 20 69 73 20 6c 65 66 74 0a ll that is left.
183a0 20 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 ** to do h
183a1 65 72 65 20 69 73 20 66 72 65 65 20 74 68 65 20 ere is free the
183a2 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 sqlite3_backup s
183a3 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 tructure..
183a4 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
183a5 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 _free(p);.
183a6 70 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d p = 0;. }. }
183a7 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
183a8 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 p->pSrc->nBackup
183a9 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 ++;. }.. sqlit
183aa 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
183ab 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a DestDb->mutex);.
183ac 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
183ad 6c 65 61 76 65 28 70 53 72 63 44 62 2d 3e 6d 75 leave(pSrcDb->mu
183ae 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 tex);. return p
183af 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d ;.}../*.** Argum
183b0 65 6e 74 20 72 63 20 69 73 20 61 6e 20 53 51 4c ent rc is an SQL
183b1 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 ite error code.
183b2 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
183b3 68 69 73 20 65 72 72 6f 72 20 69 73 20 0a 2a 2a his error is .**
183b4 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 considered fata
183b5 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65 72 65 64 l if encountered
183b6 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 during a backup
183b7 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c 6c 20 operation. All
183b8 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20 63 6f errors.** are co
183b9 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 65 nsidered fatal e
183ba 78 63 65 70 74 20 66 6f 72 20 53 51 4c 49 54 45 xcept for SQLITE
183bb 5f 42 55 53 59 20 61 6e 64 20 53 51 4c 49 54 45 _BUSY and SQLITE
183bc 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74 _LOCKED..*/.stat
183bd 69 63 20 69 6e 74 20 69 73 46 61 74 61 6c 45 72 ic int isFatalEr
183be 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 72 ror(int rc){. r
183bf 65 74 75 72 6e 20 28 72 63 21 3d 53 51 4c 49 54 eturn (rc!=SQLIT
183c0 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 E_OK && rc!=SQLI
183c1 54 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 TE_BUSY && rc!=S
183c2 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 3b 0a 7d QLITE_LOCKED);.}
183c3 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 ../*.** Paramete
183c4 72 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74 r zSrcData point
183c5 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f s to a buffer co
183c6 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74 ntaining the dat
183c7 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 a for .** page i
183c8 53 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73 SrcPg from the s
183c9 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 ource database.
183ca 43 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69 Copy this data i
183cb 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 nto the .** dest
183cc 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
183cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
183ce 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 backupOnePage(sq
183cf 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c lite3_backup *p,
183d0 20 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f Pgno iSrcPg, co
183d1 6e 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61 nst u8 *zSrcData
183d2 29 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e ){. Pager * con
183d3 73 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 st pDestPager =
183d4 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
183d5 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 r(p->pDest);. c
183d6 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73 onst int nSrcPgs
183d7 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 z = sqlite3Btree
183d8 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 GetPageSize(p->p
183d9 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 Src);. int nDes
183da 74 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 tPgsz = sqlite3B
183db 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
183dc 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e p->pDest);. con
183dd 73 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d st int nCopy = M
183de 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 IN(nSrcPgsz, nDe
183df 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 stPgsz);. const
183e0 20 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34 i64 iEnd = (i64
183e1 29 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72 )iSrcPg*(i64)nSr
183e2 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 cPgsz;.. int rc
183e3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
183e4 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 i64 iOff;.. ass
183e5 65 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 ert( p->bDestLoc
183e6 6b 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ked );. assert(
183e7 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 !isFatalError(p
183e8 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72 ->rc) );. asser
183e9 74 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 t( iSrcPg!=PENDI
183ea 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e NG_BYTE_PAGE(p->
183eb 70 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 pSrc->pBt) );.
183ec 61 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61 assert( zSrcData
183ed 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 );.. /* Catch
183ee 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 the case where t
183ef 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 he destination i
183f0 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 s an in-memory d
183f1 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a atabase and the.
183f2 20 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20 ** page sizes
183f3 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e of the source an
183f4 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 d destination di
183f5 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 ffer. . */. if
183f6 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 ( nSrcPgsz!=nDes
183f7 74 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33 tPgsz && sqlite3
183f8 50 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c PagerIsMemdb(sql
183f9 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
183fa 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20 ->pDest)) ){.
183fb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
183fc 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a DONLY;. }.. /*
183fd 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 This loop runs
183fe 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65 once for each de
183ff 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 stination page s
18400 70 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f panned by the so
18401 75 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e urce . ** page.
18402 20 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 For each iterat
18403 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f ion, variable iO
18404 66 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ff is set to the
18405 20 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a byte offset. *
18406 2a 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 * of the destina
18407 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a tion page.. */.
18408 20 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d for(iOff=iEnd-
18409 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 (i64)nSrcPgsz; r
1840a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1840b 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b iOff<iEnd; iOff+
1840c 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 =nDestPgsz){.
1840d 20 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67 DbPage *pDestPg
1840e 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 = 0;. Pgno i
1840f 44 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f Dest = (Pgno)(iO
18410 66 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b ff/nDestPgsz)+1;
18411 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d . if( iDest==
18412 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
18413 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 E(p->pDest->pBt)
18414 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
18415 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
18416 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
18417 65 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72 erGet(pDestPager
18418 2c 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50 , iDest, &pDestP
18419 67 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 g)). && SQLI
1841a 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
1841b 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
1841c 44 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a DestPg)). ){.
1841d 20 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a const u8 *
1841e 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b zIn = &zSrcData[
1841f 69 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a iOff%nSrcPgsz];.
18420 20 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44 u8 *zDestD
18421 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ata = sqlite3Pag
18422 65 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50 erGetData(pDestP
18423 67 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f g);. u8 *zO
18424 75 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b ut = &zDestData[
18425 69 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b iOff%nDestPgsz];
18426 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 .. /* Copy
18427 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 the data from th
18428 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e e source page in
18429 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 to the destinati
1842a 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a on page.. *
1842b 2a 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 * Then clear the
1842c 20 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d Btree layer Mem
1842d 50 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 Page.isInit flag
1842e 2e 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 . Both this modu
1842f 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 le. ** and
18430 74 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75 the pager code u
18431 73 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63 se this trick (c
18432 6c 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73 learing the firs
18433 74 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 t byte. **
18434 6f 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74 of the page 'ext
18435 72 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76 ra' space to inv
18436 61 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65 alidate the Btre
18437 65 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a e layers. *
18438 2a 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f * cached parse o
18439 66 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d f the page). Mem
1843a 50 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d Page.isInit is m
1843b 61 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 arked . **
1843c 22 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20 "MUST BE FIRST"
1843d 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 for this purpose
1843e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1843f 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 memcpy(zOut, zI
18440 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 n, nCopy);.
18441 20 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 ((u8 *)sqlite3P
18442 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65 agerGetExtra(pDe
18443 73 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 stPg))[0] = 0;.
18444 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
18445 50 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74 PagerUnref(pDest
18446 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 Pg);. }.. retu
18447 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
18448 49 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72 If pFile is curr
18449 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 ently larger tha
1844a 6e 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 n iSize bytes, t
1844b 68 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20 hen truncate it
1844c 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 to.** exactly iS
1844d 69 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46 ize bytes. If pF
1844e 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 ile is not large
1844f 72 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 r than iSize byt
18450 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 es, then.** this
18451 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
18452 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 o-op..**.** Retu
18453 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
18454 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 everything is su
18455 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 ccessful, or an
18456 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a SQLite error .**
18457 20 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f code if an erro
18458 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 r occurs..*/.sta
18459 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72 tic int backupTr
1845a 75 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74 uncateFile(sqlit
1845b 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
1845c 69 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 i64 iSize){. i6
1845d 34 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 4 iCurrent;. in
1845e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 t rc = sqlite3Os
1845f 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 FileSize(pFile,
18460 26 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 &iCurrent);. if
18461 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18462 26 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a && iCurrent>iSiz
18463 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 e ){. rc = sq
18464 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
18465 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 pFile, iSize);.
18466 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
18467 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 }../*.** Registe
18468 72 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62 r this backup ob
18469 6a 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73 ject with the as
1846a 73 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20 sociated source
1846b 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c pager for.** cal
1846c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65 lbacks when page
1846d 73 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72 s are changed or
1846e 20 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c the cache inval
1846f 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 idated..*/.stati
18470 63 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63 c void attachBac
18471 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 kupObject(sqlite
18472 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 3_backup *p){.
18473 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
18474 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 *pp;. assert( s
18475 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
18476 4d 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29 Mutex(p->pSrc) )
18477 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 ;. pp = sqlite3
18478 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 PagerBackupPtr(s
18479 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
1847a 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d (p->pSrc));. p-
1847b 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 >pNext = *pp;.
1847c 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 *pp = p;. p->is
1847d 41 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a Attached = 1;.}.
1847e 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 ./*.** Copy nPag
1847f 65 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 e pages from the
18480 20 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74 source b-tree t
18481 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
18482 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 n..*/.SQLITE_API
18483 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
18484 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 kup_step(sqlite3
18485 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 _backup *p, int
18486 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 nPage){. int rc
18487 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ;.. sqlite3_mut
18488 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 ex_enter(p->pSrc
18489 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 Db->mutex);. sq
1848a 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1848b 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 p->pSrc);. if(
1848c 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
1848d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1848e 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 enter(p->pDestDb
1848f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 ->mutex);. }..
18490 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 rc = p->rc;. i
18491 66 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 f( !isFatalError
18492 28 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65 (rc) ){. Page
18493 72 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61 r * const pSrcPa
18494 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 ger = sqlite3Btr
18495 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 eePager(p->pSrc)
18496 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 ; /* Source
18497 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 pager */. Pag
18498 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 er * const pDest
18499 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 Pager = sqlite3B
1849a 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 treePager(p->pDe
1849b 73 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 st); /* Dest p
1849c 61 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ager */. int
1849d 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ii;
1849e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1849f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
184a0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e ble */. int n
184a1 53 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 SrcPage = -1;
184a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
184a3 20 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 Size of source
184a4 64 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 db in pages */.
184a5 20 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 int bCloseTra
184a6 6e 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ns = 0;
184a7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
184a8 20 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73 src db requires
184a9 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 unlocking */..
184aa 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 /* If the sou
184ab 72 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72 rce pager is cur
184ac 72 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74 rently in a writ
184ad 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 e-transaction, r
184ae 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c eturn. ** SQL
184af 49 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 ITE_BUSY immedia
184b0 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tely.. */.
184b1 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
184b2 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d && p->pSrc->pBt-
184b3 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
184b4 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 TRANS_WRITE ){.
184b5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
184b6 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 _BUSY;. }else
184b7 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
184b8 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 ITE_OK;. }..
184b9 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 /* Lock the d
184ba 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
184bb 61 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f ase, if it is no
184bc 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 t locked already
184bd 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c . */. if( SQL
184be 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d ITE_OK==rc && p-
184bf 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a >bDestLocked==0.
184c0 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f && SQLITE_O
184c1 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
184c2 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
184c3 70 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20 p->pDest, 2)) .
184c4 20 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 ){. p->b
184c5 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a DestLocked = 1;.
184c6 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
184c7 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 e3BtreeGetMeta(p
184c8 2d 3e 70 44 65 73 74 2c 20 31 2c 20 26 70 2d 3e ->pDest, 1, &p->
184c9 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20 20 iDestSchema);.
184ca 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
184cb 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 here is no open
184cc 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e read-transaction
184cd 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 on the source d
184ce 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20 atabase, open.
184cf 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 ** one now. If
184d0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
184d1 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20 74 s opened here, t
184d2 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 63 hen it will be c
184d3 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 losed. ** bef
184d4 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
184d5 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a n exits.. */.
184d6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
184d7 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 TE_OK && 0==sqli
184d8 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 te3BtreeIsInRead
184d9 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 Trans(p->pSrc) )
184da 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
184db 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 ite3BtreeBeginTr
184dc 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b ans(p->pSrc, 0);
184dd 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72 61 . bCloseTra
184de 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ns = 1;. }.
184df 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 . /* Now that
184e0 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 there is a read
184e1 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75 -lock on the sou
184e2 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71 75 rce database, qu
184e3 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 ery the. ** s
184e4 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 20 ource pager for
184e5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
184e6 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
184e7 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
184e8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
184e9 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
184ea 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
184eb 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72 2c count(pSrcPager,
184ec 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20 20 &nSrcPage);.
184ed 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b }. for(ii=0;
184ee 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c (nPage<0 || ii<
184ef 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 nPage) && p->iNe
184f0 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 xt<=(Pgno)nSrcPa
184f1 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 ge && !rc; ii++)
184f2 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 {. const Pg
184f3 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 no iSrcPg = p->i
184f4 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
184f5 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
184f6 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 page number */.
184f7 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67 21 if( iSrcPg!
184f8 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
184f9 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 GE(p->pSrc->pBt)
184fa 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 61 ){. DbPa
184fb 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 20 ge *pSrcPg;
184fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
184fd 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
184fe 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f e page object */
184ff 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
18500 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53 lite3PagerGet(pS
18501 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c rcPager, iSrcPg,
18502 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 &pSrcPg);.
18503 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
18504 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
18505 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 rc = backupOne
18506 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c 20 Page(p, iSrcPg,
18507 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
18508 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 20 ata(pSrcPg));.
18509 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
1850a 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 agerUnref(pSrcPg
1850b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1850c 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e }. p->iN
1850d 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ext++;. }.
1850e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1850f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e OK ){. p->n
18510 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 Pagecount = nSrc
18511 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e Page;. p->n
18512 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 Remaining = nSrc
18513 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b Page+1-p->iNext;
18514 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4e . if( p->iN
18515 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 ext>(Pgno)nSrcPa
18516 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ge ){. rc
18517 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a = SQLITE_DONE;.
18518 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
18519 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29 !p->isAttached )
1851a 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 68 {. attach
1851b 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b BackupObject(p);
1851c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1851d 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 . if( rc==SQ
1851e 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1851f 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 const int nSr
18520 63 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 cPagesize = sqli
18521 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 te3BtreeGetPageS
18522 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 ize(p->pSrc);.
18523 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 const int nD
18524 65 73 74 50 61 67 65 73 69 7a 65 20 3d 20 73 71 estPagesize = sq
18525 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
18526 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b eSize(p->pDest);
18527 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 65 73 74 . int nDest
18528 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20 Truncate;. .
18529 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
1852a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 schema version
1852b 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73 field in the des
1852c 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
1852d 65 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a e. This. **
1852e 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 is to make sure
1852f 20 74 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 that the schema
18530 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 -version really
18531 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 does change in.
18532 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 ** the case
18533 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 where the sourc
18534 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f e and destinatio
18535 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65 n databases have
18536 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 61 the. ** sa
18537 6d 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f me schema versio
18538 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
18539 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 sqlite3BtreeUp
1853a 64 61 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 dateMeta(p->pDes
1853b 74 2c 20 31 2c 20 70 2d 3e 69 44 65 73 74 53 63 t, 1, p->iDestSc
1853c 68 65 6d 61 2b 31 29 3b 0a 20 20 20 20 20 20 69 hema+1);. i
1853d 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
1853e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1853f 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 ResetInternalSch
18540 65 6d 61 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 ema(p->pDestDb,
18541 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0);. }..
18542 20 20 20 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 /* Set nDestT
18543 72 75 6e 63 61 74 65 20 74 6f 20 74 68 65 20 66 runcate to the f
18544 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 inal number of p
18545 61 67 65 73 20 69 6e 20 74 68 65 20 64 65 73 74 ages in the dest
18546 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a ination. **
18547 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 database. The c
18548 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 omplication here
18549 20 69 73 20 74 68 61 74 20 74 68 65 20 64 65 73 is that the des
1854a 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 tination page.
1854b 20 20 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 ** size may
1854c 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 be different to
1854d 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 the source page
1854e 73 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a size. . **.
1854f 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
18550 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 source page size
18551 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
18552 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
18553 20 70 61 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 page size, .
18554 20 20 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 ** round up.
18555 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
18556 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
18557 4f 73 54 72 75 6e 63 61 74 65 28 29 20 62 65 6c OsTruncate() bel
18558 6f 77 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a ow will. **
18559 20 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66 fix the size of
1855a 20 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 the file. Howev
1855b 65 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 er it is importa
1855c 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 nt to call.
1855d 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 ** sqlite3Pager
1855e 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 TruncateImage()
1855f 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e 79 here so that any
18560 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a 20 pages in the .
18561 20 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 ** destinat
18562 69 6f 6e 20 66 69 6c 65 20 74 68 61 74 20 6c 69 ion file that li
18563 65 20 62 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 e beyond the nDe
18564 73 74 54 72 75 6e 63 61 74 65 20 70 61 67 65 20 stTruncate page
18565 6d 61 72 6b 20 61 72 65 0a 20 20 20 20 20 20 2a mark are. *
18566 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 * journalled by
18567 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
18568 4f 6e 65 28 29 20 62 65 66 6f 72 65 20 74 68 65 One() before the
18569 79 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a y are destroyed.
1856a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 ** by the
1856b 66 69 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e file truncation.
1856c 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1856d 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 if( nSrcPagesize
1856e 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 <nDestPagesize )
1856f 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 61 {. int ra
18570 74 69 6f 20 3d 20 6e 44 65 73 74 50 61 67 65 73 tio = nDestPages
18571 69 7a 65 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 ize/nSrcPagesize
18572 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 ;. nDestT
18573 72 75 6e 63 61 74 65 20 3d 20 28 6e 53 72 63 50 runcate = (nSrcP
18574 61 67 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 age+ratio-1)/rat
18575 69 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 io;. if(
18576 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 nDestTruncate==(
18577 69 6e 74 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 int)PENDING_BYTE
18578 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e _PAGE(p->pDest->
18579 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt) ){.
1857a 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2d nDestTruncate-
1857b 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 -;. }.
1857c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1857d 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 nDestTruncate
1857e 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 6e 53 = nSrcPage * (nS
1857f 72 63 50 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 rcPagesize/nDest
18580 50 61 67 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 Pagesize);.
18581 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
18582 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
18583 67 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e ge(pDestPager, n
18584 44 65 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a DestTruncate);..
18585 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 if( nSrcPa
18586 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 gesize<nDestPage
18587 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 size ){.
18588 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 /* If the source
18589 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6d page-size is sm
1858a 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 aller than the d
1858b 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d estination page-
1858c 73 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a size,. **
1858d 20 74 77 6f 20 65 78 74 72 61 20 74 68 69 6e 67 two extra thing
1858e 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 68 61 s may need to ha
1858f 70 70 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a ppen:. **
18590 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 . ** *
18591 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 The destination
18592 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74 may need to be t
18593 72 75 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 runcated, and.
18594 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
18595 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 74 6f ** * Data sto
18596 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 73 red on the pages
18597 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c immediately fol
18598 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20 20 20 lowing the .
18599 20 20 20 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 ** pendi
1859a 6e 67 2d 62 79 74 65 20 70 61 67 65 20 69 6e 20 ng-byte page in
1859b 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
1859c 61 73 65 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 ase may need to
1859d 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 be. **
1859e 20 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 copied into th
1859f 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
185a0 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 tabase..
185a1 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 */. const
185a2 20 69 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36 i64 iSize = (i6
185a3 34 29 6e 53 72 63 50 61 67 65 73 69 7a 65 20 2a 4)nSrcPagesize *
185a4 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 3b 0a (i64)nSrcPage;.
185a5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
185a6 66 69 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 file * const pFi
185a7 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 le = sqlite3Page
185a8 72 46 69 6c 65 28 70 44 65 73 74 50 61 67 65 72 rFile(pDestPager
185a9 29 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 );.. asse
185aa 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 rt( pFile );.
185ab 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69 36 assert( (i6
185ac 34 29 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2a 4)nDestTruncate*
185ad 28 69 36 34 29 6e 44 65 73 74 50 61 67 65 73 69 (i64)nDestPagesi
185ae 7a 65 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 ze >= iSize || (
185af 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e . n
185b0 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 DestTruncate==(i
185b1 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 nt)(PENDING_BYTE
185b2 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e _PAGE(p->pDest->
185b3 70 42 74 29 2d 31 29 0a 20 20 20 20 20 20 20 20 pBt)-1).
185b4 20 20 20 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e && iSize>=PEN
185b5 44 49 4e 47 5f 42 59 54 45 20 26 26 20 69 53 69 DING_BYTE && iSi
185b6 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ze<=PENDING_BYTE
185b7 2b 6e 44 65 73 74 50 61 67 65 73 69 7a 65 0a 20 +nDestPagesize.
185b8 20 20 20 20 20 20 20 29 29 3b 0a 20 20 20 20 20 ));.
185b9 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
185ba 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 ==(rc = sqlite3P
185bb 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f agerCommitPhaseO
185bc 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 ne(pDestPager, 0
185bd 2c 20 31 29 29 0a 20 20 20 20 20 20 20 20 20 26 , 1)). &
185be 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 & SQLITE_OK==(rc
185bf 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 = backupTruncat
185c0 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 eFile(pFile, iSi
185c1 7a 65 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 ze)). &&
185c2 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 SQLITE_OK==(rc
185c3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 = sqlite3PagerSy
185c4 6e 63 28 70 44 65 73 74 50 61 67 65 72 29 29 0a nc(pDestPager)).
185c5 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
185c6 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 i64 iOff;.
185c7 20 20 20 20 20 20 20 20 20 69 36 34 20 69 45 6e i64 iEn
185c8 64 20 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f d = MIN(PENDING_
185c9 42 59 54 45 20 2b 20 6e 44 65 73 74 50 61 67 65 BYTE + nDestPage
185ca 73 69 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 size, iSize);.
185cb 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 for(.
185cc 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45 iOff=PE
185cd 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 NDING_BYTE+nSrcP
185ce 61 67 65 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 agesize; .
185cf 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
185d0 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 _OK && iOff<iEnd
185d1 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ; . i
185d2 4f 66 66 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a Off+=nSrcPagesiz
185d3 65 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 e. ){.
185d4 20 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 PgHdr
185d5 20 2a 70 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 *pSrcPg = 0;.
185d6 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 const
185d7 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 50 Pgno iSrcPg = (P
185d8 67 6e 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 gno)((iOff/nSrcP
185d9 61 67 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 agesize)+1);.
185da 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
185db 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53 lite3PagerGet(pS
185dc 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c rcPager, iSrcPg,
185dd 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 &pSrcPg);.
185de 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
185df 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
185e0 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a 44 u8 *zD
185e1 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ata = sqlite3Pag
185e2 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67 erGetData(pSrcPg
185e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
185e4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
185e5 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 rite(pFile, zDat
185e6 61 2c 20 6e 53 72 63 50 61 67 65 73 69 7a 65 2c a, nSrcPagesize,
185e7 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20 iOff);.
185e8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
185e9 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
185ea 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20 ref(pSrcPg);.
185eb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
185ec 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
185ed 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
185ee 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
185ef 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 haseOne(pDestPag
185f0 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 er, 0, 0);.
185f1 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 }. . /* F
185f2 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 inish committing
185f3 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
185f4 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 to the destinat
185f5 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 2a 2f ion database. */
185f6 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
185f7 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 E_OK==rc.
185f8 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 && SQLITE_OK==(r
185f9 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
185fa 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
185fb 2d 3e 70 44 65 73 74 29 29 0a 20 20 20 20 20 20 ->pDest)).
185fc 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
185fd 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
185fe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
185ff 20 20 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 /* If bCloseTr
18600 61 6e 73 20 69 73 20 74 72 75 65 2c 20 74 68 65 ans is true, the
18601 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
18602 6f 70 65 6e 65 64 20 61 20 72 65 61 64 20 74 72 opened a read tr
18603 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a ansaction. **
18604 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 on the source d
18605 61 74 61 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 atabase. Close t
18606 68 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 he read transact
18607 69 6f 6e 20 68 65 72 65 2e 20 54 68 65 72 65 20 ion here. There
18608 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 is. ** no nee
18609 64 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 d to check the r
1860a 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 eturn values of
1860b 74 68 65 20 62 74 72 65 65 20 6d 65 74 68 6f 64 the btree method
1860c 73 20 68 65 72 65 2c 20 61 73 0a 20 20 20 20 2a s here, as. *
1860d 2a 20 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 * "committing" a
1860e 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 read-only trans
1860f 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 action cannot fa
18610 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 il.. */. i
18611 66 28 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 f( bCloseTrans )
18612 7b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 {. TESTONLY
18613 28 20 69 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 ( int rc2 );.
18614 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 TESTONLY( rc2
18615 20 20 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 = ) sqlite3Btr
18616 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
18617 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 (p->pSrc, 0);.
18618 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 TESTONLY( rc
18619 32 20 7c 3d 20 29 20 73 71 6c 69 74 65 33 42 74 2 |= ) sqlite3Bt
1861a 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 reeCommitPhaseTw
1861b 6f 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 o(p->pSrc);.
1861c 20 20 61 73 73 65 72 74 28 20 72 63 32 3d 3d 53 assert( rc2==S
1861d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
1861e 7d 0a 20 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d }. . p->rc =
1861f 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 rc;. }. if( p
18620 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
18621 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18622 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d eave(p->pDestDb-
18623 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 >mutex);. }. s
18624 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
18625 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c (p->pSrc);. sql
18626 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
18627 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 (p->pSrcDb->mute
18628 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b x);. return rc;
18629 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 .}../*.** Releas
1862a 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 e all resources
1862b 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1862c 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 an sqlite3_backu
1862d 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 p* handle..*/.SQ
1862e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1862f 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
18630 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 sh(sqlite3_backu
18631 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 p *p){. sqlite3
18632 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 _backup **pp;
18633 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18634 20 50 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20 Ptr to head of
18635 70 61 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69 pagers backup li
18636 73 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f st */. sqlite3_
18637 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
18638 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18639 4d 75 74 65 78 20 74 6f 20 70 72 6f 74 65 63 74 Mutex to protect
1863a 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
1863b 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
1863c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1863d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
1863e 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f lue to return */
1863f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 .. /* Enter the
18640 20 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 73 71 mutexes */. sq
18641 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
18642 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 r(p->pSrcDb->mut
18643 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ex);. sqlite3Bt
18644 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 reeEnter(p->pSrc
18645 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e );. mutex = p->
18646 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 pSrcDb->mutex;.
18647 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
18648 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
18649 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 utex_enter(p->pD
1864a 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 estDb->mutex);.
1864b 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 }.. /* Detach
1864c 74 68 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d this backup from
1864d 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
1864e 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 r. */. if( p->p
1864f 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d DestDb ){. p-
18650 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d >pSrc->nBackup--
18651 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 ;. }. if( p->i
18652 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20 sAttached ){.
18653 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 pp = sqlite3Pag
18654 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 erBackupPtr(sqli
18655 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
18656 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69 >pSrc));. whi
18657 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 le( *pp!=p ){.
18658 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d pp = &(*pp)-
18659 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
1865a 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 *pp = p->pNext
1865b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 ;. }.. /* If a
1865c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1865d 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 still open on th
1865e 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 e Btree, roll it
1865f 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 back. */. sqli
18660 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
18661 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f (p->pDest);.. /
18662 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 * Set the error
18663 63 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74 code of the dest
18664 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
18665 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 handle. */. rc
18666 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 = (p->rc==SQLIT
18667 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 E_DONE) ? SQLITE
18668 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 _OK : p->rc;. s
18669 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 qlite3Error(p->p
1866a 44 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a DestDb, rc, 0);.
1866b 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d . /* Exit the m
1866c 75 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20 utexes and free
1866d 74 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 the backup conte
1866e 78 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f xt structure. */
1866f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 . if( p->pDestD
18670 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
18671 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
18672 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b pDestDb->mutex);
18673 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
18674 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 reeLeave(p->pSrc
18675 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 );. if( p->pDes
18676 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tDb ){. sqlit
18677 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a e3_free(p);. }.
18678 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
18679 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 leave(mutex);.
1867a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1867b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1867c 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
1867d 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 till to be backe
1867e 64 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d d up as of the m
1867f 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 ost recent.** ca
18680 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
18681 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a ckup_step()..*/.
18682 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
18683 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 qlite3_backup_re
18684 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f maining(sqlite3_
18685 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 backup *p){. re
18686 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 turn p->nRemaini
18687 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ng;.}../*.** Ret
18688 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
18689 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1868a 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1868b 62 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d base as of the m
1868c 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 ost .** recent c
1868d 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
1868e 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f ackup_step()..*/
1868f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
18690 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 sqlite3_backup_p
18691 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 agecount(sqlite3
18692 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 _backup *p){. r
18693 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f eturn p->nPageco
18694 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 unt;.}../*.** Th
18695 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
18696 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 alled after the
18697 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 contents of page
18698 20 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a iPage of the.**
18699 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
1869a 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
1869b 69 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61 ied. If page iPa
1869c 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ge has already b
1869d 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 een .** copied i
1869e 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 nto the destinat
1869f 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68 ion database, th
186a0 65 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74 en the data writ
186a1 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 ten to the.** de
186a2 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 stination is now
186a3 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68 invalidated. Th
186a4 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f e destination co
186a5 70 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64 py of iPage need
186a6 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74 s.** to be updat
186a7 65 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 ed with the new
186a8 64 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20 data before the
186a9 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
186aa 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e is.** complete.
186ab 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 .**.** It is ass
186ac 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 umed that the mu
186ad 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 tex associated w
186ae 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 ith the BtShared
186af 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 object.** corre
186b0 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
186b1 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
186b2 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 is held when thi
186b3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a s function is.**
186b4 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 called..*/.SQLI
186b5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
186b6 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
186b7 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ate(sqlite3_back
186b8 75 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e up *pBackup, Pgn
186b9 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 o iPage, const u
186ba 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 8 *aData){. sql
186bb 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 ite3_backup *p;
186bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186bd 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
186be 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 riable */. for(
186bf 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d p=pBackup; p; p=
186c0 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
186c1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
186c2 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 utex_held(p->pSr
186c3 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 c->pBt->mutex) )
186c4 3b 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74 ;. if( !isFat
186c5 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 alError(p->rc) &
186c6 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 & iPage<p->iNext
186c7 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 ){. /* The
186c8 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 backup process
186c9 70 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f p has already co
186ca 70 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e pied page iPage.
186cb 20 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 But now it.
186cc 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
186cd 64 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e dified by a tran
186ce 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 saction on the s
186cf 6f 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70 ource pager. Cop
186d0 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e y. ** the n
186d1 65 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 ew data into the
186d2 20 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a backup.. *
186d3 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d /. int rc =
186d4 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 backupOnePage(p
186d5 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b , iPage, aData);
186d6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
186d7 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c!=SQLITE_BUSY &
186d8 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 & rc!=SQLITE_LOC
186d9 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 KED );. if(
186da 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
186db 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 {. p->rc
186dc 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = rc;. }.
186dd 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
186de 20 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 Restart the bac
186df 6b 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 kup process. Thi
186e0 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e s is called when
186e1 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
186e2 0a 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 .** detects that
186e3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
186e4 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
186e5 62 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 by an external d
186e6 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
186e7 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 ction. In this c
186e8 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ase there is no
186e9 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 way of knowing w
186ea 68 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 hich of the.** p
186eb 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 ages that have b
186ec 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 een copied into
186ed 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
186ee 64 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 database are sti
186ef 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 ll .** valid and
186f0 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 which are not,
186f1 73 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 so the entire pr
186f2 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 ocess needs to b
186f3 65 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a e restarted..**.
186f4 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
186f5 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 that the mutex
186f6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
186f7 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
186f8 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e ect.** correspon
186f9 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 ding to the sour
186fa 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 ce database is h
186fb 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
186fc 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c nction is.** cal
186fd 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
186fe 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
186ff 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
18700 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
18701 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c *pBackup){. sql
18702 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 ite3_backup *p;
18703 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18704 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
18705 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 riable */. for(
18706 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d p=pBackup; p; p=
18707 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
18708 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
18709 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 utex_held(p->pSr
1870a 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 c->pBt->mutex) )
1870b 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d ;. p->iNext =
1870c 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 1;. }.}..#ifnd
1870d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1870e 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 ACUUM./*.** Copy
1870f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f the complete co
18710 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d ntent of pBtFrom
18711 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 into pBtTo. A
18712 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d transaction.** m
18713 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f ust be active fo
18714 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a r both files..**
18715 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
18716 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 file pTo may be
18717 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 reduced by this
18718 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e operation. If an
18719 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 ything .** goes
1871a 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 wrong, the trans
1871b 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 action on pTo is
1871c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 rolled back. If
1871d 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 successful, the
1871e 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
1871f 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 is committed be
18720 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
18721 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18722 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18723 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 eeCopyFile(Btree
18724 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 *pTo, Btree *pF
18725 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a rom){. int rc;.
18726 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
18727 20 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 b;. sqlite3Btr
18728 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 eeEnter(pTo);.
18729 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1872a 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 r(pFrom);.. /*
1872b 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65 Set up an sqlite
1872c 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 3_backup object.
1872d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e sqlite3_backup.
1872e 70 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20 pDestDb must be
1872f 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 set. ** to 0. T
18730 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
18731 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
18732 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 ns of sqlite3_ba
18733 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a ckup_step(). **
18734 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 and sqlite3_bac
18735 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 kup_finish() to
18736 64 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79 detect that they
18737 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 are being calle
18738 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 d. ** from this
18739 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 function, not d
1873a 69 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75 irectly by the u
1873b 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 ser.. */. mems
1873c 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 et(&b, 0, sizeof
1873d 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 (b));. b.pSrcDb
1873e 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 = pFrom->db;.
1873f 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a b.pSrc = pFrom;.
18740 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b b.pDest = pTo;
18741 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a . b.iNext = 1;.
18742 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46 . /* 0x7FFFFFFF
18743 20 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d is the hard lim
18744 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 it for the numbe
18745 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20 r of pages in a
18746 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 database. ** fi
18747 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74 le. By passing t
18748 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65 his as the numbe
18749 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f r of pages to co
1874a 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 py to. ** sqlit
1874b 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
1874c 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74 , we can guarant
1874d 65 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79 ee that the copy
1874e 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 finishes . **
1874f 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 within a single
18750 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 call (unless an
18751 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 error occurs). T
18752 68 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 he assert() stat
18753 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b ement. ** check
18754 73 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f s this assumptio
18755 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 n - (p->rc) shou
18756 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74 ld be set to eit
18757 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 her SQLITE_DONE
18758 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f . ** or an erro
18759 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 r code.. */. s
1875a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
1875b 65 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46 ep(&b, 0x7FFFFFF
1875c 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e F);. assert( b.
1875d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
1875e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f . rc = sqlite3_
1875f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 backup_finish(&b
18760 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
18761 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 ITE_OK ){. pT
18762 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 o->pBt->pageSize
18763 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a Fixed = 0;. }..
18764 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18765 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 ave(pFrom);. sq
18766 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18767 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pTo);. return r
18768 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 c;.}.#endif /* S
18769 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
1876a 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a M */../*********
1876b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 ***** End of bac
1876c 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a kup.c **********
1876d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1876e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1876f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
18770 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
18771 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a vdbemem.c *****
18772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18773 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18774 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
18775 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 May 26.**.** Th
18776 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
18777 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
18778 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
18779 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1877a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1877b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1877c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1877d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1877e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1877f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
18780 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
18781 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
18782 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
18783 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
18784 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
18785 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
18786 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
18787 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18788 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18789 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1878a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1878b 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1878c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1878d 63 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 code use to mani
1878e 70 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72 pulate "Mem" str
1878f 75 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 ucture. A "Mem"
18790 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e .** stores a sin
18791 67 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 gle value in the
18792 20 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 VDBE. Mem is a
18793 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 n opaque structu
18794 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e re visible.** on
18795 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44 ly within the VD
18796 42 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72 BE. Interface r
18797 6f 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f outines refer to
18798 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 a Mem using the
18799 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f .** name sqlite_
1879a 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a value.**.** $Id:
1879b 20 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 vdbemem.c,v 1.1
1879c 34 34 20 32 30 30 39 2f 30 35 2f 30 35 20 31 32 44 2009/05/05 12
1879d 3a 35 34 3a 35 30 20 64 72 68 20 45 78 70 20 24 :54:50 drh Exp $
1879e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 .*/../*.** Call
1879f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
187a0 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 pandBlob() on th
187a1 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 e supplied value
187a2 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 (type Mem*).**
187a3 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a P if required..*
187a4 2f 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64 /.#define expand
187a5 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66 Blob(P) (((P)->f
187a6 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 lags&MEM_Zero)?s
187a7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
187a8 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f andBlob(P):0)../
187a9 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 *.** If pMem is
187aa 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 an object with a
187ab 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65 valid string re
187ac 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 presentation, th
187ad 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e is routine.** en
187ae 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e sures the intern
187af 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 al encoding for
187b0 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 the string repre
187b1 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 sentation is.**
187b2 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 'desiredEnc', on
187b3 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38 e of SQLITE_UTF8
187b4 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
187b5 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 or SQLITE_UTF16
187b6 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 BE..**.** If pMe
187b7 6d 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e m is not a strin
187b8 67 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 g object, or the
187b9 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
187ba 20 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 string.** repre
187bb 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 sentation is alr
187bc 65 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e eady stored usin
187bd 67 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 g the requested
187be 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 encoding, then t
187bf 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 his.** routine i
187c0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
187c1 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
187c2 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f turned if the co
187c3 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63 nversion is succ
187c4 65 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 essful (or not r
187c5 65 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c equired)..** SQL
187c6 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 ITE_NOMEM may be
187c7 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d returned if a m
187c8 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75 alloc() fails du
187c9 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a ring conversion.
187ca 2a 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 ** between forma
187cb 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts..*/.SQLITE_PR
187cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
187cd 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
187ce 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 ing(Mem *pMem, i
187cf 6e 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a nt desiredEnc){.
187d0 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
187d1 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
187d2 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
187d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 );. assert( des
187d4 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
187d5 55 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45 UTF8 || desiredE
187d6 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
187d7 4c 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c LE. ||
187d8 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
187d9 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 ITE_UTF16BE );.
187da 20 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 if( !(pMem->fla
187db 67 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 gs&MEM_Str) || p
187dc 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 Mem->enc==desire
187dd 64 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 dEnc ){. retu
187de 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
187df 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d }. assert( pMem
187e0 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
187e1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
187e2 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
187e3 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
187e4 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74 OMIT_UTF16. ret
187e5 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
187e6 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 ;.#else.. /* Me
187e7 6d 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 mTranslate() may
187e8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
187e9 4b 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 K or SQLITE_NOME
187ea 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 M. If NOMEM is r
187eb 65 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 eturned,. ** th
187ec 65 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 en the encoding
187ed 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79 of the value may
187ee 20 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65 not have change
187ef 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 d.. */. rc = s
187f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 qlite3VdbeMemTra
187f1 6e 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 nslate(pMem, (u8
187f2 29 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 )desiredEnc);.
187f3 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
187f4 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 E_OK || rc==S
187f5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 QLITE_NOMEM);.
187f6 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
187f7 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d E_OK || pMem-
187f8 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 >enc!=desiredEnc
187f9 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d );. assert(rc==
187fa 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
187fb 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 pMem->enc==desir
187fc 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e edEnc);. return
187fd 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f rc;.#endif.}../
187fe 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 *.** Make sure p
187ff 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem->z points to
18800 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f a writable allo
18801 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 cation of at lea
18802 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a st .** n bytes..
18803 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d **.** If the mem
18804 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 ory cell current
18805 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 ly contains stri
18806 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a ng or blob data.
18807 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 ** and the third
18808 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 argument passed
18809 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
1880a 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a n is true, the .
1880b 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 ** current conte
1880c 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 nt of the cell i
1880d 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68 s preserved. Oth
1880e 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a erwise, it may.*
1880f 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 * be discarded.
18810 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e .**.** This fun
18811 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d ction sets the M
18812 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 EM_Dyn flag and
18813 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20 clears any xDel
18814 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 callback..** It
18815 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f also clears MEM_
18816 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 Ephem and MEM_St
18817 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 atic. If the pre
18818 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a serve flag is .*
18819 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e * not set, Mem.n
1881a 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 is zeroed..*/.S
1881b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1881c 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
1881d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Grow(Mem *pMem,
1881e 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 int n, int prese
1881f 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 rve){. assert(
18820 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 1 >=. ((pMem-
18821 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d >zMalloc && pMem
18822 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d ->zMalloc==pMem-
18823 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 >z) ? 1 : 0) +.
18824 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 (((pMem->flag
18825 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d s&MEM_Dyn)&&pMem
18826 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 ->xDel) ? 1 : 0)
18827 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e + . ((pMem->
18828 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 flags&MEM_Ephem)
18829 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 ? 1 : 0) + .
1882a 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ((pMem->flags&M
1882b 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a EM_Static) ? 1 :
1882c 20 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 0). );. asser
1882d 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
1882e 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
1882f 3b 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20 ;.. if( n<32 )
18830 6e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71 n = 32;. if( sq
18831 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
18832 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
18833 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a ->zMalloc)<n ){.
18834 20 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65 if( preserve
18835 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 && pMem->z==pMe
18836 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 m->zMalloc ){.
18837 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d pMem->z = pM
18838 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 em->zMalloc = sq
18839 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
1883a 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 Free(pMem->db, p
1883b 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 Mem->z, n);.
1883c 20 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a preserve = 0;.
1883d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1883e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1883f 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
18840 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 Malloc);. p
18841 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 Mem->zMalloc = s
18842 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
18843 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a w(pMem->db, n);.
18844 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
18845 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 preserve && pMe
18846 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d m->z && pMem->zM
18847 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a alloc && pMem->z
18848 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 !=pMem->zMalloc
18849 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d ){. memcpy(pM
1884a 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 em->zMalloc, pMe
1884b 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a m->z, pMem->n);.
1884c 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e }. if( pMem->
1884d 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 flags&MEM_Dyn &&
1884e 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 pMem->xDel ){.
1884f 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 pMem->xDel((v
18850 6f 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 oid *)(pMem->z))
18851 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a ;. }.. pMem->z
18852 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 = pMem->zMalloc
18853 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d ;. if( pMem->z=
18854 3d 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e =0 ){. pMem->
18855 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
18856 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
18857 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
18858 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 MEM_Ephem|MEM_St
18859 61 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 atic);. }. pMe
1885a 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 m->xDel = 0;. r
1885b 65 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f eturn (pMem->z ?
1885c 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
1885d 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f ITE_NOMEM);.}../
1885e 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 *.** Make the gi
1885f 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d ven Mem object M
18860 45 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 EM_Dyn. In othe
18861 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 r words, make it
18862 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 so.** that any
18863 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e TEXT or BLOB con
18864 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 tent is stored i
18865 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
18866 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 d from.** malloc
18867 28 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 (). In this way
18868 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 , we know that t
18869 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 he memory is saf
1886a 65 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 e to be.** overw
1886b 72 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 ritten or altere
1886c 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 d..**.** Return
1886d 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
1886e 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e cess or SQLITE_N
1886f 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 OMEM if malloc f
18870 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ails..*/.SQLITE_
18871 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18872 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
18873 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 iteable(Mem *pMe
18874 6d 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 m){. int f;. a
18875 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
18876 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
18877 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
18878 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
18879 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
1887a 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
1887b 3d 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c =0 );. expandBl
1887c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 ob(pMem);. f =
1887d 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
1887e 66 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d f( (f&(MEM_Str|M
1887f 45 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 EM_Blob)) && pMe
18880 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c m->z!=pMem->zMal
18881 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 loc ){. if( s
18882 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
18883 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 w(pMem, pMem->n
18884 2b 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 + 2, 1) ){.
18885 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
18886 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
18887 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d pMem->z[pMem->n]
18888 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e = 0;. pMem->
18889 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 z[pMem->n+1] = 0
1888a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
1888b 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
1888c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }.. return SQL
1888d 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1888e 20 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 If the given Me
1888f 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 m* has a zero-fi
18890 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 lled tail, turn
18891 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e it into an ordin
18892 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 ary.** blob stor
18893 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c ed in dynamicall
18894 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 y allocated spac
18895 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
18896 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
18897 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 OB.SQLITE_PRIVAT
18898 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
18899 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d eMemExpandBlob(M
1889a 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 em *pMem){. if(
1889b 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
1889c 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 EM_Zero ){. i
1889d 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 nt nByte;. as
1889e 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 sert( pMem->flag
1889f 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 s&MEM_Blob );.
188a0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
188a1 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 >flags&MEM_RowSe
188a2 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 t)==0 );. ass
188a3 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
188a4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
188a5 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
188a6 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 >mutex) );..
188a7 2f 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20 /* Set nByte to
188a8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
188a9 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 tes required to
188aa 73 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64 store the expand
188ab 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 ed blob. */.
188ac 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 nByte = pMem->n
188ad 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b + pMem->u.nZero;
188ae 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d . if( nByte<=
188af 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 0 ){. nByte
188b0 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
188b1 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
188b2 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
188b3 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 te, 1) ){.
188b4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
188b5 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 MEM;. }..
188b6 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b memset(&pMem->z[
188b7 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 pMem->n], 0, pMe
188b8 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 m->u.nZero);.
188b9 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d pMem->n += pMem
188ba 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 ->u.nZero;. p
188bb 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
188bc 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 MEM_Zero|MEM_Ter
188bd 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e m);. }. return
188be 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
188bf 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b ndif.../*.** Mak
188c0 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e e sure the given
188c1 20 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74 Mem is \u0000 t
188c2 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 erminated..*/.SQ
188c3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
188c4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
188c5 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 ulTerminate(Mem
188c6 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 *pMem){. assert
188c7 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
188c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
188c9 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
188ca 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70 tex) );. if( (p
188cb 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
188cc 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d _Term)!=0 || (pM
188cd 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
188ce 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Str)==0 ){. r
188cf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
188d0 20 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f /* Nothing to
188d1 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 do */. }. if(
188d2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
188d3 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e row(pMem, pMem->
188d4 6e 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 n+2, 1) ){. r
188d5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
188d6 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e EM;. }. pMem->
188d7 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a z[pMem->n] = 0;.
188d8 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
188d9 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d n+1] = 0;. pMem
188da 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 ->flags |= MEM_T
188db 65 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 erm;. return SQ
188dc 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
188dd 2a 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f * Add MEM_Str to
188de 20 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72 the set of repr
188df 65 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 esentations for
188e0 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 the given Mem.
188e1 4e 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 Numbers.** are c
188e2 6f 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73 onverted using s
188e3 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
188e4 29 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 ). Converting a
188e5 20 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e BLOB to a strin
188e6 67 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e g.** is a no-op.
188e7 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 .**.** Existing
188e8 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
188e9 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f MEM_Int and MEM_
188ea 52 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 Real are *not* i
188eb 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a nvalidated..**.*
188ec 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c * A MEM_Null val
188ed 75 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 ue will never be
188ee 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 passed to this
188ef 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 function. This f
188f0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 unction is.** us
188f1 65 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e ed for convertin
188f2 67 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 g values to text
188f3 20 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 for returning t
188f4 6f 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e o the user (i.e.
188f5 20 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f via.** sqlite3_
188f6 76 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f value_text()), o
188f7 72 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 r for ensuring t
188f8 68 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65 hat values to be
188f9 20 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a used as btree.*
188fa 2a 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e * keys are strin
188fb 67 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 gs. In the forme
188fc 72 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f r case a NULL po
188fd 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
188fe 64 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e d the.** user an
188ff 64 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61 d the later is a
18900 6e 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 n internal progr
18901 61 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f amming error..*/
18902 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18903 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
18904 65 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 emStringify(Mem
18905 2a 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b *pMem, int enc){
18906 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
18907 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 TE_OK;. int fg
18908 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 = pMem->flags;.
18909 20 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 const int nByte
1890a 20 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 = 32;.. assert
1890b 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
1890c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1890d 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
1890e 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
1890f 28 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 ( !(fg&MEM_Zero)
18910 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 );. assert( !(
18911 66 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f fg&(MEM_Str|MEM_
18912 42 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65 Blob)) );. asse
18913 72 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c rt( fg&(MEM_Int|
18914 4d 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 MEM_Real) );. a
18915 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
18916 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
18917 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
18918 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
18919 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a MENT(pMem) );...
1891a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1891b 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
1891c 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
1891d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1891e 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 MEM;. }.. /* F
1891f 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 or a Real or Int
18920 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65 eger, use sqlite
18921 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70 3_mprintf() to p
18922 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38 roduce the UTF-8
18923 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 . ** string rep
18924 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
18925 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 he value. Then,
18926 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 if the required
18927 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 encoding. ** is
18928 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 UTF-16le or UTF
18929 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73 -16be do a trans
1892a 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 lation.. ** .
1892b 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f ** FIX ME: It wo
1892c 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66 uld be better if
1892d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1892e 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 f() could do UTF
1892f 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 -16.. */. if(
18930 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a fg & MEM_Int ){.
18931 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
18932 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d intf(nByte, pMem
18933 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 ->z, "%lld", pMe
18934 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 m->u.i);. }else
18935 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67 {. assert( fg
18936 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 & MEM_Real );.
18937 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
18938 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d ntf(nByte, pMem-
18939 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d >z, "%!.15g", pM
1893a 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d em->r);. }. pM
1893b 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 em->n = sqlite3S
1893c 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 trlen30(pMem->z)
1893d 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
1893e 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 SQLITE_UTF8;. p
1893f 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
18940 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
18941 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
18942 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
18943 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e , enc);. return
18944 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 rc;.}../*.** Me
18945 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 mory cell pMem c
18946 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 ontains the cont
18947 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67 ext of an aggreg
18948 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ate function..**
18949 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
1894a 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 lls the finalize
1894b 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 method for that
1894c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a function. The.
1894d 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 ** result of the
1894e 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74 aggregate is st
1894f 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 ored back into p
18950 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 Mem..**.** Retur
18951 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 n SQLITE_ERROR i
18952 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 f the finalizer
18953 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 reports an error
18954 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 . SQLITE_OK.**
18955 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 otherwise..*/.SQ
18956 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18957 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
18958 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 inalize(Mem *pMe
18959 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e m, FuncDef *pFun
1895a 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 c){. int rc = S
1895b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
1895c 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e pFunc && pFunc->
1895d 78 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 xFinalize ){.
1895e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1895f 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 ctx;. assert
18960 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
18961 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c MEM_Null)!=0 ||
18962 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e pFunc==pMem->u.
18963 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65 pDef );. asse
18964 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
18965 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
18966 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
18967 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 mutex) );. me
18968 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 mset(&ctx, 0, si
18969 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20 zeof(ctx));.
1896a 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
1896b 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e M_Null;. ctx.
1896c 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b s.db = pMem->db;
1896d 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 . ctx.pMem =
1896e 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 pMem;. ctx.pF
1896f 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 unc = pFunc;.
18970 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a pFunc->xFinaliz
18971 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73 e(&ctx);. ass
18972 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 ert( 0==(pMem->f
18973 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 lags&MEM_Dyn) &&
18974 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a !pMem->xDel );.
18975 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
18976 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
18977 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 ->zMalloc);.
18978 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 memcpy(pMem, &ct
18979 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e x.s, sizeof(ctx.
1897a 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28 63 s));. rc = (c
1897b 74 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49 54 tx.isError?SQLIT
1897c 45 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f 4f E_ERROR:SQLITE_O
1897d 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e K);. }. return
1897e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
1897f 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
18980 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69 contains a stri
18981 6e 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75 ng value that mu
18982 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a st be freed by.*
18983 2a 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 * invoking an ex
18984 74 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c ternal callback,
18985 20 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 free it now. Ca
18986 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 lling this funct
18987 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 ion.** does not
18988 66 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 free any Mem.zMa
18989 6c 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a lloc buffer..*/.
1898a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1898b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
1898c 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1898d 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 l(Mem *p){. ass
1898e 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c ert( p->db==0 ||
1898f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18990 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
18991 29 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c ) );. if( p->fl
18992 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d ags&(MEM_Agg|MEM
18993 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 _Dyn|MEM_RowSet)
18994 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 ){. if( p->f
18995 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a lags&MEM_Agg ){.
18996 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
18997 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 eMemFinalize(p,
18998 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 p->u.pDef);.
18999 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c assert( (p->fl
1899a 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d ags & MEM_Agg)==
1899b 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
1899c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1899d 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 (p);. }else i
1899e 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f f( p->flags&MEM_
1899f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 Dyn && p->xDel )
189a0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
189a1 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f (p->flags&MEM_Ro
189a2 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 wSet)==0 );.
189a3 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 p->xDel((void
189a4 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 *)p->z);. p
189a5 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 ->xDel = 0;.
189a6 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 }else if( p->fla
189a7 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b gs&MEM_RowSet ){
189a8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f . sqlite3Ro
189a9 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 wSetClear(p->u.p
189aa 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 RowSet);. }.
189ab 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 }.}../*.** Rele
189ac 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 ase any memory h
189ad 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 eld by the Mem.
189ae 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65 20 74 This may leave t
189af 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 he Mem in an.**
189b0 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 inconsistent sta
189b1 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 te, for example
189b2 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 with (Mem.z==0)
189b3 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 and.** (Mem.type
189b4 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a ==SQLITE_TEXT)..
189b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
189b6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
189b7 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d beMemRelease(Mem
189b8 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 *p){. sqlite3V
189b9 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
189ba 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 ernal(p);. sqli
189bb 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
189bc 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 p->zMalloc);.
189bd 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a p->z = 0;. p->z
189be 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d Malloc = 0;. p-
189bf 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a >xDel = 0;.}../*
189c0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 36 34 .** Convert a 64
189c1 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62 6c 65 -bit IEEE double
189c2 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 into a 64-bit s
189c3 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a igned integer..*
189c4 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c 65 20 * If the double
189c5 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65 is too large, re
189c6 74 75 72 6e 20 30 78 38 30 30 30 30 30 30 30 30 turn 0x800000000
189c7 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 0000000..**.** M
189c8 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70 70 65 ost systems appe
189c9 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20 73 69 ar to do this si
189ca 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e 69 6e mply by assignin
189cb 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 g.** variables a
189cc 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 65 nd without the e
189cd 78 74 72 61 20 72 61 6e 67 65 20 74 65 73 74 73 xtra range tests
189ce 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 65 20 . But.** there
189cf 61 72 65 20 72 65 70 6f 72 74 73 20 74 68 61 74 are reports that
189d0 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 windows throws
189d1 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 an expection.**
189d2 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 if the floating
189d3 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 6f point value is o
189d4 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 ut of range. (Se
189d5 65 20 74 69 63 6b 65 74 20 23 32 38 38 30 2e 29 e ticket #2880.)
189d6 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65 20 64 .** Because we d
189d7 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 o not completely
189d8 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 understand the
189d9 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c problem, we will
189da 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 6f 6e .** take the con
189db 73 65 72 76 61 74 69 76 65 20 61 70 70 72 6f 61 servative approa
189dc 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20 64 6f ch and always do
189dd 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20 range tests.**
189de 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e before attemptin
189df 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e g the conversion
189e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 ..*/.static i64
189e1 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f doubleToInt64(do
189e2 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 uble r){. /*.
189e3 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 ** Many compiler
189e4 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 64 s we encounter d
189e5 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e o not define con
189e6 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 0a 20 stants for the.
189e7 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 ** minimum and
189e8 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 20 69 maximum 64-bit i
189e9 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68 65 79 ntegers, or they
189ea 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a define them. *
189eb 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79 * inconsistently
189ec 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e . And many do n
189ed 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 ot understand th
189ee 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e e "LL" notation.
189ef 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 66 69 . ** So we defi
189f0 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 74 69 ne our own stati
189f1 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 72 65 c constants here
189f2 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 using nothing.
189f3 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 ** larger than
189f4 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
189f5 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a constant.. */.
189f6 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 static const i
189f7 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 64 maxInt = LARG
189f8 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61 EST_INT64;. sta
189f9 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 69 tic const i64 mi
189fa 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f nInt = SMALLEST_
189fb 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c INT64;.. if( r<
189fc 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29 (double)minInt )
189fd 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e {. return min
189fe 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 Int;. }else if(
189ff 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e r>(double)maxIn
18a00 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 6e 49 t ){. /* minI
18a01 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20 68 65 nt is correct he
18a02 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e 74 2e re - not maxInt.
18a03 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74 20 74 It turns out t
18a04 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a 20 20 hat assigning.
18a05 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 72 67 ** a very larg
18a06 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 e positive numbe
18a07 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 r to an integer
18a08 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76 65 72 results in a ver
18a09 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a 20 6e y large. ** n
18a0a 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 2e egative integer.
18a0b 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e 6f 20 This makes no
18a0c 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20 69 73 sense, but it is
18a0d 20 77 68 61 74 20 78 38 36 20 68 61 72 64 77 61 what x86 hardwa
18a0e 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 73 re. ** does s
18a0f 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c o for compatibil
18a10 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f 20 74 ity we will do t
18a11 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 74 77 he same in softw
18a12 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 are. */. retu
18a13 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c rn minInt;. }el
18a14 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 se{. return (
18a15 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a i64)r;. }.}../*
18a16 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20 .** Return some
18a17 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20 kind of integer
18a18 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74 value which is t
18a19 68 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64 he best we can d
18a1a 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e o.** at represen
18a1b 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74 ting the value t
18a1c 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69 hat *pMem descri
18a1d 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65 bes as an intege
18a1e 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 r..** If pMem is
18a1f 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 an integer, the
18a20 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65 n the value is e
18a21 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 xact. If pMem i
18a22 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d s.** a floating-
18a23 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76 point then the v
18a24 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
18a25 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 the integer par
18a26 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 t..** If pMem is
18a27 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
18a28 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20 b, then we make
18a29 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f an attempt to co
18a2a 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f nvert.** it into
18a2b 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 a integer and r
18a2c 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20 eturn that. If
18a2d 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 pMem represents
18a2e 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e 55 4c an.** an SQL-NUL
18a2f 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72 6e 20 L value, return
18a30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 0..**.** If pMem
18a31 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 74 represents a st
18a32 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 73 20 ring value, its
18a33 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62 encoding might b
18a34 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 e changed..*/.SQ
18a35 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
18a36 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
18a37 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b alue(Mem *pMem){
18a38 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 . int flags;.
18a39 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
18a3a 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
18a3b 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
18a3c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
18a3d 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
18a3e 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 TE_ALIGNMENT(pMe
18a3f 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 m) );. flags =
18a40 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
18a41 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 f( flags & MEM_I
18a42 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e nt ){. return
18a43 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 pMem->u.i;. }e
18a44 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
18a45 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
18a46 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 return doubleToI
18a47 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 nt64(pMem->r);.
18a48 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 }else if( flags
18a49 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f & (MEM_Str|MEM_
18a4a 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 Blob) ){. i64
18a4b 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d value;. pMem
18a4c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 ->flags |= MEM_S
18a4d 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 tr;. if( sqli
18a4e 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
18a4f 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 oding(pMem, SQLI
18a50 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 TE_UTF8).
18a51 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 || sqlite3VdbeMe
18a52 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d mNulTerminate(pM
18a53 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 em) ){. ret
18a54 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 0;. }.
18a55 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a assert( pMem->z
18a56 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 );. sqlite3A
18a57 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 toi64(pMem->z, &
18a58 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 value);. retu
18a59 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 rn value;. }els
18a5a 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b e{. return 0;
18a5b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
18a5c 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 turn the best re
18a5d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
18a5e 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e pMem that we can
18a5f 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 get into a.** d
18a60 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 ouble. If pMem
18a61 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 is already a dou
18a62 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 ble or an intege
18a63 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a r, return its.**
18a64 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 value. If it i
18a65 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c s a string or bl
18a66 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 ob, try to conve
18a67 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c rt it to a doubl
18a68 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 e..** If it is a
18a69 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e NULL, return 0.
18a6a 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
18a6b 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 VATE double sqli
18a6c 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
18a6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
18a6e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
18a6f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
18a70 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
18a71 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
18a72 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
18a73 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d E_ALIGNMENT(pMem
18a74 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d ) );. if( pMem-
18a75 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
18a76 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
18a77 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 pMem->r;. }else
18a78 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
18a79 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
18a7a 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 return (double
18a7b 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 )pMem->u.i;. }e
18a7c 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c lse if( pMem->fl
18a7d 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d ags & (MEM_Str|M
18a7e 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 EM_Blob) ){.
18a7f 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 /* (double)0 In
18a80 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f case of SQLITE_O
18a81 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
18a82 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 6f 75 NT... */. dou
18a83 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 62 6c ble val = (doubl
18a84 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 e)0;. pMem->f
18a85 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b lags |= MEM_Str;
18a86 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
18a87 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
18a88 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f ng(pMem, SQLITE_
18a89 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 UTF8). ||
18a8a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
18a8b 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 lTerminate(pMem)
18a8c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f ){. /* (do
18a8d 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f uble)0 In case o
18a8e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
18a8f 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 OATING_POINT...
18a90 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
18a91 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 7d (double)0;. }
18a92 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
18a93 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 m->z );. sqli
18a94 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c te3AtoF(pMem->z,
18a95 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75 &val);. retu
18a96 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b rn val;. }else{
18a97 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 . /* (double)
18a98 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 0 In case of SQL
18a99 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
18a9a 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 G_POINT... */.
18a9b 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 return (double
18a9c 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a )0;. }.}../*.**
18a9d 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74 75 The MEM structu
18a9e 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
18a9f 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20 74 MEM_Real. Try t
18aa0 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61 o also make it a
18aa1 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77 .** MEM_Int if w
18aa2 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 e can..*/.SQLITE
18aa3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18aa4 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
18aa5 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d Affinity(Mem *pM
18aa6 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
18aa7 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
18aa8 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 _Real );. asser
18aa9 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 t( (pMem->flags
18aaa 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
18aab 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
18aac 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
18aad 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18aae 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
18aaf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
18ab0 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
18ab1 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 70 NT(pMem) );.. p
18ab2 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c Mem->u.i = doubl
18ab3 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 eToInt64(pMem->r
18ab4 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 );. if( pMem->r
18ab5 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e ==(double)pMem->
18ab6 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d u.i ){. pMem-
18ab7 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e >flags |= MEM_In
18ab8 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t;. }.}../*.**
18ab9 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 Convert pMem to
18aba 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 type integer. I
18abb 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
18abc 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
18abd 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
18abe 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18abf 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
18ac0 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ify(Mem *pMem){.
18ac1 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
18ac2 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
18ac3 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
18ac4 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
18ac5 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
18ac6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
18ac7 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
18ac8 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
18ac9 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
18aca 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 );.. pMem->u.i
18acb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
18acc 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d Value(pMem);. M
18acd 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d emSetTypeFlag(pM
18ace 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 em, MEM_Int);.
18acf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18ad0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
18ad1 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 rt pMem so that
18ad2 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 it is of type ME
18ad3 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c M_Real..** Inval
18ad4 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
18ad5 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
18ad6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18ad7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
18ad8 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d beMemRealify(Mem
18ad9 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 *pMem){. asser
18ada 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
18adb 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
18adc 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
18add 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
18ade 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
18adf 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b IGNMENT(pMem) );
18ae0 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 .. pMem->r = sq
18ae1 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
18ae2 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 ue(pMem);. MemS
18ae3 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c etTypeFlag(pMem,
18ae4 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 MEM_Real);. re
18ae5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
18ae6 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
18ae7 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 pMem so that it
18ae8 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 has types MEM_R
18ae9 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f eal or MEM_Int o
18aea 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c r both..** Inval
18aeb 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
18aec 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
18aed 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18aee 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
18aef 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 beMemNumerify(Me
18af0 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 m *pMem){. doub
18af1 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 le r1, r2;. i64
18af2 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 i;. assert( (p
18af3 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
18af4 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d M_Int|MEM_Real|M
18af5 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a EM_Null))==0 );.
18af6 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
18af7 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c >flags & (MEM_Bl
18af8 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 ob|MEM_Str))!=0
18af9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
18afa 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
18afb 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
18afc 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
18afd 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 );. r1 = sqlite
18afe 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
18aff 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 Mem);. i = doub
18b00 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 leToInt64(r1);.
18b01 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b r2 = (double)i;
18b02 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b . if( r1==r2 ){
18b03 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
18b04 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d MemIntegerify(pM
18b05 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
18b06 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a pMem->r = r1;.
18b07 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
18b08 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 ag(pMem, MEM_Rea
18b09 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
18b0a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
18b0b 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 *.** Delete any
18b0c 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 previous value a
18b0d 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 nd set the value
18b0e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d stored in *pMem
18b0f 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
18b10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
18b11 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
18b12 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d etNull(Mem *pMem
18b13 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 ){. if( pMem->f
18b14 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
18b15 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
18b16 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d RowSetClear(pMem
18b17 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 ->u.pRowSet);.
18b18 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c }. MemSetTypeFl
18b19 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c ag(pMem, MEM_Nul
18b1a 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 l);. pMem->type
18b1b 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a = SQLITE_NULL;.
18b1c 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
18b1d 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c any previous val
18b1e 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 ue and set the v
18b1f 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f alue to be a BLO
18b20 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e B of length.** n
18b21 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 containing all
18b22 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 zeros..*/.SQLITE
18b23 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18b24 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
18b25 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 eroBlob(Mem *pMe
18b26 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c m, int n){. sql
18b27 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
18b28 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d se(pMem);. pMem
18b29 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
18b2a 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 ob|MEM_Zero;. p
18b2b 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
18b2c 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d TE_BLOB;. pMem-
18b2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c >n = 0;. if( n<
18b2e 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 0 ) n = 0;. pMe
18b2f 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a m->u.nZero = n;.
18b30 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 pMem->enc = SQ
18b31 4c 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a LITE_UTF8;.}../*
18b32 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
18b33 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
18b34 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
18b35 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 stored in *pMem
18b36 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 to val,.** manif
18b37 65 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 est type INTEGER
18b38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b39 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
18b3a 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
18b3b 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 Mem *pMem, i64 v
18b3c 61 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 al){. sqlite3Vd
18b3d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
18b3e 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 m);. pMem->u.i
18b3f 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 = val;. pMem->f
18b40 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
18b41 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
18b42 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d QLITE_INTEGER;.}
18b43 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
18b44 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
18b45 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 e and set the va
18b46 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 lue stored in *p
18b47 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d Mem to val,.** m
18b48 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 anifest type REA
18b49 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 L..*/.SQLITE_PRI
18b4a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18b4b 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 3VdbeMemSetDoubl
18b4c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 e(Mem *pMem, dou
18b4d 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 ble val){. if(
18b4e 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c sqlite3IsNaN(val
18b4f 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
18b50 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
18b51 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Mem);. }else{.
18b52 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
18b53 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
18b54 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 pMem->r = va
18b55 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 l;. pMem->fla
18b56 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 gs = MEM_Real;.
18b57 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
18b58 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 SQLITE_FLOAT;.
18b59 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 }.}../*.** Delet
18b5a 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
18b5b 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
18b5c 20 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74 value of pMem t
18b5d 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 o be an.** empty
18b5e 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a boolean index..
18b5f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18b60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
18b61 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d beMemSetRowSet(M
18b62 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c em *pMem){. sql
18b63 69 74 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d ite3 *db = pMem-
18b64 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 >db;. assert( d
18b65 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d b!=0 );. if( pM
18b66 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
18b67 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 RowSet ){. sq
18b68 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 lite3RowSetClear
18b69 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 (pMem->u.pRowSet
18b6a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
18b6b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18b6c 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
18b6d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d pMem->zMalloc =
18b6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
18b6f 52 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d Raw(db, 64);. }
18b70 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
18b71 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 cFailed ){. p
18b72 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
18b73 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a _Null;. }else{.
18b74 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
18b75 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 ->zMalloc );.
18b76 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 pMem->u.pRowSet
18b77 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 = sqlite3RowSet
18b78 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a Init(db, pMem->z
18b79 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20 Malloc, .
18b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b7c 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 sqlite3DbMallocS
18b7d 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d ize(db, pMem->zM
18b7e 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73 alloc));. ass
18b7f 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f ert( pMem->u.pRo
18b80 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 wSet!=0 );. p
18b81 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
18b82 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a _RowSet;. }.}..
18b83 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
18b84 65 20 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a e if the Mem obj
18b85 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 ect contains a T
18b86 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 EXT or BLOB that
18b87 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 is.** too large
18b88 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 - whose size ex
18b89 63 65 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 ceeds SQLITE_MAX
18b8a 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 _LENGTH..*/.SQLI
18b8b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18b8c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f qlite3VdbeMemToo
18b8d 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 Big(Mem *p){. a
18b8e 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 ssert( p->db!=0
18b8f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 );. if( p->flag
18b90 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d s & (MEM_Str|MEM
18b91 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e _Blob) ){. in
18b92 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 t n = p->n;.
18b93 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d if( p->flags & M
18b94 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 EM_Zero ){.
18b95 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f n += p->u.nZero
18b96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
18b97 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d rn n>p->db->aLim
18b98 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
18b99 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 LENGTH];. }. r
18b9a 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a eturn 0; .}../*.
18b9b 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 ** Size of struc
18b9c 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 t Mem not includ
18b9d 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c ing the Mem.zMal
18b9e 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 loc member..*/.#
18b9f 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 define MEMCELLSI
18ba0 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 ZE (size_t)(&(((
18ba1 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f Mem *)0)->zMallo
18ba2 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 c))../*.** Make
18ba3 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 an shallow copy
18ba4 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 of pFrom into pT
18ba5 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e o. Prior conten
18ba6 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 ts of.** pTo are
18ba7 20 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 freed. The pFr
18ba8 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e om->z field is n
18ba9 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 ot duplicated.
18baa 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 If.** pFrom->z i
18bab 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f s used, then pTo
18bac 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ->z points to th
18bad 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 e same thing as
18bae 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 pFrom->z.** and
18baf 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 flags gets srcTy
18bb0 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 pe (either MEM_E
18bb1 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 phem or MEM_Stat
18bb2 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ic)..*/.SQLITE_P
18bb3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
18bb4 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f te3VdbeMemShallo
18bb5 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 wCopy(Mem *pTo,
18bb6 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d const Mem *pFrom
18bb7 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a , int srcType){.
18bb8 20 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d assert( (pFrom
18bb9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
18bba 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 wSet)==0 );. sq
18bbb 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
18bbc 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 aseExternal(pTo)
18bbd 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 ;. memcpy(pTo,
18bbe 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 pFrom, MEMCELLSI
18bbf 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c ZE);. pTo->xDel
18bc0 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 = 0;. if( (pFr
18bc1 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 om->flags&MEM_Dy
18bc2 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e n)!=0 || pFrom->
18bc3 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f z==pFrom->zMallo
18bc4 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c c ){. pTo->fl
18bc5 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e ags &= ~(MEM_Dyn
18bc6 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f |MEM_Static|MEM_
18bc7 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 Ephem);. asse
18bc8 72 74 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d rt( srcType==MEM
18bc9 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 _Ephem || srcTyp
18bca 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b e==MEM_Static );
18bcb 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 . pTo->flags
18bcc 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a |= srcType;. }.
18bcd 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 }../*.** Make a
18bce 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 full copy of pFr
18bcf 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 om into pTo. Pr
18bd0 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 ior contents of
18bd1 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 pTo are.** freed
18bd2 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 before the copy
18bd3 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c is made..*/.SQL
18bd4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
18bd5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f sqlite3VdbeMemCo
18bd6 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e py(Mem *pTo, con
18bd7 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a st Mem *pFrom){.
18bd8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
18bd9 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 E_OK;.. assert(
18bda 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 (pFrom->flags &
18bdb 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
18bdc 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
18bdd 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
18bde 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 al(pTo);. memcp
18bdf 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 y(pTo, pFrom, ME
18be0 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 MCELLSIZE);. pT
18be1 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d o->flags &= ~MEM
18be2 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f _Dyn;.. if( pTo
18be3 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 ->flags&(MEM_Str
18be4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 |MEM_Blob) ){.
18be5 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d if( 0==(pFrom-
18be6 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 >flags&MEM_Stati
18be7 63 29 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d c) ){. pTo-
18be8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 >flags |= MEM_Ep
18be9 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 hem;. rc =
18bea 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 sqlite3VdbeMemMa
18beb 6b 65 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 keWriteable(pTo)
18bec 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
18bed 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
18bee 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 ** Transfer the
18bef 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f contents of pFro
18bf0 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78 m to pTo. Any ex
18bf1 69 73 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 isting value in
18bf2 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e pTo is.** freed.
18bf3 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 If pFrom contai
18bf4 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 ns ephemeral dat
18bf5 61 2c 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64 a, a copy is mad
18bf6 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 e..**.** pFrom c
18bf7 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e ontains an SQL N
18bf8 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f ULL when this ro
18bf9 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a utine returns..*
18bfa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18bfb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18bfc 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54 eMemMove(Mem *pT
18bfd 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a o, Mem *pFrom){.
18bfe 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d assert( pFrom-
18bff 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
18c00 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72 3_mutex_held(pFr
18c01 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 om->db->mutex) )
18c02 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d ;. assert( pTo-
18c03 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
18c04 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f 3_mutex_held(pTo
18c05 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
18c06 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d assert( pFrom-
18c07 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64 >db==0 || pTo->d
18c08 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64 b==0 || pFrom->d
18c09 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20 b==pTo->db );..
18c0a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
18c0b 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d elease(pTo);. m
18c0c 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d emcpy(pTo, pFrom
18c0d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a , sizeof(Mem));.
18c0e 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d pFrom->flags =
18c0f 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72 MEM_Null;. pFr
18c10 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 om->xDel = 0;.
18c11 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d pFrom->zMalloc =
18c12 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 0;.}../*.** Cha
18c13 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
18c14 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 a Mem to be a s
18c15 74 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e tring or a BLOB.
18c16 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 .**.** The memor
18c17 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 y management str
18c18 61 74 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e ategy depends on
18c19 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
18c1a 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65 e xDel.** parame
18c1b 74 65 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75 ter. If the valu
18c1c 65 20 70 61 73 73 65 64 20 69 73 20 53 51 4c 49 e passed is SQLI
18c1d 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68 TE_TRANSIENT, th
18c1e 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e en the .** strin
18c1f 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f g is copied into
18c20 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69 a (possibly exi
18c21 73 74 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61 sting) buffer ma
18c22 6e 61 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a naged by the .**
18c23 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 Mem structure.
18c24 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65 Otherwise, any e
18c25 78 69 73 74 69 6e 67 20 62 75 66 66 65 72 20 69 xisting buffer i
18c26 73 20 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a s freed and the.
18c27 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65 ** pointer copie
18c28 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
18c29 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18c2a 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 VdbeMemSetStr(.
18c2b 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 Mem *pMem,
18c2c 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
18c2d 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 ell to set to st
18c2e 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 ring value */.
18c2f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 const char *z,
18c30 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f /* String po
18c31 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e inter */. int n
18c32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
18c33 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e * Bytes in strin
18c34 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a g, or negative *
18c35 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 /. u8 enc,
18c36 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
18c37 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 ing of z. 0 for
18c38 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 BLOBs */. void
18c39 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 (*xDel)(void*)
18c3a 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 /* Destructor fu
18c3b 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 nction */.){. i
18c3c 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 nt nByte = n;
18c3d 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 /* New value
18c3e 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 for pMem->n */.
18c3f 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 int iLimit;
18c40 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
18c41 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f allowed string o
18c42 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 r blob size */.
18c43 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 u16 flags = 0;
18c44 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 /* New valu
18c45 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 e for pMem->flag
18c46 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 s */.. assert(
18c47 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
18c48 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18c49 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
18c4a 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
18c4b 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d (pMem->flags & M
18c4c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
18c4d 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 .. /* If z is a
18c4e 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 NULL pointer, s
18c4f 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 et pMem to conta
18c50 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 in an SQL NULL.
18c51 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 */. if( !z ){.
18c52 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
18c53 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a mSetNull(pMem);.
18c54 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
18c55 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 E_OK;. }.. if(
18c56 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 pMem->db ){.
18c57 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e iLimit = pMem->
18c58 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
18c59 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b E_LIMIT_LENGTH];
18c5a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c . }else{. iL
18c5b 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 imit = SQLITE_MA
18c5c 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 X_LENGTH;. }.
18c5d 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f flags = (enc==0?
18c5e 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 MEM_Blob:MEM_Str
18c5f 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 );. if( nByte<0
18c60 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
18c61 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 enc!=0 );. if
18c62 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ( enc==SQLITE_UT
18c63 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 F8 ){. for(
18c64 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d nByte=0; nByte<=
18c65 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 iLimit && z[nByt
18c66 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 e]; nByte++){}.
18c67 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
18c68 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 for(nByte=0; nBy
18c69 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a te<=iLimit && (z
18c6a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 [nByte] | z[nByt
18c6b 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 e+1]); nByte+=2)
18c6c 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 {}. }. fla
18c6d 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a gs |= MEM_Term;.
18c6e 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f }.. /* The fo
18c6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 llowing block se
18c70 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 ts the new value
18c71 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d s of Mem.z and M
18c72 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a em.xDel. It. **
18c73 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 also sets a fla
18c74 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 g in local varia
18c75 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 ble "flags" to i
18c76 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f ndicate the memo
18c77 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 ry. ** manageme
18c78 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 nt (one of MEM_D
18c79 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 yn or MEM_Static
18c7a 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 ).. */. if( xD
18c7b 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 el==SQLITE_TRANS
18c7c 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 IENT ){. int
18c7d 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a nAlloc = nByte;.
18c7e 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 if( flags&ME
18c7f 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 M_Term ){.
18c80 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d nAlloc += (enc==
18c81 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 SQLITE_UTF8?1:2)
18c82 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
18c83 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a nByte>iLimit ){.
18c84 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
18c85 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 ITE_TOOBIG;.
18c86 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
18c87 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
18c88 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b m, nAlloc, 0) ){
18c89 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
18c8a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
18c8b 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 }. memcpy(pMe
18c8c 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 m->z, z, nAlloc)
18c8d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 ;. }else if( xD
18c8e 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d el==SQLITE_DYNAM
18c8f 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 IC ){. sqlite
18c90 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
18c91 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d pMem);. pMem-
18c92 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d >zMalloc = pMem-
18c93 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a >z = (char *)z;.
18c94 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d pMem->xDel =
18c95 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
18c96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
18c97 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
18c98 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 pMem->z = (cha
18c99 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d r *)z;. pMem-
18c9a 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 >xDel = xDel;.
18c9b 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 flags |= ((xDe
18c9c 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 l==SQLITE_STATIC
18c9d 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d )?MEM_Static:MEM
18c9e 5f 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _Dyn);. }. if(
18c9f 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b nByte>iLimit ){
18ca0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
18ca1 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a TE_TOOBIG;. }..
18ca2 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 pMem->n = nByt
18ca3 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 e;. pMem->flags
18ca4 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d = flags;. pMem
18ca5 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 ->enc = (enc==0
18ca6 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 ? SQLITE_UTF8 :
18ca7 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 enc);. pMem->ty
18ca8 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 pe = (enc==0 ? S
18ca9 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c QLITE_BLOB : SQL
18caa 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e ITE_TEXT);..#ifn
18cab 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18cac 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d UTF16. if( pMem
18cad 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 ->enc!=SQLITE_UT
18cae 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 F8 && sqlite3Vdb
18caf 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d eMemHandleBom(pM
18cb0 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 em) ){. retur
18cb1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
18cb2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 }.#endif.. re
18cb3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
18cb4 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
18cb5 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 the values cont
18cb6 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f ained by the two
18cb7 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 memory cells, r
18cb8 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 eturning.** nega
18cb9 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f tive, zero or po
18cba 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 sitive if pMem1
18cbb 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
18cbc 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
18cbd 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 er.** than pMem2
18cbe 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 . Sorting order
18cbf 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c is NULL's first,
18cc0 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d followed by num
18cc1 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a bers (integers.*
18cc2 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 * and reals) sor
18cc3 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c ted numerically,
18cc4 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 followed by tex
18cc5 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 t ordered by the
18cc6 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 collating.** se
18cc7 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 quence pColl and
18cc8 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 finally blob's
18cc9 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d ordered by memcm
18cca 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e p()..**.** Two N
18ccb 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 ULL values are c
18ccc 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 onsidered equal
18ccd 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
18cce 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18ccf 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
18cd0 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 emCompare(const
18cd1 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 Mem *pMem1, cons
18cd2 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f t Mem *pMem2, co
18cd3 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f nst CollSeq *pCo
18cd4 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ll){. int rc;.
18cd5 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 int f1, f2;. i
18cd6 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 nt combined_flag
18cd7 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 s;.. /* Interch
18cd8 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 ange pMem1 and p
18cd9 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c Mem2 if the coll
18cda 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 ating sequence s
18cdb 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 pecifies. ** DE
18cdc 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 SC order.. */.
18cdd 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 f1 = pMem1->fla
18cde 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 gs;. f2 = pMem2
18cdf 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 ->flags;. combi
18ce0 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 ned_flags = f1|f
18ce1 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 2;. assert( (co
18ce2 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d mbined_flags & M
18ce3 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
18ce4 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 . . /* If one v
18ce5 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 alue is NULL, it
18ce6 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 is less than th
18ce7 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 e other. If both
18ce8 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 values. ** are
18ce9 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e NULL, return 0.
18cea 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 . */. if( comb
18ceb 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e ined_flags&MEM_N
18cec 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
18ced 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 n (f2&MEM_Null)
18cee 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b - (f1&MEM_Null);
18cef 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e . }.. /* If on
18cf0 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d e value is a num
18cf1 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 ber and the othe
18cf2 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 r is not, the nu
18cf3 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 mber is less..
18cf4 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e ** If both are n
18cf5 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 umbers, compare
18cf6 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 as reals if one
18cf7 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 is a real, or as
18cf8 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 integers. ** i
18cf9 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 f both values ar
18cfa 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f e integers.. */
18cfb 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f . if( combined_
18cfc 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d flags&(MEM_Int|M
18cfd 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 EM_Real) ){.
18cfe 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e if( !(f1&(MEM_In
18cff 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a t|MEM_Real)) ){.
18d00 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
18d01 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 }. if( !(
18d02 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f f2&(MEM_Int|MEM_
18d03 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 Real)) ){.
18d04 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
18d05 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 . if( (f1 & f
18d06 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 2 & MEM_Int)==0
18d07 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 ){. double
18d08 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 r1, r2;. if
18d09 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d ( (f1&MEM_Real)=
18d0a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 =0 ){. r1
18d0b 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 = (double)pMem1
18d0c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c ->u.i;. }el
18d0d 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d se{. r1 =
18d0e 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 pMem1->r;.
18d0f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 }. if( (f2
18d10 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b &MEM_Real)==0 ){
18d11 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 . r2 = (d
18d12 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 ouble)pMem2->u.i
18d13 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
18d14 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d r2 = pMem
18d15 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 2->r;. }.
18d16 20 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 if( r1<r2 )
18d17 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 return -1;.
18d18 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 if( r1>r2 ) ret
18d19 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 urn 1;. ret
18d1a 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 urn 0;. }else
18d1b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
18d1c 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 f1&MEM_Int );.
18d1d 20 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d assert( f2&M
18d1e 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 EM_Int );.
18d1f 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c if( pMem1->u.i <
18d20 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 pMem2->u.i ) re
18d21 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 turn -1;. i
18d22 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 f( pMem1->u.i >
18d23 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 pMem2->u.i ) ret
18d24 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 urn 1;. ret
18d25 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
18d26 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 .. /* If one va
18d27 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 lue is a string
18d28 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 and the other is
18d29 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 a blob, the str
18d2a 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a ing is less.. *
18d2b 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 * If both are st
18d2c 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 rings, compare u
18d2d 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 sing the collati
18d2e 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 ng functions..
18d2f 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 */. if( combine
18d30 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 d_flags&MEM_Str
18d31 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 ){. if( (f1 &
18d32 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a MEM_Str)==0 ){.
18d33 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
18d34 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 }. if( (f
18d35 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 2 & MEM_Str)==0
18d36 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
18d37 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 -1;. }.. a
18d38 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e ssert( pMem1->en
18d39 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b c==pMem2->enc );
18d3a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
18d3b 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f m1->enc==SQLITE_
18d3c 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 UTF8 || .
18d3d 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d pMem1->enc=
18d3e 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 =SQLITE_UTF16LE
18d3f 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 || pMem1->enc==S
18d40 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b QLITE_UTF16BE );
18d41 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c .. /* The col
18d42 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
18d43 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 must be defined
18d44 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 at this point, e
18d45 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 ven if. ** th
18d46 65 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 e user deletes t
18d47 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
18d48 75 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 uence after the
18d49 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a vdbe program is.
18d4a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 ** compiled
18d4b 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c (this was not al
18d4c 77 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a ways the case)..
18d4d 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
18d4e 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f t( !pColl || pCo
18d4f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 ll->xCmp );..
18d50 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
18d51 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 if( pMem1->e
18d52 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 nc==pColl->enc )
18d53 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
18d54 20 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 strings are alr
18d55 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 eady in the corr
18d56 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 ect encoding. C
18d57 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 all the.
18d58 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 ** comparison fu
18d59 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 nction directly
18d5a 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
18d5b 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 n pColl->xCmp(pC
18d5c 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 oll->pUser,pMem1
18d5d 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 ->n,pMem1->z,pMe
18d5e 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b m2->n,pMem2->z);
18d5f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
18d60 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 const void
18d61 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 *v1, *v2;.
18d62 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 int n1, n2;.
18d63 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 Mem c1;.
18d64 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 Mem c2;.
18d65 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 memset(&c
18d66 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 1, 0, sizeof(c1)
18d67 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 );. memse
18d68 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 t(&c2, 0, sizeof
18d69 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 (c2));. s
18d6a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
18d6b 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d llowCopy(&c1, pM
18d6c 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b em1, MEM_Ephem);
18d6d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
18d6e 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
18d6f 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d py(&c2, pMem2, M
18d70 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 EM_Ephem);.
18d71 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 v1 = sqlite3V
18d72 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 alueText((sqlite
18d73 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 3_value*)&c1, pC
18d74 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 oll->enc);.
18d75 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 n1 = v1==0 ?
18d76 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 0 : c1.n;.
18d77 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 v2 = sqlite3Va
18d78 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 lueText((sqlite3
18d79 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f _value*)&c2, pCo
18d7a 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 ll->enc);.
18d7b 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 n2 = v2==0 ? 0
18d7c 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 : c2.n;.
18d7d 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d rc = pColl->xCm
18d7e 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 p(pColl->pUser,
18d7f 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b n1, v1, n2, v2);
18d80 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
18d81 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
18d82 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c c1);. sql
18d83 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
18d84 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 se(&c2);.
18d85 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
18d86 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a }. }. /*
18d87 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 If a NULL point
18d88 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 er was passed as
18d89 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e the collate fun
18d8a 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f ction, fall thro
18d8b 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 ugh. ** to th
18d8c 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 e blob case and
18d8d 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a use memcmp(). *
18d8e 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 /. }. . /* Bot
18d8f 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 h values must be
18d90 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 blobs. Compare
18d91 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e using memcmp().
18d92 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 */. rc = memc
18d93 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 mp(pMem1->z, pMe
18d94 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e m2->z, (pMem1->n
18d95 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 >pMem2->n)?pMem2
18d96 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 ->n:pMem1->n);.
18d97 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 if( rc==0 ){.
18d98 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 rc = pMem1->n
18d99 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a - pMem2->n;. }.
18d9a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
18d9b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 /*.** Move data
18d9c 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b out of a btree k
18d9d 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 ey or data field
18d9e 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 and into a Mem
18d9f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 structure..** Th
18da0 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 e data or key is
18da1 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 taken from the
18da2 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 entry that pCur
18da3 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
18da4 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 nting.** to. of
18da5 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 fset and amt det
18da6 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 ermine what port
18da7 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 ion of the data
18da8 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 or key to retrie
18da9 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 ve..** key is tr
18daa 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 ue to get the ke
18dab 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 y or false to ge
18dac 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 t data. The res
18dad 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a ult is written.*
18dae 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 * into the pMem
18daf 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 element..**.** T
18db0 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 he pMem structur
18db1 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 e is assumed to
18db2 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 be uninitialized
18db3 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e . Any prior con
18db4 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 tent.** is overw
18db5 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 ritten without b
18db6 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a eing freed..**.*
18db7 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e * If this routin
18db8 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 e fails for any
18db9 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 reason (malloc r
18dba 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 eturns NULL or u
18dbb 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 nable.** to read
18dbc 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 from the disk)
18dbd 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 then the pMem is
18dbe 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
18dbf 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a nsistent state..
18dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18dc1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
18dc2 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 eMemFromBtree(.
18dc3 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
18dc4 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 /* Cursor poi
18dc5 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 nting at record
18dc6 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a to retrieve. */.
18dc7 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 int offset,
18dc8 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 /* Offset fr
18dc9 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 om the start of
18dca 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 data to return b
18dcb 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 ytes from. */.
18dcc 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
18dcd 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
18dce 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 ytes to return.
18dcf 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 */. int key,
18dd0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
18dd1 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d e, retrieve from
18dd2 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 the btree key,
18dd3 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d not data. */. M
18dd4 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 em *pMem
18dd5 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 /* OUT: Return
18dd6 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d data in this Mem
18dd7 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 structure. */.)
18dd8 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b {. char *zData;
18dd9 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 /* Data f
18dda 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 rom the btree la
18ddb 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 yer */. int ava
18ddc 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e ilable = 0; /* N
18ddd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 umber of bytes a
18dde 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
18ddf 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 local btree page
18de0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
18de1 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 b; /* Data
18de2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
18de3 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
18de4 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d LITE_OK;.. db =
18de5 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
18de6 73 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 sorDb(pCur);. a
18de7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
18de8 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
18de9 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
18dea 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
18deb 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
18dec 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a );. if( key ){.
18ded 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 zData = (cha
18dee 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 r *)sqlite3Btree
18def 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 KeyFetch(pCur, &
18df0 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 available);. }e
18df1 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d lse{. zData =
18df2 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
18df3 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 BtreeDataFetch(p
18df4 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 Cur, &available)
18df5 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
18df6 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69 zData!=0 );.. i
18df7 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61 f( offset+amt<=a
18df8 76 61 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d vailable && ((pM
18df9 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
18dfa 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78 n)==0 || pMem->x
18dfb 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 Del) ){. sqli
18dfc 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
18dfd 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 e(pMem);. pMe
18dfe 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66 m->z = &zData[of
18dff 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d fset];. pMem-
18e00 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
18e01 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d b|MEM_Ephem;. }
18e02 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f else if( SQLITE_
18e03 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK==(rc = sqlite
18e04 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
18e05 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b m, amt+2, 0)) ){
18e06 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
18e07 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f = MEM_Blob|MEM_
18e08 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 Dyn|MEM_Term;.
18e09 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b pMem->enc = 0;
18e0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
18e0b 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 = SQLITE_BLOB;.
18e0c 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 if( key ){.
18e0d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
18e0e 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f BtreeKey(pCur, o
18e0f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d ffset, amt, pMem
18e10 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ->z);. }else{
18e11 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
18e12 74 65 33 42 74 72 65 65 44 61 74 61 28 70 43 75 te3BtreeData(pCu
18e13 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 r, offset, amt,
18e14 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a pMem->z);. }.
18e15 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d pMem->z[amt]
18e16 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e = 0;. pMem->
18e17 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 z[amt+1] = 0;.
18e18 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18e19 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
18e1a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
18e1b 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a se(pMem);. }.
18e1c 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
18e1d 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 amt;.. return r
18e1e 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 c;.}../* This fu
18e1f 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 nction is only a
18e20 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61 vailable interna
18e21 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 lly, it is not p
18e22 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 art of the.** ex
18e23 74 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77 ternal API. It w
18e24 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61 orks in a simila
18e25 72 20 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33 r way to sqlite3
18e26 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a _value_text(),.*
18e27 2a 20 65 78 63 65 70 74 20 74 68 65 20 64 61 74 * except the dat
18e28 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e a returned is in
18e29 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70 the encoding sp
18e2a 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 73 ecified by the s
18e2b 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 econd.** paramet
18e2c 65 72 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 er, which must b
18e2d 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f e one of SQLITE_
18e2e 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f UTF16BE, SQLITE_
18e2f 55 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 UTF16LE or.** SQ
18e30 4c 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a LITE_UTF8..**.**
18e31 20 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20 (2006-02-16:)
18e32 54 68 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61 The enc value ca
18e33 6e 20 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20 n be or-ed with
18e34 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
18e35 47 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74 GNED..** If that
18e36 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
18e37 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75 en the result mu
18e38 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e st be aligned on
18e39 20 61 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a an even byte.**
18e3a 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 boundary..*/.SQ
18e3b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
18e3c 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
18e3d 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 ValueText(sqlite
18e3e 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75 3_value* pVal, u
18e3f 38 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70 8 enc){. if( !p
18e40 56 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a Val ) return 0;.
18e41 0a 20 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d . assert( pVal-
18e42 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
18e43 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 3_mutex_held(pVa
18e44 6c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b l->db->mutex) );
18e45 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e 63 26 . assert( (enc&
18e46 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45 3)==(enc&~SQLITE
18e47 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 _UTF16_ALIGNED)
18e48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 );. assert( (pV
18e49 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f al->flags & MEM_
18e4a 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 RowSet)==0 );..
18e4b 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 if( pVal->flags
18e4c 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 &MEM_Null ){.
18e4d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18e4e 20 61 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c assert( (MEM_Bl
18e4f 6f 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 ob>>3) == MEM_St
18e50 72 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 r );. pVal->fla
18e51 67 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 gs |= (pVal->fla
18e52 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e gs & MEM_Blob)>>
18e53 33 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 3;. expandBlob(
18e54 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 pVal);. if( pVa
18e55 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 l->flags&MEM_Str
18e56 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
18e57 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
18e58 67 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 g(pVal, enc & ~S
18e59 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
18e5a 4e 45 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 NED);. if( (e
18e5b 6e 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 nc & SQLITE_UTF1
18e5c 36 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 6_ALIGNED)!=0 &&
18e5d 20 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 1==(1&SQLITE_PT
18e5e 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a R_TO_INT(pVal->z
18e5f 29 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 )) ){. asse
18e60 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 rt( (pVal->flags
18e61 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 & (MEM_Ephem|ME
18e62 4d 5f 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b M_Static))!=0 );
18e63 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
18e64 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
18e65 74 65 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 teable(pVal)!=SQ
18e66 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
18e67 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
18e68 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
18e69 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c qlite3VdbeMemNul
18e6a 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b Terminate(pVal);
18e6b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
18e6c 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 sert( (pVal->fla
18e6d 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 gs&MEM_Blob)==0
18e6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
18e6f 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 beMemStringify(p
18e70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61 Val, enc);. a
18e71 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c ssert( 0==(1&SQL
18e72 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 ITE_PTR_TO_INT(p
18e73 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a Val->z)) );. }.
18e74 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 assert(pVal->e
18e75 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 nc==(enc & ~SQLI
18e76 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
18e77 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 ) || pVal->db==0
18e78 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c . |
18e79 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c | pVal->db->mall
18e7a 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 ocFailed );. if
18e7b 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e ( pVal->enc==(en
18e7c 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 c & ~SQLITE_UTF1
18e7d 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 6_ALIGNED) ){.
18e7e 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a return pVal->z
18e7f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
18e80 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a eturn 0;. }.}..
18e81 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
18e82 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ew sqlite3_value
18e83 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 object..*/.SQLI
18e84 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
18e85 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 e3_value *sqlite
18e86 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 3ValueNew(sqlite
18e87 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 3 *db){. Mem *p
18e88 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
18e89 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
18e8a 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 f(*p));. if( p
18e8b 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 ){. p->flags
18e8c 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
18e8d 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 p->type = SQLITE
18e8e 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 _NULL;. p->db
18e8f 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 = db;. }. ret
18e90 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
18e91 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c Create a new sql
18e92 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
18e93 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 t, containing th
18e94 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 e value of pExpr
18e95 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c ..**.** This onl
18e96 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 y works for very
18e97 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 simple expressi
18e98 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 ons that consist
18e99 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 of one constant
18e9a 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 .** token (i.e.
18e9b 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 "5", "5.1", "'a
18e9c 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 string'"). If th
18e9d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e e expression can
18e9e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 .** be converted
18e9f 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 directly into a
18ea0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 value, then the
18ea1 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 value is alloca
18ea2 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 ted and.** a poi
18ea3 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 nter written to
18ea4 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c *ppVal. The call
18ea5 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c er is responsibl
18ea6 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 e for deallocati
18ea7 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 ng.** the value
18ea8 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f by passing it to
18ea9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 sqlite3ValueFre
18eaa 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 e() later on. If
18eab 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a the expression.
18eac 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e ** cannot be con
18ead 76 65 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 verted to a valu
18eae 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 e, then *ppVal i
18eaf 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a s set to NULL..*
18eb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18eb1 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 int sqlite3Valu
18eb2 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c eFromExpr(. sql
18eb3 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
18eb4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
18eb5 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
18eb6 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 n */. Expr *pEx
18eb7 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pr,
18eb8 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 /* The expressi
18eb9 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a on to evaluate *
18eba 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 /. u8 enc,
18ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18ebc 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 Encoding to use
18ebd 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 */. u8 affinit
18ebe 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y,
18ebf 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 /* Affinity to u
18ec0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f se */. sqlite3_
18ec1 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 value **ppVal
18ec2 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
18ec3 65 77 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f ew value here */
18ec4 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 .){. int op;.
18ec5 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a char *zVal = 0;.
18ec6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
18ec7 2a 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 *pVal = 0;.. if
18ec8 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 ( !pExpr ){.
18ec9 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 *ppVal = 0;.
18eca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18ecb 3b 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 ;. }. op = pEx
18ecc 70 72 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f pr->op;.. if( o
18ecd 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 p==TK_STRING ||
18ece 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 op==TK_FLOAT ||
18ecf 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 op==TK_INTEGER )
18ed0 7b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c {. zVal = sql
18ed1 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 ite3DbStrNDup(db
18ed2 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e , (char*)pExpr->
18ed3 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e token.z, pExpr->
18ed4 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 token.n);. pV
18ed5 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 al = sqlite3Valu
18ed6 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 eNew(db);. if
18ed7 28 20 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c ( !zVal || !pVal
18ed8 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
18ed9 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
18eda 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c SetStr(pVal, -1,
18edb 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 zVal, SQLITE_UT
18edc 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d F8, SQLITE_DYNAM
18edd 49 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 IC);. if( (op
18ede 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 ==TK_INTEGER ||
18edf 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 op==TK_FLOAT ) &
18ee0 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 & affinity==SQLI
18ee1 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 TE_AFF_NONE ){.
18ee2 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 sqlite3Valu
18ee3 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 eApplyAffinity(p
18ee4 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f Val, SQLITE_AFF_
18ee5 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f NUMERIC, SQLITE_
18ee6 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 UTF8);. }else
18ee7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
18ee8 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
18ee9 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 y(pVal, affinity
18eea 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
18eeb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e }. if( en
18eec 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c!=SQLITE_UTF8 )
18eed 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
18eee 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
18eef 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 g(pVal, enc);.
18ef0 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
18ef1 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 op==TK_UMINUS )
18ef2 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 {. if( SQLITE
18ef3 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 _OK==sqlite3Valu
18ef4 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 eFromExpr(db,pEx
18ef5 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 pr->pLeft,enc,af
18ef6 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b finity,&pVal) ){
18ef7 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 . pVal->u.i
18ef8 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e = -1 * pVal->u.
18ef9 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 i;. /* (dou
18efa 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f ble)-1 In case o
18efb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
18efc 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 OATING_POINT...
18efd 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 */. pVal->r
18efe 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 = (double)-1 *
18eff 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 pVal->r;. }.
18f00 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
18f01 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
18f02 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f RAL. else if( o
18f03 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 p==TK_BLOB ){.
18f04 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 int nVal;.
18f05 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 assert( pExpr->t
18f06 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 oken.n>=3 );.
18f07 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
18f08 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 token.z[0]=='x'
18f09 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e || pExpr->token.
18f0a 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 z[0]=='X' );.
18f0b 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
18f0c 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 token.z[1]=='\''
18f0d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
18f0e 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 pExpr->token.z[p
18f0f 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d Expr->token.n-1]
18f10 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 =='\'' );. pV
18f11 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 al = sqlite3Valu
18f12 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 eNew(db);. if
18f13 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e ( !pVal ) goto n
18f14 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20 o_mem;. nVal
18f15 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e = pExpr->token.n
18f16 20 2d 20 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d - 3;. zVal =
18f17 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 (char*)pExpr->t
18f18 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 oken.z + 2;.
18f19 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
18f1a 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 tStr(pVal, sqlit
18f1b 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 e3HexToBlob(db,
18f1c 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 zVal, nVal), nVa
18f1d 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 l/2,.
18f1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
18f1f 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
18f20 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
18f21 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 *ppVal = pVal;.
18f22 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
18f23 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 K;..no_mem:. db
18f24 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
18f25 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 1;. sqlite3DbF
18f26 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 ree(db, zVal);.
18f27 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 sqlite3ValueFre
18f28 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 e(pVal);. *ppVa
18f29 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 l = 0;. return
18f2a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a SQLITE_NOMEM;.}.
18f2b 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
18f2c 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f e string value o
18f2d 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c f an sqlite3_val
18f2e 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c ue object.*/.SQL
18f2f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
18f30 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
18f31 53 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 Str(. sqlite3_v
18f32 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 alue *v, /*
18f33 56 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 Value to be set
18f34 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 */. int n,
18f35 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
18f36 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a ngth of string z
18f37 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
18f38 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 *z, /* T
18f39 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 ext of the new s
18f3a 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e tring */. u8 en
18f3b 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
18f3c 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 /* Encoding to
18f3d 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a use */. void (*
18f3e 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f xDel)(void*) /
18f3f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 * Destructor for
18f40 20 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 the string */.)
18f41 7b 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 {. if( v ) sqli
18f42 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
18f43 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c ((Mem *)v, z, n,
18f44 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a enc, xDel);.}..
18f45 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 /*.** Free an sq
18f46 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
18f47 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ct.*/.SQLITE_PRI
18f48 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18f49 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 3ValueFree(sqlit
18f4a 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 e3_value *v){.
18f4b 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b if( !v ) return;
18f4c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
18f4d 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 mRelease((Mem *)
18f4e 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 v);. sqlite3DbF
18f4f 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 ree(((Mem*)v)->d
18f50 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 b, v);.}../*.**
18f51 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
18f52 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
18f53 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e sqlite3_value
18f54 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a object assuming.
18f55 2a 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20 ** that it uses
18f56 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e the encoding "en
18f57 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 c".*/.SQLITE_PRI
18f58 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18f59 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 ValueBytes(sqlit
18f5a 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 e3_value *pVal,
18f5b 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a u8 enc){. Mem *
18f5c 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a p = (Mem*)pVal;.
18f5d 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 if( (p->flags
18f5e 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c & MEM_Blob)!=0 |
18f5f 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 | sqlite3ValueTe
18f60 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b xt(pVal, enc) ){
18f61 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 . if( p->flag
18f62 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
18f63 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e return p->
18f64 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a n + p->u.nZero;.
18f65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18f66 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 return p->n;.
18f67 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
18f68 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../********
18f69 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
18f6a 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a bemem.c ********
18f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f6d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
18f6e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
18f6f 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a e vdbeaux.c ****
18f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f72 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
18f73 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 3 September 6.**
18f74 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
18f75 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
18f76 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
18f77 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
18f78 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
18f79 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
18f7a 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
18f7b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
18f7c 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
18f7d 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
18f7e 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
18f7f 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
18f80 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
18f81 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
18f82 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
18f83 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
18f84 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
18f85 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
18f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
18f8a 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
18f8b 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 ns code used for
18f8c 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 creating, destr
18f8d 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c oying, and popul
18f8e 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 ating.** a VDBE
18f8f 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f (or an "sqlite3_
18f90 73 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b stmt" as it is k
18f91 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 nown to the outs
18f92 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 ide world.) Pri
18f93 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e or.** to version
18f94 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 2.8.7, all this
18f95 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e code was combin
18f96 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 ed into the vdbe
18f97 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a .c source file..
18f98 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 ** But that file
18f99 20 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f was getting too
18f9a 20 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 big so this sub
18f9b 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 routines were sp
18f9c 6c 69 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 lit out..**.** $
18f9d 49 64 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 Id: vdbeaux.c,v
18f9e 31 2e 34 35 37 20 32 30 30 39 2f 30 35 2f 30 36 1.457 2009/05/06
18f9f 20 31 38 3a 35 37 3a 31 30 20 73 68 61 6e 65 20 18:57:10 shane
18fa0 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a Exp $.*/..../*.*
18fa1 2a 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67 * When debugging
18fa2 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 the code genera
18fa3 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69 tor in a symboli
18fa4 63 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20 c debugger, one
18fa5 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73 can.** set the s
18fa6 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 qlite3VdbeAddopT
18fa7 72 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c race to 1 and al
18fa8 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62 l opcodes will b
18fa9 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20 e printed.** as
18faa 74 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74 they are added t
18fab 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f o the instructio
18fac 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 n stream..*/.#if
18fad 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
18fae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18faf 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
18fb0 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23 ddopTrace = 0;.#
18fb1 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 endif.../*.** Cr
18fb2 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 eate a new virtu
18fb3 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 al database engi
18fb4 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ne..*/.SQLITE_PR
18fb5 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 IVATE Vdbe *sqli
18fb6 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 te3VdbeCreate(sq
18fb7 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 lite3 *db){. Vd
18fb8 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c be *p;. p = sql
18fb9 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
18fba 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 (db, sizeof(Vdbe
18fbb 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 ) );. if( p==0
18fbc 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d ) return 0;. p-
18fbd 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 >db = db;. if(
18fbe 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 db->pVdbe ){.
18fbf 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65 db->pVdbe->pPre
18fc0 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e v = p;. }. p->
18fc1 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62 pNext = db->pVdb
18fc2 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 e;. p->pPrev =
18fc3 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 0;. db->pVdbe =
18fc4 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d p;. p->magic =
18fc5 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 VDBE_MAGIC_INIT
18fc6 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
18fc7 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 ./*.** Remember
18fc8 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66 the SQL string f
18fc9 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 or a prepared st
18fca 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 atement..*/.SQLI
18fcb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18fcc 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 sqlite3VdbeSetSq
18fcd 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 l(Vdbe *p, const
18fce 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c char *z, int n,
18fcf 20 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32 int isPrepareV2
18fd0 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
18fd1 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53 return;.#ifdef S
18fd2 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
18fd3 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72 . if( !isPrepar
18fd4 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 eV2 ) return;.#e
18fd5 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 ndif. assert( p
18fd6 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 ->zSql==0 );. p
18fd7 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 ->zSql = sqlite3
18fd8 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c DbStrNDup(p->db,
18fd9 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 z, n);. p->isP
18fda 72 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65 repareV2 = isPre
18fdb 70 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a pareV2 ? 1 : 0;.
18fdc 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
18fdd 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74 the SQL associat
18fde 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72 ed with a prepar
18fdf 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a ed statement.*/.
18fe0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
18fe1 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
18fe2 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ql(sqlite3_stmt
18fe3 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
18fe4 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
18fe5 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d mt;. return (p-
18fe6 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70 >isPrepareV2 ? p
18fe7 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a ->zSql : 0);.}..
18fe8 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 /*.** Swap all c
18fe9 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 ontent between t
18fea 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 wo VDBE structur
18feb 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
18fec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18fed 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 e3VdbeSwap(Vdbe
18fee 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a *pA, Vdbe *pB){.
18fef 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d Vdbe tmp, *pTm
18ff0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b p;. char *zTmp;
18ff1 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 . tmp = *pA;.
18ff2 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 *pA = *pB;. *pB
18ff3 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d = tmp;. pTmp =
18ff4 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 pA->pNext;. pA
18ff5 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e ->pNext = pB->pN
18ff6 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 ext;. pB->pNext
18ff7 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 = pTmp;. pTmp
18ff8 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 = pA->pPrev;. p
18ff9 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 A->pPrev = pB->p
18ffa 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 Prev;. pB->pPre
18ffb 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 v = pTmp;. zTmp
18ffc 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 = pA->zSql;. p
18ffd 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 A->zSql = pB->zS
18ffe 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d ql;. pB->zSql =
18fff 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 zTmp;. pB->isP
19000 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 repareV2 = pA->i
19001 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 sPrepareV2;.}..#
19002 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
19003 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 UG./*.** Turn tr
19004 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a acing on or off.
19005 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19006 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19007 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c beTrace(Vdbe *p,
19008 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 FILE *trace){.
19009 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 p->trace = trac
1900a 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e;.}.#endif../*.
1900b 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 ** Resize the Vd
1900c 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 be.aOp array so
1900d 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 that it is at le
1900e 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 ast one op large
1900f 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 r than .** it wa
19010 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f s..**.** If an o
19011 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 ut-of-memory err
19012 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
19013 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 resizing the arr
19014 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ay, return.** SQ
19015 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 LITE_NOMEM. In t
19016 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f his case Vdbe.aO
19017 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c p and Vdbe.nOpAl
19018 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 loc remain .** u
19019 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 nchanged (this i
1901a 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 s so that any op
1901b 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c codes already al
1901c 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a located can be .
1901d 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 ** correctly dea
1901e 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 llocated along w
1901f 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 ith the rest of
19020 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 the Vdbe)..*/.st
19021 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 atic int growOpA
19022 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 rray(Vdbe *p){.
19023 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 VdbeOp *pNew;.
19024 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e int nNew = (p->
19025 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f nOpAlloc ? p->nO
19026 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 pAlloc*2 : (int)
19027 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 (1024/sizeof(Op)
19028 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c ));. pNew = sql
19029 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d ite3DbRealloc(p-
1902a 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 >db, p->aOp, nNe
1902b 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 w*sizeof(Op));.
1902c 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 if( pNew ){.
1902d 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 p->nOpAlloc = s
1902e 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
1902f 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f ze(p->db, pNew)/
19030 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 sizeof(Op);.
19031 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 p->aOp = pNew;.
19032 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 }. return (pNe
19033 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 w ? SQLITE_OK :
19034 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
19035 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
19036 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f w instruction to
19037 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 the list of ins
19038 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e tructions curren
19039 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 t in the.** VDBE
1903a 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 . Return the ad
1903b 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 dress of the new
1903c 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
1903d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a .** Parameters:.
1903e 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 **.** p
1903f 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 Pointer
19040 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a to the VDBE.**.
19041 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 ** op
19042 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 The opcode
19043 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 for this instru
19044 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ction.**.** p
19045 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 1, p2, p3 O
19046 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 perands.**.** Us
19047 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 e the sqlite3Vdb
19048 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 eResolveLabel()
19049 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 function to fix
1904a 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a an address and.*
1904b 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 * the sqlite3Vdb
1904c 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 eChangeP4() func
1904d 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 tion to change t
1904e 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1904f 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a P4.** operand..*
19050 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19051 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19052 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 AddOp3(Vdbe *p,
19053 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 int op, int p1,
19054 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b int p2, int p3){
19055 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 . int i;. Vdbe
19056 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 Op *pOp;.. i =
19057 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 p->nOp;. assert
19058 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
19059 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
1905a 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 assert( op>0 &&
1905b 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 op<0xff );. if
1905c 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 ( p->nOpAlloc<=i
1905d 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 ){. if( grow
1905e 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 OpArray(p) ){.
1905f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
19060 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 }. }. p->nOp
19061 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e ++;. pOp = &p->
19062 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f aOp[i];. pOp->o
19063 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a pcode = (u8)op;.
19064 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 pOp->p5 = 0;.
19065 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 pOp->p1 = p1;.
19066 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 pOp->p2 = p2;.
19067 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 pOp->p3 = p3;.
19068 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOp->p4.p = 0;.
19069 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1906a 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d P4_NOTUSED;. p-
1906b 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 >expired = 0;.#i
1906c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1906d 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e G. pOp->zCommen
1906e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c t = 0;. if( sql
1906f 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 ite3VdbeAddopTra
19070 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 ce ) sqlite3Vdbe
19071 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 PrintOp(0, i, &p
19072 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 ->aOp[i]);.#endi
19073 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 f.#ifdef VDBE_PR
19074 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 OFILE. pOp->cyc
19075 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e les = 0;. pOp->
19076 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a cnt = 0;.#endif.
19077 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 return i;.}.SQ
19078 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19079 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1907a 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p0(Vdbe *p, int
1907b 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 op){. return sq
1907c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1907d 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b p, op, 0, 0, 0);
1907e 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1907f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19080 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c eAddOp1(Vdbe *p,
19081 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 int op, int p1)
19082 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
19083 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
19084 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d op, p1, 0, 0);.}
19085 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19086 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
19087 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69 ddOp2(Vdbe *p, i
19088 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 nt op, int p1, i
19089 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e nt p2){. return
1908a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1908b 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 p3(p, op, p1, p2
1908c 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 , 0);.}.../*.**
1908d 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 Add an opcode th
1908e 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 at includes the
1908f 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f p4 value as a po
19090 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inter..*/.SQLITE
19091 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19092 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a ite3VdbeAddOp4(.
19093 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 Vdbe *p,
19094 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 /* Add the
19095 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20 opcode to this
19096 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 VM */. int op,
19097 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
19098 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f he new opcode */
19099 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 . int p1,
1909a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 /* The P1
1909b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
1909c 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20 t p2,
1909d 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72 /* The P2 oper
1909e 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c and */. int p3,
1909f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
190a0 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a The P3 operand *
190a1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
190a2 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 zP4, /* The P
190a3 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 4 operand */. i
190a4 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20 nt p4type
190a5 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 /* P4 operand
190a6 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e type */.){. in
190a7 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 t addr = sqlite3
190a8 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 VdbeAddOp3(p, op
190a9 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 , p1, p2, p3);.
190aa 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
190ab 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 geP4(p, addr, zP
190ac 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65 4, p4type);. re
190ad 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a turn addr;.}../*
190ae 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
190af 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 symbolic label
190b0 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 for an instructi
190b1 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 on that has yet
190b2 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 to be.** coded.
190b3 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 The symbolic la
190b4 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 bel is really ju
190b5 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 st a negative nu
190b6 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 mber. The.** la
190b7 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 bel can be used
190b8 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 as the P2 value
190b9 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e of an operation.
190ba 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a Later, when.**
190bb 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 the label is re
190bc 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 solved to a spec
190bd 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 ific address, th
190be 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e e VDBE will scan
190bf 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 .** through its
190c0 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 operation list a
190c1 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 nd change all va
190c2 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 lues of P2 which
190c3 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 match.** the la
190c4 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 bel into the res
190c5 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a olved address..*
190c6 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e *.** The VDBE kn
190c7 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 ows that a P2 va
190c8 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 lue is a label b
190c9 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 ecause labels ar
190ca 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 e.** always nega
190cb 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 tive and P2 valu
190cc 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 es are suppose t
190cd 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 o be non-negativ
190ce 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e e..** Hence, a n
190cf 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 egative P2 value
190d0 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 is a label that
190d1 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 has yet to be r
190d2 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a esolved..**.** Z
190d3 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 ero is returned
190d4 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
190d5 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
190d6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
190d7 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
190d8 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
190d9 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 i;. i = p->nLab
190da 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 el++;. assert(
190db 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
190dc 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
190dd 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 f( i>=p->nLabelA
190de 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 lloc ){. int
190df 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c n = p->nLabelAll
190e0 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d oc*2 + 5;. p-
190e1 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 >aLabel = sqlite
190e2 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
190e3 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 (p->db, p->aLabe
190e4 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 l,.
190e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190e6 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 n*size
190e7 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 of(p->aLabel[0])
190e8 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c );. p->nLabel
190e9 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 Alloc = sqlite3D
190ea 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 bMallocSize(p->d
190eb 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 b, p->aLabel)/si
190ec 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 zeof(p->aLabel[0
190ed 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d ]);. }. if( p-
190ee 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 >aLabel ){. p
190ef 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 ->aLabel[i] = -1
190f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d ;. }. return -
190f1 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 1-i;.}../*.** Re
190f2 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 solve label "x"
190f3 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 to be the addres
190f4 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
190f5 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 struction to.**
190f6 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 be inserted. Th
190f7 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 e parameter "x"
190f8 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f must have been o
190f9 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
190fa 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 a prior call to
190fb 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
190fc 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 abel()..*/.SQLIT
190fd 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
190fe 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
190ff 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 eLabel(Vdbe *p,
19100 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 int x){. int j
19101 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 = -1-x;. assert
19102 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
19103 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
19104 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 assert( j>=0 &&
19105 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a j<p->nLabel );.
19106 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 if( p->aLabel
19107 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c ){. p->aLabel
19108 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 [j] = p->nOp;.
19109 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 }.}../*.** Loop
1910a 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 through the prog
1910b 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ram looking for
1910c 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 P2 values that a
1910d 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f re negative.** o
1910e 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 n jump instructi
1910f 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 ons. Each such
19110 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c value is a label
19111 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a . Resolve the.*
19112 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 * label by setti
19113 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 ng the P2 value
19114 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e to its correct n
19115 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a on-zero value..*
19116 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
19117 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 e is called once
19118 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 after all opcod
19119 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 es have been ins
1911a 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 erted..**.** Var
1911b 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 iable *pMaxFuncA
1911c 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 rgs is set to th
1911d 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 e maximum value
1911e 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 of any P2 argume
1911f 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f nt .** to an OP_
19120 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 Function, OP_Agg
19121 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 Step or OP_VFilt
19122 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 er opcode. This
19123 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 is used by .** s
19124 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
19125 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 ady() to size th
19126 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 e Vdbe.apArg[] a
19127 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rray..**.** This
19128 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f routine also do
19129 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
1912a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20 optimization:
1912b 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20 It scans for.**
1912c 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 instructions tha
1912d 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20 t might cause a
1912e 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 statement rollba
1912f 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75 ck. Such instru
19130 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a ctions.** are:.*
19131 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c *.** * OP_Hal
19132 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 t with P1=SQLITE
19133 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 _CONSTRAINT and
19134 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 P2=OE_Abort..**
19135 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a * OP_Destroy.
19136 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 ** * OP_VUpda
19137 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 te.** * OP_VR
19138 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ename.**.** If n
19139 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69 o such instructi
1913a 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 on is found, the
1913b 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e n every Statemen
1913c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a t instruction .*
1913d 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 * is changed to
1913e 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73 a Noop. In this
1913f 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 way, we avoid c
19140 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 reating the stat
19141 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61 ement .** journa
19142 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 l file unnecessa
19143 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rily..*/.static
19144 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 void resolveP2Va
19145 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e lues(Vdbe *p, in
19146 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 t *pMaxFuncArgs)
19147 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
19148 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 nMaxArgs = 0;.
19149 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 Op *pOp;. int
1914a 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 *aLabel = p->aLa
1914b 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53 bel;. int doesS
1914c 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b tatementRollback
1914d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53 = 0;. int hasS
1914e 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 tatementBegin =
1914f 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 0;. p->readOnly
19150 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53 = 1;. p->usesS
19151 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a tmtJournal = 0;.
19152 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 for(pOp=p->aOp
19153 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e , i=p->nOp-1; i>
19154 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b =0; i--, pOp++){
19155 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d . u8 opcode =
19156 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 pOp->opcode;..
19157 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
19158 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 P_Function || op
19159 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 code==OP_AggStep
1915a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
1915b 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 p->p5>nMaxArgs )
1915c 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d nMaxArgs = pOp-
1915d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >p5;.#ifndef SQL
1915e 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1915f 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 TABLE. }else
19160 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 if( opcode==OP_V
19161 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 Update ){.
19162 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 if( pOp->p2>nMax
19163 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
19164 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 = pOp->p2;.#endi
19165 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 f. }. if(
19166 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 opcode==OP_Halt
19167 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ){. if( pOp
19168 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e ->p1==SQLITE_CON
19169 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e STRAINT && pOp->
1916a 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a p2==OE_Abort ){.
1916b 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 doesStat
1916c 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 ementRollback =
1916d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
1916e 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d else if( opcode=
1916f 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b =OP_Statement ){
19170 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d . hasStatem
19171 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20 entBegin = 1;.
19172 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a p->usesStmtJ
19173 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 ournal = 1;.
19174 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 }else if( opcode
19175 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a ==OP_Destroy ){.
19176 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d doesStatem
19177 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b entRollback = 1;
19178 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f . }else if( o
19179 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 pcode==OP_Transa
1917a 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 ction && pOp->p2
1917b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e !=0 ){. p->
1917c 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 readOnly = 0;.#i
1917d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1917e 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1917f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
19180 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 ode==OP_VUpdate
19181 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 || opcode==OP_VR
19182 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64 ename ){. d
19183 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c oesStatementRoll
19184 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 back = 1;. }e
19185 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
19186 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20 OP_VFilter ){.
19187 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 int n;.
19188 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20 assert( p->nOp
19189 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20 - i >= 3 );.
1918a 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
1918b 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74 ].opcode==OP_Int
1918c 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20 eger );. n
1918d 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 = pOp[-1].p1;.
1918e 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72 if( n>nMaxAr
1918f 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 gs ) nMaxArgs =
19190 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a n;.#endif. }.
19191 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
19192 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f VdbeOpcodeHasPro
19193 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50 perty(opcode, OP
19194 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70 FLG_JUMP) && pOp
19195 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 ->p2<0 ){.
19196 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e assert( -1-pOp->
19197 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a p2<p->nLabel );.
19198 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 pOp->p2 =
19199 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70 aLabel[-1-pOp->p
1919a 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2];. }. }.
1919b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
1919c 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b >db, p->aLabel);
1919d 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30 . p->aLabel = 0
1919e 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 ;.. *pMaxFuncAr
1919f 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a gs = nMaxArgs;..
191a0 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 /* If we never
191a1 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 rollback a stat
191a2 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
191a3 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e n, then statemen
191a4 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 t. ** transacti
191a5 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 ons are not need
191a6 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 ed. So change e
191a7 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e very OP_Statemen
191a8 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e t. ** opcode in
191a9 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 to an OP_Noop.
191aa 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c This avoid a cal
191ab 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 l to sqlite3OsOp
191ac 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 enExclusive().
191ad 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ** which can be
191ae 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d expensive on som
191af 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a e platforms.. *
191b0 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 /. if( hasState
191b1 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f mentBegin && !do
191b2 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
191b3 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73 ack ){. p->us
191b4 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 esStmtJournal =
191b5 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70 0;. for(pOp=p
191b6 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d ->aOp, i=p->nOp-
191b7 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 1; i>=0; i--, pO
191b8 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 p++){. if(
191b9 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
191ba 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 Statement ){.
191bb 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 pOp->opcode
191bc 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 = OP_Noop;.
191bd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
191be 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
191bf 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
191c0 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
191c1 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 n to be inserted
191c2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
191c3 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
191c4 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 dbeCurrentAddr(V
191c5 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 dbe *p){. asser
191c6 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
191c7 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
191c8 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b return p->nOp;
191c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 .}../*.** Add a
191ca 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 whole list of op
191cb 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 erations to the
191cc 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e operation stack.
191cd 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 Return the.**
191ce 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 address of the f
191cf 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 irst operation a
191d0 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dded..*/.SQLITE_
191d1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
191d2 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
191d3 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f (Vdbe *p, int nO
191d4 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
191d5 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 nst *aOp){. int
191d6 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 addr;. assert(
191d7 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
191d8 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
191d9 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 if( p->nOp + nOp
191da 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 > p->nOpAlloc &
191db 26 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 & growOpArray(p)
191dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
191dd 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 ;. }. addr = p
191de 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 ->nOp;. if( nOp
191df 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b >0 ){. int i;
191e0 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 . VdbeOpList
191e1 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 const *pIn = aOp
191e2 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
191e3 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b <nOp; i++, pIn++
191e4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 ){. int p2
191e5 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 = pIn->p2;.
191e6 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 VdbeOp *pOut =
191e7 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b &p->aOp[i+addr];
191e8 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 . pOut->opc
191e9 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 ode = pIn->opcod
191ea 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 e;. pOut->p
191eb 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 1 = pIn->p1;.
191ec 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 if( p2<0 && s
191ed 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
191ee 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 HasProperty(pOut
191ef 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f ->opcode, OPFLG_
191f0 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 JUMP) ){.
191f1 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 pOut->p2 = addr
191f2 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 + ADDR(p2);.
191f3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
191f4 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b pOut->p2 = p2;
191f5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
191f6 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 Out->p3 = pIn->p
191f7 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 3;. pOut->p
191f8 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
191f9 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e ED;. pOut->
191fa 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 p4.p = 0;.
191fb 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 pOut->p5 = 0;.#i
191fc 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
191fd 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 G. pOut->zC
191fe 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 omment = 0;.
191ff 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
19200 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 eAddopTrace ){.
19201 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
19202 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 bePrintOp(0, i+a
19203 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 ddr, &p->aOp[i+a
19204 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 ddr]);. }.#
19205 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
19206 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 p->nOp += nOp;.
19207 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 }. return addr
19208 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
19209 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
1920a 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f he P1 operand fo
1920b 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 r a specific ins
1920c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 truction..** Thi
1920d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
1920e 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 ful when a large
1920f 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 program is load
19210 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 ed from a.** sta
19211 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 tic array using
19212 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
19213 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 List but we want
19214 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 to make a.** fe
19215 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 w minor changes
19216 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a to the program..
19217 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19218 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19219 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 beChangeP1(Vdbe
1921a 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e *p, int addr, in
1921b 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 t val){. assert
1921c 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 ( p==0 || p->mag
1921d 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
1921e 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 NIT );. if( p &
1921f 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e & addr>=0 && p->
19220 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 nOp>addr && p->a
19221 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 Op ){. p->aOp
19222 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b [addr].p1 = val;
19223 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 . }.}../*.** Ch
19224 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
19225 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 f the P2 operand
19226 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 for a specific
19227 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
19228 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
19229 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 useful for setti
1922a 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e ng a jump destin
1922b 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
1922c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1922d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1922e 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 2(Vdbe *p, int a
1922f 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 ddr, int val){.
19230 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c assert( p==0 ||
19231 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
19232 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
19233 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 if( p && addr>=0
19234 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 && p->nOp>addr
19235 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 && p->aOp ){.
19236 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 p->aOp[addr].p2
19237 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f = val;. }.}../
19238 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
19239 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 value of the P3
1923a 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 operand for a sp
1923b 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 ecific instructi
1923c 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1923d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1923e 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 e3VdbeChangeP3(V
1923f 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
19240 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 , int val){. as
19241 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d sert( p==0 || p-
19242 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
19243 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
19244 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 p && addr>=0 &&
19245 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 p->nOp>addr &&
19246 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d p->aOp ){. p-
19247 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 >aOp[addr].p3 =
19248 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a val;. }.}../*.*
19249 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
1924a 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 ue of the P5 ope
1924b 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 rand for the mos
1924c 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 t recently.** ad
1924d 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a ded operation..*
1924e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1924f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19250 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a eChangeP5(Vdbe *
19251 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 p, u8 val){. as
19252 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d sert( p==0 || p-
19253 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
19254 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
19255 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a p && p->aOp ){.
19256 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
19257 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 Op>0 );. p->a
19258 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 Op[p->nOp-1].p5
19259 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
1925a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 .** Change the P
1925b 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 2 operand of ins
1925c 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f truction addr so
1925d 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1925e 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 to.** the addres
1925f 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
19260 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 struction to be
19261 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 coded..*/.SQLITE
19262 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19263 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
19264 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 e(Vdbe *p, int a
19265 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 ddr){. sqlite3V
19266 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 dbeChangeP2(p, a
19267 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a ddr, p->nOp);.}.
19268 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ../*.** If the i
19269 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 nput FuncDef str
1926a 75 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 ucture is epheme
1926b 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 ral, then free i
1926c 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 t. If.** the Fu
1926d 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 ncDef is not eph
1926e 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e ermal, then do n
1926f 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 othing..*/.stati
19270 63 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d c void freeEphem
19271 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c eralFunction(sql
19272 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 ite3 *db, FuncDe
19273 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 f *pDef){. if(
19274 70 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66 pDef && (pDef->f
19275 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
19276 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a NC_EPHEM)!=0 ){.
19277 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
19278 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d e(db, pDef);. }
19279 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
1927a 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e a P4 value if n
1927b 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 ecessary..*/.sta
1927c 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 tic void freeP4(
1927d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
1927e 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 p4type, void *p
1927f 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 4){. if( p4 ){.
19280 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 switch( p4ty
19281 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 pe ){. case
19282 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 P4_REAL:.
19283 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 case P4_INT64:.
19284 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 case P4_MPR
19285 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 INTF:. case
19286 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 P4_DYNAMIC:.
19287 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
19288 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 FO:. case P
19289 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 4_INTARRAY:.
1928a 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
1928b 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 O_HANDOFF: {.
1928c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1928d 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 ee(db, p4);.
1928e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1928f 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
19290 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 _VDBEFUNC: {.
19291 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 VdbeFunc *p
19292 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 VdbeFunc = (Vdbe
19293 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 Func *)p4;.
19294 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c freeEphemeral
19295 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 Function(db, pVd
19296 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a beFunc->pFunc);.
19297 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
19298 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
19299 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a (pVdbeFunc, 0);.
1929a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1929b 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 bFree(db, pVdbeF
1929c 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 unc);. br
1929d 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1929e 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 case P4_FUNCD
1929f 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 EF: {. fr
192a0 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 eeEphemeralFunct
192a1 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 ion(db, (FuncDef
192a2 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 *)p4);. b
192a3 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
192a4 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a case P4_MEM:
192a5 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
192a6 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c e3ValueFree((sql
192a7 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b ite3_value*)p4);
192a8 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
192a9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
192aa 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}.../*.** Chan
192ab 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 ge N opcodes sta
192ac 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f rting at addr to
192ad 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 No-ops..*/.SQLI
192ae 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
192af 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
192b0 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c eToNoop(Vdbe *p,
192b1 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e int addr, int N
192b2 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d ){. if( p && p-
192b3 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 >aOp ){. Vdbe
192b4 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f Op *pOp = &p->aO
192b5 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c p[addr];. sql
192b6 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
192b7 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d ;. while( N--
192b8 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 ){. freeP4
192b9 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 (db, pOp->p4type
192ba 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 , pOp->p4.p);.
192bb 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 memset(pOp,
192bc 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 0, sizeof(pOp[0]
192bd 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f ));. pOp->o
192be 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b pcode = OP_Noop;
192bf 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 . pOp++;.
192c0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
192c1 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
192c2 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 e of the P4 oper
192c3 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
192c4 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
192c5 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
192c6 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 is useful when a
192c7 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 large program i
192c8 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a s loaded from a.
192c9 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 ** static array
192ca 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 using sqlite3Vdb
192cb 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 eAddOpList but w
192cc 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 e want to make a
192cd 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 .** few minor ch
192ce 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f anges to the pro
192cf 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e gram..**.** If n
192d0 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 >=0 then the P4
192d1 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d operand is dynam
192d2 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 ic, meaning that
192d3 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 a copy of.** th
192d4 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 e string is made
192d5 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
192d6 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
192d7 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 e3_malloc()..**
192d8 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 A value of n==0
192d9 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 means copy bytes
192da 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e of zP4 up to an
192db 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a d including the.
192dc 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 ** first null by
192dd 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e te. If n>0 then
192de 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 copy n+1 bytes
192df 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 of zP4..**.** If
192e0 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 n==P4_KEYINFO i
192e1 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 t means that zP4
192e2 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
192e3 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
192e4 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 ture..** A copy
192e5 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b is made of the K
192e6 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
192e7 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
192e8 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
192e9 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f lite3_malloc, to
192ea 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 be freed when t
192eb 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c he Vdbe is final
192ec 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b ized..** n==P4_K
192ed 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 EYINFO_HANDOFF i
192ee 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 ndicates that zP
192ef 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 4 points to a Ke
192f0 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a yInfo structure.
192f1 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d ** stored in mem
192f2 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c ory that the cal
192f3 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 ler has obtained
192f4 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
192f5 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 lloc. The .** ca
192f6 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ller should not
192f7 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 free the allocat
192f8 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 ion, it will be
192f9 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
192fa 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 dbe is.** finali
192fb 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 zed..** .** Othe
192fc 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 r values of n (P
192fd 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4_STATIC, P4_COL
192fe 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 LSEQ etc.) indic
192ff 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 ate that zP4 poi
19300 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 nts.** to a stri
19301 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 ng or structure
19302 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 that is guarante
19303 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 ed to exist for
19304 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a the lifetime of.
19305 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 ** the Vdbe. In
19306 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 these cases we c
19307 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 an just copy the
19308 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
19309 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 If addr<0 then c
1930a 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 hange P4 on the
1930b 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e most recently in
1930c 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 serted instructi
1930d 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1930e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1930f 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 e3VdbeChangeP4(V
19310 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
19311 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
19312 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 4, int n){. Op
19313 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 *pOp;. sqlite3
19314 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 *db;. assert( p
19315 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d !=0 );. db = p-
19316 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 >db;. assert( p
19317 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
19318 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 GIC_INIT );. if
19319 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 ( p->aOp==0 || d
1931a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1931b 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20 ){. if (n !=
1931c 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20 P4_KEYINFO) {.
1931d 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e freeP4(db, n
1931e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a , (void*)*(char*
1931f 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 *)&zP4);. }.
19320 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
19321 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d assert( addr<p-
19322 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 >nOp );. if( ad
19323 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 dr<0 ){. addr
19324 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 = p->nOp - 1;.
19325 20 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20 if( addr<0 )
19326 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f return;. }. pO
19327 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 p = &p->aOp[addr
19328 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20 ];. freeP4(db,
19329 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 pOp->p4type, pOp
1932a 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e ->p4.p);. pOp->
1932b 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 p4.p = 0;. if(
1932c 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 n==P4_INT32 ){.
1932d 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73 /* Note: this
1932e 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62 cast is safe, b
1932f 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 ecause the origi
19330 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73 n data point was
19331 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74 an int. ** t
19332 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20 hat was cast to
19333 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 a (const char *)
19334 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34 . */. pOp->p4
19335 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f .i = SQLITE_PTR_
19336 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20 TO_INT(zP4);.
19337 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
19338 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65 4_INT32;. }else
19339 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 if( zP4==0 ){.
1933a 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 pOp->p4.p = 0
1933b 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
1933c 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a e = P4_NOTUSED;.
1933d 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 }else if( n==P
1933e 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 4_KEYINFO ){.
1933f 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
19340 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 fo;. int nFie
19341 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20 ld, nByte;..
19342 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e nField = ((KeyIn
19343 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64 fo*)zP4)->nField
19344 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 ;. nByte = si
19345 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20 zeof(*pKeyInfo)
19346 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a + (nField-1)*siz
19347 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 eof(pKeyInfo->aC
19348 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64 oll[0]) + nField
19349 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d ;. pKeyInfo =
1934a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
1934b 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70 nByte );. pOp
1934c 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 ->p4.pKeyInfo =
1934d 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 pKeyInfo;. if
1934e 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 ( pKeyInfo ){.
1934f 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 u8 *aSortOrd
19350 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 er;. memcpy
19351 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20 (pKeyInfo, zP4,
19352 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53 nByte);. aS
19353 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 ortOrder = pKeyI
19354 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b nfo->aSortOrder;
19355 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74 . if( aSort
19356 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 Order ){.
19357 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
19358 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65 Order = (unsigne
19359 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66 d char*)&pKeyInf
1935a 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d o->aColl[nField]
1935b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
1935c 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 (pKeyInfo->aSort
1935d 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65 Order, aSortOrde
1935e 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 r, nField);.
1935f 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 }. pOp->p
19360 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 4type = P4_KEYIN
19361 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 FO;. }else{.
19362 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c p->db->mall
19363 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
19364 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
19365 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 = P4_NOTUSED;.
19366 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
19367 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 n==P4_KEYINFO_HA
19368 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70 NDOFF ){. pOp
19369 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 ->p4.p = (void*)
1936a 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 zP4;. pOp->p4
1936b 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46 type = P4_KEYINF
1936c 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e O;. }else if( n
1936d 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 <0 ){. pOp->p
1936e 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 4.p = (void*)zP4
1936f 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
19370 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 e = (signed char
19371 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 )n;. }else{.
19372 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 if( n==0 ) n =
19373 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
19374 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 zP4);. pOp->p
19375 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 4.z = sqlite3DbS
19376 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 trNDup(p->db, zP
19377 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 4, n);. pOp->
19378 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 p4type = P4_DYNA
19379 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e MIC;. }.}..#ifn
1937a 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a def NDEBUG./*.**
1937b 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d Change the comm
1937c 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d ent on the the m
1937d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 ost recently cod
1937e 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ed instruction.
1937f 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 Or.** insert a
19380 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 No-op and add th
19381 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 e comment to tha
19382 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f t new instructio
19383 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65 n. This.** make
19384 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 s the code easie
19385 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 r to read during
19386 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e debugging. Non
19387 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e e of this happen
19388 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63 s.** in a produc
19389 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53 tion build..*/.S
1938a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1938b 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f id sqlite3VdbeCo
1938c 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 mment(Vdbe *p, c
1938d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
1938e 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
1938f 69 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74 ist ap;. assert
19390 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d ( p->nOp>0 || p-
19391 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 >aOp==0 );. ass
19392 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c ert( p->aOp==0 |
19393 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d | p->aOp[p->nOp-
19394 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 1].zComment==0 |
19395 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
19396 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
19397 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 ->nOp ){. cha
19398 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 r **pz = &p->aOp
19399 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d [p->nOp-1].zComm
1939a 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 ent;. va_star
1939b 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
1939c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1939d 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 e(p->db, *pz);.
1939e 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 *pz = sqlite3
1939f 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 VMPrintf(p->db,
193a0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
193a1 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
193a2 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.}.SQLITE_PRIVA
193a3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
193a4 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 dbeNoopComment(V
193a5 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 dbe *p, const ch
193a6 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
193a7 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
193a8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
193a9 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 dOp0(p, OP_Noop)
193aa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
193ab 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d Op>0 || p->aOp==
193ac 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
193ad 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 ->aOp==0 || p->a
193ae 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f Op[p->nOp-1].zCo
193af 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 mment==0 || p->d
193b0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
193b1 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 );. if( p->nOp
193b2 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a ){. char **pz
193b3 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f = &p->aOp[p->nO
193b4 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 p-1].zComment;.
193b5 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 va_start(ap,
193b6 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 zFormat);. sq
193b7 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
193b8 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a b, *pz);. *pz
193b9 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
193ba 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 tf(p->db, zForma
193bb 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 t, ap);. va_e
193bc 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 nd(ap);. }.}.#e
193bd 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 ndif /* NDEBUG
193be 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
193bf 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 the opcode for
193c0 61 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e a given address.
193c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
193c2 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 TE VdbeOp *sqlit
193c3 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 e3VdbeGetOp(Vdbe
193c4 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a *p, int addr){.
193c5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
193c6 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
193c7 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 NIT );. assert(
193c8 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 (addr>=0 && add
193c9 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e r<p->nOp) || p->
193ca 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
193cb 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61 );. return ((a
193cc 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 ddr>=0 && addr<p
193cd 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b ->nOp)?(&p->aOp[
193ce 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69 addr]):0);.}..#i
193cf 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
193d0 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 E_OMIT_EXPLAIN)
193d1 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 || !defined(NDEB
193d2 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 UG) \. || de
193d3 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 fined(VDBE_PROFI
193d4 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 LE) || defined(S
193d5 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
193d6 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 ** Compute a str
193d7 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 ing that describ
193d8 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 es the P4 parame
193d9 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 ter for an opcod
193da 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 e..** Use zTemp
193db 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 for any required
193dc 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 temporary buffe
193dd 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 r space..*/.stat
193de 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 ic char *display
193df 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 P4(Op *pOp, char
193e0 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 *zTemp, int nTe
193e1 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 mp){. char *zP4
193e2 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 = zTemp;. asse
193e3 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b rt( nTemp>=20 );
193e4 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e . switch( pOp->
193e5 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 p4type ){. ca
193e6 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 se P4_KEYINFO_ST
193e7 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 ATIC:. case P
193e8 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 4_KEYINFO: {.
193e9 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 int i, j;.
193ea 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
193eb 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Info = pOp->p4.p
193ec 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 KeyInfo;. s
193ed 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
193ee 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b nTemp, zTemp, "k
193ef 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 eyinfo(%d", pKey
193f0 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20 Info->nField);.
193f1 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 i = sqlite3
193f2 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b Strlen30(zTemp);
193f3 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 . for(j=0;
193f4 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 j<pKeyInfo->nFie
193f5 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ld; j++){.
193f6 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
193f7 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f = pKeyInfo->aCo
193f8 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 ll[j];. i
193f9 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 f( pColl ){.
193fa 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 int n = sq
193fb 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 lite3Strlen30(pC
193fc 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 oll->zName);.
193fd 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e if( i+n>n
193fe 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
193ff 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 memcpy(&zT
19400 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 emp[i],",...",4)
19401 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 ;. br
19402 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
19403 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 . zTemp
19404 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 [i++] = ',';.
19405 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 if( pKeyI
19406 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 nfo->aSortOrder
19407 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f && pKeyInfo->aSo
19408 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 rtOrder[j] ){.
19409 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b zTemp[
1940a 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 i++] = '-';.
1940b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1940c 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b memcpy(&zTemp[
1940d 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 i], pColl->zName
1940e 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ,n+1);.
1940f 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 i += n;.
19410 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e }else if( i+4<n
19411 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
19412 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d memcpy(&zTem
19413 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a p[i],",nil",4);.
19414 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 i += 4
19415 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
19416 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b }. zTemp[
19417 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 i++] = ')';.
19418 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a zTemp[i] = 0;.
19419 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c assert( i<
1941a 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 nTemp );. b
1941b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1941c 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a case P4_COLLSEQ:
1941d 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 {. CollSeq
1941e 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 *pColl = pOp->p
1941f 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 4.pColl;. s
19420 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19421 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 nTemp, zTemp, "c
19422 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 ollseq(%.20s)",
19423 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pColl->zName);.
19424 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
19425 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 }. case P4_FU
19426 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 NCDEF: {. F
19427 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 uncDef *pDef = p
19428 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
19429 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1942a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1942b 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 p, "%s(%d)", pDe
1942c 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e f->zName, pDef->
1942d 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 nArg);. bre
1942e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1942f 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 se P4_INT64: {.
19430 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
19431 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
19432 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 mp, "%lld", *pOp
19433 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 ->p4.pI64);.
19434 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
19435 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 case P4_INT32
19436 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
19437 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
19438 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 , zTemp, "%d", p
19439 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 Op->p4.i);.
1943a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1943b 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 case P4_REAL:
1943c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1943d 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1943e 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 zTemp, "%.16g",
1943f 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b *pOp->p4.pReal);
19440 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
19441 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
19442 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d MEM: {. Mem
19443 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 *pMem = pOp->p4
19444 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 .pMem;. ass
19445 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
19446 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
19447 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d );. if( pM
19448 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
19449 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a Str ){. z
1944a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 P4 = pMem->z;.
1944b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d }else if( pM
1944c 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1944d 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Int ){. s
1944e 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1944f 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 nTemp, zTemp, "%
19450 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 lld", pMem->u.i)
19451 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
19452 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
19453 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
19454 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
19455 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
19456 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d p, "%.16g", pMem
19457 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ->r);. }.
19458 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
19459 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1945a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1945b 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 E. case P4_VT
1945c 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AB: {. sqli
1945d 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 te3_vtab *pVtab
1945e 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b = pOp->p4.pVtab;
1945f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
19460 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
19461 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 Temp, "vtab:%p:%
19462 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 p", pVtab, pVtab
19463 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 ->pModule);.
19464 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
19465 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 endif. case P
19466 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 4_INTARRAY: {.
19467 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
19468 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
19469 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a p, "intarray");.
1946a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1946b 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
1946c 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f {. zP4 = pO
1946d 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 p->p4.z;. i
1946e 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 f( zP4==0 ){.
1946f 20 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 zP4 = zTemp
19470 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b ;. zTemp[
19471 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0] = 0;. }.
19472 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
19473 72 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 rt( zP4!=0 );.
19474 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 return zP4;.}.#e
19475 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c ndif../*.** Decl
19476 61 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 are to the Vdbe
19477 74 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f that the BTree o
19478 62 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 bject at db->aDb
19479 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a [i] is used..**.
1947a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1947b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1947c 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 beUsesBtree(Vdbe
1947d 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 *p, int i){. i
1947e 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72 nt mask;. asser
1947f 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
19480 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69 7a db->nDb && i<siz
19481 65 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20 20 eof(u32)*8 );.
19482 61 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 73 assert( i<(int)s
19483 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 izeof(p->btreeMa
19484 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20 sk)*8 );. mask
19485 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a 20 = ((u32)1)<<i;.
19486 20 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 if( (p->btreeMa
19487 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b sk & mask)==0 ){
19488 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73 . p->btreeMas
19489 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73 k |= mask;. s
1948a 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
1948b 41 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e ArrayInsert(&p->
1948c 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 aMutex, p->db->a
1948d 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a Db[i].pBt);. }.
1948e 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 }...#if defined(
1948f 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c VDBE_PROFILE) ||
19490 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
19491 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 DEBUG)./*.** Pri
19492 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f nt a single opco
19493 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e de. This routin
19494 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 e is used for de
19495 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f bugging only..*/
19496 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19497 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19498 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f PrintOp(FILE *pO
19499 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a ut, int pc, Op *
1949a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 pOp){. char *zP
1949b 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35 4;. char zPtr[5
1949c 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 0];. static con
1949d 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
1949e 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25 1 = "%4d %-13s %
1949f 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20 4d %4d %4d %-4s
194a0 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66 %.2X %s\n";. if
194a1 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74 ( pOut==0 ) pOut
194a2 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34 = stdout;. zP4
194a3 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 = displayP4(pOp
194a4 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a , zPtr, sizeof(z
194a5 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66 Ptr));. fprintf
194a6 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c (pOut, zFormat1,
194a7 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 pc, . sqli
194a8 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f te3OpcodeName(pO
194a9 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d p->opcode), pOp-
194aa 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f >p1, pOp->p2, pO
194ab 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d p->p3, zP4, pOp-
194ac 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 >p5,.#ifdef SQLI
194ad 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 TE_DEBUG. p
194ae 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 Op->zComment ? p
194af 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 Op->zComment : "
194b0 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22 ".#else. ""
194b1 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66 .#endif. );. f
194b2 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 flush(pOut);.}.#
194b3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c endif../*.** Rel
194b4 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66 ease an array of
194b5 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a N Mem elements.
194b6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
194b7 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d eleaseMemArray(M
194b8 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 em *p, int N){.
194b9 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20 if( p && N ){.
194ba 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 Mem *pEnd;.
194bb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
194bc 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d 61 p->db;. u8 ma
194bd 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62 lloc_failed = db
194be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a ->mallocFailed;.
194bf 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b for(pEnd=&p[
194c0 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 N]; p<pEnd; p++)
194c1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
194c2 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c (&p[1])==pEnd ||
194c3 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 p[0].db==p[1].d
194c4 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 b );.. /* T
194c5 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61 his block is rea
194c6 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76 lly an inlined v
194c7 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 ersion of sqlite
194c8 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
194c9 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ). ** that
194ca 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20 takes advantage
194cb 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74 of the fact that
194cc 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
194cd 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20 value is .
194ce 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f ** being set to
194cf 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65 NULL after rele
194d0 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69 asing any dynami
194d1 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20 c resources..
194d2 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 **. ** T
194d3 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e he justification
194d4 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67 for duplicating
194d5 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63 code is that ac
194d6 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20 cording to .
194d7 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20 ** callgrind,
194d8 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65 this causes a ce
194d9 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20 rtain test case
194da 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34 to hit the CPU 4
194db 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72 .7 . ** per
194dc 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c cent less (x86 l
194dd 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f inux, gcc versio
194de 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68 n 4.1.2, -O6) th
194df 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20 an if . **
194e0 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73 sqlite3MemReleas
194e1 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20 e() were called
194e2 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20 from here. With
194e3 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a -O2, this jumps.
194e4 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20 ** to 6.6
194e5 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73 percent. The tes
194e6 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74 t case is insert
194e7 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e ing 1000 rows in
194e8 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20 to a table .
194e9 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64 ** with no ind
194ea 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e exes using a sin
194eb 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53 gle prepared INS
194ec 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 ERT statement, b
194ed 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20 ind() . **
194ee 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73 and reset(). Ins
194ef 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64 erts are grouped
194f0 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74 into a transact
194f1 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
194f2 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 if( p->flags
194f3 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 &(MEM_Agg|MEM_Dy
194f4 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 n) ){. sq
194f5 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
194f6 61 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 ase(p);. }e
194f7 6c 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c lse if( p->zMall
194f8 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 oc ){. sq
194f9 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
194fa 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 p->zMalloc);.
194fb 20 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 p->zMalloc
194fc 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 = 0;. }..
194fd 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d p->flags = M
194fe 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 EM_Null;. }.
194ff 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
19500 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 led = malloc_fai
19501 6c 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 led;. }.}..#ifd
19502 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
19503 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
19504 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 NT.SQLITE_PRIVAT
19505 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19506 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 eReleaseBuffers(
19507 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
19508 69 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 ii;. int nFree
19509 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 = 0;. assert( s
1950a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1950b 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
1950c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 );. for(ii=1; i
1950d 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b i<=p->nMem; ii++
1950e 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d ){. Mem *pMem
1950f 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b = &p->aMem[ii];
19510 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 . if( pMem->f
19511 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19512 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 t ){. sqlit
19513 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d e3RowSetClear(pM
19514 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a em->u.pRowSet);.
19515 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4d }. if( pM
19516 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 em->z && pMem->f
19517 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a lags&MEM_Dyn ){.
19518 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
19519 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 Mem->xDel );.
1951a 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 nFree += sqli
1951b 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
1951c 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e pMem->db, pMem->
1951d 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 z);. sqlite
1951e 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1951f 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pMem);. }. }
19520 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b . return nFree;
19521 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
19522 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 ef SQLITE_OMIT_E
19523 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 XPLAIN./*.** Giv
19524 65 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74 e a listing of t
19525 68 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68 he program in th
19526 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
19527 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 e..**.** The int
19528 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 erface is the sa
19529 6d 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62 me as sqlite3Vdb
1952a 65 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e eExec(). But in
1952b 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e stead of.** runn
1952c 69 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74 ing the code, it
1952d 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c invokes the cal
1952e 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 lback once for e
1952f 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ach instruction.
19530 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65 .** This feature
19531 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c is used to impl
19532 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e ement "EXPLAIN".
19533 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 .**.** When p->e
19534 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20 xplain==1, each
19535 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c instruction is l
19536 69 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 isted. When.**
19537 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f p->explain==2, o
19538 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69 nly OP_Explain i
19539 6e 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20 nstructions are
1953a 6c 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65 listed and these
1953b 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e .** are shown in
1953c 20 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 a different for
1953d 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e mat. p->explain
1953e 3d 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69 ==2 is used to i
1953f 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c mplement.** EXPL
19540 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a AIN QUERY PLAN..
19541 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19542 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19543 65 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70 eList(. Vdbe *p
19544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19545 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a /* The VDBE *
19546 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a /.){. sqlite3 *
19547 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e db = p->db;. in
19548 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t i;. int rc =
19549 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
1954a 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 *pMem = p->pRes
1954b 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 ultSet = &p->aMe
1954c 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 m[1];.. assert(
1954d 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 p->explain );.
1954e 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 if( p->magic!=V
1954f 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20 DBE_MAGIC_RUN )
19550 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
19551 53 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20 SUSE;. assert(
19552 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
19553 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a E_MAGIC_BUSY );.
19554 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d assert( p->rc=
19555 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d =SQLITE_OK || p-
19556 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 >rc==SQLITE_BUSY
19557 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 || p->rc==SQLIT
19558 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a E_NOMEM );.. /*
19559 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 Even though thi
1955a 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f s opcode does no
1955b 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 t use dynamic st
1955c 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 rings for. ** t
1955d 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c he result, resul
1955e 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 t columns may be
1955f 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 come dynamic if
19560 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 the user calls.
19561 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
19562 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 mn_text16(), cau
19563 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 sing a translati
19564 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 on to UTF-16 enc
19565 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 oding.. */. re
19566 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d leaseMemArray(pM
19567 65 6d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20 em, p->nMem);..
19568 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
19569 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
1956a 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
1956b 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e if a malloc() in
1956c 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 side a call to s
1956d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1956e 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 xt() or. ** s
1956f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
19570 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 xt16() failed.
19571 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f */. db->mallo
19572 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
19573 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
19574 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b RROR;. }.. do{
19575 0a 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b . i = p->pc++
19576 3b 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d ;. }while( i<p-
19577 3e 6e 4f 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 >nOp && p->expla
19578 69 6e 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b in==2 && p->aOp[
19579 69 5d 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 i].opcode!=OP_Ex
1957a 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 plain );. if( i
1957b 3e 3d 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 >=p->nOp ){.
1957c 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
1957d 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 K;. rc = SQLI
1957e 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 TE_DONE;. }else
1957f 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e if( db->u1.isIn
19580 74 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 terrupted ){.
19581 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
19582 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 72 INTERRUPT;. r
19583 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
19584 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 ;. sqlite3Set
19585 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
19586 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
19587 6c 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72 lite3ErrStr(p->r
19588 63 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 c));. }else{.
19589 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f char *z;. O
1958a 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 p *pOp = &p->aOp
1958b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e [i];. if( p->
1958c 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 explain==1 ){.
1958d 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1958e 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
1958f 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
19590 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 LITE_INTEGER;.
19591 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
19592 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
19593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19594 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 /* Program cou
19595 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d nter */. pM
19596 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 em++;. . p
19597 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
19598 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c _Static|MEM_Str|
19599 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 MEM_Term;.
1959a 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a pMem->z = (char*
1959b 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 )sqlite3OpcodeNa
1959c 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b me(pOp->opcode);
1959d 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 /* Opcode */.
1959e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 assert( pMe
1959f 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 m->z!=0 );.
195a0 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 pMem->n = sqlit
195a1 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d e3Strlen30(pMem-
195a2 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d >z);. pMem-
195a3 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 >type = SQLITE_T
195a4 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d EXT;. pMem-
195a5 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 >enc = SQLITE_UT
195a6 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b F8;. pMem++
195a7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65 ;. }.. pMe
195a8 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
195a9 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e nt;. pMem->u.
195aa 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 i = pOp->p1;
195ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195ac 20 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 /* P1 */.
195ad 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
195ae 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
195af 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 pMem++;..
195b0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
195b1 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d EM_Int;. pMem
195b2 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b ->u.i = pOp->p2;
195b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20 /* P2
195b5 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 */. pMem->typ
195b6 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 e = SQLITE_INTEG
195b7 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a ER;. pMem++;.
195b8 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c . if( p->expl
195b9 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 ain==1 ){.
195ba 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
195bb 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 M_Int;. pMe
195bc 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 m->u.i = pOp->p3
195bd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
195be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 /* P3
195bf 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
195c0 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e type = SQLITE_IN
195c1 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 TEGER;. pMe
195c2 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 m++;. }..
195c3 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
195c4 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c emGrow(pMem, 32,
195c5 20 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 0) ){
195c6 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20 /* P4 */.
195c7 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e assert( p->db->
195c8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
195c9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
195ca 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d ITE_ERROR;. }
195cb 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
195cc 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 = MEM_Dyn|MEM_S
195cd 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 tr|MEM_Term;.
195ce 20 7a 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 z = displayP4(p
195cf 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 Op, pMem->z, 32)
195d0 3b 0a 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65 ;. if( z!=pMe
195d1 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 m->z ){. sq
195d2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
195d3 74 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 tr(pMem, z, -1,
195d4 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b SQLITE_UTF8, 0);
195d5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
195d6 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
195d7 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d z!=0 );. pM
195d8 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 em->n = sqlite3S
195d9 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 trlen30(pMem->z)
195da 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e ;. pMem->en
195db 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
195dc 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
195dd 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 >type = SQLITE_T
195de 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b EXT;. pMem++;
195df 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 .. if( p->exp
195e0 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 lain==1 ){.
195e1 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
195e2 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c MemGrow(pMem, 4,
195e3 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 0) ){. a
195e4 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 ssert( p->db->ma
195e5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
195e6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
195e7 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
195e8 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 }. pMem->f
195e9 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d lags = MEM_Dyn|M
195ea 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Str|MEM_Term;
195eb 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
195ec 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2;. sqlite
195ed 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 3_snprintf(3, pM
195ee 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 em->z, "%.2x", p
195ef 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 Op->p5); /* P5
195f0 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
195f1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
195f2 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
195f3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
195f4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
195f5 0a 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 . .#ifdef SQLIT
195f6 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 E_DEBUG. if
195f7 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 ( pOp->zComment
195f8 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d ){. pMem-
195f9 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 >flags = MEM_Str
195fa 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 |MEM_Term;.
195fb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 pMem->z = pOp
195fc 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 ->zComment;.
195fd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
195fe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
195ff 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20 em->z);.
19600 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
19601 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20 TE_UTF8;.
19602 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
19603 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 LITE_TEXT;.
19604 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 }else.#endif.
19605 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d {. pM
19606 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
19607 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Null;
19608 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
19609 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 omment */.
1960a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1960b 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 QLITE_NULL;.
1960c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 }. }.. p
1960d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 ->nResColumn = 8
1960e 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e - 5*(p->explain
1960f 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d -1);. p->rc =
19610 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
19611 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b rc = SQLITE_ROW;
19612 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
19613 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
19614 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
19615 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c N */..#ifdef SQL
19616 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
19617 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68 Print the SQL th
19618 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67 at was used to g
19619 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70 enerate a VDBE p
1961a 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 rogram..*/.SQLIT
1961b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1961c 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 qlite3VdbePrintS
1961d 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 ql(Vdbe *p){. i
1961e 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b nt nOp = p->nOp;
1961f 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a . VdbeOp *pOp;.
19620 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 if( nOp<1 ) re
19621 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 turn;. pOp = &p
19622 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 ->aOp[0];. if(
19623 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
19624 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 Trace && pOp->p4
19625 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e .z!=0 ){. con
19626 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 st char *z = pOp
19627 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c ->p4.z;. whil
19628 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 e( sqlite3Isspac
19629 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 e(*z) ) z++;.
1962a 20 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 printf("SQL: [%
1962b 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d s]\n", z);. }.}
1962c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 .#endif..#if !de
1962d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1962e 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 T_TRACE) && defi
1962f 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
19630 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a E_IOTRACE)./*.**
19631 20 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 Print an IOTRAC
19632 45 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e E message showin
19633 67 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a g SQL content..*
19634 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19635 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19636 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 eIOTraceSql(Vdbe
19637 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 *p){. int nOp
19638 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 = p->nOp;. Vdbe
19639 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 Op *pOp;. if( s
1963a 71 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 qlite3IoTrace==0
1963b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
1963c 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b nOp<1 ) return;
1963d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 . pOp = &p->aOp
1963e 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e [0];. if( pOp->
1963f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 opcode==OP_Trace
19640 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 && pOp->p4.z!=0
19641 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a ){. int i, j
19642 3b 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 ;. char z[100
19643 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0];. sqlite3_
19644 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
19645 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 z), z, "%s", pOp
19646 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 ->p4.z);. for
19647 28 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 73 (i=0; sqlite3Iss
19648 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 pace(z[i]); i++)
19649 7b 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 {}. for(j=0;
1964a 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 z[i]; i++){.
1964b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 73 if( sqlite3Iss
1964c 70 61 63 65 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 pace(z[i]) ){.
1964d 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d if( z[i-1]
1964e 21 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 !=' ' ){.
1964f 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b z[j++] = ' ';
19650 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19651 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19652 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 z[j++] = z[i];.
19653 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
19654 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 z[j] = 0;. s
19655 71 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 qlite3IoTrace("S
19656 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 QL %s\n", z);.
19657 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 }.}.#endif /* !S
19658 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
19659 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
1965a 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a E_IOTRACE */../*
1965b 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 .** Allocate spa
1965c 63 65 20 66 72 6f 6d 20 61 20 66 69 78 65 64 20 ce from a fixed
1965d 73 69 7a 65 20 62 75 66 66 65 72 2e 20 20 4d 61 size buffer. Ma
1965e 6b 65 20 2a 70 70 20 70 6f 69 6e 74 20 74 6f 20 ke *pp point to
1965f 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 the.** allocated
19660 20 73 70 61 63 65 2e 20 20 28 4e 6f 74 65 3a 20 space. (Note:
19661 20 70 70 20 69 73 20 61 20 63 68 61 72 2a 20 72 pp is a char* r
19662 61 74 68 65 72 20 74 68 61 6e 20 61 20 76 6f 69 ather than a voi
19663 64 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61 d** to.** work a
19664 72 6f 75 6e 64 20 74 68 65 20 70 6f 69 6e 74 65 round the pointe
19665 72 20 61 6c 69 61 73 69 6e 67 20 72 75 6c 65 73 r aliasing rules
19666 20 6f 66 20 43 2e 29 20 20 2a 70 70 20 73 68 6f of C.) *pp sho
19667 75 6c 64 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a uld initially.**
19668 20 62 65 20 7a 65 72 6f 2e 20 20 49 66 20 2a 70 be zero. If *p
19669 70 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 p is not zero, t
1966a 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 hat means that t
1966b 68 65 20 73 70 61 63 65 20 68 61 73 20 61 6c 72 he space has alr
1966c 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c eady.** been all
1966d 6f 63 61 74 65 64 20 61 6e 64 20 74 68 69 73 20 ocated and this
1966e 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 6f routine is a noo
1966f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20 69 p..**.** nByte i
19670 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
19671 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e bytes of space n
19672 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 eeded..**.** *pp
19673 46 72 6f 6d 20 70 6f 69 6e 74 20 74 6f 20 61 76 From point to av
19674 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 61 6e ailable space an
19675 64 20 70 45 6e 64 20 70 6f 69 6e 74 73 20 74 6f d pEnd points to
19676 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
19677 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 ** available spa
19678 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74 ce..**.** *pnByt
19679 65 20 69 73 20 61 20 63 6f 75 6e 74 65 72 20 6f e is a counter o
1967a 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
1967b 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 74 bytes of space t
1967c 68 61 74 20 68 61 76 65 20 66 61 69 6c 65 64 0a hat have failed.
1967d 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 ** to allocate.
1967e 20 49 66 20 74 68 65 72 65 20 69 73 20 69 6e 73 If there is ins
1967f 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 ufficient space
19680 69 6e 20 2a 70 70 46 72 6f 6d 20 74 6f 20 73 61 in *ppFrom to sa
19681 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 72 65 71 tisfy the.** req
19682 75 65 73 74 2c 20 74 68 65 6e 20 69 6e 63 72 65 uest, then incre
19683 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20 62 79 20 ment *pnByte by
19684 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 the amount of th
19685 65 20 72 65 71 75 65 73 74 2e 0a 2a 2f 0a 73 74 e request..*/.st
19686 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 53 atic void allocS
19687 70 61 63 65 28 0a 20 20 63 68 61 72 20 2a 70 70 pace(. char *pp
19688 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
19689 49 4e 2f 4f 55 54 3a 20 53 65 74 20 2a 70 70 20 IN/OUT: Set *pp
1968a 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f to point to allo
1968b 63 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0a cated buffer */.
1968c 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
1968d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1968e 20 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c of bytes to all
1968f 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a ocate */. u8 **
19690 70 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 ppFrom,
19691 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 /* IN/OUT: Alloc
19692 61 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d ate from *ppFrom
19693 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20 */. u8 *pEnd,
19694 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
19695 69 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20 inter to 1 byte
19696 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
19697 2a 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a *ppFrom buffer *
19698 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20 /. int *pnByte
19699 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 /* If a
1969a 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 llocation cannot
1969b 20 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d be made, increm
1969c 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 ent *pnByte */.)
1969d 7b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 {. assert( EIGH
1969e 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
1969f 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69 (*ppFrom) );. i
196a0 66 28 20 28 2a 28 76 6f 69 64 2a 2a 29 70 70 29 f( (*(void**)pp)
196a1 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 ==0 ){. nByte
196a2 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 = ROUND8(nByte)
196a3 3b 0a 20 20 20 20 69 66 28 20 28 70 45 6e 64 20 ;. if( (pEnd
196a4 2d 20 2a 70 70 46 72 6f 6d 29 3e 3d 6e 42 79 74 - *ppFrom)>=nByt
196a5 65 20 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69 e ){. *(voi
196a6 64 2a 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a d**)pp = (void *
196a7 29 2a 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 )*ppFrom;.
196a8 2a 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 *ppFrom += nByte
196a9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
196aa 20 20 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 *pnByte += nB
196ab 79 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d yte;. }. }.}
196ac 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 ../*.** Prepare
196ad 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e a virtual machin
196ae 65 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e e for execution.
196af 20 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 This involves
196b0 74 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 things such.** a
196b1 73 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 s allocating sta
196b2 63 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 ck space and ini
196b3 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 tializing the pr
196b4 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a ogram counter..*
196b5 2a 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45 * After the VDBE
196b6 20 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c has be prepped,
196b7 20 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 it can be execu
196b8 74 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f ted by one or mo
196b9 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 re.** calls to s
196ba 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 qlite3VdbeExec()
196bb 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 . .**.** This i
196bc 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 s the only way t
196bd 6f 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72 o move a VDBE fr
196be 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e om VDBE_MAGIC_IN
196bf 49 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 IT to.** VDBE_MA
196c0 47 49 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 GIC_RUN..**.** T
196c1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
196c2 20 62 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 be called more
196c3 74 68 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73 than once on a s
196c4 69 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61 ingle virtual ma
196c5 63 68 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69 chine..** The fi
196c6 72 73 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65 rst call is made
196c7 20 77 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 while compiling
196c8 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
196c9 6e 74 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a nt. Subsequent.*
196ca 2a 20 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 * calls are made
196cb 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
196cc 70 72 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74 process of reset
196cd 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 ting a statement
196ce 20 74 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65 to be.** re-exe
196cf 63 75 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61 cuted (from a ca
196d0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 ll to sqlite3_re
196d1 73 65 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72 set()). The nVar
196d2 2c 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 , nMem, nCursor
196d3 0a 2a 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69 .** and isExplai
196d4 6e 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 n parameters are
196d5 20 6f 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72 only passed cor
196d6 72 65 63 74 20 76 61 6c 75 65 73 20 74 68 65 20 rect values the
196d7 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 first time.** th
196d8 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 e function is ca
196d9 6c 6c 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75 lled. On subsequ
196da 65 6e 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 ent calls, from
196db 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c sqlite3_reset(),
196dc 20 6e 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73 nVar.** is pass
196dd 65 64 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 ed -1 and nMem,
196de 6e 43 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78 nCursor and isEx
196df 70 6c 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61 plain are all pa
196e0 73 73 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 ssed zero..*/.SQ
196e1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
196e2 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b d sqlite3VdbeMak
196e3 65 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a eReady(. Vdbe *
196e4 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
196e5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
196e6 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 VDBE */. int nV
196e7 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ar,
196e8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
196e9 65 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e er of '?' see in
196ea 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
196eb 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d nt */. int nMem
196ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
196ed 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
196ee 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 of memory cells
196ef 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a to allocate */.
196f0 20 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 int nCursor,
196f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
196f2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 /* Number of cu
196f3 72 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 rsors to allocat
196f4 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70 e */. int isExp
196f5 6c 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 lain
196f6 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
196f7 20 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79 the EXPLAIN key
196f8 77 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74 words is present
196f9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a */.){. int n;.
196fa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
196fb 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 p->db;.. assert
196fc 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
196fd 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 rt( p->magic==VD
196fe 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b BE_MAGIC_INIT );
196ff 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f .. /* There sho
19700 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 uld be at least
19701 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f one opcode.. */
19702 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f . assert( p->nO
19703 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 p>0 );.. /* Set
19704 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 the magic to VD
19705 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f BE_MAGIC_RUN soo
19706 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 ner rather than
19707 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d later. */. p->m
19708 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
19709 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 C_RUN;.. /* For
1970a 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 each cursor req
1970b 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f uired, also allo
1970c 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 cate a memory ce
1970d 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 ll. Memory. **
1970e 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 cells (nMem+1-nC
1970f 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e ursor)..nMem, in
19710 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 clusive, will ne
19711 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 ver be used by.
19712 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f ** the vdbe pro
19713 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 gram. Instead th
19714 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ey are used to a
19715 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
19716 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f r. ** VdbeCurso
19717 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 r/BtCursor struc
19718 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 tures. The blob
19719 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 of memory associ
1971a 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 ated with . **
1971b 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 cursor 0 is stor
1971c 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c ed in memory cel
1971d 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 l nMem. Memory c
1971e 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a ell (nMem-1). *
1971f 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f * stores the blo
19720 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f b of memory asso
19721 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 ciated with curs
19722 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a or 1, etc.. **.
19723 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 ** See also: a
19724 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e llocateCursor().
19725 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 . */. nMem +=
19726 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 nCursor;.. /* A
19727 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
19728 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 r memory registe
19729 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 rs, SQL variable
1972a 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 s, VDBE cursors
1972b 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 and . ** an arr
1972c 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 ay to marshal SQ
1972d 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d L function argum
1972e 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 ents in. This is
1972f 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 only done the.
19730 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 ** first time t
19731 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
19732 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 called for a giv
19733 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 en VDBE, not whe
19734 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 n it is. ** bei
19735 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 ng called from s
19736 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 qlite3_reset() t
19737 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 o reset the virt
19738 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a ual machine.. *
19739 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 /. if( nVar>=0
1973a 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 && !db->mallocFa
1973b 69 6c 65 64 20 29 7b 0a 20 20 20 20 75 38 20 2a iled ){. u8 *
1973c 7a 43 73 72 20 3d 20 28 75 38 20 2a 29 26 70 2d zCsr = (u8 *)&p-
1973d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 >aOp[p->nOp];.
1973e 20 20 75 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38 u8 *zEnd = (u8
1973f 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f *)&p->aOp[p->nO
19740 70 41 6c 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74 pAlloc];. int
19741 20 6e 42 79 74 65 3b 0a 20 20 20 20 69 6e 74 20 nByte;. int
19742 6e 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d nArg; /* M
19743 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
19744 20 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 args passed to
19745 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e a user function.
19746 20 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 */. resolveP
19747 32 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 2Values(p, &nArg
19748 29 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 );. if( isExp
19749 6c 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 lain && nMem<10
1974a 29 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 ){. nMem =
1974b 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 10;. }. zC
1974c 73 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 sr += (zCsr - (u
1974d 38 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73 8*)0)&7;. ass
1974e 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1974f 41 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 ALIGNMENT(zCsr)
19750 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 6e 64 3c );. if( zEnd<
19751 7a 43 73 72 20 29 20 7a 45 6e 64 20 3d 20 7a 43 zCsr ) zEnd = zC
19752 73 72 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 sr;.. do {.
19753 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c memset(zCsr,
19754 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 0, zEnd-zCsr);.
19755 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30 3b nByte = 0;
19756 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 . allocSpac
19757 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d 65 e((char*)&p->aMe
19758 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d m, nMem*sizeof(M
19759 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 em), &zCsr, zEnd
1975a 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 , &nByte);.
1975b 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 allocSpace((cha
1975c 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56 61 r*)&p->aVar, nVa
1975d 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 r*sizeof(Mem), &
1975e 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 zCsr, zEnd, &nBy
1975f 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 te);. alloc
19760 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d Space((char*)&p-
19761 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69 7a >apArg, nArg*siz
19762 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73 72 eof(Mem*), &zCsr
19763 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b , zEnd, &nByte);
19764 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 . allocSpac
19765 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a 56 e((char*)&p->azV
19766 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 ar, nVar*sizeof(
19767 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a char*), &zCsr, z
19768 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 End, &nByte);.
19769 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 allocSpace((
1976a 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72 2c char*)&p->apCsr,
1976b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1976c 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65 6f nCursor*sizeo
1976d 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c 20 f(VdbeCursor*),
1976e 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 &zCsr, zEnd, &nB
1976f 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 yte. );.
19770 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a if( nByte ){.
19771 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65 65 p->pFree
19772 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
19773 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 29 ocRaw(db, nByte)
19774 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
19775 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b zCsr = p->pFree;
19776 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a . zEnd = &z
19777 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20 Csr[nByte];.
19778 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26 26 }while( nByte &&
19779 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
1977a 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43 ed );.. p->nC
1977b 75 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72 3b ursor = nCursor;
1977c 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 . if( p->aVar
1977d 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 ){. p->nVa
1977e 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 r = nVar;.
1977f 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b for(n=0; n<nVar;
19780 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 n++){. p
19781 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 ->aVar[n].flags
19782 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
19783 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 p->aVar[n].d
19784 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a b = db;. }.
19785 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d }. if( p-
19786 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 >aMem ){. p
19787 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 ->aMem--;
19788 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19789 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 * aMem[] goes fr
1978a 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 om 1..nMem */.
1978b 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d p->nMem = nM
1978c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1978d 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 /* not
1978e 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 from 0..nMem-1
1978f 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 */. for(n=1
19790 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b ; n<=nMem; n++){
19791 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d . p->aMem
19792 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f [n].flags = MEM_
19793 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d Null;. p-
19794 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 >aMem[n].db = db
19795 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
19796 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
19797 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d E_DEBUG. for(n=
19798 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 1; n<p->nMem; n+
19799 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
1979a 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 p->aMem[n].db==d
1979b 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a b );. }.#endif.
1979c 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 . p->pc = -1;.
1979d 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
1979e 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 OK;. p->errorAc
1979f 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b tion = OE_Abort;
197a0 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d . p->explain |=
197a1 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d isExplain;. p-
197a2 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
197a3 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 GIC_RUN;. p->nC
197a4 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e hange = 0;. p->
197a5 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 cacheCtr = 1;.
197a6 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
197a7 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 ormat = 255;. p
197a8 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 ->iStatement = 0
197a9 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ;.#ifdef VDBE_PR
197aa 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e OFILE. {. in
197ab 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
197ac 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 ; i<p->nOp; i++)
197ad 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 {. p->aOp[i
197ae 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 ].cnt = 0;.
197af 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 p->aOp[i].cycle
197b0 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d s = 0;. }. }
197b1 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
197b2 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75 Close a VDBE cu
197b3 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65 rsor and release
197b4 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63 all the resourc
197b5 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a es that cursor .
197b6 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f ** happens to ho
197b7 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ld..*/.SQLITE_PR
197b8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
197b9 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 e3VdbeFreeCursor
197ba 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75 (Vdbe *p, VdbeCu
197bb 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 rsor *pCx){. if
197bc 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 ( pCx==0 ){.
197bd 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 return;. }. if
197be 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 ( pCx->pBt ){.
197bf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
197c0 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 ose(pCx->pBt);.
197c1 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 /* The pCx->p
197c2 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 Cursor will be c
197c3 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c lose automatical
197c4 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 ly, if it exists
197c5 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 , by. ** the
197c6 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 call above. */.
197c7 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e }else if( pCx->
197c8 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 pCursor ){. s
197c9 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
197ca 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 Cursor(pCx->pCur
197cb 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 sor);. }.#ifnde
197cc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
197cd 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 RTUALTABLE. if(
197ce 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f pCx->pVtabCurso
197cf 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
197d0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
197d1 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d tabCursor = pCx-
197d2 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 >pVtabCursor;.
197d3 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
197d4 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 module *pModule
197d5 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a = pCx->pModule;.
197d6 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
197d7 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f hod = 1;. (vo
197d8 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
197d9 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 Off(p->db);.
197da 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 pModule->xClose(
197db 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 pVtabCursor);.
197dc 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
197dd 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a afetyOn(p->db);.
197de 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
197df 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e hod = 0;. }.#en
197e0 64 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d 3e dif. if( !pCx->
197e1 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 ephemPseudoTable
197e2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
197e3 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 78 bFree(p->db, pCx
197e4 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a ->pData);. }.}.
197e5 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c ./*.** Close all
197e6 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20 cursors except
197e7 66 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72 73 for VTab cursors
197e8 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e that are curren
197e9 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a 2a tly.** in use..*
197ea 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c /.static void cl
197eb 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 oseAllCursorsExc
197ec 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 56 eptActiveVtabs(V
197ed 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
197ee 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 ;. if( p->apCsr
197ef 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
197f0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 for(i=0; i<p->nC
197f1 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 ursor; i++){.
197f2 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 VdbeCursor *pC
197f3 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
197f4 20 20 20 69 66 28 20 70 43 20 26 26 20 28 21 70 if( pC && (!p
197f5 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 7c ->inVtabMethod |
197f6 7c 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72 73 | !pC->pVtabCurs
197f7 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c or) ){. sql
197f8 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
197f9 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20 20 or(p, pC);.
197fa 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 p->apCsr[i] = 0
197fb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
197fc 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 *.** Clean up th
197fd 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75 e VM after execu
197fe 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
197ff 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 routine will au
19800 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 tomatically clos
19801 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c e any cursors, l
19802 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 ists, and/or.**
19803 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72 sorters that wer
19804 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 e left open. It
19805 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68 also deletes th
19806 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 e values of.** v
19807 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 ariables in the
19808 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f aVar[] array..*/
19809 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 .static void Cle
1980a 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 anup(Vdbe *p){.
1980b 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 int i;. sqlite
1980c 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 3 *db = p->db;.
1980d 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63 6c Mem *pMem;. cl
1980e 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 oseAllCursorsExc
1980f 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 eptActiveVtabs(p
19810 29 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26 70 );. for(pMem=&p
19811 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b 20 ->aMem[1], i=1;
19812 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 2c i<=p->nMem; i++,
19813 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 pMem++){. if
19814 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
19815 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 MEM_RowSet ){.
19816 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 sqlite3RowSe
19817 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 tClear(pMem->u.p
19818 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 RowSet);. }.
19819 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1981a 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pMem, MEM_Null
1981b 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 );. }. release
1981c 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 MemArray(&p->aMe
1981d 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a m[1], p->nMem);.
1981e 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 if( p->context
1981f 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c Stack ){. sql
19820 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
19821 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29 3b ->contextStack);
19822 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 . }. p->contex
19823 74 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70 2d tStack = 0;. p-
19824 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 >contextStackDep
19825 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e th = 0;. p->con
19826 74 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20 30 textStackTop = 0
19827 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
19828 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
19829 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 );. p->zErrMsg
1982a 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c = 0;. p->pResul
1982b 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a tSet = 0;.}../*.
1982c 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
1982d 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 r of result colu
1982e 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 mns that will be
1982f 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
19830 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 s SQL.** stateme
19831 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 nt. This is now
19832 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 set at compile t
19833 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e ime, rather than
19834 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 during.** execu
19835 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65 tion of the vdbe
19836 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 program so that
19837 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
19838 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 count() can.** b
19839 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 e called on an S
1983a 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66 QL statement bef
1983b 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ore sqlite3_step
1983c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
1983d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1983e 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
1983f 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 (Vdbe *p, int nR
19840 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d esColumn){. Mem
19841 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e *pColName;. in
19842 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a t n;. sqlite3 *
19843 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 db = p->db;.. r
19844 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
19845 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e ->aColName, p->n
19846 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d ResColumn*COLNAM
19847 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 E_N);. sqlite3D
19848 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f bFree(db, p->aCo
19849 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 lName);. n = nR
1984a 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 esColumn*COLNAME
1984b 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c _N;. p->nResCol
1984c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e umn = nResColumn
1984d 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20 ;. p->aColName
1984e 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65 = pColName = (Me
1984f 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c m*)sqlite3DbMall
19850 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
19851 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69 66 f(Mem)*n );. if
19852 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30 ( p->aColName==0
19853 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 ) return;. whi
19854 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 le( n-- > 0 ){.
19855 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 pColName->fla
19856 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
19857 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20 pColName->db
19858 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43 6f = p->db;. pCo
19859 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a lName++;. }.}..
1985a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 61 /*.** Set the na
1985b 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27 74 68 me of the idx'th
1985c 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72 65 column to be re
1985d 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 51 turned by the SQ
1985e 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 L statement..**
1985f 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61 20 zName must be a
19860 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 6c pointer to a nul
19861 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 terminated stri
19862 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 ng..**.** This c
19863 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 65 all must be made
19864 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f after a call to
19865 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e sqlite3VdbeSetN
19866 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 umCols()..**.**
19867 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 The final parame
19868 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 20 ter, xDel, must
19869 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 be one of SQLITE
1986a 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 45 _DYNAMIC, SQLITE
1986b 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53 51 _STATIC.** or SQ
1986c 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 20 LITE_TRANSIENT.
1986d 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 45 5f If it is SQLITE_
1986e 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74 68 DYNAMIC, then th
1986f 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 e buffer pointed
19870 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65 20 .** to by zName
19871 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 79 will be freed by
19872 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 sqlite3DbFree()
19873 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 20 69 when the vdbe i
19874 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a s destroyed..*/.
19875 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19876 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 nt sqlite3VdbeSe
19877 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62 65 tColName(. Vdbe
19878 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
19879 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1987a 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66 69 Vdbe being confi
1987b 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 gured */. int i
1987c 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 dx,
1987d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1987e 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a ndex of column z
1987f 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f 20 Name applies to
19880 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20 20 */. int var,
19881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19882 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
19883 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f the COLNAME_* co
19884 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e nstants */. con
19885 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
19886 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19887 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 Pointer to buff
19888 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 61 er containing na
19889 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 me */. void (*x
1988a 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 Del)(void*)
1988b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
1988c 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 ry management st
1988d 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d 65 rategy for zName
1988e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1988f 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 . Mem *pColName
19890 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c ;. assert( idx<
19891 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b p->nResColumn );
19892 0a 20 20 61 73 73 65 72 74 28 20 76 61 72 3c 43 . assert( var<C
19893 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 OLNAME_N );. if
19894 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ( p->db->mallocF
19895 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73 73 ailed ){. ass
19896 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 78 ert( !zName || x
19897 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41 Del!=SQLITE_DYNA
19898 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75 72 MIC );. retur
19899 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
1989a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d }. assert( p-
1989b 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a >aColName!=0 );.
1989c 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28 70 pColName = &(p
1989d 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76 ->aColName[idx+v
1989e 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e ar*p->nResColumn
1989f 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ]);. rc = sqlit
198a0 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
198a1 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c pColName, zName,
198a2 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
198a3 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65 72 , xDel);. asser
198a4 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e 61 t( rc!=0 || !zNa
198a5 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d me || (pColName-
198a6 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 29 >flags&MEM_Term)
198a7 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 !=0 );. return
198a8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 rc;.}../*.** A r
198a9 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 ead or write tra
198aa 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72 20 nsaction may or
198ab 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69 76 may not be activ
198ac 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 e on database ha
198ad 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20 61 ndle.** db. If a
198ae 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
198af 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20 69 active, commit i
198b0 74 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 t. If there is a
198b1 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61 .** write-transa
198b2 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d ction spanning m
198b3 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 ore than one dat
198b4 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 abase file, this
198b5 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 routine.** take
198b6 73 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d 61 s care of the ma
198b7 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69 ster journal tri
198b8 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 ckery..*/.static
198b9 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 28 int vdbeCommit(
198ba 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62 sqlite3 *db, Vdb
198bb 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a e *p){. int i;.
198bc 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20 30 int nTrans = 0
198bd 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 ; /* Number of
198be 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 61 databases with a
198bf 6e 20 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 n active write-t
198c0 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
198c1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
198c2 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58 63 OK;. int needXc
198c3 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a ommit = 0;.. /*
198c4 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e Before doing an
198c5 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c ything else, cal
198c6 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 l the xSync() ca
198c7 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 llback for any.
198c8 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 ** virtual modu
198c9 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 le tables writte
198ca 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 n in this transa
198cb 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 ction. This has
198cc 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 to. ** be done
198cd 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 before determini
198ce 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 ng whether a mas
198cf 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
198d0 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 is . ** requir
198d1 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 ed, as an xSync(
198d2 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 ) callback may a
198d3 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 dd an attached d
198d4 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 atabase. ** to
198d5 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
198d6 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
198d7 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c ite3VtabSync(db,
198d8 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 &p->zErrMsg);.
198d9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
198da 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
198db 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 rc;. }.. /* T
198dc 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 his loop determi
198dd 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 nes (a) if the c
198de 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c ommit hook shoul
198df 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 d be invoked and
198e0 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 . ** (b) how ma
198e1 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ny database file
198e2 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 s have open writ
198e3 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 e transactions,
198e4 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 not . ** includ
198e5 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 ing the temp dat
198e6 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d abase. (b) is im
198e7 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 portant because
198e8 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 if more than .
198e9 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 ** one database
198ea 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e file has an open
198eb 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
198ec 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 on, a master jou
198ed 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 rnal. ** file i
198ee 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 s required for a
198ef 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e n atomic commit.
198f0 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 . */ . for(i=0
198f1 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
198f2 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 ){ . Btree *p
198f3 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
198f4 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c pBt;. if( sql
198f5 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
198f6 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 ns(pBt) ){.
198f7 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 needXcommit = 1
198f8 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 ;. if( i!=1
198f9 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 ) nTrans++;.
198fa 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
198fb 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 there are any wr
198fc 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite-transactions
198fd 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 at all, invoke
198fe 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 the commit hook
198ff 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f */. if( needXco
19900 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d mmit && db->xCom
19901 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 mitCallback ){.
19902 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e assert( (db->
19903 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 flags & SQLITE_C
19904 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29 3b ommitBusy)==0 );
19905 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c . db->flags |
19906 3d 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 42 = SQLITE_CommitB
19907 75 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 usy;. (void)s
19908 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
19909 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 db);. rc = db
1990a 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 ->xCommitCallbac
1990b 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 k(db->pCommitArg
1990c 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c );. (void)sql
1990d 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1990e 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 ;. db->flags
1990f 26 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 &= ~SQLITE_Commi
19910 74 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20 72 tBusy;. if( r
19911 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 c ){. retur
19912 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 n SQLITE_CONSTRA
19913 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a INT;. }. }..
19914 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 /* The simple
19915 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 case - no more t
19916 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 han one database
19917 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 file (not count
19918 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d ing the. ** TEM
19919 50 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 P database) has
1991a 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 a transaction ac
1991b 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 tive. There is
1991c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 no need for the
1991d 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 . ** master-jou
1991e 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rnal.. **. **
1991f 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 If the return va
19920 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 lue of sqlite3Bt
19921 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 reeGetFilename()
19922 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 is a zero lengt
19923 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 h. ** string, i
19924 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e t means the main
19925 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 database is :me
19926 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 mory: or a temp
19927 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 file. In . **
19928 74 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 that case we do
19929 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d not support atom
1992a 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f ic multi-file co
1992b 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 mmits, so use th
1992c 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 e . ** simple c
1992d 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 ase then too..
1992e 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 */. if( 0==sqli
1992f 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 te3Strlen30(sqli
19930 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
19931 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 ame(db->aDb[0].p
19932 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e Bt)). || nTran
19933 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f s<=1. ){. fo
19934 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
19935 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 E_OK && i<db->nD
19936 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 b; i++){. B
19937 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
19938 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
19939 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
1993a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1993b 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1993c 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20 eOne(pBt, 0);.
1993d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1993e 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 /* Do the commi
1993f 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 t only if all da
19940 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 tabases successf
19941 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 ully complete ph
19942 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49 ase 1. . ** I
19943 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72 f one of the Btr
19944 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
19945 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20 () calls fails,
19946 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61 this indicates a
19947 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f n. ** IO erro
19948 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67 r while deleting
19949 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61 or truncating a
1994a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 journal file. I
1994b 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20 t is unlikely,.
1994c 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20 ** but could
1994d 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20 happen. In this
1994e 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f case abandon pro
1994f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 cessing and retu
19950 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 rn the error..
19951 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
19952 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
19953 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b && i<db->nDb; i+
19954 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 +){. Btree
19955 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *pBt = db->aDb[i
19956 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 ].pBt;. if(
19957 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt ){.
19958 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
19959 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
1995a 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 pBt);. }.
1995b 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
1995c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1995d 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f sqlite3VtabCo
1995e 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a mmit(db);. }.
1995f 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f }.. /* The co
19960 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65 mplex case - The
19961 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69 re is a multi-fi
19962 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 le write-transac
19963 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a tion active.. *
19964 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 * This requires
19965 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
19966 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20 file to ensure
19967 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
19968 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 is. ** committe
19969 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f d atomicly.. */
1996a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1996b 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c OMIT_DISKIO. el
1996c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f se{. sqlite3_
1996d 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e vfs *pVfs = db->
1996e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 pVfs;. int ne
1996f 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 edSync = 0;.
19970 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 char *zMaster =
19971 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d 0; /* File-nam
19972 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72 e for the master
19973 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 journal */.
19974 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69 char const *zMai
19975 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 nFile = sqlite3B
19976 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 treeGetFilename(
19977 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b db->aDb[0].pBt);
19978 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c . sqlite3_fil
19979 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a e *pMaster = 0;.
1997a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d i64 offset =
1997b 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0;. int res;
1997c 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 .. /* Select
1997d 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
1997e 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 file name */.
1997f 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32 do {. u32
19980 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 iRandom;.
19981 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
19982 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
19983 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
19984 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e ness(sizeof(iRan
19985 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b dom), &iRandom);
19986 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d . zMaster =
19987 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
19988 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c db, "%s-mj%08X",
19989 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e zMainFile, iRan
1998a 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b dom&0x7fffffff);
1998b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 . if( !zMas
1998c 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ter ){. r
1998d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1998e 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
1998f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
19990 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 Access(pVfs, zMa
19991 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 ster, SQLITE_ACC
19992 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 ESS_EXISTS, &res
19993 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 );. }while( r
19994 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
19995 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 res );. if( r
19996 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
19997 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 /* Open th
19998 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
19999 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 . */. rc =
1999a 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c sqlite3OsOpenMal
1999b 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 loc(pVfs, zMaste
1999c 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 r, &pMaster, .
1999d 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f SQLITE_O
1999e 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
1999f 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
199a0 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 |. SQLI
199a1 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
199a2 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 E|SQLITE_OPEN_MA
199a3 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a STER_JOURNAL, 0.
199a4 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 );. }.
199a5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
199a6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
199a7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
199a8 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
199a9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
199aa 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 . . /* Write
199ab 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 the name of each
199ac 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
199ad 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
199ae 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 n into the new.
199af 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 ** master jou
199b0 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e rnal file. If an
199b1 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 error occurs at
199b2 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 this point clos
199b3 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c e. ** and del
199b4 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ete the master j
199b5 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c ournal file. All
199b6 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
199b7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 journal files.
199b8 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 ** still have
199b9 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 'null' as the ma
199ba 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
199bb 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 nter, so they wi
199bc 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 ll roll. ** b
199bd 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c ack independentl
199be 79 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f y if a failure o
199bf 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 ccurs.. */.
199c0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
199c1 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
199c2 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
199c3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
199c4 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 if( i==1 )
199c5 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 continue; /* I
199c6 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 gnore the TEMP d
199c7 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 atabase */.
199c8 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 if( sqlite3Btre
199c9 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 eIsInTrans(pBt)
199ca 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 ){. char
199cb 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 const *zFile = s
199cc 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f qlite3BtreeGetJo
199cd 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a urnalname(pBt);.
199ce 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c if( zFil
199cf 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e e[0]==0 ) contin
199d0 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a ue; /* Ignore :
199d1 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 memory: database
199d2 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 s */. if(
199d3 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 !needSync && !s
199d4 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 qlite3BtreeSyncD
199d5 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a isabled(pBt) ){.
199d6 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 needSy
199d7 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nc = 1;.
199d8 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 }. rc = s
199d9 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d qlite3OsWrite(pM
199da 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 aster, zFile, sq
199db 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 lite3Strlen30(zF
199dc 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b ile)+1, offset);
199dd 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 . offset
199de 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
199df 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 30(zFile)+1;.
199e0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
199e1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
199e2 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
199e3 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b seFree(pMaster);
199e4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
199e5 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c e3OsDelete(pVfs,
199e6 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 zMaster, 0);.
199e7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
199e8 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 bFree(db, zMaste
199e9 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 r);. re
199ea 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
199eb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
199ec 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 .. /* Sync th
199ed 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
199ee 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f file. If the IO
199ef 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 CAP_SEQUENTIAL d
199f0 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 evice. ** fla
199f1 67 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73 g is set this is
199f2 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 not required..
199f3 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 */. if( ne
199f4 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 edSync . &&
199f5 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 0==(sqlite3OsDev
199f6 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
199f7 63 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 cs(pMaster)&SQLI
199f8 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
199f9 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c IAL). && SQL
199fa 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
199fb 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 lite3OsSync(pMas
199fc 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 ter, SQLITE_SYNC
199fd 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b _NORMAL)). ){
199fe 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
199ff 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 CloseFree(pMaste
19a00 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 r);. sqlite
19a01 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
19a02 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 zMaster, 0);.
19a03 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
19a04 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
19a05 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
19a06 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 }.. /* Sy
19a07 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 nc all the db fi
19a08 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 les involved in
19a09 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
19a0a 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 The same call.
19a0b 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d ** sets the m
19a0c 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f aster journal po
19a0d 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e inter in each in
19a0e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c dividual journal
19a0f 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 . If. ** an e
19a10 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 rror occurs here
19a11 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 , do not delete
19a12 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
19a13 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a al file.. **.
19a14 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 ** If the er
19a15 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
19a16 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c g the first call
19a17 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 to. ** sqlit
19a18 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
19a19 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 seOne(), then th
19a1a 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 ere is a chance
19a1b 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 that the. **
19a1c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
19a1d 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 ile will be orph
19a1e 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e aned. But we can
19a1f 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 not delete it,.
19a20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 ** in case th
19a21 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
19a22 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 file name was w
19a23 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
19a24 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 journal. ** f
19a25 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 ile before the f
19a26 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e ailure occurred.
19a27 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 . */. for(
19a28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f i=0; rc==SQLITE_
19a29 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b OK && i<db->nDb;
19a2a 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 i++){ . Bt
19a2b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
19a2c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
19a2d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
19a2e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19a2f 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
19a30 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 One(pBt, zMaster
19a31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
19a32 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
19a33 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 oseFree(pMaster)
19a34 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
19a35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
19a36 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
19a37 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 b, zMaster);.
19a38 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
19a39 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 }.. /* Dele
19a3a 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
19a3b 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 urnal file. This
19a3c 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 commits the tra
19a3d 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a nsaction. After.
19a3e 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 ** doing thi
19a3f 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
19a40 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 is synced again
19a41 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 before any indiv
19a42 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 idual. ** tra
19a43 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 nsaction files a
19a44 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 re deleted..
19a45 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
19a46 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
19a47 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 , zMaster, 1);.
19a48 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
19a49 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
19a4a 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a zMaster = 0;.
19a4b 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
19a4c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
19a4d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c }.. /* All
19a4e 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 files and direc
19a4f 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 tories have alre
19a50 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c ady been synced,
19a51 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e so the followin
19a52 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 g. ** calls t
19a53 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f o sqlite3BtreeCo
19a54 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 mmitPhaseTwo() a
19a55 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 re only closing
19a56 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a files and. **
19a57 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 deleting or tru
19a58 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 ncating journals
19a59 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 . If something g
19a5a 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a oes wrong while.
19a5b 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 ** this is h
19a5c 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 appening we don'
19a5d 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 t really care. T
19a5e 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 he integrity of
19a5f 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 the. ** trans
19a60 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 action is alread
19a61 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 y guaranteed, bu
19a62 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f t some stray 'co
19a63 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 ld' journals.
19a64 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 ** may be lying
19a65 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 around. Returni
19a66 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ng an error code
19a67 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 won't help matt
19a68 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ers.. */.
19a69 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
19a6a 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 d_io_errors();.
19a6b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
19a6c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
19a6d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
19a6e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
19a6f 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
19a70 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
19a71 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
19a72 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
19a73 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
19a74 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 eTwo(pBt);.
19a75 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
19a76 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
19a77 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c loc();. enabl
19a78 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
19a79 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 rrors();.. sq
19a7a 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 lite3VtabCommit(
19a7b 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a db);. }.#endif.
19a7c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
19a7d 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ./* .** This rou
19a7e 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 tine checks that
19a7f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 the sqlite3.act
19a80 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 iveVdbeCnt count
19a81 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 variable.** mat
19a82 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ches the number
19a83 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 of vdbe's in the
19a84 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 list sqlite3.pV
19a85 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 dbe that are.**
19a86 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 currently active
19a87 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 . An assertion f
19a88 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 ails if the two
19a89 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 counts do not ma
19a8a 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 tch..** This is
19a8b 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 an internal self
19a8c 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 -check only - it
19a8d 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e is not an essen
19a8e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a tial processing.
19a8f 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 ** step..**.** T
19a90 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 his is a no-op i
19a91 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 f NDEBUG is defi
19a92 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ned..*/.#ifndef
19a93 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f NDEBUG.static vo
19a94 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 id checkActiveVd
19a95 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 beCnt(sqlite3 *d
19a96 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 b){. Vdbe *p;.
19a97 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
19a98 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a int nWrite = 0;.
19a99 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b p = db->pVdbe;
19a9a 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 . while( p ){.
19a9b 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d if( p->magic=
19a9c 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
19a9d 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 && p->pc>=0 ){.
19a9e 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 cnt++;.
19a9f 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c if( p->readOnl
19aa0 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b y==0 ) nWrite++;
19aa1 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 . }. p = p
19aa2 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 ->pNext;. }. a
19aa3 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e ssert( cnt==db->
19aa4 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b activeVdbeCnt );
19aa5 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 . assert( nWrit
19aa6 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 e==db->writeVdbe
19aa7 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 Cnt );.}.#else.#
19aa8 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 define checkActi
19aa9 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e veVdbeCnt(x).#en
19aaa 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 dif../*.** For e
19aab 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 very Btree that
19aac 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e in database conn
19aad 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 ection db which
19aae 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 .** has been mod
19aaf 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 ified, "trip" or
19ab0 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 invalidate each
19ab1 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 cursor in.** th
19ab2 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 at Btree might h
19ab3 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 ave been modifie
19ab4 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 d so that the cu
19ab5 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 rsor.** can neve
19ab6 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e r be used again.
19ab7 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 This happens w
19ab8 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a hen a rollback.*
19ab9 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 ** occurs. We h
19aba 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 ave to trip all
19abb 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 the other cursor
19abc 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f s, even.** curso
19abd 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 r from other VMs
19abe 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 in different da
19abf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
19ac0 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e ns,.** so that n
19ac1 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 one of them try
19ac2 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 to use the data
19ac3 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a at which they.**
19ac4 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 were pointing a
19ac5 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 nd which now may
19ac6 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 have been chang
19ac7 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 ed due.** to the
19ac8 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
19ac9 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 Remember that a
19aca 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 rollback can de
19acb 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 lete tables comp
19acc 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 lete and.** reor
19acd 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 der rootpages.
19ace 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 So it is not suf
19acf 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 ficient just to
19ad0 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 save.** the stat
19ad1 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e e of the cursor.
19ad2 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 We have to inv
19ad3 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 alidate the curs
19ad4 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 or.** so that it
19ad5 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 is never used a
19ad6 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 gain..*/.static
19ad7 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 void invalidateC
19ad8 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 ursorsOnModified
19ad9 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a Btrees(sqlite3 *
19ada 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 db){. int i;.
19adb 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
19adc 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 Db; i++){. Bt
19add 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 ree *p = db->aDb
19ade 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
19adf 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 p && sqlite3Btr
19ae0 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 eeIsInTrans(p) )
19ae1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
19ae2 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
19ae3 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f rs(p, SQLITE_ABO
19ae4 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d RT);. }. }.}
19ae5 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 ../*.** If the V
19ae6 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 dbe passed as th
19ae7 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
19ae8 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d opened a statem
19ae9 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c ent-transaction,
19aea 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77 .** close it now
19aeb 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d . Argument eOp m
19aec 75 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41 ust be either SA
19aed 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
19aee 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 or.** SAVEPOINT
19aef 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20 _RELEASE. If it
19af0 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c is SAVEPOINT_ROL
19af1 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 LBACK, then the
19af2 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 statement.** tra
19af3 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
19af4 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 ed back. If eOp
19af5 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c is SAVEPOINT_REL
19af6 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a EASE, then the .
19af7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 ** statement tra
19af8 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
19af9 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tted..**.** If a
19afa 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
19afb 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 s, an SQLITE_IOE
19afc 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 RR_XXX error cod
19afd 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a e is returned. .
19afe 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c ** Otherwise SQL
19aff 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
19b00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19b01 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
19b02 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c atement(Vdbe *p,
19b03 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c int eOp){. sql
19b04 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d ite3 *const db =
19b05 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 p->db;. int rc
19b06 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
19b07 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e if( p->iStatemen
19b08 74 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65 6d t && db->nStatem
19b09 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ent ){. int i
19b0a 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 ;. const int
19b0b 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d 3e iSavepoint = p->
19b0c 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a 20 iStatement-1;..
19b0d 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d 3d assert( eOp==
19b0e 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
19b0f 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 50 CK || eOp==SAVEP
19b10 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20 OINT_RELEASE);.
19b11 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e assert( db->n
19b12 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 Statement>0 );.
19b13 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 53 assert( p->iS
19b14 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e 6e tatement==(db->n
19b15 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e 53 Statement+db->nS
19b16 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 20 avepoint) );..
19b17 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
19b18 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 >nDb; i++){ .
19b19 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c int rc2 = SQL
19b1a 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 74 ITE_OK;. Bt
19b1b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
19b1c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
19b1d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
19b1e 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56 if( eOp==SAV
19b1f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
19b20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32 ){. rc2
19b21 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 = sqlite3BtreeS
19b22 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 avepoint(pBt, SA
19b23 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
19b24 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 , iSavepoint);.
19b25 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
19b26 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 if( rc2==SQLITE
19b27 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
19b28 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 rc2 = sqlite3Bt
19b29 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 reeSavepoint(pBt
19b2a 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 , SAVEPOINT_RELE
19b2b 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 ASE, iSavepoint)
19b2c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
19b2d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
19b2e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
19b2f 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 rc = rc2;.
19b30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
19b31 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 74 }. db->nSt
19b32 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 70 atement--;. p
19b33 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 ->iStatement = 0
19b34 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
19b35 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 c;.}../*.** If S
19b36 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
19b37 64 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61 d to support sha
19b38 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61 red-cache mode a
19b39 6e 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 nd to be threads
19b3a 61 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 afe,.** this rou
19b3b 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65 tine obtains the
19b3c 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
19b3d 64 20 77 69 74 68 20 65 61 63 68 20 42 74 53 68 d with each BtSh
19b3e 61 72 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a ared structure.*
19b3f 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 63 * that may be ac
19b40 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d cessed by the VM
19b41 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 passed as an ar
19b42 67 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67 gument. In doing
19b43 20 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20 74 so it.** sets t
19b44 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 6d he BtShared.db m
19b45 65 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66 ember of each of
19b46 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 the BtShared st
19b47 72 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72 69 ructures, ensuri
19b48 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 ng.** that the c
19b49 6f 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e 64 orrect busy-hand
19b4a 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ler callback is
19b4b 69 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75 69 invoked if requi
19b4c 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 red..**.** If SQ
19b4d 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65 Lite is not thre
19b4e 61 64 73 61 66 65 20 62 75 74 20 64 6f 65 73 20 adsafe but does
19b4f 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 support shared-c
19b50 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 0a ache mode, then.
19b51 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 ** sqlite3BtreeE
19b52 6e 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e 76 nterAll() is inv
19b53 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 oked to set the
19b54 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69 BtShared.db vari
19b55 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20 ables.** of all
19b56 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75 of BtShared stru
19b57 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c ctures accessibl
19b58 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61 e via the databa
19b59 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73 se handle .** as
19b5a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
19b5b 65 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65 20 e VM. Of course
19b5c 6f 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f 66 only a subset of
19b5d 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
19b5e 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63 63 s.** will be acc
19b5f 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 2c essed by the VM,
19b60 20 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75 73 and we could us
19b61 65 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73 6b e Vdbe.btreeMask
19b62 20 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74 68 to figure.** th
19b63 61 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20 62 at subset out, b
19b64 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 ut there is no a
19b65 64 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69 6e dvantage to doin
19b66 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 g so..**.** If S
19b67 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 QLite is not thr
19b68 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73 eadsafe and does
19b69 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61 not support sha
19b6a 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 red-cache mode,
19b6b 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
19b6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
19b6d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19b6e 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
19b6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19b70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19b71 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
19b72 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53 51 Vdbe *p){.#if SQ
19b73 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
19b74 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
19b75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70 texArrayEnter(&p
19b76 2d 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73 65 ->aMutex);.#else
19b77 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
19b78 6e 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b 0a nterAll(p->db);.
19b79 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 0a #endif.}.#endif.
19b7a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
19b7b 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 68 ine is called th
19b7c 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 72 e when a VDBE tr
19b7d 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 66 ies to halt. If
19b7e 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 73 the VDBE.** has
19b7f 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 6e made changes an
19b80 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d d is in autocomm
19b81 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 6f it mode, then co
19b82 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 68 mmit those.** ch
19b83 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f 6c anges. If a rol
19b84 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 2c lback is needed,
19b85 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f 6c then do the rol
19b86 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 lback..**.** Thi
19b87 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 s routine is the
19b88 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 only way to mov
19b89 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 61 e the state of a
19b8a 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c 49 VM from.** SQLI
19b8b 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f 20 TE_MAGIC_RUN to
19b8c 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c SQLITE_MAGIC_HAL
19b8d 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 T. It is harmle
19b8e 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 68 ss to.** call th
19b8f 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 20 is on a VM that
19b90 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 is in the SQLITE
19b91 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 74 _MAGIC_HALT stat
19b92 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 e..**.** Return
19b93 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 an error code.
19b94 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 6f If the commit co
19b95 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 uld not complete
19b96 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 6c because of.** l
19b97 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c 20 ock contention,
19b98 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
19b99 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 42 SY. If SQLITE_B
19b9a 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2c USY is returned,
19b9b 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 65 it.** means the
19b9c 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 68 close did not h
19b9d 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 20 appen and needs
19b9e 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e 0a to be repeated..
19b9f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19ba0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19ba1 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a eHalt(Vdbe *p){.
19ba2 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
19ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ba4 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74 6f /* Used to sto
19ba5 72 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65 74 re transient ret
19ba6 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 73 urn codes */. s
19ba7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
19ba8 64 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 db;.. /* This f
19ba9 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 unction contains
19baa 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 the logic that
19bab 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20 determines if a
19bac 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a statement or. *
19bad 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 * transaction wi
19bae 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 ll be committed
19baf 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 or rolled back a
19bb0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 s a result of th
19bb1 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e e. ** execution
19bb2 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c of this virtual
19bb3 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a machine. . **.
19bb4 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 ** If any of t
19bb5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 he following err
19bb6 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a 0a ors occur:. **.
19bb7 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
19bb8 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 53 NOMEM. ** S
19bb9 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a 2a QLITE_IOERR. **
19bba 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c SQLITE_FULL
19bbb 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 . ** SQLITE
19bbc 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a 0a _INTERRUPT. **.
19bbd 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 6e ** Then the in
19bbe 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 67 ternal cache mig
19bbf 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 66 ht have been lef
19bc0 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 t in an inconsis
19bc1 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 2e tent. ** state.
19bc2 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f 6c We need to rol
19bc3 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 6d lback the statem
19bc4 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ent transaction,
19bc5 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 2a if there is. *
19bc6 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 6f * one, or the co
19bc7 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 69 mplete transacti
19bc8 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 6e on if there is n
19bc9 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e o statement tran
19bca 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 saction.. */..
19bcb 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c if( p->db->mall
19bcc 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
19bcd 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e p->rc = SQLITE_N
19bce 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 OMEM;. }. clos
19bcf 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 70 eAllCursorsExcep
19bd0 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29 3b tActiveVtabs(p);
19bd1 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 . if( p->magic!
19bd2 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
19bd3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
19bd4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63 LITE_OK;. }. c
19bd5 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e heckActiveVdbeCn
19bd6 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 t(db);.. /* No
19bd7 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
19bd8 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 ck needed if the
19bd9 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73 program never s
19bda 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 tarted */. if(
19bdb 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 p->pc>=0 ){.
19bdc 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 int mrc; /* Pr
19bdd 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65 imary error code
19bde 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20 from p->rc */.
19bdf 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e int eStatemen
19be0 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 tOp = 0;. int
19be1 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b isSpecialError;
19be2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
19be3 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 et to true if a
19be4 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20 'special' error
19be5 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 */.. /* Lock
19be6 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20 all btrees used
19be7 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
19be8 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
19be9 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 dbeMutexArrayEnt
19bea 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 er(p);.. /* C
19beb 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 heck for one of
19bec 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f the special erro
19bed 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 rs */. mrc =
19bee 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 p->rc & 0xff;.
19bef 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 isSpecialError
19bf0 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e = mrc==SQLITE_N
19bf1 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c OMEM || mrc==SQL
19bf2 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 ITE_IOERR.
19bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
19bf4 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e | mrc==SQLITE_IN
19bf5 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d TERRUPT || mrc==
19bf6 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 SQLITE_FULL;.
19bf7 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 if( isSpecialEr
19bf8 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ror ){. /*
19bf9 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73 If the query was
19bfa 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e read-only, we n
19bfb 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 eed do no rollba
19bfc 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 ck at all. Other
19bfd 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 wise,. ** p
19bfe 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 roceed with the
19bff 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 special handling
19c00 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
19c01 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c if( !p->readOnl
19c02 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 y || mrc!=SQLITE
19c03 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 _INTERRUPT ){.
19c04 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d if( p->rc=
19c05 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c =SQLITE_IOERR_BL
19c06 4f 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 OCKED && p->uses
19c07 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 StmtJournal ){.
19c08 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d eStatem
19c09 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e entOp = SAVEPOIN
19c0a 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 T_ROLLBACK;.
19c0b 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 p->rc = SQ
19c0c 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
19c0d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 }else if( (mr
19c0e 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
19c0f 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 || mrc==SQLITE_F
19c10 55 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20 ULL).
19c11 20 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73 && p->us
19c12 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b esStmtJournal ){
19c13 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 . eStat
19c14 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
19c15 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
19c16 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19c17 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 /* We are
19c18 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 forced to roll
19c19 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
19c1a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 transaction. Bef
19c1b 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 ore doing.
19c1c 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 ** so, abort
19c1d 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 any other state
19c1e 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c ments this handl
19c1f 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 e currently has
19c20 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 active..
19c21 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 */. i
19c22 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 nvalidateCursors
19c23 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 OnModifiedBtrees
19c24 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
19c25 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
19c26 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
19c27 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
19c28 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
19c29 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
19c2a 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
19c2b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
19c2c 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
19c2d 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 the auto-commit
19c2e 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 flag is set and
19c2f 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c this is the onl
19c30 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 y active writer
19c31 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e . ** VM, then
19c32 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 we do either a
19c33 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
19c34 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ck of the curren
19c35 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a t transaction. .
19c36 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
19c37 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 te: This block a
19c38 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 lso runs if one
19c39 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 of the special e
19c3a 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 rrors handled .
19c3b 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 ** above has
19c3c 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a occurred. . *
19c3d 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 /. if( !sqlit
19c3e 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
19c3f 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 . && db->au
19c40 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 toCommit . &
19c41 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
19c42 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 nt==(p->readOnly
19c43 3d 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 ==0) . && (d
19c44 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
19c45 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 E_CommitBusy)==0
19c46 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 . ){. if
19c47 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
19c48 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 OK || (p->errorA
19c49 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 ction==OE_Fail &
19c4a 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f & !isSpecialErro
19c4b 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a r) ){. /*
19c4c 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 The auto-commit
19c4d 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 flag is true, a
19c4e 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 nd the vdbe prog
19c4f 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 ram was .
19c50 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f ** successful o
19c51 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 r hit an 'OR FAI
19c52 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 L' constraint. T
19c53 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d his means a comm
19c54 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 it . ** i
19c55 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 s required..
19c56 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 */. r
19c57 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 c = vdbeCommit(d
19c58 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 b, p);. i
19c59 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 f( rc==SQLITE_BU
19c5a 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 SY ){.
19c5b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
19c5c 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
19c5d 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 aMutex);.
19c5e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19c5f 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d _BUSY;. }
19c60 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c else if( rc!=SQL
19c61 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
19c62 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a p->rc = rc;.
19c63 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
19c64 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
19c65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
19c66 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
19c67 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c e3CommitInternal
19c68 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 Changes(db);.
19c69 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
19c6a 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
19c6b 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
19c6c 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 b);. }.
19c6d 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 db->nStatement
19c6e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
19c6f 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 if( eStatementOp
19c70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
19c71 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
19c72 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 K || p->errorAct
19c73 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a ion==OE_Fail ){.
19c74 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 eStateme
19c75 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 ntOp = SAVEPOINT
19c76 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 _RELEASE;.
19c77 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 }else if( p->err
19c78 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f orAction==OE_Abo
19c79 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 rt ){. eS
19c7a 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 tatementOp = SAV
19c7b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b EPOINT_ROLLBACK;
19c7c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19c7d 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 invalidate
19c7e 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 CursorsOnModifie
19c7f 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 dBtrees(db);.
19c80 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c sqlite3Roll
19c81 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 backAll(db);.
19c82 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 sqlite3Clos
19c83 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
19c84 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
19c85 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
19c86 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
19c87 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 /* If eStateme
19c88 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f ntOp is non-zero
19c89 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 , then a stateme
19c8a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e nt transaction n
19c8b 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 eeds to. ** b
19c8c 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 e committed or r
19c8d 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c olled back. Call
19c8e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
19c8f 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a eStatement() to.
19c90 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 ** do so. If
19c91 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
19c92 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
19c93 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e , and the curren
19c94 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 t statement.
19c95 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ** error code is
19c96 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 SQLITE_OK or SQ
19c97 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c LITE_CONSTRAINT,
19c98 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 then set the er
19c99 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 ror. ** code
19c9a 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 to the new value
19c9b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
19c9c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b eStatementOp ){
19c9d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
19c9e 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 te3VdbeCloseStat
19c9f 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d ement(p, eStatem
19ca0 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 entOp);. if
19ca1 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d ( rc && (p->rc==
19ca2 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
19ca3 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 rc==SQLITE_CONST
19ca4 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 RAINT) ){.
19ca5 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
19ca6 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
19ca7 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
19ca8 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e sg);. p->
19ca9 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
19caa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
19cab 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 /* If this was
19cac 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 an INSERT, UPDA
19cad 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 TE or DELETE and
19cae 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 no statement tr
19caf 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a ansaction. **
19cb0 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 has been rolled
19cb1 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 back, update th
19cb2 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
19cb3 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 ction change-cou
19cb4 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 nter. . */.
19cb5 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 if( p->changeC
19cb6 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 ntOn && p->pc>=0
19cb7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 ){. if( eS
19cb8 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 tatementOp!=SAVE
19cb9 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
19cba 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
19cbb 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
19cbc 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
19cbd 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19cbe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19cbf 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 eSetChanges(db,
19cc0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
19cc1 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
19cc2 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
19cc3 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f * Rollback or co
19cc4 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 mmit any schema
19cc5 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 changes that occ
19cc6 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 urred. */. if
19cc7 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f ( p->rc!=SQLITE_
19cc8 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 OK && db->flags&
19cc9 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
19cca 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 nges ){. sq
19ccb 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
19ccc 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b alSchema(db, 0);
19ccd 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 . db->flags
19cce 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 = (db->flags |
19ccf 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
19cd0 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 nges);. }..
19cd1 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 /* Release the
19cd2 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 locks */. sq
19cd3 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
19cd4 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d rrayLeave(&p->aM
19cd5 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a utex);. }.. /*
19cd6 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 We have success
19cd7 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 fully halted and
19cd8 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 closed the VM.
19cd9 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 Record this fac
19cda 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 t. */. if( p->p
19cdb 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e c>=0 ){. db->
19cdc 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b activeVdbeCnt--;
19cdd 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 . if( !p->rea
19cde 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 dOnly ){. d
19cdf 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d b->writeVdbeCnt-
19ce0 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 -;. }. ass
19ce1 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 ert( db->activeV
19ce2 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 dbeCnt>=db->writ
19ce3 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a eVdbeCnt );. }.
19ce4 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
19ce5 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 E_MAGIC_HALT;.
19ce6 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 checkActiveVdbeC
19ce7 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d nt(db);. if( p-
19ce8 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
19ce9 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
19cea 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19ceb 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
19cec 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
19ced 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c is set to true,
19cee 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 then any locks
19cef 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 that were held.
19cf0 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f ** by connectio
19cf1 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 n db have now be
19cf2 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c en released. Cal
19cf3 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 l sqlite3Connect
19cf4 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 ionUnlocked() .
19cf5 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e ** to invoke an
19cf6 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 y required unloc
19cf7 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
19cf8 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 ks.. */. if( d
19cf9 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b b->autoCommit ){
19cfa 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e . sqlite3Conn
19cfb 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 ectionUnlocked(d
19cfc 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 b);. }.. asser
19cfd 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 t( db->activeVdb
19cfe 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 eCnt>0 || db->au
19cff 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 toCommit==0 || d
19d00 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 b->nStatement==0
19d01 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c );. return SQL
19d02 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
19d03 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 * Each VDBE hold
19d04 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
19d05 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
19d06 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 sqlite3_step() c
19d07 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e all.** in p->rc.
19d08 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
19d09 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 ets that result
19d0a 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f back to SQLITE_O
19d0b 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 K..*/.SQLITE_PRI
19d0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19d0d 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 3VdbeResetStepRe
19d0e 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 sult(Vdbe *p){.
19d0f 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
19d10 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 OK;.}../*.** Cle
19d11 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 an up a VDBE aft
19d12 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 er execution but
19d13 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 do not delete t
19d14 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 he VDBE just yet
19d15 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 ..** Write any e
19d16 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e rror messages in
19d17 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 to *pzErrMsg. R
19d18 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 eturn the result
19d19 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 code..**.** Aft
19d1a 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
19d1b 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 is run, the VDBE
19d1c 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 should be ready
19d1d 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a to be executed.
19d1e 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 ** again..**.**
19d1f 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e To look at it an
19d20 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 other way, this
19d21 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 routine resets t
19d22 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a he state of the.
19d23 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ** virtual machi
19d24 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 ne from VDBE_MAG
19d25 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d IC_RUN or VDBE_M
19d26 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 AGIC_HALT back t
19d27 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f o.** VDBE_MAGIC_
19d28 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f INIT..*/.SQLITE_
19d29 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19d2a 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 te3VdbeReset(Vdb
19d2b 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 e *p){. sqlite3
19d2c 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e *db;. db = p->
19d2d 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 db;.. /* If the
19d2e 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 VM did not run
19d2f 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 to completion or
19d30 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 if it encounter
19d31 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 ed an. ** error
19d32 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 , then it might
19d33 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 not have been ha
19d34 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 lted properly.
19d35 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 So halt. ** it
19d36 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 now.. */. (voi
19d37 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
19d38 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 n(db);. sqlite3
19d39 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 VdbeHalt(p);. (
19d3a 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
19d3b 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a tyOff(db);.. /*
19d3c 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 If the VDBE has
19d3d 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 be run even par
19d3e 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 tially, then tra
19d3f 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 nsfer the error
19d40 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 code. ** and er
19d41 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d ror message from
19d42 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 the VDBE into t
19d43 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
19d44 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 structure. But
19d45 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 . ** if the VDB
19d46 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 E has just been
19d47 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 set to run but h
19d48 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 as not actually
19d49 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a executed any. *
19d4a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 * instructions y
19d4b 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 et, leave the ma
19d4c 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f in database erro
19d4d 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e r information un
19d4e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 changed.. */.
19d4f 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a if( p->pc>=0 ){.
19d50 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d if( p->zErrM
19d51 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 sg ){. sqli
19d52 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
19d53 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 lloc();. sq
19d54 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
19d55 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e (db->pErr,-1,p->
19d56 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 zErrMsg,SQLITE_U
19d57 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 TF8,SQLITE_TRANS
19d58 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c IENT);. sql
19d59 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
19d5a 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d loc();. db-
19d5b 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
19d5c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
19d5d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
19d5e 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e rMsg);. p->
19d5f 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
19d60 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 }else if( p->rc
19d61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19d62 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 3Error(db, p->rc
19d63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
19d64 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
19d65 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f ror(db, SQLITE_O
19d66 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d K, 0);. }. }
19d67 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 else if( p->rc &
19d68 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a & p->expired ){.
19d69 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 /* The expir
19d6a 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 ed flag was set
19d6b 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f on the VDBE befo
19d6c 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c re the first cal
19d6d 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 l. ** to sqli
19d6e 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 te3_step(). For
19d6f 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e consistency (sin
19d70 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 ce sqlite3_step(
19d71 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c ) was. ** cal
19d72 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 led), set the da
19d73 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 tabase error in
19d74 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c this case as wel
19d75 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 l.. */. sq
19d76 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 lite3Error(db, p
19d77 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 ->rc, 0);. sq
19d78 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
19d79 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 (db->pErr, -1, p
19d7a 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 ->zErrMsg, SQLIT
19d7b 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 E_UTF8, SQLITE_T
19d7c 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 RANSIENT);. s
19d7d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19d7e 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
19d7f 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 p->zErrMsg = 0
19d80 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c ;. }.. /* Recl
19d81 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 aim all memory u
19d82 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a sed by the VDBE.
19d83 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 */. Cleanup(p
19d84 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 );.. /* Save pr
19d85 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 ofiling informat
19d86 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 ion from this VD
19d87 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 BE run.. */.#if
19d88 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
19d89 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f . {. FILE *o
19d8a 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 ut = fopen("vdbe
19d8b 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 _profile.out", "
19d8c 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 a");. if( out
19d8d 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b ){. int i;
19d8e 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
19d8f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 ut, "---- ");.
19d90 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
19d91 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nOp; i++){.
19d92 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
19d93 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 , "%02x", p->aOp
19d94 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 [i].opcode);.
19d95 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e }. fprin
19d96 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 tf(out, "\n");.
19d97 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
19d98 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 p->nOp; i++){.
19d99 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
19d9a 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 t, "%6d %10lld %
19d9b 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 8lld ",.
19d9c 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 p->aOp[i].cnt
19d9d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
19d9e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 aOp[i].cycles,.
19d9f 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 p->aOp
19da0 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 [i].cnt>0 ? p->a
19da1 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e Op[i].cycles/p->
19da2 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 aOp[i].cnt : 0.
19da3 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
19da4 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
19da5 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d ntOp(out, i, &p-
19da6 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 >aOp[i]);.
19da7 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f }. fclose(o
19da8 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 ut);. }. }.#
19da9 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 endif. p->magic
19daa 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e = VDBE_MAGIC_IN
19dab 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e IT;. return p->
19dac 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b rc & db->errMask
19dad 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 ;.}. ./*.** Clea
19dae 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 n up and delete
19daf 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 a VDBE after exe
19db0 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 cution. Return
19db1 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
19db2 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c is.** the resul
19db3 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 t code. Write a
19db4 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ny error message
19db5 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 text into *pzEr
19db6 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rMsg..*/.SQLITE_
19db7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19db8 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 te3VdbeFinalize(
19db9 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
19dba 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
19dbb 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d if( p->magic==
19dbc 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c VDBE_MAGIC_RUN |
19dbd 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 | p->magic==VDBE
19dbe 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 _MAGIC_HALT ){.
19dbf 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
19dc0 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 dbeReset(p);.
19dc1 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 assert( (rc & p
19dc2 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d ->db->errMask)==
19dc3 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rc );. }else if
19dc4 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 ( p->magic!=VDBE
19dc5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 _MAGIC_INIT ){.
19dc6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19dc7 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 _MISUSE;. }. s
19dc8 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
19dc9 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
19dca 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 ;.}../*.** Call
19dcb 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
19dcc 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 or each auxdata
19dcd 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 entry in pVdbeFu
19dce 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 nc for which.**
19dcf 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
19dd0 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 g bit in mask is
19dd1 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 clear. Auxdata
19dd2 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 entries beyond
19dd3 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 31.** are always
19dd4 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 destroyed. To
19dd5 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 destroy all auxd
19dd6 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c ata entries, cal
19dd7 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e l this.** routin
19dd8 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a e with mask==0..
19dd9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19dda 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19ddb 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 beDeleteAuxData(
19ddc 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 VdbeFunc *pVdbeF
19ddd 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a unc, int mask){.
19dde 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
19ddf 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d =0; i<pVdbeFunc-
19de0 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nAux; i++){.
19de1 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 struct AuxData
19de2 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 *pAux = &pVdbeFu
19de3 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 nc->apAux[i];.
19de4 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 if( (i>31 || !
19de5 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c (mask&(((u32)1)<
19de6 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 <i))) && pAux->p
19de7 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Aux ){. if(
19de8 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 pAux->xDelete )
19de9 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e {. pAux->
19dea 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 xDelete(pAux->pA
19deb 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ux);. }.
19dec 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 pAux->pAux =
19ded 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
19dee 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 /*.** Delete an
19def 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a entire VDBE..*/.
19df0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19df1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
19df2 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a elete(Vdbe *p){.
19df3 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 int i;. sqlit
19df4 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 e3 *db;.. if( p
19df5 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
19df6 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 db = p->db;. if
19df7 28 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 ( p->pPrev ){.
19df8 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 p->pPrev->pNex
19df9 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 t = p->pNext;.
19dfa 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
19dfb 74 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 t( db->pVdbe==p
19dfc 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 );. db->pVdbe
19dfd 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d = p->pNext;. }
19dfe 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 . if( p->pNext
19dff 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d ){. p->pNext-
19e00 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 >pPrev = p->pPre
19e01 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e v;. }. if( p->
19e02 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 aOp ){. Op *p
19e03 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 Op = p->aOp;.
19e04 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
19e05 4f 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b Op; i++, pOp++){
19e06 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 . freeP4(db
19e07 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 , pOp->p4type, p
19e08 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 Op->p4.p);.#ifde
19e09 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
19e0a 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
19e0b 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d ee(db, pOp->zCom
19e0c 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 ment);.#endif
19e0d 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 . }. }. r
19e0e 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
19e0f 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29 ->aVar, p->nVar)
19e10 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
19e11 65 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 e(db, p->aLabel)
19e12 3b 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 ;. releaseMemAr
19e13 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c ray(p->aColName,
19e14 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 p->nResColumn*C
19e15 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c OLNAME_N);. sql
19e16 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
19e17 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 ->aColName);. s
19e18 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19e19 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e p->zSql);. p->
19e1a 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
19e1b 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 IC_DEAD;. sqlit
19e1c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
19e1d 61 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 aOp);. sqlite3D
19e1e 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 bFree(db, p->pFr
19e1f 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ee);. sqlite3Db
19e20 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a Free(db, p);.}..
19e21 2f 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 /*.** If a MoveT
19e22 6f 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 o operation is p
19e23 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 ending on the gi
19e24 76 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e ven cursor, then
19e25 20 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 do that.** Move
19e26 54 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20 To now. Return
19e27 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 an error code.
19e28 49 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20 If no MoveTo is
19e29 70 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a pending, this.**
19e2a 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
19e2b 74 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e thing and return
19e2c 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a s SQLITE_OK..*/.
19e2d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19e2e 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 nt sqlite3VdbeCu
19e2f 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 rsorMoveto(VdbeC
19e30 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 ursor *p){. if(
19e31 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 p->deferredMove
19e32 74 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 to ){. int re
19e33 73 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 s, rc;.#ifdef SQ
19e34 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 LITE_TEST. ex
19e35 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
19e36 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 _search_count;.#
19e37 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 endif. assert
19e38 28 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a ( p->isTable );.
19e39 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19e3a 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
19e3b 6b 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 ked(p->pCursor,
19e3c 30 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 0, p->movetoTarg
19e3d 65 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 et, 0, &res);.
19e3e 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
19e3f 6e 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 n rc;. p->las
19e40 74 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e tRowid = keyToIn
19e41 74 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 t(p->movetoTarge
19e42 74 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 t);. p->rowid
19e43 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 IsValid = ALWAYS
19e44 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 (res==0) ?1:0;.
19e45 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 if( NEVER(res
19e46 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 <0) ){. rc
19e47 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
19e48 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 xt(p->pCursor, &
19e49 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 res);. if(
19e4a 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
19e4b 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
19e4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c ITE_TEST. sql
19e4d 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
19e4e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 t++;.#endif.
19e4f 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
19e50 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 o = 0;. p->ca
19e51 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
19e52 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 E_STALE;. }else
19e53 20 69 66 28 20 70 2d 3e 70 43 75 72 73 6f 72 20 if( p->pCursor
19e54 29 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f ){. int hasMo
19e55 76 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 ved;. int rc
19e56 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 = sqlite3BtreeCu
19e57 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e rsorHasMoved(p->
19e58 70 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 pCursor, &hasMov
19e59 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ed);. if( rc
19e5a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
19e5b 20 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b if( hasMoved ){
19e5c 0a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53 . p->cacheS
19e5d 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
19e5e 41 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 ALE;. p->nu
19e5f 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d llRow = 1;. }
19e60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
19e61 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
19e62 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
19e63 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a functions:.**.**
19e64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
19e65 61 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 alType().** sqli
19e66 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
19e67 65 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 eLen().** sqlite
19e68 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 3VdbeSerialLen()
19e69 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
19e6a 65 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 erialPut().** sq
19e6b 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
19e6c 65 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 et().**.** encap
19e6d 73 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20 sulate the code
19e6e 74 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20 that serializes
19e6f 76 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61 values for stora
19e70 67 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 ge in SQLite.**
19e71 64 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72 data and index r
19e72 65 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72 ecords. Each ser
19e73 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f ialized value co
19e74 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 nsists of a.** '
19e75 73 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 serial-type' and
19e76 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e a blob of data.
19e77 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 The serial type
19e78 20 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e is an 8-byte un
19e79 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
19e7a 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76 r, stored as a v
19e7b 61 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 arint..**.** In
19e7c 61 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20 an SQLite index
19e7d 72 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69 record, the seri
19e7e 61 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65 al type is store
19e7f 64 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 d directly befor
19e80 65 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 e.** the blob of
19e81 20 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f data that it co
19e82 72 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e rresponds to. In
19e83 20 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c a table record,
19e84 20 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 all serial.** t
19e85 79 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 ypes are stored
19e86 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
19e87 74 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 the record, and
19e88 74 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 the blobs of dat
19e89 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e a at.** the end.
19e8a 20 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e Hence these fun
19e8b 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 ctions allow the
19e8c 20 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c caller to handl
19e8d 65 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d e the.** serial-
19e8e 74 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c type and data bl
19e8f 6f 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a ob seperately..*
19e90 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
19e91 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62 ng table describ
19e92 65 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 es the various s
19e93 74 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66 torage classes f
19e94 6f 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 or data:.**.**
19e95 20 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20 serial type
19e96 20 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74 bytes of dat
19e97 61 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 a type.**
19e98 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 --------------
19e99 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ------------
19e9a 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d --- ---------
19e9b 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 ------.** 0
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e9d 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 0
19e9e 20 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 NULL.** 1
19e9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ea0 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 1
19ea1 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 signed integer
19ea2 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20 .** 2
19ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
19ea4 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
19ea5 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
19ea6 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 3
19ea7 20 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20 3
19ea8 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 signed int
19ea9 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 eger.** 4
19eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eab 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 4
19eac 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
19ead 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20 * 5
19eae 20 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20 6
19eaf 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
19eb0 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
19eb1 20 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6
19eb2 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 8
19eb3 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
19eb4 65 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 er.** 7
19eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eb6 20 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45 8 IE
19eb7 45 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 EE float.**
19eb8 20 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 8
19eb9 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 0
19eba 20 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 Integer cons
19ebb 74 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 tant 0.** 9
19ebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ebd 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 0
19ebe 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 Integer consta
19ebf 6e 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 nt 1.** 10,1
19ec0 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1
19ec1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ec2 72 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70 reserved for exp
19ec3 61 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d ansion.** N>=
19ec4 31 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20 12 and even
19ec5 20 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 (N-12)/2
19ec6 20 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d BLOB.** N>=
19ec7 31 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20 13 and odd
19ec8 20 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 (N-13)/2
19ec9 20 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 text.**.** The
19eca 20 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77 8 and 9 types w
19ecb 65 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33 ere added in 3.3
19ecc 2e 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 .0, file format
19ecd 34 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f 4. Prior versio
19ece 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 ns.** of SQLite
19ecf 77 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 will not underst
19ed0 61 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c and those serial
19ed1 20 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a types..*/../*.*
19ed2 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72 * Return the ser
19ed3 69 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65 ial-type for the
19ed4 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
19ed5 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pMem..*/.SQLITE
19ed6 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
19ed7 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
19ed8 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e pe(Mem *pMem, in
19ed9 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a t file_format){.
19eda 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d int flags = pM
19edb 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 em->flags;. int
19edc 20 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 n;.. if( flags
19edd 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 &MEM_Null ){.
19ede 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
19edf 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 if( flags&MEM_I
19ee0 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 nt ){. /* Fig
19ee1 75 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20 ure out whether
19ee2 74 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 to use 1, 2, 4,
19ee3 36 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 6 or 8 bytes. */
19ee4 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f .# define MAX_
19ee5 36 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78 6BYTE ((((i64)0x
19ee6 30 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 00008000)<<32)-1
19ee7 29 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d ). i64 i = pM
19ee8 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 em->u.i;. u64
19ee9 20 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65 u;. if( file
19eea 5f 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 _format>=4 && (i
19eeb 26 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 &1)==i ){.
19eec 72 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b return 8+(u32)i;
19eed 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 . }. u = i
19eee 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 <0 ? -i : i;.
19eef 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 if( u<=127 ) re
19ef0 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 turn 1;. if(
19ef1 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 u<=32767 ) retur
19ef2 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d n 2;. if( u<=
19ef3 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 8388607 ) return
19ef4 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 3;. if( u<=2
19ef5 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75 147483647 ) retu
19ef6 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c rn 4;. if( u<
19ef7 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 =MAX_6BYTE ) ret
19ef8 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 urn 5;. retur
19ef9 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 n 6;. }. if( f
19efa 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b lags&MEM_Real ){
19efb 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 . return 7;.
19efc 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 }. assert( pMe
19efd 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 m->db->mallocFai
19efe 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 led || flags&(ME
19eff 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
19f00 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e );. n = pMem->n
19f01 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
19f02 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
19f03 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 n += pMem->u.nZe
19f04 72 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ro;. }. assert
19f05 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 ( n>=0 );. retu
19f06 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b rn ((n*2) + 12 +
19f07 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 ((flags&MEM_Str
19f08 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a )!=0));.}../*.**
19f09 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 Return the leng
19f0a 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63 th of the data c
19f0b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
19f0c 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72 the supplied ser
19f0d 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c ial-type..*/.SQL
19f0e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19f0f 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
19f10 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72 lTypeLen(u32 ser
19f11 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 ial_type){. if(
19f12 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 serial_type>=12
19f13 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
19f14 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f serial_type-12)/
19f15 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2;. }else{.
19f16 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 static const u8
19f17 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 aSize[] = { 0, 1
19f18 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c , 2, 3, 4, 6, 8,
19f19 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 8, 0, 0, 0, 0 }
19f1a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69 ;. return aSi
19f1b 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b ze[serial_type];
19f1c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
19f1d 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 we are on an ar
19f1e 63 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20 chitecture with
19f1f 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f mixed-endian flo
19f20 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 ating .** points
19f21 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e (ex: ARM7) then
19f22 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20 swap the lower
19f23 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65 4 bytes with the
19f24 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74 .** upper 4 byt
19f25 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 es. Return the
19f26 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f result..**.** Fo
19f27 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74 r most architect
19f28 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20 ures, this is a
19f29 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 no-op..**.** (la
19f2a 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70 ter): It is rep
19f2b 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74 orted to me that
19f2c 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 the mixed-endia
19f2d 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 n problem.** on
19f2e 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65 ARM7 is an issue
19f2f 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 with GCC, not w
19f30 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69 ith the ARM7 chi
19f31 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 p. It seems.**
19f32 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69 that early versi
19f33 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65 ons of GCC store
19f34 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20 d the two words
19f35 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 of a 64-bit.** f
19f36 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e loat in the wron
19f37 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68 g order. And th
19f38 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 at error has bee
19f39 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 n propagated.**
19f3a 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65 ever since. The
19f3b 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 blame is not ne
19f3c 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47 cessarily with G
19f3d 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 CC, though..** G
19f3e 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75 CC might have ju
19f3f 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70 st copying the p
19f40 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 roblem from a pr
19f41 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a ior compiler..**
19f42 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 I am also told
19f43 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69 that newer versi
19f44 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20 ons of GCC that
19f45 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65 follow a differe
19f46 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68 nt.** ABI get th
19f47 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67 e byte order rig
19f48 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f ht..**.** Develo
19f49 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74 pers using SQLit
19f4a 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f e on an ARM7 sho
19f4b 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 uld compile and
19f4c 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 run their.** app
19f4d 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d lication using -
19f4e 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 DSQLITE_DEBUG=1
19f4f 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 at least once.
19f50 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e With DEBUG.** en
19f51 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 abled, some asse
19f52 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 rts below will e
19f53 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62 nsure that the b
19f54 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 yte order of.**
19f55 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
19f56 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74 alues is correct
19f57 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 ..**.** (2007-08
19f58 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 -30) Frank van
19f59 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64 Vugt has studied
19f5a 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c this problem cl
19f5b 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 osely.** and has
19f5c 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e send his findin
19f5d 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65 gs to the SQLite
19f5e 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 developers. Fr
19f5f 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 ank.** writes th
19f60 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 at some Linux ke
19f61 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 rnels offer floa
19f62 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77 ting point hardw
19f63 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e are.** emulation
19f64 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 that uses only
19f65 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73 32-bit mantissas
19f66 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75 instead of a fu
19f67 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 ll .** 48-bits a
19f68 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 s required by th
19f69 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e e IEEE standard.
19f6a 20 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a (This is the.*
19f6b 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 * CONFIG_FPE_FAS
19f6c 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f TFPE option.) O
19f6d 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20 n such systems,
19f6e 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a floating point.*
19f6f 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 * byte swapping
19f70 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d becomes very com
19f71 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76 plicated. To av
19f72 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a oid problems,.**
19f73 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62 the necessary b
19f74 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20 yte swapping is
19f75 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e carried out usin
19f76 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 g a 64-bit integ
19f77 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61 er.** rather tha
19f78 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 n a 64-bit float
19f79 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73 . Frank assures
19f7a 20 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64 us that the cod
19f7b 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 e here.** works
19f7c 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 for him. We, th
19f7d 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 e developers, ha
19f7e 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 ve no way to ind
19f7f 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 ependently.** ve
19f80 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46 rify this, but F
19f81 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e rank seems to kn
19f82 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61 ow what he is ta
19f83 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 lking about.** s
19f84 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a o we trust him..
19f85 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
19f86 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 _MIXED_ENDIAN_64
19f87 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 BIT_FLOAT.static
19f88 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75 u64 floatSwap(u
19f89 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 64 in){. union
19f8a 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20 {. u64 r;.
19f8b 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 u32 i[2];. } u
19f8c 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e ;. u32 t;.. u.
19f8d 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e r = in;. t = u.
19f8e 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d i[0];. u.i[0] =
19f8f 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 u.i[1];. u.i[1
19f90 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 ] = t;. return
19f91 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 u.r;.}.# define
19f92 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 swapMixedEndianF
19f93 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f loat(X) X = flo
19f94 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a atSwap(X).#else.
19f95 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 # define swapMix
19f96 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 edEndianFloat(X)
19f97 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
19f98 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69 rite the seriali
19f99 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f zed data blob fo
19f9a 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 r the value stor
19f9b 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 ed in pMem into
19f9c 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61 .** buf. It is a
19f9d 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
19f9e 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 caller has alloc
19f9f 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20 ated sufficient
19fa0 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e space..** Return
19fa1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
19fa2 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a ytes written..**
19fa3 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20 .** nBuf is the
19fa4 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 amount of space
19fa5 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 left in buf[].
19fa6 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73 nBuf must always
19fa7 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f be.** large eno
19fa8 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ugh to hold the
19fa9 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45 entire field. E
19faa 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69 xcept, if the fi
19fab 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 eld is.** a blob
19fac 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c with a zero-fil
19fad 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 led tail, then b
19fae 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 uf[] might be ju
19faf 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 st the right.**
19fb0 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 size to hold eve
19fb1 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66 rything except f
19fb2 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c or the zero-fill
19fb3 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66 ed tail. If buf
19fb4 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 [].** is only bi
19fb5 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 g enough to hold
19fb6 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 the non-zero pr
19fb7 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 efix, then only
19fb8 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 write that.** pr
19fb9 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e efix into buf[].
19fba 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69 But if buf[] i
19fbb 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 s large enough t
19fbc 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a o hold both the.
19fbd 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68 ** prefix and th
19fbe 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74 e tail then writ
19fbf 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64 e the prefix and
19fc0 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f set the tail to
19fc1 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a all.** zeros..*
19fc2 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
19fc3 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
19fc4 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e actually written
19fc5 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 into buf[]. Th
19fc6 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 e number.** of b
19fc7 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f ytes in the zero
19fc8 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 -filled tail is
19fc9 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 included in the
19fca 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c return value onl
19fcb 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79 y.** if those by
19fcc 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20 tes were zeroed
19fcd 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 in buf[]..*/ .SQ
19fce 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19fcf 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
19fd0 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69 alPut(u8 *buf, i
19fd1 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d nt nBuf, Mem *pM
19fd2 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 em, int file_for
19fd3 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69 mat){. u32 seri
19fd4 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 al_type = sqlite
19fd5 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
19fd6 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 pMem, file_forma
19fd7 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a t);. int len;..
19fd8 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 /* Integer and
19fd9 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 Real */. if( s
19fda 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 erial_type<=7 &&
19fdb 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 serial_type>0 )
19fdc 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20 {. u64 v;.
19fdd 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20 int i;. if(
19fde 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 serial_type==7 )
19fdf 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
19fe0 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f sizeof(v)==sizeo
19fe1 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 f(pMem->r) );.
19fe2 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 memcpy(&v, &
19fe3 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 pMem->r, sizeof(
19fe4 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d v));. swapM
19fe5 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
19fe6 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 v);. }else{.
19fe7 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 v = pMem->u
19fe8 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 .i;. }. le
19fe9 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56 n = i = sqlite3V
19fea 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
19feb 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 (serial_type);.
19fec 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d assert( len<=
19fed 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c nBuf );. whil
19fee 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 e( i-- ){.
19fef 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 buf[i] = (u8)(v&
19ff0 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 0xFF);. v >
19ff1 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 >= 8;. }.
19ff2 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a return len;. }.
19ff3 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 . /* String or
19ff4 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 blob */. if( se
19ff5 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b rial_type>=12 ){
19ff6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
19ff7 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 m->n + ((pMem->f
19ff8 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 lags & MEM_Zero)
19ff9 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 ?pMem->u.nZero:0
19ffa 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d ). =
19ffb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
19ffc 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
19ffd 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 l_type) );. a
19ffe 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d ssert( pMem->n<=
19fff 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 nBuf );. len
1a000 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d = pMem->n;. m
1a001 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d emcpy(buf, pMem-
1a002 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 >z, len);. if
1a003 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
1a004 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
1a005 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 len += pMem->u
1a006 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 69 66 .nZero;. if
1a007 28 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20 ( len>nBuf ){.
1a008 20 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66 len = nBuf
1a009 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1a00a 6d 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d memset(&buf[pMem
1a00b 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 ->n], 0, len-pMe
1a00c 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 m->n);. }.
1a00d 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d return len;. }
1a00e 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 .. /* NULL or c
1a00f 6f 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 onstants 0 or 1
1a010 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
1a011 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c ../*.** Deserial
1a012 69 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f ize the data blo
1a013 62 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 b pointed to by
1a014 62 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 buf as serial ty
1a015 70 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a pe serial_type.*
1a016 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1a017 72 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 result in pMem.
1a018 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1a019 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 er of bytes read
1a01a 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
1a01b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1a01c 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 VdbeSerialGet(.
1a01d 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1a01e 63 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f char *buf, /
1a01f 2a 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 * Buffer to dese
1a020 72 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a rialize from */.
1a021 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1a022 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
1a023 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 /* Serial type t
1a024 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f o deserialize */
1a025 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 . Mem *pMem
1a026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a027 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 /* Memory cell
1a028 74 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 to write value i
1a029 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 nto */.){. swit
1a02a 63 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 ch( serial_type
1a02b 29 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 ){. case 10:
1a02c 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f /* Reserved fo
1a02d 72 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a r future use */.
1a02e 20 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f case 11: /
1a02f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 * Reserved for f
1a030 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 uture use */.
1a031 20 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e case 0: { /* N
1a032 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ULL */. pMe
1a033 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e m->flags = MEM_N
1a034 75 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ull;. break
1a035 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1a036 20 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 1: { /* 1-byte
1a037 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
1a038 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1a039 69 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 i = (signed char
1a03a 29 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 )buf[0];. p
1a03b 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a03c 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
1a03d 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
1a03e 63 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 case 2: { /* 2-b
1a03f 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
1a040 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d er */. pMem
1a041 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 ->u.i = (((signe
1a042 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c d char)buf[0])<<
1a043 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 8) | buf[1];.
1a044 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a045 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1a046 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a return 2;. }.
1a047 20 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a case 3: { /*
1a048 20 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 3-byte signed i
1a049 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
1a04a 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 pMem->u.i = (((s
1a04b 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 igned char)buf[0
1a04c 5d 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 ])<<16) | (buf[1
1a04d 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a ]<<8) | buf[2];.
1a04e 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1a04f 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a050 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 return 3;.
1a051 20 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b }. case 4: {
1a052 20 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 /* 4-byte signe
1a053 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
1a054 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 pMem->u.i = (
1a055 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 buf[0]<<24) | (b
1a056 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 uf[1]<<16) | (bu
1a057 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 f[2]<<8) | buf[3
1a058 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ];. pMem->f
1a059 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1a05a 20 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a return 4;.
1a05b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 }. case 5
1a05c 3a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 : { /* 6-byte si
1a05d 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
1a05e 20 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 u64 x = ((
1a05f 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 (signed char)buf
1a060 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 [0])<<8) | buf[1
1a061 5d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d ];. u32 y =
1a062 20 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 (buf[2]<<24) |
1a063 28 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 (buf[3]<<16) | (
1a064 62 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 buf[4]<<8) | buf
1a065 5b 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 [5];. x = (
1a066 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 x<<32) | y;.
1a067 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 pMem->u.i = *(
1a068 69 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 i64*)&x;. p
1a069 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a06a 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
1a06b 72 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 6;. }.
1a06c 63 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 case 6: /* 8-b
1a06d 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
1a06e 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 er */. case 7
1a06f 3a 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 : { /* IEEE floa
1a070 74 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 ting point */.
1a071 20 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 u64 x;.
1a072 20 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 u32 y;.#if !def
1a073 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
1a074 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1a075 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1a076 49 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 INT). /* Ve
1a077 72 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 rify that intege
1a078 72 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 rs and floating
1a079 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 point values use
1a07a 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 the same.
1a07b 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 ** byte order.
1a07c 4f 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 Or, that if SQLI
1a07d 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f TE_MIXED_ENDIAN_
1a07e 36 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 64BIT_FLOAT is.
1a07f 20 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 ** defined
1a080 74 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 that 64-bit floa
1a081 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
1a082 73 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 s really are mix
1a083 65 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 ed. ** endi
1a084 61 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 an.. */.
1a085 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
1a086 75 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 u64 t1 = ((u64)0
1a087 78 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a x3ff00000)<<32;.
1a088 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
1a089 73 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 st double r1 = 1
1a08a 2e 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 .0;. u64 t2
1a08b 20 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 = t1;. swa
1a08c 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 pMixedEndianFloa
1a08d 74 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 t(t2);. ass
1a08e 65 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d ert( sizeof(r1)=
1a08f 3d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d =sizeof(t2) && m
1a090 65 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 emcmp(&r1, &t2,
1a091 73 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 sizeof(r1))==0 )
1a092 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 ;.#endif..
1a093 78 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 x = (buf[0]<<24)
1a094 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 | (buf[1]<<16)
1a095 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 | (buf[2]<<8) |
1a096 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 buf[3];. y
1a097 3d 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c = (buf[4]<<24) |
1a098 20 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 (buf[5]<<16) |
1a099 28 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 (buf[6]<<8) | bu
1a09a 66 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 f[7];. x =
1a09b 28 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 (x<<32) | y;.
1a09c 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 if( serial_ty
1a09d 70 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 pe==6 ){.
1a09e 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 pMem->u.i = *(i
1a09f 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 64*)&x;.
1a0a0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1a0a1 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c M_Int;. }el
1a0a2 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 se{. asse
1a0a3 72 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 rt( sizeof(x)==8
1a0a4 20 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d && sizeof(pMem-
1a0a5 3e 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 >r)==8 );.
1a0a6 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 swapMixedEndia
1a0a7 6e 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 nFloat(x);.
1a0a8 20 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d memcpy(&pMem-
1a0a9 3e 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 >r, &x, sizeof(x
1a0aa 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d ));. pMem
1a0ab 2d 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 ->flags = sqlite
1a0ac 33 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3IsNaN(pMem->r)
1a0ad 3f 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d ? MEM_Null : MEM
1a0ae 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 _Real;. }.
1a0af 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 return 8;.
1a0b0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a }. case 8:
1a0b1 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 /* Integer 0
1a0b2 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 */. case 9:
1a0b3 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 { /* Integer 1
1a0b4 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 */. pMem->u
1a0b5 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 .i = serial_type
1a0b6 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e -8;. pMem->
1a0b7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1a0b8 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
1a0b9 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1a0ba 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 lt: {. int
1a0bb 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 len = (serial_ty
1a0bc 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 pe-12)/2;.
1a0bd 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 pMem->z = (char
1a0be 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 *)buf;. pMe
1a0bf 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 m->n = len;.
1a0c0 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 pMem->xDel = 0
1a0c1 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 ;. if( seri
1a0c2 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a al_type&0x01 ){.
1a0c3 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
1a0c4 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 ags = MEM_Str |
1a0c5 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
1a0c6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1a0c7 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1a0c8 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 M_Blob | MEM_Eph
1a0c9 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 em;. }.
1a0ca 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 return len;.
1a0cb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1a0cc 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 0;.}.../*.** Gi
1a0cd 76 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 ven the nKey-byt
1a0ce 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 e encoding of a
1a0cf 72 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d record in pKey[]
1a0d0 2c 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 , parse the.** r
1a0d1 65 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 ecord into a Unp
1a0d2 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 ackedRecord stru
1a0d3 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 cture. Return a
1a0d4 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 pointer to.** t
1a0d5 68 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a hat structure..*
1a0d6 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 *.** The calling
1a0d7 20 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 function might
1a0d8 70 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 provide szSpace
1a0d9 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a bytes of memory.
1a0da 2a 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 ** space at pSpa
1a0db 63 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 ce. This space
1a0dc 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 can be used to h
1a0dd 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
1a0de 0a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 .** VDbeParsedRe
1a0df 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 cord structure i
1a0e0 66 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e f it is large en
1a0e1 6f 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a ough. If it is.
1a0e2 2a 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 ** not big enoug
1a0e3 68 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 h, space is obta
1a0e4 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1a0e5 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 3_malloc()..**.*
1a0e6 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 * The returned s
1a0e7 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 tructure should
1a0e8 62 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 be closed by a c
1a0e9 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
1a0ea 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 3VdbeDeleteUnpac
1a0eb 6b 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 kedRecord()..*/
1a0ec 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a0ed 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1a0ee 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
1a0ef 64 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e dUnpack(. KeyIn
1a0f0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
1a0f1 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
1a0f2 20 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 about the recor
1a0f3 64 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e d format */. in
1a0f4 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 t nKey,
1a0f5 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1a0f6 74 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 the binary recor
1a0f7 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
1a0f8 64 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a d *pKey, /*
1a0f9 20 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f The binary reco
1a0fa 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53 rd */. char *pS
1a0fb 70 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f pace, /
1a0fc 2a 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63 * Unaligned spac
1a0fd 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 e available to h
1a0fe 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a old the object *
1a0ff 2f 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20 /. int szSpace
1a100 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1a101 7a 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 ze of pSpace[] i
1a102 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 n bytes */.){.
1a103 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1a104 68 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e har *aKey = (con
1a105 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1a106 20 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63 *)pKey;. Unpac
1a107 6b 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f kedRecord *p; /
1a108 2a 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 * The unpacked r
1a109 65 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69 ecord that we wi
1a10a 6c 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 ll return */. i
1a10b 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 nt nByte;
1a10c 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61 /* Memory spa
1a10d 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c ce needed to hol
1a10e 64 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f d p, in bytes */
1a10f 0a 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20 . int d;. u32
1a110 69 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 idx;. u16 u;
1a111 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
1a112 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e signed loop coun
1a113 74 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 ter */. u32 szH
1a114 64 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b dr;. Mem *pMem;
1a115 0a 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 . int nOff;
1a116 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 /* Increa
1a117 73 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69 se pSpace by thi
1a118 73 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65 s much to 8-byte
1a119 20 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a align it */. .
1a11a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e /*. ** We wan
1a11b 74 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70 t to shift the p
1a11c 6f 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70 ointer pSpace up
1a11d 20 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73 such that it is
1a11e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e 8-byte aligned.
1a11f 0a 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e . ** Thus, we n
1a120 65 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 eed to calculate
1a121 20 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 a value, nOff,
1a122 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c between 0 and 7,
1a123 20 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20 to shift . **
1a124 69 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63 it by. If pSpac
1a125 65 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62 e is already 8-b
1a126 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 yte aligned, nOf
1a127 66 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f f should be zero
1a128 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 .. */. nOff =
1a129 28 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52 (8 - (SQLITE_PTR
1a12a 5f 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20 _TO_INT(pSpace)
1a12b 26 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70 & 7)) & 7;. pSp
1a12c 61 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 ace += nOff;. s
1a12d 7a 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a zSpace -= nOff;.
1a12e 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 nByte = ROUND8
1a12f 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 (sizeof(Unpacked
1a130 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f Record)) + sizeo
1a131 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f f(Mem)*(pKeyInfo
1a132 2d 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 ->nField+1);. i
1a133 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 f( nByte>szSpace
1a134 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 ){. p = sqli
1a135 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
1a136 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 KeyInfo->db, nBy
1a137 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d te);. if( p==
1a138 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
1a139 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 p->flags = UNP
1a13a 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 ACKED_NEED_FREE
1a13b 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f | UNPACKED_NEED_
1a13c 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 DESTROY;. }else
1a13d 7b 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63 {. p = (Unpac
1a13e 6b 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63 kedRecord*)pSpac
1a13f 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 e;. p->flags
1a140 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f = UNPACKED_NEED_
1a141 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 DESTROY;. }. p
1a142 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 ->pKeyInfo = pKe
1a143 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 yInfo;. p->nFie
1a144 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e ld = pKeyInfo->n
1a145 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e Field + 1;. p->
1a146 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d aMem = pMem = (M
1a147 65 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b em*)&((char*)p)[
1a148 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e ROUND8(sizeof(Un
1a149 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b packedRecord))];
1a14a 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
1a14b 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
1a14c 70 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d pMem) );. idx =
1a14d 20 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 getVarint32(aKe
1a14e 79 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d y, szHdr);. d =
1a14f 20 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b szHdr;. u = 0;
1a150 0a 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a . while( idx<sz
1a151 48 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 Hdr && u<p->nFie
1a152 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 ld ){. u32 se
1a153 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 rial_type;..
1a154 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 idx += getVarint
1a155 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 32(&aKey[idx], s
1a156 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 erial_type);.
1a157 20 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20 if( d>=nKey &&
1a158 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1a159 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f lTypeLen(serial_
1a15a 74 79 70 65 29 3e 30 20 29 20 62 72 65 61 6b 3b type)>0 ) break;
1a15b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d . pMem->enc =
1a15c 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a pKeyInfo->enc;.
1a15d 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 pMem->db = p
1a15e 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 KeyInfo->db;.
1a15f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 pMem->flags = 0
1a160 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ;. pMem->zMal
1a161 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b loc = 0;. d +
1a162 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1a163 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c ialGet(&aKey[d],
1a164 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d serial_type, pM
1a165 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b em);. pMem++;
1a166 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 . u++;. }.
1a167 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 assert( u<=pKeyI
1a168 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 nfo->nField + 1
1a169 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d );. p->nField =
1a16a 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f u;. return (vo
1a16b 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 id*)p;.}../*.**
1a16c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 This routine des
1a16d 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 troys a Unpacked
1a16e 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f Record object.*/
1a16f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a170 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a171 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 DeleteUnpackedRe
1a172 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 cord(UnpackedRec
1a173 6f 72 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 ord *p){. if( p
1a174 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 ){. if( p->f
1a175 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1a176 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a NEED_DESTROY ){.
1a177 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1a178 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 Mem *pMem;.
1a179 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 for(i=0, pMe
1a17a 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e m=p->aMem; i<p->
1a17b 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 nField; i++, pMe
1a17c 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 m++){. if
1a17d 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 ( pMem->zMalloc
1a17e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1a17f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1a180 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 se(pMem);.
1a181 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1a182 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 }. if( p->fla
1a183 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 gs & UNPACKED_NE
1a184 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 20 ED_FREE ){.
1a185 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1a186 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 ->pKeyInfo->db,
1a187 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a p);. }. }.}.
1a188 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1a189 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 tion compares th
1a18a 65 20 74 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 e two table rows
1a18b 20 6f 72 20 69 6e 64 65 78 20 72 65 63 6f 72 64 or index record
1a18c 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 s.** specified b
1a18d 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d y {nKey1, pKey1}
1a18e 20 61 6e 64 20 70 50 4b 65 79 32 2e 20 20 49 74 and pPKey2. It
1a18f 20 72 65 74 75 72 6e 73 20 61 20 6e 65 67 61 74 returns a negat
1a190 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 ive, zero.** or
1a191 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 positive integer
1a192 20 69 66 20 6b 65 79 31 20 69 73 20 6c 65 73 73 if key1 is less
1a193 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 than, equal to
1a194 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 or .** greater t
1a195 68 61 6e 20 6b 65 79 32 2e 20 20 54 68 65 20 7b han key2. The {
1a196 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 nKey1, pKey1} ke
1a197 79 20 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 y must be a blob
1a198 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 .** created by t
1a199 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 h OP_MakeRecord
1a19a 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 opcode of the VD
1a19b 42 45 2e 20 20 54 68 65 20 70 50 4b 65 79 32 0a BE. The pPKey2.
1a19c 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 ** key must be a
1a19d 20 70 61 72 73 65 64 20 6b 65 79 20 73 75 63 68 parsed key such
1a19e 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f as obtained fro
1a19f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 m.** sqlite3Vdbe
1a1a0 50 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a ParseRecord..**.
1a1a1 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 ** Key1 and Key2
1a1a2 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 do not have to
1a1a3 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 61 6d 65 contain the same
1a1a4 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 number of field
1a1a5 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 s..** The key wi
1a1a6 74 68 20 66 65 77 65 72 20 66 69 65 6c 64 73 20 th fewer fields
1a1a7 69 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 is usually compa
1a1a8 72 65 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 res less than th
1a1a9 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 e .** longer key
1a1aa 2e 20 20 48 6f 77 65 76 65 72 20 69 66 20 74 68 . However if th
1a1ab 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b e UNPACKED_INCRK
1a1ac 45 59 20 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 EY flags in pPKe
1a1ad 79 32 20 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 y2 is set.** and
1a1ae 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 the common pref
1a1af 69 78 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 ixes are equal,
1a1b0 74 68 65 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 then key1 is les
1a1b1 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 s than key2..**
1a1b2 4f 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b Or if the UNPACK
1a1b3 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 58 20 ED_MATCH_PREFIX
1a1b4 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 flag is set and
1a1b5 74 68 65 20 70 72 65 66 69 78 65 73 20 61 72 65 the prefixes are
1a1b6 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 .** equal, then
1a1b7 74 68 65 20 6b 65 79 73 20 61 72 65 20 63 6f 6e the keys are con
1a1b8 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 sidered to be eq
1a1b9 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 ual and.** the p
1a1ba 61 72 74 73 20 62 65 79 6f 6e 64 20 74 68 65 20 arts beyond the
1a1bb 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 common prefix ar
1a1bc 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a e ignored..**.**
1a1bd 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 If the UNPACKED
1a1be 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c _IGNORE_ROWID fl
1a1bf 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
1a1c0 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 the last byte of
1a1c1 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f .** the header o
1a1c2 66 20 70 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 f pKey1 is ignor
1a1c3 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d ed. It is assum
1a1c4 65 64 20 74 68 61 74 20 70 4b 65 79 31 20 69 73 ed that pKey1 is
1a1c5 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 .** an index key
1a1c6 2c 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 , and thus ends
1a1c7 77 69 74 68 20 61 20 72 6f 77 69 64 20 76 61 6c with a rowid val
1a1c8 75 65 2e 20 20 54 68 65 20 6c 61 73 74 20 62 79 ue. The last by
1a1c9 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 te.** of the hea
1a1ca 64 65 72 20 77 69 6c 6c 20 74 68 65 72 65 66 6f der will therefo
1a1cb 72 65 20 62 65 20 74 68 65 20 73 65 72 69 61 6c re be the serial
1a1cc 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 type of the row
1a1cd 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c id:.** one of 1,
1a1ce 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 2, 3, 4, 5, 6,
1a1cf 38 2c 20 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 8, or 9 - the in
1a1d0 74 65 67 65 72 20 73 65 72 69 61 6c 20 74 79 70 teger serial typ
1a1d1 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 es..** The seria
1a1d2 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 69 l type of the fi
1a1d3 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 nal rowid will a
1a1d4 6c 77 61 79 73 20 62 65 20 61 20 73 69 6e 67 6c lways be a singl
1a1d5 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 e byte..** By ig
1a1d6 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c 61 73 74 noring this last
1a1d7 20 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 byte of the hea
1a1d8 64 65 72 2c 20 77 65 20 66 6f 72 63 65 20 74 68 der, we force th
1a1d9 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 e comparison.**
1a1da 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 72 6f to ignore the ro
1a1db 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f wid at the end o
1a1dc 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 f key1..*/.SQLIT
1a1dd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1a1de 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
1a1df 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b ompare(. int nK
1a1e0 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 ey1, const void
1a1e1 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 *pKey1, /* Left
1a1e2 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 key */. Unpacke
1a1e3 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 dRecord *pPKey2
1a1e4 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 /* Right
1a1e5 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 key */.){. int
1a1e6 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f d1; /
1a1e7 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b * Offset into aK
1a1e8 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 ey[] of next dat
1a1e9 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 a element */. u
1a1ea 33 32 20 69 64 78 31 3b 20 20 20 20 20 20 20 20 32 idx1;
1a1eb 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
1a1ec 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 aKey[] of next
1a1ed 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a header element *
1a1ee 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 3b 20 /. u32 szHdr1;
1a1ef 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1a1f0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68 65 61 of bytes in hea
1a1f1 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d der */. int i =
1a1f2 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 0;. int nField
1a1f3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a ;. int rc = 0;.
1a1f4 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
1a1f5 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 char *aKey1 = (
1a1f6 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1a1f7 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b har *)pKey1;. K
1a1f8 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
1a1f9 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 ;. Mem mem1;..
1a1fa 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 pKeyInfo = pPKe
1a1fb 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 y2->pKeyInfo;.
1a1fc 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 mem1.enc = pKeyI
1a1fd 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 nfo->enc;. mem1
1a1fe 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e .db = pKeyInfo->
1a1ff 64 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 db;. mem1.flags
1a200 20 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69 = 0;. mem1.u.i
1a201 20 3d 20 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65 = 0; /* not ne
1a202 65 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 eded, here to si
1a203 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 lence compiler w
1a204 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31 arning */. mem1
1a205 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 .zMalloc = 0;.
1a206 0a 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 . idx1 = getVar
1a207 69 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 int32(aKey1, szH
1a208 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 dr1);. d1 = szH
1a209 64 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 dr1;. if( pPKey
1a20a 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 2->flags & UNPAC
1a20b 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 KED_IGNORE_ROWID
1a20c 20 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d ){. szHdr1--
1a20d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d ;. }. nField =
1a20e 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c pKeyInfo->nFiel
1a20f 64 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 d;. while( idx1
1a210 3c 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b <szHdr1 && i<pPK
1a211 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 ey2->nField ){.
1a212 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 u32 serial_ty
1a213 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 pe1;.. /* Rea
1a214 64 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 d the serial typ
1a215 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 es for the next
1a216 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 element in each
1a217 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 key. */. idx1
1a218 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 += getVarint32(
1a219 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 aKey1+idx1, ser
1a21a 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 ial_type1 );.
1a21b 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 if( d1>=nKey1 &
1a21c 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 & sqlite3VdbeSer
1a21d 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
1a21e 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 l_type1)>0 ) bre
1a21f 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 ak;.. /* Extr
1a220 61 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 act the values t
1a221 6f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 o be compared..
1a222 20 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 */. d1 +=
1a223 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1a224 6c 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c lGet(&aKey1[d1],
1a225 20 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 serial_type1, &
1a226 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 mem1);.. /* D
1a227 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e o the comparison
1a228 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
1a229 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 sqlite3MemCompa
1a22a 72 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 re(&mem1, &pPKey
1a22b 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 2->aMem[i],.
1a22c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a22d 20 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 i<nField
1a22e 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c ? pKeyInfo->aCol
1a22f 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 l[i] : 0);. i
1a230 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 f( rc!=0 ){.
1a231 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a232 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 i++;. }. if
1a233 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 ( mem1.zMalloc )
1a234 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1a235 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a elease(&mem1);..
1a236 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 /* If the PREF
1a237 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 IX_SEARCH flag i
1a238 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 s set and all fi
1a239 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 elds except the
1a23a 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64 final. ** rowid
1a23b 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 field were equa
1a23c 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 l, then clear th
1a23d 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 e PREFIX_SEARCH
1a23e 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 flag and set .
1a23f 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 ** pPKey2->rowid
1a240 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1a241 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 the rowid field
1a242 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 in (pKey1, nKey
1a243 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 1).. ** This is
1a244 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f used by the OP_
1a245 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e IsUnique opcode.
1a246 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b . */. if( (pPK
1a247 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
1a248 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1a249 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 RCH) && i==(pPKe
1a24a 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b y2->nField-1) ){
1a24b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
1a24c 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 1==szHdr1 && rc
1a24d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d );. assert( m
1a24e 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em1.flags & MEM_
1a24f 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79 Int );. pPKey
1a250 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 2->flags &= ~UNP
1a251 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1a252 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d RCH;. pPKey2-
1a253 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e >rowid = mem1.u.
1a254 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 i;. }.. if( rc
1a255 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63 ==0 ){. /* rc
1a256 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74 ==0 here means t
1a257 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b hat one of the k
1a258 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 eys ran out of f
1a259 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a ields and. **
1a25a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20 all the fields
1a25b 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74 up to that point
1a25c 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 were equal. If
1a25d 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 the UNPACKED_INC
1a25e 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67 RKEY. ** flag
1a25f 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 is set, then br
1a260 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 eak the tie by t
1a261 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 reating key2 as
1a262 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49 larger.. ** I
1a263 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52 f the UPACKED_PR
1a264 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20 EFIX_MATCH flag
1a265 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79 is set, then key
1a266 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 s with common pr
1a267 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72 efixes. ** ar
1a268 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 e considered to
1a269 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 be equal. Other
1a26a 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 wise, the longer
1a26b 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20 key is the .
1a26c 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 ** larger. As
1a26d 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 it happens, the
1a26e 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 pPKey2 will alwa
1a26f 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 ys be the longer
1a270 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 . ** if there
1a271 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 is a difference
1a272 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1a273 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 pPKey2->flags &
1a274 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
1a275 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 Y ){. rc =
1a276 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 -1;. }else if
1a277 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 ( pPKey2->flags
1a278 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1a279 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20 X_MATCH ){.
1a27a 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 /* Leave rc==0
1a27b 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 */. }else if(
1a27c 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a idx1<szHdr1 ){.
1a27d 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 rc = 1;.
1a27e 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
1a27f 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
1a280 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e rder && i<pKeyIn
1a281 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20 fo->nField.
1a282 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65 && pKe
1a283 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1a284 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d r[i] ){. rc =
1a285 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 -rc;. }.. ret
1a286 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a urn rc;.}. ../*.
1a287 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 ** pCur points a
1a288 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 t an index entry
1a289 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 created using t
1a28a 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 he OP_MakeRecord
1a28b 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 opcode..** Read
1a28c 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20 the rowid (the
1a28d 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68 last field in th
1a28e 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74 e record) and st
1a28f 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64 ore it in *rowid
1a290 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 ..** Return SQLI
1a291 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
1a292 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e ing works, or an
1a293 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 error code othe
1a294 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 rwise..**.** pCu
1a295 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 r might be point
1a296 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61 ing to text obta
1a297 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72 ined from a corr
1a298 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c upt database fil
1a299 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e e..** So the con
1a29a 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74 tent cannot be t
1a29b 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72 rusted. Do appr
1a29c 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f opriate checks o
1a29d 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a n the content..*
1a29e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a29f 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1a2a0 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f IdxRowid(BtCurso
1a2a1 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f r *pCur, i64 *ro
1a2a2 77 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c wid){. i64 nCel
1a2a3 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 lKey = 0;. int
1a2a4 72 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b rc;. u32 szHdr;
1a2a5 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
1a2a6 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f of the header */
1a2a7 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 . u32 typeRowid
1a2a8 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 ; /* Serial t
1a2a9 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 ype of the rowid
1a2aa 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 */. u32 lenRow
1a2ab 69 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 id; /* Size
1a2ac 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a of the rowid */.
1a2ad 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 2f Mem m, v;.. /
1a2ae 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f * Get the size o
1a2af 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
1a2b0 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 y. Only indices
1a2b1 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 entries of less
1a2b2 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 . ** than 2GiB
1a2b3 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e are support - an
1a2b4 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 ything large mus
1a2b5 74 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f t be database co
1a2b6 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 rruption */. sq
1a2b7 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a lite3BtreeKeySiz
1a2b8 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 e(pCur, &nCellKe
1a2b9 79 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 y);. if( unlike
1a2ba 6c 79 28 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c ly(nCellKey<=0 |
1a2bb 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 | nCellKey>0x7ff
1a2bc 66 66 66 66 66 29 20 29 7b 0a 20 20 20 20 72 65 fffff) ){. re
1a2bd 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1a2be 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 UPT_BKPT;. }..
1a2bf 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 /* Read in the
1a2c0 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 complete content
1a2c1 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1a2c2 74 72 79 20 2a 2f 0a 20 20 6d 2e 66 6c 61 67 73 try */. m.flags
1a2c3 20 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30 = 0;. m.db = 0
1a2c4 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 ;. m.zMalloc =
1a2c5 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 0;. rc = sqlite
1a2c6 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
1a2c7 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 e(pCur, 0, (int)
1a2c8 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 nCellKey, 1, &m)
1a2c9 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1a2ca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1a2cb 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 .. /* The index
1a2cc 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69 entry must begi
1a2cd 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20 n with a header
1a2ce 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29 size */. (void)
1a2cf 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a getVarint32((u8*
1a2d0 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 )m.z, szHdr);.
1a2d1 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d testcase( szHdr=
1a2d2 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 =2 );. testcase
1a2d3 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a ( szHdr==m.n );.
1a2d4 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 if( unlikely(s
1a2d5 7a 48 64 72 3c 32 20 7c 7c 20 28 69 6e 74 29 73 zHdr<2 || (int)s
1a2d6 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 zHdr>m.n) ){.
1a2d7 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f goto idx_rowid_
1a2d8 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a corruption;. }.
1a2d9 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66 . /* The last f
1a2da 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 ield of the inde
1a2db 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 x should be an i
1a2dc 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57 nteger - the ROW
1a2dd 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20 ID.. ** Verify
1a2de 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e that the last en
1a2df 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e try really is an
1a2e0 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28 integer. */. (
1a2e1 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 void)getVarint32
1a2e2 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 ((u8*)&m.z[szHdr
1a2e3 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b -1], typeRowid);
1a2e4 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1a2e5 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74 eRowid==1 );. t
1a2e6 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1a2e7 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 id==2 );. testc
1a2e8 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1a2e9 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 3 );. testcase(
1a2ea 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b typeRowid==4 );
1a2eb 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1a2ec 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74 eRowid==5 );. t
1a2ed 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1a2ee 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63 id==6 );. testc
1a2ef 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1a2f0 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 8 );. testcase(
1a2f1 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b typeRowid==9 );
1a2f2 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 . if( unlikely(
1a2f3 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 typeRowid<1 || t
1a2f4 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 ypeRowid>9 || ty
1a2f5 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20 peRowid==7) ){.
1a2f6 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 goto idx_rowi
1a2f7 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 d_corruption;.
1a2f8 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 }. lenRowid = s
1a2f9 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1a2fa 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 TypeLen(typeRowi
1a2fb 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 d);. testcase(
1a2fc 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3d 3d 73 7a m.n-lenRowid==sz
1a2fd 48 64 72 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c Hdr );. if( unl
1a2fe 69 6b 65 6c 79 28 6d 2e 6e 2d 6c 65 6e 52 6f 77 ikely(m.n-lenRow
1a2ff 69 64 3c 73 7a 48 64 72 29 20 29 7b 0a 20 20 20 id<szHdr) ){.
1a300 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f goto idx_rowid_
1a301 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a corruption;. }.
1a302 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 . /* Fetch the
1a303 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20 integer off the
1a304 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 end of the index
1a305 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c record */. sql
1a306 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 ite3VdbeSerialGe
1a307 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d t((u8*)&m.z[m.n-
1a308 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 lenRowid], typeR
1a309 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f owid, &v);. *ro
1a30a 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 wid = v.u.i;. s
1a30b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1a30c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 ease(&m);. retu
1a30d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 rn SQLITE_OK;..
1a30e 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 /* Jump here if
1a30f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
1a310 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 tion is detected
1a311 20 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65 after m has bee
1a312 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 n. ** allocated
1a313 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62 . Free the m ob
1a314 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 ject and return
1a315 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 SQLITE_CORRUPT.
1a316 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 */.idx_rowid_cor
1a317 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63 ruption:. testc
1a318 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d ase( m.zMalloc!=
1a319 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 0 );. sqlite3Vd
1a31a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 beMemRelease(&m)
1a31b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1a31c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1a31d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
1a31e 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 the key of the
1a31f 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 index entry that
1a320 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f cursor pC is po
1a321 69 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a int to against.*
1a322 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 * the key string
1a323 20 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e in pKey (of len
1a324 67 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74 gth nKey). Writ
1a325 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e e into *pRes a n
1a326 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 umber.** that is
1a327 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c negative, zero,
1a328 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 or positive if
1a329 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c pC is less than,
1a32a 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 equal to,.** or
1a32b 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b greater than pK
1a32c 65 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 ey. Return SQLI
1a32d 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
1a32e 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20 ..**.** pKey is
1a32f 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77 either created w
1a330 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f ithout a rowid o
1a331 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73 r is truncated s
1a332 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 o that it.** omi
1a333 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 ts the rowid at
1a334 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f the end. The ro
1a335 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f wid at the end o
1a336 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
1a337 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 y.** is ignored
1a338 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c as well. Hence,
1a339 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e this routine on
1a33a 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 ly compares the
1a33b 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 prefixes .** of
1a33c 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74 the keys prior t
1a33d 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 o the final rowi
1a33e 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 d, not the entir
1a33f 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e e key..**.** pUn
1a340 70 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e packed may be an
1a341 20 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f unpacked versio
1a342 6e 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 n of pKey,nKey.
1a343 20 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73 If pUnpacked is
1a344 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20 .** supplied it
1a345 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 is used in place
1a346 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a of pKey,nKey..*
1a347 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a348 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1a349 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 IdxKeyCompare(.
1a34a 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c VdbeCursor *pC,
1a34b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a34c 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f The cursor to co
1a34d 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f mpare against */
1a34e 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1a34f 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f d *pUnpacked, /
1a350 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 * Unpacked versi
1a351 6f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e on of pKey and n
1a352 4b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 Key */. int *re
1a353 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
1a354 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
1a355 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 e comparison res
1a356 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ult here */.){.
1a357 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 i64 nCellKey =
1a358 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 0;. int rc;. B
1a359 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 tCursor *pCur =
1a35a 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d pC->pCursor;. M
1a35b 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 em m;.. sqlite3
1a35c 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
1a35d 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 r, &nCellKey);.
1a35e 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 if( nCellKey<=0
1a35f 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 || nCellKey>0x7
1a360 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 2a fffffff ){. *
1a361 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 res = 0;. ret
1a362 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1a363 20 7d 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 }. m.db = 0;.
1a364 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 m.flags = 0;.
1a365 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 m.zMalloc = 0;.
1a366 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1a367 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 eMemFromBtree(pC
1a368 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 ->pCursor, 0, (i
1a369 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 nt)nCellKey, 1,
1a36a 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b &m);. if( rc ){
1a36b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1a36c 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 }. assert( pU
1a36d 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 npacked->flags &
1a36e 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
1a36f 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 _ROWID );. *res
1a370 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1a371 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c cordCompare(m.n,
1a372 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 m.z, pUnpacked)
1a373 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a374 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 emRelease(&m);.
1a375 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1a376 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 K;.}../*.** This
1a377 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1a378 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 e value to be re
1a379 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 turned by subseq
1a37a 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a uent calls to.**
1a37b 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1a37c 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 () on the databa
1a37d 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 se handle 'db'.
1a37e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a37f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a380 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 dbeSetChanges(sq
1a381 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e lite3 *db, int n
1a382 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 Change){. asser
1a383 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1a384 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
1a385 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 );. db->nChang
1a386 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 e = nChange;. d
1a387 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 b->nTotalChange
1a388 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f += nChange;.}../
1a389 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 *.** Set a flag
1a38a 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 in the vdbe to u
1a38b 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 pdate the change
1a38c 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 counter when it
1a38d 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a is finalised.**
1a38e 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 or reset..*/.SQ
1a38f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a390 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 d sqlite3VdbeCou
1a391 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a ntChanges(Vdbe *
1a392 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 v){. v->changeC
1a393 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a ntOn = 1;.}../*.
1a394 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 ** Mark every pr
1a395 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1a396 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1a397 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a database conn
1a398 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 ection.** as exp
1a399 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 ired..**.** An e
1a39a 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 xpired statement
1a39b 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f means that reco
1a39c 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 mpilation of the
1a39d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a statement is.**
1a39e 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 recommend. Sta
1a39f 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 tements expire w
1a3a0 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 hen things happe
1a3a1 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 n that make thei
1a3a2 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 r.** programs ob
1a3a3 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e solete. Removin
1a3a4 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 g user-defined f
1a3a5 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c unctions or coll
1a3a6 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 ating.** sequenc
1a3a7 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 es, or changing
1a3a8 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e an authorization
1a3a9 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 function are th
1a3aa 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 e types of.** th
1a3ab 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 ings that make p
1a3ac 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1a3ad 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a ts obsolete..*/.
1a3ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a3af 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 oid sqlite3Expir
1a3b0 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 ePreparedStateme
1a3b1 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 nts(sqlite3 *db)
1a3b2 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 {. Vdbe *p;. f
1a3b3 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 or(p = db->pVdbe
1a3b4 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
1a3b5 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 {. p->expired
1a3b6 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 1;. }.}../*.
1a3b7 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 ** Return the da
1a3b8 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 tabase associate
1a3b9 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e d with the Vdbe.
1a3ba 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a3bb 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 TE sqlite3 *sqli
1a3bc 74 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a te3VdbeDb(Vdbe *
1a3bd 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e v){. return v->
1a3be 64 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a db;.}../********
1a3bf 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
1a3c0 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a beaux.c ********
1a3c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3c3 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1a3c4 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1a3c5 65 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a e vdbeapi.c ****
1a3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3c8 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1a3c9 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 4 May 26.**.** T
1a3ca 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1a3cb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1a3cc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1a3cd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1a3ce 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1a3cf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1a3d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1a3d1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1a3d2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1a3d3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1a3d4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1a3d5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1a3d6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1a3d7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1a3d8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1a3d9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1a3da 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1a3db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
1a3e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1a3e1 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 code use to imp
1a3e2 6c 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 lement APIs that
1a3e3 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 are part of the
1a3e4 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 .** VDBE..**.**
1a3e5 24 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 $Id: vdbeapi.c,v
1a3e6 20 31 2e 31 36 34 20 32 30 30 39 2f 30 34 2f 32 1.164 2009/04/2
1a3e7 37 20 31 38 3a 34 36 3a 30 36 20 64 72 68 20 45 7 18:46:06 drh E
1a3e8 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 30 20 26 xp $.*/..#if 0 &
1a3e9 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
1a3ea 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
1a3eb 41 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a 0a 2a 2a ANAGEMENT)./*.**
1a3ec 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 The following s
1a3ed 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1a3ee 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 s pointers to th
1a3ef 65 20 65 6e 64 20 70 6f 69 6e 74 73 20 6f 66 20 e end points of
1a3f0 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b a.** doubly-link
1a3f1 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 ed list of all c
1a3f2 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 ompiled SQL stat
1a3f3 65 6d 65 6e 74 73 20 74 68 61 74 20 6d 61 79 20 ements that may
1a3f4 62 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 62 75 be holding.** bu
1a3f5 66 66 65 72 73 20 65 6c 69 67 69 62 6c 65 20 66 ffers eligible f
1a3f6 6f 72 20 72 65 6c 65 61 73 65 20 77 68 65 6e 20 or release when
1a3f7 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 the sqlite3_rele
1a3f8 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 ase_memory() int
1a3f9 65 72 66 61 63 65 20 69 73 0a 2a 2a 20 69 6e 76 erface is.** inv
1a3fa 6f 6b 65 64 2e 20 41 63 63 65 73 73 20 74 6f 20 oked. Access to
1a3fb 74 68 69 73 20 6c 69 73 74 20 69 73 20 70 72 6f this list is pro
1a3fc 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 51 tected by the SQ
1a3fd 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
1a3fe 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 65 78 2e C_LRU2.** mutex.
1a3ff 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 .**.** Statement
1a400 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 s are added to t
1a401 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 6c he end of this l
1a402 69 73 74 20 77 68 65 6e 20 73 71 6c 69 74 65 33 ist when sqlite3
1a403 5f 72 65 73 65 74 28 29 20 69 73 0a 2a 2a 20 63 _reset() is.** c
1a404 61 6c 6c 65 64 2e 20 54 68 65 79 20 61 72 65 20 alled. They are
1a405 72 65 6d 6f 76 65 64 20 65 69 74 68 65 72 20 77 removed either w
1a406 68 65 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 hen sqlite3_step
1a407 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 () or sqlite3_fi
1a408 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 73 20 63 nalize().** is c
1a409 61 6c 6c 65 64 2e 20 57 68 65 6e 20 73 74 61 74 alled. When stat
1a40a 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 ements are added
1a40b 20 74 6f 20 74 68 69 73 20 6c 69 73 74 2c 20 74 to this list, t
1a40c 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a he associated .*
1a40d 2a 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79 * register array
1a40e 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e 70 2d 3e (p->aMem[1..p->
1a40f 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 6f 6e 74 61 nMem]) may conta
1a410 69 6e 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65 in dynamic buffe
1a411 72 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 rs that.** can b
1a412 65 20 66 72 65 65 64 20 75 73 69 6e 67 20 73 71 e freed using sq
1a413 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 lite3VdbeRelease
1a414 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 Memory()..**.**
1a415 57 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20 When statements
1a416 61 72 65 20 61 64 64 65 64 20 6f 72 20 72 65 6d are added or rem
1a417 6f 76 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6c oved from this l
1a418 69 73 74 2c 20 74 68 65 20 6d 75 74 65 78 0a 2a ist, the mutex.*
1a419 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1a41a 68 20 74 68 65 20 56 64 62 65 20 62 65 69 6e 67 h the Vdbe being
1a41b 20 61 64 64 65 64 20 6f 72 20 72 65 6d 6f 76 65 added or remove
1a41c 64 20 28 56 64 62 65 2e 64 62 2d 3e 6d 75 74 65 d (Vdbe.db->mute
1a41d 78 29 20 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 x) is.** already
1a41e 20 68 65 6c 64 2e 20 54 68 65 20 4c 52 55 32 20 held. The LRU2
1a41f 6d 75 74 65 78 20 69 73 20 74 68 65 6e 20 6f 62 mutex is then ob
1a420 74 61 69 6e 65 64 2c 20 62 6c 6f 63 6b 69 6e 67 tained, blocking
1a421 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c 0a 2a if necessary,.*
1a422 2a 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 * the linked-lis
1a423 74 20 70 6f 69 6e 74 65 72 73 20 6d 61 6e 69 70 t pointers manip
1a424 75 6c 61 74 65 64 20 61 6e 64 20 74 68 65 20 4c ulated and the L
1a425 52 55 32 20 6d 75 74 65 78 20 72 65 6c 69 6e 71 RU2 mutex relinq
1a426 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 uished..*/.struc
1a427 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69 t StatementLruLi
1a428 73 74 20 7b 0a 20 20 56 64 62 65 20 2a 70 46 69 st {. Vdbe *pFi
1a429 72 73 74 3b 0a 20 20 56 64 62 65 20 2a 70 4c 61 rst;. Vdbe *pLa
1a42a 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 20 73 74 st;.};.static st
1a42b 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 ruct StatementLr
1a42c 75 4c 69 73 74 20 73 71 6c 69 74 65 33 4c 72 75 uList sqlite3Lru
1a42d 53 74 61 74 65 6d 65 6e 74 73 3b 0a 0a 2f 2a 0a Statements;../*.
1a42e 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 ** Check that th
1a42f 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 74 6f 20 e list looks to
1a430 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f be internally co
1a431 6e 73 69 73 74 65 6e 74 2e 20 54 68 69 73 20 69 nsistent. This i
1a432 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 70 61 72 s used.** as par
1a433 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 t of an assert()
1a434 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f statement as fo
1a435 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 llows:.**.** a
1a436 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 ssert( stmtLruCh
1a437 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 69 66 6e eck() );.*/.#ifn
1a438 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 def NDEBUG.stati
1a439 63 20 69 6e 74 20 73 74 6d 74 4c 72 75 43 68 65 c int stmtLruChe
1a43a 63 6b 28 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b ck(){. Vdbe *p;
1a43b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 . for(p=sqlite3
1a43c 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 LruStatements.pF
1a43d 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c irst; p; p=p->pL
1a43e 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 ruNext){. ass
1a43f 65 72 74 28 70 2d 3e 70 4c 72 75 4e 65 78 74 20 ert(p->pLruNext
1a440 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 || p==sqlite3Lru
1a441 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 Statements.pLast
1a442 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70 );. assert(!p
1a443 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 2d ->pLruNext || p-
1a444 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 >pLruNext->pLruP
1a445 72 65 76 3d 3d 70 29 3b 0a 20 20 20 20 61 73 73 rev==p);. ass
1a446 65 72 74 28 70 2d 3e 70 4c 72 75 50 72 65 76 20 ert(p->pLruPrev
1a447 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 || p==sqlite3Lru
1a448 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 Statements.pFirs
1a449 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 t);. assert(!
1a44a 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 p->pLruPrev || p
1a44b 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 ->pLruPrev->pLru
1a44c 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 7d 0a 20 20 Next==p);. }.
1a44d 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
1a44e 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 76 64 if../*.** Add vd
1a44f 62 65 20 70 20 74 6f 20 74 68 65 20 65 6e 64 20 be p to the end
1a450 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 of the statement
1a451 20 6c 72 75 20 6c 69 73 74 2e 20 49 74 20 69 73 lru list. It is
1a452 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a assumed that.**
1a453 20 70 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 p is not alread
1a454 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 y part of the li
1a455 73 74 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 st when this is
1a456 63 61 6c 6c 65 64 2e 20 54 68 65 20 6c 72 75 20 called. The lru
1a457 6c 69 73 74 0a 2a 2a 20 69 73 20 70 72 6f 74 65 list.** is prote
1a458 63 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 cted by the SQLI
1a459 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
1a45a 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 LRU mutex..*/.st
1a45b 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 atic void stmtLr
1a45c 75 41 64 64 28 56 64 62 65 20 2a 70 29 7b 0a 20 uAdd(Vdbe *p){.
1a45d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1a45e 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 nter(sqlite3Mute
1a45f 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
1a460 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 TEX_STATIC_LRU2)
1a461 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 );.. if( p->pLr
1a462 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 uPrev || p->pLru
1a463 4e 65 78 74 20 7c 7c 20 73 71 6c 69 74 65 33 4c Next || sqlite3L
1a464 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 ruStatements.pFi
1a465 72 73 74 3d 3d 70 20 29 7b 0a 20 20 20 20 73 71 rst==p ){. sq
1a466 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1a467 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c e(sqlite3MutexAl
1a468 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
1a469 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a _STATIC_LRU2));.
1a46a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
1a46b 0a 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c . assert( stmtL
1a46c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 ruCheck() );..
1a46d 69 66 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 if( !sqlite3LruS
1a46e 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 tatements.pFirst
1a46f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1a470 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 !sqlite3LruState
1a471 6d 65 6e 74 73 2e 70 4c 61 73 74 20 29 3b 0a 20 ments.pLast );.
1a472 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 sqlite3LruSta
1a473 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d tements.pFirst =
1a474 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c p;. sqlite3L
1a475 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
1a476 73 74 20 3d 20 70 3b 0a 20 20 7d 65 6c 73 65 7b st = p;. }else{
1a477 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 . assert( !sq
1a478 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e lite3LruStatemen
1a479 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 ts.pLast->pLruNe
1a47a 78 74 20 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 xt );. p->pLr
1a47b 75 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 4c uPrev = sqlite3L
1a47c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
1a47d 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c st;. sqlite3L
1a47e 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
1a47f 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 st->pLruNext = p
1a480 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75 ;. sqlite3Lru
1a481 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 Statements.pLast
1a482 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 = p;. }.. ass
1a483 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 ert( stmtLruChec
1a484 6b 28 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 k() );.. sqlite
1a485 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 3_mutex_leave(sq
1a486 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
1a487 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
1a488 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f TIC_LRU2));.}../
1a489 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 *.** Assuming th
1a48a 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
1a48b 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 TATIC_LRU2 mutex
1a48c 74 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c t is already hel
1a48d 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 d, remove.** sta
1a48e 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 tement p from th
1a48f 65 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 e least-recently
1a490 2d 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20 -used statement
1a491 6c 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a list. If the .**
1a492 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f statement is no
1a493 74 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74 t currently part
1a494 20 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 of the list, th
1a495 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
1a496 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f op..*/.static vo
1a497 69 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 id stmtLruRemove
1a498 4e 6f 6d 75 74 65 78 28 56 64 62 65 20 2a 70 29 Nomutex(Vdbe *p)
1a499 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 {. if( p->pLruP
1a49a 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 rev || p->pLruNe
1a49b 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 xt || p==sqlite3
1a49c 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 LruStatements.pF
1a49d 69 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 irst ){. asse
1a49e 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b rt( stmtLruCheck
1a49f 28 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d () );. if( p-
1a4a0 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 >pLruNext ){.
1a4a1 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e p->pLruNext->
1a4a2 70 4c 72 75 50 72 65 76 20 3d 20 70 2d 3e 70 4c pLruPrev = p->pL
1a4a3 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 ruPrev;. }els
1a4a4 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
1a4a5 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c LruStatements.pL
1a4a6 61 73 74 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65 ast = p->pLruPre
1a4a7 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 v;. }. if(
1a4a8 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a p->pLruPrev ){.
1a4a9 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 p->pLruPre
1a4aa 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 2d v->pLruNext = p-
1a4ab 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d >pLruNext;. }
1a4ac 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
1a4ad 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
1a4ae 2e 70 46 69 72 73 74 20 3d 20 70 2d 3e 70 4c 72 .pFirst = p->pLr
1a4af 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 uNext;. }.
1a4b0 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 p->pLruNext = 0
1a4b1 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 ;. p->pLruPre
1a4b2 76 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 v = 0;. asser
1a4b3 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 t( stmtLruCheck(
1a4b4 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ) );. }.}../*.*
1a4b5 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 53 * Assuming the S
1a4b6 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
1a4b7 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 74 20 69 IC_LRU2 mutext i
1a4b8 73 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 6d 6f s not held, remo
1a4b9 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ve.** statement
1a4ba 70 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 p from the least
1a4bb 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 -recently-used s
1a4bc 74 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 tatement list. I
1a4bd 66 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d f the .** statem
1a4be 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ent is not curre
1a4bf 6e 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 ntly part of the
1a4c0 20 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c list, this call
1a4c1 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
1a4c2 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 static void stmt
1a4c3 4c 72 75 52 65 6d 6f 76 65 28 56 64 62 65 20 2a LruRemove(Vdbe *
1a4c4 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 p){. sqlite3_mu
1a4c5 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
1a4c6 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
1a4c7 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
1a4c8 4c 52 55 32 29 29 3b 0a 20 20 73 74 6d 74 4c 72 LRU2));. stmtLr
1a4c9 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70 uRemoveNomutex(p
1a4ca 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
1a4cb 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
1a4cc 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
1a4cd 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
1a4ce 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 RU2));.}../*.**
1a4cf 54 72 79 20 74 6f 20 72 65 6c 65 61 73 65 20 6e Try to release n
1a4d0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
1a4d1 20 62 79 20 66 72 65 65 69 6e 67 20 62 75 66 66 by freeing buff
1a4d2 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a ers associated .
1a4d3 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f ** with the memo
1a4d4 72 79 20 72 65 67 69 73 74 65 72 73 20 6f 66 20 ry registers of
1a4d5 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 currently unused
1a4d6 20 76 64 62 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 vdbes..*/.SQLIT
1a4d7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1a4d8 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 lite3VdbeRelease
1a4d9 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 20 Memory(int n){.
1a4da 20 56 64 62 65 20 2a 70 3b 0a 20 20 56 64 62 65 Vdbe *p;. Vdbe
1a4db 20 2a 70 4e 65 78 74 3b 0a 20 20 69 6e 74 20 6e *pNext;. int n
1a4dc 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 73 71 6c Free = 0;.. sql
1a4dd 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1a4de 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
1a4df 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
1a4e0 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 STATIC_LRU2));.
1a4e1 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c 72 for(p=sqlite3Lr
1a4e2 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 uStatements.pFir
1a4e3 73 74 3b 20 70 20 26 26 20 6e 46 72 65 65 3c 6e st; p && nFree<n
1a4e4 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 ; p=pNext){.
1a4e5 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e pNext = p->pLruN
1a4e6 65 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 ext;.. /* For
1a4e7 20 65 61 63 68 20 73 74 61 74 65 6d 65 6e 74 20 each statement
1a4e8 68 61 6e 64 6c 65 20 69 6e 20 74 68 65 20 6c 72 handle in the lr
1a4e9 75 20 6c 69 73 74 2c 20 61 74 74 65 6d 70 74 20 u list, attempt
1a4ea 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 0a 20 20 to obtain the.
1a4eb 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 ** associated
1a4ec 64 61 74 61 62 61 73 65 20 6d 75 74 65 78 2e 20 database mutex.
1a4ed 49 66 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 If it cannot be
1a4ee 6f 62 74 61 69 6e 65 64 2c 20 63 6f 6e 74 69 6e obtained, contin
1a4ef 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 ue. ** to the
1a4f0 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 next statement
1a4f1 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6e 6f handle. It is no
1a4f2 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 62 6c t possible to bl
1a4f3 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a 20 74 68 ock on. ** th
1a4f4 65 20 64 61 74 61 62 61 73 65 20 6d 75 74 65 78 e database mutex
1a4f5 20 2d 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61 - that could ca
1a4f6 75 73 65 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 use deadlock..
1a4f7 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c */. if( SQL
1a4f8 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 5f ITE_OK==sqlite3_
1a4f9 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 64 62 2d mutex_try(p->db-
1a4fa 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 20 >mutex) ){.
1a4fb 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 nFree += sqlite
1a4fc 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 3VdbeReleaseBuff
1a4fd 65 72 73 28 70 29 3b 0a 20 20 20 20 20 20 73 74 ers(p);. st
1a4fe 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 mtLruRemoveNomut
1a4ff 65 78 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c ex(p);. sql
1a500 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1a501 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1a502 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 }. }. sqli
1a503 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1a504 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
1a505 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
1a506 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 TATIC_LRU2));..
1a507 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d return nFree;.}
1a508 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c ../*.** Call sql
1a509 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 ite3Reprepare()
1a50a 6f 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 on the statement
1a50b 2e 20 52 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d . Remove it from
1a50c 20 74 68 65 0a 2a 2a 20 6c 72 75 20 6c 69 73 74 the.** lru list
1a50d 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f before doing so
1a50e 2c 20 61 73 20 52 65 70 72 65 70 61 72 65 28 29 , as Reprepare()
1a50f 20 77 69 6c 6c 20 66 72 65 65 20 61 6c 6c 20 74 will free all t
1a510 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 67 he.** memory reg
1a511 69 73 74 65 72 20 62 75 66 66 65 72 73 20 61 6e ister buffers an
1a512 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20 76 64 62 yway..*/.int vdb
1a513 65 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20 eReprepare(Vdbe
1a514 2a 70 29 7b 0a 20 20 73 74 6d 74 4c 72 75 52 65 *p){. stmtLruRe
1a515 6d 6f 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 move(p);. retur
1a516 6e 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 n sqlite3Reprepa
1a517 72 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c 73 65 20 re(p);.}..#else
1a518 20 20 20 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 /* !SQLITE
1a519 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
1a51a 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 20 20 23 ANAGEMENT */. #
1a51b 64 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 52 65 define stmtLruRe
1a51c 6d 6f 76 65 28 78 29 0a 20 20 23 64 65 66 69 6e move(x). #defin
1a51d 65 20 73 74 6d 74 4c 72 75 41 64 64 28 78 29 0a e stmtLruAdd(x).
1a51e 20 20 23 64 65 66 69 6e 65 20 76 64 62 65 52 65 #define vdbeRe
1a51f 70 72 65 70 61 72 65 28 78 29 20 73 71 6c 69 74 prepare(x) sqlit
1a520 65 33 52 65 70 72 65 70 61 72 65 28 78 29 0a 23 e3Reprepare(x).#
1a521 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 endif...#ifndef
1a522 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 SQLITE_OMIT_DEPR
1a523 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 ECATED./*.** Ret
1a524 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 urn TRUE (non-ze
1a525 72 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 ro) of the state
1a526 6d 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 ment supplied as
1a527 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 an argument nee
1a528 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f ds.** to be reco
1a529 6d 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 mpiled. A state
1a52a 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 ment needs to be
1a52b 20 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e recompiled when
1a52c 65 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 ever the.** exec
1a52d 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e ution environmen
1a52e 74 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 t changes in a w
1a52f 61 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c ay that would al
1a530 74 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a ter the program.
1a531 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f ** that sqlite3_
1a532 70 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 prepare() genera
1a533 74 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c tes. For exampl
1a534 65 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 e, if new functi
1a535 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 ons or.** collat
1a536 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 ing sequences ar
1a537 65 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 e registered or
1a538 69 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 if an authorizer
1a539 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 function is.**
1a53a 61 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 added or changed
1a53b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1a53c 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 int sqlite3_expi
1a53d 72 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 red(sqlite3_stmt
1a53e 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 *pStmt){. Vdbe
1a53f 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
1a540 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d mt;. return p==
1a541 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0 || p->expired;
1a542 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1a543 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
1a544 6f 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 outine destroys
1a545 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e a virtual machin
1a546 65 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 e that is create
1a547 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 d by.** the sqli
1a548 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f te3_compile() ro
1a549 75 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 utine. The integ
1a54a 65 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 er returned is a
1a54b 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 n SQLITE_.** suc
1a54c 63 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 cess/failure cod
1a54d 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
1a54e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 the result of e
1a54f 78 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 xecuting the vir
1a550 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e tual.** machine.
1a551 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1a552 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 ine sets the err
1a553 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 or code and stri
1a554 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ng returned by.*
1a555 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 * sqlite3_errcod
1a556 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 e(), sqlite3_err
1a557 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 msg() and sqlite
1a558 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 3_errmsg16()..*/
1a559 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1a55a 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
1a55b 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a55c 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b Stmt){. int rc;
1a55d 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 . if( pStmt==0
1a55e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
1a55f 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
1a560 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 Vdbe *v = (V
1a561 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 dbe*)pStmt;.
1a562 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d sqlite3 *db = v-
1a563 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f >db;.#if SQLITE_
1a564 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 THREADSAFE. s
1a565 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
1a566 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 tex = v->db->mut
1a567 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 ex;.#endif. s
1a568 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1a569 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 er(mutex);. s
1a56a 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b tmtLruRemove(v);
1a56b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1a56c 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3VdbeFinalize(v)
1a56d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1a56e 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 e3ApiExit(db, rc
1a56f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1a570 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
1a571 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1a572 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 rc;.}../*.** Ter
1a573 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65 minate the curre
1a574 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 nt execution of
1a575 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
1a576 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a and reset it.**
1a577 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61 back to its sta
1a578 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74 rting state so t
1a579 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 hat it can be re
1a57a 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20 used. A success
1a57b 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 code from.** the
1a57c 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e prior execution
1a57d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1a57e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1a57f 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 sets the error
1a580 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20 code and string
1a581 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 returned by.** s
1a582 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
1a583 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 , sqlite3_errmsg
1a584 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 () and sqlite3_e
1a585 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 rrmsg16()..*/.SQ
1a586 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a587 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74 ite3_reset(sqlit
1a588 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1a589 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
1a58a 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 pStmt==0 ){.
1a58b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1a58c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 . }else{. Vd
1a58d 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 be *v = (Vdbe*)p
1a58e 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 Stmt;. sqlite
1a58f 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3_mutex_enter(v-
1a590 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1a591 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1a592 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73 eReset(v);. s
1a593 74 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 tmtLruAdd(v);.
1a594 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b sqlite3VdbeMak
1a595 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c eReady(v, -1, 0,
1a596 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 0, 0);. asse
1a597 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62 rt( (rc & (v->db
1a598 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 ->errMask))==rc
1a599 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1a59a 74 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62 te3ApiExit(v->db
1a59b 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 , rc);. sqlit
1a59c 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76 e3_mutex_leave(v
1a59d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1a59e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1a59f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 ../*.** Set all
1a5a0 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 the parameters i
1a5a1 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53 n the compiled S
1a5a2 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 QL statement to
1a5a3 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
1a5a4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a5a5 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 clear_bindings(s
1a5a6 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1a5a7 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 mt){. int i;.
1a5a8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1a5a9 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 OK;. Vdbe *p =
1a5aa 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69 (Vdbe*)pStmt;.#i
1a5ab 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
1a5ac 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 AFE. sqlite3_mu
1a5ad 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56 tex *mutex = ((V
1a5ae 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d dbe*)pStmt)->db-
1a5af 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 >mutex;.#endif.
1a5b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1a5b1 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 nter(mutex);. f
1a5b2 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 or(i=0; i<p->nVa
1a5b3 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c r; i++){. sql
1a5b4 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1a5b5 73 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b se(&p->aVar[i]);
1a5b6 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e . p->aVar[i].
1a5b7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1a5b8 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1a5b9 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
1a5ba 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b x);. return rc;
1a5bb 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}.../**********
1a5bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a5bd 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
1a5be 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a _ *************
1a5bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a5c0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
1a5c1 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74 ing routines ext
1a5c2 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ract information
1a5c3 20 66 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73 from a Mem or s
1a5c4 71 6c 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 qlite3_value.**
1a5c5 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 structure..*/.SQ
1a5c6 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1a5c7 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
1a5c8 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f ue_blob(sqlite3_
1a5c9 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1a5ca 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 Mem *p = (Mem*)p
1a5cb 56 61 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c Val;. if( p->fl
1a5cc 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c ags & (MEM_Blob|
1a5cd 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 MEM_Str) ){.
1a5ce 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
1a5cf 70 61 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20 pandBlob(p);.
1a5d0 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 p->flags &= ~ME
1a5d1 4d 5f 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c M_Str;. p->fl
1a5d2 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b ags |= MEM_Blob;
1a5d3 0a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a . return p->z
1a5d4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1a5d5 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 eturn sqlite3_va
1a5d6 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a lue_text(pVal);.
1a5d7 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 }.}.SQLITE_API
1a5d8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
1a5d9 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ue_bytes(sqlite3
1a5da 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1a5db 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1a5dc 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 alueBytes(pVal,
1a5dd 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a SQLITE_UTF8);.}.
1a5de 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a5df 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
1a5e0 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c es16(sqlite3_val
1a5e1 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
1a5e2 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
1a5e3 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 Bytes(pVal, SQLI
1a5e4 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b TE_UTF16NATIVE);
1a5e5 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f .}.SQLITE_API do
1a5e6 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c uble sqlite3_val
1a5e7 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 ue_double(sqlite
1a5e8 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1a5e9 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1a5ea 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d VdbeRealValue((M
1a5eb 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c em*)pVal);.}.SQL
1a5ec 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a5ed 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 te3_value_int(sq
1a5ee 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1a5ef 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e l){. return (in
1a5f0 74 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 t)sqlite3VdbeInt
1a5f1 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c Value((Mem*)pVal
1a5f2 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1a5f3 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c sqlite_int64 sql
1a5f4 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 ite3_value_int64
1a5f5 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1a5f6 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1a5f7 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1a5f8 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b lue((Mem*)pVal);
1a5f9 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f .}.SQLITE_API co
1a5fa 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1a5fb 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r *sqlite3_value
1a5fc 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 _text(sqlite3_va
1a5fd 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 lue *pVal){. re
1a5fe 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69 turn (const unsi
1a5ff 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 gned char *)sqli
1a600 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 te3ValueText(pVa
1a601 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b l, SQLITE_UTF8);
1a602 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1a603 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1a604 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1a605 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
1a606 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 e_text16(sqlite3
1a607 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 _value* pVal){.
1a608 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1a609 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 alueText(pVal, S
1a60a 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
1a60b 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 E);.}.SQLITE_API
1a60c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1a60d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
1a60e 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 6be(sqlite3_valu
1a60f 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1a610 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 rn sqlite3ValueT
1a611 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 ext(pVal, SQLITE
1a612 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c _UTF16BE);.}.SQL
1a613 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1a614 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
1a615 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 e_text16le(sqlit
1a616 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
1a617 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1a618 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 3ValueText(pVal,
1a619 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 SQLITE_UTF16LE)
1a61a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1a61b 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
1a61c 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1a61d 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
1a61e 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
1a61f 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue* pVal){. ret
1a620 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a urn pVal->type;.
1a621 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1a622 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a623 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1a624 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **************
1a625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a626 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1a627 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 ng routines are
1a628 75 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 66 used by user-def
1a629 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 ined functions t
1a62a 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65 o specify.** the
1a62b 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 function result
1a62c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1a62d 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1a62e 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 ult_blob(. sqli
1a62f 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1a630 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 x, . const void
1a631 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a *z, . int n, .
1a632 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1a633 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 oid *).){. asse
1a634 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 rt( n>=0 );. as
1a635 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a636 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a637 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a638 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a639 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1a63a 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a z, n, 0, xDel);.
1a63b 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1a63c 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1a63d 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
1a63e 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64 context *pCtx, d
1a63f 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 ouble rVal){. a
1a640 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a641 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a642 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a643 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a644 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d SetDouble(&pCtx-
1a645 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c >s, rVal);.}.SQL
1a646 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1a647 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
1a648 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 r(sqlite3_contex
1a649 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 t *pCtx, const c
1a64a 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a har *z, int n){.
1a64b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1a64c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1a64d 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1a64e 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 );. pCtx->isErr
1a64f 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f or = SQLITE_ERRO
1a650 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 R;. sqlite3Vdbe
1a651 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
1a652 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 >s, z, n, SQLITE
1a653 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 _UTF8, SQLITE_TR
1a654 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e ANSIENT);.}.#ifn
1a655 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a656 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1a657 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1a658 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c sult_error16(sql
1a659 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1a65a 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a tx, const void *
1a65b 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 z, int n){. ass
1a65c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1a65d 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1a65e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1a65f 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 pCtx->isError =
1a660 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a661 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1a662 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a tStr(&pCtx->s, z
1a663 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 , n, SQLITE_UTF1
1a664 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 6NATIVE, SQLITE_
1a665 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 TRANSIENT);.}.#e
1a666 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 ndif.SQLITE_API
1a667 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1a668 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f ult_int(sqlite3_
1a669 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 context *pCtx, i
1a66a 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 nt iVal){. asse
1a66b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1a66c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1a66d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1a66e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1a66f 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 Int64(&pCtx->s,
1a670 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 (i64)iVal);.}.SQ
1a671 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1a672 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
1a673 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 64(sqlite3_conte
1a674 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56 xt *pCtx, i64 iV
1a675 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 al){. assert( s
1a676 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1a677 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1a678 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1a679 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 3VdbeMemSetInt64
1a67a 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 (&pCtx->s, iVal)
1a67b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1a67c 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1a67d 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f lt_null(sqlite3_
1a67e 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a context *pCtx){.
1a67f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1a680 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1a681 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1a682 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1a683 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 MemSetNull(&pCtx
1a684 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 ->s);.}.SQLITE_A
1a685 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1a686 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 result_text(. s
1a687 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a688 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 pCtx, . const c
1a689 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e har *z, . int n
1a68a 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 ,. void (*xDel)
1a68b 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
1a68c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a68d 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a68e 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a68f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a690 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1a691 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1a692 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 8, xDel);.}.#ifn
1a693 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a694 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1a695 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1a696 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 73 sult_text16(. s
1a697 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a698 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 pCtx, . const v
1a699 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e oid *z, . int n
1a69a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
1a69b 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
1a69c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a69d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a69e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a69f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a6a0 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1a6a1 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
1a6a2 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 F16NATIVE, xDel)
1a6a3 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1a6a4 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1a6a5 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 lt_text16be(. s
1a6a6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a6a7 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 pCtx, . const v
1a6a8 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e oid *z, . int n
1a6a9 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
1a6aa 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
1a6ab 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a6ac 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a6ad 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a6ae 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a6af 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1a6b0 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
1a6b1 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a F16BE, xDel);.}.
1a6b2 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1a6b3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
1a6b4 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 ext16le(. sqlit
1a6b5 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1a6b6 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1a6b7 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1a6b8 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1a6b9 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
1a6ba 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1a6bb 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1a6bc 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1a6bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1a6be 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 tr(&pCtx->s, z,
1a6bf 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c n, SQLITE_UTF16L
1a6c0 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 E, xDel);.}.#end
1a6c1 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1a6c2 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 T_UTF16 */.SQLIT
1a6c3 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1a6c4 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
1a6c5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a6c6 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 *pCtx, sqlite3_v
1a6c7 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 alue *pValue){.
1a6c8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1a6c9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1a6ca 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1a6cb 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a6cc 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c emCopy(&pCtx->s,
1a6cd 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 pValue);.}.SQLI
1a6ce 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1a6cf 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
1a6d0 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 lob(sqlite3_cont
1a6d1 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e ext *pCtx, int n
1a6d2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1a6d3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1a6d4 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1a6d5 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
1a6d6 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f dbeMemSetZeroBlo
1a6d7 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a b(&pCtx->s, n);.
1a6d8 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1a6d9 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1a6da 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 _error_code(sqli
1a6db 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1a6dc 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b x, int errCode){
1a6dd 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 . pCtx->isError
1a6de 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 = errCode;. if
1a6df 28 20 70 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20 ( pCtx->s.flags
1a6e0 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1a6e1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a6e2 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1a6e3 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 65 sqlite3ErrStr(e
1a6e4 72 72 43 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20 rrCode), -1, .
1a6e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6e6 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 SQLITE_UT
1a6e7 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 F8, SQLITE_STATI
1a6e8 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f C);. }.}../* Fo
1a6e9 72 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f rce an SQLITE_TO
1a6ea 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 OBIG error. */.S
1a6eb 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1a6ec 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1a6ed 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 ror_toobig(sqlit
1a6ee 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1a6ef 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1a6f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1a6f1 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1a6f2 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 x) );. pCtx->is
1a6f3 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 Error = SQLITE_T
1a6f4 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 OOBIG;. sqlite3
1a6f5 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 VdbeMemSetStr(&p
1a6f6 43 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 Ctx->s, "string
1a6f7 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 or blob too big"
1a6f8 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 , -1, .
1a6f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 SQ
1a6fa 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
1a6fb 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a E_STATIC);.}../*
1a6fc 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d An SQLITE_NOMEM
1a6fd 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 error. */.SQLIT
1a6fe 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1a6ff 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
1a700 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f nomem(sqlite3_co
1a701 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 ntext *pCtx){.
1a702 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1a703 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1a704 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1a705 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1a706 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e mSetNull(&pCtx->
1a707 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 s);. pCtx->isEr
1a708 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d ror = SQLITE_NOM
1a709 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 EM;. pCtx->s.db
1a70a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1a70b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 1;.}../*.** Exe
1a70c 63 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 cute the stateme
1a70d 6e 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72 nt pStmt, either
1a70e 20 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 until a row of
1a70f 64 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74 data is ready, t
1a710 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 he.** statement
1a711 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 is completely ex
1a712 65 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72 ecuted or an err
1a713 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a or occurs..**.**
1a714 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d This routine im
1a715 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c plements the bul
1a716 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62 k of the logic b
1a717 65 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65 ehind the sqlite
1a718 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 _step().** API.
1a719 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 The only thing
1a71a 6f 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61 omitted is the a
1a71b 75 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 utomatic recompi
1a71c 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 le if a .** sche
1a71d 6d 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 ma change has oc
1a71e 63 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65 curred. That de
1a71f 74 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 tail is handled
1a720 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 by the.** outer
1a721 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77 sqlite3_step() w
1a722 72 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65 rapper procedure
1a723 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1a724 73 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65 sqlite3Step(Vdbe
1a725 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
1a726 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a *db;. int rc;..
1a727 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69 assert(p);. i
1a728 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 f( p->magic!=VDB
1a729 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 E_MAGIC_RUN ){.
1a72a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1a72b 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 _MISUSE;. }..
1a72c 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d /* Assert that m
1a72d 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 alloc() has not
1a72e 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d failed */. db =
1a72f 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 p->db;. if( db
1a730 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1a731 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1a732 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
1a733 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 if( p->pc<=0 &
1a734 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a & p->expired ){.
1a735 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 if( ALWAYS(p
1a736 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 ->rc==SQLITE_OK)
1a737 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 ){. p->rc
1a738 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b = SQLITE_SCHEMA;
1a739 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1a73a 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a73b 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 goto end_of_st
1a73c 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 ep;. }. if( sq
1a73d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1a73e 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d ) ){. p->rc =
1a73f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1a740 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1a741 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
1a742 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 if( p->pc<0 ){.
1a743 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
1a744 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 re no other stat
1a745 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 ements currently
1a746 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 running, then.
1a747 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 ** reset the
1a748 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 interrupt flag.
1a749 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 This prevents a
1a74a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1a74b 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a _interrupt. *
1a74c 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 * from interrupt
1a74d 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ing a statement
1a74e 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 that has not yet
1a74f 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f started.. */
1a750 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 . if( db->act
1a751 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b iveVdbeCnt==0 ){
1a752 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 . db->u1.is
1a753 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b Interrupted = 0;
1a754 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 . }..#ifndef
1a755 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1a756 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 E. if( db->xP
1a757 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 rofile && !db->i
1a758 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
1a759 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 double rNow;.
1a75a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 sqlite3OsCu
1a75b 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 rrentTime(db->pV
1a75c 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 fs, &rNow);.
1a75d 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d p->startTime =
1a75e 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 (u64)((rNow - (
1a75f 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 int)rNow)*3600.0
1a760 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 *24.0*1000000000
1a761 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 .0);. }.#endi
1a762 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 f.. db->activ
1a763 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 eVdbeCnt++;.
1a764 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d if( p->readOnly=
1a765 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 =0 ) db->writeVd
1a766 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e beCnt++;. p->
1a767 70 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74 pc = 0;. stmt
1a768 4c 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 LruRemove(p);.
1a769 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a76a 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 _OMIT_EXPLAIN.
1a76b 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 if( p->explain )
1a76c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1a76d 65 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 e3VdbeList(p);.
1a76e 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a }else.#endif /*
1a76f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 SQLITE_OMIT_EXP
1a770 4c 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 LAIN */. {.
1a771 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a772 45 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 Exec(p);. }..
1a773 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1a774 79 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 yOff(db) ){.
1a775 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 rc = SQLITE_MISU
1a776 53 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 SE;. }..#ifndef
1a777 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 SQLITE_OMIT_TRA
1a778 43 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 CE. /* Invoke t
1a779 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 he profile callb
1a77a 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 ack if there is
1a77b 6f 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 one. */. if( r
1a77c 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 c!=SQLITE_ROW &&
1a77d 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 db->xProfile &&
1a77e 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
1a77f 26 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 && p->zSql ){.
1a780 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 double rNow;.
1a781 20 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d u64 elapseTim
1a782 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f e;.. sqlite3O
1a783 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d sCurrentTime(db-
1a784 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 >pVfs, &rNow);.
1a785 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 elapseTime =
1a786 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 (u64)((rNow - (i
1a787 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a nt)rNow)*3600.0*
1a788 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 24.0*1000000000.
1a789 30 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 0);. elapseTi
1a78a 6d 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 me -= p->startTi
1a78b 6d 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f me;. db->xPro
1a78c 66 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c file(db->pProfil
1a78d 65 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 eArg, p->zSql, e
1a78e 6c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a lapseTime);. }.
1a78f 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 #endif.. db->er
1a790 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 rCode = rc;. if
1a791 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d ( SQLITE_NOMEM==
1a792 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 sqlite3ApiExit(p
1a793 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a ->db, p->rc) ){.
1a794 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 p->rc = SQLI
1a795 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e TE_NOMEM;. }.en
1a796 64 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 d_of_step:. /*
1a797 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f At this point lo
1a798 63 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 cal variable rc
1a799 68 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 holds the value
1a79a 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a that should be .
1a79b 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 ** returned if
1a79c 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 this statement
1a79d 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 was compiled usi
1a79e 6e 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 ng the legacy .
1a79f 20 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 ** sqlite3_prep
1a7a0 61 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e are() interface.
1a7a1 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 According to th
1a7a2 65 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e e docs, this can
1a7a3 20 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e only. ** be on
1a7a4 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 e of the values
1a7a5 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 in the first ass
1a7a6 65 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 ert() below. Var
1a7a7 69 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a iable p->rc . *
1a7a8 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 * contains the v
1a7a9 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 alue that would
1a7aa 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 be returned if s
1a7ab 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1a7ac 29 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c ) . ** were cal
1a7ad 6c 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 led on statement
1a7ae 20 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 p.. */. asser
1a7af 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f t( rc==SQLITE_RO
1a7b0 57 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 W || rc==SQLITE
1a7b1 5f 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 _DONE || rc==S
1a7b2 51 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 QLITE_ERROR .
1a7b3 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 || rc==SQLIT
1a7b4 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 E_BUSY || rc==SQ
1a7b5 4c 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b LITE_MISUSE. );
1a7b6 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 . assert( p->rc
1a7b7 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1a7b8 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f p->rc!=SQLITE_DO
1a7b9 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 NE );. if( p->i
1a7ba 73 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 sPrepareV2 && rc
1a7bb 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1a7bc 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
1a7bd 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ){. /* If thi
1a7be 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 s statement was
1a7bf 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 prepared using s
1a7c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
1a7c1 32 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2(), and an.
1a7c2 2a 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 ** error has occ
1a7c3 75 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 ured, then retur
1a7c4 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 n the error code
1a7c5 20 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 in p->rc to the
1a7c6 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 . ** caller.
1a7c7 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f Set the error co
1a7c8 64 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 de in the databa
1a7c9 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 se handle to the
1a7ca 20 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 same value..
1a7cb 20 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 */ . rc = db
1a7cc 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 ->errCode = p->r
1a7cd 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 c;. }. return
1a7ce 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 (rc&db->errMask)
1a7cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1a7d0 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c is the top-level
1a7d1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
1a7d2 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 of sqlite3_step(
1a7d3 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 ). Call.** sqli
1a7d4 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 te3Step() to do
1a7d5 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b most of the work
1a7d6 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 . If a schema e
1a7d7 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 rror occurs,.**
1a7d8 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 call sqlite3Repr
1a7d9 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 epare() and try
1a7da 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 again..*/.#ifdef
1a7db 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 SQLITE_OMIT_PAR
1a7dc 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 SER.SQLITE_API i
1a7dd 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
1a7de 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1a7df 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d tmt){. int rc =
1a7e0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1a7e1 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 if( pStmt ){.
1a7e2 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 Vdbe *v;.
1a7e3 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 v = (Vdbe*)pStmt
1a7e4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a7e5 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d tex_enter(v->db-
1a7e6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 >mutex);. rc
1a7e7 3d 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 = sqlite3Step(v)
1a7e8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a7e9 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d tex_leave(v->db-
1a7ea 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1a7eb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 eturn rc;.}.#els
1a7ec 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 e.SQLITE_API int
1a7ed 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 sqlite3_step(sq
1a7ee 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1a7ef 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
1a7f0 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1a7f1 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 if( pStmt ){.
1a7f2 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
1a7f3 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 Vdbe *v = (Vdb
1a7f4 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 e*)pStmt;. sq
1a7f5 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 lite3 *db = v->d
1a7f6 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d b;. sqlite3_m
1a7f7 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d utex_enter(db->m
1a7f8 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 utex);. while
1a7f9 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 ( (rc = sqlite3S
1a7fa 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f tep(v))==SQLITE_
1a7fb 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 SCHEMA.
1a7fc 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 && cnt++ < 5.
1a7fd 20 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 && (rc
1a7fe 20 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65 = vdbeReprepare
1a7ff 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 (v))==SQLITE_OK
1a800 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1a801 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 _reset(pStmt);.
1a802 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 v->expired
1a803 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
1a804 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 f( rc==SQLITE_SC
1a805 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76 HEMA && ALWAYS(v
1a806 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26 ->isPrepareV2) &
1a807 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72 & ALWAYS(db->pEr
1a808 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 r) ){. /* T
1a809 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 his case occurs
1a80a 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f after failing to
1a80b 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 recompile an sq
1a80c 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 l statement. .
1a80d 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 ** The error
1a80e 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 message from th
1a80f 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 e SQL compiler h
1a810 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
1a811 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a loaded . **
1a812 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
1a813 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 se handle. This
1a814 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 block copies the
1a815 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a error message .
1a816 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 ** from th
1a817 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1a818 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 e into the state
1a819 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 ment and sets th
1a81a 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 e statement.
1a81b 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 ** program cou
1a81c 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 nter to 0 to ens
1a81d 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 ure that when th
1a81e 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a e statement is .
1a81f 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a ** finaliz
1a820 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 ed or reset the
1a821 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 parser error mes
1a822 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c sage is availabl
1a823 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 e via. ** s
1a824 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1a825 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 and sqlite3_errc
1a826 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a ode().. */.
1a827 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1a828 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 *zErr = (const
1a829 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 char *)sqlite3_v
1a82a 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 alue_text(db->pE
1a82b 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 rr); . sqli
1a82c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d te3DbFree(db, v-
1a82d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
1a82e 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 if( !db->malloc
1a82f 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
1a830 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 v->zErrMsg = s
1a831 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
1a832 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 b, zErr);.
1a833 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 } else {.
1a834 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b v->zErrMsg = 0;
1a835 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d . v->rc =
1a836 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1a837 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1a838 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1a839 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1a83a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1a83b 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
1a83c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1a83d 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a c;.}.#endif../*.
1a83e 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 ** Extract the u
1a83f 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 ser data from a
1a840 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a841 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 structure and re
1a842 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 turn a.** pointe
1a843 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 r to it..*/.SQLI
1a844 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
1a845 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 ite3_user_data(s
1a846 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a847 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
1a848 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 && p->pFunc );.
1a849 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 return p->pFunc
1a84a 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a ->pUserData;.}..
1a84b 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 /*.** Extract th
1a84c 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d e user data from
1a84d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 a sqlite3_conte
1a84e 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 xt structure and
1a84f 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 return a.** poi
1a850 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 nter to it..*/.S
1a851 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1a852 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 3 *sqlite3_conte
1a853 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c xt_db_handle(sql
1a854 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 ite3_context *p)
1a855 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 {. assert( p &&
1a856 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 p->pFunc );. r
1a857 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d eturn p->s.db;.}
1a858 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1a859 6f 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 owing is the imp
1a85a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 lementation of a
1a85b 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 n SQL function t
1a85c 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 hat always.** fa
1a85d 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f ils with an erro
1a85e 72 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e r message statin
1a85f 67 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 g that the funct
1a860 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 ion is used in t
1a861 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 he.** wrong cont
1a862 65 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ext. The sqlite
1a863 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 3_overload_funct
1a864 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 ion() API might
1a865 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c construct.** SQL
1a866 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 function that u
1a867 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 se this routine
1a868 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 so that the func
1a869 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 tions will exist
1a86a 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 .** for name res
1a86b 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 olution but are
1a86c 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 actually overloa
1a86d 64 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 ded by the xFind
1a86e 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 Function.** meth
1a86f 6f 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 od of virtual ta
1a870 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f bles..*/.SQLITE_
1a871 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a872 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 ite3InvalidFunct
1a873 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 ion(. sqlite3_c
1a874 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1a875 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f /* The functio
1a876 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 n calling contex
1a877 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 t */. int NotUs
1a878 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ed,
1a879 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
1a87a 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
1a87b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 function */. sq
1a87c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
1a87d 74 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 tUsed2 /* Valu
1a87e 65 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 e of each argume
1a87f 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 nt */.){. const
1a880 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 char *zName = c
1a881 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a ontext->pFunc->z
1a882 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 Name;. char *zE
1a883 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 rr;. UNUSED_PAR
1a884 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
1a885 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 NotUsed2);. zE
1a886 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 rr = sqlite3_mpr
1a887 69 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 intf(. "una
1a888 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 ble to use funct
1a889 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 ion %s in the re
1a88a 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 quested context"
1a88b 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 , zName);. sqli
1a88c 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1a88d 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 (context, zErr,
1a88e 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 -1);. sqlite3_f
1a88f 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a ree(zErr);.}../*
1a890 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 .** Allocate or
1a891 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 return the aggre
1a892 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 gate context for
1a893 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1a894 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 . A new.** cont
1a895 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ext is allocated
1a896 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 on the first ca
1a897 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 ll. Subsequent
1a898 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 calls return the
1a899 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 .** same context
1a89a 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e that was return
1a89b 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c ed on prior call
1a89c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
1a89d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 void *sqlite3_a
1a89e 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
1a89f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1a8a0 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b *p, int nByte){
1a8a1 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 . Mem *pMem;.
1a8a2 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e assert( p && p->
1a8a3 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e pFunc && p->pFun
1a8a4 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 c->xStep );. as
1a8a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a8a6 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 tex_held(p->s.db
1a8a7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d ->mutex) );. pM
1a8a8 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 em = p->pMem;.
1a8a9 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( (pMem->flags
1a8aa 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 & MEM_Agg)==0 )
1a8ab 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d {. if( nByte=
1a8ac 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1a8ad 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1a8ae 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b eExternal(pMem);
1a8af 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1a8b0 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1a8b1 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 pMem->z = 0
1a8b2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1a8b3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1a8b4 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
1a8b5 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 e, 0);. pMe
1a8b6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 m->flags = MEM_A
1a8b7 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e gg;. pMem->
1a8b8 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e u.pDef = p->pFun
1a8b9 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 c;. if( pMe
1a8ba 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 m->z ){.
1a8bb 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 memset(pMem->z,
1a8bc 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 0, nByte);.
1a8bd 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1a8be 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 eturn (void*)pMe
1a8bf 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 m->z;.}../*.** R
1a8c0 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 eturn the auxila
1a8c1 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c ry data pointer,
1a8c2 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 if any, for the
1a8c3 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e iArg'th argumen
1a8c4 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 t to.** the user
1a8c5 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 -function define
1a8c6 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 d by pCtx..*/.SQ
1a8c7 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
1a8c8 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 qlite3_get_auxda
1a8c9 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ta(sqlite3_conte
1a8ca 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 xt *pCtx, int iA
1a8cb 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 rg){. VdbeFunc
1a8cc 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 *pVdbeFunc;.. a
1a8cd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a8ce 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a8cf 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a8d0 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 pVdbeFunc = pC
1a8d1 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 tx->pVdbeFunc;.
1a8d2 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 if( !pVdbeFunc
1a8d3 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 || iArg>=pVdbeFu
1a8d4 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 nc->nAux || iArg
1a8d5 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e <0 ){. return
1a8d6 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
1a8d7 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 pVdbeFunc->apAu
1a8d8 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a x[iArg].pAux;.}.
1a8d9 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 ./*.** Set the a
1a8da 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 uxilary data poi
1a8db 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 nter and delete
1a8dc 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 function, for th
1a8dd 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 e iArg'th.** arg
1a8de 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 ument to the use
1a8df 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e r-function defin
1a8e0 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 ed by pCtx. Any
1a8e1 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 previous value i
1a8e2 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 s.** deleted by
1a8e3 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 calling the dele
1a8e4 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 te function spec
1a8e5 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 ified when it wa
1a8e6 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 s set..*/.SQLITE
1a8e7 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1a8e8 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 3_set_auxdata(.
1a8e9 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1a8ea 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 *pCtx, . int i
1a8eb 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 Arg, . void *pA
1a8ec 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 ux, . void (*xD
1a8ed 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b elete)(void*).){
1a8ee 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 . struct AuxDat
1a8ef 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 a *pAuxData;. V
1a8f0 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 dbeFunc *pVdbeFu
1a8f1 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 nc;. if( iArg<0
1a8f2 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a ) goto failed;.
1a8f3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1a8f4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1a8f5 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1a8f6 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 );. pVdbeFunc
1a8f7 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e = pCtx->pVdbeFun
1a8f8 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 c;. if( !pVdbeF
1a8f9 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 unc || pVdbeFunc
1a8fa 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a ->nAux<=iArg ){.
1a8fb 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 int nAux = (
1a8fc 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 pVdbeFunc ? pVdb
1a8fd 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 eFunc->nAux : 0)
1a8fe 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f ;. int nMallo
1a8ff 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 c = sizeof(VdbeF
1a900 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 unc) + sizeof(st
1a901 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 ruct AuxData)*iA
1a902 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e rg;. pVdbeFun
1a903 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 c = sqlite3DbRea
1a904 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c lloc(pCtx->s.db,
1a905 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c pVdbeFunc, nMal
1a906 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 loc);. if( !p
1a907 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 VdbeFunc ){.
1a908 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
1a909 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 }. pCtx->p
1a90a 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 VdbeFunc = pVdbe
1a90b 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 Func;. memset
1a90c 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 (&pVdbeFunc->apA
1a90d 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a ux[nAux], 0, siz
1a90e 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 eof(struct AuxDa
1a90f 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 ta)*(iArg+1-nAux
1a910 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e ));. pVdbeFun
1a911 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 c->nAux = iArg+1
1a912 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d ;. pVdbeFunc-
1a913 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 >pFunc = pCtx->p
1a914 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 Func;. }.. pAu
1a915 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 xData = &pVdbeFu
1a916 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b nc->apAux[iArg];
1a917 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d . if( pAuxData-
1a918 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 >pAux && pAuxDat
1a919 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 a->xDelete ){.
1a91a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c pAuxData->xDel
1a91b 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 ete(pAuxData->pA
1a91c 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 ux);. }. pAuxD
1a91d 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 ata->pAux = pAux
1a91e 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 ;. pAuxData->xD
1a91f 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b elete = xDelete;
1a920 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c . return;..fail
1a921 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 ed:. if( xDelet
1a922 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 e ){. xDelete
1a923 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 (pAux);. }.}..#
1a924 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a925 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
1a926 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1a927 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
1a928 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e he Step function
1a929 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 of a aggregate
1a92a 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c has been .** cal
1a92b 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
1a92c 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 function is depr
1a92d 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 ecated. Do not
1a92e 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 use it for new c
1a92f 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 ode. It is.** p
1a930 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 rovide only to a
1a931 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 void breaking le
1a932 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 gacy code. New
1a933 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
1a934 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 on.** implementa
1a935 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 tions should kee
1a936 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e p their own coun
1a937 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 ts within their
1a938 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e aggregate.** con
1a939 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f text..*/.SQLITE_
1a93a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1a93b 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 aggregate_count(
1a93c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a93d 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1a93e 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 && p->pMem && p
1a93f 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 ->pFunc && p->pF
1a940 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 unc->xStep );.
1a941 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e return p->pMem->
1a942 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a n;.}.#endif../*.
1a943 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1a944 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1a945 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
1a946 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d t for the statem
1a947 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 ent pStmt..*/.SQ
1a948 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a949 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
1a94a 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1a94b 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1a94c 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 pVm = (Vdbe *)pS
1a94d 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 tmt;. return pV
1a94e 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c m ? pVm->nResCol
1a94f 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a umn : 0;.}../*.*
1a950 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1a951 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 ber of values av
1a952 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 ailable from the
1a953 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
1a954 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 the.** currently
1a955 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 executing state
1a956 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 ment pStmt..*/.S
1a957 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a958 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 lite3_data_count
1a959 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a95a 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1a95b 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1a95c 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 mt;. if( pVm==0
1a95d 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 || pVm->pResult
1a95e 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Set==0 ) return
1a95f 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 0;. return pVm-
1a960 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a >nResColumn;.}..
1a961 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
1a962 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 see if column iC
1a963 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 ol of the given
1a964 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c statement is val
1a965 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 id. If.** it is
1a966 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 , return a point
1a967 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f er to the Mem fo
1a968 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 r the value of t
1a969 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 hat column..** I
1a96a 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 f iCol is not va
1a96b 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f lid, return a po
1a96c 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 inter to a Mem w
1a96d 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 hich has a value
1a96e 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a .** of NULL..*/.
1a96f 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 static Mem *colu
1a970 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 mnMem(sqlite3_st
1a971 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1a972 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a ){. Vdbe *pVm;.
1a973 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 int vals;. Me
1a974 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 m *pOut;.. pVm
1a975 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1a976 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 . if( pVm && pV
1a977 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 m->pResultSet!=0
1a978 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 && i<pVm->nResC
1a979 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b olumn && i>=0 ){
1a97a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a97b 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 ex_enter(pVm->db
1a97c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 ->mutex);. va
1a97d 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 ls = sqlite3_dat
1a97e 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a a_count(pStmt);.
1a97f 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d pOut = &pVm-
1a980 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a >pResultSet[i];.
1a981 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1a982 28 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63 ((double)0) In c
1a983 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
1a984 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
1a985 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74 T... */. stat
1a986 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c ic const Mem nul
1a987 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f lMem = {{0}, (do
1a988 75 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 uble)0, 0, "", 0
1a989 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 , MEM_Null, SQLI
1a98a 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 TE_NULL, 0, 0, 0
1a98b 20 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20 };. if( pVm
1a98c 26 26 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 && ALWAYS(pVm->d
1a98d 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 b) ){. sqli
1a98e 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1a98f 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b pVm->db->mutex);
1a990 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1a991 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c ror(pVm->db, SQL
1a992 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 ITE_RANGE, 0);.
1a993 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 }. pOut =
1a994 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a (Mem*)&nullMem;.
1a995 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 }. return pOu
1a996 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 t;.}../*.** This
1a997 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
1a998 6c 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 led after invoki
1a999 6e 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 ng an sqlite3_va
1a99a 6c 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e lue_XXX function
1a99b 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e on a .** column
1a99c 20 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 value (i.e. a v
1a99d 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
1a99e 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 evaluating an S
1a99f 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e QL expression in
1a9a0 20 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c the.** select l
1a9a1 69 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 ist of a SELECT
1a9a2 73 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 statement) that
1a9a3 6d 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c may cause a mall
1a9a4 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 oc() failure. If
1a9a5 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 .** malloc() ha
1a9a6 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 s failed, the th
1a9a7 72 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c reads mallocFail
1a9a8 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 ed flag is clear
1a9a9 65 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c ed and the resul
1a9aa 74 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 t.** code of sta
1a9ab 74 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 tement pStmt set
1a9ac 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d to SQLITE_NOMEM
1a9ad 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 ..**.** Specific
1a9ae 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 ally, this is ca
1a9af 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1a9b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 :.**.** sqli
1a9b1 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 te3_column_int()
1a9b2 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
1a9b3 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a column_int64().*
1a9b4 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1a9b5 6c 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 lumn_text().**
1a9b6 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
1a9b7 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 n_text16().**
1a9b8 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
1a9b9 5f 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 _real().** s
1a9ba 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
1a9bb 74 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c tes().** sql
1a9bc 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
1a9bd 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 s16().**.** But
1a9be 6e 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f not for sqlite3_
1a9bf 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 column_blob(), w
1a9c0 68 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 hich never calls
1a9c1 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
1a9c2 61 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e atic void column
1a9c3 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 MallocFailure(sq
1a9c4 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1a9c5 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c t).{. /* If mal
1a9c6 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 loc() failed dur
1a9c7 69 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 ing an encoding
1a9c8 63 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 conversion withi
1a9c9 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 n an. ** sqlite
1a9ca 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 3_column_XXX API
1a9cb 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 , then set the r
1a9cc 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 eturn code of th
1a9cd 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 e statement to.
1a9ce 20 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ** SQLITE_NOMEM
1a9cf 2e 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 . The next call
1a9d0 74 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 to _step() (if a
1a9d1 6e 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ny) will return
1a9d2 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a SQLITE_ERROR. *
1a9d3 2a 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 * and _finalize(
1a9d4 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f ) will return NO
1a9d5 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 MEM.. */. Vdbe
1a9d6 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
1a9d7 74 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a tmt;. if( p ){.
1a9d8 20 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 p->rc = sqli
1a9d9 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 te3ApiExit(p->db
1a9da 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 , p->rc);. sq
1a9db 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1a9dc 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1a9dd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a . }.}../*******
1a9de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a9df 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ***** sqlite3_co
1a9e0 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a lumn_ *********
1a9e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a9e2 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ******.** The fo
1a9e3 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 llowing routines
1a9e4 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 are used to acc
1a9e5 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 ess elements of
1a9e6 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a the current row.
1a9e7 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ** in the result
1a9e8 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f set..*/.SQLITE_
1a9e9 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1a9ea 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
1a9eb 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 lob(sqlite3_stmt
1a9ec 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
1a9ed 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 . const void *v
1a9ee 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 al;. val = sqli
1a9ef 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 te3_value_blob(
1a9f0 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c columnMem(pStmt,
1a9f1 69 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 i) );. /* Even
1a9f2 74 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 though there is
1a9f3 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 no encoding conv
1a9f4 65 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c ersion, value_bl
1a9f5 6f 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 ob() might. **
1a9f6 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c need to call mal
1a9f7 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 loc() to expand
1a9f8 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 the result of a
1a9f9 7a 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a zeroblob() . **
1a9fa 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 expression. .
1a9fb 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f */. columnMallo
1a9fc 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1a9fd 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1a9fe 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1a9ff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
1aa00 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ytes(sqlite3_stm
1aa01 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1aa02 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 {. int val = sq
1aa03 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1aa04 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 s( columnMem(pSt
1aa05 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1aa06 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1aa07 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1aa08 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1aa09 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
1aa0a 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c lumn_bytes16(sql
1aa0b 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aa0c 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
1aa0d 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1aa0e 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c lue_bytes16( col
1aa0f 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1aa10 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1aa11 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1aa12 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1aa13 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
1aa14 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
1aa15 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
1aa16 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1aa17 74 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 t i){. double v
1aa18 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1aa19 75 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d ue_double( colum
1aa1a 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1aa1b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1aa1c 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1aa1d 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1aa1e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1aa1f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 lite3_column_int
1aa20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1aa21 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1aa22 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 int val = sqlite
1aa23 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 3_value_int( col
1aa24 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1aa25 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1aa26 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1aa27 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1aa28 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1aa29 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 te_int64 sqlite3
1aa2a 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 _column_int64(sq
1aa2b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1aa2c 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c t, int i){. sql
1aa2d 69 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 ite_int64 val =
1aa2e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
1aa2f 74 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 t64( columnMem(p
1aa30 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1aa31 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1aa32 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1aa33 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1aa34 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e API const unsign
1aa35 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 ed char *sqlite3
1aa36 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c _column_text(sql
1aa37 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aa38 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 , int i){. cons
1aa39 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1aa3a 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 *val = sqlite3_v
1aa3b 61 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d alue_text( colum
1aa3c 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1aa3d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1aa3e 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1aa3f 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1aa40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1aa41 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
1aa42 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 _column_value(sq
1aa43 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1aa44 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d t, int i){. Mem
1aa45 20 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d *pOut = columnM
1aa46 65 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 em(pStmt, i);.
1aa47 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 if( pOut->flags&
1aa48 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 MEM_Static ){.
1aa49 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d pOut->flags &=
1aa4a 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 ~MEM_Static;.
1aa4b 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d pOut->flags |=
1aa4c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a MEM_Ephem;. }.
1aa4d 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
1aa4e 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
1aa4f 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
1aa50 76 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a value *)pOut;.}.
1aa51 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1aa52 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1aa53 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1aa54 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1aa55 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 text16(sqlite3_s
1aa56 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1aa57 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 i){. const void
1aa58 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f *val = sqlite3_
1aa59 76 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f value_text16( co
1aa5a 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
1aa5b 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c );. columnMall
1aa5c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 ocFailure(pStmt)
1aa5d 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a ;. return val;.
1aa5e 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1aa5f 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1aa60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1aa61 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1aa62 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ype(sqlite3_stmt
1aa63 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
1aa64 0a 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 . int iType = s
1aa65 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
1aa66 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 e( columnMem(pSt
1aa67 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1aa68 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1aa69 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1aa6a 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 iType;.}../* The
1aa6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 following funct
1aa6c 69 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e ion is experimen
1aa6d 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 tal and subject
1aa6e 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 to change or.**
1aa6f 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 removal */./*int
1aa70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1aa71 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c numeric_type(sql
1aa72 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aa73 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 , int i){.** re
1aa74 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c turn sqlite3_val
1aa75 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
1aa76 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1aa77 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f ,i) );.**}.*/../
1aa78 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 *.** Convert the
1aa79 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 N-th element of
1aa7a 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 pStmt->pColName
1aa7b 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 [] into a string
1aa7c 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 using.** xFunc(
1aa7d 29 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 ) then return th
1aa7e 61 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e at string. If N
1aa7f 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
1aa80 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a , return 0..**.*
1aa81 2a 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74 * There are up t
1aa82 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 o 5 names for ea
1aa83 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 ch column. useT
1aa84 79 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 ype determines w
1aa85 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 hich.** name is
1aa86 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 returned. Here
1aa87 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a are the names:.*
1aa88 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 *.** 0 T
1aa89 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 he column name a
1aa8a 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 s it should be d
1aa8b 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 isplayed for out
1aa8c 70 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 put.** 1
1aa8d 20 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 The datatype na
1aa8e 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d me for the colum
1aa8f 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 n.** 2 T
1aa90 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 he name of the d
1aa91 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 atabase that the
1aa92 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 column derives
1aa93 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 from.** 3
1aa94 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 The name of th
1aa95 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 e table that the
1aa96 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 column derives
1aa97 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 from.** 4
1aa98 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 The name of th
1aa99 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 e table column t
1aa9a 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 hat the result c
1aa9b 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
1aa9c 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 om.**.** If the
1aa9d 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 result is not a
1aa9e 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 simple column re
1aa9f 66 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 ference (if it i
1aaa0 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a s an expression.
1aaa1 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 ** or a constant
1aaa2 29 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 ) then useTypes
1aaa3 32 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 2, 3, and 4 retu
1aaa4 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 rn NULL..*/.stat
1aaa5 69 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 ic const void *c
1aaa6 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c olumnName(. sql
1aaa7 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aaa8 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e ,. int N,. con
1aaa9 73 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 st void *(*xFunc
1aaaa 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 )(Mem*),. int u
1aaab 73 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 seType.){. cons
1aaac 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b t void *ret = 0;
1aaad 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1aaae 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e be *)pStmt;. in
1aaaf 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a t n;. sqlite3 *
1aab0 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 db = p->db;. .
1aab1 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 assert( db!=0 )
1aab2 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f ;. n = sqlite3_
1aab3 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 column_count(pSt
1aab4 6d 74 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 mt);. if( N<n &
1aab5 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 & N>=0 ){. N
1aab6 2b 3d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 += useType*n;.
1aab7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1aab8 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 enter(db->mutex)
1aab9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 ;. assert( db
1aaba 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d ->mallocFailed==
1aabb 30 20 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 0 );. ret = x
1aabc 46 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d Func(&p->aColNam
1aabd 65 5b 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 e[N]);. /* A
1aabe 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 malloc may have
1aabf 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f failed inside o
1aac0 66 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 f the xFunc() ca
1aac1 6c 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 ll. If this.
1aac2 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 ** is the case,
1aac3 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 clear the malloc
1aac4 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 Failed flag and
1aac5 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 return NULL..
1aac6 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e */. if( db->
1aac7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1aac8 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db->malloc
1aac9 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 Failed = 0;.
1aaca 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d ret = 0;. }
1aacb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1aacc 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 ex_leave(db->mut
1aacd 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1aace 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n ret;.}../*.**
1aacf 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 Return the name
1aad0 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d of the Nth colum
1aad1 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 n of the result
1aad2 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 set returned by
1aad3 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
1aad4 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 pStmt..*/.SQLIT
1aad5 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1aad6 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1aad7 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
1aad8 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
1aad9 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
1aada 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
1aadb 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
1aadc 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
1aadd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1aade 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b , COLNAME_NAME);
1aadf 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1aae0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1aae1 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1aae2 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1aae3 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 mn_name16(sqlite
1aae4 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1aae5 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1aae6 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
1aae7 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
1aae8 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
1aae9 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
1aaea 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f text16, COLNAME_
1aaeb 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a NAME);.}.#endif.
1aaec 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e ./*.** Constrain
1aaed 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 t: If you have
1aaee 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
1aaef 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 TADATA then you
1aaf0 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 must.** not defi
1aaf1 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 ne OMIT_DECLTYPE
1aaf2 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
1aaf3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 (SQLITE_OMIT_DEC
1aaf4 4c 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 LTYPE) && define
1aaf5 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
1aaf6 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 COLUMN_METADATA)
1aaf7 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e .# error "Must n
1aaf8 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 ot define both S
1aaf9 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
1aafa 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 YPE \. a
1aafb 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 nd SQLITE_ENABLE
1aafc 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
1aafd 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ".#endif..#ifnde
1aafe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
1aaff 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 CLTYPE./*.** Ret
1ab00 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 urn the column d
1ab01 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 eclaration type
1ab02 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 (if applicable)
1ab03 6f 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c of the 'i'th col
1ab04 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 umn.** of the re
1ab05 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 sult set of SQL
1ab06 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e statement pStmt.
1ab07 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1ab08 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1ab09 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 e3_column_declty
1ab0a 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 pe(sqlite3_stmt
1ab0b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1ab0c 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1ab0d 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1ab0e 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1ab0f 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1ab10 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 e3_value_text, C
1ab11 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 OLNAME_DECLTYPE)
1ab12 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1ab13 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1ab14 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1ab15 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1ab16 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 umn_decltype16(s
1ab17 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ab18 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1ab19 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1ab1a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1ab1b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1ab1c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1ab1d 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
1ab1e 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a NAME_DECLTYPE);.
1ab1f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1ab20 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1ab21 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1ab22 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 E_OMIT_DECLTYPE
1ab23 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
1ab24 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
1ab25 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 METADATA./*.** R
1ab26 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
1ab27 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1ab28 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 rom which a resu
1ab29 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 lt column derive
1ab2a 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 s..** NULL is re
1ab2b 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 turned if the re
1ab2c 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 sult column is a
1ab2d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 n expression or
1ab2e 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 constant or.** a
1ab2f 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 nything else whi
1ab30 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 ch is not an una
1ab31 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 biguous referenc
1ab32 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
1ab33 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 column..*/.SQLIT
1ab34 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1ab35 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1ab36 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 _database_name(s
1ab37 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ab38 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1ab39 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1ab3a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1ab3b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1ab3c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1ab3d 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
1ab3e 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a ME_DATABASE);.}.
1ab3f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ab40 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1ab41 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1ab42 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1ab43 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 database_name16(
1ab44 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1ab45 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 tmt, int N){. r
1ab46 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 eturn columnName
1ab47 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e (. pStmt, N
1ab48 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a , (const void*(*
1ab49 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f )(Mem*))sqlite3_
1ab4a 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f value_text16, CO
1ab4b 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b LNAME_DATABASE);
1ab4c 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1ab4d 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1ab4e 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
1ab4f 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1ab50 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 table from which
1ab51 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
1ab52 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c derives..** NUL
1ab53 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 L is returned if
1ab54 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
1ab55 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 mn is an express
1ab56 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 ion or constant
1ab57 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 or.** anything e
1ab58 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 lse which is not
1ab59 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 an unabiguous r
1ab5a 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 eference to a da
1ab5b 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a tabase column..*
1ab5c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1ab5d 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1ab5e 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
1ab5f 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 me(sqlite3_stmt
1ab60 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1ab61 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1ab62 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1ab63 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1ab64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1ab65 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 e3_value_text, C
1ab66 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d OLNAME_TABLE);.}
1ab67 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ab68 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1ab69 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1ab6a 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1ab6b 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 _table_name16(sq
1ab6c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1ab6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1ab6e 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1ab6f 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1ab70 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1ab71 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1ab72 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1ab73 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 AME_TABLE);.}.#e
1ab74 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1ab75 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a MIT_UTF16 */../*
1ab76 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1ab77 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1ab78 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 column from whi
1ab79 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 ch a result colu
1ab7a 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e mn derives..** N
1ab7b 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
1ab7c 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f if the result co
1ab7d 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 lumn is an expre
1ab7e 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e ssion or constan
1ab7f 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 t or.** anything
1ab80 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e else which is n
1ab81 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 ot an unabiguous
1ab82 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
1ab83 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e database column.
1ab84 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1ab85 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1ab86 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
1ab87 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
1ab88 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
1ab89 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
1ab8a 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
1ab8b 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
1ab8c 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
1ab8d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1ab8e 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e , COLNAME_COLUMN
1ab8f 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
1ab90 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1ab91 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1ab92 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
1ab93 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 lumn_origin_name
1ab94 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1ab95 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1ab96 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1ab97 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1ab98 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1ab99 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1ab9a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
1ab9b 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 COLNAME_COLUMN)
1ab9c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1ab9d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
1ab9e 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c */.#endif /* SQL
1ab9f 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ITE_ENABLE_COLUM
1aba0 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a N_METADATA */...
1aba1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1aba2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aba3 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 sqlite3_bind_
1aba4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aba5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a ***********.** .
1aba6 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 ** Routines used
1aba7 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 to attach value
1aba8 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 s to wildcards i
1aba9 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c n a compiled SQL
1abaa 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f statement..*/./
1abab 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 *.** Unbind the
1abac 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 value bound to v
1abad 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 ariable i in vir
1abae 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 tual machine p.
1abaf 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 This is the .**
1abb0 74 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 the same as bind
1abb1 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 ing a NULL value
1abb2 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 to the column.
1abb3 49 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d If the "i" param
1abb4 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f eter is.** out o
1abb5 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 f range, then SQ
1abb6 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 LITE_RANGE is re
1abb7 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 turned. Othewise
1abb8 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a SQLITE_OK..**.*
1abb9 2a 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 * A successful e
1abba 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 valuation of thi
1abbb 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 s routine acquir
1abbc 65 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 es the mutex on
1abbd 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 p..** the mutex
1abbe 69 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 is released if a
1abbf 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 ny kind of error
1abc0 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 occurs..**.** T
1abc1 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 he error code st
1abc2 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 ored in database
1abc3 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 p->db is overwr
1abc4 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 itten with the r
1abc5 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 eturn.** value i
1abc6 6e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 n any case..*/.s
1abc7 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e tatic int vdbeUn
1abc8 62 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e bind(Vdbe *p, in
1abc9 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 t i){. Mem *pVa
1abca 72 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 r;. if( p==0 )
1abcb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
1abcc 53 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f SUSE;. sqlite3_
1abcd 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 mutex_enter(p->d
1abce 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 b->mutex);. if(
1abcf 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f p->magic!=VDBE_
1abd0 4d 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e MAGIC_RUN || p->
1abd1 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c pc>=0 ){. sql
1abd2 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c ite3Error(p->db,
1abd3 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 SQLITE_MISUSE,
1abd4 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0);. sqlite3_
1abd5 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1abd6 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
1abd7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1abd8 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 USE;. }. if( i
1abd9 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 <1 || i>p->nVar
1abda 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1abdb 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 ror(p->db, SQLIT
1abdc 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 E_RANGE, 0);.
1abdd 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1abde 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1abdf 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 x);. return S
1abe0 51 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d QLITE_RANGE;. }
1abe1 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d . i--;. pVar =
1abe2 20 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 &p->aVar[i];.
1abe3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1abe4 6c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 lease(pVar);. p
1abe5 56 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Var->flags = MEM
1abe6 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 _Null;. sqlite3
1abe7 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c Error(p->db, SQL
1abe8 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 ITE_OK, 0);. re
1abe9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1abea 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 }../*.** Bind a
1abeb 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c text or BLOB val
1abec 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ue..*/.static in
1abed 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 t bindText(. sq
1abee 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1abef 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 t, /* The stat
1abf0 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 ement to bind ag
1abf1 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ainst */. int i
1abf2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1abf3 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1abf4 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 e parameter to b
1abf5 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ind */. const v
1abf6 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 oid *zData,
1abf7 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
1abf8 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 e data to be bou
1abf9 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 nd */. int nDat
1abfa 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f a, /
1abfb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1abfc 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 s of data to be
1abfd 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 bound */. void
1abfe 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 (*xDel)(void*),
1abff 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
1ac00 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a for the data */.
1ac01 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 u8 encoding
1ac02 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
1ac03 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 ding for the dat
1ac04 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a a */.){. Vdbe *
1ac05 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
1ac06 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a t;. Mem *pVar;.
1ac07 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 int rc;.. rc
1ac08 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 = vdbeUnbind(p,
1ac09 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 i);. if( rc==SQ
1ac0a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
1ac0b 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 f( zData!=0 ){.
1ac0c 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e pVar = &p->
1ac0d 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 aVar[i-1];.
1ac0e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1ac0f 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c eMemSetStr(pVar,
1ac10 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 zData, nData, e
1ac11 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a ncoding, xDel);.
1ac12 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1ac13 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 LITE_OK && encod
1ac14 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ing!=0 ){.
1ac15 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1ac16 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
1ac17 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 (pVar, ENC(p->db
1ac18 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
1ac19 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
1ac1a 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 ->db, rc, 0);.
1ac1b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ac1c 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 ApiExit(p->db, r
1ac1d 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 c);. }. sq
1ac1e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1ac1f 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1ac20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1ac21 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 ;.}.../*.** Bind
1ac22 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f a blob value to
1ac23 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1ac24 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 t variable..*/.S
1ac25 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1ac26 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
1ac27 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1ac28 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 *pStmt, . int i
1ac29 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1ac2a 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e *zData, . int n
1ac2b 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a Data, . void (*
1ac2c 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a xDel)(void*).){.
1ac2d 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 return bindTex
1ac2e 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 t(pStmt, i, zDat
1ac2f 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 a, nData, xDel,
1ac30 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 0);.}.SQLITE_API
1ac31 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1ac32 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 d_double(sqlite3
1ac33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1ac34 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c t i, double rVal
1ac35 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ue){. int rc;.
1ac36 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1ac37 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d *)pStmt;. rc =
1ac38 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
1ac39 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1ac3a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 ITE_OK ){. sq
1ac3b 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
1ac3c 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 ouble(&p->aVar[i
1ac3d 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 -1], rValue);.
1ac3e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1ac3f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1ac40 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1ac41 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 n rc;.}.SQLITE_A
1ac42 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1ac43 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f ind_int(sqlite3_
1ac44 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 stmt *p, int i,
1ac45 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 int iValue){. r
1ac46 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 eturn sqlite3_bi
1ac47 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 nd_int64(p, i, (
1ac48 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 i64)iValue);.}.S
1ac49 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1ac4a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 lite3_bind_int64
1ac4b 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1ac4c 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c Stmt, int i, sql
1ac4d 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 ite_int64 iValue
1ac4e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
1ac4f 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
1ac50 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 )pStmt;. rc = v
1ac51 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b dbeUnbind(p, i);
1ac52 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1ac53 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
1ac54 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
1ac55 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 64(&p->aVar[i-1]
1ac56 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 , iValue);. s
1ac57 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1ac58 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1ac59 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1ac5a 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 c;.}.SQLITE_API
1ac5b 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1ac5c 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 _null(sqlite3_st
1ac5d 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1ac5e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
1ac5f 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
1ac60 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
1ac61 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
1ac62 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ac63 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
1ac64 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1ac65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1ac66 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1ac67 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1ac68 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
1ac69 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 t( . sqlite3_st
1ac6a 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e mt *pStmt, . in
1ac6b 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 t i, . const ch
1ac6c 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e ar *zData, . in
1ac6d 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 t nData, . void
1ac6e 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a (*xDel)(void*).
1ac6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 ){. return bind
1ac70 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a Text(pStmt, i, z
1ac71 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 Data, nData, xDe
1ac72 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b l, SQLITE_UTF8);
1ac73 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1ac74 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1ac75 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1ac76 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 te3_bind_text16(
1ac77 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1ac78 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 *pStmt, . int i
1ac79 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1ac7a 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e *zData, . int n
1ac7b 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a Data, . void (*
1ac7c 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a xDel)(void*).){.
1ac7d 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 return bindTex
1ac7e 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 t(pStmt, i, zDat
1ac7f 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 a, nData, xDel,
1ac80 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
1ac81 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a VE);.}.#endif /*
1ac82 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1ac83 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 16 */.SQLITE_API
1ac84 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1ac85 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f d_value(sqlite3_
1ac86 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1ac87 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 i, const sqlite
1ac88 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 3_value *pValue)
1ac89 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77 {. int rc;. sw
1ac8a 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79 itch( pValue->ty
1ac8b 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 pe ){. case S
1ac8c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b QLITE_INTEGER: {
1ac8d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1ac8e 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 te3_bind_int64(p
1ac8f 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d Stmt, i, pValue-
1ac90 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65 >u.i);. bre
1ac91 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1ac92 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a se SQLITE_FLOAT:
1ac93 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 {. rc = sq
1ac94 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c lite3_bind_doubl
1ac95 65 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c e(pStmt, i, pVal
1ac96 75 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72 ue->r);. br
1ac97 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1ac98 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a ase SQLITE_BLOB:
1ac99 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56 61 {. if( pVa
1ac9a 6c 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d lue->flags & MEM
1ac9b 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 _Zero ){.
1ac9c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 rc = sqlite3_bi
1ac9d 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d nd_zeroblob(pStm
1ac9e 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e t, i, pValue->u.
1ac9f 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 nZero);. }e
1aca0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
1aca1 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 = sqlite3_bind_b
1aca2 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 lob(pStmt, i, pV
1aca3 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d alue->z, pValue-
1aca4 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 >n,SQLITE_TRANSI
1aca5 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ENT);. }.
1aca6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1aca7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
1aca8 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72 _TEXT: {. r
1aca9 63 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74 c = bindText(pSt
1acaa 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a mt,i, pValue->z
1acab 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c , pValue->n, SQL
1acac 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 ITE_TRANSIENT,.
1acad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1acae 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 61 pVa
1acaf 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 lue->enc);.
1acb0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1acb1 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1acb2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1acb3 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c bind_null(pStmt,
1acb4 20 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b i);. break
1acb5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1acb6 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
1acb7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1acb8 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
1acb9 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1acba 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 tmt, int i, int
1acbb 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 n){. int rc;.
1acbc 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1acbd 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 *)pStmt;. rc =
1acbe 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 vdbeUnbind(p, i)
1acbf 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1acc0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
1acc1 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 ite3VdbeMemSetZe
1acc2 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b roBlob(&p->aVar[
1acc3 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71 i-1], n);. sq
1acc4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1acc5 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1acc6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1acc7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1acc8 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1acc9 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 63 wildcards that c
1acca 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c an be potentiall
1accb 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 y bound to..** T
1accc 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
1accd 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 dded to support
1acce 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a DBD::SQLite. .*
1accf 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1acd0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 sqlite3_bind_pa
1acd1 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 rameter_count(sq
1acd2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1acd3 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 t){. Vdbe *p =
1acd4 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1acd5 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 return p ? p->nV
1acd6 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ar : 0;.}../*.**
1acd7 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e Create a mappin
1acd8 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 g from variable
1acd9 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 numbers to varia
1acda 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 ble names.** in
1acdb 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d the Vdbe.azVar[]
1acdc 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 array, if such
1acdd 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e a mapping does n
1acde 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 ot already.** ex
1acdf 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
1ace0 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 oid createVarMap
1ace1 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 (Vdbe *p){. if(
1ace2 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 !p->okVar ){.
1ace3 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 int j;. Op
1ace4 2a 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 *pOp;. sqlite
1ace5 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
1ace6 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1ace7 20 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e /* The race con
1ace8 64 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68 dition here is h
1ace9 61 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f armless. If two
1acea 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 threads call th
1aceb 69 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e is. ** routin
1acec 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64 e on the same Vd
1aced 62 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 be at the same t
1acee 69 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d ime, they both m
1acef 69 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20 ight end. **
1acf0 75 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 up initializing
1acf1 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d the Vdbe.azVar[]
1acf2 20 61 72 72 61 79 2e 20 20 54 68 61 74 20 69 73 array. That is
1acf3 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a a little extra.
1acf4 20 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20 ** work but
1acf5 69 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 it results in th
1acf6 65 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20 e same answer..
1acf7 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d */. for(j=
1acf8 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 0, pOp=p->aOp; j
1acf9 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f <p->nOp; j++, pO
1acfa 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 p++){. if(
1acfb 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1acfc 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 Variable ){.
1acfd 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1acfe 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 >p1>0 && pOp->p1
1acff 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 <=p->nVar );.
1ad00 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f p->azVar[pO
1ad01 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e p->p1-1] = pOp->
1ad02 70 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 p4.z;. }.
1ad03 20 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 }. p->okVar
1ad04 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 1;. sqlite
1ad05 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1ad06 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1ad07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1ad08 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 the name of a w
1ad09 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 ildcard paramete
1ad0a 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 r. Return NULL
1ad0b 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 if the index.**
1ad0c 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 is out of range
1ad0d 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 or if the wildca
1ad0e 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a rd is unnamed..*
1ad0f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 *.** The result
1ad10 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e is always UTF-8.
1ad11 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1ad12 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1ad13 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
1ad14 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 r_name(sqlite3_s
1ad15 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1ad16 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 i){. Vdbe *p =
1ad17 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1ad18 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 if( p==0 || i<1
1ad19 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a || i>p->nVar ){.
1ad1a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1ad1b 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 }. createVarMap
1ad1c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d (p);. return p-
1ad1d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a >azVar[i-1];.}..
1ad1e 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 /*.** Given a wi
1ad1f 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 ldcard parameter
1ad20 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 name, return th
1ad21 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 e index of the v
1ad22 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 ariable.** with
1ad23 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 that name. If t
1ad24 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 here is no varia
1ad25 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 ble with the giv
1ad26 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 en name,.** retu
1ad27 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rn 0..*/.SQLITE_
1ad28 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1ad29 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
1ad2a 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d ndex(sqlite3_stm
1ad2b 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 t *pStmt, const
1ad2c 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 char *zName){.
1ad2d 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
1ad2e 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b )pStmt;. int i;
1ad2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
1ad30 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1ad31 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 . createVarMap(
1ad32 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 p); . if( zName
1ad33 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
1ad34 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 i<p->nVar; i++)
1ad35 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 {. const ch
1ad36 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 ar *z = p->azVar
1ad37 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a [i];. if( z
1ad38 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 && strcmp(z,zNa
1ad39 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
1ad3a 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 return i+1;.
1ad3b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1ad3c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
1ad3d 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c *.** Transfer al
1ad3e 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 l bindings from
1ad3f 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d the first statem
1ad40 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 ent over to the
1ad41 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 second..*/.SQLIT
1ad42 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ad43 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e lite3TransferBin
1ad44 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
1ad45 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 mt *pFromStmt, s
1ad46 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f qlite3_stmt *pTo
1ad47 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1ad48 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 From = (Vdbe*)pF
1ad49 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 romStmt;. Vdbe
1ad4a 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 *pTo = (Vdbe*)pT
1ad4b 6f 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a oStmt;. int i;.
1ad4c 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 assert( pTo->d
1ad4d 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a b==pFrom->db );.
1ad4e 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e assert( pTo->n
1ad4f 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 Var==pFrom->nVar
1ad50 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 );. sqlite3_mu
1ad51 74 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64 tex_enter(pTo->d
1ad52 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 b->mutex);. for
1ad53 28 69 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e (i=0; i<pFrom->n
1ad54 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 Var; i++){. s
1ad55 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1ad56 65 28 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c e(&pTo->aVar[i],
1ad57 20 26 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d &pFrom->aVar[i]
1ad58 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1ad59 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f _mutex_leave(pTo
1ad5a 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1ad5b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1ad5c 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1ad5d 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 ITE_OMIT_DEPRECA
1ad5e 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 TED./*.** Deprec
1ad5f 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e ated external in
1ad60 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e terface. Intern
1ad61 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 al/core SQLite c
1ad62 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 ode.** should ca
1ad63 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 ll sqlite3Transf
1ad64 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a erBindings..**.*
1ad65 2a 20 49 73 20 69 73 20 6d 69 73 75 73 65 20 74 * Is is misuse t
1ad66 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 o call this rout
1ad67 69 6e 65 20 77 69 74 68 20 73 74 61 74 65 6d 65 ine with stateme
1ad68 6e 74 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65 nts from differe
1ad69 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 nt.** database c
1ad6a 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 onnections. But
1ad6b 20 61 73 20 74 68 69 73 20 69 73 20 61 20 64 65 as this is a de
1ad6c 70 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 precated interfa
1ad6d 63 65 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e ce, we.** will n
1ad6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65 ot bother to che
1ad6f 63 6b 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64 ck for that cond
1ad70 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ition..**.** If
1ad71 74 68 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e the two statemen
1ad72 74 73 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 ts contain a dif
1ad73 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 ferent number of
1ad74 20 62 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a bindings, then.
1ad75 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 ** an SQLITE_ERR
1ad76 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OR is returned.
1ad77 20 4e 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61 Nothing else ca
1ad78 6e 20 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f n go wrong, so o
1ad79 74 68 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49 therwise.** SQLI
1ad7a 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
1ad7b 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
1ad7c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 int sqlite3_tra
1ad7d 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 nsfer_bindings(s
1ad7e 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 qlite3_stmt *pFr
1ad7f 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f omStmt, sqlite3_
1ad80 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a stmt *pToStmt){.
1ad81 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 Vdbe *pFrom =
1ad82 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 (Vdbe*)pFromStmt
1ad83 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 ;. Vdbe *pTo =
1ad84 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a (Vdbe*)pToStmt;.
1ad85 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 if( pFrom->nVa
1ad86 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a r!=pTo->nVar ){.
1ad87 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1ad88 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 E_ERROR;. }. r
1ad89 65 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 eturn sqlite3Tra
1ad8a 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 nsferBindings(pF
1ad8b 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 romStmt, pToStmt
1ad8c 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
1ad8d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 ** Return the sq
1ad8e 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 lite3* database
1ad8f 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 handle to which
1ad90 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 the prepared sta
1ad91 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 tement given.**
1ad92 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 in the argument
1ad93 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 belongs. This i
1ad94 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 s the same datab
1ad95 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 ase handle that
1ad96 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 was.** the first
1ad97 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1ad98 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1ad99 28 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 () that was used
1ad9a 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 to create.** th
1ad9b 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 e statement in t
1ad9c 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a he first place..
1ad9d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
1ad9e 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 lite3 *sqlite3_d
1ad9f 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 b_handle(sqlite3
1ada0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
1ada1 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 return pStmt ?
1ada2 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e ((Vdbe*)pStmt)->
1ada3 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a db : 0;.}../*.**
1ada4 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
1ada5 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 r to the next pr
1ada6 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1ada7 20 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 after pStmt ass
1ada8 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 ociated.** with
1ada9 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1adaa 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 ion pDb. If pSt
1adab 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 mt is NULL, retu
1adac 72 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 rn the first.**
1adad 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1adae 6e 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 nt for the datab
1adaf 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ase connection.
1adb0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
1adb1 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
1adb2 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f more..*/.SQLITE_
1adb3 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 API sqlite3_stmt
1adb4 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 *sqlite3_next_s
1adb5 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 tmt(sqlite3 *pDb
1adb6 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a , sqlite3_stmt *
1adb7 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 pStmt){. sqlite
1adb8 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 3_stmt *pNext;.
1adb9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1adba 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 nter(pDb->mutex)
1adbb 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 ;. if( pStmt==0
1adbc 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 ){. pNext =
1adbd 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 (sqlite3_stmt*)p
1adbe 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c Db->pVdbe;. }el
1adbf 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 se{. pNext =
1adc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 (sqlite3_stmt*)(
1adc1 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 (Vdbe*)pStmt)->p
1adc2 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Next;. }. sqli
1adc3 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1adc4 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 pDb->mutex);. r
1adc5 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a eturn pNext;.}..
1adc6 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1adc7 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 value of a stat
1adc8 75 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 us counter for a
1adc9 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1adca 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ent.*/.SQLITE_AP
1adcb 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 I int sqlite3_st
1adcc 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 mt_status(sqlite
1adcd 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1adce 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 nt op, int reset
1adcf 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 Flag){. Vdbe *p
1add0 56 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 Vdbe = (Vdbe*)pS
1add1 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 tmt;. int v = p
1add2 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f Vdbe->aCounter[o
1add3 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 p-1];. if( rese
1add4 74 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 tFlag ) pVdbe->a
1add5 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 Counter[op-1] =
1add6 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0;. return v;.}
1add7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1add8 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 * End of vdbeapi
1add9 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1adda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1addb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1addc 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1addd 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
1adde 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1addf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade1 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
1ade2 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
1ade3 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
1ade4 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
1ade5 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
1ade6 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
1ade7 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
1ade8 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
1ade9 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
1adea 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
1adeb 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
1adec 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1aded 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1adee 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
1adef 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
1adf0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
1adf1 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
1adf2 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
1adf3 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
1adf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adf8 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
1adf9 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
1adfa 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 e implements exe
1adfb 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 cution method of
1adfc 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c the .** Virtual
1adfd 20 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 Database Engine
1adfe 20 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 (VDBE). A sepa
1adff 72 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 rate file ("vdbe
1ae00 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c aux.c").** handl
1ae01 65 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 es housekeeping
1ae02 64 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 details such as
1ae03 63 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c creating and del
1ae04 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e eting.** VDBE in
1ae05 73 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 stances. This f
1ae06 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e ile is solely in
1ae07 74 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 terested in exec
1ae08 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 uting.** the VDB
1ae09 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a E program..**.**
1ae0a 20 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c In the external
1ae0b 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 interface, an "
1ae0c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 sqlite3_stmt*" i
1ae0d 73 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e s an opaque poin
1ae0e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 ter.** to a VDBE
1ae0f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 ..**.** The SQL
1ae10 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 parser generates
1ae11 20 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 a program which
1ae12 20 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 is then execute
1ae13 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 d by.** the VDBE
1ae14 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 to do the work
1ae15 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 of the SQL state
1ae16 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 ment. VDBE prog
1ae17 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d rams are .** sim
1ae18 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 ilar in form to
1ae19 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 assembly languag
1ae1a 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 e. The program
1ae1b 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 consists of.** a
1ae1c 20 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 linear sequence
1ae1d 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 of operations.
1ae1e 20 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 Each operation
1ae1f 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a has an opcode .*
1ae20 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 * and 5 operands
1ae21 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 . Operands P1,
1ae22 50 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 P2, and P3 are i
1ae23 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e ntegers. Operan
1ae24 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 d P4 .** is a nu
1ae25 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 ll-terminated st
1ae26 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 ring. Operand P
1ae27 35 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 5 is an unsigned
1ae28 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 character..** F
1ae29 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 ew opcodes use a
1ae2a 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a ll 5 operands..*
1ae2b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e *.** Computation
1ae2c 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f results are sto
1ae2d 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 red on a set of
1ae2e 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 registers number
1ae2f 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 ed beginning.**
1ae30 77 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 with 1 and going
1ae31 20 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d up to Vdbe.nMem
1ae32 2e 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72 . Each register
1ae33 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 can store.** ei
1ae34 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c ther an integer,
1ae35 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 a null-terminat
1ae36 65 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f ed string, a flo
1ae37 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e ating point.** n
1ae38 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 umber, or the SQ
1ae39 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 L "NULL" value.
1ae3a 20 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e An implicit con
1ae3b 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 version from one
1ae3c 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 .** type to the
1ae3d 6f 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 other occurs as
1ae3e 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a necessary..** .*
1ae3f 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f * Most of the co
1ae40 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
1ae41 69 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 is taken up by t
1ae42 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 he sqlite3VdbeEx
1ae43 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ec().** function
1ae44 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 which does the
1ae45 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 work of interpre
1ae46 74 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 ting a VDBE prog
1ae47 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 ram..** But othe
1ae48 72 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 r routines are a
1ae49 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 lso provided to
1ae4a 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 help in building
1ae4b 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d up.** a program
1ae4c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 instruction by
1ae4d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a instruction..**.
1ae4e 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 ** Various scrip
1ae4f 74 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 ts scan this sou
1ae50 72 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 rce file in orde
1ae51 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 r to generate HT
1ae52 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 ML.** documentat
1ae53 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c ion, headers fil
1ae54 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 es, or other der
1ae55 69 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 ived files. The
1ae56 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f formatting.** o
1ae57 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 f the code in th
1ae58 69 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 is file is, ther
1ae59 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 efore, important
1ae5a 2e 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d . See other com
1ae5b 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 ments.** in this
1ae5c 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c file for detail
1ae5d 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c s. If in doubt,
1ae5e 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 do not deviate
1ae5f 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a from existing.**
1ae60 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 commenting and
1ae61 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 indentation prac
1ae62 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 tices when chang
1ae63 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f ing or adding co
1ae64 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 de..**.** $Id: v
1ae65 64 62 65 2e 63 2c 76 20 31 2e 38 34 32 2e 32 2e dbe.c,v 1.842.2.
1ae66 31 20 32 30 30 39 2f 30 35 2f 31 38 20 31 36 3a 1 2009/05/18 16:
1ae67 31 34 3a 32 35 20 64 72 68 20 45 78 70 20 24 0a 14:25 drh Exp $.
1ae68 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f */../*.** The fo
1ae69 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 llowing global v
1ae6a 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 ariable is incre
1ae6b 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d mented every tim
1ae6c 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f e a cursor.** mo
1ae6d 76 65 73 2c 20 65 69 74 68 65 72 20 62 79 20 74 ves, either by t
1ae6e 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50 he OP_SeekXX, OP
1ae6f 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 _Next, or OP_Pre
1ae70 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 v opcodes. The
1ae71 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72 test.** procedur
1ae72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f es use this info
1ae73 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 rmation to make
1ae74 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 63 65 sure that indice
1ae75 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 s are.** working
1ae76 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 68 69 correctly. Thi
1ae77 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e s variable has n
1ae78 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 o function other
1ae79 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 than to.** help
1ae7a 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
1ae7b 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
1ae7c 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f the library..*/
1ae7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1ae7e 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
1ae7f 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 nt sqlite3_searc
1ae80 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e h_count = 0;.#en
1ae81 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 dif../*.** When
1ae82 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 this global vari
1ae83 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69 76 65 able is positive
1ae84 2c 20 69 74 20 67 65 74 73 20 64 65 63 72 65 6d , it gets decrem
1ae85 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 ented once befor
1ae86 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74 72 75 e.** each instru
1ae87 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56 44 42 ction in the VDB
1ae88 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68 65 73 E. When reaches
1ae89 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 zero, the u1.is
1ae8a 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 Interrupted.** f
1ae8b 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 ield of the sqli
1ae8c 74 65 33 20 73 74 72 75 63 74 75 72 65 20 69 73 te3 structure is
1ae8d 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f set in order to
1ae8e 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e simulate and in
1ae8f 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 terrupt..**.** T
1ae90 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 his facility is
1ae91 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
1ae92 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 purposes only.
1ae93 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e It does not fun
1ae94 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f ction.** in an o
1ae95 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a rdinary build..*
1ae96 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1ae97 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
1ae98 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 int sqlite3_inte
1ae99 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b rrupt_count = 0;
1ae9a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1ae9b 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 he next global v
1ae9c 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 ariable is incre
1ae9d 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79 70 65 mented each type
1ae9e 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 the OP_Sort opc
1ae9f 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 ode.** is execut
1aea0 65 64 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 ed. The test pr
1aea1 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69 ocedures use thi
1aea2 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f s information to
1aea3 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a make sure that.
1aea4 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 ** sorting is oc
1aea5 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f curring or not o
1aea6 63 63 75 72 72 69 6e 67 20 61 74 20 61 70 70 72 ccurring at appr
1aea7 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20 opriate times.
1aea8 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a This variable.*
1aea9 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f * has no functio
1aeaa 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20 n other than to
1aeab 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 help verify the
1aeac 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f correct operatio
1aead 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 n of the.** libr
1aeae 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ary..*/.#ifdef S
1aeaf 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
1aeb0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1aeb1 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3_sort_count = 0
1aeb2 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
1aeb3 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 The next global
1aeb4 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73 variable records
1aeb5 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1aeb6 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f largest MEM_Blo
1aeb7 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20 b.** or MEM_Str
1aeb8 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 73 that has been us
1aeb9 65 64 20 62 79 20 61 20 56 44 42 45 20 6f 70 63 ed by a VDBE opc
1aeba 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74 20 70 ode. The test p
1aebb 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65 rocedures.** use
1aebc 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f this informatio
1aebd 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 n to make sure t
1aebe 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f hat the zero-blo
1aebf 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a b functionality.
1aec0 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f ** is working co
1aec1 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20 rrectly. This
1aec2 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 variable has no
1aec3 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 function other t
1aec4 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 han to.** help v
1aec5 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 erify the correc
1aec6 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 t operation of t
1aec7 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 he library..*/.#
1aec8 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1aec9 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
1aeca 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f sqlite3_max_blo
1aecb 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69 bsize = 0;.stati
1aecc 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d 61 78 c void updateMax
1aecd 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29 Blobsize(Mem *p)
1aece 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 {. if( (p->flag
1aecf 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d s & (MEM_Str|MEM
1aed0 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20 70 2d _Blob))!=0 && p-
1aed1 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 >n>sqlite3_max_b
1aed2 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 73 lobsize ){. s
1aed3 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 qlite3_max_blobs
1aed4 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a ize = p->n;. }.
1aed5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1aed6 54 65 73 74 20 61 20 72 65 67 69 73 74 65 72 20 Test a register
1aed7 74 6f 20 73 65 65 20 69 66 20 69 74 20 65 78 63 to see if it exc
1aed8 65 65 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 eeds the current
1aed9 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 maximum blob si
1aeda 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 ze..** If it doe
1aedb 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 s, record the ne
1aedc 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 w maximum blob s
1aedd 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ize..*/.#if defi
1aede 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
1aedf 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
1aee0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
1aee1 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 _TEST).# define
1aee2 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1aee3 49 5a 45 28 50 29 20 20 75 70 64 61 74 65 4d 61 IZE(P) updateMa
1aee4 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c xBlobsize(P).#el
1aee5 73 65 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 se.# define UPDA
1aee6 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1aee7 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a P).#endif../*.**
1aee8 20 43 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76 Convert the giv
1aee9 65 6e 20 72 65 67 69 73 74 65 72 20 69 6e 74 6f en register into
1aeea 20 61 20 73 74 72 69 6e 67 20 69 66 20 69 74 20 a string if it
1aeeb 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 isn't one.** alr
1aeec 65 61 64 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e eady. Return non
1aeed 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f -zero if a mallo
1aeee 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 c() fails..*/.#d
1aeef 65 66 69 6e 65 20 53 74 72 69 6e 67 69 66 79 28 efine Stringify(
1aef0 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 P, enc) \. if(
1aef1 28 28 50 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d ((P)->flags&(MEM
1aef2 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d _Str|MEM_Blob))=
1aef3 3d 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 =0 && sqlite3Vdb
1aef4 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 50 2c eMemStringify(P,
1aef5 65 6e 63 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 enc)) \. { g
1aef6 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f oto no_mem; }../
1aef7 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 *.** An ephemera
1aef8 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28 l string value (
1aef9 73 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65 signified by the
1aefa 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 MEM_Ephem flag)
1aefb 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 contains.** a p
1aefc 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e 61 ointer to a dyna
1aefd 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
1aefe 64 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 73 d string where s
1aeff 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74 79 ome other entity
1af00 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 .** is responsib
1af01 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 le for deallocat
1af02 69 6e 67 20 74 68 61 74 20 73 74 72 69 6e 67 2e ing that string.
1af03 20 20 42 65 63 61 75 73 65 20 74 68 65 20 72 65 Because the re
1af04 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e gister.** does n
1af05 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 ot control the s
1af06 74 72 69 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 tring, it might
1af07 62 65 20 64 65 6c 65 74 65 64 20 77 69 74 68 6f be deleted witho
1af08 75 74 20 74 68 65 20 72 65 67 69 73 74 65 72 0a ut the register.
1af09 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a ** knowing it..*
1af0a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1af0b 65 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 e converts an ep
1af0c 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 69 hemeral string i
1af0d 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c nto a dynamicall
1af0e 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 y allocated.** s
1af0f 74 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 72 tring that the r
1af10 65 67 69 73 74 65 72 20 69 74 73 65 6c 66 20 63 egister itself c
1af11 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 ontrols. In oth
1af12 65 72 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 er words, it.**
1af13 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f converts an MEM_
1af14 45 70 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 Ephem string int
1af15 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 o an MEM_Dyn str
1af16 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ing..*/.#define
1af17 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 Deephemeralize(P
1af18 29 20 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d ) \. if( ((P)-
1af19 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d >flags&MEM_Ephem
1af1a 29 21 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26 )!=0 \. &&
1af1b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1af1c 61 6b 65 57 72 69 74 65 61 62 6c 65 28 50 29 20 akeWriteable(P)
1af1d 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d ){ goto no_mem;}
1af1e 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c ../*.** Call sql
1af1f 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
1af20 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 dBlob() on the s
1af21 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 upplied value (t
1af22 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 ype Mem*).** P i
1af23 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 f required..*/.#
1af24 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f define ExpandBlo
1af25 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 b(P) (((P)->flag
1af26 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 s&MEM_Zero)?sqli
1af27 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
1af28 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a Blob(P):0)../*.*
1af29 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 * Argument pMem
1af2a 70 6f 69 6e 74 73 20 61 74 20 61 20 72 65 67 69 points at a regi
1af2b 73 74 65 72 20 74 68 61 74 20 77 69 6c 6c 20 62 ster that will b
1af2c 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a e passed to a.**
1af2d 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 user-defined fu
1af2e 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e nction or return
1af2f 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61 ed to the user a
1af30 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
1af31 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 a query..** The
1af32 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
1af33 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68 65 'db_enc' is the
1af34 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 text encoding u
1af35 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 sed by the vdbe
1af36 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 for.** register
1af37 76 61 72 69 61 62 6c 65 73 2e 20 20 54 68 69 73 variables. This
1af38 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1af39 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 e pMem->enc and
1af3a 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 pMem->type.** va
1af3b 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 riables used by
1af3c 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 the sqlite3_valu
1af3d 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a e_*() routines..
1af3e 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74 6f 72 65 */.#define store
1af3f 54 79 70 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 TypeInfo(A,B) _s
1af40 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 29 0a toreTypeInfo(A).
1af41 73 74 61 74 69 63 20 76 6f 69 64 20 5f 73 74 6f static void _sto
1af42 72 65 54 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a reTypeInfo(Mem *
1af43 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 pMem){. int fla
1af44 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 gs = pMem->flags
1af45 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
1af46 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1af47 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1af48 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 ITE_NULL;. }.
1af49 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
1af4a 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1af4b 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1af4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d ITE_INTEGER;. }
1af4d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 . else if( flag
1af4e 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a s & MEM_Real ){.
1af4f 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1af50 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 SQLITE_FLOAT;.
1af51 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c }. else if( fl
1af52 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b ags & MEM_Str ){
1af53 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
1af54 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 = SQLITE_TEXT;.
1af55 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d }else{. pMem
1af56 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1af57 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a BLOB;. }.}../*.
1af58 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66 ** Properties of
1af59 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f opcodes. The O
1af5a 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 PFLG_INITIALIZER
1af5b 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 macro is.** cre
1af5c 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 ated by mkopcode
1af5d 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d h.awk during com
1af5e 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 pilation. Data
1af5f 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 is obtained.** f
1af60 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 rom the comments
1af61 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 following the "
1af62 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 case OP_xxxx:" s
1af63 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 tatements in.**
1af64 74 68 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a this file. .*/.
1af65 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
1af66 69 67 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64 igned char opcod
1af67 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 eProperty[] = OP
1af68 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b FLG_INITIALIZER;
1af69 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1af6a 72 75 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 rue if an opcode
1af6b 20 68 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20 has any of the
1af6c 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 OPFLG_xxx proper
1af6d 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 ties.** specifie
1af6e 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 d by mask..*/.SQ
1af6f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1af70 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f sqlite3VdbeOpco
1af71 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e deHasProperty(in
1af72 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 t opcode, int ma
1af73 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f sk){. assert( o
1af74 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 pcode>0 && opcod
1af75 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 e<(int)sizeof(op
1af76 63 6f 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b codeProperty) );
1af77 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 . return (opcod
1af78 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 eProperty[opcode
1af79 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f ]&mask)!=0;.}../
1af7a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 *.** Allocate Vd
1af7b 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 beCursor number
1af7c 69 43 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20 iCur. Return a
1af7d 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
1af7e 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 Return NULL.** i
1af7f 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 f we run out of
1af80 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 memory..*/.stati
1af81 63 20 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c c VdbeCursor *al
1af82 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20 locateCursor(.
1af83 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 Vdbe *p,
1af84 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 /* The vir
1af85 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a tual machine */.
1af86 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 int iCur,
1af87 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1af88 20 6f 66 20 74 68 65 20 6e 65 77 20 56 64 62 65 of the new Vdbe
1af89 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 Cursor */. int
1af8a 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20 nField,
1af8b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
1af8c 69 65 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62 ields in the tab
1af8d 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 le or index */.
1af8e 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 int iDb,
1af8f 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 /* When d
1af90 61 74 61 62 61 73 65 20 74 68 65 20 63 75 72 73 atabase the curs
1af91 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f or belongs to, o
1af92 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 r -1 */. int is
1af93 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 20 20 BtreeCursor
1af94 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 /* */.){. /* Fi
1af95 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 nd the memory ce
1af96 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ll that will be
1af97 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
1af98 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 e blob of memory
1af99 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 . ** required f
1af9a 6f 72 20 74 68 69 73 20 56 64 62 65 43 75 72 73 or this VdbeCurs
1af9b 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 or structure. It
1af9c 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 is convenient t
1af9d 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 o use a . ** vd
1af9e 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 be memory cell t
1af9f 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d o manage the mem
1afa0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
1afa1 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 equired for a.
1afa2 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 ** VdbeCursor st
1afa3 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 ructure for the
1afa4 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e following reason
1afa5 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a s:. **. ** *
1afa6 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f Sometimes curso
1afa7 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 r numbers are us
1afa8 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 ed for a couple
1afa9 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a of different. *
1afaa 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 * purposes i
1afab 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d n a vdbe program
1afac 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 . The different
1afad 75 73 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 uses might requi
1afae 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 re. ** diff
1afaf 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f erent sized allo
1afb0 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 cations. Memory
1afb1 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 cells provide gr
1afb2 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 owable. **
1afb3 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a allocations.. *
1afb4 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 *. ** * When
1afb5 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d using ENABLE_MEM
1afb6 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 ORY_MANAGEMENT,
1afb7 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 memory cell buff
1afb8 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 ers can. **
1afb9 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 be freed lazily
1afba 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 via the sqlite3
1afbb 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
1afbc 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a ) API. This. **
1afbd 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 minimizes t
1afbe 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c he number of mal
1afbf 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 loc calls made b
1afc0 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 y the system..
1afc1 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 **. ** Memory c
1afc2 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 ells for cursors
1afc3 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 are allocated a
1afc4 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 t the top of the
1afc5 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 address. ** sp
1afc6 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c ace. Memory cell
1afc7 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 (p->nMem) corre
1afc8 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 sponds to cursor
1afc9 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 0. Space for.
1afca 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d ** cursor 1 is m
1afcb 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 anaged by memory
1afcc 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 cell (p->nMem-1
1afcd 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d ), etc.. */. M
1afce 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 em *pMem = &p->a
1afcf 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 Mem[p->nMem-iCur
1afd0 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b ];.. int nByte;
1afd1 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1afd2 43 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 Cx = 0;. nByte
1afd3 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 = . sizeof(
1afd4 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 VdbeCursor) + .
1afd5 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72 (isBtreeCur
1afd6 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 sor?sqlite3Btree
1afd7 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 CursorSize():0)
1afd8 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c + . 2*nFiel
1afd9 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a d*sizeof(u32);..
1afda 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 assert( iCur<p
1afdb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 ->nCursor );. i
1afdc 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 f( p->apCsr[iCur
1afdd 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
1afde 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 VdbeFreeCursor(p
1afdf 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d , p->apCsr[iCur]
1afe0 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b );. p->apCsr[
1afe1 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 iCur] = 0;. }.
1afe2 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
1afe3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1afe4 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
1afe5 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 0) ){. p->apC
1afe6 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d sr[iCur] = pCx =
1afe7 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d (VdbeCursor*)pM
1afe8 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 em->z;. memse
1afe9 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 t(pMem->z, 0, nB
1afea 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 yte);. pCx->i
1afeb 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 Db = iDb;. pC
1afec 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 x->nField = nFie
1afed 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 ld;. if( nFie
1afee 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d ld ){. pCx-
1afef 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 >aType = (u32 *)
1aff0 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 &pMem->z[sizeof(
1aff1 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 VdbeCursor)];.
1aff2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 }. if( isBt
1aff3 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 reeCursor ){.
1aff4 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 pCx->pCursor
1aff5 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 = (BtCursor*).
1aff6 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a &pMem->z
1aff7 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 [sizeof(VdbeCurs
1aff8 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a or)+2*nField*siz
1aff9 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d eof(u32)];. }
1affa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 . }. return pC
1affb 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 x;.}../*.** Try
1affc 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c to convert a val
1affd 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 ue into a numeri
1affe 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e c representation
1afff 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f if we can.** do
1b000 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 so without loss
1b001 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e of information.
1b002 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1b003 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a , if the string.
1b004 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 ** looks like a
1b005 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 number, convert
1b006 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 it into a number
1b007 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f . If it does no
1b008 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 t.** look like a
1b009 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 number, leave i
1b00a 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 t alone..*/.stat
1b00b 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d ic void applyNum
1b00c 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d ericAffinity(Mem
1b00d 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 *pRec){. if( (
1b00e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d pRec->flags & (M
1b00f 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 EM_Real|MEM_Int)
1b010 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 )==0 ){. int
1b011 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c realnum;. sql
1b012 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
1b013 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 rminate(pRec);.
1b014 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c if( (pRec->fl
1b015 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 ags&MEM_Str).
1b016 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
1b017 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a IsNumber(pRec->z
1b018 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 , &realnum, pRec
1b019 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 ->enc) ){.
1b01a 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 i64 value;.
1b01b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1b01c 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c geEncoding(pRec,
1b01d 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 SQLITE_UTF8);.
1b01e 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 if( !realnu
1b01f 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 m && sqlite3Atoi
1b020 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 64(pRec->z, &val
1b021 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ue) ){. p
1b022 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 Rec->u.i = value
1b023 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 ;. MemSet
1b024 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d TypeFlag(pRec, M
1b025 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d EM_Int);. }
1b026 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
1b027 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
1b028 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 ify(pRec);.
1b029 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
1b02a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 /*.** Processing
1b02b 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 is determine by
1b02c 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 the affinity pa
1b02d 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 rameter:.**.** S
1b02e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1b02f 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 R:.** SQLITE_AFF
1b030 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 _REAL:.** SQLITE
1b031 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a _AFF_NUMERIC:.**
1b032 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 Try to conve
1b033 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e rt pRec to an in
1b034 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1b035 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 tion or a .**
1b036 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
1b037 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 representation i
1b038 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 f an integer rep
1b039 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 resentation.**
1b03a 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c is not possibl
1b03b 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 e. Note that th
1b03c 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 e integer repres
1b03d 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 entation is.**
1b03e 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 always preferr
1b03f 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 ed, even if the
1b040 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c affinity is REAL
1b041 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 , because.**
1b042 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 an integer repre
1b043 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 sentation is mor
1b044 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e e space efficien
1b045 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a t on disk..**.**
1b046 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1b047 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 :.** Convert
1b048 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 pRec to a text r
1b049 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a epresentation..*
1b04a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f *.** SQLITE_AFF_
1b04b 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f NONE:.** No-o
1b04c 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 p. pRec is unch
1b04d 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 anged..*/.static
1b04e 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e void applyAffin
1b04f 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 ity(. Mem *pRec
1b050 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 , /* Th
1b051 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 e value to apply
1b052 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a affinity to */.
1b053 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c char affinity,
1b054 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 /* The aff
1b055 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c inity to be appl
1b056 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 ied */. u8 enc
1b057 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b058 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e Use this text en
1b059 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 coding */.){. i
1b05a 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c f( affinity==SQL
1b05b 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a ITE_AFF_TEXT ){.
1b05c 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 /* Only atte
1b05d 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 mpt the conversi
1b05e 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 on to TEXT if th
1b05f 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ere is an intege
1b060 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a r or real. **
1b061 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1b062 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 (blob and NULL d
1b063 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 o not get conver
1b064 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 ted) but no stri
1b065 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 ng. ** repres
1b066 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f entation.. */
1b067 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 . if( 0==(pRe
1b068 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 c->flags&MEM_Str
1b069 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 ) && (pRec->flag
1b06a 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f s&(MEM_Real|MEM_
1b06b 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 Int)) ){. s
1b06c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
1b06d 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 ingify(pRec, enc
1b06e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 );. }. pRe
1b06f 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 c->flags &= ~(ME
1b070 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b M_Real|MEM_Int);
1b071 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 . }else if( aff
1b072 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 inity!=SQLITE_AF
1b073 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 F_NONE ){. as
1b074 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d sert( affinity==
1b075 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1b076 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d ER || affinity==
1b077 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a SQLITE_AFF_REAL.
1b078 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1b079 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
1b07a 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a _AFF_NUMERIC );.
1b07b 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 applyNumeric
1b07c 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a Affinity(pRec);.
1b07d 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c if( pRec->fl
1b07e 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
1b07f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1b080 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 dbeIntegerAffini
1b081 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a ty(pRec);. }.
1b082 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 }.}../*.** Try
1b083 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
1b084 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 type of a functi
1b085 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 on argument or a
1b086 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a result column.*
1b087 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 * into a numeric
1b088 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e representation.
1b089 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 Use either INT
1b08a 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 EGER or REAL whi
1b08b 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 chever.** is app
1b08c 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f ropriate. But o
1b08d 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 nly do the conve
1b08e 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 rsion if it is p
1b08f 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a ossible without.
1b090 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 ** loss of infor
1b091 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 mation and retur
1b092 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79 n the revised ty
1b093 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 pe of the argume
1b094 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 nt..**.** This i
1b095 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 s an EXPERIMENTA
1b096 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 L api and is sub
1b097 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f ject to change o
1b098 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 r removal..*/.SQ
1b099 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b09a 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 ite3_value_numer
1b09b 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f ic_type(sqlite3_
1b09c 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1b09d 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d Mem *pMem = (Mem
1b09e 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e *)pVal;. applyN
1b09f 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
1b0a0 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 Mem);. storeTyp
1b0a1 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a eInfo(pMem, 0);.
1b0a2 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 return pMem->t
1b0a3 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 ype;.}../*.** Ex
1b0a4 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f ported version o
1b0a5 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 f applyAffinity(
1b0a6 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b ). This one work
1b0a7 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c s on sqlite3_val
1b0a8 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 ue*, .** not the
1b0a9 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 internal Mem* t
1b0aa 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ype..*/.SQLITE_P
1b0ab 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b0ac 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 te3ValueApplyAff
1b0ad 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 inity(. sqlite3
1b0ae 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 _value *pVal, .
1b0af 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 u8 affinity, .
1b0b0 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 u8 enc.){. app
1b0b1 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 lyAffinity((Mem
1b0b2 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 *)pVal, affinity
1b0b3 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 , enc);.}..#ifde
1b0b4 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
1b0b5 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 *.** Write a nic
1b0b6 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 e string represe
1b0b7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 ntation of the c
1b0b8 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 ontents of cell
1b0b9 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 pMem.** into buf
1b0ba 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 fer zBuf, length
1b0bb 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nBuf..*/.SQLITE
1b0bc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1b0bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 lite3VdbeMemPret
1b0be 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 tyPrint(Mem *pMe
1b0bf 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a m, char *zBuf){.
1b0c0 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a char *zCsr = z
1b0c1 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 Buf;. int f = p
1b0c2 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 Mem->flags;.. s
1b0c3 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
1b0c4 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 *const encnames
1b0c5 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 [] = {"(X)", "(8
1b0c6 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 )", "(16LE)", "(
1b0c7 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 16BE)"};.. if(
1b0c8 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 f&MEM_Blob ){.
1b0c9 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 int i;. cha
1b0ca 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 r c;. if( f &
1b0cb 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 MEM_Dyn ){.
1b0cc 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 c = 'z';.
1b0cd 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1b0ce 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 EM_Static|MEM_Ep
1b0cf 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 hem))==0 );.
1b0d0 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 }else if( f & ME
1b0d1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
1b0d2 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 c = 't';.
1b0d3 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1b0d4 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d EM_Dyn|MEM_Ephem
1b0d5 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c ))==0 );. }el
1b0d6 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 se if( f & MEM_E
1b0d7 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 phem ){. c
1b0d8 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 = 'e';. ass
1b0d9 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 ert( (f & (MEM_S
1b0da 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d tatic|MEM_Dyn))=
1b0db 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b =0 );. }else{
1b0dc 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a . c = 's';.
1b0dd 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1b0de 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c e3_snprintf(100,
1b0df 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b zCsr, "%c", c);
1b0e0 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c . zCsr += sql
1b0e1 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 ite3Strlen30(zCs
1b0e2 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f r);. sqlite3_
1b0e3 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
1b0e4 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d sr, "%d[", pMem-
1b0e5 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d >n);. zCsr +=
1b0e6 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1b0e7 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 (zCsr);. for(
1b0e8 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 i=0; i<16 && i<p
1b0e9 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 Mem->n; i++){.
1b0ea 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1b0eb 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 intf(100, zCsr,
1b0ec 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d "%02X", ((int)pM
1b0ed 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 em->z[i] & 0xFF)
1b0ee 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d );. zCsr +=
1b0ef 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1b0f0 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (zCsr);. }.
1b0f1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 for(i=0; i<16
1b0f2 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b && i<pMem->n; i+
1b0f3 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a +){. char z
1b0f4 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 = pMem->z[i];.
1b0f5 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c if( z<32 ||
1b0f6 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b z>126 ) *zCsr++
1b0f7 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c = '.';. el
1b0f8 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a se *zCsr++ = z;.
1b0f9 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1b0fa 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c e3_snprintf(100,
1b0fb 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e zCsr, "]%s", en
1b0fc 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 cnames[pMem->enc
1b0fd 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 ]);. zCsr +=
1b0fe 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1b0ff 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66 zCsr);. if( f
1b100 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
1b101 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1b102 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c rintf(100, zCsr,
1b103 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e "+%dz",pMem->u.n
1b104 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 Zero);. zCs
1b105 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c r += sqlite3Strl
1b106 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 en30(zCsr);.
1b107 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c }. *zCsr = '\
1b108 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0';. }else if(
1b109 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 f & MEM_Str ){.
1b10a 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 int j, k;.
1b10b 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a zBuf[0] = ' ';.
1b10c 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f if( f & MEM_
1b10d 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 Dyn ){. zBu
1b10e 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 f[1] = 'z';.
1b10f 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 assert( (f & (
1b110 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 MEM_Static|MEM_E
1b111 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 phem))==0 );.
1b112 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1b113 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 EM_Static ){.
1b114 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 zBuf[1] = 't'
1b115 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1b116 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 (f & (MEM_Dyn|ME
1b117 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a M_Ephem))==0 );.
1b118 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 }else if( f
1b119 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 & MEM_Ephem ){.
1b11a 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 zBuf[1] = '
1b11b 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e';. assert
1b11c 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 ( (f & (MEM_Stat
1b11d 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 ic|MEM_Dyn))==0
1b11e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1b11f 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 zBuf[1] = 's
1b120 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d ';. }. k =
1b121 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 2;. sqlite3_
1b122 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a snprintf(100, &z
1b123 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d Buf[k], "%d", pM
1b124 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d em->n);. k +=
1b125 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1b126 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 (&zBuf[k]);.
1b127 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b zBuf[k++] = '[';
1b128 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c . for(j=0; j<
1b129 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 15 && j<pMem->n;
1b12a 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 j++){. u8
1b12b 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a c = pMem->z[j];.
1b12c 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 if( c>=0x2
1b12d 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 0 && c<0x7f ){.
1b12e 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d zBuf[k++]
1b12f 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = c;. }els
1b130 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b e{. zBuf[
1b131 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 k++] = '.';.
1b132 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 }. }. zB
1b133 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 uf[k++] = ']';.
1b134 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1b135 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d ntf(100,&zBuf[k]
1b136 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d , encnames[pMem-
1b137 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d >enc]);. k +=
1b138 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1b139 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 (&zBuf[k]);.
1b13a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 zBuf[k++] = 0;.
1b13b 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 }.}.#endif..#if
1b13c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1b13d 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 ./*.** Print the
1b13e 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69 value of a regi
1b13f 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67 ster for tracing
1b140 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 purposes:.*/.st
1b141 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 atic void memTra
1b142 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 cePrint(FILE *ou
1b143 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 t, Mem *p){. if
1b144 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1b145 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 _Null ){. fpr
1b146 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c intf(out, " NULL
1b147 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ");. }else if(
1b148 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d (p->flags & (MEM
1b149 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d _Int|MEM_Str))==
1b14a 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 (MEM_Int|MEM_Str
1b14b 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 ) ){. fprintf
1b14c 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 (out, " si:%lld"
1b14d 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c , p->u.i);. }el
1b14e 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 se if( p->flags
1b14f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1b150 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
1b151 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 i:%lld", p->u.i)
1b152 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
1b153 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1b154 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 l ){. fprintf
1b155 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 (out, " r:%g", p
1b156 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ->r);. }else if
1b157 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1b158 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66 _RowSet ){. f
1b159 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 printf(out, " (r
1b15a 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 owset)");. }els
1b15b 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 e{. char zBuf
1b15c 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 [200];. sqlit
1b15d 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
1b15e 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 rint(p, zBuf);.
1b15f 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1b160 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 " ");. fprint
1b161 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 f(out, "%s", zBu
1b162 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 f);. }.}.static
1b163 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 72 void registerTr
1b164 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 ace(FILE *out, i
1b165 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 nt iReg, Mem *p)
1b166 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c {. fprintf(out,
1b167 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 "REG[%d] = ", i
1b168 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 Reg);. memTrace
1b169 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 Print(out, p);.
1b16a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c fprintf(out, "\
1b16b 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 n");.}.#endif..#
1b16c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1b16d 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 UG.# define REG
1b16e 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 ISTER_TRACE(R,M)
1b16f 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67 if(p->trace)reg
1b170 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 isterTrace(p->tr
1b171 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 ace,R,M).#else.#
1b172 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 define REGISTE
1b173 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e R_TRACE(R,M).#en
1b174 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 dif...#ifdef VDB
1b175 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a E_PROFILE../* .*
1b176 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
1b177 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
1b178 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
1b179 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
1b17a 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
1b17b 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
1b17c 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
1b17d 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
1b17e 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
1b17f 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a le of vdbe.c ***
1b180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b181 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1b182 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
1b183 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
1b184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b186 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
1b187 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
1b188 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1b189 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1b18a 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1b18b 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1b18c 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1b18d 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1b18e 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1b18f 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1b190 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1b191 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1b192 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1b193 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1b194 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1b195 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1b196 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1b197 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1b198 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1b199 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b19a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b19b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b19c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b19d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1b19e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1b19f 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
1b1a0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
1b1a1 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
1b1a2 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
1b1a3 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
1b1a4 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
1b1a5 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
1b1a6 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
1b1a7 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
1b1a8 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
1b1a9 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
1b1aa 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
1b1ab 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1b1ac 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
1b1ad 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
1b1ae 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
1b1af 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
1b1b0 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
1b1b1 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
1b1b2 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
1b1b3 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
1b1b4 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
1b1b5 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
1b1b6 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
1b1b7 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
1b1b8 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
1b1b9 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
1b1ba 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
1b1bb 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
1b1bc 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
1b1bd 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
1b1be 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
1b1bf 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
1b1c0 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
1b1c1 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
1b1c2 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
1b1c3 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
1b1c4 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
1b1c5 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
1b1c6 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
1b1c7 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
1b1c8 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
1b1c9 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
1b1ca 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
1b1cb 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
1b1cc 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
1b1cd 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
1b1ce 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
1b1cf 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
1b1d0 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
1b1d1 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
1b1d2 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
1b1d3 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
1b1d4 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
1b1d5 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
1b1d6 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
1b1d7 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
1b1d8 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
1b1d9 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
1b1da 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
1b1db 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
1b1dc 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
1b1dd 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
1b1de 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1b1df 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
1b1e0 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
1b1e1 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
1b1e2 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
1b1e3 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
1b1e4 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
1b1e5 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
1b1e6 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
1b1e7 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
1b1e8 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
1b1e9 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
1b1ea 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
1b1eb 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
1b1ec 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
1b1ed 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
1b1ee 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
1b1ef 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
1b1f0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
1b1f1 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
1b1f2 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
1b1f3 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
1b1f4 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
1b1f5 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
1b1f6 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
1b1f7 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
1b1f8 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
1b1f9 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
1b1fa 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
1b1fb 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
1b1fc 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
1b1fd 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
1b1fe 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
1b1ff 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
1b200 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
1b201 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
1b202 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
1b203 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
1b204 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
1b205 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
1b206 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
1b207 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
1b208 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
1b209 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
1b20a 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
1b20b 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
1b20c 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
1b20d 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
1b20e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
1b20f 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
1b210 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
1b211 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
1b212 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
1b213 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
1b214 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
1b215 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
1b216 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
1b217 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
1b218 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
1b219 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
1b21a 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
1b21b 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
1b21c 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
1b21d 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
1b21e 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
1b21f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1b220 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
1b221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b223 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1b224 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
1b225 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
1b226 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 ft off in vdbe.c
1b227 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b228 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 ********/..#endi
1b229 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 f../*.** The CHE
1b22a 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 CK_FOR_INTERRUPT
1b22b 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 macro defined h
1b22c 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 ere looks to see
1b22d 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 if the.** sqlit
1b22e 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 e3_interrupt() r
1b22f 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 outine has been
1b230 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 called. If it h
1b231 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a as been, then.**
1b232 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 processing of t
1b233 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 he VDBE program
1b234 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a is interrupted..
1b235 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f **.** This macro
1b236 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 added to every
1b237 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 instruction that
1b238 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 does a jump in
1b239 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c order to.** impl
1b23a 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 ement a loop. T
1b23b 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f his test used to
1b23c 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e be on every sin
1b23d 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c gle instruction,
1b23e 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 .** but that mea
1b23f 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 nt we more testi
1b240 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 ng that we neede
1b241 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 d. By only test
1b242 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 ing the.** flag
1b243 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 on jump instruct
1b244 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 ions, we get a (
1b245 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 small) speed imp
1b246 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 rovement..*/.#de
1b247 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 fine CHECK_FOR_I
1b248 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 NTERRUPT \. if
1b249 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 ( db->u1.isInter
1b24a 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 rupted ) goto ab
1b24b 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 ort_due_to_inter
1b24c 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 rupt;..#ifdef SQ
1b24d 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 LITE_DEBUG.stati
1b24e 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 c int fileExists
1b24f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
1b250 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 nst char *zFile)
1b251 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b {. int res = 0;
1b252 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1b253 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 TE_OK;.#ifdef SQ
1b254 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 LITE_TEST. /* I
1b255 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 f we are current
1b256 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 ly testing IO er
1b257 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f rors, then do no
1b258 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 t call OsAccess(
1b259 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 ) to. ** test f
1b25a 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 or the presence
1b25b 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 of zFile. This i
1b25c 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f s because any IO
1b25d 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a error that. **
1b25e 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c occurs here wil
1b25f 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 l not be reporte
1b260 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 d, causing the t
1b261 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a est to fail.. *
1b262 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 /. extern int s
1b263 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
1b264 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 pending;. if( s
1b265 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
1b266 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e pending<=0 ).#en
1b267 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c dif. rc = sql
1b268 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d ite3OsAccess(db-
1b269 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 >pVfs, zFile, SQ
1b26a 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
1b26b 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 TS, &res);. ret
1b26c 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d urn (res && rc==
1b26d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 SQLITE_OK);.}.#e
1b26e 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 ndif..#ifndef ND
1b26f 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 EBUG./*.** This
1b270 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
1b271 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1b272 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 hin an assert()
1b273 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a expression. It.*
1b274 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 * checks that th
1b275 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 e sqlite3.nTrans
1b276 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 action variable
1b277 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 is correctly set
1b278 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 to.** the numbe
1b279 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 r of non-transac
1b27a 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 tion savepoints
1b27b 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
1b27c 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 .** linked list
1b27d 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c starting at sql
1b27e 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e ite3.pSavepoint.
1b27f 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a .** .** Usage:.*
1b280 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 *.** assert(
1b281 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 checkSavepointC
1b282 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 ount(db) );.*/.s
1b283 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 tatic int checkS
1b284 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 avepointCount(sq
1b285 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
1b286 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 t n = 0;. Savep
1b287 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 oint *p;. for(p
1b288 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b =db->pSavepoint;
1b289 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 p; p=p->pNext)
1b28a 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e n++;. assert( n
1b28b 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e ==(db->nSavepoin
1b28c 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 t + db->isTransa
1b28d 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 ctionSavepoint)
1b28e 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d );. return 1;.}
1b28f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
1b290 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f xecute as much o
1b291 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d f a VDBE program
1b292 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 as we can then
1b293 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 return..**.** sq
1b294 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
1b295 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c dy() must be cal
1b296 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 led before this
1b297 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 routine in order
1b298 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 to.** close the
1b299 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 program with a
1b29a 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e final OP_Halt an
1b29b 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 d to set up the
1b29c 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 callbacks.** and
1b29d 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
1b29e 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a ge pointer..**.*
1b29f 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 * Whenever a row
1b2a0 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 or result data
1b2a1 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 is available, th
1b2a2 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1b2a3 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 either.** invoke
1b2a4 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c the result call
1b2a5 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 back (if there i
1b2a6 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e s one) or return
1b2a7 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f with.** SQLITE_
1b2a8 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ROW..**.** If an
1b2a9 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
1b2aa 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 to open a locke
1b2ab 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e d database, then
1b2ac 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
1b2ad 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 will either inv
1b2ae 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c oke the busy cal
1b2af 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 lback (if there
1b2b0 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 is one) or it wi
1b2b1 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c ll.** return SQL
1b2b2 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 ITE_BUSY..**.**
1b2b3 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1b2b4 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 rs, an error mes
1b2b5 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 sage is written
1b2b6 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
1b2b7 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 ed.** from sqlit
1b2b8 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 e3_malloc() and
1b2b9 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 p->zErrMsg is ma
1b2ba 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 de to point to t
1b2bb 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 hat memory..** T
1b2bc 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 he error code is
1b2bd 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 stored in p->rc
1b2be 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
1b2bf 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
1b2c0 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 _ERROR..**.** If
1b2c1 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 the callback ev
1b2c2 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a er returns non-z
1b2c3 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 ero, then the pr
1b2c4 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 ogram exits.** i
1b2c5 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 mmediately. The
1b2c6 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 re will be no er
1b2c7 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 ror message but
1b2c8 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 the p->rc field
1b2c9 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c is.** set to SQL
1b2ca 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 ITE_ABORT and th
1b2cb 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1b2cc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1b2cd 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d ROR..**.** A mem
1b2ce 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
1b2cf 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 rror causes p->r
1b2d0 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 c to be set to S
1b2d1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 QLITE_NOMEM and
1b2d2 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
1b2d3 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 to return SQLITE
1b2d4 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 _ERROR..**.** Ot
1b2d5 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 her fatal errors
1b2d6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1b2d7 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 RROR..**.** Afte
1b2d8 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 r this routine h
1b2d9 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c as finished, sql
1b2da 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1b2db 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 () should be.**
1b2dc 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 used to clean up
1b2dd 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 the mess that w
1b2de 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a as left behind..
1b2df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1b2e1 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 eExec(. Vdbe *p
1b2e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2e3 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
1b2e4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 */.){. int pc;
1b2e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2e6 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 /* The progra
1b2e7 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f m counter */. O
1b2e8 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 p *pOp;
1b2e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1b2ea 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a rent operation *
1b2eb 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
1b2ec 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f ITE_OK; /
1b2ed 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1b2ee 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
1b2ef 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 db = p->db;
1b2f0 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1b2f1 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 e */. u8 encodi
1b2f2 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 ng = ENC(db);
1b2f3 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1b2f4 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 e encoding */.
1b2f5 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 Mem *pIn1 = 0;
1b2f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
1b2f7 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 t input operand
1b2f8 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d */. Mem *pIn2 =
1b2f9 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1b2fa 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 /* 2nd input ope
1b2fb 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 rand */. Mem *p
1b2fc 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 In3 = 0;
1b2fd 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 /* 3rd inpu
1b2fe 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d t operand */. M
1b2ff 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 em *pOut = 0;
1b300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
1b301 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1b302 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a u8 opProperty;.
1b303 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d int iCompare =
1b304 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
1b305 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f Result of last O
1b306 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 P_Compare operat
1b307 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 ion */. int *aP
1b308 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 ermute = 0;
1b309 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 /* Permutati
1b30a 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f on of columns fo
1b30b 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a r OP_Compare */.
1b30c 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1b30d 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b ILE. u64 start;
1b30e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b30f 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f /* CPU clock co
1b310 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 unt at start of
1b311 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 opcode */. int
1b312 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 origPc;
1b313 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 /* Progra
1b314 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 m counter at sta
1b315 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a rt of opcode */.
1b316 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
1b317 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 QLITE_OMIT_PROGR
1b318 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 ESS_CALLBACK. i
1b319 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 nt nProgressOps
1b31a 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 = 0; /* Opc
1b31b 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69 odes executed si
1b31c 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c nce progress cal
1b31d 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 lback. */.#endif
1b31e 0a 0a 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 .. /* Temporary
1b31f 20 73 70 61 63 65 20 69 6e 74 6f 20 77 68 69 63 space into whic
1b320 68 20 74 6f 20 75 6e 70 61 63 6b 20 61 20 72 65 h to unpack a re
1b321 63 6f 72 64 2e 20 2a 2f 0a 20 20 63 68 61 72 20 cord. */. char
1b322 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 aTempRec[ROUND8(
1b323 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 sizeof(UnpackedR
1b324 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 ecord)) + sizeof
1b325 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 0a 20 (Mem)*3 + 7];..
1b326 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1b327 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1b328 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 N ); /* sqlite3
1b329 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73 _step() verifies
1b32a 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 this */. asser
1b32b 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 t( db->magic==SQ
1b32c 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
1b32d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1b32e 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
1b32f 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d p);. if( p->rc=
1b330 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
1b331 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1b332 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 pens if a malloc
1b333 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c () inside a call
1b334 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
1b335 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20 mn_text() or.
1b336 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
1b337 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c mn_text16() fail
1b338 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f ed. */. goto
1b339 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 no_mem;. }. a
1b33a 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 ssert( p->rc==SQ
1b33b 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1b33c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b ==SQLITE_BUSY );
1b33d 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1b33e 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1b33f 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b p->explain==0 );
1b340 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 . p->pResultSet
1b341 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 = 0;. db->busy
1b342 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 Handler.nBusy =
1b343 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 0;. CHECK_FOR_I
1b344 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 NTERRUPT;. sqli
1b345 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
1b346 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c l(p);.#ifdef SQL
1b347 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 ITE_DEBUG. sqli
1b348 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
1b349 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d lloc();. if( p-
1b34a 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 >pc==0 . && ((
1b34b 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 p->db->flags & S
1b34c 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e QLITE_VdbeListin
1b34d 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 g) || fileExists
1b34e 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 (db, "vdbe_expla
1b34f 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 in")). ){. i
1b350 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 nt i;. printf
1b351 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c ("VDBE Program L
1b352 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 isting:\n");.
1b353 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1b354 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 tSql(p);. for
1b355 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
1b356 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
1b357 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 te3VdbePrintOp(s
1b358 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f tdout, i, &p->aO
1b359 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d p[i]);. }. }
1b35a 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 . if( fileExist
1b35b 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 s(db, "vdbe_trac
1b35c 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 e") ){. p->tr
1b35d 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 ace = stdout;.
1b35e 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 }. sqlite3EndBe
1b35f 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 nignMalloc();.#e
1b360 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d ndif. for(pc=p-
1b361 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f >pc; rc==SQLITE_
1b362 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 OK; pc++){. a
1b363 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 ssert( pc>=0 &&
1b364 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 pc<p->nOp );.
1b365 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1b366 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f ailed ) goto no_
1b367 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 mem;.#ifdef VDBE
1b368 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 _PROFILE. ori
1b369 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 gPc = pc;. st
1b36a 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 art = sqlite3Hwt
1b36b 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ime();.#endif.
1b36c 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
1b36d 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c pc];.. /* Onl
1b36e 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20 y allow tracing
1b36f 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 if SQLITE_DEBUG
1b370 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 is defined..
1b371 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1b372 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 _DEBUG. if( p
1b373 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 ->trace ){.
1b374 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20 if( pc==0 ){.
1b375 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 printf("VD
1b376 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61 BE Execution Tra
1b377 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 ce:\n");.
1b378 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1b379 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d tSql(p);. }
1b37a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1b37b 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61 bePrintOp(p->tra
1b37c 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20 ce, pc, pOp);.
1b37d 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 }. if( p->t
1b37e 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30 race==0 && pc==0
1b37f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1b380 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1b381 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20 oc();. if(
1b382 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 22 fileExists(db, "
1b383 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22 29 20 vdbe_sqltrace")
1b384 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1b385 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 e3VdbePrintSql(p
1b386 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1b387 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1b388 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d nMalloc();. }
1b389 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a .#endif. ..
1b38a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 /* Check to
1b38b 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 see if we need t
1b38c 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e o simulate an in
1b38d 74 65 72 72 75 70 74 2e 20 20 54 68 69 73 20 6f terrupt. This o
1b38e 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20 20 20 nly happens.
1b38f 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20 61 20 ** if we have a
1b390 73 70 65 63 69 61 6c 20 74 65 73 74 20 62 75 69 special test bui
1b391 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 ld.. */.#ifde
1b392 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1b393 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
1b394 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 terrupt_count>0
1b395 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b396 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1b397 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 --;. if( sq
1b398 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f lite3_interrupt_
1b399 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 count==0 ){.
1b39a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1b39b 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20 20 20 rrupt(db);.
1b39c 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1b39d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b39e 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 OMIT_PROGRESS_CA
1b39f 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61 LLBACK. /* Ca
1b3a0 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 ll the progress
1b3a1 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74 20 69 callback if it i
1b3a2 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61 6e 64 s configured and
1b3a3 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 75 the required nu
1b3a4 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56 mber. ** of V
1b3a5 44 42 45 20 6f 70 73 20 68 61 76 65 20 62 65 65 DBE ops have bee
1b3a6 6e 20 65 78 65 63 75 74 65 64 20 28 65 69 74 68 n executed (eith
1b3a7 65 72 20 73 69 6e 63 65 20 74 68 69 73 20 69 6e er since this in
1b3a8 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 vocation of.
1b3a9 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 ** sqlite3VdbeEx
1b3aa 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20 6c 61 ec() or since la
1b3ab 73 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f 67 st time the prog
1b3ac 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 61 ress callback wa
1b3ad 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a s called).. *
1b3ae 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 * If the progres
1b3af 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 s callback retur
1b3b0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69 ns non-zero, exi
1b3b1 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 t the virtual ma
1b3b2 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20 20 2a chine with. *
1b3b3 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64 65 20 * a return code
1b3b4 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 SQLITE_ABORT..
1b3b5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d */. if( db-
1b3b6 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a 20 20 >xProgress ){.
1b3b7 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f if( db->nPro
1b3b8 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 gressOps==nProgr
1b3b9 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20 20 20 essOps ){.
1b3ba 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20 20 20 int prc;.
1b3bb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1b3bc 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1b3bd 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1b3be 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 misuse;.
1b3bf 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65 prc =db->xProgre
1b3c0 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 ss(db->pProgress
1b3c1 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 Arg);. if
1b3c2 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1b3c3 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1b3c4 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1b3c5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 ;. if( pr
1b3c6 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 c!=0 ){.
1b3c7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e rc = SQLITE_IN
1b3c8 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20 TERRUPT;.
1b3c9 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 goto vdbe_err
1b3ca 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20 20 20 or_halt;.
1b3cb 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72 6f 67 }. nProg
1b3cc 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 ressOps = 0;.
1b3cd 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67 }. nProg
1b3ce 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d ressOps++;. }
1b3cf 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
1b3d0 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 Do common setup
1b3d1 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61 processing for a
1b3d2 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69 ny opcode that i
1b3d3 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20 s marked. **
1b3d4 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 2d 70 with the "out2-p
1b3d5 72 65 72 65 6c 65 61 73 65 22 20 74 61 67 2e 20 rerelease" tag.
1b3d6 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20 68 61 Such opcodes ha
1b3d7 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20 20 20 ve a single.
1b3d8 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63 68 20 ** output which
1b3d9 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 is specified by
1b3da 74 68 65 20 50 32 20 70 61 72 61 6d 65 74 65 72 the P2 parameter
1b3db 2e 20 20 54 68 65 20 50 32 20 72 65 67 69 73 74 . The P2 regist
1b3dc 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 69 er. ** is ini
1b3dd 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55 tialized to a NU
1b3de 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f LL.. */. o
1b3df 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f pProperty = opco
1b3e0 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e deProperty[pOp->
1b3e1 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 opcode];. if(
1b3e2 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f (opProperty & O
1b3e3 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c PFLG_OUT2_PREREL
1b3e4 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 EASE)!=0 ){.
1b3e5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1b3e6 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 2>0 );. ass
1b3e7 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d ert( pOp->p2<=p-
1b3e8 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 >nMem );. p
1b3e9 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1b3ea 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73 Op->p2];. s
1b3eb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1b3ec 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 easeExternal(pOu
1b3ed 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e t);. pOut->
1b3ee 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1b3ef 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6e 20 ;. pOut->n
1b3f0 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20 = 0;. }else.
1b3f1 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f . /* Do commo
1b3f2 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f n setup for opco
1b3f3 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 des marked with
1b3f4 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
1b3f5 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62 wing. ** comb
1b3f6 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70 inations of prop
1b3f7 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 erties.. **.
1b3f8 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
1b3f9 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 in1. **
1b3fa 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20 in1 in2.
1b3fb 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e ** in
1b3fc 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a 1 in2 out3. *
1b3fd 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 * in1
1b3fe 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a in3. **. *
1b3ff 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31 * Variables pIn1
1b400 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33 , pIn2, and pIn3
1b401 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 are made to poi
1b402 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 nt to appropriat
1b403 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 e. ** registe
1b404 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20 rs for inputs.
1b405 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f Variable pOut po
1b406 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70 ints to the outp
1b407 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20 ut register..
1b408 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50 */. if( (opP
1b409 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1b40a 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 IN1)!=0 ){.
1b40b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1b40c 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
1b40d 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e rt( pOp->p1<=p->
1b40e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 nMem );. pI
1b40f 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n1 = &p->aMem[pO
1b410 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45 p->p1];. RE
1b411 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1b412 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20 ->p1, pIn1);.
1b413 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1b414 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 ty & OPFLG_IN2)!
1b415 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 =0 ){. as
1b416 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1b417 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
1b418 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e t( pOp->p2<=p->n
1b419 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 Mem );. p
1b41a 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In2 = &p->aMem[p
1b41b 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 Op->p2];.
1b41c 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1b41d 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a pOp->p2, pIn2);.
1b41e 20 20 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 if( (opP
1b41f 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1b420 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 OUT3)!=0 ){.
1b421 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1b422 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 p->p3>0 );.
1b423 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1b424 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1b425 0a 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 20 . pOut
1b426 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1b427 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 p3];. }.
1b428 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 }else if( (
1b429 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1b42a 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 LG_IN3)!=0 ){.
1b42b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1b42c 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 p->p3>0 );.
1b42d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1b42e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1b42f 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 pIn3 = &p
1b430 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1b431 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 . REGISTE
1b432 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1b433 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a pIn3);. }.
1b434 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f }else if( (o
1b435 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1b436 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 G_IN2)!=0 ){.
1b437 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1b438 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 p2>0 );. as
1b439 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 sert( pOp->p2<=p
1b43a 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
1b43b 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn2 = &p->aMem[
1b43c 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 pOp->p2];.
1b43d 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1b43e 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 Op->p2, pIn2);.
1b43f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 }else if( (op
1b440 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1b441 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN3)!=0 ){.
1b442 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1b443 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 3>0 );. ass
1b444 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d ert( pOp->p3<=p-
1b445 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 >nMem );. p
1b446 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In3 = &p->aMem[p
1b447 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52 Op->p3];. R
1b448 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1b449 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 p->p3, pIn3);.
1b44a 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 }.. switch(
1b44b 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a pOp->opcode ){.
1b44c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b44d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b44e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b44f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1b451 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 ** What follows
1b452 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 69 is a massive swi
1b453 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68 tch statement wh
1b454 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69 6d ere each case im
1b455 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 plements a.** se
1b456 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 69 parate instructi
1b457 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 on in the virtua
1b458 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77 l machine. If w
1b459 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 e follow the usu
1b45a 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f al.** indentatio
1b45b 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 n conventions, e
1b45c 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20 ach case should
1b45d 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36 be indented by 6
1b45e 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a spaces. But.**
1b45f 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f that is a lot o
1b460 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20 6f f wasted space o
1b461 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69 n the left margi
1b462 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20 n. So the code
1b463 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 within.** the sw
1b464 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 itch statement w
1b465 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63 ill break with c
1b466 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 onvention and be
1b467 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f flush-left. Ano
1b468 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d ther.** big comm
1b469 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 ent (similar to
1b46a 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d this one) will m
1b46b 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e ark the point in
1b46c 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a the code where.
1b46d 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e ** we transition
1b46e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 back to normal
1b46f 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a indentation..**.
1b470 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e ** The formattin
1b471 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 69 g of each case i
1b472 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 s important. Th
1b473 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 e makefile for S
1b474 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 QLite.** generat
1b475 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22 es two C files "
1b476 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 opcodes.h" and "
1b477 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63 opcodes.c" by sc
1b478 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 anning this.** f
1b479 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ile looking for
1b47a 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e lines that begin
1b47b 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22 with "case OP_"
1b47c 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 . The opcodes.h
1b47d 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 files.** will b
1b47e 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64 e filled with #d
1b47f 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 65 efines that give
1b480 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20 unique integer
1b481 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a values to each.*
1b482 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 * opcode and the
1b483 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 opcodes.c file
1b484 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 is filled with a
1b485 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e n array of strin
1b486 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 gs where.** each
1b487 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 string is the s
1b488 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 ymbolic name for
1b489 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1b48a 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 ng opcode. If t
1b48b 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65 he.** case state
1b48c 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 ment is followed
1b48d 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 by a comment of
1b48e 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 the form "/# sa
1b48f 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a me as ... #/".**
1b490 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 that comment is
1b491 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
1b492 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 ne the particula
1b493 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f r value of the o
1b494 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 pcode..**.** Oth
1b495 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 er keywords in t
1b496 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 he comment that
1b497 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73 follows each cas
1b498 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a e are used to.**
1b499 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f construct the O
1b49a 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 PFLG_INITIALIZER
1b49b 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74 value that init
1b49c 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 ializes opcodePr
1b49d 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 operty[]..** Key
1b49e 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 words include: i
1b49f 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 n1, in2, in3, ou
1b4a0 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f t2_prerelease, o
1b4a1 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a ut2, out3. See.
1b4a2 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 ** the mkopcodeh
1b4a3 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 .awk script for
1b4a4 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
1b4a5 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f mation..**.** Do
1b4a6 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 cumentation abou
1b4a7 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 t VDBE opcodes i
1b4a8 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 s generated by s
1b4a9 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c canning this fil
1b4aa 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f e.** for lines o
1b4ab 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 f that contain "
1b4ac 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 Opcode:". That
1b4ad 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 line and all sub
1b4ae 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 sequent.** comme
1b4af 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 nt lines are use
1b4b0 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 d in the generat
1b4b1 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 ion of the opcod
1b4b2 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 e.html documenta
1b4b3 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a tion.** file..**
1b4b4 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a .** SUMMARY:.**.
1b4b5 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e ** Formattin
1b4b6 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 g is important t
1b4b7 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20 73 o scripts that s
1b4b8 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a can this file..*
1b4b9 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 * Do not dev
1b4ba 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f iate from the fo
1b4bb 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 rmatting style c
1b4bc 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e urrently in use.
1b4bd 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1b4be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4c2 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 */../* Opcode:
1b4c3 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a Goto * P2 * * *.
1b4c4 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 **.** An uncondi
1b4c5 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 tional jump to a
1b4c6 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 ddress P2..** Th
1b4c7 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1b4c8 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c on executed will
1b4c9 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 be .** the one
1b4ca 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d at index P2 from
1b4cb 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
1b4cc 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d f.** the program
1b4cd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 ..*/.case OP_Got
1b4ce 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 o: {
1b4cf 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 /* jump */. CH
1b4d0 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 ECK_FOR_INTERRUP
1b4d1 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 T;. pc = pOp->p
1b4d2 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 2 - 1;. break;.
1b4d3 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 }../* Opcode: G
1b4d4 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a osub P1 P2 * * *
1b4d5 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 .**.** Write the
1b4d6 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 current address
1b4d7 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 onto register P
1b4d8 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 1.** and then ju
1b4d9 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 mp to address P2
1b4da 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 ..*/.case OP_Gos
1b4db 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ub: {
1b4dc 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73 /* jump */. as
1b4dd 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
1b4de 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1b4df 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p1<=p->nMem );
1b4e0 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d . pIn1 = &p->aM
1b4e1 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 em[pOp->p1];. a
1b4e2 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c ssert( (pIn1->fl
1b4e3 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d ags & MEM_Dyn)==
1b4e4 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 0 );. pIn1->fla
1b4e5 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b4e6 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a pIn1->u.i = pc;.
1b4e7 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1b4e8 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b (pOp->p1, pIn1);
1b4e9 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 . pc = pOp->p2
1b4ea 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a - 1;. break;.}.
1b4eb 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 ./* Opcode: Ret
1b4ec 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a urn P1 * * * *.*
1b4ed 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 *.** Jump to the
1b4ee 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1b4ef 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64 72 n after the addr
1b4f0 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 ess in register
1b4f1 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 P1..*/.case OP_R
1b4f2 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20 eturn: {
1b4f3 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 /* in1 */. a
1b4f4 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1b4f5 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a gs & MEM_Int );.
1b4f6 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 pc = (int)pIn1
1b4f7 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a ->u.i;. break;.
1b4f8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 }../* Opcode: Y
1b4f9 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a ield P1 * * * *.
1b4fa 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 **.** Swap the p
1b4fb 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 rogram counter w
1b4fc 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e ith the value in
1b4fd 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f register P1..*/
1b4fe 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 .case OP_Yield:
1b4ff 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1b500 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 in1 */. int pcD
1b501 65 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 28 est;. assert( (
1b502 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1b503 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 M_Dyn)==0 );. p
1b504 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d In1->flags = MEM
1b505 5f 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d _Int;. pcDest =
1b506 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b (int)pIn1->u.i;
1b507 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 . pIn1->u.i = p
1b508 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 c;. REGISTER_TR
1b509 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e ACE(pOp->p1, pIn
1b50a 31 29 3b 0a 20 20 70 63 20 3d 20 70 63 44 65 73 1);. pc = pcDes
1b50b 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f t;. break;.}../
1b50c 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 * Opcode: HaltI
1b50d 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20 fNull P1 P2 P3
1b50e 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b P4 *.**.** Check
1b50f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1b510 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69 gister P3. If i
1b511 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 s is NULL then H
1b512 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 alt using.** par
1b513 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61 ameter P1, P2, a
1b514 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73 nd P4 as if this
1b515 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73 were a Halt ins
1b516 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 truction. If th
1b517 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 e.** value in re
1b518 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74 gister P3 is not
1b519 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 NULL, then this
1b51a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1b51b 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f -op..*/.case OP_
1b51c 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 HaltIfNull: {
1b51d 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 /* in3 */. i
1b51e 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 f( (pIn3->flags
1b51f 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1b520 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c break;. /* Fal
1b521 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f l through into O
1b522 50 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 P_Halt */.}../*
1b523 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 Opcode: Halt P1
1b524 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1b525 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74 65 6c Exit immediatel
1b526 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 y. All open cur
1b527 73 6f 72 73 2c 20 65 74 63 20 61 72 65 20 63 6c sors, etc are cl
1b528 6f 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 osed.** automati
1b529 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 cally..**.** P1
1b52a 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f is the result co
1b52b 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 de returned by s
1b52c 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73 qlite3_exec(), s
1b52d 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0a qlite3_reset(),.
1b52e 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 ** or sqlite3_fi
1b52f 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 nalize(). For a
1b530 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 normal halt, th
1b531 69 73 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c is should be SQL
1b532 49 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 ITE_OK (0)..** F
1b533 6f 72 20 65 72 72 6f 72 73 2c 20 69 74 20 63 61 or errors, it ca
1b534 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 n be some other
1b535 76 61 6c 75 65 2e 20 20 49 66 20 50 31 21 3d 30 value. If P1!=0
1b536 20 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 then P2 will de
1b537 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 termine.** wheth
1b538 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c er or not to rol
1b539 6c 62 61 63 6b 20 74 68 65 20 63 75 72 72 65 6e lback the curren
1b53a 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 t transaction.
1b53b 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a Do not rollback.
1b53c 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 ** if P2==OE_Fai
1b53d 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 l. Do the rollba
1b53e 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c ck if P2==OE_Rol
1b53f 6c 62 61 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f lback. If P2==O
1b540 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e E_Abort,.** then
1b541 20 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 back out all ch
1b542 61 6e 67 65 73 20 74 68 61 74 20 68 61 76 65 20 anges that have
1b543 6f 63 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 occurred during
1b544 74 68 69 73 20 65 78 65 63 75 74 69 6f 6e 20 6f this execution o
1b545 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 f the.** VDBE, b
1b546 75 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 ut do not rollba
1b547 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ck the transacti
1b548 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 on. .**.** If P4
1b549 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 is not null the
1b54a 6e 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 n it is an error
1b54b 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e message string.
1b54c 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
1b54d 61 6e 20 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 an implied "Halt
1b54e 20 30 20 30 20 30 22 20 69 6e 73 74 72 75 63 74 0 0 0" instruct
1b54f 69 6f 6e 20 69 6e 73 65 72 74 65 64 20 61 74 20 ion inserted at
1b550 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 0a the very end of.
1b551 2a 2a 20 65 76 65 72 79 20 70 72 6f 67 72 61 6d ** every program
1b552 2e 20 20 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 . So a jump pas
1b553 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 t the last instr
1b554 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 uction of the pr
1b555 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 ogram.** is the
1b556 73 61 6d 65 20 61 73 20 65 78 65 63 75 74 69 6e same as executin
1b557 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 g Halt..*/.case
1b558 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e OP_Halt: {. p->
1b559 72 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 rc = pOp->p1;.
1b55a 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d p->pc = pc;. p-
1b55b 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 >errorAction = p
1b55c 4f 70 2d 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f Op->p2;. if( pO
1b55d 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 p->p4.z ){. s
1b55e 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1b55f 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1b560 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a "%s", pOp->p4.z
1b561 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 );. }. rc = sq
1b562 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 lite3VdbeHalt(p)
1b563 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
1b564 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 SQLITE_BUSY || r
1b565 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
1b566 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1b567 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e _BUSY ){. p->
1b568 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = rc = SQLITE
1b569 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a _BUSY;. }else{.
1b56a 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f rc = p->rc ?
1b56b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 SQLITE_ERROR :
1b56c 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
1b56d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 . goto vdbe_ret
1b56e 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 urn;.}../* Opcod
1b56f 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 e: Integer P1 P2
1b570 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1b571 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
1b572 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 value P1 is writ
1b573 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ten into registe
1b574 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1b575 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 _Integer: {
1b576 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1b577 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 elease */. pOut
1b578 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1b579 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d t;. pOut->u.i =
1b57a 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 pOp->p1;. brea
1b57b 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1b57c 20 49 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 Int64 * P2 * P4
1b57d 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 *.**.** P4 is a
1b57e 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 pointer to a 64
1b57f 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c -bit integer val
1b580 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 ue..** Write tha
1b581 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 t value into reg
1b582 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 ister P2..*/.cas
1b583 65 20 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 e OP_Int64: {
1b584 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1b585 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1b586 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
1b587 70 49 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 pI64!=0 );. pOu
1b588 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 t->flags = MEM_I
1b589 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 nt;. pOut->u.i
1b58a 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b = *pOp->p4.pI64;
1b58b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1b58c 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 Opcode: Real * P
1b58d 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 2 * P4 *.**.** P
1b58e 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1b58f 6f 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 o a 64-bit float
1b590 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e ing point value.
1b591 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 .** Write that v
1b592 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 alue into regist
1b593 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P2..*/.case O
1b594 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 P_Real: {
1b595 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1b596 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 TK_FLOAT, out2-p
1b597 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 rerelease */. p
1b598 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1b599 5f 52 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 _Real;. assert(
1b59a 20 21 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a !sqlite3IsNaN(*
1b59b 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 pOp->p4.pReal) )
1b59c 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 ;. pOut->r = *p
1b59d 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 Op->p4.pReal;.
1b59e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1b59f 6f 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 ode: String8 * P
1b5a0 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 2 * P4 *.**.** P
1b5a1 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 4 points to a nu
1b5a2 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 l terminated UTF
1b5a3 2d 38 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 -8 string. This
1b5a4 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 opcode is transf
1b5a5 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 ormed .** into a
1b5a6 6e 20 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f n OP_String befo
1b5a7 72 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65 re it is execute
1b5a8 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 d for the first
1b5a9 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 time..*/.case OP
1b5aa 5f 53 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 _String8: {
1b5ab 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1b5ac 4b 5f 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 K_STRING, out2-p
1b5ad 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 rerelease */. a
1b5ae 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a ssert( pOp->p4.z
1b5af 21 3d 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 !=0 );. pOp->op
1b5b0 63 6f 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 code = OP_String
1b5b1 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 ;. pOp->p1 = sq
1b5b2 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f lite3Strlen30(pO
1b5b3 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 p->p4.z);..#ifnd
1b5b4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1b5b5 54 46 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 TF16. if( encod
1b5b6 69 6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 ing!=SQLITE_UTF8
1b5b7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1b5b8 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 dbeMemSetStr(pOu
1b5b9 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 t, pOp->p4.z, -1
1b5ba 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
1b5bb 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
1b5bc 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
1b5bd 21 3d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 !=sqlite3VdbeCha
1b5be 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 ngeEncoding(pOut
1b5bf 2c 20 65 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f , encoding) ) go
1b5c0 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 to no_mem;. i
1b5c1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1b5c2 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
1b5c3 57 72 69 74 65 61 62 6c 65 28 70 4f 75 74 29 20 Writeable(pOut)
1b5c4 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1b5c5 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 pOut->zMalloc
1b5c6 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e = 0;. pOut->
1b5c7 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 flags |= MEM_Sta
1b5c8 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 tic;. pOut->f
1b5c9 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e lags &= ~MEM_Dyn
1b5ca 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 ;. if( pOp->p
1b5cb 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 4type==P4_DYNAMI
1b5cc 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 C ){. sqlit
1b5cd 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 e3DbFree(db, pOp
1b5ce 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 ->p4.z);. }.
1b5cf 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d pOp->p4type =
1b5d0 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 P4_DYNAMIC;.
1b5d1 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 pOp->p4.z = pOu
1b5d2 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 t->z;. pOp->p
1b5d3 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 20 1 = pOut->n;.
1b5d4 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d if( pOp->p1>db-
1b5d5 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1b5d6 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1b5d7 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 goto too_b
1b5d8 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 55 50 ig;. }. UP
1b5d9 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1b5da 45 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 E(pOut);. bre
1b5db 61 6b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ak;. }.#endif.
1b5dc 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d if( pOp->p1>db-
1b5dd 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1b5de 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1b5df 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1b5e0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 ;. }. /* Fall
1b5e1 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1b5e2 65 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 ext case, OP_Str
1b5e3 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f ing */.}. ./* O
1b5e4 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 20 50 31 pcode: String P1
1b5e5 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1b5e6 20 54 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75 The string valu
1b5e7 65 20 50 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 e P4 of length P
1b5e8 31 20 28 62 79 74 65 73 29 20 69 73 20 73 74 6f 1 (bytes) is sto
1b5e9 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1b5ea 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 P2..*/.case OP_S
1b5eb 74 72 69 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 tring: {
1b5ec 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1b5ed 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 ease */. assert
1b5ee 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 ( pOp->p4.z!=0 )
1b5ef 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 ;. pOut->flags
1b5f0 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 = MEM_Str|MEM_St
1b5f1 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 atic|MEM_Term;.
1b5f2 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e pOut->z = pOp->
1b5f3 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 p4.z;. pOut->n
1b5f4 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 = pOp->p1;. pOu
1b5f5 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1b5f6 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1b5f7 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1b5f8 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1b5f9 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 pcode: Null * P2
1b5fa 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 * * *.**.** Wri
1b5fb 74 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 te a NULL into r
1b5fc 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 egister P2..*/.c
1b5fd 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 ase OP_Null: {
1b5fe 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1b5ff 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1b600 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f break;.}.../* O
1b601 70 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 pcode: Blob P1 P
1b602 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 2 * P4.**.** P4
1b603 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 points to a blob
1b604 20 6f 66 20 64 61 74 61 20 50 31 20 62 79 74 65 of data P1 byte
1b605 73 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 s long. Store t
1b606 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 his.** blob in r
1b607 65 67 69 73 74 65 72 20 50 32 2e 20 54 68 69 73 egister P2. This
1b608 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1b609 6e 6f 74 20 63 6f 64 65 64 20 64 69 72 65 63 74 not coded direct
1b60a 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d ly.** by the com
1b60b 70 69 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 piler. Instead,
1b60c 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 the compiler lay
1b60d 65 72 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 er specifies.**
1b60e 61 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 an OP_HexBlob op
1b60f 63 6f 64 65 2c 20 77 69 74 68 20 74 68 65 20 68 code, with the h
1b610 65 78 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 ex string repres
1b611 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 entation of.** t
1b612 68 65 20 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 he blob as P4. T
1b613 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 his opcode is tr
1b614 61 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 ansformed to an
1b615 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 OP_Blob.** the f
1b616 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 irst time it is
1b617 65 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 executed..*/.cas
1b618 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 e OP_Blob: {
1b619 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1b61a 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1b61b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1b61c 3e 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 >p1 <= SQLITE_MA
1b61d 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 X_LENGTH );. sq
1b61e 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1b61f 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 tr(pOut, pOp->p4
1b620 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 .z, pOp->p1, 0,
1b621 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 0);. pOut->enc
1b622 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 = encoding;. UP
1b623 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1b624 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
1b625 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1b626 56 61 72 69 61 62 6c 65 20 50 31 20 50 32 20 50 Variable P1 P2 P
1b627 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 3 P4 *.**.** Tra
1b628 6e 73 66 65 72 20 74 68 65 20 76 61 6c 75 65 73 nsfer the values
1b629 20 6f 66 20 62 6f 75 6e 64 20 70 61 72 61 6d 65 of bound parame
1b62a 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31 ters P1..P1+P3-1
1b62b 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 0a into registers.
1b62c 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a ** P2..P2+P3-1..
1b62d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 **.** If the par
1b62e 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 64 2c ameter is named,
1b62f 20 74 68 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 then its name a
1b630 70 70 65 61 72 73 20 69 6e 20 50 34 20 61 6e 64 ppears in P4 and
1b631 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 P3==1..** The P
1b632 34 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 4 value is used
1b633 62 79 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f by sqlite3_bind_
1b634 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 parameter_name()
1b635 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72 ..*/.case OP_Var
1b636 69 61 62 6c 65 3a 20 7b 0a 20 20 69 6e 74 20 6a iable: {. int j
1b637 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a = pOp->p1 - 1;.
1b638 20 20 69 6e 74 20 6b 20 3d 20 70 4f 70 2d 3e 70 int k = pOp->p
1b639 32 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 2;. Mem *pVar;.
1b63a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 int n = pOp->p
1b63b 33 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d 3;. assert( j>=
1b63c 30 20 26 26 20 6a 2b 6e 3c 3d 70 2d 3e 6e 56 61 0 && j+n<=p->nVa
1b63d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b r );. assert( k
1b63e 3e 3d 31 20 26 26 20 6b 2b 6e 2d 31 3c 3d 70 2d >=1 && k+n-1<=p-
1b63f 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 >nMem );. asser
1b640 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 t( pOp->p4.z==0
1b641 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b || pOp->p3==1 );
1b642 0a 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e .. while( n-- >
1b643 20 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20 3d 0 ){. pVar =
1b644 20 26 70 2d 3e 61 56 61 72 5b 6a 2b 2b 5d 3b 0a &p->aVar[j++];.
1b645 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1b646 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 56 61 dbeMemTooBig(pVa
1b647 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f r) ){. goto
1b648 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a too_big;. }.
1b649 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 pOut = &p->a
1b64a 4d 65 6d 5b 6b 2b 2b 5d 3b 0a 20 20 20 20 73 71 Mem[k++];. sq
1b64b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1b64c 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 aseExternal(pOut
1b64d 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 );. pOut->fla
1b64e 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1b64f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b650 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 mShallowCopy(pOu
1b651 74 2c 20 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61 t, pVar, MEM_Sta
1b652 74 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54 45 tic);. UPDATE
1b653 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
1b654 75 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b ut);. }. break
1b655 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1b656 4d 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20 Move P1 P2 P3 *
1b657 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 *.**.** Move the
1b658 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1b659 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 ter P1..P1+P3-1
1b65a 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 over into.** reg
1b65b 69 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 isters P2..P2+P3
1b65c 2d 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50 -1. Registers P
1b65d 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 1..P1+P1-1 are.*
1b65e 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 * left holding a
1b65f 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e NULL. It is an
1b660 20 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73 error for regis
1b661 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 ter ranges.** P1
1b662 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 ..P1+P3-1 and P2
1b663 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 ..P2+P3-1 to ove
1b664 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 rlap..*/.case OP
1b665 5f 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20 _Move: {. char
1b666 2a 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 *zMalloc;. int
1b667 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 n = pOp->p3;. i
1b668 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b nt p1 = pOp->p1;
1b669 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d . int p2 = pOp-
1b66a 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e >p2;. assert( n
1b66b 3e 30 20 26 26 20 70 31 3e 30 20 26 26 20 70 32 >0 && p1>0 && p2
1b66c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
1b66d 70 31 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e p1+n<=p2 || p2+n
1b66e 3c 3d 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20 <=p1 );.. pIn1
1b66f 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a = &p->aMem[p1];.
1b670 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1b671 6d 5b 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 m[p2];. while(
1b672 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 n-- ){. asser
1b673 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65 t( pOut<=&p->aMe
1b674 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 m[p->nMem] );.
1b675 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d assert( pIn1<=
1b676 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d &p->aMem[p->nMem
1b677 5d 20 29 3b 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63 ] );. zMalloc
1b678 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 = pOut->zMalloc
1b679 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c ;. pOut->zMal
1b67a 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c loc = 0;. sql
1b67b 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1b67c 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 pOut, pIn1);.
1b67d 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d pIn1->zMalloc =
1b67e 20 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 zMalloc;. RE
1b67f 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b GISTER_TRACE(p2+
1b680 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49 +, pOut);. pI
1b681 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b n1++;. pOut++
1b682 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1b683 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70 ../* Opcode: Cop
1b684 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a y P1 P2 * * *.**
1b685 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 .** Make a copy
1b686 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 of register P1 i
1b687 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1b688 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1b689 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 ruction makes a
1b68a 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65 deep copy of the
1b68b 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69 value. A dupli
1b68c 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20 cate.** is made
1b68d 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 of any string or
1b68e 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 blob constant.
1b68f 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f See also OP_SCo
1b690 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 py..*/.case OP_C
1b691 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 opy: {
1b692 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 /* in1 */. a
1b693 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1b694 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1b695 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
1b696 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
1b697 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1b698 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 assert( pOut!=pI
1b699 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 n1 );. sqlite3V
1b69a 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
1b69b 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 y(pOut, pIn1, ME
1b69c 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 M_Ephem);. Deep
1b69d 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 hemeralize(pOut)
1b69e 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1b69f 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 CE(pOp->p2, pOut
1b6a0 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1b6a1 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 * Opcode: SCopy
1b6a2 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1b6a3 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 * Make a shallow
1b6a4 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 copy of registe
1b6a5 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 r P1 into regist
1b6a6 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 er P2..**.** Thi
1b6a7 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 s instruction ma
1b6a8 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f kes a shallow co
1b6a9 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e py of the value.
1b6aa 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1b6ab 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 * is a string or
1b6ac 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 blob, then the
1b6ad 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 copy is only a p
1b6ae 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a ointer to the.**
1b6af 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 original and he
1b6b0 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 nce if the origi
1b6b1 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 nal changes so w
1b6b2 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a ill the copy..**
1b6b3 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f Worse, if the o
1b6b4 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c riginal is deall
1b6b5 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 ocated, the copy
1b6b6 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 becomes invalid
1b6b7 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 ..** Thus the pr
1b6b8 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 ogram must guara
1b6b9 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 ntee that the or
1b6ba 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 iginal will not
1b6bb 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 change.** during
1b6bc 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
1b6bd 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 the copy. Use
1b6be 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 OP_Copy to make
1b6bf 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f a complete.** co
1b6c0 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 py..*/.case OP_S
1b6c1 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 Copy: {
1b6c2 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 /* in1 */. R
1b6c3 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1b6c4 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 p->p1, pIn1);.
1b6c5 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e assert( pOp->p2>
1b6c6 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1b6c7 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1b6c8 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e );. pOut = &p->
1b6c9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1b6ca 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 assert( pOut!=p
1b6cb 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 In1 );. sqlite3
1b6cc 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1b6cd 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d py(pOut, pIn1, M
1b6ce 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 EM_Ephem);. REG
1b6cf 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1b6d0 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 >p2, pOut);. br
1b6d1 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b6d2 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 e: ResultRow P1
1b6d3 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 P2 * * *.**.** T
1b6d4 68 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20 he registers P1
1b6d5 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 through P1+P2-1
1b6d6 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 contain a single
1b6d7 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c row of.** resul
1b6d8 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 ts. This opcode
1b6d9 63 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 causes the sqlit
1b6da 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 e3_step() call t
1b6db 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 o terminate.** w
1b6dc 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f ith an SQLITE_RO
1b6dd 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e W return code an
1b6de 64 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65 d it sets up the
1b6df 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a sqlite3_stmt.**
1b6e0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 structure to pr
1b6e1 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 ovide access to
1b6e2 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 the top P1 value
1b6e3 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a s as the result.
1b6e4 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 ** row..*/.case
1b6e5 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a OP_ResultRow: {.
1b6e6 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 Mem *pMem;. i
1b6e7 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
1b6e8 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 p->nResColumn==p
1b6e9 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 Op->p2 );. asse
1b6ea 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1b6eb 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1b6ec 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e p1+pOp->p2<=p->n
1b6ed 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 Mem+1 );.. /* I
1b6ee 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 f the SQLITE_Cou
1b6ef 6e 74 52 6f 77 73 20 66 6c 61 67 20 69 73 20 73 ntRows flag is s
1b6f0 65 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c et in sqlite3.fl
1b6f1 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 0a ags mask, then .
1b6f2 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d 65 ** DML stateme
1b6f3 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 nts invoke this
1b6f4 6f 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e opcode to return
1b6f5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
1b6f6 6f 77 73 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 ows . ** modifi
1b6f7 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
1b6f8 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 This is the only
1b6f9 20 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74 way that a VM t
1b6fa 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61 hat. ** opens a
1b6fb 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1b6fc 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b action may invok
1b6fd 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20 e this opcode..
1b6fe 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65 **. ** In case
1b6ff 20 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20 this is such a
1b700 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65 statement, close
1b701 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74 any statement t
1b702 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 ransaction. **
1b703 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56 opened by this V
1b704 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 M before returni
1b705 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 ng control to th
1b706 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 e user. This is
1b707 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74 to. ** ensure t
1b708 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 hat statement-tr
1b709 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61 ansactions are a
1b70a 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f lways nested, no
1b70b 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20 t overlapping..
1b70c 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20 ** If the open
1b70d 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 statement-transa
1b70e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f ction is not clo
1b70f 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74 sed here, then t
1b710 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79 he user. ** may
1b711 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d step another VM
1b712 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20 that opens its
1b713 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 own statement tr
1b714 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a ansaction. This.
1b715 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f ** may lead to
1b716 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61 overlapping sta
1b717 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1b718 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ons.. */. asse
1b719 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e rt( p->iStatemen
1b71a 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 t==0 || db->flag
1b71b 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f s&SQLITE_CountRo
1b71c 77 73 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 ws );. if( SQLI
1b71d 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
1b71e 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 ite3VdbeCloseSta
1b71f 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 4f tement(p, SAVEPO
1b720 49 4e 54 5f 52 45 4c 45 41 53 45 29 29 20 29 7b INT_RELEASE)) ){
1b721 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1b722 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 . /* Invalidate
1b723 20 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 all ephemeral c
1b724 75 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 ursor row caches
1b725 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 */. p->cacheCt
1b726 72 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 r = (p->cacheCtr
1b727 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d + 2)|1;.. /* M
1b728 61 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 ake sure the res
1b729 75 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 ults of the curr
1b72a 65 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 ent row are \000
1b72b 20 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a terminated. **
1b72c 20 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 and have an ass
1b72d 69 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 igned type. The
1b72e 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d results are de-
1b72f 65 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 ephemeralized as
1b730 0a 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 . ** as side ef
1b731 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65 fect.. */. pMe
1b732 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 m = p->pResultSe
1b733 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1b734 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 ->p1];. for(i=0
1b735 3b 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b ; i<pOp->p2; i++
1b736 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1b737 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
1b738 65 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20 e(&pMem[i]);.
1b739 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26 storeTypeInfo(&
1b73a 70 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e pMem[i], encodin
1b73b 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 g);. REGISTER
1b73c 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69 _TRACE(pOp->p1+i
1b73d 2c 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d , &pMem[i]);. }
1b73e 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1b73f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e cFailed ) goto n
1b740 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 o_mem;.. /* Ret
1b741 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 urn SQLITE_ROW.
1b742 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 */. p->pc = pc
1b743 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c + 1;. rc = SQL
1b744 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 ITE_ROW;. goto
1b745 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a vdbe_return;.}..
1b746 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 /* Opcode: Conca
1b747 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
1b748 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 *.** Add the tex
1b749 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 t in register P1
1b74a 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 onto the end of
1b74b 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 the text in.**
1b74c 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 register P2 and
1b74d 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1b74e 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1b74f 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 .** If either th
1b750 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 e P1 or P2 text
1b751 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 are NULL then st
1b752 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a ore NULL in P3..
1b753 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 **.** P3 = P2
1b754 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 || P1.**.** It i
1b755 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 s illegal for P1
1b756 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 and P3 to be th
1b757 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e e same register.
1b758 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 Sometimes,.** i
1b759 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 f P3 is the same
1b75a 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c register as P2,
1b75b 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
1b75c 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 ion is able.** t
1b75d 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 o avoid a memcpy
1b75e 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 ()..*/.case OP_C
1b75f 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 oncat: {
1b760 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b761 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e _CONCAT, in1, in
1b762 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36 34 2, out3 */. i64
1b763 20 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65 72 nByte;.. asser
1b764 74 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b t( pIn1!=pOut );
1b765 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1b766 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 ags | pIn2->flag
1b767 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b s) & MEM_Null ){
1b768 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1b769 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 MemSetNull(pOut)
1b76a 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d ;. break;. }
1b76b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 . ExpandBlob(pI
1b76c 6e 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 n1);. Stringify
1b76d 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 (pIn1, encoding)
1b76e 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 ;. ExpandBlob(p
1b76f 49 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 In2);. Stringif
1b770 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 y(pIn2, encoding
1b771 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49 6e );. nByte = pIn
1b772 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 1->n + pIn2->n;.
1b773 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e if( nByte>db->
1b774 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1b775 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
1b776 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1b777 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 . }. MemSetTyp
1b778 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1b779 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Str);. if( sqli
1b77a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
1b77b 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79 74 65 2b Out, (int)nByte+
1b77c 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 2, pOut==pIn2) )
1b77d 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1b77e 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 m;. }. if( pOu
1b77f 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d t!=pIn2 ){. m
1b780 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 emcpy(pOut->z, p
1b781 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 In2->z, pIn2->n)
1b782 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 ;. }. memcpy(&
1b783 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d pOut->z[pIn2->n]
1b784 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d , pIn1->z, pIn1-
1b785 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e >n);. pOut->z[n
1b786 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 Byte] = 0;. pOu
1b787 74 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d 20 3d 20 t->z[nByte+1] =
1b788 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 0;. pOut->flags
1b789 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
1b78a 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e pOut->n = (int)n
1b78b 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e Byte;. pOut->en
1b78c 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 c = encoding;.
1b78d 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1b78e 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1b78f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b790 3a 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a : Add P1 P2 P3 *
1b791 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 *.**.** Add the
1b792 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b793 65 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c er P1 to the val
1b794 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1b795 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 2.** and store t
1b796 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1b797 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 ister P3..** If
1b798 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1b799 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1b79a 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 is NULL..*/./*
1b79b 4f 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 Opcode: Multiply
1b79c 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1b79d 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 .**.** Multiply
1b79e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1b79f 69 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 ister P1 by the
1b7a0 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1b7a1 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 r P2.** and stor
1b7a2 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 e the result in
1b7a3 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 register P3..**
1b7a4 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1b7a5 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
1b7a6 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
1b7a7 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 /* Opcode: Subtr
1b7a8 61 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a act P1 P2 P3 * *
1b7a9 0a 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 .**.** Subtract
1b7aa 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1b7ab 69 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 ister P1 from th
1b7ac 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b7ad 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 ter P2.** and st
1b7ae 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1b7af 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1b7b0 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1b7b1 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1b7b2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1b7b3 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 /./* Opcode: Div
1b7b4 69 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a ide P1 P2 P3 * *
1b7b5 0a 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 .**.** Divide th
1b7b6 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b7b7 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 ter P1 by the va
1b7b8 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1b7b9 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 P2.** and store
1b7ba 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1b7bb 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 74 gister P3. If t
1b7bc 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1b7bd 73 74 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65 ster P2.** is ze
1b7be 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ro, then the res
1b7bf 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 ult is NULL..**
1b7c0 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1b7c1 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
1b7c2 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
1b7c3 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 /* Opcode: Remai
1b7c4 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a 20 nder P1 P2 P3 *
1b7c5 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 *.**.** Compute
1b7c6 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 the remainder af
1b7c7 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 69 ter integer divi
1b7c8 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 sion of the valu
1b7c9 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 e in.** register
1b7ca 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 P1 by the value
1b7cb 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1b7cc 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 and store the re
1b7cd 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20 sult in P3. .**
1b7ce 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 If the value in
1b7cf 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20 7a register P2 is z
1b7d0 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69 ero the result i
1b7d1 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 s NULL..** If ei
1b7d2 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1b7d3 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1b7d4 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 is NULL..*/.cas
1b7d5 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 e OP_Add:
1b7d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1b7d7 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20 ame as TK_PLUS,
1b7d8 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1b7d9 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 /.case OP_Subtra
1b7da 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 ct:
1b7db 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d /* same as TK_M
1b7dc 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 INUS, in1, in2,
1b7dd 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f out3 */.case OP_
1b7de 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20 Multiply:
1b7df 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1b7e0 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 s TK_STAR, in1,
1b7e1 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1b7e2 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 e OP_Divide:
1b7e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1b7e4 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c ame as TK_SLASH,
1b7e5 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1b7e6 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 */.case OP_Remai
1b7e7 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 nder: {
1b7e8 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1b7e9 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f REM, in1, in2, o
1b7ea 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ut3 */. int fla
1b7eb 67 73 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 gs;. applyNumer
1b7ec 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29 icAffinity(pIn1)
1b7ed 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 ;. applyNumeric
1b7ee 41 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a Affinity(pIn2);.
1b7ef 20 20 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e flags = pIn1->
1b7f0 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c flags | pIn2->fl
1b7f1 61 67 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 ags;. if( (flag
1b7f2 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 s & MEM_Null)!=0
1b7f3 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 ) goto arithmet
1b7f4 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1b7f5 6c 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e l;. if( (pIn1->
1b7f6 66 6c 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c flags & pIn2->fl
1b7f7 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d ags & MEM_Int)==
1b7f8 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69 MEM_Int ){. i
1b7f9 36 34 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 64 a, b;. a =
1b7fa 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 pIn1->u.i;.
1b7fb 62 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 b = pIn2->u.i;.
1b7fc 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e switch( pOp->
1b7fd 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 opcode ){.
1b7fe 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 case OP_Add:
1b7ff 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 b += a;
1b800 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1b801 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 case OP_Subtract
1b802 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 : b -= a;
1b803 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1b804 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 case OP_Multiply
1b805 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 : b *= a;
1b806 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1b807 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 case OP_Divide:
1b808 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d {. if( a=
1b809 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d =0 ) goto arithm
1b80a 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1b80b 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ull;. /*
1b80c 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 Dividing the lar
1b80d 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 gest possible ne
1b80e 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e gative 64-bit in
1b80f 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 teger (1<<63) by
1b810 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 . ** -1
1b811 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
1b812 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 er too large to
1b813 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 store in a 64-bi
1b814 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a t data-type. On.
1b815 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 ** some
1b816 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 architectures, t
1b817 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f he value overflo
1b818 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f ws to (1<<63). O
1b819 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 n others,.
1b81a 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 ** a SIGFPE is
1b81b 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c issued. The fol
1b81c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
1b81d 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 normalizes this
1b81e 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 . ** beha
1b81f 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c vior so that all
1b820 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62 architectures b
1b821 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 ehave as if inte
1b822 67 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ger . **
1b823 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 overflow occurre
1b824 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 d.. */.
1b825 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 if( a==-1
1b826 26 26 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 && b==SMALLEST_I
1b827 4e 54 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20 NT64 ) a = 1;.
1b828 20 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 b /= a;.
1b829 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1b82a 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 }. defau
1b82b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 lt: {. if
1b82c 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 ( a==0 ) goto ar
1b82d 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f ithmetic_result_
1b82e 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 is_null;.
1b82f 20 69 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d if( a==-1 ) a =
1b830 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d 1;. b %=
1b831 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 a;. brea
1b832 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1b833 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1b834 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 b;. MemSetTy
1b835 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1b836 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a _Int);. }else{.
1b837 20 20 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b double a, b;
1b838 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 . a = sqlite3
1b839 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 VdbeRealValue(pI
1b83a 6e 31 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c n1);. b = sql
1b83b 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
1b83c 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 e(pIn2);. swi
1b83d 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 tch( pOp->opcode
1b83e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ){. case O
1b83f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 P_Add: b
1b840 20 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 += a; bre
1b841 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b842 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 P_Subtract: b
1b843 20 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 -= a; bre
1b844 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b845 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 P_Multiply: b
1b846 20 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 *= a; bre
1b847 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b848 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 P_Divide: {.
1b849 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 /* (double)0
1b84a 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
1b84b 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
1b84c 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
1b84d 20 20 20 20 20 69 66 28 20 61 3d 3d 28 64 6f 75 if( a==(dou
1b84e 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69 ble)0 ) goto ari
1b84f 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1b850 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1b851 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 b /= a;.
1b852 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1b853 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
1b854 20 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d i64 ia =
1b855 20 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 (i64)a;.
1b856 20 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 i64 ib = (i64)b
1b857 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61 ;. if( ia
1b858 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 ==0 ) goto arith
1b859 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1b85a 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 null;. if
1b85b 28 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 ( ia==-1 ) ia =
1b85c 31 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28 1;. b = (
1b85d 64 6f 75 62 6c 65 29 28 69 62 20 25 20 69 61 29 double)(ib % ia)
1b85e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1b85f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1b860 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
1b861 4e 61 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20 NaN(b) ){.
1b862 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1b863 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1b864 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e }. pOut->
1b865 72 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 r = b;. MemSe
1b866 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1b867 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 MEM_Real);. i
1b868 66 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f f( (flags & MEM_
1b869 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Real)==0 ){.
1b86a 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 sqlite3VdbeInt
1b86b 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 egerAffinity(pOu
1b86c 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 t);. }. }.
1b86d 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 break;..arithmet
1b86e 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1b86f 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 l:. sqlite3Vdbe
1b870 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 MemSetNull(pOut)
1b871 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1b872 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 Opcode: CollSeq
1b873 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 * * P4.**.** P4
1b874 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1b875 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
1b876 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 t. If the next c
1b877 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 all to a user fu
1b878 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 nction.** or agg
1b879 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c regate calls sql
1b87a 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
1b87b 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 eq(), this colla
1b87c 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 tion sequence wi
1b87d 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 ll.** be returne
1b87e 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 d. This is used
1b87f 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 by the built-in
1b880 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 min(), max() and
1b881 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e nullif().** fun
1b882 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ctions..**.** Th
1b883 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 e interface used
1b884 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e by the implemen
1b885 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 tation of the af
1b886 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e orementioned fun
1b887 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 ctions.** to ret
1b888 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 rieve the collat
1b889 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 ion sequence set
1b88a 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 by this opcode
1b88b 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
1b88c 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e .** publicly, on
1b88d 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 ly to user funct
1b88e 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 ions defined in
1b88f 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 func.c..*/.case
1b890 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 OP_CollSeq: {.
1b891 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1b892 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 ype==P4_COLLSEQ
1b893 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1b894 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 * Opcode: Functi
1b895 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 on P1 P2 P3 P4 P
1b896 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 5.**.** Invoke a
1b897 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 user function (
1b898 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1b899 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 to a Function st
1b89a 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 ructure that.**
1b89b 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 defines the func
1b89c 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 tion) with P5 ar
1b89d 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 guments taken fr
1b89e 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 om register P2 a
1b89f 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 nd.** successors
1b8a0 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 . The result of
1b8a1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 the function is
1b8a2 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1b8a3 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 ter P3..** Regis
1b8a4 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 ter P3 must not
1b8a5 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 be one of the fu
1b8a6 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a nction inputs..*
1b8a7 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d *.** P1 is a 32-
1b8a8 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 bit bitmask indi
1b8a9 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f cating whether o
1b8aa 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d r not each argum
1b8ab 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 ent to the .** f
1b8ac 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 unction was dete
1b8ad 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e rmined to be con
1b8ae 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 stant at compile
1b8af 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 time. If the fi
1b8b0 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
1b8b1 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 was constant the
1b8b2 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 n bit 0 of P1 is
1b8b3 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 set. This is us
1b8b4 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a ed to determine.
1b8b5 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 ** whether meta
1b8b6 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
1b8b7 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 with a user func
1b8b8 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 tion argument us
1b8b9 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 ing the.** sqlit
1b8ba 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 e3_set_auxdata()
1b8bb 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 API may be safe
1b8bc 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 ly retained unti
1b8bd 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e l the next.** in
1b8be 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 vocation of this
1b8bf 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 opcode..**.** S
1b8c0 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 ee also: AggStep
1b8c1 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f and AggFinal.*/
1b8c2 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f .case OP_Functio
1b8c3 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 n: {. int i;.
1b8c4 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c Mem *pArg;. sql
1b8c5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 ite3_context ctx
1b8c6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ;. sqlite3_valu
1b8c7 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 e **apVal;. int
1b8c8 20 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 n = pOp->p5;..
1b8c9 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 apVal = p->apAr
1b8ca 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 g;. assert( apV
1b8cb 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 al || n==0 );..
1b8cc 20 61 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c assert( n==0 ||
1b8cd 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 (pOp->p2>0 && p
1b8ce 4f 70 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 Op->p2+n<=p->nMe
1b8cf 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 m+1) );. assert
1b8d0 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 ( pOp->p3<pOp->p
1b8d1 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 2 || pOp->p3>=pO
1b8d2 70 2d 3e 70 32 2b 6e 20 29 3b 0a 20 20 70 41 72 p->p2+n );. pAr
1b8d3 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 g = &p->aMem[pOp
1b8d4 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 ->p2];. for(i=0
1b8d5 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 41 72 67 ; i<n; i++, pArg
1b8d6 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 ++){. apVal[i
1b8d7 5d 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 73 74 ] = pArg;. st
1b8d8 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 41 72 67 oreTypeInfo(pArg
1b8d9 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 , encoding);.
1b8da 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1b8db 70 4f 70 2d 3e 70 32 2c 20 70 41 72 67 29 3b 0a pOp->p2, pArg);.
1b8dc 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
1b8dd 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 Op->p4type==P4_F
1b8de 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 UNCDEF || pOp->p
1b8df 34 74 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4type==P4_VDBEFU
1b8e0 4e 43 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d NC );. if( pOp-
1b8e1 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 >p4type==P4_FUNC
1b8e2 44 45 46 20 29 7b 0a 20 20 20 20 63 74 78 2e 70 DEF ){. ctx.p
1b8e3 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Func = pOp->p4.p
1b8e4 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 56 Func;. ctx.pV
1b8e5 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d dbeFunc = 0;. }
1b8e6 65 6c 73 65 7b 0a 20 20 20 20 63 74 78 2e 70 56 else{. ctx.pV
1b8e7 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 dbeFunc = (VdbeF
1b8e8 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 unc*)pOp->p4.pVd
1b8e9 62 65 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e beFunc;. ctx.
1b8ea 70 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64 62 pFunc = ctx.pVdb
1b8eb 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 eFunc->pFunc;.
1b8ec 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 }.. assert( pOp
1b8ed 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1b8ee 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1b8ef 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1b8f0 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 63 74 78 2e pOp->p3];. ctx.
1b8f1 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 s.flags = MEM_Nu
1b8f2 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 3d ll;. ctx.s.db =
1b8f3 20 64 62 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 db;. ctx.s.xDe
1b8f4 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a l = 0;. ctx.s.z
1b8f5 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f Malloc = 0;.. /
1b8f6 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c * The output cel
1b8f7 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 l may already ha
1b8f8 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f ve a buffer allo
1b8f9 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a cated. Move. **
1b8fa 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
1b8fb 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 ctx.s so in case
1b8fc 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 the user-functi
1b8fd 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 on can use. **
1b8fe 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f the already allo
1b8ff 63 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 cated buffer ins
1b900 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 tead of allocati
1b901 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 ng a new one..
1b902 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1b903 4d 65 6d 4d 6f 76 65 28 26 63 74 78 2e 73 2c 20 MemMove(&ctx.s,
1b904 70 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 pOut);. MemSetT
1b905 79 70 65 46 6c 61 67 28 26 63 74 78 2e 73 2c 20 ypeFlag(&ctx.s,
1b906 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74 MEM_Null);.. ct
1b907 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 x.isError = 0;.
1b908 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e if( ctx.pFunc->
1b909 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
1b90a 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a UNC_NEEDCOLL ){.
1b90b 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e assert( pOp>
1b90c 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 p->aOp );. as
1b90d 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 sert( pOp[-1].p4
1b90e 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 type==P4_COLLSEQ
1b90f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1b910 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d pOp[-1].opcode==
1b911 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 OP_CollSeq );.
1b912 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f ctx.pColl = pO
1b913 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a p[-1].p4.pColl;.
1b914 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1b915 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1b916 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1b917 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 63 to_misuse;. (*c
1b918 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 tx.pFunc->xFunc)
1b919 28 26 63 74 78 2c 20 6e 2c 20 61 70 56 61 6c 29 (&ctx, n, apVal)
1b91a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1b91b 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 afetyOn(db) ){.
1b91c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b91d 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 mRelease(&ctx.s)
1b91e 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 ;. goto abort
1b91f 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1b920 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1b921 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1b922 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 /* Even though
1b923 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 a malloc() has
1b924 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c failed, the impl
1b925 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1b926 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75 e. ** user fu
1b927 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 nction may have
1b928 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 called an sqlite
1b929 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66 3_result_XXX() f
1b92a 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 unction. ** t
1b92b 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 o return a value
1b92c 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 . The following
1b92d 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e call releases an
1b92e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20 y resources.
1b92f 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ** associated wi
1b930 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e th such a value.
1b931 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e . **. ** N
1b932 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65 ote: Maybe MemRe
1b933 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62 lease() should b
1b934 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69 e called if sqli
1b935 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20 te3SafetyOn().
1b936 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20 ** fails also
1b937 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61 (the if(...) sta
1b938 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42 tement above). B
1b939 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65 ut if people are
1b93a 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67 . ** misusing
1b93b 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61 sqlite, they ha
1b93c 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65 ve bigger proble
1b93d 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64 ms than a leaked
1b93e 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 value.. */.
1b93f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b940 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 mRelease(&ctx.s)
1b941 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ;. goto no_me
1b942 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 m;. }.. /* If
1b943 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 any auxiliary da
1b944 74 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 ta functions hav
1b945 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 e been called by
1b946 20 74 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 this user funct
1b947 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 ion,. ** immedi
1b948 61 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 ately call the d
1b949 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e estructor for an
1b94a 79 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c y non-static val
1b94b 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ues.. */. if(
1b94c 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b ctx.pVdbeFunc ){
1b94d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1b94e 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 63 74 DeleteAuxData(ct
1b94f 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 x.pVdbeFunc, pOp
1b950 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e ->p1);. pOp->
1b951 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 63 p4.pVdbeFunc = c
1b952 74 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 tx.pVdbeFunc;.
1b953 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1b954 50 34 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d P4_VDBEFUNC;. }
1b955 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75 .. /* If the fu
1b956 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 nction returned
1b957 61 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 an error, throw
1b958 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a an exception */.
1b959 20 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f if( ctx.isErro
1b95a 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
1b95b 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1b95c 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1b95d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1b95e 65 78 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20 ext(&ctx.s));.
1b95f 20 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 rc = ctx.isErr
1b960 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f or;. }.. /* Co
1b961 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 py the result of
1b962 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e the function in
1b963 74 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 2a to register P3 *
1b964 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 /. sqlite3VdbeC
1b965 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63 hangeEncoding(&c
1b966 74 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b tx.s, encoding);
1b967 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b968 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 26 63 74 78 mMove(pOut, &ctx
1b969 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 .s);. if( sqlit
1b96a 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
1b96b 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 pOut) ){. got
1b96c 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 o too_big;. }.
1b96d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1b96e 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a pOp->p3, pOut);.
1b96f 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1b970 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1b971 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b972 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 de: BitAnd P1 P2
1b973 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 P3 * *.**.** Ta
1b974 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 ke the bit-wise
1b975 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 AND of the value
1b976 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 s in register P1
1b977 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 and P2 and.** s
1b978 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1b979 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1b97a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 ** If either inp
1b97b 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 ut is NULL, the
1b97c 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a result is NULL..
1b97d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 */./* Opcode: Bi
1b97e 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a tOr P1 P2 P3 * *
1b97f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1b980 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 bit-wise OR of t
1b981 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 he values in reg
1b982 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 ister P1 and P2
1b983 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 and.** store the
1b984 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 result in regis
1b985 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 ter P3..** If ei
1b986 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 ther input is NU
1b987 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 LL, the result i
1b988 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 s NULL..*/./* Op
1b989 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 code: ShiftLeft
1b98a 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1b98b 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 ** Shift the int
1b98c 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 eger value in re
1b98d 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 gister P2 to the
1b98e 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 left by the.**
1b98f 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 number of bits s
1b990 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 pecified by the
1b991 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 integer in regis
1b992 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 er P1..** Store
1b993 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1b994 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 gister P3..** If
1b995 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 either input is
1b996 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1b997 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a t is NULL..*/./*
1b998 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 Opcode: ShiftRi
1b999 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a ght P1 P2 P3 * *
1b99a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 .**.** Shift the
1b99b 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 integer value i
1b99c 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f n register P2 to
1b99d 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 the right by th
1b99e 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 e.** number of b
1b99f 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 its specified by
1b9a0 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 the integer in
1b9a1 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 register P1..**
1b9a2 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
1b9a3 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1b9a4 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
1b9a5 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
1b9a6 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1b9a7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 .*/.case OP_BitA
1b9a8 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 nd:
1b9a9 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1b9aa 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 K_BITAND, in1, i
1b9ab 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1b9ac 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 OP_BitOr:
1b9ad 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1b9ae 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c ame as TK_BITOR,
1b9af 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1b9b0 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 */.case OP_Shift
1b9b1 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 Left:
1b9b2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1b9b3 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e _LSHIFT, in1, in
1b9b4 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
1b9b5 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b OP_ShiftRight: {
1b9b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b9b7 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c me as TK_RSHIFT,
1b9b8 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1b9b9 2a 2f 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a */. i64 a, b;..
1b9ba 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1b9bb 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 gs | pIn2->flags
1b9bc 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a ) & MEM_Null ){.
1b9bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b9be 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1b9bf 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1b9c0 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 a = sqlite3Vdb
1b9c1 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b eIntValue(pIn2);
1b9c2 0a 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64 . b = sqlite3Vd
1b9c3 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 beIntValue(pIn1)
1b9c4 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d ;. switch( pOp-
1b9c5 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 >opcode ){. c
1b9c6 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 ase OP_BitAnd:
1b9c7 20 20 20 20 61 20 26 3d 20 62 3b 20 20 20 20 20 a &= b;
1b9c8 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1b9c9 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1b9ca 61 20 7c 3d 20 62 3b 20 20 20 20 20 62 72 65 61 a |= b; brea
1b9cb 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53 k;. case OP_S
1b9cc 68 69 66 74 4c 65 66 74 3a 20 20 20 61 20 3c 3c hiftLeft: a <<
1b9cd 3d 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 = b; break;.
1b9ce 20 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 default: ass
1b9cf 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 ert( pOp->opcode
1b9d0 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 ==OP_ShiftRight
1b9d1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
1b9d2 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3e 3e a >>
1b9d3 3d 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 = b; break;.
1b9d4 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d }. pOut->u.i =
1b9d5 20 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 a;. MemSetType
1b9d6 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1b9d7 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a nt);. break;.}.
1b9d8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 ./* Opcode: AddI
1b9d9 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a mm P1 P2 * * *.
1b9da 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 ** .** Add the c
1b9db 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68 onstant P2 to th
1b9dc 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b9dd 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 ter P1..** The r
1b9de 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 esult is always
1b9df 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a an integer..**.*
1b9e0 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 * To force any r
1b9e1 65 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e egister to be an
1b9e2 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61 integer, just a
1b9e3 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 dd 0..*/.case OP
1b9e4 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 _AddImm: {
1b9e5 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1b9e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b9e7 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 Integerify(pIn1)
1b9e8 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d ;. pIn1->u.i +=
1b9e9 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61 pOp->p2;. brea
1b9ea 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1b9eb 20 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32 MustBeInt P1 P2
1b9ec 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f * * *.** .** Fo
1b9ed 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1b9ee 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1b9ef 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 be an integer.
1b9f0 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
1b9f1 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 in P1 is not an
1b9f2 69 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e integer and cann
1b9f3 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 ot be converted
1b9f4 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a into an integer.
1b9f5 2a 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20 ** without data
1b9f6 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 loss, then jump
1b9f7 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 immediately to P
1b9f8 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2, or if P2==0.*
1b9f9 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 * raise an SQLIT
1b9fa 45 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65 70 E_MISMATCH excep
1b9fb 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1b9fc 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 _MustBeInt: {
1b9fd 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1b9fe 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 , in1 */. apply
1b9ff 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 Affinity(pIn1, S
1ba00 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
1ba01 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 C, encoding);.
1ba02 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1ba03 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 & MEM_Int)==0 )
1ba04 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 {. if( pOp->p
1ba05 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 2==0 ){. rc
1ba06 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 = SQLITE_MISMAT
1ba07 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 CH;. goto a
1ba08 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1ba09 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 r;. }else{.
1ba0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1ba0b 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 - 1;. }. }e
1ba0c 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 lse{. MemSetT
1ba0d 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 ypeFlag(pIn1, ME
1ba0e 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 M_Int);. }. br
1ba0f 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ba10 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 79 20 e: RealAffinity
1ba11 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1ba12 20 49 66 20 72 65 67 69 73 74 65 72 20 50 31 20 If register P1
1ba13 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 holds an integer
1ba14 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1ba15 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a real value..**.
1ba16 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1ba17 73 20 75 73 65 64 20 77 68 65 6e 20 65 78 74 72 s used when extr
1ba18 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 acting informati
1ba19 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e on from a column
1ba1a 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 that.** has REA
1ba1b 4c 20 61 66 66 69 6e 69 74 79 2e 20 20 53 75 63 L affinity. Suc
1ba1c 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 h column values
1ba1d 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 73 74 6f may still be sto
1ba1e 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 red as.** intege
1ba1f 72 73 2c 20 66 6f 72 20 73 70 61 63 65 20 65 66 rs, for space ef
1ba20 66 69 63 69 65 6e 63 79 2c 20 62 75 74 20 61 66 ficiency, but af
1ba21 74 65 72 20 65 78 74 72 61 63 74 69 6f 6e 20 77 ter extraction w
1ba22 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 e want them.** t
1ba23 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 o have only a re
1ba24 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 al value..*/.cas
1ba25 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 e OP_RealAffinit
1ba26 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 y: {
1ba27 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1ba28 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1ba29 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1ba2a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1ba2b 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a mRealify(pIn1);.
1ba2c 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ba2d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ba2e 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f MIT_CAST./* Opco
1ba2f 64 65 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 de: ToText P1 *
1ba30 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 * * *.**.** Forc
1ba31 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1ba32 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 egister P1 to be
1ba33 20 74 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 text..** If the
1ba34 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 value is numeri
1ba35 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f c, convert it to
1ba36 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 a string using
1ba37 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e the.** equivalen
1ba38 74 20 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 t of printf().
1ba39 42 6c 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 Blob values are
1ba3a 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a unchanged and.**
1ba3b 20 61 72 65 20 61 66 74 65 72 77 61 72 64 73 20 are afterwards
1ba3c 73 69 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 simply interpret
1ba3d 65 64 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a ed as text..**.*
1ba3e 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1ba3f 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1ba40 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1ba41 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1ba42 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 .*/.case OP_ToTe
1ba43 78 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 xt: {
1ba44 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ba45 73 20 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e s TK_TO_TEXT, in
1ba46 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 1 */. if( pIn1-
1ba47 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1ba48 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 l ) break;. ass
1ba49 65 72 74 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d ert( MEM_Str==(M
1ba4a 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 EM_Blob>>3) );.
1ba4b 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 pIn1->flags |=
1ba4c 28 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d (pIn1->flags&MEM
1ba4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 _Blob)>>3;. app
1ba4e 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c lyAffinity(pIn1,
1ba4f 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1ba50 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 , encoding);. r
1ba51 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 c = ExpandBlob(p
1ba52 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 In1);. assert(
1ba53 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1ba54 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c M_Str || db->mal
1ba55 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 locFailed );. p
1ba56 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 In1->flags &= ~(
1ba57 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
1ba58 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 |MEM_Blob|MEM_Ze
1ba59 72 6f 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 ro);. UPDATE_MA
1ba5a 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 X_BLOBSIZE(pIn1)
1ba5b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ba5c 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 Opcode: ToBlob
1ba5d 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1ba5e 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1ba5f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1ba60 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a to be a BLOB..**
1ba61 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 If the value is
1ba62 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 numeric, conver
1ba63 74 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 t it to a string
1ba64 20 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e first..** Strin
1ba65 67 73 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65 gs are simply re
1ba66 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 62 interpreted as b
1ba67 6c 6f 62 73 20 77 69 74 68 20 6e 6f 20 63 68 61 lobs with no cha
1ba68 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e nge.** to the un
1ba69 64 65 72 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a derlying data..*
1ba6a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 *.** A NULL valu
1ba6b 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
1ba6c 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
1ba6d 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 . It remains NU
1ba6e 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 LL..*/.case OP_T
1ba6f 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 oBlob: {
1ba70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1ba71 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c e as TK_TO_BLOB,
1ba72 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 in1 */. if( pI
1ba73 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1ba74 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 Null ) break;.
1ba75 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1ba76 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 & MEM_Blob)==0
1ba77 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 ){. applyAffi
1ba78 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 nity(pIn1, SQLIT
1ba79 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f E_AFF_TEXT, enco
1ba7a 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72 ding);. asser
1ba7b 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 t( pIn1->flags &
1ba7c 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e MEM_Str || db->
1ba7d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1ba7e 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1ba7f 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f ag(pIn1, MEM_Blo
1ba80 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 b);. }else{.
1ba81 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 pIn1->flags &=
1ba82 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e ~(MEM_TypeMask&~
1ba83 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 MEM_Blob);. }.
1ba84 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1ba85 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 SIZE(pIn1);. br
1ba86 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ba87 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 e: ToNumeric P1
1ba88 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1ba89 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1ba8a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1ba8b 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68 be numeric (eith
1ba8c 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 er an.** integer
1ba8d 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 or a floating-p
1ba8e 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a oint number.).**
1ba8f 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 If the value is
1ba90 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 text or blob, t
1ba91 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 ry to convert it
1ba92 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65 to an using the
1ba93 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
1ba94 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 f atoi() or atof
1ba95 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 () and store 0 i
1ba96 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 f no such conver
1ba97 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 sion .** is poss
1ba98 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 ible..**.** A NU
1ba99 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1ba9a 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1ba9b 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1ba9c 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1ba9d 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a se OP_ToNumeric:
1ba9e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1ba9f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1baa0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e K_TO_NUMERIC, in
1baa1 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 1 */. if( (pIn1
1baa2 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e ->flags & (MEM_N
1baa3 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f ull|MEM_Int|MEM_
1baa4 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 Real))==0 ){.
1baa5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
1baa6 75 6d 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 umerify(pIn1);.
1baa7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1baa8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1baa9 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 MIT_CAST */../*
1baaa 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31 Opcode: ToInt P1
1baab 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 * * * *.**.** F
1baac 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 orce the value i
1baad 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 65 n register P1 be
1baae 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 an integer. If
1baaf 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 .** The value is
1bab0 20 63 75 72 72 65 6e 74 6c 79 20 61 20 72 65 61 currently a rea
1bab1 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70 20 69 l number, drop i
1bab2 74 73 20 66 72 61 63 74 69 6f 6e 61 6c 20 70 61 ts fractional pa
1bab3 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 rt..** If the va
1bab4 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 lue is text or b
1bab5 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 lob, try to conv
1bab6 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 ert it to an int
1bab7 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a eger using the.*
1bab8 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 * equivalent of
1bab9 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 atoi() and store
1baba 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 0 if no such co
1babb 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 nversion is poss
1babc 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 ible..**.** A NU
1babd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1babe 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1babf 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1bac0 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1bac1 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 se OP_ToInt: {
1bac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bac3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
1bac4 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 _INT, in1 */. i
1bac5 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1bac6 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1bac7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1bac8 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1bac9 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 In1);. }. brea
1baca 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 k;.}..#ifndef SQ
1bacb 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f LITE_OMIT_CAST./
1bacc 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c * Opcode: ToReal
1bacd 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1bace 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 * Force the valu
1bacf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1bad0 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e to be a floatin
1bad1 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a g point number..
1bad2 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20 ** If The value
1bad3 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 is currently an
1bad4 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 integer, convert
1bad5 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 it..** If the v
1bad6 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 alue is text or
1bad7 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e blob, try to con
1bad8 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e vert it to an in
1bad9 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a teger using the.
1bada 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 ** equivalent of
1badb 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 atoi() and stor
1badc 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 e 0.0 if no such
1badd 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 conversion is p
1bade 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 ossible..**.** A
1badf 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
1bae0 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
1bae1 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
1bae2 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
1bae3 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a .case OP_ToReal:
1bae4 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1bae5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1bae6 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a K_TO_REAL, in1 *
1bae7 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 /. if( (pIn1->f
1bae8 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1bae9 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1baea 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 e3VdbeMemRealify
1baeb 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
1baec 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1baed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
1baee 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a T */../* Opcode:
1baef 20 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20 Lt P1 P2 P3 P4
1baf0 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 P5.**.** Compare
1baf1 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 the values in r
1baf2 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 egister P1 and P
1baf3 33 2e 20 20 49 66 20 72 65 67 28 50 33 29 3c 72 3. If reg(P3)<r
1baf4 65 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a eg(P1) then.** j
1baf5 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 ump to address P
1baf6 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 2. .**.** If th
1baf7 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e e SQLITE_JUMPIFN
1baf8 55 4c 4c 20 62 69 74 20 6f 66 20 50 35 20 69 73 ULL bit of P5 is
1baf9 20 73 65 74 20 61 6e 64 20 65 69 74 68 65 72 20 set and either
1bafa 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65 reg(P1) or.** re
1bafb 67 28 50 33 29 20 69 73 20 4e 55 4c 4c 20 74 68 g(P3) is NULL th
1bafc 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
1bafd 2e 20 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 . If the SQLITE
1bafe 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 _JUMPIFNULL .**
1baff 62 69 74 20 69 73 20 63 6c 65 61 72 20 74 68 65 bit is clear the
1bb00 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 66 20 65 n fall thru if e
1bb01 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1bb02 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 NULL..**.** The
1bb03 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
1bb04 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d portion of P5 m
1bb05 75 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69 ust be an affini
1bb06 74 79 20 63 68 61 72 61 63 74 65 72 20 2d 0a 2a ty character -.*
1bb07 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 * SQLITE_AFF_TEX
1bb08 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e T, SQLITE_AFF_IN
1bb09 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f TEGER, and so fo
1bb0a 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 rth. An attempt
1bb0b 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63 is made .** to c
1bb0c 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70 75 74 oerce both input
1bb0d 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 s according to t
1bb0e 68 69 73 20 61 66 66 69 6e 69 74 79 20 62 65 66 his affinity bef
1bb0f 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 ore the.** compa
1bb10 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 rison is made. I
1bb11 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 f the SQLITE_AFF
1bb12 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c 20 74 _MASK is 0x00, t
1bb13 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 hen numeric.** a
1bb14 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 2e ffinity is used.
1bb15 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 Note that the a
1bb16 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 72 73 69 ffinity conversi
1bb17 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a ons are stored.*
1bb18 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 * back into the
1bb19 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20 input registers
1bb1a 50 31 20 61 6e 64 20 50 33 2e 20 20 53 6f 20 74 P1 and P3. So t
1bb1b 68 69 73 20 6f 70 63 6f 64 65 20 63 61 6e 20 63 his opcode can c
1bb1c 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 73 74 65 ause.** persiste
1bb1d 6e 74 20 63 68 61 6e 67 65 73 20 74 6f 20 72 65 nt changes to re
1bb1e 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 gisters P1 and P
1bb1f 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 3..**.** Once an
1bb20 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 y conversions ha
1bb21 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20 ve taken place,
1bb22 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61 6c 75 and neither valu
1bb23 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 e is NULL, .** t
1bb24 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f he values are co
1bb25 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74 68 20 mpared. If both
1bb26 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f 62 73 values are blobs
1bb27 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69 then memcmp() i
1bb28 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 s.** used to det
1bb29 65 72 6d 69 6e 65 20 74 68 65 20 72 65 73 75 6c ermine the resul
1bb2a 74 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 ts of the compar
1bb2b 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68 20 76 ison. If both v
1bb2c 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74 65 78 alues.** are tex
1bb2d 74 2c 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 t, then the appr
1bb2e 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e opriate collatin
1bb2f 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 g function speci
1bb30 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73 fied in.** P4 is
1bb31 20 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65 used to do the
1bb32 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 comparison. If
1bb33 20 50 34 20 69 73 20 6e 6f 74 20 73 70 65 63 69 P4 is not speci
1bb34 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d fied then.** mem
1bb35 63 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f cmp() is used to
1bb36 20 63 6f 6d 70 61 72 65 20 74 65 78 74 20 73 74 compare text st
1bb37 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 68 20 76 ring. If both v
1bb38 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d alues are.** num
1bb39 65 72 69 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d eric, then a num
1bb3a 65 72 69 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 eric comparison
1bb3b 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 is used. If the
1bb3c 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 two values.** ar
1bb3d 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 74 e of different t
1bb3e 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 ypes, then numbe
1bb3f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 rs are considere
1bb40 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73 d less than.** s
1bb41 74 72 69 6e 67 73 20 61 6e 64 20 73 74 72 69 6e trings and strin
1bb42 67 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 gs are considere
1bb43 64 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 d less than blob
1bb44 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 s..**.** If the
1bb45 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 62 SQLITE_STOREP2 b
1bb46 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c it of P5 is set,
1bb47 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d then do not jum
1bb48 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 p. Instead,.**
1bb49 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20 store a boolean
1bb4a 72 65 73 75 6c 74 20 28 65 69 74 68 65 72 20 30 result (either 0
1bb4b 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 , or 1, or NULL)
1bb4c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1bb4d 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .*/./* Opcode: N
1bb4e 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 e P1 P2 P3 P4 P5
1bb4f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b .**.** This work
1bb50 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 s just like the
1bb51 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 Lt opcode except
1bb52 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 that the jump i
1bb53 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 s taken if.** th
1bb54 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 e operands in re
1bb55 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 gisters P1 and P
1bb56 33 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 3 are not equal.
1bb57 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 See the Lt opc
1bb58 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 ode for.** addit
1bb59 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1bb5a 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a n..*/./* Opcode:
1bb5b 20 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20 Eq P1 P2 P3 P4
1bb5c 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1bb5d 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1bb5e 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1bb5f 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1bb60 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1bb61 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 the operands in
1bb62 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 registers P1 and
1bb63 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a P3 are equal..*
1bb64 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 * See the Lt opc
1bb65 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ode for addition
1bb66 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
1bb67 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 */./* Opcode: Le
1bb68 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1bb69 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1bb6a 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
1bb6b 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
1bb6c 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
1bb6d 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
1bb6e 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1bb6f 73 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 ster P3 is less
1bb70 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1bb71 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a the content of.
1bb72 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 ** register P1.
1bb73 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1bb74 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1bb75 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1bb76 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 /./* Opcode: Gt
1bb77 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1bb78 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
1bb79 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 just like the Lt
1bb7a 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 opcode except t
1bb7b 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 hat the jump is
1bb7c 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 taken if.** the
1bb7d 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1bb7e 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65 ter P3 is greate
1bb7f 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 r than the conte
1bb80 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 nt of.** registe
1bb81 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c r P1. See the L
1bb82 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 t opcode for add
1bb83 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1bb84 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ion..*/./* Opcod
1bb85 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 e: Ge P1 P2 P3 P
1bb86 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 4 P5.**.** This
1bb87 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
1bb88 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 the Lt opcode ex
1bb89 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 cept that the ju
1bb8a 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a mp is taken if.*
1bb8b 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
1bb8c 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 register P3 is
1bb8d 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1bb8e 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e equal to the con
1bb8f 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 tent of.** regis
1bb90 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 ter P1. See the
1bb91 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
1bb92 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1bb93 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f ation..*/.case O
1bb94 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 P_Eq:
1bb95 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1bb96 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c K_EQ, jump, in1,
1bb97 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1bb98 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 Ne:
1bb99 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1bb9a 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 NE, jump, in1, i
1bb9b 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 n3 */.case OP_Lt
1bb9c 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1bb9d 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 /* same as TK_LT
1bb9e 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 , jump, in1, in3
1bb9f 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 */.case OP_Le:
1bba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bba1 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 same as TK_LE,
1bba2 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1bba3 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 /.case OP_Gt:
1bba4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1bba5 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 ame as TK_GT, ju
1bba6 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1bba7 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 case OP_Ge: {
1bba8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1bba9 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 e as TK_GE, jump
1bbaa 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 20 20 , in1, in3 */.
1bbab 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 6e 74 int flags;. int
1bbac 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61 66 66 res;. char aff
1bbad 69 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67 73 20 inity;.. flags
1bbae 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 = pIn1->flags|pI
1bbaf 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 69 66 n3->flags;.. if
1bbb0 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ( flags&MEM_Null
1bbb1 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65 69 ){. /* If ei
1bbb2 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1bbb3 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 NULL then the re
1bbb4 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e sult is always N
1bbb5 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 ULL.. ** The
1bbb6 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 jump is taken if
1bbb7 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1bbb8 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65 IFNULL bit is se
1bbb9 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 t.. */. if
1bbba 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 ( pOp->p5 & SQLI
1bbbb 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 TE_STOREP2 ){.
1bbbc 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 pOut = &p->a
1bbbd 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1bbbe 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1bbbf 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c ag(pOut, MEM_Nul
1bbc0 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 l);. REGIST
1bbc1 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1bbc2 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65 6c , pOut);. }el
1bbc3 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 se if( pOp->p5 &
1bbc4 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 SQLITE_JUMPIFNU
1bbc5 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d LL ){. pc =
1bbc6 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 pOp->p2-1;.
1bbc7 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d }. break;. }
1bbc8 0a 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20 70 .. affinity = p
1bbc9 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f Op->p5 & SQLITE_
1bbca 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20 AFF_MASK;. if(
1bbcb 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 affinity ){.
1bbcc 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 applyAffinity(pI
1bbcd 6e 31 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e n1, affinity, en
1bbce 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70 coding);. app
1bbcf 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c lyAffinity(pIn3,
1bbd0 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 affinity, encod
1bbd1 69 6e 67 29 3b 0a 20 20 20 20 69 66 28 20 64 62 ing);. if( db
1bbd2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1bbd3 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1bbd4 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 }.. assert( pOp
1bbd5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c ->p4type==P4_COL
1bbd6 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e LSEQ || pOp->p4.
1bbd7 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 pColl==0 );. Ex
1bbd8 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a pandBlob(pIn1);.
1bbd9 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e ExpandBlob(pIn
1bbda 33 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 6c 69 3);. res = sqli
1bbdb 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 te3MemCompare(pI
1bbdc 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 n3, pIn1, pOp->p
1bbdd 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77 69 74 4.pColl);. swit
1bbde 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ch( pOp->opcode
1bbdf 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 45 ){. case OP_E
1bbe0 71 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3d q: res = res=
1bbe1 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 =0; break;.
1bbe2 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 case OP_Ne:
1bbe3 20 20 72 65 73 20 3d 20 72 65 73 21 3d 30 3b 20 res = res!=0;
1bbe4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
1bbe5 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 72 65 ase OP_Lt: re
1bbe6 73 20 3d 20 72 65 73 3c 30 3b 20 20 20 20 20 20 s = res<0;
1bbe7 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1bbe8 4f 50 5f 4c 65 3a 20 20 20 20 72 65 73 20 3d 20 OP_Le: res =
1bbe9 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 res<=0; brea
1bbea 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47 k;. case OP_G
1bbeb 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e t: res = res>
1bbec 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 0; break;.
1bbed 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 default:
1bbee 20 20 72 65 73 20 3d 20 72 65 73 3e 3d 30 3b 20 res = res>=0;
1bbef 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a break;. }..
1bbf0 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1bbf1 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 SQLITE_STOREP2 )
1bbf2 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d {. pOut = &p-
1bbf3 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1bbf4 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1bbf5 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
1bbf6 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 );. pOut->u.i
1bbf7 20 3d 20 72 65 73 3b 0a 20 20 20 20 52 45 47 49 = res;. REGI
1bbf8 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1bbf9 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 7d 65 6c p2, pOut);. }el
1bbfa 73 65 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 se if( res ){.
1bbfb 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1bbfc 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1bbfd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 ../* Opcode: Per
1bbfe 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 mutation * * * P
1bbff 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 4 *.**.** Set th
1bc00 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 e permutation us
1bc01 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d ed by the OP_Com
1bc02 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f pare operator to
1bc03 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a be the array.**
1bc04 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20 of integers in
1bc05 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 P4..**.** The pe
1bc06 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c rmutation is onl
1bc07 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 y valid until th
1bc08 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74 e next OP_Permut
1bc09 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 ation, OP_Compar
1bc0a 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f e,.** OP_Halt, o
1bc0b 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20 r OP_ResultRow.
1bc0c 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f Typically the O
1bc0d 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68 P_Permutation sh
1bc0e 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d ould occur.** im
1bc0f 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20 mediately prior
1bc10 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 to the OP_Compar
1bc11 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65 e..*/.case OP_Pe
1bc12 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 rmutation: {. a
1bc13 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1bc14 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20 pe==P4_INTARRAY
1bc15 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1bc16 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65 ->p4.ai );. aPe
1bc17 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e rmute = pOp->p4.
1bc18 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a ai;. break;.}..
1bc19 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 /* Opcode: Compa
1bc1a 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a re P1 P2 P3 P4 *
1bc1b 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 .**.** Compare t
1bc1c 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67 o vectors of reg
1bc1d 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31 isters in reg(P1
1bc1e 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20 )..reg(P1+P3-1)
1bc1f 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 (all this.** one
1bc20 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67 "A") and in reg
1bc21 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d (P2)..reg(P2+P3-
1bc22 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20 1) ("B"). Save
1bc23 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
1bc24 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1bc25 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e for use by the n
1bc26 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 ext OP_Jump inst
1bc27 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 ruct..**.** P4 i
1bc28 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 s a KeyInfo stru
1bc29 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e cture that defin
1bc2a 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 es collating seq
1bc2b 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a uences and sort.
1bc2c 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68 ** orders for th
1bc2d 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 e comparison. T
1bc2e 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 he permutation a
1bc2f 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74 pplies to regist
1bc30 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 ers.** only. Th
1bc31 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e e KeyInfo elemen
1bc32 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75 ts are used sequ
1bc33 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 entially..**.**
1bc34 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 The comparison i
1bc35 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 s a sort compari
1bc36 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f son, so NULLs co
1bc37 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 mpare equal,.**
1bc38 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 NULLs are less t
1bc39 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d han numbers, num
1bc3a 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 bers are less th
1bc3b 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 an strings,.** a
1bc3c 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c nd strings are l
1bc3d 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a ess than blobs..
1bc3e 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61 */.case OP_Compa
1bc3f 72 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 re: {. int n =
1bc40 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 69 pOp->p3;. int i
1bc41 2c 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f 6e 73 , p1, p2;. cons
1bc42 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 t KeyInfo *pKeyI
1bc43 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b nfo = pOp->p4.pK
1bc44 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 74 eyInfo;. assert
1bc45 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ( n>0 );. asser
1bc46 74 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 t( pKeyInfo!=0 )
1bc47 3b 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 ;. p1 = pOp->p1
1bc48 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 30 ;. assert( p1>0
1bc49 20 26 26 20 70 31 2b 6e 3c 3d 70 2d 3e 6e 4d 65 && p1+n<=p->nMe
1bc4a 6d 2b 31 20 29 3b 0a 20 20 70 32 20 3d 20 70 4f m+1 );. p2 = pO
1bc4b 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 p->p2;. assert(
1bc4c 20 70 32 3e 30 20 26 26 20 70 32 2b 6e 3c 3d 70 p2>0 && p2+n<=p
1bc4d 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 66 6f ->nMem+1 );. fo
1bc4e 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 r(i=0; i<n; i++)
1bc4f 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 {. int idx =
1bc50 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d aPermute ? aPerm
1bc51 75 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20 ute[i] : i;.
1bc52 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 CollSeq *pColl;
1bc53 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 /* Collating
1bc54 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20 sequence to use
1bc55 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a on this term */.
1bc56 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 int bRev;
1bc57 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 /* True f
1bc58 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f or DESCENDING so
1bc59 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 rt order */.
1bc5a 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1bc5b 31 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 1+idx, &p->aMem[
1bc5c 70 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 p1+idx]);. RE
1bc5d 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b GISTER_TRACE(p2+
1bc5e 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 idx, &p->aMem[p2
1bc5f 2b 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 +idx]);. asse
1bc60 72 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e rt( i<pKeyInfo->
1bc61 6e 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43 nField );. pC
1bc62 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e oll = pKeyInfo->
1bc63 61 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52 aColl[i];. bR
1bc64 65 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ev = pKeyInfo->a
1bc65 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 SortOrder[i];.
1bc66 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c iCompare = sql
1bc67 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 ite3MemCompare(&
1bc68 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c p->aMem[p1+idx],
1bc69 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 &p->aMem[p2+idx
1bc6a 5d 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 ], pColl);. i
1bc6b 66 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 f( iCompare ){.
1bc6c 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20 if( bRev )
1bc6d 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1bc6e 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
1bc6f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
1bc70 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
1bc71 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1bc72 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
1bc73 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1bc74 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
1bc75 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
1bc76 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
1bc77 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1bc78 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1bc79 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1bc7a 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1bc7b 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1bc7c 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1bc7d 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1bc7e 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
1bc7f 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
1bc80 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
1bc81 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
1bc82 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1bc83 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
1bc84 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1bc85 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
1bc86 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
1bc87 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1bc88 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1bc89 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1bc8a 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1bc8b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1bc8c 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1bc8d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1bc8e 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
1bc8f 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1bc90 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1bc91 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
1bc92 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
1bc93 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1bc94 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1bc95 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
1bc96 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
1bc97 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
1bc98 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1bc99 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1bc9a 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1bc9b 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1bc9c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1bc9d 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1bc9e 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
1bc9f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1bca0 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
1bca1 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1bca2 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1bca3 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1bca4 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
1bca5 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1bca6 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1bca7 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
1bca8 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1bca9 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1bcaa 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1bcab 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1bcac 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1bcad 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1bcae 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
1bcaf 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
1bcb0 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
1bcb1 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1bcb2 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
1bcb3 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1bcb4 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
1bcb5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1bcb6 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
1bcb7 6f 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31 out3 */. int v1
1bcb8 2c 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46 , v2; /* 0==F
1bcb9 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1bcba 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1bcbb 4c 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 L */.. if( pIn1
1bcbc 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1bcbd 6c 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 ll ){. v1 = 2
1bcbe 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 ;. }else{. v
1bcbf 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 1 = sqlite3VdbeI
1bcc0 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 ntValue(pIn1)!=0
1bcc1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 ;. }. if( pIn2
1bcc2 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1bcc3 6c 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32 ll ){. v2 = 2
1bcc4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 ;. }else{. v
1bcc5 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 2 = sqlite3VdbeI
1bcc6 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 ntValue(pIn2)!=0
1bcc7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d ;. }. if( pOp-
1bcc8 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 >opcode==OP_And
1bcc9 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f ){. static co
1bcca 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1bccb 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 r and_logic[] =
1bccc 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c { 0, 0, 0, 0, 1,
1bccd 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 2, 0, 2, 2 };.
1bcce 20 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 v1 = and_logi
1bccf 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 c[v1*3+v2];. }e
1bcd0 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
1bcd1 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1bcd2 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d har or_logic[] =
1bcd3 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 { 0, 1, 2, 1, 1
1bcd4 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a , 1, 2, 1, 2 };.
1bcd5 20 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 v1 = or_logi
1bcd6 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a c[v1*3+v2];. }.
1bcd7 20 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 if( v1==2 ){.
1bcd8 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1bcd9 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pOut, MEM_Null
1bcda 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1bcdb 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a pOut->u.i = v1;.
1bcdc 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1bcdd 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
1bcde 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1bcdf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
1bce0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1bce1 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 .** Interpret th
1bce2 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1bce3 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c ter P1 as a bool
1bce4 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f 72 ean value. Stor
1bce5 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 6e e the.** boolean
1bce6 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 72 complement in r
1bce7 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 egister P2. If
1bce8 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1bce9 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a 20 ister P1 is .**
1bcea 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 4c NULL, then a NUL
1bceb 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50 L is stored in P
1bcec 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 2..*/.case OP_No
1bced 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 t: {
1bcee 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1bcef 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 K_NOT, in1 */.
1bcf0 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1bcf1 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 pOp->p2];. if(
1bcf2 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1bcf3 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 M_Null ){. sq
1bcf4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1bcf5 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c ull(pOut);. }el
1bcf6 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 se{. sqlite3V
1bcf7 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 dbeMemSetInt64(p
1bcf8 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 Out, !sqlite3Vdb
1bcf9 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 eIntValue(pIn1))
1bcfa 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1bcfb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 ../* Opcode: Bit
1bcfc 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Not P1 P2 * * *.
1bcfd 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
1bcfe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1bcff 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e egister P1 as an
1bd00 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65 integer. Store
1bd01 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d the.** ones-com
1bd02 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 plement of the P
1bd03 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 1 value into reg
1bd04 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31 ister P2. If P1
1bd05 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c holds.** a NULL
1bd06 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 then store a NU
1bd07 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 LL in P2..*/.cas
1bd08 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 e OP_BitNot: {
1bd09 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1bd0a 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c me as TK_BITNOT,
1bd0b 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d in1 */. pOut =
1bd0c 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1bd0d 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 2];. if( pIn1->
1bd0e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1bd0f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1bd10 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f dbeMemSetNull(pO
1bd11 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ut);. }else{.
1bd12 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1bd13 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 7e SetInt64(pOut, ~
1bd14 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1bd15 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a lue(pIn1));. }.
1bd16 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1bd17 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20 pcode: If P1 P2
1bd18 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d P3 * *.**.** Jum
1bd19 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 p to P2 if the v
1bd1a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1bd1b 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68 P1 is true. Th
1bd1c 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 e value is.** is
1bd1d 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 considered true
1bd1e 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69 if it is numeri
1bd1f 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 c and non-zero.
1bd20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a If the value.**
1bd21 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 in P1 is NULL t
1bd22 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d hen take the jum
1bd23 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e p if P3 is true.
1bd24 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .*/./* Opcode: I
1bd25 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20 fNot P1 P2 P3 *
1bd26 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1bd27 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 P2 if the value
1bd28 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1bd29 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61 s False. The va
1bd2a 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e lue is.** is con
1bd2b 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 sidered true if
1bd2c 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63 it has a numeric
1bd2d 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20 value of zero.
1bd2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a If the value.**
1bd2f 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 in P1 is NULL t
1bd30 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d hen take the jum
1bd31 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e p if P3 is true.
1bd32 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 .*/.case OP_If:
1bd33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bd34 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a /* jump, in1 */.
1bd35 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b case OP_IfNot: {
1bd36 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1bd37 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e ump, in1 */. in
1bd38 74 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d t c;. if( pIn1-
1bd39 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1bd3a 6c 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 l ){. c = pOp
1bd3b 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 ->p3;. }else{.#
1bd3c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1bd3d 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1bd3e 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
1bd3f 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1bd40 31 29 21 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 1)!=0;.#else.
1bd41 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 c = sqlite3Vdbe
1bd42 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 RealValue(pIn1)!
1bd43 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 =0.0;.#endif.
1bd44 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1bd45 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 63 20 3d ==OP_IfNot ) c =
1bd46 20 21 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 !c;. }. if( c
1bd47 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1bd48 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 ->p2-1;. }. br
1bd49 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1bd4a 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 e: IsNull P1 P2
1bd4b 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d P3 * *.**.** Jum
1bd4c 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 p to P2 if the v
1bd4d 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1bd4e 20 50 31 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 P1 is NULL. If
1bd4f 20 50 33 20 69 73 20 67 72 65 61 74 65 72 0a 2a P3 is greater.*
1bd50 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 * than zero, the
1bd51 6e 20 63 68 65 63 6b 20 61 6c 6c 20 76 61 6c 75 n check all valu
1bd52 65 73 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 es reg(P1), reg(
1bd53 50 31 2b 31 29 2c 20 0a 2a 2a 20 72 65 67 28 50 P1+1), .** reg(P
1bd54 31 2b 32 29 2c 20 2e 2e 2e 2c 20 72 65 67 28 50 1+2), ..., reg(P
1bd55 31 2b 50 33 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65 1+P3-1)..*/.case
1bd56 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 OP_IsNull: {
1bd57 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1bd58 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a as TK_ISNULL, j
1bd59 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e ump, in1 */. in
1bd5a 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 t n = pOp->p3;.
1bd5b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1bd5c 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30 ==0 || pOp->p1>0
1bd5d 20 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 66 );. do{. if
1bd5e 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1bd5f 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b MEM_Null)!=0 ){
1bd60 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1bd61 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 >p2 - 1;. b
1bd62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1bd63 70 49 6e 31 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 pIn1++;. }while
1bd64 28 20 2d 2d 6e 20 3e 20 30 20 29 3b 0a 20 20 62 ( --n > 0 );. b
1bd65 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1bd66 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 de: NotNull P1 P
1bd67 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 2 * * *.**.** Ju
1bd68 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 mp to P2 if the
1bd69 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1bd6a 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c r P1 is not NULL
1bd6b 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e . .*/.case OP_N
1bd6c 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 otNull: {
1bd6d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1bd6e 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 TK_NOTNULL, jump
1bd6f 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 , in1 */. if( (
1bd70 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1bd71 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Null)==0 ){.
1bd72 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1bd73 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1bd74 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1bd75 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50 etNumColumns * P
1bd76 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 2 * * *.**.** Th
1bd77 69 73 20 6f 70 63 6f 64 65 20 73 65 74 73 20 74 is opcode sets t
1bd78 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1bd79 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 63 75 72 umns for the cur
1bd7a 73 6f 72 20 6f 70 65 6e 65 64 20 62 79 20 74 68 sor opened by th
1bd7b 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 e.** following i
1bd7c 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 50 32 nstruction to P2
1bd7d 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65 ..**.** An OP_Se
1bd7e 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 73 20 6f tNumColumns is o
1bd7f 6e 6c 79 20 75 73 65 66 75 6c 20 69 66 20 69 74 nly useful if it
1bd80 20 6f 63 63 75 72 73 20 69 6d 6d 65 64 69 61 74 occurs immediat
1bd81 65 6c 79 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6f ely before .** o
1bd82 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
1bd83 69 6e 67 20 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a ing opcodes:.**.
1bd84 2a 2a 20 20 20 20 20 4f 70 65 6e 52 65 61 64 0a ** OpenRead.
1bd85 2a 2a 20 20 20 20 20 4f 70 65 6e 57 72 69 74 65 ** OpenWrite
1bd86 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 50 73 65 75 .** OpenPseu
1bd87 64 6f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 do.**.** If the
1bd88 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
1bd89 20 69 73 20 74 6f 20 62 65 20 65 78 65 63 75 74 is to be execut
1bd8a 65 64 20 6f 6e 20 61 20 63 75 72 73 6f 72 2c 20 ed on a cursor,
1bd8b 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 6f 70 63 then.** this opc
1bd8c 6f 64 65 20 6d 75 73 74 20 62 65 20 70 72 65 73 ode must be pres
1bd8d 65 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ent immediately
1bd8e 62 65 66 6f 72 65 20 74 68 65 20 6f 70 63 6f 64 before the opcod
1bd8f 65 20 74 68 61 74 0a 2a 2a 20 6f 70 65 6e 73 20 e that.** opens
1bd90 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 23 the cursor..*/.#
1bd91 69 66 20 30 0a 63 61 73 65 20 4f 50 5f 53 65 74 if 0.case OP_Set
1bd92 4e 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 NumColumns: {.
1bd93 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a break;.}.#endif.
1bd94 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 ./* Opcode: Colu
1bd95 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a mn P1 P2 P3 P4 *
1bd96 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 .**.** Interpret
1bd97 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63 the data that c
1bd98 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 ursor P1 points
1bd99 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72 to as a structur
1bd9a 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a e built using.**
1bd9b 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 the MakeRecord
1bd9c 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 instruction. (S
1bd9d 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 ee the MakeRecor
1bd9e 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 d opcode for add
1bd9f 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 itional.** infor
1bda0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
1bda1 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 format of the d
1bda2 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 ata.) Extract t
1bda3 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a he P2-th column.
1bda4 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 ** from this rec
1bda5 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 ord. If there a
1bda6 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 re less that (P2
1bda7 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 +1) .** values i
1bda8 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 n the record, ex
1bda9 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a tract a NULL..**
1bdaa 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 .** The value ex
1bdab 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 tracted is store
1bdac 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1bdad 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
1bdae 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 olumn contains f
1bdaf 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 ewer than P2 fie
1bdb0 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 lds, then extrac
1bdb1 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a t a NULL. Or,.*
1bdb2 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75 * if the P4 argu
1bdb3 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d ment is a P4_MEM
1bdb4 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f use the value o
1bdb5 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e f the P4 argumen
1bdb6 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 t as.** the resu
1bdb7 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 lt..*/.case OP_C
1bdb8 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70 olumn: {. int p
1bdb9 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a ayloadSize; /*
1bdba 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1bdbb 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1bdbc 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 /. int p1 = pOp
1bdbd 2d 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c ->p1; /* P1 val
1bdbe 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 ue of the opcode
1bdbf 20 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 */. int p2 = p
1bdc0 4f 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75 Op->p2; /* colu
1bdc1 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 mn number to ret
1bdc2 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 rieve */. VdbeC
1bdc3 75 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a ursor *pC = 0;/*
1bdc4 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
1bdc5 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 */. char *zRec
1bdc6 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
1bdc7 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 ter to complete
1bdc8 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 record-data */.
1bdc9 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1bdca 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 ; /* The BTree
1bdcb 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 cursor */. u32
1bdcc 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 *aType;
1bdcd 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 /* aType[i] hold
1bdce 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 s the numeric ty
1bdcf 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 pe of the i-th c
1bdd0 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a olumn */. u32 *
1bdd1 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a aOffset; /*
1bdd2 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f aOffset[i] is o
1bdd3 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f ffset to start o
1bdd4 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 f data for i-th
1bdd5 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 column */. int
1bdd6 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f nField; /
1bdd7 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * number of fiel
1bdd8 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1bdd9 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 */. int len;
1bdda 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1bddb 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 length of the se
1bddc 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f rialized data fo
1bddd 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a r the column */.
1bdde 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
1bddf 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1bde0 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a nter */. char *
1bde1 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 zData; /*
1bde2 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f Part of the reco
1bde3 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 rd being decoded
1bde4 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 */. Mem *pDest
1bde5 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 ; /* Wher
1bde6 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 e to write the e
1bde7 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a xtracted value *
1bde8 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 /. Mem sMem;
1bde9 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 /* For st
1bdea 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 oring the record
1bdeb 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a being decoded *
1bdec 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4d 65 /.. memset(&sMe
1bded 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4d 65 m, 0, sizeof(sMe
1bdee 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 m));. assert( p
1bdef 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1bdf0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1bdf1 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1bdf2 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 p->nMem );. pDe
1bdf3 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f st = &p->aMem[pO
1bdf4 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 65 74 p->p3];. MemSet
1bdf5 54 79 70 65 46 6c 61 67 28 70 44 65 73 74 2c 20 TypeFlag(pDest,
1bdf6 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a MEM_Null);.. /*
1bdf7 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 This block sets
1bdf8 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 70 61 the variable pa
1bdf9 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20 yloadSize to be
1bdfa 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
1bdfb 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 of. ** bytes i
1bdfc 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 n the record..
1bdfd 2a 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69 73 20 **. ** zRec is
1bdfe 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 63 6f set to be the co
1bdff 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 mplete text of t
1be00 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 he record if it
1be01 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 is available..
1be02 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 ** The complete
1be03 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 20 61 record text is a
1be04 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 20 lways available
1be05 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 for pseudo-table
1be06 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 s. ** If the re
1be07 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 cord is stored i
1be08 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 20 n a cursor, the
1be09 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20 complete record
1be0a 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 text. ** might
1be0b 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 be available in
1be0c 74 68 65 20 20 70 43 2d 3e 61 52 6f 77 20 63 61 the pC->aRow ca
1be0d 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 che. Or it migh
1be0e 74 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 t not be.. ** I
1be0f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e f the data is un
1be10 61 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52 65 63 available, zRec
1be11 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
1be12 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c . **. ** We al
1be13 73 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e so compute the n
1be14 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1be15 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 in the record.
1be16 20 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 For cursors,.
1be17 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
1be18 20 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 columns is stor
1be19 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 ed in the VdbeCu
1be1a 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d rsor.nField elem
1be1b 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 20 3d ent.. */. pC =
1be1c 20 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b 0a 20 p->apCsr[p1];.
1be1d 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 assert( pC!=0 )
1be1e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1be1f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1be20 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d LE. assert( pC-
1be21 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 >pVtabCursor==0
1be22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 );.#endif. if(
1be23 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 pC->pCursor!=0 )
1be24 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 {. /* The rec
1be25 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e ord is stored in
1be26 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 a B-Tree */.
1be27 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1be28 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 eCursorMoveto(pC
1be29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
1be2a 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1be2b 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a 52 65 o_error;. zRe
1be2c 63 20 3d 20 30 3b 0a 20 20 20 20 70 43 72 73 72 c = 0;. pCrsr
1be2d 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a = pC->pCursor;.
1be2e 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c if( pC->null
1be2f 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 61 79 Row ){. pay
1be30 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 loadSize = 0;.
1be31 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e }else if( pC->
1be32 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e cacheStatus==p->
1be33 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 cacheCtr ){.
1be34 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 payloadSize =
1be35 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b pC->payloadSize;
1be36 0a 20 20 20 20 20 20 7a 52 65 63 20 3d 20 28 63 . zRec = (c
1be37 68 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b 0a 20 har*)pC->aRow;.
1be38 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d }else if( pC-
1be39 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 >isIndex ){.
1be3a 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a i64 payloadSiz
1be3b 65 36 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 e64;. sqlit
1be3c 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 e3BtreeKeySize(p
1be3d 43 72 73 72 2c 20 26 70 61 79 6c 6f 61 64 53 69 Crsr, &payloadSi
1be3e 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 70 61 79 ze64);. pay
1be3f 6c 6f 61 64 53 69 7a 65 20 3d 20 28 69 6e 74 29 loadSize = (int)
1be40 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 payloadSize64;.
1be41 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1be42 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
1be43 53 69 7a 65 28 70 43 72 73 72 2c 20 28 75 33 32 Size(pCrsr, (u32
1be44 20 2a 29 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 *)&payloadSize)
1be45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 ;. }. nFie
1be46 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b ld = pC->nField;
1be47 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1be48 73 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f sert( pC->pseudo
1be49 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 Table );. /*
1be4a 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68 The record is th
1be4b 65 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 e sole entry of
1be4c 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a a pseudo-table *
1be4d 2f 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a /. payloadSiz
1be4e 65 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 e = pC->nData;.
1be4f 20 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 zRec = pC->pD
1be50 61 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 ata;. pC->cac
1be51 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1be52 5f 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 _STALE;. asse
1be53 72 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d rt( payloadSize=
1be54 3d 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b =0 || zRec!=0 );
1be55 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 . nField = pC
1be56 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 ->nField;. pC
1be57 72 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 rsr = 0;. }..
1be58 2f 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a /* If payloadSiz
1be59 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 e is 0, then jus
1be5a 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a t store a NULL *
1be5b 2f 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 /. if( payloadS
1be5c 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 ize==0 ){. as
1be5d 73 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 sert( pDest->fla
1be5e 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 gs&MEM_Null );.
1be5f 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1be60 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 n_out;. }. if(
1be61 20 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d payloadSize>db-
1be62 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1be63 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1be64 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1be65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
1be66 20 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 p2<nField );..
1be67 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 /* Read and par
1be68 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 se the table hea
1be69 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 der. Store the
1be6a 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 results of the p
1be6b 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 arse. ** into t
1be6c 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 he record header
1be6d 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 cache fields of
1be6e 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a the cursor.. *
1be6f 2f 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e /. aType = pC->
1be70 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d aType;. if( pC-
1be71 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d >cacheStatus==p-
1be72 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 >cacheCtr ){.
1be73 20 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 aOffset = pC->a
1be74 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b Offset;. }else{
1be75 0a 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 . u8 *zIdx;
1be76 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
1be77 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 nto header */.
1be78 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 u8 *zEndHdr;
1be79 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1be7a 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 first byte afte
1be7b 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a r the header */.
1be7c 20 20 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 int offset;
1be7d 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
1be7e 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a nto the data */.
1be7f 20 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b int szHdrSz;
1be80 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1be81 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 the header size
1be82 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f field at start o
1be83 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 f record */.
1be84 69 6e 74 20 61 76 61 69 6c 20 3d 20 30 3b 20 20 int avail = 0;
1be85 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1be86 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 tes of available
1be87 20 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 data */.. as
1be88 73 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20 sert(aType);.
1be89 20 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 pC->aOffset = a
1be8a 4f 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b Offset = &aType[
1be8b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d nField];. pC-
1be8c 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 >payloadSize = p
1be8d 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
1be8e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1be8f 3d 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a = p->cacheCtr;..
1be90 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 /* Figure ou
1be91 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 t how many bytes
1be92 20 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 are in the head
1be93 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 er */. if( zR
1be94 65 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 ec ){. zDat
1be95 61 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 a = zRec;. }e
1be96 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 lse{. if( p
1be97 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 C->isIndex ){.
1be98 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 zData = (c
1be99 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 har*)sqlite3Btre
1be9a 65 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c eKeyFetch(pCrsr,
1be9b 20 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 &avail);.
1be9c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a }else{. z
1be9d 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 Data = (char*)sq
1be9e 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 lite3BtreeDataFe
1be9f 74 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 tch(pCrsr, &avai
1bea0 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 l);. }.
1bea1 20 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 /* If KeyFetch
1bea2 28 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d ()/DataFetch() m
1bea3 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 anaged to get th
1bea4 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 e entire payload
1bea5 2c 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 ,. ** save
1bea6 74 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 the payload in t
1bea7 68 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 he pC->aRow cach
1bea8 65 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 e. That will sa
1bea9 76 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 ve us from.
1beaa 20 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 ** having to ma
1beab 6b 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 ke additional ca
1beac 6c 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 lls to fetch the
1bead 20 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e content portion
1beae 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 of. ** the
1beaf 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a record.. *
1beb0 2f 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 /. if( avai
1beb1 6c 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 l>=payloadSize )
1beb2 7b 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d {. zRec =
1beb3 20 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 zData;.
1beb4 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 pC->aRow = (u8*)
1beb5 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c zData;. }el
1beb6 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e se{. pC->
1beb7 61 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 aRow = 0;.
1beb8 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 }. }. /* T
1beb9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
1beba 65 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 ert is true in a
1bebb 6c 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 ll cases accept
1bebc 77 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 when. ** the
1bebd 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
1bebe 73 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 s been corrupted
1bebf 20 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 externally..
1bec0 20 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a ** assert( z
1bec1 52 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e Rec!=0 || avail>
1bec2 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 =payloadSize ||
1bec3 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 avail>=9 ); */.
1bec4 20 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 szHdrSz = get
1bec5 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 Varint32((u8*)zD
1bec6 61 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 ata, offset);..
1bec7 20 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 /* The KeyFet
1bec8 63 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 ch() or DataFetc
1bec9 68 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 h() above are fa
1beca 73 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 st and will get
1becb 74 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a the entire. *
1becc 2a 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 * record header
1becd 69 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 in most cases.
1bece 42 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 But they will fa
1becf 69 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f il to get the co
1bed0 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 mplete. ** re
1bed1 63 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74 cord header if t
1bed2 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 he record header
1bed3 20 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e does not fit on
1bed4 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 a single page.
1bed5 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 ** in the B-T
1bed6 72 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 ree. When that
1bed7 68 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c happens, use sql
1bed8 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
1bed9 74 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a tree() to. **
1beda 20 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d acquire the com
1bedb 70 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78 plete header tex
1bedc 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 t.. */. if
1bedd 28 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c ( !zRec && avail
1bede 3c 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 <offset ){.
1bedf 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b sMem.flags = 0;
1bee0 0a 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d . sMem.db =
1bee1 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
1bee2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1bee3 6d 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c mBtree(pCrsr, 0,
1bee4 20 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 offset, pC->isI
1bee5 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 ndex, &sMem);.
1bee6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1bee7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1bee8 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f goto op_column_
1bee9 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 out;. }.
1beea 20 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e zData = sMem.
1beeb 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e z;. }. zEn
1beec 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 dHdr = (u8 *)&zD
1beed 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 ata[offset];.
1beee 20 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a zIdx = (u8 *)&z
1beef 44 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a Data[szHdrSz];..
1bef0 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 /* Scan the
1bef1 68 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 header and use i
1bef2 74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 t to fill in the
1bef3 20 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 aType[] and aOf
1bef4 66 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 fset[]. ** ar
1bef5 72 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 rays. aType[i]
1bef6 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 will contain the
1bef7 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f type integer fo
1bef8 72 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a r the i-th. *
1bef9 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 * column and aOf
1befa 66 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e fset[i] will con
1befb 74 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20 tain the offset
1befc 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 from the beginni
1befd 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ng. ** of the
1befe 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 record to the s
1beff 74 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 tart of the data
1bf00 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f for the i-th co
1bf01 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 lumn. */.
1bf02 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c for(i=0; i<nFiel
1bf03 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 d; i++){. i
1bf04 66 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 f( zIdx<zEndHdr
1bf05 29 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73 ){. aOffs
1bf06 65 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a et[i] = offset;.
1bf07 20 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 zIdx +=
1bf08 67 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78 getVarint32(zIdx
1bf09 2c 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 , aType[i]);.
1bf0a 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 offset += s
1bf0b 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1bf0c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d TypeLen(aType[i]
1bf0d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1bf0e 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20 /* If i
1bf0f 69 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69 is less that nFi
1bf10 65 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 eld, then there
1bf11 61 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 are less fields
1bf12 69 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 in this.
1bf13 2a 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 ** record than S
1bf14 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 etNumColumns ind
1bf15 69 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65 icated there are
1bf16 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a columns in the.
1bf17 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 ** table
1bf18 2e 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74 . Set the offset
1bf19 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 for any extra c
1bf1a 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 olumns not prese
1bf1b 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a nt in. **
1bf1c 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 the record to 0
1bf1d 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 . This tells cod
1bf1e 65 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 e below to store
1bf1f 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 a NULL.
1bf20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 ** instead of de
1bf21 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 serializing a va
1bf22 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 lue from the rec
1bf23 6f 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ord.. */.
1bf24 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b aOffset[
1bf25 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a i] = 0;. }.
1bf26 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1bf27 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1bf28 26 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d &sMem);. sMem
1bf29 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
1bf2a 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 l;.. /* If we
1bf2b 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 have read more
1bf2c 68 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e header data than
1bf2d 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 was contained i
1bf2e 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 n the header,.
1bf2f 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 ** or if the e
1bf30 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1bf31 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1bf32 62 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 be past the end
1bf33 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 of the. ** re
1bf34 63 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 cord, or if the
1bf35 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 end of the last
1bf36 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f field appears to
1bf37 20 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 be before the e
1bf38 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 nd. ** of the
1bf39 20 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c record (when al
1bf3a 6c 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 l fields present
1bf3b 29 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 ), then we must
1bf3c 62 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 be dealing .
1bf3d 2a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 ** with a corrup
1bf3e 74 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 t database..
1bf3f 2a 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e */. if( zIdx>
1bf40 7a 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 zEndHdr || offse
1bf41 74 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 t>payloadSize .
1bf42 20 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 || (zIdx==zE
1bf43 6e 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21 ndHdr && offset!
1bf44 3d 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b =payloadSize) ){
1bf45 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1bf46 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1bf47 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 . goto op_c
1bf48 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d olumn_out;. }
1bf49 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 . }.. /* Get t
1bf4a 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d he column inform
1bf4b 61 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 ation. If aOffse
1bf4c 74 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 t[p2] is non-zer
1bf4d 6f 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 o, then . ** de
1bf4e 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 serialize the va
1bf4f 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 lue from the rec
1bf50 6f 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b ord. If aOffset[
1bf51 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a p2] is zero,. *
1bf52 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 * then there are
1bf53 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c not enough fiel
1bf54 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1bf55 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
1bf56 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 ** request. I
1bf57 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 n this case, set
1bf58 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 the value NULL
1bf59 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 or to P4 if P4 i
1bf5a 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 s. ** a pointer
1bf5b 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 to a Mem object
1bf5c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 .. */. if( aOf
1bf5d 66 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 fset[p2] ){.
1bf5e 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1bf5f 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 TE_OK );. if(
1bf60 20 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 zRec ){. s
1bf61 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1bf62 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65 easeExternal(pDe
1bf63 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 st);. sqlit
1bf64 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 e3VdbeSerialGet(
1bf65 28 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 (u8 *)&zRec[aOff
1bf66 73 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b set[p2]], aType[
1bf67 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 p2], pDest);.
1bf68 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 }else{. le
1bf69 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 n = sqlite3VdbeS
1bf6a 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 erialTypeLen(aTy
1bf6b 70 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 pe[p2]);. s
1bf6c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1bf6d 65 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b e(&sMem, pDest);
1bf6e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1bf6f 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1bf70 72 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73 ree(pCrsr, aOffs
1bf71 65 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d et[p2], len, pC-
1bf72 3e 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 >isIndex, &sMem)
1bf73 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1bf74 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bf75 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c goto op_col
1bf76 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d umn_out;. }
1bf77 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 . zData = s
1bf78 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c Mem.z;. sql
1bf79 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 ite3VdbeSerialGe
1bf7a 74 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 t((u8*)zData, aT
1bf7b 79 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b ype[p2], pDest);
1bf7c 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74 . }. pDest
1bf7d 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 ->enc = encoding
1bf7e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
1bf7f 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d f( pOp->p4type==
1bf80 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 P4_MEM ){.
1bf81 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 sqlite3VdbeMemSh
1bf82 61 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c allowCopy(pDest,
1bf83 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d pOp->p4.pMem, M
1bf84 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 EM_Static);.
1bf85 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1bf86 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 ert( pDest->flag
1bf87 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 s&MEM_Null );.
1bf88 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
1bf89 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 we dynamically
1bf8a 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 allocated space
1bf8b 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 to hold the data
1bf8c 20 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 (in the. ** sq
1bf8d 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
1bf8e 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f Btree() call abo
1bf8f 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 ve) then transfe
1bf90 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 r control of tha
1bf91 74 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c t. ** dynamical
1bf92 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 ly allocated spa
1bf93 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70 ce over to the p
1bf94 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a Dest structure..
1bf95 20 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e ** This preven
1bf96 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 ts a memory copy
1bf97 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 .. */. if( sMe
1bf98 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 m.zMalloc ){.
1bf99 20 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d assert( sMem.z=
1bf9a 3d 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b =sMem.zMalloc );
1bf9b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70 . assert( !(p
1bf9c 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 Dest->flags & ME
1bf9d 4d 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 M_Dyn) );. as
1bf9e 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 sert( !(pDest->f
1bf9f 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 lags & (MEM_Blob
1bfa0 7c 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 |MEM_Str)) || pD
1bfa1 65 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 est->z==sMem.z )
1bfa2 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 ;. pDest->fla
1bfa3 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 gs &= ~(MEM_Ephe
1bfa4 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 m|MEM_Static);.
1bfa5 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 pDest->flags
1bfa6 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 |= MEM_Term;.
1bfa7 20 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d pDest->z = sMem
1bfa8 2e 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a .z;. pDest->z
1bfa9 4d 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d Malloc = sMem.zM
1bfaa 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 alloc;. }.. rc
1bfab 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
1bfac 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 mMakeWriteable(p
1bfad 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d Dest);..op_colum
1bfae 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f n_out:. UPDATE_
1bfaf 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 MAX_BLOBSIZE(pDe
1bfb0 73 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f st);. REGISTER_
1bfb1 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 TRACE(pOp->p3, p
1bfb2 44 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a Dest);. break;.
1bfb3 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 }../* Opcode: Af
1bfb4 66 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 finity P1 P2 * P
1bfb5 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 4 *.**.** Apply
1bfb6 61 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 affinities to a
1bfb7 72 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 range of P2 regi
1bfb8 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 sters starting w
1bfb9 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 ith P1..**.** P4
1bfba 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 is a string tha
1bfbb 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 t is P2 characte
1bfbc 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 rs long. The nth
1bfbd 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 character of th
1bfbe 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 e.** string indi
1bfbf 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e cates the column
1bfc0 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 affinity that s
1bfc1 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
1bfc2 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d r the nth.** mem
1bfc3 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 ory cell in the
1bfc4 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f range..*/.case O
1bfc5 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 P_Affinity: {.
1bfc6 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 char *zAffinity
1bfc7 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d = pOp->p4.z;. M
1bfc8 65 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d em *pData0 = &p-
1bfc9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1bfca 20 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 Mem *pLast = &
1bfcb 70 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 pData0[pOp->p2-1
1bfcc 5d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a ];. Mem *pRec;.
1bfcd 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 . for(pRec=pDat
1bfce 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b a0; pRec<=pLast;
1bfcf 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 pRec++){. Ex
1bfd0 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a pandBlob(pRec);.
1bfd1 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1bfd2 79 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 y(pRec, zAffinit
1bfd3 79 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 y[pRec-pData0],
1bfd4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 encoding);. }.
1bfd5 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1bfd6 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 code: MakeRecord
1bfd7 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1bfd8 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 *.** Convert P2
1bfd9 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e registers beginn
1bfda 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f ing with P1 into
1bfdb 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a a single entry.
1bfdc 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 ** suitable for
1bfdd 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 use as a data re
1bfde 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 cord in a databa
1bfdf 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 se table or as a
1bfe0 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e key.** in an in
1bfe1 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c dex. The detail
1bfe2 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 s of the format
1bfe3 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61 are irrelevant a
1bfe4 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 s long as.** the
1bfe5 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 OP_Column opcod
1bfe6 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 e can decode the
1bfe7 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a record later..*
1bfe8 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63 * Refer to sourc
1bfe9 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 e code comments
1bfea 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20 for the details
1bfeb 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a of the record.**
1bfec 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 format..**.** P
1bfed 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 4 may be a strin
1bfee 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61 g that is P2 cha
1bfef 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 racters long. T
1bff0 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 he nth character
1bff1 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e of the.** strin
1bff2 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 g indicates the
1bff3 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 column affinity
1bff4 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 that should be u
1bff5 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a sed for the nth.
1bff6 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 ** field of the
1bff7 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a index key..**.**
1bff8 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f The mapping fro
1bff9 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 m character to a
1bffa 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e ffinity is given
1bffb 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 by the SQLITE_A
1bffc 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 FF_.** macros de
1bffd 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 fined in sqliteI
1bffe 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 nt.h..**.** If P
1bfff 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 4 is NULL then a
1c000 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 ll index fields
1c001 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 have the affinit
1c002 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 y NONE..*/.case
1c003 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b OP_MakeRecord: {
1c004 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 . /* Assuming t
1c005 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 he record contai
1c006 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 ns N fields, the
1c007 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c record format l
1c008 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 ooks. ** like t
1c009 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d his:. **. ** -
1c00a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c00b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c00c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c00d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c00e 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 -------. ** | h
1c00f 64 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30 dr-size | type 0
1c010 20 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 | type 1 | ...
1c011 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 | type N-1 | dat
1c012 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 a0 | ... | data
1c013 4e 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d N-1 | . ** ----
1c014 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c015 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c016 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c017 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c018 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 ----. **. ** D
1c019 61 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 ata(0) is taken
1c01a 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 from register P1
1c01b 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 . Data(1) comes
1c01c 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 from register P
1c01d 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 1+1. ** and so
1c01e 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a froth.. **. **
1c01f 20 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 Each type field
1c020 20 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 is a varint rep
1c021 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 resenting the se
1c022 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 rial type of the
1c023 20 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e . ** correspon
1c024 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e ding data elemen
1c025 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 t (see sqlite3Vd
1c026 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e beSerialType()).
1c027 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 The. ** hdr-si
1c028 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f ze field is also
1c029 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 a varint which
1c02a 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 is the offset fr
1c02b 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 om the beginning
1c02c 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 . ** of the rec
1c02d 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 ord to data0..
1c02e 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 */. u8 *zNewRec
1c02f 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ord; /* A
1c030 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
1c031 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
1c032 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 new record */.
1c033 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 Mem *pRec;
1c034 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
1c035 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 ew record */. u
1c036 36 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 64 nData = 0;
1c037 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c038 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
1c039 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 space */. int
1c03a 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 nHdr = 0;
1c03b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c03c 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 bytes of header
1c03d 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e space */. i64 n
1c03e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 Byte = 0;
1c03f 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 /* Data space
1c040 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 required for thi
1c041 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e s record */. in
1c042 74 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 t nZero = 0;
1c043 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c044 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 f zero bytes at
1c045 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 the end of the r
1c046 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1c047 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 Varint;
1c048 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1c049 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 ytes in a varint
1c04a 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c */. u32 serial
1c04b 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 _type; /*
1c04c 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 Type field */.
1c04d 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 Mem *pData0;
1c04e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1c04f 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 field to be comb
1c050 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 ined into the re
1c051 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 cord */. Mem *p
1c052 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Last;
1c053 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f /* Last field o
1c054 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a f the record */.
1c055 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 int nField;
1c056 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c057 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
1c058 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c059 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b char *zAffinity;
1c05a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 /* The af
1c05b 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f finity string fo
1c05c 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a r the record */.
1c05d 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 int file_forma
1c05e 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 t; /* File
1c05f 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 format to use f
1c060 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 or encoding */.
1c061 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1c062 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
1c063 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 used in zNewRec
1c064 6f 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 ord[] */.. nFie
1c065 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 ld = pOp->p1;.
1c066 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d zAffinity = pOp-
1c067 3e 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 >p4.z;. assert(
1c068 20 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 nField>0 && pOp
1c069 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p2>0 && pOp->p
1c06a 32 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 2+nField<=p->nMe
1c06b 6d 2b 31 20 29 3b 0a 20 20 70 44 61 74 61 30 20 m+1 );. pData0
1c06c 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c = &p->aMem[nFiel
1c06d 64 5d 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 d];. nField = p
1c06e 4f 70 2d 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20 Op->p2;. pLast
1c06f 3d 20 26 70 44 61 74 61 30 5b 6e 46 69 65 6c 64 = &pData0[nField
1c070 2d 31 5d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d -1];. file_form
1c071 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 at = p->minWrite
1c072 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f FileFormat;.. /
1c073 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 * Loop through t
1c074 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 he elements that
1c075 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 will make up th
1c076 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75 e record to figu
1c077 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 re. ** out how
1c078 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 much space is re
1c079 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e quired for the n
1c07a 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a ew record.. */.
1c07b 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 for(pRec=pData
1c07c 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 0; pRec<=pLast;
1c07d 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 pRec++){. int
1c07e 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41 len;. if( zA
1c07f 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 ffinity ){.
1c080 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1c081 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 Rec, zAffinity[p
1c082 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 Rec-pData0], enc
1c083 6f 64 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 oding);. }.
1c084 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 if( pRec->flag
1c085 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52 s&MEM_Zero && pR
1c086 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 ec->n>0 ){.
1c087 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 sqlite3VdbeMemE
1c088 78 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b xpandBlob(pRec);
1c089 0a 20 20 20 20 7d 0a 20 20 20 20 73 65 72 69 61 . }. seria
1c08a 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 l_type = sqlite3
1c08b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 VdbeSerialType(p
1c08c 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 Rec, file_format
1c08d 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c );. len = sql
1c08e 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1c08f 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 peLen(serial_typ
1c090 65 29 3b 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d e);. nData +=
1c091 20 6c 65 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b len;. nHdr +
1c092 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c = sqlite3VarintL
1c093 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b en(serial_type);
1c094 0a 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 . if( pRec->f
1c095 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
1c096 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 ){. /* Only
1c097 20 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 pure zero-fille
1c098 64 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 d BLOBs can be i
1c099 6e 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 nput to this Opc
1c09a 6f 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 ode.. ** We
1c09b 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c do not allow bl
1c09c 6f 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 obs with a prefi
1c09d 78 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c x and a zero-fil
1c09e 6c 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 led tail. */.
1c09f 20 20 20 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63 nZero += pRec
1c0a0 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d ->u.nZero;. }
1c0a1 65 6c 73 65 20 69 66 28 20 6c 65 6e 20 29 7b 0a else if( len ){.
1c0a2 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b nZero = 0;
1c0a3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1c0a4 20 41 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c Add the initial
1c0a5 20 68 65 61 64 65 72 20 76 61 72 69 6e 74 20 61 header varint a
1c0a6 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a nd total the siz
1c0a7 65 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d 20 6e e */. nHdr += n
1c0a8 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 Varint = sqlite3
1c0a9 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 3b VarintLen(nHdr);
1c0aa 0a 20 20 69 66 28 20 6e 56 61 72 69 6e 74 3c 73 . if( nVarint<s
1c0ab 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1c0ac 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 6e 48 64 nHdr) ){. nHd
1c0ad 72 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 r++;. }. nByte
1c0ae 20 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d 6e 5a = nHdr+nData-nZ
1c0af 65 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 ero;. if( nByte
1c0b0 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1c0b1 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1c0b2 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
1c0b3 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 _big;. }.. /*
1c0b4 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75 Make sure the ou
1c0b5 74 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61 tput register ha
1c0b6 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 s a buffer large
1c0b7 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 enough to store
1c0b8 20 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72 . ** the new r
1c0b9 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75 ecord. The outpu
1c0ba 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d t register (pOp-
1c0bb 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f >p3) is not allo
1c0bc 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f wed to. ** be o
1c0bd 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 ne of the input
1c0be 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75 registers (becau
1c0bf 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
1c0c0 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71 call to. ** sq
1c0c1 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1c0c2 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72 () could clobber
1c0c3 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 the value befor
1c0c4 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20 e it is used)..
1c0c5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1c0c6 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c p->p3<pOp->p1 ||
1c0c7 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 pOp->p3>=pOp->p
1c0c8 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70 1+pOp->p2 );. p
1c0c9 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1c0ca 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 Op->p3];. if( s
1c0cb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1c0cc 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79 w(pOut, (int)nBy
1c0cd 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f te, 0) ){. go
1c0ce 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1c0cf 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75 zNewRecord = (u
1c0d0 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 8 *)pOut->z;..
1c0d1 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 63 /* Write the rec
1c0d2 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 75 74 ord */. i = put
1c0d3 56 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 65 63 Varint32(zNewRec
1c0d4 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 66 6f ord, nHdr);. fo
1c0d5 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 r(pRec=pData0; p
1c0d6 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 Rec<=pLast; pRec
1c0d7 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c 5f ++){. serial_
1c0d8 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 type = sqlite3Vd
1c0d9 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65 beSerialType(pRe
1c0da 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b c, file_format);
1c0db 0a 20 20 20 20 69 20 2b 3d 20 70 75 74 56 61 72 . i += putVar
1c0dc 69 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 6f 72 int32(&zNewRecor
1c0dd 64 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 d[i], serial_typ
1c0de 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 e); /* seri
1c0df 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 al type */. }.
1c0e0 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 for(pRec=pData0
1c0e1 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 ; pRec<=pLast; p
1c0e2 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 Rec++){ /* seri
1c0e3 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 al data */. i
1c0e4 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 += sqlite3VdbeS
1c0e5 65 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65 erialPut(&zNewRe
1c0e6 63 6f 72 64 5b 69 5d 2c 20 28 69 6e 74 29 28 6e cord[i], (int)(n
1c0e7 42 79 74 65 2d 69 29 2c 20 70 52 65 63 2c 66 69 Byte-i), pRec,fi
1c0e8 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a le_format);. }.
1c0e9 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79 assert( i==nBy
1c0ea 74 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 te );.. assert(
1c0eb 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f pOp->p3>0 && pO
1c0ec 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1c0ed 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 ;. pOut->n = (i
1c0ee 6e 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 nt)nByte;. pOut
1c0ef 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
1c0f0 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 ob | MEM_Dyn;.
1c0f1 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a pOut->xDel = 0;.
1c0f2 20 20 69 66 28 20 6e 5a 65 72 6f 20 29 7b 0a 20 if( nZero ){.
1c0f3 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f pOut->u.nZero
1c0f4 20 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f = nZero;. pO
1c0f5 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
1c0f6 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75 _Zero;. }. pOu
1c0f7 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f t->enc = SQLITE_
1c0f8 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 UTF8; /* In cas
1c0f9 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 e the blob is ev
1c0fa 65 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 er converted to
1c0fb 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54 text */. REGIST
1c0fc 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1c0fd 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 , pOut);. UPDAT
1c0fe 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1c0ff 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1c100 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 ../* Opcode: Cou
1c101 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a nt P1 P2 * * *.*
1c102 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e *.** Store the n
1c103 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
1c104 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c (an integer val
1c105 75 65 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ue) in the table
1c106 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70 or index .** op
1c107 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 ened by cursor P
1c108 31 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 1 in register P2
1c109 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1c10a 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 TE_OMIT_BTREECOU
1c10b 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74 NT.case OP_Count
1c10c 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f : { /* o
1c10d 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1c10e 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a /. i64 nEntry;.
1c10f 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c110 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 r = p->apCsr[pOp
1c111 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a ->p1]->pCursor;.
1c112 20 20 69 66 28 20 70 43 72 73 72 20 29 7b 0a 20 if( pCrsr ){.
1c113 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1c114 74 72 65 65 43 6f 75 6e 74 28 70 43 72 73 72 2c treeCount(pCrsr,
1c115 20 26 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 6c &nEntry);. }el
1c116 73 65 7b 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d se{. nEntry =
1c117 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 0;. }. pOut->
1c118 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1c119 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e . pOut->u.i = n
1c11a 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a Entry;. break;.
1c11b 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 }.#endif../* Opc
1c11c 6f 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20 50 ode: Statement P
1c11d 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1c11e 42 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 Begin an individ
1c11f 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 ual statement tr
1c120 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68 20 ansaction which
1c121 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61 72 is part of a lar
1c122 67 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ger.** transacti
1c123 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 on. This is nee
1c124 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 ded so that the
1c125 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e statement.** can
1c126 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 be rolled back
1c127 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 after an error w
1c128 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
1c129 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a roll back the.*
1c12a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 * entire transac
1c12b 74 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74 65 tion. The state
1c12c 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1c12d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 will automatica
1c12e 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 lly.** commit wh
1c12f 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 en the VDBE halt
1c130 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 s..**.** If the
1c131 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1c132 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ion is currently
1c133 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d in autocommit m
1c134 6f 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69 73 ode (that .** is
1c135 20 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20 69 to say, if it i
1c136 73 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45 47 s in between BEG
1c137 49 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a IN and COMMIT).*
1c138 2a 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 61 * and if there a
1c139 72 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74 69 re no other acti
1c13a 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e ve statements on
1c13b 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
1c13c 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
1c13d 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65 72 , then this oper
1c13e 61 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ation is a no-op
1c13f 2e 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20 . No statement
1c140 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 transaction.** i
1c141 73 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20 61 s needed since a
1c142 6e 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73 65 ny error can use
1c143 20 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c the normal ROLL
1c144 42 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a BACK process to.
1c145 2a 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e ** undo changes.
1c146 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61 74 .**.** If a stat
1c147 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1c148 6e 20 69 73 20 73 74 61 72 74 65 64 2c 20 74 68 n is started, th
1c149 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a en a statement j
1c14a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77 ournal file.** w
1c14b 69 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 ill be allocated
1c14c 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 and initialized
1c14d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 ..**.** The stat
1c14e 65 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20 6f ement is begun o
1c14f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1c150 69 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20 50 ile with index P
1c151 31 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 1. The main.**
1c152 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
1c153 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 s an index of 0
1c154 61 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 65 and the file use
1c155 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 d for temporary
1c156 74 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e tables.** has an
1c157 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a index of 1..*/.
1c158 63 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e case OP_Statemen
1c159 74 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61 t: {. if( db->a
1c15a 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 utoCommit==0 ||
1c15b 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1c15c 74 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 t>1 ){. int i
1c15d 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 = pOp->p1;.
1c15e 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 Btree *pBt;.
1c15f 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1c160 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 i<db->nDb );.
1c161 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 assert( db->aDb
1c162 5b 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 [i].pBt!=0 );.
1c163 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b pBt = db->aDb[
1c164 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 i].pBt;. asse
1c165 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1c166 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 IsInTrans(pBt) )
1c167 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1c168 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1c169 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 <<i))!=0 );.
1c16a 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e if( p->iStatemen
1c16b 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 t==0 ){. as
1c16c 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 sert( db->nState
1c16d 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e ment>=0 && db->n
1c16e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a Savepoint>=0 );.
1c16f 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 db->nState
1c170 6d 65 6e 74 2b 2b 3b 20 0a 20 20 20 20 20 20 70 ment++; . p
1c171 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 64 ->iStatement = d
1c172 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 b->nSavepoint +
1c173 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b 0a db->nStatement;.
1c174 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1c175 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e qlite3BtreeBegin
1c176 53 74 6d 74 28 70 42 74 2c 20 70 2d 3e 69 53 74 Stmt(pBt, p->iSt
1c177 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 atement);. }.
1c178 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1c179 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 ode: Savepoint P
1c17a 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1c17b 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f Open, release o
1c17c 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 r rollback the s
1c17d 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 avepoint named b
1c17e 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20 y parameter P4,
1c17f 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 depending.** on
1c180 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e the value of P1.
1c181 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 To open a new s
1c182 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e avepoint, P1==0.
1c183 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d To release (com
1c184 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 mit) an.** exist
1c185 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 ing savepoint, P
1c186 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 1==1, or to roll
1c187 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 back an existing
1c188 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 savepoint P1==2
1c189 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 ..*/.case OP_Sav
1c18a 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e 74 20 epoint: {. int
1c18b 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 p1 = pOp->p1;.
1c18c 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4f char *zName = pO
1c18d 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20 20 20 p->p4.z;
1c18e 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65 /* Name of save
1c18f 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 41 point */.. /* A
1c190 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 70 ssert that the p
1c191 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76 1 parameter is v
1c192 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 alid. Also that
1c193 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f if there is no o
1c194 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 pen. ** transac
1c195 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 tion, then there
1c196 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 cannot be any s
1c197 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20 2a 2f avepoints. . */
1c198 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 . assert( db->p
1c199 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 Savepoint==0 ||
1c19a 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d db->autoCommit==
1c19b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1c19c 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 1==SAVEPOINT_BEG
1c19d 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49 4e IN||p1==SAVEPOIN
1c19e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d 3d 53 T_RELEASE||p1==S
1c19f 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1c1a0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 K );. assert( d
1c1a1 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c b->pSavepoint ||
1c1a2 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 db->isTransacti
1c1a3 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 onSavepoint==0 )
1c1a4 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65 63 ;. assert( chec
1c1a5 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 kSavepointCount(
1c1a6 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 31 db) );.. if( p1
1c1a7 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 ==SAVEPOINT_BEGI
1c1a8 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d N ){. if( db-
1c1a9 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 >writeVdbeCnt>0
1c1aa 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 ){. /* A ne
1c1ab 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e w savepoint cann
1c1ac 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 66 ot be created if
1c1ad 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
1c1ae 65 20 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a e write . *
1c1af 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e * statements (i.
1c1b0 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 e. open read/wri
1c1b1 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 te incremental b
1c1b2 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 lob handles)..
1c1b3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
1c1b4 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1c1b5 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1c1b6 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 cannot open save
1c1b7 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 point - ".
1c1b8 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 "SQL statement
1c1b9 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b s in progress");
1c1ba 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1c1bb 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c TE_BUSY;. }el
1c1bc 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e se{. int nN
1c1bd 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ame = sqlite3Str
1c1be 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 len30(zName);.
1c1bf 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 Savepoint *p
1c1c0 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 New;.. /* C
1c1c1 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65 reate a new save
1c1c2 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e point structure.
1c1c3 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 20 3d */. pNew =
1c1c4 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1c1c5 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 53 Raw(db, sizeof(S
1c1c6 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d 65 2b avepoint)+nName+
1c1c7 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 1);. if( pN
1c1c8 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e ew ){. pN
1c1c9 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 ew->zName = (cha
1c1ca 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 r *)&pNew[1];.
1c1cb 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 memcpy(pNe
1c1cc 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c w->zName, zName,
1c1cd 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 0a nName+1);. .
1c1ce 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1c1cf 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74 ere is no open t
1c1d0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e ransaction, then
1c1d1 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20 mark this as a
1c1d2 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20 special.
1c1d3 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** "transaction
1c1d4 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20 savepoint". */.
1c1d5 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 if( db->a
1c1d6 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 utoCommit ){.
1c1d7 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1c1d8 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 ommit = 0;.
1c1d9 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 db->isTrans
1c1da 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 actionSavepoint
1c1db 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 1;. }el
1c1dc 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 se{. db
1c1dd 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a ->nSavepoint++;.
1c1de 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 0a 20 }. .
1c1df 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 /* Link t
1c1e0 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 he new savepoint
1c1e1 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
1c1e2 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74 se handle's list
1c1e3 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 . */. pNe
1c1e4 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 w->pNext = db->p
1c1e5 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 Savepoint;.
1c1e6 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e db->pSavepoin
1c1e7 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 t = pNew;.
1c1e8 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b }. }. }else{
1c1e9 0a 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a . Savepoint *
1c1ea 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 pSavepoint;.
1c1eb 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d int iSavepoint =
1c1ec 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 0;.. /* Find
1c1ed 20 74 68 65 20 6e 61 6d 65 64 20 73 61 76 65 70 the named savep
1c1ee 6f 69 6e 74 2e 20 49 66 20 74 68 65 72 65 20 69 oint. If there i
1c1ef 73 20 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f s no such savepo
1c1f0 69 6e 74 2c 20 74 68 65 6e 20 61 6e 0a 20 20 20 int, then an.
1c1f1 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 ** an error is
1c1f2 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1c1f3 75 73 65 72 2e 20 20 2a 2f 0a 20 20 20 20 66 6f user. */. fo
1c1f4 72 28 0a 20 20 20 20 20 20 70 53 61 76 65 70 6f r(. pSavepo
1c1f5 69 6e 74 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 int=db->pSavepoi
1c1f6 6e 74 3b 20 0a 20 20 20 20 20 20 70 53 61 76 65 nt; . pSave
1c1f7 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 point && sqlite3
1c1f8 53 74 72 49 43 6d 70 28 70 53 61 76 65 70 6f 69 StrICmp(pSavepoi
1c1f9 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 nt->zName, zName
1c1fa 29 3b 0a 20 20 20 20 20 20 70 53 61 76 65 70 6f );. pSavepo
1c1fb 69 6e 74 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e int=pSavepoint->
1c1fc 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 pNext. ){.
1c1fd 20 20 20 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b iSavepoint++;
1c1fe 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
1c1ff 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
1c200 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1c201 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1c202 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 , db, "no such s
1c203 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 7a avepoint: %s", z
1c204 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 63 20 Name);. rc
1c205 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1c206 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 0a 20 }else if( .
1c207 20 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 db->write
1c208 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 28 70 31 VdbeCnt>0 || (p1
1c209 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
1c20a 42 41 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69 BACK && db->acti
1c20b 76 65 56 64 62 65 43 6e 74 3e 31 29 20 0a 20 20 veVdbeCnt>1) .
1c20c 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 ){. /* It
1c20d 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1c20e 20 74 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d to release (com
1c20f 6d 69 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 mit) a savepoint
1c210 20 69 66 20 74 68 65 72 65 20 61 72 65 20 0a 20 if there are .
1c211 20 20 20 20 20 2a 2a 20 61 63 74 69 76 65 20 77 ** active w
1c212 72 69 74 65 20 73 74 61 74 65 6d 65 6e 74 73 2e rite statements.
1c213 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
1c214 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 ble to rollback
1c215 61 20 73 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 a savepoint.
1c216 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 ** if there ar
1c217 65 20 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 e any active sta
1c218 74 65 6d 65 6e 74 73 20 61 74 20 61 6c 6c 2e 0a tements at all..
1c219 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1c21a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1c21b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1c21c 20 0a 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f . "canno
1c21d 74 20 25 73 20 73 61 76 65 70 6f 69 6e 74 20 2d t %s savepoint -
1c21e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
1c21f 69 6e 20 70 72 6f 67 72 65 73 73 22 2c 0a 20 20 in progress",.
1c220 20 20 20 20 20 20 28 70 31 3d 3d 53 41 56 45 50 (p1==SAVEP
1c221 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 OINT_ROLLBACK ?
1c222 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c "rollback": "rel
1c223 65 61 73 65 22 29 0a 20 20 20 20 20 20 29 3b 0a ease"). );.
1c224 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1c225 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 E_BUSY;. }els
1c226 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 e{.. /* Det
1c227 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
1c228 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20 61 20 r not this is a
1c229 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 transaction save
1c22a 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 point. If so,.
1c22b 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 ** and this
1c22c 69 73 20 61 20 52 45 4c 45 41 53 45 20 63 6f 6d is a RELEASE com
1c22d 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 mand, then the c
1c22e 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
1c22f 6f 6e 20 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 on . ** is
1c230 63 6f 6d 6d 69 74 74 65 64 2e 20 0a 20 20 20 20 committed. .
1c231 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 */. int i
1c232 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 sTransaction = p
1c233 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1c234 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61 ==0 && db->isTra
1c235 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1c236 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54 t;. if( isT
1c237 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 31 ransaction && p1
1c238 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
1c239 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 64 ASE ){. d
1c23a 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1c23b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 1;. if( s
1c23c 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 qlite3VdbeHalt(p
1c23d 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 )==SQLITE_BUSY )
1c23e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 {. p->p
1c23f 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20 c = pc;.
1c240 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 db->autoCommit
1c241 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
1c242 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1c243 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
1c244 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 goto vdbe_re
1c245 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a turn;. }.
1c246 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 db->isTr
1c247 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 ansactionSavepoi
1c248 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nt = 0;.
1c249 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 rc = p->rc;.
1c24a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1c24b 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 int ii;.
1c24c 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 iSavepoint = db
1c24d 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2d 20 69 ->nSavepoint - i
1c24e 53 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 Savepoint - 1;.
1c24f 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b for(ii=0;
1c250 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b ii<db->nDb; ii+
1c251 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 +){. rc
1c252 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 = sqlite3BtreeS
1c253 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62 avepoint(db->aDb
1c254 5b 69 69 5d 2e 70 42 74 2c 20 70 31 2c 20 69 53 [ii].pBt, p1, iS
1c255 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1c256 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1c257 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1c258 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1c259 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1c25a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1c25b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1c25c 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 p1==SAVEPOINT_R
1c25d 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e OLLBACK && (db->
1c25e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 flags&SQLITE_Int
1c25f 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 ernChanges)!=0 )
1c260 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
1c261 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 te3ExpirePrepare
1c262 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b dStatements(db);
1c263 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1c264 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 e3ResetInternalS
1c265 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 chema(db, 0);.
1c266 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1c267 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61 . /* Rega
1c268 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
1c269 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45 r this is a RELE
1c26a 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c ASE or ROLLBACK,
1c26b 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 0a 20 20 destroy all .
1c26c 20 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 ** savepoint
1c26d 73 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20 s nested inside
1c26e 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 of the savepoint
1c26f 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 being operated
1c270 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 on. */. whi
1c271 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 le( db->pSavepoi
1c272 6e 74 21 3d 70 53 61 76 65 70 6f 69 6e 74 20 29 nt!=pSavepoint )
1c273 7b 0a 20 20 20 20 20 20 20 20 53 61 76 65 70 6f {. Savepo
1c274 69 6e 74 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e int *pTmp = db->
1c275 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 pSavepoint;.
1c276 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 db->pSavepoi
1c277 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e 65 78 74 nt = pTmp->pNext
1c278 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1c279 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 3DbFree(db, pTmp
1c27a 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e );. db->n
1c27b 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 Savepoint--;.
1c27c 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 }.. /* I
1c27d 66 20 69 74 20 69 73 20 61 20 52 45 4c 45 41 53 f it is a RELEAS
1c27e 45 2c 20 74 68 65 6e 20 64 65 73 74 72 6f 79 20 E, then destroy
1c27f 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 the savepoint be
1c280 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 ing operated on
1c281 74 6f 6f 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 too */. if(
1c282 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 p1==SAVEPOINT_R
1c283 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 ELEASE ){.
1c284 20 20 61 73 73 65 72 74 28 20 70 53 61 76 65 70 assert( pSavep
1c285 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53 61 76 65 70 oint==db->pSavep
1c286 6f 69 6e 74 20 29 3b 0a 20 20 20 20 20 20 20 20 oint );.
1c287 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d db->pSavepoint =
1c288 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 pSavepoint->pNe
1c289 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 xt;. sqli
1c28a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 te3DbFree(db, pS
1c28b 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1c28c 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 61 if( !isTransa
1c28d 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 ction ){.
1c28e 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1c28f 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 t--;. }.
1c290 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1c291 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a .. break;.}../*
1c292 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f 6d Opcode: AutoCom
1c293 6d 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a mit P1 P2 * * *.
1c294 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64 61 **.** Set the da
1c295 74 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d tabase auto-comm
1c296 69 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28 31 it flag to P1 (1
1c297 20 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69 73 or 0). If P2 is
1c298 20 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 true, roll.** b
1c299 61 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 6c ack any currentl
1c29a 79 20 61 63 74 69 76 65 20 62 74 72 65 65 20 74 y active btree t
1c29b 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 ransactions. If
1c29c 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 there are any ac
1c29d 74 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61 tive.** VMs (apa
1c29e 72 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 rt from this one
1c29f 29 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 41 ), then a ROLLBA
1c2a0 43 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f 4d CK fails. A COM
1c2a1 4d 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a 20 MIT fails if.**
1c2a2 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 there are active
1c2a3 20 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 20 writing VMs or
1c2a4 61 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 20 active VMs that
1c2a5 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65 use shared cache
1c2a6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
1c2a7 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73 20 truction causes
1c2a8 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e 0a the VM to halt..
1c2a9 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f 43 */.case OP_AutoC
1c2aa 6f 6d 6d 69 74 3a 20 7b 0a 20 20 69 6e 74 20 64 ommit: {. int d
1c2ab 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1c2ac 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e = pOp->p1;. in
1c2ad 74 20 72 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70 t rollback = pOp
1c2ae 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 74 75 72 6e ->p2;. int turn
1c2af 4f 6e 41 43 20 3d 20 64 65 73 69 72 65 64 41 75 OnAC = desiredAu
1c2b0 74 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d toCommit && !db-
1c2b1 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 0a 20 20 >autoCommit;..
1c2b2 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41 assert( desiredA
1c2b3 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 utoCommit==1 ||
1c2b4 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1c2b5 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t==0 );. assert
1c2b6 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d ( desiredAutoCom
1c2b7 6d 69 74 3d 3d 31 20 7c 7c 20 72 6f 6c 6c 62 61 mit==1 || rollba
1c2b8 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 ck==0 );.. asse
1c2b9 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
1c2ba 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 beCnt>0 ); /* A
1c2bb 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f 6e 65 t least this one
1c2bc 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 2a 2f VM is active */
1c2bd 0a 0a 20 20 69 66 28 20 74 75 72 6e 4f 6e 41 43 .. if( turnOnAC
1c2be 20 26 26 20 72 6f 6c 6c 62 61 63 6b 20 26 26 20 && rollback &&
1c2bf 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1c2c0 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 t>1 ){. /* If
1c2c1 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1c2c2 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52 n implements a R
1c2c3 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f 74 68 65 OLLBACK and othe
1c2c4 72 20 56 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a r VMs are. **
1c2c5 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 still running,
1c2c6 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f and a transactio
1c2c7 6e 20 69 73 20 61 63 74 69 76 65 2c 20 72 65 74 n is active, ret
1c2c8 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 urn an error ind
1c2c9 69 63 61 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 icating. ** t
1c2ca 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 56 4d hat the other VM
1c2cb 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 s must complete
1c2cc 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 first. . */.
1c2cd 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1c2ce 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1c2cf 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72 6f 6c db, "cannot rol
1c2d0 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f lback transactio
1c2d1 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 n - ". "S
1c2d2 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e QL statements in
1c2d3 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 progress");.
1c2d4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1c2d5 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 74 Y;. }else if( t
1c2d6 75 72 6e 4f 6e 41 43 20 26 26 20 21 72 6f 6c 6c urnOnAC && !roll
1c2d7 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 back && db->writ
1c2d8 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 eVdbeCnt>1 ){.
1c2d9 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 /* If this ins
1c2da 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 truction impleme
1c2db 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 nts a COMMIT and
1c2dc 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 other VMs are w
1c2dd 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 riting. ** re
1c2de 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e turn an error in
1c2df 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 dicating that th
1c2e0 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 e other VMs must
1c2e1 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e complete first.
1c2e2 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c . */. sql
1c2e3 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1c2e4 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1c2e5 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 cannot commit tr
1c2e6 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 ansaction - ".
1c2e7 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 "SQL state
1c2e8 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 ments in progres
1c2e9 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 s");. rc = SQ
1c2ea 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
1c2eb 73 65 20 69 66 28 20 64 65 73 69 72 65 64 41 75 se if( desiredAu
1c2ec 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e 61 75 toCommit!=db->au
1c2ed 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 toCommit ){.
1c2ee 69 66 28 20 72 6f 6c 6c 62 61 63 6b 20 29 7b 0a if( rollback ){.
1c2ef 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65 assert( de
1c2f0 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d siredAutoCommit=
1c2f1 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =1 );. sqli
1c2f2 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
1c2f3 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 b);. db->au
1c2f4 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 toCommit = 1;.
1c2f5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1c2f6 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1c2f7 28 75 38 29 64 65 73 69 72 65 64 41 75 74 6f 43 (u8)desiredAutoC
1c2f8 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 66 28 ommit;. if(
1c2f9 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
1c2fa 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 (p)==SQLITE_BUSY
1c2fb 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 ){. p->p
1c2fc 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20 c = pc;.
1c2fd 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d db->autoCommit =
1c2fe 20 28 75 38 29 28 31 2d 64 65 73 69 72 65 64 41 (u8)(1-desiredA
1c2ff 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 utoCommit);.
1c300 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d p->rc = rc =
1c301 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
1c302 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1c303 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
1c304 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1c305 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 ( db->nStatement
1c306 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==0 );. sqlit
1c307 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 e3CloseSavepoint
1c308 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 s(db);. if( p
1c309 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1c30a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1c30b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
1c30c 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
1c30d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1c30e 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 }. goto vd
1c30f 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c be_return;. }el
1c310 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 se{. sqlite3S
1c311 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1c312 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 rMsg, db,.
1c313 20 20 28 21 64 65 73 69 72 65 64 41 75 74 6f 43 (!desiredAutoC
1c314 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 ommit)?"cannot s
1c315 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 tart a transacti
1c316 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e on within a tran
1c317 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 saction":(.
1c318 20 20 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 (rollback)?"c
1c319 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d annot rollback -
1c31a 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 no transaction
1c31b 69 73 20 61 63 74 69 76 65 22 3a 0a 20 20 20 20 is active":.
1c31c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
1c31d 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 cannot commit -
1c31e 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 no transaction i
1c31f 73 20 61 63 74 69 76 65 22 29 29 3b 0a 20 20 20 s active"));.
1c320 20 20 20 20 20 20 0a 20 20 20 20 72 63 20 3d 20 . rc =
1c321 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1c322 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1c323 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 Opcode: Transac
1c324 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a tion P1 P2 * * *
1c325 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 .**.** Begin a t
1c326 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 ransaction. The
1c327 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 transaction end
1c328 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 s when a Commit
1c329 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f or Rollback.** o
1c32a 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 pcode is encount
1c32b 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 ered. Depending
1c32c 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c on the ON CONFL
1c32d 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 ICT setting, the
1c32e 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
1c32f 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f might also be ro
1c330 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 lled back if an
1c331 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
1c332 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 ered..**.** P1 i
1c333 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
1c334 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1c335 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 on which the tr
1c336 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 ansaction is.**
1c337 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 started. Index
1c338 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 0 is the main da
1c339 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
1c33a 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a index 1 is the.*
1c33b 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 * file used for
1c33c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1c33d 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 . Indices of 2
1c33e 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 or more are used
1c33f 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 for.** attached
1c340 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a databases..**.*
1c341 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a * If P2 is non-z
1c342 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 ero, then a writ
1c343 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e-transaction is
1c344 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 started. A RES
1c345 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a ERVED lock is.**
1c346 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 obtained on the
1c347 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
1c348 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e hen a write-tran
1c349 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1c34a 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 ed. No.** other
1c34b 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 process can sta
1c34c 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 rt another write
1c34d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 transaction whi
1c34e 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 le this transact
1c34f 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 ion is.** underw
1c350 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 ay. Starting a
1c351 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
1c352 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 n also creates a
1c353 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1c354 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 l. A.** write tr
1c355 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
1c356 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 e started before
1c357 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e any changes can
1c358 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a be made to the.
1c359 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 ** database. If
1c35a 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 P2 is 2 or grea
1c35b 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c ter then an EXCL
1c35c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c USIVE lock is al
1c35d 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f so obtained.** o
1c35e 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a n the file..**.*
1c35f 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c * If P2 is zero,
1c360 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63 then a read-loc
1c361 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e k is obtained on
1c362 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1c363 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 le..*/.case OP_T
1c364 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20 20 ransaction: {.
1c365 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1c366 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a . Btree *pBt;..
1c367 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1c368 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 & i<db->nDb );.
1c369 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1c36a 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 eeMask & (1<<i))
1c36b 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 !=0 );. pBt = d
1c36c 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a b->aDb[i].pBt;..
1c36d 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
1c36e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c36f 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 eeBeginTrans(pBt
1c370 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 , pOp->p2);.
1c371 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 if( rc==SQLITE_B
1c372 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e USY ){. p->
1c373 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 pc = pc;. p
1c374 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 ->rc = rc = SQLI
1c375 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67 TE_BUSY;. g
1c376 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b oto vdbe_return;
1c377 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
1c378 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c!=SQLITE_OK &&
1c379 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f rc!=SQLITE_READO
1c37a 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d 53 51 NLY /* && rc!=SQ
1c37b 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a LITE_BUSY */ ){.
1c37c 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1c37d 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1c37e 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1c37f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c380 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 ReadCookie P1 P2
1c381 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 P3 * *.**.** Re
1c382 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 ad cookie number
1c383 20 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 P3 from databas
1c384 65 20 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 e P1 and write i
1c385 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 t into register
1c386 50 32 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73 20 P2..** P3==0 is
1c387 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 the schema versi
1c388 6f 6e 2e 20 20 50 33 3d 3d 31 20 69 73 20 74 68 on. P3==1 is th
1c389 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 e database forma
1c38a 74 2e 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20 74 t..** P3==2 is t
1c38b 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 he recommended p
1c38c 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c ager cache size,
1c38d 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 and so forth.
1c38e 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 P1==0 is.** the
1c38f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1c390 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 le and P1==1 is
1c391 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1c392 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a e used to store.
1c393 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ** temporary tab
1c394 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 les..**.** If P1
1c395 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 is negative, th
1c396 65 6e 20 74 68 69 73 20 69 73 20 61 20 72 65 71 en this is a req
1c397 75 65 73 74 20 74 6f 20 72 65 61 64 20 74 68 65 uest to read the
1c398 20 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64 61 size of a.** da
1c399 74 61 62 61 73 65 73 20 66 72 65 65 2d 6c 69 73 tabases free-lis
1c39a 74 2e 20 50 33 20 6d 75 73 74 20 62 65 20 73 65 t. P3 must be se
1c39b 74 20 74 6f 20 31 20 69 6e 20 74 68 69 73 20 63 t to 1 in this c
1c39c 61 73 65 2e 20 54 68 65 20 61 63 74 75 61 6c 0a ase. The actual.
1c39d 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 63 65 ** database acce
1c39e 73 73 65 64 20 69 73 20 28 28 50 31 2b 31 29 2a ssed is ((P1+1)*
1c39f 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 -1). For example
1c3a0 2c 20 61 20 50 31 20 70 61 72 61 6d 65 74 65 72 , a P1 parameter
1c3a1 20 6f 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65 73 of -1.** corres
1c3a2 70 6f 6e 64 73 20 74 6f 20 64 61 74 61 62 61 73 ponds to databas
1c3a3 65 20 30 20 28 22 6d 61 69 6e 22 29 2c 20 61 20 e 0 ("main"), a
1c3a4 50 31 20 6f 66 20 2d 32 20 69 73 20 64 61 74 61 P1 of -2 is data
1c3a5 62 61 73 65 20 31 20 28 22 74 65 6d 70 22 29 2e base 1 ("temp").
1c3a6 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
1c3a7 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b t be a read-lock
1c3a8 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1c3a9 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 (either a trans
1c3aa 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 action.** must b
1c3ab 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 e started or the
1c3ac 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 re must be an op
1c3ad 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 en cursor) befor
1c3ae 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 e.** executing t
1c3af 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e his instruction.
1c3b0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 .*/.case OP_Read
1c3b1 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 Cookie: {
1c3b2 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1c3b3 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1c3b4 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 int iMeta;. int
1c3b5 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a iDb = pOp->p1;.
1c3b6 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d 20 int iCookie =
1c3b7 70 4f 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73 65 pOp->p3;.. asse
1c3b8 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 rt( pOp->p3<SQLI
1c3b9 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 TE_N_BTREE_META
1c3ba 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 );. if( iDb<0 )
1c3bb 7b 0a 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a {. iDb = (-1*
1c3bc 28 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 69 43 (iDb+1));. iC
1c3bd 6f 6f 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d ookie *= -1;. }
1c3be 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d . assert( iDb>=
1c3bf 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 0 && iDb<db->nDb
1c3c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1c3c1 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d ->aDb[iDb].pBt!=
1c3c2 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 0 );. assert( (
1c3c3 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1c3c4 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 1<<iDb))!=0 );.
1c3c5 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 69 6e 67 /* The indexing
1c3c6 20 6f 66 20 6d 65 74 61 20 76 61 6c 75 65 73 20 of meta values
1c3c7 61 74 20 74 68 65 20 73 63 68 65 6d 61 20 6c 61 at the schema la
1c3c8 79 65 72 20 69 73 20 6f 66 66 20 62 79 20 6f 6e yer is off by on
1c3c9 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 e from. ** the
1c3ca 69 6e 64 65 78 69 6e 67 20 69 6e 20 74 68 65 20 indexing in the
1c3cb 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 54 68 btree layer. Th
1c3cc 65 20 62 74 72 65 65 20 63 6f 6e 73 69 64 65 72 e btree consider
1c3cd 73 20 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20 2a s meta[0] to. *
1c3ce 2a 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 * be the number
1c3cf 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 69 6e of free pages in
1c3d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 61 the database (a
1c3d1 20 72 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 read-only value
1c3d2 29 0a 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61 5b ). ** and meta[
1c3d3 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 1] to be the sch
1c3d4 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 ema cookie. The
1c3d5 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 63 6f schema layer co
1c3d6 6e 73 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65 74 nsiders. ** met
1c3d7 61 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 a[1] to be the s
1c3d8 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 53 chema cookie. S
1c3d9 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 68 69 o we have to shi
1c3da 66 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a ft the index. *
1c3db 2a 20 62 79 20 6f 6e 65 20 69 6e 20 74 68 65 20 * by one in the
1c3dc 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d following statem
1c3dd 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ent.. */. rc =
1c3de 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1c3df 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 69 44 62 Meta(db->aDb[iDb
1c3e0 5d 2e 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f 6b ].pBt, 1 + iCook
1c3e1 69 65 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 ie, (u32 *)&iMet
1c3e2 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 a);. pOut->u.i
1c3e3 3d 20 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65 = iMeta;. MemSe
1c3e4 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1c3e5 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
1c3e6 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c3e7 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32 SetCookie P1 P2
1c3e8 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 P3 * *.**.** Wr
1c3e9 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ite the content
1c3ea 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 28 of register P3 (
1c3eb 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 interpreted as a
1c3ec 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e n integer).** in
1c3ed 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 to cookie number
1c3ee 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65 20 P2 of database
1c3ef 50 31 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 20 P1..** P2==0 is
1c3f0 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 the schema versi
1c3f1 6f 6e 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68 on. P2==1 is th
1c3f2 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 e database forma
1c3f3 74 2e 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 t..** P2==2 is t
1c3f4 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 he recommended p
1c3f5 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c ager cache size,
1c3f6 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 and so forth.
1c3f7 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 P1==0 is.** the
1c3f8 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1c3f9 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 le and P1==1 is
1c3fa 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1c3fb 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a e used to store.
1c3fc 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ** temporary tab
1c3fd 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 les..**.** A tra
1c3fe 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 nsaction must be
1c3ff 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 started before
1c400 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 6f executing this o
1c401 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f pcode..*/.case O
1c402 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 P_SetCookie: {
1c403 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 /* in3 */.
1c404 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 Db *pDb;. asse
1c405 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 rt( pOp->p2<SQLI
1c406 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 TE_N_BTREE_META
1c407 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1c408 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1c409 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p1<db->nDb );.
1c40a 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1c40b 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
1c40c 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 >p1))!=0 );. pD
1c40d 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 b = &db->aDb[pOp
1c40e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1c40f 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a pDb->pBt!=0 );.
1c410 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c411 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 Integerify(pIn3)
1c412 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 ;. /* See note
1c413 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66 about index shif
1c414 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 ting on OP_ReadC
1c415 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 ookie */. rc =
1c416 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
1c417 74 65 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 2c teMeta(pDb->pBt,
1c418 20 31 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 1+pOp->p2, (int
1c419 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 )pIn3->u.i);. i
1c41a 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b f( pOp->p2==0 ){
1c41b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 . /* When the
1c41c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
1c41d 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 hanges, record t
1c41e 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e he new cookie in
1c41f 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 ternally */.
1c420 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 pDb->pSchema->sc
1c421 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28 69 hema_cookie = (i
1c422 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 nt)pIn3->u.i;.
1c423 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 db->flags |= S
1c424 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
1c425 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ges;. }else if(
1c426 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b 0a 20 pOp->p2==1 ){.
1c427 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 61 /* Record cha
1c428 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 nges in the file
1c429 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 70 format */. p
1c42a 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c Db->pSchema->fil
1c42b 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 70 e_format = (u8)p
1c42c 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 In3->u.i;. }.
1c42d 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 if( pOp->p1==1 )
1c42e 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64 {. /* Invalid
1c42f 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 ate all prepared
1c430 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e statements when
1c431 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61 ever the TEMP da
1c432 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 63 tabase. ** sc
1c433 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e hema is changed.
1c434 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a Ticket #1644 *
1c435 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 /. sqlite3Exp
1c436 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1c437 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a 20 ments(db);. }.
1c438 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c439 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b code: VerifyCook
1c43a 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a ie P1 P2 *.**.**
1c43b 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 Check the value
1c43c 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 of global datab
1c43d 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 ase parameter nu
1c43e 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 mber 0 (the.** s
1c43f 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 chema version) a
1c440 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 nd make sure it
1c441 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 is equal to P2.
1c442 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 .** P1 is the d
1c443 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 atabase number w
1c444 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 hich is 0 for th
1c445 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1c446 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f file.** and 1 fo
1c447 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 r the file holdi
1c448 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ng temporary tab
1c449 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 les and some hig
1c44a 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f her number.** fo
1c44b 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 r auxiliary data
1c44c 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 bases..**.** The
1c44d 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 cookie changes
1c44e 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76 its value whenev
1c44f 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 er the database
1c450 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a schema changes..
1c451 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f ** This operatio
1c452 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 n is used to det
1c453 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74 68 ect when that th
1c454 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 e cookie has cha
1c455 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 nged.** and that
1c456 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f the current pro
1c457 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65 cess needs to re
1c458 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e read the schema.
1c459 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 .**.** Either a
1c45a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
1c45b 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73 s to have been s
1c45c 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f tarted or an OP_
1c45d 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f Open needs.** to
1c45e 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74 6f be executed (to
1c45f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 establish a rea
1c460 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 d lock) before t
1c461 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a his opcode is.**
1c462 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 invoked..*/.cas
1c463 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 e OP_VerifyCooki
1c464 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74 61 e: {. int iMeta
1c465 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a ;. Btree *pBt;.
1c466 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c467 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1c468 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1c469 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1c46a 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 sk & (1<<pOp->p1
1c46b 29 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d ))!=0 );. pBt =
1c46c 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 db->aDb[pOp->p1
1c46d 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 70 42 74 ].pBt;. if( pBt
1c46e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1c46f 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
1c470 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a 29 (pBt, 1, (u32 *)
1c471 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65 &iMeta);. }else
1c472 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1c473 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65 74 61 20 E_OK;. iMeta
1c474 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 = 0;. }. if( r
1c475 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1c476 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 iMeta!=pOp->p2 )
1c477 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
1c478 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1c479 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 sg);. p->zErr
1c47a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 Msg = sqlite3DbS
1c47b 74 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 trDup(db, "datab
1c47c 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 ase schema has c
1c47d 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a hanged");. /*
1c47e 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 If the schema-c
1c47f 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 ookie from the d
1c480 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 atabase file mat
1c481 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 ches the cookie
1c482 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 . ** stored w
1c483 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ith the in-memor
1c484 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
1c485 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20 of the schema,
1c486 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 do. ** not re
1c487 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 load the schema
1c488 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1c489 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 e file.. **.
1c48a 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c ** If virtual
1c48b 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75 -tables are in u
1c48c 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20 se, this is not
1c48d 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 just an optimiza
1c48e 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 tion.. ** Oft
1c48f 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f en, v-tables sto
1c490 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69 6e re their data in
1c491 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74 61 other SQLite ta
1c492 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20 bles, which.
1c493 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20 66 ** are queried f
1c494 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74 rom within xNext
1c495 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 () and other v-t
1c496 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69 able methods usi
1c497 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72 ng. ** prepar
1c498 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20 73 ed queries. If s
1c499 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20 6f uch a query is o
1c49a 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 ut-of-date, we d
1c49b 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 o not want to.
1c49c 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65 ** discard the
1c49d 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1c49e 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63 6f , as the user co
1c49f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 de implementing
1c4a0 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 the. ** v-tab
1c4a1 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f le would have to
1c4a2 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74 68 be ready for th
1c4a3 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 e sqlite3_vtab s
1c4a4 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a tructure itself.
1c4a5 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 ** to be inv
1c4a6 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76 65 alidated wheneve
1c4a7 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 r sqlite3_step()
1c4a8 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 is called from
1c4a9 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 61 within . ** a
1c4aa 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e v-table method.
1c4ab 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1c4ac 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1c4ad 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 .pSchema->schema
1c4ae 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74 61 20 29 _cookie!=iMeta )
1c4af 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
1c4b0 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 esetInternalSche
1c4b1 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b ma(db, pOp->p1);
1c4b2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 . }.. sqli
1c4b3 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 te3ExpirePrepare
1c4b4 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b dStatements(db);
1c4b5 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1c4b6 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 _SCHEMA;. }. b
1c4b7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c4b8 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 20 de: OpenRead P1
1c4b9 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1c4ba 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e * Open a read-on
1c4bb 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 ly cursor for th
1c4bc 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 e database table
1c4bd 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 whose root page
1c4be 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 is.** P2 in a d
1c4bf 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
1c4c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1c4c1 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
1c4c2 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 y P3. .** P3==0
1c4c3 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 means the main d
1c4c4 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d atabase, P3==1 m
1c4c5 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 eans the databas
1c4c6 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 e used for .** t
1c4c7 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c emporary tables,
1c4c8 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 and P3>1 means
1c4c9 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 70 used the corresp
1c4ca 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 0a onding attached.
1c4cb 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 69 ** database. Gi
1c4cc 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 6f ve the new curso
1c4cd 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 r an identifier
1c4ce 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a 2a of P1. The P1.*
1c4cf 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f * values need no
1c4d0 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 t be contiguous
1c4d1 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 but all P1 value
1c4d2 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c s should be smal
1c4d3 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 l integers..** I
1c4d4 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f t is an error fo
1c4d5 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 74 r P1 to be negat
1c4d6 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 ive..**.** If P5
1c4d7 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 65 !=0 then use the
1c4d8 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1c4d9 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20 72 ster P2 as the r
1c4da 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a oot page, not.**
1c4db 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 32 the value of P2
1c4dc 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 itself..**.** T
1c4dd 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 72 here will be a r
1c4de 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ead lock on the
1c4df 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 65 database wheneve
1c4e0 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a r there is an.**
1c4e1 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 open cursor. I
1c4e2 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 f the database w
1c4e3 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f as unlocked prio
1c4e4 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 75 r to this instru
1c4e5 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 ction.** then a
1c4e6 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 read lock is acq
1c4e7 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 uired as part of
1c4e8 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1c4e9 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f n. A read.** lo
1c4ea 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 ck allows other
1c4eb 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 61 processes to rea
1c4ec 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 d the database b
1c4ed 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 ut prohibits.**
1c4ee 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
1c4ef 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 s from modifying
1c4f0 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1c4f1 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 The read lock is
1c4f2 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 .** released whe
1c4f3 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 n all cursors ar
1c4f4 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 e closed. If th
1c4f5 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 is instruction a
1c4f6 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 ttempts.** to ge
1c4f7 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 t a read lock bu
1c4f8 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 72 t fails, the scr
1c4f9 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 77 ipt terminates w
1c4fa 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 ith an.** SQLITE
1c4fb 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 65 _BUSY error code
1c4fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 ..**.** The P4 v
1c4fd 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 alue may be eith
1c4fe 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 er an integer (P
1c4ff 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 4_INT32) or a po
1c500 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 inter to.** a Ke
1c501 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1c502 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 (P4_KEYINFO). If
1c503 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 it is a pointer
1c504 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a to a KeyInfo .*
1c505 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
1c506 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65 n said structure
1c507 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e defines the con
1c508 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 tent and collati
1c509 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ng .** sequence
1c50a 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 of the index bei
1c50b 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 ng opened. Other
1c50c 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61 wise, if P4 is a
1c50d 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 n integer .** va
1c50e 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74 lue, it is set t
1c50f 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1c510 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 columns in the t
1c511 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 able..**.** See
1c512 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a also OpenWrite..
1c513 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 */./* Opcode: Op
1c514 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33 enWrite P1 P2 P3
1c515 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 P4 P5.**.** Ope
1c516 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 n a read/write c
1c517 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f ursor named P1 o
1c518 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 n the table or i
1c519 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a ndex whose root.
1c51a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20 ** page is P2.
1c51b 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20 Or if P5!=0 use
1c51c 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1c51d 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69 egister P2 to fi
1c51e 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 nd the.** root p
1c51f 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 age..**.** The P
1c520 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 4 value may be e
1c521 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 ither an integer
1c522 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 (P4_INT32) or a
1c523 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 pointer to.** a
1c524 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
1c525 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e re (P4_KEYINFO).
1c526 20 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e If it is a poin
1c527 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f ter to a KeyInfo
1c528 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 .** structure,
1c529 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 then said struct
1c52a 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ure defines the
1c52b 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c content and coll
1c52c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e ating .** sequen
1c52d 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ce of the index
1c52e 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 being opened. Ot
1c52f 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 herwise, if P4 i
1c530 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a s an integer .**
1c531 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 value, it is se
1c532 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 t to the number
1c533 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
1c534 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 e table..**.** T
1c535 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1c536 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
1c537 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 OpenRead except
1c538 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 that it opens th
1c539 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 e cursor.** in r
1c53a 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 ead/write mode.
1c53b 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 For a given tab
1c53c 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 le, there can be
1c53d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 one or more rea
1c53e 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 d-only.** cursor
1c53f 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 s or a single re
1c540 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 ad/write cursor
1c541 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a but not both..**
1c542 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 .** See also Ope
1c543 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f nRead..*/.case O
1c544 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 P_OpenRead:.case
1c545 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b OP_OpenWrite: {
1c546 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d 20 . int nField =
1c547 30 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 0;. KeyInfo *pK
1c548 65 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 69 6e eyInfo = 0;. in
1c549 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1c54a 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 int p2 = pOp->p
1c54b 32 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 2;. int iDb = p
1c54c 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 77 72 Op->p3;. int wr
1c54d 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 Flag;. Btree *p
1c54e 58 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 X;. VdbeCursor
1c54f 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 *pCur;. Db *pDb
1c550 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 69 ;. . assert( i
1c551 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d Db>=0 && iDb<db-
1c552 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1c553 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1c554 26 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 & (1<<iDb))!=0 )
1c555 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 ;. pDb = &db->a
1c556 44 62 5b 69 44 62 5d 3b 0a 20 20 70 58 20 3d 20 Db[iDb];. pX =
1c557 70 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 pDb->pBt;. asse
1c558 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 69 rt( pX!=0 );. i
1c559 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1c55a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a OP_OpenWrite ){.
1c55b 20 20 20 20 77 72 46 6c 61 67 20 3d 20 31 3b 0a wrFlag = 1;.
1c55c 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 if( pDb->pSc
1c55d 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1c55e 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 t < p->minWriteF
1c55f 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 ileFormat ){.
1c560 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 p->minWriteFi
1c561 6c 65 46 6f 72 6d 61 74 20 3d 20 70 44 62 2d 3e leFormat = pDb->
1c562 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f pSchema->file_fo
1c563 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 rmat;. }. }e
1c564 6c 73 65 7b 0a 20 20 20 20 77 72 46 6c 61 67 20 lse{. wrFlag
1c565 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 = 0;. }. if( p
1c566 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 61 73 Op->p5 ){. as
1c567 73 65 72 74 28 20 70 32 3e 30 20 29 3b 0a 20 20 sert( p2>0 );.
1c568 20 20 61 73 73 65 72 74 28 20 70 32 3c 3d 70 2d assert( p2<=p-
1c569 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70 49 6e >nMem );. pIn
1c56a 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d 2 = &p->aMem[p2]
1c56b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1c56c 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1c56d 49 6e 32 29 3b 0a 20 20 20 20 70 32 20 3d 20 28 In2);. p2 = (
1c56e 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 int)pIn2->u.i;.
1c56f 20 20 20 69 66 28 20 70 32 3c 32 20 29 20 7b 0a if( p2<2 ) {.
1c570 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1c571 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1c572 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1c573 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1c574 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
1c575 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 69 66 28 t( i>=0 );. if(
1c576 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1c577 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 _KEYINFO ){.
1c578 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
1c579 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 p4.pKeyInfo;.
1c57a 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d pKeyInfo->enc =
1c57b 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 ENC(p->db);.
1c57c 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e nField = pKeyIn
1c57d 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 fo->nField+1;.
1c57e 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 }else if( pOp->p
1c57f 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 4type==P4_INT32
1c580 29 7b 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 ){. nField =
1c581 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a 20 pOp->p4.i;. }.
1c582 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 pCur = allocate
1c583 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 6e 46 69 Cursor(p, i, nFi
1c584 65 6c 64 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 eld, iDb, 1);.
1c585 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f if( pCur==0 ) go
1c586 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 to no_mem;. pCu
1c587 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a r->nullRow = 1;.
1c588 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c589 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32 reeCursor(pX, p2
1c58a 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e , wrFlag, pKeyIn
1c58b 66 6f 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 6f fo, pCur->pCurso
1c58c 72 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 r);. pCur->pKey
1c58d 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b Info = pKeyInfo;
1c58e 0a 0a 20 20 73 77 69 74 63 68 28 20 72 63 20 29 .. switch( rc )
1c58f 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
1c590 45 5f 42 55 53 59 3a 20 7b 0a 20 20 20 20 20 20 E_BUSY: {.
1c591 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 p->pc = pc;.
1c592 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 p->rc = rc = S
1c593 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
1c594 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 goto vdbe_retu
1c595 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 rn;. }. ca
1c596 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a 20 7b 0a se SQLITE_OK: {.
1c597 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 int flags
1c598 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c = sqlite3BtreeFl
1c599 61 67 73 28 70 43 75 72 2d 3e 70 43 75 72 73 6f ags(pCur->pCurso
1c59a 72 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 61 6e r);. /* San
1c59b 69 74 79 20 63 68 65 63 6b 69 6e 67 2e 20 20 4f ity checking. O
1c59c 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 66 6f nly the lower fo
1c59d 75 72 20 62 69 74 73 20 6f 66 20 74 68 65 20 66 ur bits of the f
1c59e 6c 61 67 73 20 62 79 74 65 20 73 68 6f 75 6c 64 lags byte should
1c59f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 . ** be use
1c5a0 64 2e 20 20 42 69 74 20 33 20 28 6d 61 73 6b 20 d. Bit 3 (mask
1c5a1 30 78 30 38 29 20 69 73 20 75 6e 70 72 65 64 69 0x08) is unpredi
1c5a2 63 74 61 62 6c 65 2e 20 20 54 68 65 20 6c 6f 77 ctable. The low
1c5a3 65 72 20 33 20 62 69 74 73 0a 20 20 20 20 20 20 er 3 bits.
1c5a4 2a 2a 20 28 6d 61 73 6b 20 30 78 30 37 29 20 73 ** (mask 0x07) s
1c5a5 68 6f 75 6c 64 20 62 65 20 65 69 74 68 65 72 20 hould be either
1c5a6 35 20 28 69 6e 74 6b 65 79 2b 6c 65 61 66 64 61 5 (intkey+leafda
1c5a7 74 61 20 66 6f 72 20 74 61 62 6c 65 73 29 20 6f ta for tables) o
1c5a8 72 0a 20 20 20 20 20 20 2a 2a 20 32 20 28 7a 65 r. ** 2 (ze
1c5a9 72 6f 64 61 74 61 20 66 6f 72 20 69 6e 64 69 63 rodata for indic
1c5aa 65 73 29 2e 20 20 49 66 20 74 68 65 73 65 20 63 es). If these c
1c5ab 6f 6e 64 69 74 69 6f 6e 73 20 61 72 65 20 6e 6f onditions are no
1c5ac 74 20 6d 65 74 20 69 74 20 63 61 6e 0a 20 20 20 t met it can.
1c5ad 20 20 20 2a 2a 20 6f 6e 6c 79 20 6d 65 61 6e 20 ** only mean
1c5ae 74 68 61 74 20 77 65 20 61 72 65 20 64 65 61 6c that we are deal
1c5af 69 6e 67 20 77 69 74 68 20 61 20 63 6f 72 72 75 ing with a corru
1c5b0 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
1c5b1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1c5b2 69 66 28 20 28 66 6c 61 67 73 20 26 20 30 78 66 if( (flags & 0xf
1c5b3 30 29 21 3d 30 20 7c 7c 20 28 28 66 6c 61 67 73 0)!=0 || ((flags
1c5b4 20 26 20 30 78 30 37 29 21 3d 35 20 26 26 20 28 & 0x07)!=5 && (
1c5b5 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d 32 flags & 0x07)!=2
1c5b6 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
1c5b7 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1c5b8 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 _BKPT;. g
1c5b9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c5ba 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a _error;. }.
1c5bb 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61 pCur->isTa
1c5bc 62 6c 65 20 3d 20 28 66 6c 61 67 73 20 26 20 42 ble = (flags & B
1c5bd 54 52 45 45 5f 49 4e 54 4b 45 59 29 21 3d 30 20 TREE_INTKEY)!=0
1c5be 3f 31 3a 30 3b 0a 20 20 20 20 20 20 70 43 75 72 ?1:0;. pCur
1c5bf 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c 61 ->isIndex = (fla
1c5c0 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f 44 gs & BTREE_ZEROD
1c5c1 41 54 41 29 21 3d 30 20 3f 31 3a 30 3b 0a 20 20 ATA)!=0 ?1:0;.
1c5c2 20 20 20 20 2f 2a 20 49 66 20 50 34 3d 3d 30 20 /* If P4==0
1c5c3 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 it means we are
1c5c4 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70 65 6e expected to open
1c5c5 20 61 20 74 61 62 6c 65 2e 20 20 49 66 20 50 34 a table. If P4
1c5c6 21 3d 30 20 74 68 65 6e 0a 20 20 20 20 20 20 2a !=0 then. *
1c5c7 2a 20 77 65 20 65 78 70 65 63 74 20 74 6f 20 62 * we expect to b
1c5c8 65 20 6f 70 65 6e 69 6e 67 20 61 6e 20 69 6e 64 e opening an ind
1c5c9 65 78 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 ex. If this is
1c5ca 6e 6f 74 20 77 68 61 74 20 68 61 70 70 65 6e 65 not what happene
1c5cb 64 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e d,. ** then
1c5cc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1c5cd 20 63 6f 72 72 75 70 74 0a 20 20 20 20 20 20 2a corrupt. *
1c5ce 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 43 75 /. if( (pCu
1c5cf 72 2d 3e 69 73 54 61 62 6c 65 20 26 26 20 70 4f r->isTable && pO
1c5d0 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 p->p4type==P4_KE
1c5d1 59 49 4e 46 4f 29 0a 20 20 20 20 20 20 20 7c 7c YINFO). ||
1c5d2 20 28 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20 (pCur->isIndex
1c5d3 26 26 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d && pOp->p4type!=
1c5d4 50 34 5f 4b 45 59 49 4e 46 4f 29 20 29 7b 0a 20 P4_KEYINFO) ){.
1c5d5 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1c5d6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1c5d7 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1c5d8 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c5d9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1c5da 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1c5db 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4d 50 case SQLITE_EMP
1c5dc 54 59 3a 20 7b 0a 20 20 20 20 20 20 70 43 75 72 TY: {. pCur
1c5dd 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f 70 2d ->isTable = pOp-
1c5de 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 >p4type!=P4_KEYI
1c5df 4e 46 4f 3b 0a 20 20 20 20 20 20 70 43 75 72 2d NFO;. pCur-
1c5e0 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 75 72 >isIndex = !pCur
1c5e1 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 20 20 20 ->isTable;.
1c5e2 20 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 20 3d pCur->pCursor =
1c5e3 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 0;. rc = S
1c5e4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1c5e5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1c5e6 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
1c5e7 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1c5e8 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d _to_error;. }
1c5e9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1c5ea 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e ./* Opcode: Open
1c5eb 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20 Ephemeral P1 P2
1c5ec 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 * P4 *.**.** Ope
1c5ed 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50 n a new cursor P
1c5ee 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 1 to a transient
1c5ef 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 table..** The c
1c5f0 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20 ursor is always
1c5f1 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 opened read/writ
1c5f2 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68 e even if .** th
1c5f3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1c5f4 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 is read-only. T
1c5f5 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20 he transient or
1c5f6 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 virtual.** table
1c5f7 20 69 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f is deleted auto
1c5f8 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 matically when t
1c5f9 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f he cursor is clo
1c5fa 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 sed..**.** P2 is
1c5fb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
1c5fc 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69 olumns in the vi
1c5fd 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 rtual table..**
1c5fe 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 The cursor point
1c5ff 73 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 s to a BTree tab
1c600 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 le if P4==0 and
1c601 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 to a BTree index
1c602 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 .** if P4 is not
1c603 20 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 0. If P4 is no
1c604 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 t NULL, it point
1c605 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 s to a KeyInfo s
1c606 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 tructure.** that
1c607 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 defines the for
1c608 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 mat of keys in t
1c609 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 he index..**.**
1c60a 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 This opcode was
1c60b 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e once called Open
1c60c 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 Temp. But that
1c60d 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 created.** confu
1c60e 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 sion because the
1c60f 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c term "temp tabl
1c610 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 e", might refer
1c611 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 either.** to a T
1c612 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 EMP table at the
1c613 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 SQL level, or t
1c614 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 o a table opened
1c615 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f by.** this opco
1c616 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f de. Then this o
1c617 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f pcode was call O
1c618 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 penVirtual. But
1c619 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 .** that created
1c61a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 confusion with
1c61b 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 the whole virtua
1c61c 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f l-table idea..*/
1c61d 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68 .case OP_OpenEph
1c61e 65 6d 65 72 61 6c 3a 20 7b 0a 20 20 69 6e 74 20 emeral: {. int
1c61f 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1c620 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a dbeCursor *pCx;.
1c621 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 static const i
1c622 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 0a nt openFlags = .
1c623 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
1c624 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 N_READWRITE |.
1c625 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
1c626 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53 CREATE |. S
1c627 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
1c628 53 49 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c SIVE |. SQL
1c629 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
1c62a 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 NCLOSE |. S
1c62b 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 QLITE_OPEN_TRANS
1c62c 49 45 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 IENT_DB;.. asse
1c62d 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 rt( i>=0 );. pC
1c62e 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 x = allocateCurs
1c62f 6f 72 28 70 2c 20 69 2c 20 70 4f 70 2d 3e 70 32 or(p, i, pOp->p2
1c630 2c 20 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 , -1, 1);. if(
1c631 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f pCx==0 ) goto no
1c632 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c _mem;. pCx->nul
1c633 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d lRow = 1;. rc =
1c634 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 sqlite3BtreeFac
1c635 74 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 53 tory(db, 0, 1, S
1c636 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 QLITE_DEFAULT_TE
1c637 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 6f MP_CACHE_SIZE, o
1c638 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 penFlags,.
1c639 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c63a 20 20 20 20 20 26 70 43 78 2d 3e 70 42 74 29 3b &pCx->pBt);
1c63b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1c63c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
1c63d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
1c63e 69 6e 54 72 61 6e 73 28 70 43 78 2d 3e 70 42 74 inTrans(pCx->pBt
1c63f 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 , 1);. }. if(
1c640 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1c641 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61 . /* If a tra
1c642 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20 nsient index is
1c643 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65 required, create
1c644 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 it by calling.
1c645 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 ** sqlite3Btr
1c646 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20 eeCreateTable()
1c647 77 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a with the BTREE_Z
1c648 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66 ERODATA flag bef
1c649 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 ore. ** openi
1c64a 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e ng it. If a tran
1c64b 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72 sient table is r
1c64c 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73 equired, just us
1c64d 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 e the. ** aut
1c64e 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 omatically creat
1c64f 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f ed table with ro
1c650 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e ot-page 1 (an IN
1c651 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 TKEY table)..
1c652 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d */. if( pOp-
1c653 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a >p4.pKeyInfo ){.
1c654 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a int pgno;.
1c655 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c656 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 p->p4type==P4_KE
1c657 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 YINFO );. r
1c658 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1c659 43 72 65 61 74 65 54 61 62 6c 65 28 70 43 78 2d CreateTable(pCx-
1c65a 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52 >pBt, &pgno, BTR
1c65b 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 20 0a 20 EE_ZERODATA); .
1c65c 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1c65d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1c65e 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d assert( pgno==
1c65f 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b MASTER_ROOT+1 );
1c660 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1c661 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
1c662 28 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c (pCx->pBt, pgno,
1c663 20 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 1, .
1c664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c665 20 20 20 20 20 28 4b 65 79 49 6e 66 6f 2a 29 70 (KeyInfo*)p
1c666 4f 70 2d 3e 70 34 2e 7a 2c 20 70 43 78 2d 3e 70 Op->p4.z, pCx->p
1c667 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 Cursor);.
1c668 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCx->pKeyInfo =
1c669 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 pOp->p4.pKeyInf
1c66a 6f 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e o;. pCx->
1c66b 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 pKeyInfo->enc =
1c66c 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 ENC(p->db);.
1c66d 20 20 7d 0a 20 20 20 20 20 20 70 43 78 2d 3e 69 }. pCx->i
1c66e 73 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 sTable = 0;.
1c66f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1c670 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 = sqlite3BtreeCu
1c671 72 73 6f 72 28 70 43 78 2d 3e 70 42 74 2c 20 4d rsor(pCx->pBt, M
1c672 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 ASTER_ROOT, 1, 0
1c673 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b , pCx->pCursor);
1c674 0a 20 20 20 20 20 20 70 43 78 2d 3e 69 73 54 61 . pCx->isTa
1c675 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ble = 1;. }.
1c676 20 7d 0a 20 20 70 43 78 2d 3e 69 73 49 6e 64 65 }. pCx->isInde
1c677 78 20 3d 20 21 70 43 78 2d 3e 69 73 54 61 62 6c x = !pCx->isTabl
1c678 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f e;. break;.}../
1c679 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73 * Opcode: OpenPs
1c67a 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20 eudo P1 P2 P3 *
1c67b 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e *.**.** Open a n
1c67c 65 77 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 ew cursor that p
1c67d 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20 oints to a fake
1c67e 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 table that conta
1c67f 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 ins a single.**
1c680 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20 41 6e row of data. An
1c681 79 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 y attempt to wri
1c682 74 65 20 61 20 73 65 63 6f 6e 64 20 72 6f 77 20 te a second row
1c683 6f 66 20 64 61 74 61 20 63 61 75 73 65 73 20 74 of data causes t
1c684 68 65 0a 2a 2a 20 66 69 72 73 74 20 72 6f 77 20 he.** first row
1c685 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 20 to be deleted.
1c686 41 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65 All data is dele
1c687 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 ted when the cur
1c688 73 6f 72 20 69 73 0a 2a 2a 20 63 6c 6f 73 65 64 sor is.** closed
1c689 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f ..**.** A pseudo
1c68a 2d 74 61 62 6c 65 20 63 72 65 61 74 65 64 20 62 -table created b
1c68b 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 y this opcode is
1c68c 20 75 73 65 66 75 6c 20 66 6f 72 20 68 6f 6c 64 useful for hold
1c68d 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 45 57 20 6f ing the.** NEW o
1c68e 72 20 4f 4c 44 20 74 61 62 6c 65 73 20 69 6e 20 r OLD tables in
1c68f 61 20 74 72 69 67 67 65 72 2e 20 20 41 6c 73 6f a trigger. Also
1c690 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 used to hold th
1c691 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f e a single.** ro
1c692 77 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 74 68 w output from th
1c693 65 20 73 6f 72 74 65 72 20 73 6f 20 74 68 61 74 e sorter so that
1c694 20 74 68 65 20 72 6f 77 20 63 61 6e 20 62 65 20 the row can be
1c695 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e 74 6f 0a decomposed into.
1c696 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 63 6f ** individual co
1c697 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74 68 65 20 lumns using the
1c698 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
1c699 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 4f 50 5f ..**.** When OP_
1c69a 49 6e 73 65 72 74 20 69 73 20 65 78 65 63 75 74 Insert is execut
1c69b 65 64 20 74 6f 20 69 6e 73 65 72 74 20 61 20 72 ed to insert a r
1c69c 6f 77 20 69 6e 20 74 6f 20 74 68 65 20 70 73 65 ow in to the pse
1c69d 75 64 6f 20 74 61 62 6c 65 2c 0a 2a 2a 20 74 68 udo table,.** th
1c69e 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 e pseudo-table c
1c69f 75 72 73 6f 72 20 6d 61 79 20 6f 72 20 6d 61 79 ursor may or may
1c6a0 20 6e 6f 74 20 6d 61 6b 65 20 69 74 27 73 20 6f not make it's o
1c6a1 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a wn copy of the.*
1c6a2 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64 * original row d
1c6a3 61 74 61 2e 20 49 66 20 50 32 20 69 73 20 30 2c ata. If P2 is 0,
1c6a4 20 74 68 65 6e 20 74 68 65 20 70 73 65 75 64 6f then the pseudo
1c6a5 2d 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 70 79 -table will copy
1c6a6 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c the.** original
1c6a7 20 72 6f 77 20 64 61 74 61 2e 20 4f 74 68 65 72 row data. Other
1c6a8 77 69 73 65 2c 20 61 20 70 6f 69 6e 74 65 72 20 wise, a pointer
1c6a9 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
1c6aa 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 2a 2a 20 69 memory cell.** i
1c6ab 73 20 73 74 6f 72 65 64 2e 20 49 6e 20 74 68 69 s stored. In thi
1c6ac 73 20 63 61 73 65 2c 20 74 68 65 20 76 64 62 65 s case, the vdbe
1c6ad 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 65 6e program must en
1c6ae 73 75 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a sure that the .*
1c6af 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f * memory cell co
1c6b0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 ntaining the row
1c6b1 20 64 61 74 61 20 69 73 20 6e 6f 74 20 6f 76 65 data is not ove
1c6b2 72 77 72 69 74 74 65 6e 20 75 6e 74 69 6c 20 74 rwritten until t
1c6b3 68 65 0a 2a 2a 20 70 73 65 75 64 6f 20 74 61 62 he.** pseudo tab
1c6b4 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 28 6f 72 le is closed (or
1c6b5 20 61 20 6e 65 77 20 72 6f 77 20 69 73 20 69 6e a new row is in
1c6b6 73 65 72 74 65 64 20 69 6e 74 6f 20 69 74 29 2e serted into it).
1c6b7 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 .**.** P3 is the
1c6b8 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 number of field
1c6b9 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 s in the records
1c6ba 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 that will be st
1c6bb 6f 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 ored by.** the p
1c6bc 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1c6bd 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 case OP_OpenPseu
1c6be 64 6f 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 do: {. int i =
1c6bf 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 pOp->p1;. VdbeC
1c6c0 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 61 73 ursor *pCx;. as
1c6c1 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 sert( i>=0 );.
1c6c2 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 pCx = allocateCu
1c6c3 72 73 6f 72 28 70 2c 20 69 2c 20 70 4f 70 2d 3e rsor(p, i, pOp->
1c6c4 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69 66 p3, -1, 0);. if
1c6c5 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 ( pCx==0 ) goto
1c6c6 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e no_mem;. pCx->n
1c6c7 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 43 ullRow = 1;. pC
1c6c8 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 3d x->pseudoTable =
1c6c9 20 31 3b 0a 20 20 70 43 78 2d 3e 65 70 68 65 6d 1;. pCx->ephem
1c6ca 50 73 65 75 64 6f 54 61 62 6c 65 20 3d 20 28 75 PseudoTable = (u
1c6cb 38 29 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 43 78 8)pOp->p2;. pCx
1c6cc 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 ->isTable = 1;.
1c6cd 20 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 pCx->isIndex =
1c6ce 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 0;. break;.}../
1c6cf 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20 * Opcode: Close
1c6d0 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1c6d1 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 Close a cursor
1c6d2 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 previously opene
1c6d3 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31 20 d as P1. If P1
1c6d4 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e is not.** curren
1c6d5 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69 tly open, this i
1c6d6 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20 nstruction is a
1c6d7 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f no-op..*/.case O
1c6d8 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 69 6e 74 P_Close: {. int
1c6d9 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
1c6da 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1c6db 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a i<p->nCursor );.
1c6dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
1c6dd 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 eCursor(p, p->ap
1c6de 43 73 72 5b 69 5d 29 3b 0a 20 20 70 2d 3e 61 70 Csr[i]);. p->ap
1c6df 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 62 72 Csr[i] = 0;. br
1c6e0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c6e1 65 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32 20 e: SeekGe P1 P2
1c6e2 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 P3 P4 *.**.** If
1c6e3 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1c6e4 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c s to an SQL tabl
1c6e5 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 e (B-Tree that u
1c6e6 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ses integer keys
1c6e7 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 ), .** use the v
1c6e8 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1c6e9 20 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e 20 P3 as the key.
1c6ea 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 If cursor P1 re
1c6eb 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 fers .** to an S
1c6ec 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 QL index, then P
1c6ed 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 3 is the first i
1c6ee 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 n an array of P4
1c6ef 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 registers .** t
1c6f0 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 hat are used as
1c6f1 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1c6f2 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 x key. .**.** Re
1c6f3 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 position cursor
1c6f4 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 P1 so that it p
1c6f5 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 oints to the sma
1c6f6 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 llest entry that
1c6f7 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 .** is greater
1c6f8 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1c6f9 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1c6fa 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
1c6fb 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61 records .** grea
1c6fc 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
1c6fd 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 l to the key and
1c6fe 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c P2 is not zero,
1c6ff 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 then jump to P2
1c700 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c701 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e : Found, NotFoun
1c702 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 d, Distinct, See
1c703 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 kLt, SeekGt, See
1c704 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 kLe.*/./* Opcode
1c705 3a 20 53 65 65 6b 47 74 20 50 31 20 50 32 20 50 : SeekGt P1 P2 P
1c706 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 3 P4 *.**.** If
1c707 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1c708 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 to an SQL table
1c709 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 (B-Tree that us
1c70a 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 es integer keys)
1c70b 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 , .** use the va
1c70c 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1c70d 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 P3 as a key. If
1c70e 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1c70f 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 .** to an SQL i
1c710 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 ndex, then P3 is
1c711 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e the first in an
1c712 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 array of P4 reg
1c713 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 isters .** that
1c714 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 are used as an u
1c715 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1c716 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 y. .**.** Reposi
1c717 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 tion cursor P1 s
1c718 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 o that it point
1c719 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 s to the smalles
1c71a 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a t entry that .**
1c71b 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1c71c 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1c71d 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
1c71e 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72 20 records greater
1c71f 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 than .** the key
1c720 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a and P2 is not z
1c721 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 ero, then jump t
1c722 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 o P2..**.** See
1c723 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 also: Found, Not
1c724 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c Found, Distinct,
1c725 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 2c SeekLt, SeekGe,
1c726 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 SeekLe.*/./* Op
1c727 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31 20 code: SeekLt P1
1c728 50 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a P2 P3 P4 * .**.*
1c729 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 * If cursor P1 r
1c72a 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 efers to an SQL
1c72b 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 table (B-Tree th
1c72c 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 at uses integer
1c72d 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 keys), .** use t
1c72e 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1c72f 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 ster P3 as a key
1c730 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 . If cursor P1 r
1c731 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 efers .** to an
1c732 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 SQL index, then
1c733 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P3 is the first
1c734 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 in an array of P
1c735 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 4 registers .**
1c736 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 that are used as
1c737 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1c738 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 ex key. .**.** R
1c739 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 eposition cursor
1c73a 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 P1 so that it
1c73b 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 points to the la
1c73c 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 rgest entry that
1c73d 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 .** is less tha
1c73e 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e n the key value.
1c73f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1c740 20 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 68 records less th
1c741 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 an .** the key a
1c742 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 nd P2 is not zer
1c743 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 o, then jump to
1c744 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c P2..**.** See al
1c745 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f so: Found, NotFo
1c746 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 und, Distinct, S
1c747 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 eekGt, SeekGe, S
1c748 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f eekLe.*/./* Opco
1c749 64 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 32 de: SeekLe P1 P2
1c74a 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 P3 P4 *.**.** I
1c74b 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 f cursor P1 refe
1c74c 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 rs to an SQL tab
1c74d 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 le (B-Tree that
1c74e 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 uses integer key
1c74f 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 s), .** use the
1c750 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1c751 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 r P3 as a key. I
1c752 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 f cursor P1 refe
1c753 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c rs .** to an SQL
1c754 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 index, then P3
1c755 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 is the first in
1c756 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 an array of P4 r
1c757 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 egisters .** tha
1c758 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e t are used as an
1c759 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 unpacked index
1c75a 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f key. .**.** Repo
1c75b 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 sition cursor P1
1c75c 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
1c75d 74 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 ts to the larges
1c75e 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a t entry that .**
1c75f 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
1c760 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 equal to the ke
1c761 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 y value. If ther
1c762 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 e are no records
1c763 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f .** less than o
1c764 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b r equal to the k
1c765 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 ey and P2 is not
1c766 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 zero, then jump
1c767 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 to P2..**.** Se
1c768 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e e also: Found, N
1c769 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 otFound, Distinc
1c76a 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 t, SeekGt, SeekG
1c76b 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61 73 e, SeekLt.*/.cas
1c76c 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20 20 e OP_SeekLt:
1c76d 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1c76e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 3 */.case OP_See
1c76f 6b 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kLe: /*
1c770 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1c771 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20 20 e OP_SeekGe:
1c772 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1c773 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 3 */.case OP_See
1c774 6b 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 kGt: { /*
1c775 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 jump, in3 */. i
1c776 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1c777 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c778 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d ;.. assert( i>=
1c779 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 0 && i<p->nCurso
1c77a 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1c77b 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 70 Op->p2!=0 );. p
1c77c 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b C = p->apCsr[i];
1c77d 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 . assert( pC!=0
1c77e 20 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 );. if( pC->pC
1c77f 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 ursor!=0 ){.
1c780 69 6e 74 20 72 65 73 2c 20 6f 63 3b 0a 20 20 20 int res, oc;.
1c781 20 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 oc = pOp->opcod
1c782 65 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 e;. pC->nullR
1c783 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 ow = 0;. if(
1c784 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20 pC->isTable ){.
1c785 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 i64 iKey;
1c786 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 /* The rowid
1c787 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 we are to seek
1c788 74 6f 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 to */.. /*
1c789 54 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 20 The input value
1c78a 69 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 6f in P3 might be o
1c78b 66 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 65 f any type: inte
1c78c 67 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 6e ger, real, strin
1c78d 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 62 g,. ** blob
1c78e 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 20 , or NULL. But
1c78f 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 it needs to be a
1c790 6e 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65 n integer before
1c791 20 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20 20 we can do.
1c792 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73 6f ** the seek, so
1c793 20 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a 20 covert it. */.
1c794 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 applyNumeri
1c795 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 3b cAffinity(pIn3);
1c796 0a 20 20 20 20 20 20 69 4b 65 79 20 3d 20 73 71 . iKey = sq
1c797 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1c798 65 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 70 e(pIn3);. p
1c799 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1c79a 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 = 0;.. /* I
1c79b 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 f the P3 value c
1c79c 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 ould not be conv
1c79d 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e erted into an in
1c79e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20 teger without.
1c79f 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 ** loss of i
1c7a0 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e nformation, then
1c7a1 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73 special process
1c7a2 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e ing is required.
1c7a3 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 .. */. if(
1c7a4 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn3->flags & M
1c7a5 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 EM_Int)==0 ){.
1c7a6 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d if( (pIn3-
1c7a7 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1c7a8 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
1c7a9 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 /* If the P3
1c7aa 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 value cannot be
1c7ab 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
1c7ac 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d ny kind of a num
1c7ad 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a ber,. *
1c7ae 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20 * then the seek
1c7af 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c is not possible,
1c7b0 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a so jump to P2 *
1c7b1 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 3d /. pc =
1c7b2 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c7b3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1c7b4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1c7b5 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 /* If we reach
1c7b6 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e this point, then
1c7b7 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75 the P3 value mu
1c7b8 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 st be a floating
1c7b9 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e . ** poin
1c7ba 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 t number. */.
1c7bb 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 49 assert( (pI
1c7bc 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1c7bd 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 Real)!=0 );..
1c7be 20 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d 53 if( iKey==S
1c7bf 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26 MALLEST_INT64 &&
1c7c0 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c (pIn3->r<(doubl
1c7c1 65 29 69 4b 65 79 20 7c 7c 20 70 49 6e 33 2d 3e e)iKey || pIn3->
1c7c2 72 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 r>0) ){.
1c7c3 20 20 2f 2a 20 54 68 65 20 50 33 20 76 61 6c 75 /* The P3 valu
1c7c4 65 20 69 73 20 74 6f 20 6c 61 72 67 65 20 69 6e e is to large in
1c7c5 20 6d 61 67 6e 69 74 75 64 65 20 74 6f 20 62 65 magnitude to be
1c7c6 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 6e expressed as an
1c7c7 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e . ** in
1c7c8 74 65 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 teger. */.
1c7c9 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 20 res = 1;.
1c7ca 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d if( pIn3-
1c7cb 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >r<0 ){.
1c7cc 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 if( oc==OP_S
1c7cd 65 65 6b 47 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f eekGt || oc==OP_
1c7ce 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 SeekGe ){.
1c7cf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1c7d0 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 70 ite3BtreeFirst(p
1c7d1 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 C->pCursor, &res
1c7d2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
1c7d3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1c7d4 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
1c7d5 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1c7d6 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1c7d7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1c7d8 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 63 3d if( oc=
1c7d9 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63 =OP_SeekLt || oc
1c7da 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 ==OP_SeekLe ){.
1c7db 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 rc
1c7dc 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 = sqlite3BtreeLa
1c7dd 73 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 st(pC->pCursor,
1c7de 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 &res);.
1c7df 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1c7e0 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1c7e1 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c7e2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1c7e3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1c7e4 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b if( res ){
1c7e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 63 20 . pc
1c7e6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1c7e7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1c7e8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1c7e9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 63 }else if( oc
1c7ea 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f ==OP_SeekLt || o
1c7eb 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a c==OP_SeekGe ){.
1c7ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
1c7ed 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 20 66 the ceiling() f
1c7ee 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 unction to conve
1c7ef 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a rt real->int */.
1c7f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 if( pI
1c7f1 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c 65 29 n3->r > (double)
1c7f2 69 4b 65 79 20 29 20 69 4b 65 79 2b 2b 3b 0a 20 iKey ) iKey++;.
1c7f3 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1c7f4 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1c7f5 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 he floor() funct
1c7f6 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 ion to convert r
1c7f7 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 eal->int */.
1c7f8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 assert( oc
1c7f9 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 6f ==OP_SeekLe || o
1c7fa 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a c==OP_SeekGt );.
1c7fb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 if( pI
1c7fc 6e 33 2d 3e 72 20 3c 20 28 64 6f 75 62 6c 65 29 n3->r < (double)
1c7fd 69 4b 65 79 20 29 20 69 4b 65 79 2d 2d 3b 0a 20 iKey ) iKey--;.
1c7fe 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1c7ff 20 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c . rc = sql
1c800 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1c801 6e 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 npacked(pC->pCur
1c802 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 69 4b 65 sor, 0, (u64)iKe
1c803 79 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 y, 0, &res);.
1c804 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1c805 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1c806 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c807 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d o_error;. }
1c808 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 3d 3d . if( res==
1c809 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 0 ){. pC-
1c80a 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1c80b 31 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6c 1;. pC->l
1c80c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b 65 79 3b astRowid = iKey;
1c80d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1c80e 73 65 7b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b se{. Unpack
1c80f 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 edRecord r;.
1c810 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d 20 70 int nField = p
1c811 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 Op->p4.i;.
1c812 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1c813 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b ype==P4_INT32 );
1c814 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e . assert( n
1c815 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20 Field>0 );.
1c816 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 r.pKeyInfo = pC
1c817 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 ->pKeyInfo;.
1c818 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 r.nField = (u1
1c819 36 29 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 6)nField;.
1c81a 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 if( oc==OP_SeekG
1c81b 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b t || oc==OP_Seek
1c81c 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 2e Le ){. r.
1c81d 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 flags = UNPACKED
1c81e 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20 20 _INCRKEY;.
1c81f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1c820 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 .flags = 0;.
1c821 20 20 7d 0a 20 20 20 20 20 20 72 2e 61 4d 65 6d }. r.aMem
1c822 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1c823 3e 70 33 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d >p3];. rc =
1c824 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1c825 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 2d 3e etoUnpacked(pC->
1c826 70 43 75 72 73 6f 72 2c 20 26 72 2c 20 30 2c 20 pCursor, &r, 0,
1c827 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 0, &res);.
1c828 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1c829 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
1c82a 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1c82b 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rror;. }.
1c82c 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 pC->rowidIsV
1c82d 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a alid = 0;. }.
1c82e 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 pC->deferred
1c82f 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
1c830 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1c831 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 = CACHE_STALE;.#
1c832 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1c833 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 T. sqlite3_se
1c834 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 arch_count++;.#e
1c835 6e 64 69 66 0a 20 20 20 20 69 66 28 20 6f 63 3d ndif. if( oc=
1c836 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 6f 63 =OP_SeekGe || oc
1c837 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 7b 0a 20 ==OP_SeekGt ){.
1c838 20 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 7c if( res<0 |
1c839 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d | (res==0 && oc=
1c83a 3d 4f 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20 =OP_SeekGt) ){.
1c83b 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1c83c 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 2d te3BtreeNext(pC-
1c83d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
1c83e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1c83f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
1c840 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1c841 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 70 43 rror;. pC
1c842 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1c843 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0;. }else{
1c844 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 30 . res = 0
1c845 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1c846 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1c847 74 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 t( oc==OP_SeekLt
1c848 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c || oc==OP_SeekL
1c849 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 e );. if( r
1c84a 65 73 3e 30 20 7c 7c 20 28 72 65 73 3d 3d 30 20 es>0 || (res==0
1c84b 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 && oc==OP_SeekLt
1c84c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
1c84d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 = sqlite3BtreePr
1c84e 65 76 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 evious(pC->pCurs
1c84f 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
1c850 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1c851 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 E_OK ) goto abor
1c852 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c853 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 pC->rowi
1c854 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1c855 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c856 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 /* res might
1c857 62 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 be negative beca
1c858 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 use the table is
1c859 20 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 empty. Check t
1c85a 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 o. ** see
1c85b 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
1c85c 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f case.. */
1c85d 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 73 . res = s
1c85e 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 70 qlite3BtreeEof(p
1c85f 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 C->pCursor);.
1c860 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 }. }. a
1c861 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1c862 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
1c863 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1c864 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d p->p2 - 1;. }
1c865 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 43 . }else if( !pC
1c866 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b ->pseudoTable ){
1c867 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1c868 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 pens when attemp
1c869 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 ting to open the
1c86a 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 sqlite3_master
1c86b 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 table. ** for
1c86c 20 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74 read access ret
1c86d 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 urns SQLITE_EMPT
1c86e 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 Y. In this case
1c86f 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 always. ** ta
1c870 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e ke the jump (sin
1c871 63 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 ce there are no
1c872 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 records in the t
1c873 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 able).. */.
1c874 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1c875 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1c876 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1c877 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a eek P1 P2 * * *.
1c878 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f **.** P1 is an o
1c879 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72 pen table cursor
1c87a 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77 and P2 is a row
1c87b 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72 id integer. Arr
1c87c 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 ange.** for P1 t
1c87d 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69 o move so that i
1c87e 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1c87f 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50 rowid given by P
1c880 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 2..**.** This is
1c881 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65 actually a defe
1c882 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 rred seek. Noth
1c883 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70 ing actually hap
1c884 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 pens until.** th
1c885 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
1c886 20 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72 to read a recor
1c887 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66 d. That way, if
1c888 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 no reads.** occ
1c889 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 ur, no unnecessa
1c88a 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a ry I/O happens..
1c88b 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a */.case OP_Seek:
1c88c 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a { /* in2 */.
1c88d 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1c88e 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1c88f 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 *pC;.. assert(
1c890 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1c891 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1c892 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
1c893 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 sert( pC!=0 );.
1c894 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 if( pC->pCursor
1c895 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 !=0 ){. asser
1c896 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 t( pC->isTable )
1c897 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f ;. pC->nullRo
1c898 77 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 6d w = 0;. pC->m
1c899 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71 ovetoTarget = sq
1c89a 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1c89b 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 70 43 2d e(pIn2);. pC-
1c89c 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1c89d 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 0;. pC->defer
1c89e 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b 0a 20 redMoveto = 1;.
1c89f 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 20 20 }. break;.}.
1c8a0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 ../* Opcode: Fou
1c8a1 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a nd P1 P2 P3 * *.
1c8a2 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
1c8a3 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 3 holds a blob c
1c8a4 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 onstructed by Ma
1c8a5 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 keRecord. P1 is
1c8a6 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 an index..** If
1c8a7 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d an entry that m
1c8a8 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 atches the value
1c8a9 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 in register p3
1c8aa 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 exists in P1 the
1c8ab 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e n.** jump to P2.
1c8ac 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 If the P3 valu
1c8ad 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 e does not match
1c8ae 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 any entry in P1
1c8af 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 .** then fall th
1c8b0 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 ru. The P1 curs
1c8b1 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
1c8b2 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 ing at the match
1c8b3 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 ing entry.** if
1c8b4 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a it exists..**.**
1c8b5 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1c8b6 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 n is used to imp
1c8b7 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 lement the IN op
1c8b8 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 erator where the
1c8b9 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 .** left-hand si
1c8ba 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 de is a SELECT s
1c8bb 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 tatement. P1 ma
1c8bc 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 y be a true inde
1c8bd 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 x, or it.** may
1c8be 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 be a temporary i
1c8bf 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 ndex that holds
1c8c0 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
1c8c1 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 he SELECT.** sta
1c8c2 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 tement. This i
1c8c3 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c nstruction is al
1c8c4 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 so used to imple
1c8c5 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 ment the.** DIST
1c8c6 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 INCT keyword in
1c8c7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1c8c8 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
1c8c9 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 struction checks
1c8ca 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e if index P1 con
1c8cb 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 tains a record f
1c8cc 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 or which .** the
1c8cd 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 first N seriali
1c8ce 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 zed values exact
1c8cf 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 ly match the N s
1c8d0 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 erialized values
1c8d1 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 .** in the recor
1c8d2 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1c8d3 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
1c8d4 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1c8d5 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 values in.** th
1c8d6 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 e P3 record (the
1c8d7 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 P3 record is a
1c8d8 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 prefix of the P1
1c8d9 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a record). .**.**
1c8da 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f See also: NotFo
1c8db 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e und, IsUnique, N
1c8dc 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f otExists.*/./* O
1c8dd 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 pcode: NotFound
1c8de 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1c8df 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68 ** Register P3 h
1c8e0 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 olds a blob cons
1c8e1 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 tructed by MakeR
1c8e2 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a ecord. P1 is.**
1c8e3 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e an index. If n
1c8e4 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 o entry exists i
1c8e5 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65 n P1 that matche
1c8e6 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20 s the blob then
1c8e7 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20 jump.** to P2.
1c8e8 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73 If an entry does
1c8e9 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20 existing, fall
1c8ea 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75 through. The cu
1c8eb 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 rsor is left.**
1c8ec 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
1c8ed 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 entry that match
1c8ee 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c es..**.** See al
1c8ef 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 so: Found, NotEx
1c8f0 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a ists, IsUnique.*
1c8f1 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 /.case OP_NotFou
1c8f2 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d nd: /* jum
1c8f3 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1c8f4 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 P_Found: {
1c8f5 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1c8f6 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1c8f7 3e 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 >p1;. int alrea
1c8f8 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 dyExists = 0;.
1c8f9 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c8fa 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1c8fb 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1c8fc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1c8fd 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1c8fe 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 if( (pC = p->apC
1c8ff 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 sr[i])->pCursor!
1c900 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
1c901 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 s;. UnpackedR
1c902 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a ecord *pIdxKey;.
1c903 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1c904 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
1c905 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d assert( pIn3-
1c906 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1c907 62 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 b );. pIdxKey
1c908 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1c909 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70 cordUnpack(pC->p
1c90a 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e KeyInfo, pIn3->n
1c90b 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 , pIn3->z,.
1c90c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c90d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c90e 20 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f aTempRec, sizeo
1c90f 66 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20 f(aTempRec));.
1c910 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 if( pIdxKey==0
1c911 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
1c912 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
1c913 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1c914 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 ==OP_Found ){.
1c915 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61 pIdxKey->fla
1c916 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 gs |= UNPACKED_P
1c917 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 REFIX_MATCH;.
1c918 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
1c919 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
1c91a 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 packed(pC->pCurs
1c91b 6f 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 or, pIdxKey, 0,
1c91c 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71 0, &res);. sq
1c91d 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
1c91e 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 npackedRecord(pI
1c91f 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 dxKey);. if(
1c920 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1c921 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1c922 20 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45 }. alreadyE
1c923 78 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29 xists = (res==0)
1c924 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 ;. pC->deferr
1c925 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
1c926 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 pC->cacheStatu
1c927 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1c928 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e . }. if( pOp->
1c929 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 opcode==OP_Found
1c92a 20 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65 ){. if( alre
1c92b 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d adyExists ) pc =
1c92c 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c92d 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 }else{. if( !
1c92e 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 alreadyExists )
1c92f 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1c930 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1c931 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 ../* Opcode: IsU
1c932 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50 nique P1 P2 P3 P
1c933 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72 4 *.**.** Cursor
1c934 20 50 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61 P1 is open on a
1c935 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 n index. So it
1c936 68 61 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 has no data and
1c937 69 74 73 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 its key consists
1c938 20 0a 2a 2a 20 6f 66 20 61 20 72 65 63 6f 72 64 .** of a record
1c939 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 generated by OP
1c93a 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72 _MakeRecord wher
1c93b 65 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 e the last field
1c93c 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 is the .** rowi
1c93d 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74 d of the entry t
1c93e 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 72 65 hat the index re
1c93f 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 fers to..**.** T
1c940 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20 63 he P3 register c
1c941 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 ontains an integ
1c942 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 er record number
1c943 2e 20 43 61 6c 6c 20 74 68 69 73 20 72 65 63 6f . Call this reco
1c944 72 64 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e rd .** number R.
1c945 20 52 65 67 69 73 74 65 72 20 50 34 20 69 73 20 Register P4 is
1c946 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 73 the first in a s
1c947 65 74 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f et of N contiguo
1c948 75 73 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 us registers.**
1c949 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 that make up an
1c94a 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b unpacked index k
1c94b 65 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 ey that can be u
1c94c 73 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 sed with cursor
1c94d 50 31 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 P1..** The value
1c94e 20 6f 66 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 of N can be inf
1c94f 65 72 72 65 64 20 66 72 6f 6d 20 74 68 65 20 63 erred from the c
1c950 75 72 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 ursor. N include
1c951 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 s the rowid.** v
1c952 61 6c 75 65 20 61 70 70 65 6e 64 65 64 20 74 6f alue appended to
1c953 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1c954 69 6e 64 65 78 20 72 65 63 6f 72 64 2e 20 54 68 index record. Th
1c955 69 73 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6d is rowid value m
1c956 61 79 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 ay.** or may not
1c957 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 be the same as
1c958 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 R..**.** If any
1c959 6f 66 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 of the N registe
1c95a 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 rs beginning wit
1c95b 68 20 72 65 67 69 73 74 65 72 20 50 34 20 63 6f h register P4 co
1c95c 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a ntains a NULL.**
1c95d 20 76 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d value, jump imm
1c95e 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1c95f 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1c960 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1c961 6e 20 63 68 65 63 6b 73 20 69 66 20 63 75 72 73 n checks if curs
1c962 6f 72 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 or P1 contains a
1c963 6e 20 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 n entry.** where
1c964 20 74 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29 the first (N-1)
1c965 20 66 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75 fields match bu
1c966 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 t the rowid valu
1c967 65 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 e at the end.**
1c968 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 of the index ent
1c969 72 79 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 ry is not R. If
1c96a 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 there is no such
1c96b 20 65 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 entry, control
1c96c 6a 75 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 jumps.** to inst
1c96d 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 ruction P2. Othe
1c96e 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64 rwise, the rowid
1c96f 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 of the conflict
1c970 69 6e 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 ing index.** ent
1c971 72 79 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 ry is copied to
1c972 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 register P3 and
1c973 63 6f 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 control falls th
1c974 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1c975 74 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e t.** instruction
1c976 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c977 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 : NotFound, NotE
1c978 78 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a xists, Found.*/.
1c979 63 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 case OP_IsUnique
1c97a 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1c97b 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 75 31 36 mp, in3 */. u16
1c97c 20 69 69 3b 0a 20 20 56 64 62 65 43 75 72 73 6f ii;. VdbeCurso
1c97d 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73 r *pCx;. BtCurs
1c97e 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 31 36 or *pCrsr;. u16
1c97f 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65 6d 20 2a nField;. Mem *
1c980 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b aMem = &p->aMem[
1c981 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 0a 20 20 2f pOp->p4.i];.. /
1c982 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 * Assert that th
1c983 65 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 e values of para
1c984 6d 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 meters P1 and P4
1c985 20 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a are in range. *
1c986 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1c987 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 >p4type==P4_INT3
1c988 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2 );. assert( p
1c989 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f Op->p4.i>0 && pO
1c98a 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d p->p4.i<=p->nMem
1c98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1c98c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1c98d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1c98e 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 ;.. /* Find the
1c98f 20 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a index cursor. *
1c990 2f 0a 20 20 70 43 78 20 3d 20 70 2d 3e 61 70 43 /. pCx = p->apC
1c991 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1c992 73 73 65 72 74 28 20 70 43 78 2d 3e 64 65 66 65 ssert( pCx->defe
1c993 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1c994 0a 20 20 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 . pCx->seekResu
1c995 6c 74 20 3d 20 30 3b 0a 20 20 70 43 78 2d 3e 63 lt = 0;. pCx->c
1c996 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1c997 48 45 5f 53 54 41 4c 45 3b 0a 20 20 70 43 72 73 HE_STALE;. pCrs
1c998 72 20 3d 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 r = pCx->pCursor
1c999 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f ;.. /* If any o
1c99a 66 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 f the values are
1c99b 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 NULL, take the
1c99c 6a 75 6d 70 2e 20 2a 2f 0a 20 20 6e 46 69 65 6c jump. */. nFiel
1c99d 64 20 3d 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 d = pCx->pKeyInf
1c99e 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 o->nField;. for
1c99f 28 69 69 3d 30 3b 20 69 69 3c 6e 46 69 65 6c 64 (ii=0; ii<nField
1c9a0 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 ; ii++){. if(
1c9a1 20 61 4d 65 6d 5b 69 69 5d 2e 66 6c 61 67 73 20 aMem[ii].flags
1c9a2 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1c9a3 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1c9a4 20 2d 20 31 3b 0a 20 20 20 20 20 20 70 43 72 73 - 1;. pCrs
1c9a5 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 r = 0;. bre
1c9a6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
1c9a7 61 73 73 65 72 74 28 20 28 61 4d 65 6d 5b 6e 46 assert( (aMem[nF
1c9a8 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 20 4d 45 ield].flags & ME
1c9a9 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 M_Null)==0 );..
1c9aa 20 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b if( pCrsr!=0 ){
1c9ab 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 . UnpackedRec
1c9ac 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20 ord r;
1c9ad 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 /* B-Tre
1c9ae 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b e index search k
1c9af 65 79 20 2a 2f 0a 20 20 20 20 69 36 34 20 52 3b ey */. i64 R;
1c9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c9b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c9b2 52 6f 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 Rowid stored in
1c9b3 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 0a register P3 */..
1c9b4 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 /* Populate
1c9b5 74 68 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 the index search
1c9b6 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 72 2e 70 key. */. r.p
1c9b7 4b 65 79 49 6e 66 6f 20 3d 20 70 43 78 2d 3e 70 KeyInfo = pCx->p
1c9b8 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e KeyInfo;. r.n
1c9b9 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 20 2b Field = nField +
1c9ba 20 31 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 1;. r.flags
1c9bb 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 = UNPACKED_PREFI
1c9bc 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 72 2e X_SEARCH;. r.
1c9bd 61 4d 65 6d 20 3d 20 61 4d 65 6d 3b 0a 0a 20 20 aMem = aMem;..
1c9be 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 /* Extract the
1c9bf 20 76 61 6c 75 65 20 6f 66 20 52 20 66 72 6f 6d value of R from
1c9c0 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a 2f register P3. */
1c9c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1c9c2 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 MemIntegerify(pI
1c9c3 6e 33 29 3b 0a 20 20 20 20 52 20 3d 20 70 49 6e n3);. R = pIn
1c9c4 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20 3->u.i;.. /*
1c9c5 53 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65 Search the B-Tre
1c9c6 65 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63 e index. If no c
1c9c7 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 onflicting recor
1c9c8 64 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70 d is found, jump
1c9c9 0a 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f . ** to P2. O
1c9ca 74 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74 therwise, copy t
1c9cb 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1c9cc 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f conflicting reco
1c9cd 72 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67 rd to. ** reg
1c9ce 69 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c ister P3 and fal
1c9cf 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1c9d0 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1c9d1 6e 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 n. */. rc =
1c9d2 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1c9d3 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72 toUnpacked(pCrsr
1c9d4 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 70 43 78 , &r, 0, 0, &pCx
1c9d5 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 ->seekResult);.
1c9d6 20 20 20 69 66 28 20 28 72 2e 66 6c 61 67 73 20 if( (r.flags
1c9d7 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1c9d8 58 5f 53 45 41 52 43 48 29 20 7c 7c 20 72 2e 72 X_SEARCH) || r.r
1c9d9 6f 77 69 64 3d 3d 52 20 29 7b 0a 20 20 20 20 20 owid==R ){.
1c9da 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1c9db 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
1c9dc 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 pIn3->u.i =
1c9dd 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 r.rowid;. }.
1c9de 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1c9df 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69 * Opcode: NotExi
1c9e0 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a sts P1 P2 P3 * *
1c9e1 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63 .**.** Use the c
1c9e2 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 ontent of regist
1c9e3 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65 67 er P3 as a integ
1c9e4 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65 er key. If a re
1c9e5 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 cord .** with th
1c9e6 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 at key does not
1c9e7 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f exist in table o
1c9e8 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20 f P1, then jump
1c9e9 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68 to P2. .** If th
1c9ea 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78 e record does ex
1c9eb 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 ist, then fall t
1c9ec 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72 hru. The cursor
1c9ed 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69 is left .** poi
1c9ee 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63 nting to the rec
1c9ef 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74 73 ord if it exists
1c9f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 ..**.** The diff
1c9f1 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 erence between t
1c9f2 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e his operation an
1c9f3 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 d NotFound is th
1c9f4 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 at this.** opera
1c9f5 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 tion assumes the
1c9f6 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 key is an integ
1c9f7 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 er and that P1 i
1c9f8 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 s a table wherea
1c9f9 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73 s.** NotFound as
1c9fa 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62 sumes key is a b
1c9fb 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 lob constructed
1c9fc 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20 from MakeRecord
1c9fd 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 and.** P1 is an
1c9fe 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 index..**.** See
1c9ff 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1ca00 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 tFound, IsUnique
1ca01 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 .*/.case OP_NotE
1ca02 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20 xists: {
1ca03 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1ca04 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1ca05 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1ca06 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1ca07 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 *pCrsr;. assert
1ca08 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1ca09 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1ca0a 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 rt( p->apCsr[i]!
1ca0b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 =0 );. if( (pCr
1ca0c 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 sr = (pC = p->ap
1ca0d 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 Csr[i])->pCursor
1ca0e 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 )!=0 ){. int
1ca0f 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 36 34 res = 0;. u64
1ca10 20 69 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72 iKey;. asser
1ca11 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 t( pIn3->flags &
1ca12 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 MEM_Int );.
1ca13 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1ca14 5b 69 5d 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a [i]->isTable );.
1ca15 20 20 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f iKey = intTo
1ca16 4b 65 79 28 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a Key(pIn3->u.i);.
1ca17 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ca18 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1ca19 6b 65 64 28 70 43 72 73 72 2c 20 30 2c 20 69 4b ked(pCrsr, 0, iK
1ca1a 65 79 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 ey, 0, &res);.
1ca1b 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 pC->lastRowid
1ca1c 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn3->u.i;.
1ca1d 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
1ca1e 64 20 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b d = res==0 ?1:0;
1ca1f 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 . pC->nullRow
1ca20 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 = 0;. pC->ca
1ca21 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1ca22 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 70 43 2d E_STALE;. pC-
1ca23 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1ca24 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 65 73 = 0;. if( res
1ca25 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 !=0 ){. pc
1ca26 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1ca27 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d assert( pC-
1ca28 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 >rowidIsValid==0
1ca29 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 );. }. pC
1ca2a 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 72 ->seekResult = r
1ca2b 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 es;. }else if(
1ca2c 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 !pC->pseudoTable
1ca2d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
1ca2e 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 6e 20 happens when an
1ca2f 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 attempt to open
1ca30 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e a read cursor on
1ca31 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 71 6c the . ** sql
1ca32 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
1ca33 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1ca34 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 EMPTY.. */.
1ca35 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 assert( pC->is
1ca36 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 63 20 Table );. pc
1ca37 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1ca38 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 assert( pC->r
1ca39 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 owidIsValid==0 )
1ca3a 3b 0a 20 20 20 20 70 43 2d 3e 73 65 65 6b 52 65 ;. pC->seekRe
1ca3b 73 75 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 sult = 0;. }.
1ca3c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1ca3d 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 50 31 ode: Sequence P1
1ca3e 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1ca3f 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 61 76 Find the next av
1ca40 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e 63 65 ailable sequence
1ca41 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75 72 73 number for curs
1ca42 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74 65 20 or P1..** Write
1ca43 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d the sequence num
1ca44 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ber into registe
1ca45 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73 65 71 r P2..** The seq
1ca46 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f 6e 20 uence number on
1ca47 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e the cursor is in
1ca48 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65 72 20 cremented after
1ca49 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75 63 74 this.** instruct
1ca4a 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f ion. .*/.case O
1ca4b 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20 20 20 P_Sequence: {
1ca4c 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1ca4d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1ca4e 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1ca4f 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1ca50 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
1ca51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1ca52 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 apCsr[i]!=0 );.
1ca53 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d 3e pOut->u.i = p->
1ca54 61 70 43 73 72 5b 69 5d 2d 3e 73 65 71 43 6f 75 apCsr[i]->seqCou
1ca55 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 79 nt++;. MemSetTy
1ca56 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1ca57 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a _Int);. break;.
1ca58 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e }.../* Opcode: N
1ca59 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 33 ewRowid P1 P2 P3
1ca5a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 * *.**.** Get a
1ca5b 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 63 new integer rec
1ca5c 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e ord number (a.k.
1ca5d 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 20 a "rowid") used
1ca5e 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 20 as the key to a
1ca5f 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65 table..** The re
1ca60 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 6e cord number is n
1ca61 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 73 ot previously us
1ca62 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 74 ed as a key in t
1ca63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 he database.** t
1ca64 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f 72 able that cursor
1ca65 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 P1 points to.
1ca66 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e The new record n
1ca67 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 6e umber is written
1ca68 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 72 .** written to r
1ca69 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a egister P2..**.*
1ca6a 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 50 * If P3>0 then P
1ca6b 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20 3 is a register
1ca6c 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6c that holds the l
1ca6d 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c argest previousl
1ca6e 79 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 y.** generated r
1ca6f 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 4e ecord number. N
1ca70 6f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d o new record num
1ca71 62 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 bers are allowed
1ca72 20 74 6f 20 62 65 20 6c 65 73 73 0a 2a 2a 20 74 to be less.** t
1ca73 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20 han this value.
1ca74 20 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 When this value
1ca75 20 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 reaches its max
1ca76 69 6d 75 6d 2c 20 61 20 53 51 4c 49 54 45 5f 46 imum, a SQLITE_F
1ca77 55 4c 4c 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 ULL.** error is
1ca78 67 65 6e 65 72 61 74 65 64 2e 20 20 54 68 65 20 generated. The
1ca79 50 33 20 72 65 67 69 73 74 65 72 20 69 73 20 75 P3 register is u
1ca7a 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 pdated with the
1ca7b 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 72 65 63 generated.** rec
1ca7c 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 ord number. Thi
1ca7d 73 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69 s P3 mechanism i
1ca7e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 s used to help i
1ca7f 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 mplement the.**
1ca80 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65 AUTOINCREMENT fe
1ca81 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f ature..*/.case O
1ca82 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20 P_NewRowid: {
1ca83 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1ca84 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1ca85 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1ca86 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 . i64 v = 0;.
1ca87 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1ca88 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1ca89 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1ca8a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1ca8b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1ca8c 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 if( (pC = p->apC
1ca8d 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 3d sr[i])->pCursor=
1ca8e 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =0 ){. /* The
1ca8f 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 zero initializa
1ca90 74 69 6f 6e 20 61 62 6f 76 65 20 69 73 20 61 6c tion above is al
1ca91 6c 20 74 68 61 74 20 69 73 20 6e 65 65 64 65 64 l that is needed
1ca92 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 */. }else{.
1ca93 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 /* The next row
1ca94 69 64 20 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d id or record num
1ca95 62 65 72 20 28 64 69 66 66 65 72 65 6e 74 20 74 ber (different t
1ca96 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 61 6d erms for the sam
1ca97 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 e. ** thing)
1ca98 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 is obtained in a
1ca99 20 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 two-step algori
1ca9a 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 thm.. **.
1ca9b 2a 2a 20 46 69 72 73 74 20 77 65 20 61 74 74 65 ** First we atte
1ca9c 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 20 mpt to find the
1ca9d 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 largest existing
1ca9e 20 72 6f 77 69 64 20 61 6e 64 20 61 64 64 20 6f rowid and add o
1ca9f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 ne. ** to tha
1caa0 74 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6c t. But if the l
1caa1 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 argest existing
1caa2 72 6f 77 69 64 20 69 73 20 61 6c 72 65 61 64 79 rowid is already
1caa3 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 the maximum.
1caa4 20 2a 2a 20 70 6f 73 69 74 69 76 65 20 69 6e 74 ** positive int
1caa5 65 67 65 72 2c 20 77 65 20 68 61 76 65 20 74 6f eger, we have to
1caa6 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f fall through to
1caa7 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 the second.
1caa8 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 ** probabilistic
1caa9 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a algorithm. *
1caaa 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 *. ** The sec
1caab 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 ond algorithm is
1caac 20 74 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77 to select a row
1caad 69 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 id at random and
1caae 20 73 65 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 see if. ** i
1caaf 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
1cab0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
1cab1 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 If it does not e
1cab2 78 69 73 74 2c 20 77 65 20 68 61 76 65 0a 20 20 xist, we have.
1cab3 20 20 2a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 ** succeeded.
1cab4 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 If the random r
1cab5 6f 77 69 64 20 64 6f 65 73 20 65 78 69 73 74 2c owid does exist,
1cab6 20 77 65 20 73 65 6c 65 63 74 20 61 20 6e 65 77 we select a new
1cab7 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 one. ** and
1cab8 74 72 79 20 61 67 61 69 6e 2c 20 75 70 20 74 6f try again, up to
1cab9 20 31 30 30 30 20 74 69 6d 65 73 2e 0a 20 20 20 1000 times..
1caba 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20 61 **. ** For a
1cabb 20 74 61 62 6c 65 20 77 69 74 68 20 6c 65 73 73 table with less
1cabc 20 74 68 61 6e 20 32 20 62 69 6c 6c 69 6f 6e 20 than 2 billion
1cabd 65 6e 74 72 69 65 73 2c 20 74 68 65 20 70 72 6f entries, the pro
1cabe 62 61 62 69 6c 69 74 79 0a 20 20 20 20 2a 2a 20 bability. **
1cabf 6f 66 20 6e 6f 74 20 66 69 6e 64 69 6e 67 20 61 of not finding a
1cac0 20 75 6e 75 73 65 64 20 72 6f 77 69 64 20 69 73 unused rowid is
1cac1 20 61 62 6f 75 74 20 31 2e 30 65 2d 33 30 30 2e about 1.0e-300.
1cac2 20 20 54 68 69 73 20 69 73 20 61 20 0a 20 20 20 This is a .
1cac3 20 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f ** non-zero pro
1cac4 62 61 62 69 6c 69 74 79 2c 20 62 75 74 20 69 74 bability, but it
1cac5 20 69 73 20 73 74 69 6c 6c 20 76 61 6e 69 73 68 is still vanish
1cac6 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 61 6e 64 20 ingly small and
1cac7 73 68 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 6e 65 should. ** ne
1cac8 76 65 72 20 63 61 75 73 65 20 61 20 70 72 6f 62 ver cause a prob
1cac9 6c 65 6d 2e 20 20 59 6f 75 20 61 72 65 20 6d 75 lem. You are mu
1caca 63 68 2c 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 ch, much more li
1cacb 6b 65 6c 79 20 74 6f 20 68 61 76 65 20 61 0a 20 kely to have a.
1cacc 20 20 20 2a 2a 20 68 61 72 64 77 61 72 65 20 66 ** hardware f
1cacd 61 69 6c 75 72 65 20 74 68 61 6e 20 66 6f 72 20 ailure than for
1cace 74 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 74 this algorithm t
1cacf 6f 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20 o fail.. **.
1cad0 20 20 20 2a 2a 20 54 68 65 20 61 6e 61 6c 79 73 ** The analys
1cad1 69 73 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f is in the previo
1cad2 75 73 20 70 61 72 61 67 72 61 70 68 20 61 73 73 us paragraph ass
1cad3 75 6d 65 73 20 74 68 61 74 20 79 6f 75 20 68 61 umes that you ha
1cad4 76 65 20 61 20 67 6f 6f 64 0a 20 20 20 20 2a 2a ve a good. **
1cad5 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f source of rando
1cad6 6d 20 6e 75 6d 62 65 72 73 2e 20 20 49 73 20 61 m numbers. Is a
1cad7 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
1cad8 6e 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 n like lrand48()
1cad9 0a 20 20 20 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f . ** good eno
1cada 75 67 68 3f 20 20 4d 61 79 62 65 2e 20 4d 61 79 ugh? Maybe. May
1cadb 62 65 20 6e 6f 74 2e 20 49 74 27 73 20 68 61 72 be not. It's har
1cadc 64 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 d to know whethe
1cadd 72 20 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 6d r there. ** m
1cade 69 67 68 74 20 62 65 20 73 75 62 74 6c 65 20 62 ight be subtle b
1cadf 75 67 73 20 69 73 20 73 6f 6d 65 20 69 6d 70 6c ugs is some impl
1cae0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 6c ementations of l
1cae1 72 61 6e 64 34 38 28 29 20 74 68 61 74 0a 20 20 rand48() that.
1cae2 20 20 2a 2a 20 63 6f 75 6c 64 20 63 61 75 73 65 ** could cause
1cae3 20 70 72 6f 62 6c 65 6d 73 2e 20 54 6f 20 61 76 problems. To av
1cae4 6f 69 64 20 75 6e 63 65 72 74 61 69 6e 74 79 2c oid uncertainty,
1cae5 20 53 51 4c 69 74 65 20 75 73 65 73 20 69 74 73 SQLite uses its
1cae6 20 6f 77 6e 20 0a 20 20 20 20 2a 2a 20 72 61 6e own . ** ran
1cae7 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 dom number gener
1cae8 61 74 6f 72 20 62 61 73 65 64 20 6f 6e 20 74 68 ator based on th
1cae9 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 2e e RC4 algorithm.
1caea 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1caeb 6f 20 70 72 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69 o promote locali
1caec 74 79 20 6f 66 20 72 65 66 65 72 65 6e 63 65 20 ty of reference
1caed 66 6f 72 20 72 65 70 65 74 69 74 69 76 65 20 69 for repetitive i
1caee 6e 73 65 72 74 73 2c 20 74 68 65 0a 20 20 20 20 nserts, the.
1caef 2a 2a 20 66 69 72 73 74 20 66 65 77 20 61 74 74 ** first few att
1caf0 65 6d 70 74 73 20 61 74 20 63 68 6f 6f 73 69 6e empts at choosin
1caf1 67 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 g a random rowid
1caf2 20 70 69 63 6b 20 76 61 6c 75 65 73 20 6a 75 73 pick values jus
1caf3 74 20 61 20 6c 69 74 74 6c 65 0a 20 20 20 20 2a t a little. *
1caf4 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 * larger than th
1caf5 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 69 64 e previous rowid
1caf6 2e 20 20 54 68 69 73 20 68 61 73 20 62 65 65 6e . This has been
1caf7 20 73 68 6f 77 6e 20 65 78 70 65 72 69 6d 65 6e shown experimen
1caf8 74 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 74 6f 20 tally. ** to
1caf9 64 6f 75 62 6c 65 20 74 68 65 20 73 70 65 65 64 double the speed
1cafa 20 6f 66 20 74 68 65 20 43 4f 50 59 20 6f 70 65 of the COPY ope
1cafb 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 ration.. */.
1cafc 20 20 20 69 6e 74 20 72 65 73 3d 30 2c 20 72 78 int res=0, rx
1cafd 3d 53 51 4c 49 54 45 5f 4f 4b 2c 20 63 6e 74 3b =SQLITE_OK, cnt;
1cafe 0a 20 20 20 20 69 36 34 20 78 3b 0a 20 20 20 20 . i64 x;.
1caff 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 cnt = 0;. if(
1cb00 20 28 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c (sqlite3BtreeFl
1cb01 61 67 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 ags(pC->pCursor)
1cb02 26 28 42 54 52 45 45 5f 49 4e 54 4b 45 59 7c 42 &(BTREE_INTKEY|B
1cb03 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 29 20 TREE_ZERODATA))
1cb04 21 3d 0a 20 20 20 20 20 20 20 20 20 20 42 54 52 !=. BTR
1cb05 45 45 5f 49 4e 54 4b 45 59 20 29 7b 0a 20 20 20 EE_INTKEY ){.
1cb06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1cb07 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1cb08 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1cb09 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1cb0a 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73 }. assert( (s
1cb0b 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 qlite3BtreeFlags
1cb0c 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 20 26 20 (pC->pCursor) &
1cb0d 42 54 52 45 45 5f 49 4e 54 4b 45 59 29 21 3d 30 BTREE_INTKEY)!=0
1cb0e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1cb0f 28 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 (sqlite3BtreeFla
1cb10 67 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 20 gs(pC->pCursor)
1cb11 26 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 & BTREE_ZERODATA
1cb12 29 3d 3d 30 20 29 3b 0a 0a 23 69 66 64 65 66 20 )==0 );..#ifdef
1cb13 53 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 SQLITE_32BIT_ROW
1cb14 49 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 ID.# define MA
1cb15 58 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66 X_ROWID 0x7fffff
1cb16 66 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 ff.#else. /*
1cb17 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63 Some compilers c
1cb18 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f omplain about co
1cb19 6e 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66 nstants of the f
1cb1a 6f 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66 orm 0x7fffffffff
1cb1b 66 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f ffffff.. ** O
1cb1c 74 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 thers complain a
1cb1d 62 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66 bout 0x7ffffffff
1cb1e 66 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65 ffffffffLL. The
1cb1f 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
1cb20 20 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f seems. ** to
1cb21 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e provide the con
1cb22 73 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69 stant while maki
1cb23 6e 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 ng all compilers
1cb24 20 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 happy.. */.#
1cb25 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f define MAX_RO
1cb26 57 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75 WID (i64)( (((u
1cb27 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 64)0x7fffffff)<<
1cb28 33 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66 32) | (u64)0xfff
1cb29 66 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a fffff ).#endif..
1cb2a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 75 73 65 if( !pC->use
1cb2b 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 RandomRowid ){.
1cb2c 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 v = sqlite3
1cb2d 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f BtreeGetCachedRo
1cb2e 77 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 wid(pC->pCursor)
1cb2f 3b 0a 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30 ;. if( v==0
1cb30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1cb31 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 sqlite3BtreeLas
1cb32 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 t(pC->pCursor, &
1cb33 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 res);. if
1cb34 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1cb35 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
1cb36 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1cb37 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a rror;. }.
1cb38 20 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20 if( res
1cb39 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76 20 3d ){. v =
1cb3a 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1;. }els
1cb3b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c e{. sql
1cb3c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1cb3d 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76 (pC->pCursor, &v
1cb3e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 76 20 3d );. v =
1cb3f 20 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20 keyToInt(v);.
1cb40 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 4d if( v==M
1cb41 41 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 AX_ROWID ){.
1cb42 20 20 20 20 20 20 20 20 70 43 2d 3e 75 73 65 52 pC->useR
1cb43 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a andomRowid = 1;.
1cb44 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1cb45 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2b 2b . v++
1cb46 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1cb47 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1cb48 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1cb49 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 OMIT_AUTOINCREME
1cb4a 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 NT. if( pOp
1cb4b 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20 20 ->p3 ){.
1cb4c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 Mem *pMem;.
1cb4d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1cb4e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1cb4f 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 20 2f 2a 20 50 =p->nMem ); /* P
1cb50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 3 is a valid mem
1cb51 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 ory cell */.
1cb52 20 20 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 pMem = &p->a
1cb53 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 09 52 Mem[pOp->p3];..R
1cb54 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1cb55 70 2d 3e 70 33 2c 20 70 4d 65 6d 29 3b 0a 20 20 p->p3, pMem);.
1cb56 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1cb57 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1cb58 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73 Mem);. as
1cb59 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
1cb5a 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 gs & MEM_Int)!=0
1cb5b 20 29 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29 20 ); /* mem(P3)
1cb5c 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 holds an integer
1cb5d 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1cb5e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52 pMem->u.i==MAX_R
1cb5f 4f 57 49 44 20 7c 7c 20 70 43 2d 3e 75 73 65 52 OWID || pC->useR
1cb60 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 andomRowid ){.
1cb61 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1cb62 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 ITE_FULL;.
1cb63 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1cb64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1cb65 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1cb66 66 28 20 76 3c 70 4d 65 6d 2d 3e 75 2e 69 2b 31 f( v<pMem->u.i+1
1cb67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76 20 ){. v
1cb68 3d 20 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b = pMem->u.i + 1;
1cb69 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1cb6a 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 pMem->u.i = v
1cb6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
1cb6c 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 .. sqlite3B
1cb6d 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 treeSetCachedRow
1cb6e 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 id(pC->pCursor,
1cb6f 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f 20 76 2b v<MAX_ROWID ? v+
1cb70 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 1 : 0);. }.
1cb71 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e if( pC->useRan
1cb72 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1cb73 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1cb74 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49 3==0 ); /* SQLI
1cb75 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76 TE_FULL must hav
1cb76 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 e occurred prior
1cb77 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20 to this */.
1cb78 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e v = db->priorN
1cb79 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 ewRowid;. c
1cb7a 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f nt = 0;. do
1cb7b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e {. if( cn
1cb7c 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66 t==0 && (v&0xfff
1cb7d 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20 fff)==v ){.
1cb7e 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 v++;.
1cb7f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1cb80 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
1cb81 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c mness(sizeof(v),
1cb82 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 &v);.
1cb83 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d if( cnt<5 ) v &=
1cb84 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 0xffffff;.
1cb85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1cb86 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 v==0 ) continue
1cb87 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e ;. x = in
1cb88 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20 tToKey(v);.
1cb89 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42 rx = sqlite3B
1cb8a 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1cb8b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 ed(pC->pCursor,
1cb8c 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72 0, (u64)x, 0, &r
1cb8d 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 es);. cnt
1cb8e 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 ++;. }while
1cb8f 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d ( cnt<100 && rx=
1cb90 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 =SQLITE_OK && re
1cb91 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 s==0 );. db
1cb92 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 ->priorNewRowid
1cb93 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72 = v;. if( r
1cb94 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 x==SQLITE_OK &&
1cb95 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1cb96 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
1cb97 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f LL;. goto
1cb98 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1cb99 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
1cb9a 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 }. pC->rowid
1cb9b 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1cb9c 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1cb9d 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d eto = 0;. pC-
1cb9e 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1cb9f 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a ACHE_STALE;. }.
1cba0 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1cba1 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1cba2 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 . pOut->u.i = v
1cba3 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cba4 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20 Opcode: Insert
1cba5 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1cba6 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e *.** Write an en
1cba7 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62 try into the tab
1cba8 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e le of cursor P1.
1cba9 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 A new entry is
1cbaa 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69 .** created if i
1cbab 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64 t doesn't alread
1cbac 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64 y exist or the d
1cbad 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74 ata for an exist
1cbae 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 ing.** entry is
1cbaf 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68 overwritten. Th
1cbb0 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61 e data is the va
1cbb1 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73 lue stored regis
1cbb2 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 ter.** number P2
1cbb3 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f . The key is sto
1cbb4 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1cbb5 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 P3. The key must
1cbb6 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65 .** be an intege
1cbb7 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
1cbb8 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 OPFLAG_NCHANGE f
1cbb9 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1cbba 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 , then the row c
1cbbb 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a hange count is.*
1cbbc 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f * incremented (o
1cbbd 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 therwise not).
1cbbe 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 If the OPFLAG_LA
1cbbf 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 STROWID flag of
1cbc0 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 P5 is set,.** th
1cbc1 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72 en rowid is stor
1cbc2 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e ed for subsequen
1cbc3 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a t return by the.
1cbc4 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f ** sqlite3_last_
1cbc5 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66 insert_rowid() f
1cbc6 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 unction (otherwi
1cbc7 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 se it is unmodif
1cbc8 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 ied)..**.** Para
1cbc9 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 meter P4 may poi
1cbca 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 nt to a string c
1cbcb 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
1cbcc 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 ble-name, or.**
1cbcd 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 may be NULL. If
1cbce 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 it is not NULL,
1cbcf 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d then the update-
1cbd0 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 hook .** (sqlite
1cbd1 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 3.xUpdateCallbac
1cbd2 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f k) is invoked fo
1cbd3 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 llowing a succes
1cbd4 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a sful insert..**.
1cbd5 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f ** (WARNING/TODO
1cbd6 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 : If P1 is a pse
1cbd7 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 udo-cursor and P
1cbd8 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2 is dynamically
1cbd9 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 .** allocated, t
1cbda 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 hen ownership of
1cbdb 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 P2 is transferr
1cbdc 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f ed to the pseudo
1cbdd 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 -cursor.** and r
1cbde 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d egister P2 becom
1cbdf 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 es ephemeral. I
1cbe0 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
1cbe1 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 changed, the.**
1cbe2 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1cbe3 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 r P2 will then c
1cbe4 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 hange. Make sur
1cbe5 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a e this does not.
1cbe6 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f ** cause any pro
1cbe7 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 blems.).**.** Th
1cbe8 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1cbe9 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 nly works on tab
1cbea 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 les. The equiva
1cbeb 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e lent instruction
1cbec 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 .** for indices
1cbed 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e is OP_IdxInsert.
1cbee 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 .*/.case OP_Inse
1cbef 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 rt: {. Mem *pDa
1cbf0 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ta = &p->aMem[pO
1cbf1 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 p->p2];. Mem *p
1cbf2 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Key = &p->aMem[p
1cbf3 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20 Op->p3];.. i64
1cbf4 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69 iKey; /* The i
1cbf5 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 nteger ROWID or
1cbf6 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f key for the reco
1cbf7 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 rd to be inserte
1cbf8 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 d */. int i = p
1cbf9 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
1cbfa 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1cbfb 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1cbfc 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 >nCursor );. pC
1cbfd 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a = p->apCsr[i];.
1cbfe 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 assert( pC!=0
1cbff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d );. assert( pC-
1cc00 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70 >pCursor!=0 || p
1cc01 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 C->pseudoTable )
1cc02 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
1cc03 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1cc04 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
1cc05 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1cc06 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1cc07 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a Op->p2, pData);.
1cc08 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cc09 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b (pOp->p3, pKey);
1cc0a 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f .. iKey = intTo
1cc0b 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a Key(pKey->u.i);.
1cc0c 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1cc0d 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1cc0e 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1cc0f 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f if( pOp->p5 & O
1cc10 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 PFLAG_LASTROWID
1cc11 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 ) db->lastRowid
1cc12 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 = pKey->u.i;. i
1cc13 66 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 f( pData->flags
1cc14 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1cc15 20 20 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a pData->z = 0;.
1cc16 20 20 20 20 70 44 61 74 61 2d 3e 6e 20 3d 20 30 pData->n = 0
1cc17 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1cc18 73 73 65 72 74 28 20 70 44 61 74 61 2d 3e 66 6c ssert( pData->fl
1cc19 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c ags & (MEM_Blob|
1cc1a 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 20 20 7d 0a MEM_Str) );. }.
1cc1b 20 20 69 66 28 20 70 43 2d 3e 70 73 65 75 64 6f if( pC->pseudo
1cc1c 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 Table ){. if(
1cc1d 20 21 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 !pC->ephemPseud
1cc1e 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 oTable ){.
1cc1f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1cc20 2c 20 70 43 2d 3e 70 44 61 74 61 29 3b 0a 20 20 , pC->pData);.
1cc21 20 20 7d 0a 20 20 20 20 70 43 2d 3e 69 4b 65 79 }. pC->iKey
1cc22 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 43 2d = iKey;. pC-
1cc23 3e 6e 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e >nData = pData->
1cc24 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 n;. if( pData
1cc25 2d 3e 7a 3d 3d 70 44 61 74 61 2d 3e 7a 4d 61 6c ->z==pData->zMal
1cc26 6c 6f 63 20 7c 7c 20 70 43 2d 3e 65 70 68 65 6d loc || pC->ephem
1cc27 50 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 PseudoTable ){.
1cc28 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d pC->pData =
1cc29 20 70 44 61 74 61 2d 3e 7a 3b 0a 20 20 20 20 20 pData->z;.
1cc2a 20 69 66 28 20 21 70 43 2d 3e 65 70 68 65 6d 50 if( !pC->ephemP
1cc2b 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 seudoTable ){.
1cc2c 20 20 20 20 20 20 70 44 61 74 61 2d 3e 66 6c 61 pData->fla
1cc2d 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a gs &= ~MEM_Dyn;.
1cc2e 20 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 66 pData->f
1cc2f 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 lags |= MEM_Ephe
1cc30 6d 3b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61 m;. pData
1cc31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
1cc32 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
1cc33 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 {. pC->pDat
1cc34 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f a = sqlite3Mallo
1cc35 63 28 20 70 43 2d 3e 6e 44 61 74 61 2b 32 20 29 c( pC->nData+2 )
1cc36 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 2d ;. if( !pC-
1cc37 3e 70 44 61 74 61 20 29 20 67 6f 74 6f 20 6e 6f >pData ) goto no
1cc38 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 6d 65 6d 63 _mem;. memc
1cc39 70 79 28 70 43 2d 3e 70 44 61 74 61 2c 20 70 44 py(pC->pData, pD
1cc3a 61 74 61 2d 3e 7a 2c 20 70 43 2d 3e 6e 44 61 74 ata->z, pC->nDat
1cc3b 61 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 a);. pC->pD
1cc3c 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61 5d 20 3d ata[pC->nData] =
1cc3d 20 30 3b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 0;. pC->pD
1cc3e 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61 2b 31 5d ata[pC->nData+1]
1cc3f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1cc40 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b pC->nullRow = 0;
1cc41 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
1cc42 74 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 6e 74 t nZero;. int
1cc43 20 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 seekResult = ((
1cc44 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1cc45 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 _USESEEKRESULT)
1cc46 3f 20 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 ? pC->seekResult
1cc47 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 : 0);. if( p
1cc48 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 Data->flags & ME
1cc49 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1cc4a 6e 5a 65 72 6f 20 3d 20 70 44 61 74 61 2d 3e 75 nZero = pData->u
1cc4b 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 .nZero;. }els
1cc4c 65 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d e{. nZero =
1cc4d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0;. }. sq
1cc4e 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1cc4f 68 65 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 hedRowid(pC->pCu
1cc50 72 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 72 63 rsor, 0);. rc
1cc51 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 = sqlite3BtreeI
1cc52 6e 73 65 72 74 28 70 43 2d 3e 70 43 75 72 73 6f nsert(pC->pCurso
1cc53 72 2c 20 30 2c 20 69 4b 65 79 2c 0a 20 20 20 20 r, 0, iKey,.
1cc54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc55 20 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a pData->z
1cc56 2c 20 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a 65 72 , pData->n, nZer
1cc57 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 o,.
1cc58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1cc59 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1cc5a 41 50 50 45 4e 44 2c 20 73 65 65 6b 52 65 73 75 APPEND, seekResu
1cc5b 6c 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 lt. );. }.
1cc5c 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 . pC->rowidIsVa
1cc5d 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 64 lid = 0;. pC->d
1cc5e 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1cc5f 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 0;. pC->cacheSt
1cc60 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1cc61 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 LE;.. /* Invoke
1cc62 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
1cc63 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f if required. */
1cc64 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1cc65 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 E_OK && db->xUpd
1cc66 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1cc67 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1cc68 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 const char *zDb
1cc69 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 = db->aDb[pC->iD
1cc6a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f b].zName;. co
1cc6b 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d nst char *zTbl =
1cc6c 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 pOp->p4.z;.
1cc6d 69 6e 74 20 6f 70 20 3d 20 28 28 70 4f 70 2d 3e int op = ((pOp->
1cc6e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 50 p5 & OPFLAG_ISUP
1cc6f 44 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f 55 DATE) ? SQLITE_U
1cc70 50 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f 49 PDATE : SQLITE_I
1cc71 4e 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73 65 NSERT);. asse
1cc72 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 rt( pC->isTable
1cc73 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 );. db->xUpda
1cc74 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 teCallback(db->p
1cc75 55 70 64 61 74 65 41 72 67 2c 20 6f 70 2c 20 7a UpdateArg, op, z
1cc76 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b Db, zTbl, iKey);
1cc77 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1cc78 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 >iDb>=0 );. }.
1cc79 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cc7a 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31 20 code: Delete P1
1cc7b 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1cc7c 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f 72 Delete the recor
1cc7d 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20 50 d at which the P
1cc7e 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 1 cursor is curr
1cc7f 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a ently pointing..
1cc80 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 **.** The cursor
1cc81 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f will be left po
1cc82 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 72 inting at either
1cc83 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 65 the next or the
1cc84 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 previous.** rec
1cc85 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ord in the table
1cc86 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74 20 . If it is left
1cc87 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 pointing at the
1cc88 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68 65 next record, the
1cc89 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 n.** the next Ne
1cc8a 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 xt instruction w
1cc8b 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 ill be a no-op.
1cc8c 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 Hence it is OK
1cc8d 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 to delete.** a r
1cc8e 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68 69 ecord from withi
1cc8f 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a n an Next loop..
1cc90 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 **.** If the OPF
1cc91 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 LAG_NCHANGE flag
1cc92 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20 74 of P2 is set, t
1cc93 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e hen the row chan
1cc94 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 ge count is.** i
1cc95 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 ncremented (othe
1cc96 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a rwise not)..**.*
1cc97 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 * P1 must not be
1cc98 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20 pseudo-table.
1cc99 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20 72 It has to be a r
1cc9a 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a 2a eal table with.*
1cc9b 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e * multiple rows.
1cc9c 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 .**.** If P4 is
1cc9d 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 not NULL, then i
1cc9e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 t is the name of
1cc9f 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
1cca0 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e P1 is.** pointin
1cca1 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61 74 g to. The updat
1cca2 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 e hook will be i
1cca3 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65 78 nvoked, if it ex
1cca4 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69 ists..** If P4 i
1cca5 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 s not NULL then
1cca6 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 the P1 cursor mu
1cca7 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f 73 st have been pos
1cca8 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67 itioned.** using
1cca9 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 OP_NotFound pri
1ccaa 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
1ccab 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 his opcode..*/.c
1ccac 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b ase OP_Delete: {
1ccad 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1ccae 70 31 3b 0a 20 20 69 36 34 20 69 4b 65 79 20 3d p1;. i64 iKey =
1ccaf 20 30 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 0;. VdbeCursor
1ccb0 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 *pC;.. assert(
1ccb1 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 i>=0 && i<p->nC
1ccb2 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 ursor );. pC =
1ccb3 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 p->apCsr[i];. a
1ccb4 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a ssert( pC!=0 );.
1ccb5 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 assert( pC->pC
1ccb6 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 ursor!=0 ); /*
1ccb7 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 72 Only valid for r
1ccb8 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 eal tables, no p
1ccb9 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a seudotables */..
1ccba 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 64 61 /* If the upda
1ccbb 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 te-hook will be
1ccbc 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 invoked, set iKe
1ccbd 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f y to the rowid o
1ccbe 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 f the. ** row b
1ccbf 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 eing deleted..
1ccc0 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 */. if( db->xUp
1ccc1 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 dateCallback &&
1ccc2 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 pOp->p4.z ){.
1ccc3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 assert( pC->isT
1ccc4 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 able );. asse
1ccc5 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 rt( pC->rowidIsV
1ccc6 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 alid ); /* last
1ccc7 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72 65 Rowid set by pre
1ccc8 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e vious OP_NotFoun
1ccc9 64 20 2a 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20 d */. iKey =
1ccca 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 pC->lastRowid;.
1cccb 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }.. rc = sqlit
1cccc 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1cccd 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63 to(pC);. if( rc
1ccce 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1cccf 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 73 71 e_to_error;. sq
1ccd0 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1ccd1 68 65 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 hedRowid(pC->pCu
1ccd2 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d rsor, 0);. rc =
1ccd3 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
1ccd4 65 74 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 ete(pC->pCursor)
1ccd5 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 ;. pC->cacheSta
1ccd6 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1ccd7 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 E;.. /* Invoke
1ccd8 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 the update-hook
1ccd9 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a if required. */.
1ccda 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ccdb 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 _OK && db->xUpda
1ccdc 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f teCallback && pO
1ccdd 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 p->p4.z ){. c
1ccde 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d onst char *zDb =
1ccdf 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 db->aDb[pC->iDb
1cce0 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e ].zName;. con
1cce1 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 st char *zTbl =
1cce2 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 pOp->p4.z;. d
1cce3 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 b->xUpdateCallba
1cce4 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72 ck(db->pUpdateAr
1cce5 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 g, SQLITE_DELETE
1cce6 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 , zDb, zTbl, iKe
1cce7 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 y);. assert(
1cce8 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 pC->iDb>=0 );.
1cce9 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 }. if( pOp->p2
1ccea 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 & OPFLAG_NCHANGE
1cceb 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b ) p->nChange++;
1ccec 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cced 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 Opcode: ResetCou
1ccee 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 nt P1 * *.**.**
1ccef 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 73 65 This opcode rese
1ccf0 74 73 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72 ts the VMs inter
1ccf1 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 nal change count
1ccf2 65 72 20 74 6f 20 30 2e 20 49 66 20 50 31 20 69 er to 0. If P1 i
1ccf3 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 s true,.** then
1ccf4 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1ccf5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1ccf6 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 is copied to the
1ccf7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1ccf8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 .** change count
1ccf9 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20 er (returned by
1ccfa 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1ccfb 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e to sqlite3_chan
1ccfc 67 65 73 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65 ges()).** before
1ccfd 20 69 74 20 69 73 20 72 65 73 65 74 2e 20 54 68 it is reset. Th
1ccfe 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 is is used by tr
1ccff 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a igger programs..
1cd00 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74 */.case OP_Reset
1cd01 43 6f 75 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70 Count: {. if( p
1cd02 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 Op->p1 ){. sq
1cd03 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1cd04 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
1cd05 67 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 ge);. }. p->nC
1cd06 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 hange = 0;. bre
1cd07 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1cd08 3a 20 52 6f 77 44 61 74 61 20 50 31 20 50 32 20 : RowData P1 P2
1cd09 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1cd0a 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1cd0b 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 P2 the complete
1cd0c 72 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 72 row data for cur
1cd0d 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 sor P1..** There
1cd0e 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 is no interpret
1cd0f 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 ation of the dat
1cd10 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 a. .** It is ju
1cd11 73 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 st copied onto t
1cd12 68 65 20 50 32 20 72 65 67 69 73 74 65 72 20 65 he P2 register e
1cd13 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 xactly as .** it
1cd14 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 is found in the
1cd15 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
1cd16 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 **.** If the P1
1cd17 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 cursor must be p
1cd18 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c ointing to a val
1cd19 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 id row (not a NU
1cd1a 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 LL row).** of a
1cd1b 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 real table, not
1cd1c 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a a pseudo-table..
1cd1d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f */./* Opcode: Ro
1cd1e 77 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20 2a wKey P1 P2 * * *
1cd1f 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 .**.** Write int
1cd20 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 o register P2 th
1cd21 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b e complete row k
1cd22 65 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 ey for cursor P1
1cd23 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f ..** There is no
1cd24 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 interpretation
1cd25 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a of the data. .*
1cd26 2a 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f 70 * The key is cop
1cd27 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 ied onto the P3
1cd28 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 register exactly
1cd29 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f as .** it is fo
1cd2a 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 und in the datab
1cd2b 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
1cd2c 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 If the P1 cursor
1cd2d 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e must be pointin
1cd2e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 g to a valid row
1cd2f 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 (not a NULL row
1cd30 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 ).** of a real t
1cd31 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1cd32 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 do-table..*/.cas
1cd33 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 e OP_RowKey:.cas
1cd34 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a e OP_RowData: {.
1cd35 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1cd36 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1cd37 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1cd38 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b *pCrsr;. u32 n;
1cd39 0a 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 .. pOut = &p->a
1cd3a 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 Mem[pOp->p2];..
1cd3b 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f /* Note that Ro
1cd3c 77 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 wKey and RowData
1cd3d 20 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 are really exac
1cd3e 74 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 tly the same ins
1cd3f 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 truction */. as
1cd40 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1cd41 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1cd42 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1cd43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e ;. assert( pC->
1cd44 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e isTable || pOp->
1cd45 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65 opcode==OP_RowKe
1cd46 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 y );. assert( p
1cd47 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f C->isIndex || pO
1cd48 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f p->opcode==OP_Ro
1cd49 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 wData );. asser
1cd4a 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 t( pC!=0 );. as
1cd4b 73 65 72 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f sert( pC->nullRo
1cd4c 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 w==0 );. assert
1cd4d 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c ( pC->pseudoTabl
1cd4e 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e==0 );. assert
1cd4f 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 ( pC->pCursor!=0
1cd50 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43 );. pCrsr = pC
1cd51 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 72 63 20 ->pCursor;. rc
1cd52 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
1cd53 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 sorMoveto(pC);.
1cd54 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 if( rc ) goto a
1cd55 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1cd56 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 69 73 49 r;. if( pC->isI
1cd57 6e 64 65 78 20 29 7b 0a 20 20 20 20 69 36 34 20 ndex ){. i64
1cd58 6e 36 34 3b 0a 20 20 20 20 61 73 73 65 72 74 28 n64;. assert(
1cd59 20 21 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b !pC->isTable );
1cd5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1cd5b 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20 eKeySize(pCrsr,
1cd5c 26 6e 36 34 29 3b 0a 20 20 20 20 69 66 28 20 6e &n64);. if( n
1cd5d 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 64>db->aLimit[SQ
1cd5e 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1cd5f 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f H] ){. goto
1cd60 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a too_big;. }.
1cd61 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 6e 36 34 n = (int)n64
1cd62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
1cd63 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 qlite3BtreeDataS
1cd64 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 29 3b 0a ize(pCrsr, &n);.
1cd65 20 20 20 20 69 66 28 20 28 69 6e 74 29 6e 3e 64 if( (int)n>d
1cd66 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1cd67 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
1cd68 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f {. goto too
1cd69 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a _big;. }. }.
1cd6a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1cd6b 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e eMemGrow(pOut, n
1cd6c 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f , 0) ){. goto
1cd6d 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 no_mem;. }. p
1cd6e 4f 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65 Out->n = n;. Me
1cd6f 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1cd70 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 t, MEM_Blob);.
1cd71 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 if( pC->isIndex
1cd72 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1cd73 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 72 73 te3BtreeKey(pCrs
1cd74 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a r, 0, n, pOut->z
1cd75 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1cd76 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1cd77 65 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c 20 eData(pCrsr, 0,
1cd78 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d n, pOut->z);. }
1cd79 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 . pOut->enc = S
1cd7a 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 QLITE_UTF8; /*
1cd7b 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 In case the blob
1cd7c 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f is ever cast to
1cd7d 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 text */. UPDAT
1cd7e 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1cd7f 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1cd80 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 ../* Opcode: Row
1cd81 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a id P1 P2 * * *.*
1cd82 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 *.** Store in re
1cd83 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 gister P2 an int
1cd84 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
1cd85 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 e key of the tab
1cd86 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a le entry that.**
1cd87 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 P1 is currently
1cd88 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a point to..**.**
1cd89 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68 65 P1 can be eithe
1cd8a 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 r an ordinary ta
1cd8b 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c ble or a virtual
1cd8c 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75 table. There u
1cd8d 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73 sed to.** be a s
1cd8e 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69 eparate OP_VRowi
1cd8f 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65 d opcode for use
1cd90 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61 with virtual ta
1cd91 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a bles, but this.*
1cd92 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77 * one opcode now
1cd93 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 works for both
1cd94 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a table types..*/.
1cd95 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b case OP_Rowid: {
1cd96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd97 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1cd98 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d ase */. int i =
1cd99 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
1cd9a 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 Cursor *pC;. i6
1cd9b 34 20 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 4 v;.. assert(
1cd9c 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1cd9d 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1cd9e 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
1cd9f 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 sert( pC!=0 );.
1cda0 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 if( pC->nullRow
1cda1 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f ){. /* Do no
1cda2 74 68 69 6e 67 20 73 6f 20 74 68 61 74 20 72 65 thing so that re
1cda3 67 5b 50 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55 g[P2] remains NU
1cda4 4c 4c 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b LL */. break;
1cda5 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d . }else if( pC-
1cda6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1cda7 29 7b 0a 20 20 20 20 76 20 3d 20 70 43 2d 3e 6d ){. v = pC->m
1cda8 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a 20 20 7d ovetoTarget;. }
1cda9 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 70 73 65 else if( pC->pse
1cdaa 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 udoTable ){.
1cdab 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 43 2d v = keyToInt(pC-
1cdac 3e 69 4b 65 79 29 3b 0a 23 69 66 6e 64 65 66 20 >iKey);.#ifndef
1cdad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1cdae 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 65 UALTABLE. }else
1cdaf 20 69 66 28 20 70 43 2d 3e 70 56 74 61 62 43 75 if( pC->pVtabCu
1cdb0 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 rsor ){. sqli
1cdb1 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1cdb2 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 . const sqlit
1cdb3 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1cdb4 6c 65 3b 0a 20 20 20 20 70 56 74 61 62 20 3d 20 le;. pVtab =
1cdb5 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d pC->pVtabCursor-
1cdb6 3e 70 56 74 61 62 3b 0a 20 20 20 20 70 4d 6f 64 >pVtab;. pMod
1cdb7 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f ule = pVtab->pMo
1cdb8 64 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 dule;. assert
1cdb9 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 ( pModule->xRowi
1cdba 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c d );. if( sql
1cdbb 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1cdbc 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1cdbd 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1cdbe 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e rc = pModule->
1cdbf 78 52 6f 77 69 64 28 70 43 2d 3e 70 56 74 61 62 xRowid(pC->pVtab
1cdc0 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 Cursor, &v);.
1cdc1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1cdc2 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1cdc3 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d p->zErrMsg =
1cdc4 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b pVtab->zErrMsg;
1cdc5 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 . pVtab->zErr
1cdc6 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 Msg = 0;. if(
1cdc7 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1cdc8 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1cdc9 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1cdca 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1cdcb 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1cdcc 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a BLE */. }else{.
1cdcd 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1cdce 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f VdbeCursorMoveto
1cdcf 28 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 (pC);. if( rc
1cdd0 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1cdd1 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1cdd2 69 66 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 if( pC->rowidIsV
1cdd3 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 76 20 alid ){. v
1cdd4 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b = pC->lastRowid;
1cdd5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1cdd6 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 assert( pC->pC
1cdd7 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 ursor!=0 );.
1cdd8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 sqlite3BtreeKe
1cdd9 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f ySize(pC->pCurso
1cdda 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 76 20 r, &v);. v
1cddb 3d 20 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 = keyToInt(v);.
1cddc 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 2d }. }. pOut-
1cddd 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 >u.i = v;. MemS
1cdde 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1cddf 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 MEM_Int);. bre
1cde0 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1cde1 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a : NullRow P1 * *
1cde2 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 * *.**.** Move
1cde3 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f the cursor P1 to
1cde4 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e a null row. An
1cde5 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 y OP_Column oper
1cde6 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f ations.** that o
1cde7 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 ccur while the c
1cde8 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 ursor is on the
1cde9 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c null row will al
1cdea 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 ways.** write a
1cdeb 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1cdec 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e _NullRow: {. in
1cded 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1cdee 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1cdef 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 .. assert( i>=0
1cdf0 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 && i<p->nCursor
1cdf1 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 );. pC = p->ap
1cdf2 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 Csr[i];. assert
1cdf3 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d ( pC!=0 );. pC-
1cdf4 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 >nullRow = 1;.
1cdf5 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1cdf6 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e = 0;. if( pC->
1cdf7 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 pCursor ){. s
1cdf8 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
1cdf9 43 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72 73 Cursor(pC->pCurs
1cdfa 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b or);. }. break
1cdfb 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cdfc 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a Last P1 P2 * * *
1cdfd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
1cdfe 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 use of the Rowid
1cdff 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 or Column or Ne
1ce00 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 xt instruction f
1ce01 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 or P1 .** will r
1ce02 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 efer to the last
1ce03 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1ce04 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1ce05 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 index..** If the
1ce06 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1ce07 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e is empty and P2>
1ce08 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 0, then jump imm
1ce09 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1ce0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 ** If P2 is 0 or
1ce0b 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 if the table or
1ce0c 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d index is not em
1ce0d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 pty, fall throug
1ce0e 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1ce0f 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1ce10 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 n..*/.case OP_La
1ce11 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 st: { /*
1ce12 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 jump */. int i
1ce13 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1ce14 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1ce15 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1ce16 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 int res;.. as
1ce17 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1ce18 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1ce19 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1ce1a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
1ce1b 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 0 );. pCrsr = p
1ce1c 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 C->pCursor;. as
1ce1d 73 65 72 74 28 20 70 43 72 73 72 21 3d 30 20 29 sert( pCrsr!=0 )
1ce1e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1ce1f 42 74 72 65 65 4c 61 73 74 28 70 43 72 73 72 2c BtreeLast(pCrsr,
1ce20 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 &res);. pC->nu
1ce21 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73 3b llRow = (u8)res;
1ce22 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d . pC->deferredM
1ce23 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d oveto = 0;. pC-
1ce24 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1ce25 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 0;. pC->cacheSt
1ce26 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1ce27 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 20 26 26 LE;. if( res &&
1ce28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 pOp->p2>0 ){.
1ce29 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1ce2a 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1ce2b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 .}.../* Opcode:
1ce2c 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a Sort P1 P2 * * *
1ce2d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1ce2e 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 de does exactly
1ce2f 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 the same thing a
1ce30 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 s OP_Rewind exce
1ce31 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e pt that.** it in
1ce32 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f crements an undo
1ce33 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 cumented global
1ce34 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f variable used fo
1ce35 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a r testing..**.**
1ce36 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f Sorting is acco
1ce37 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 mplished by writ
1ce38 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f ing records into
1ce39 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 a sorting index
1ce3a 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 ,.** then rewind
1ce3b 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 ing that index a
1ce3c 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 nd playing it ba
1ce3d 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e ck from beginnin
1ce3e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 g to.** end. We
1ce3f 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 use the OP_Sort
1ce40 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 opcode instead
1ce41 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 of OP_Rewind to
1ce42 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 do the.** rewind
1ce43 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 ing so that the
1ce44 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1ce45 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e will be incremen
1ce46 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 ted and.** regre
1ce47 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 ssion tests can
1ce48 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 determine whethe
1ce49 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 r or not the opt
1ce4a 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 imizer is.** cor
1ce4b 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e rectly optimizin
1ce4c 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a g out sorts..*/.
1ce4d 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 case OP_Sort: {
1ce4e 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1ce4f 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1ce50 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 TEST. sqlite3_s
1ce51 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 ort_count++;. s
1ce52 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1ce53 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 unt--;.#endif.
1ce54 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 p->aCounter[SQLI
1ce55 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f TE_STMTSTATUS_SO
1ce56 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 RT-1]++;. /* Fa
1ce57 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 ll through into
1ce58 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f OP_Rewind */.}./
1ce59 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 * Opcode: Rewind
1ce5a 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1ce5b 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 ** The next use
1ce5c 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 of the Rowid or
1ce5d 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 Column or Next i
1ce5e 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 nstruction for P
1ce5f 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 1 .** will refer
1ce60 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e to the first en
1ce61 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1ce62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 ase table or ind
1ce63 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 ex..** If the ta
1ce64 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 ble or index is
1ce65 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 empty and P2>0,
1ce66 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 then jump immedi
1ce67 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 ately to P2..**
1ce68 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 If P2 is 0 or if
1ce69 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1ce6a 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 dex is not empty
1ce6b 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a , fall through.*
1ce6c 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1ce6d 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ng instruction..
1ce6e 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e */.case OP_Rewin
1ce6f 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a d: { /* j
1ce70 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d ump */. int i =
1ce71 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
1ce72 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1ce73 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1ce74 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 int res;.. ass
1ce75 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1ce76 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 ->nCursor );. p
1ce77 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b C = p->apCsr[i];
1ce78 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 . assert( pC!=0
1ce79 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 );. if( (pCrsr
1ce7a 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 = pC->pCursor)!
1ce7b 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 =0 ){. rc = s
1ce7c 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 qlite3BtreeFirst
1ce7d 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 (pCrsr, &res);.
1ce7e 20 20 20 70 43 2d 3e 61 74 46 69 72 73 74 20 3d pC->atFirst =
1ce7f 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 res==0 ?1:0;.
1ce80 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f pC->deferredMo
1ce81 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 veto = 0;. pC
1ce82 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1ce83 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1ce84 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
1ce85 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a d = 0;. }else{.
1ce86 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d res = 1;. }
1ce87 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d . pC->nullRow =
1ce88 20 28 75 38 29 72 65 73 3b 0a 20 20 61 73 73 65 (u8)res;. asse
1ce89 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 rt( pOp->p2>0 &&
1ce8a 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 pOp->p2<p->nOp
1ce8b 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a );. if( res ){.
1ce8c 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1ce8d 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1ce8e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1ce8f 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 Next P1 P2 * *
1ce90 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 *.**.** Advance
1ce91 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1ce92 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
1ce93 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 he next key/data
1ce94 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 pair in its.**
1ce95 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 table or index.
1ce96 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1ce97 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 more key/value
1ce98 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 pairs then fall
1ce99 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 through.** to th
1ce9a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 e following inst
1ce9b 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 ruction. But if
1ce9c 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 61 the cursor adva
1ce9d 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 66 nce was successf
1ce9e 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 ul,.** jump imme
1ce9f 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a diately to P2..*
1cea0 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73 *.** The P1 curs
1cea1 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61 or must be for a
1cea2 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 real table, not
1cea3 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e a pseudo-table.
1cea4 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1cea5 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f Prev.*/./* Opco
1cea6 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 2a de: Prev P1 P2 *
1cea7 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 * *.**.** Back
1cea8 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 up cursor P1 so
1cea9 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1ceaa 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b o the previous k
1ceab 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20 ey/data pair in
1ceac 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 its.** table or
1cead 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65 index. If there
1ceae 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 is no previous
1ceaf 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 key/value pairs
1ceb0 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 then fall throug
1ceb1 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1ceb2 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1ceb3 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 n. But if the c
1ceb4 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73 ursor backup was
1ceb5 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 successful,.**
1ceb6 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1ceb7 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 to P2..**.** Th
1ceb8 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1ceb9 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 be for a real t
1ceba 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1cebb 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 do-table..*/.cas
1cebc 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20 e OP_Prev:
1cebd 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 /* jump */.c
1cebe 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 ase OP_Next: {
1cebf 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1cec0 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1cec1 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1cec2 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1cec3 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e .. CHECK_FOR_IN
1cec4 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 TERRUPT;. asser
1cec5 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1cec6 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1cec7 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d sor );. pC = p-
1cec8 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1cec9 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a . if( pC==0 ){.
1ceca 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 break; /* S
1cecb 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 ee ticket #2273
1cecc 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 3d */. }. pCrsr =
1cecd 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 pC->pCursor;.
1cece 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29 3b assert( pCrsr );
1cecf 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 . res = 1;. as
1ced0 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 sert( pC->deferr
1ced1 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1ced2 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 rc = pOp->opcod
1ced3 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c e==OP_Next ? sql
1ced4 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 ite3BtreeNext(pC
1ced5 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20 20 rsr, &res) :.
1ced6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ced7 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1ced8 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
1ced9 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 pCrsr, &res);.
1ceda 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 pC->nullRow = (u
1cedb 38 29 72 65 73 3b 0a 20 20 70 43 2d 3e 63 61 63 8)res;. pC->cac
1cedc 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1cedd 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 _STALE;. if( re
1cede 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d s==0 ){. pc =
1cedf 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1cee0 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 if( pOp->p5 )
1cee1 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d p->aCounter[pOp-
1cee2 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 >p5-1]++;.#ifdef
1cee3 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 SQLITE_TEST.
1cee4 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f sqlite3_search_
1cee5 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
1cee6 20 20 7d 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 }. pC->rowidI
1cee7 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 sValid = 0;. br
1cee8 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cee9 65 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 20 e: IdxInsert P1
1ceea 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a P2 P3 * P5.**.**
1ceeb 20 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c Register P2 hol
1ceec 64 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b ds a SQL index k
1ceed 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68 ey made using th
1ceee 65 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 e.** MakeRecord
1ceef 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 instructions. T
1cef0 68 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65 his opcode write
1cef1 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e s that key.** in
1cef2 74 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e to the index P1.
1cef3 20 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65 Data for the e
1cef4 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a ntry is nil..**.
1cef5 2a 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20 ** P3 is a flag
1cef6 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20 that provides a
1cef7 68 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72 hint to the b-tr
1cef8 65 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68 ee layer that th
1cef9 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 is.** insert is
1cefa 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 likely to be an
1cefb 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 append..**.** Th
1cefc 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1cefd 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e nly works for in
1cefe 64 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69 dices. The equi
1ceff 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 valent instructi
1cf00 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 on.** for tables
1cf01 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a is OP_Insert..*
1cf02 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 /.case OP_IdxIns
1cf03 65 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ert: { /*
1cf04 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20 in2 */. int i
1cf05 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1cf06 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1cf07 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1cf08 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1cf09 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1cf0a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1cf0b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1cf0c 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c assert( pIn2->fl
1cf0d 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 ags & MEM_Blob )
1cf0e 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d ;. if( (pCrsr =
1cf0f 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b (pC = p->apCsr[
1cf10 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 i])->pCursor)!=0
1cf11 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1cf12 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 pC->isTable==0 )
1cf13 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e ;. rc = Expan
1cf14 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 dBlob(pIn2);.
1cf15 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1cf16 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 OK ){. int
1cf17 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a nKey = pIn2->n;.
1cf18 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1cf19 20 2a 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a *zKey = pIn2->z
1cf1a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1cf1b 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
1cf1c 70 43 72 73 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 pCrsr, zKey, nKe
1cf1d 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 y, "", 0, 0, pOp
1cf1e 2d 3e 70 33 2c 20 0a 20 20 20 20 20 20 20 20 20 ->p3, .
1cf1f 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 ((pOp->p5 & OPF
1cf20 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c LAG_USESEEKRESUL
1cf21 54 29 20 3f 20 70 43 2d 3e 73 65 65 6b 52 65 73 T) ? pC->seekRes
1cf22 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29 ult : 0). )
1cf23 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1cf24 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1cf25 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 to==0 );. p
1cf26 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1cf27 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1cf28 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1cf29 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1cf2a 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 dxDelete P1 P2 P
1cf2b 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 3 * *.**.** The
1cf2c 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 content of P3 re
1cf2d 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 gisters starting
1cf2e 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20 at register P2
1cf2f 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 form.** an unpac
1cf30 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 ked index key. T
1cf31 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 his opcode remov
1cf32 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 es that entry fr
1cf33 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 om the .** index
1cf34 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f opened by curso
1cf35 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P1..*/.case OP
1cf36 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20 20 _IdxDelete: {.
1cf37 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b int i = pOp->p1;
1cf38 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1cf39 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1cf3a 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 Crsr;. assert(
1cf3b 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 pOp->p3>0 );. a
1cf3c 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1cf3d 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d && pOp->p2+pOp-
1cf3e 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 >p3<=p->nMem+1 )
1cf3f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 ;. assert( i>=0
1cf40 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 && i<p->nCursor
1cf41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1cf42 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a >apCsr[i]!=0 );.
1cf43 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 if( (pCrsr = (
1cf44 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1cf45 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 )->pCursor)!=0 )
1cf46 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 {. int res;.
1cf47 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 UnpackedRecor
1cf48 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 d r;. r.pKeyI
1cf49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e nfo = pC->pKeyIn
1cf4a 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 fo;. r.nField
1cf4b 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b = (u16)pOp->p3;
1cf4c 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 30 . r.flags = 0
1cf4d 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 ;. r.aMem = &
1cf4e 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
1cf4f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1cf50 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1cf51 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72 2c acked(pCrsr, &r,
1cf52 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 0, 0, &res);.
1cf53 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1cf54 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 7b _OK && res==0 ){
1cf55 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1cf56 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 70 te3BtreeDelete(p
1cf57 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Crsr);. }.
1cf58 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 assert( pC->def
1cf59 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1cf5a 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 ;. pC->cacheS
1cf5b 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1cf5c 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b ALE;. }. break
1cf5d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cf5e 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20 2a IdxRowid P1 P2 *
1cf5f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 * *.**.** Write
1cf60 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1cf61 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 2 an integer whi
1cf62 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20 65 ch is the last e
1cf63 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63 6f ntry in the reco
1cf64 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 rd at.** the end
1cf65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65 of the index ke
1cf66 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 y pointed to by
1cf67 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69 73 cursor P1. This
1cf68 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 20 integer should
1cf69 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 be.** the rowid
1cf6a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 of the table ent
1cf6b 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 ry to which this
1cf6c 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f 69 index entry poi
1cf6d 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 nts..**.** See a
1cf6e 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 lso: Rowid, Make
1cf6f 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20 Record..*/.case
1cf70 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 OP_IdxRowid: {
1cf71 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1cf72 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1cf73 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1cf74 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 >p1;. BtCursor
1cf75 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65 43 75 *pCrsr;. VdbeCu
1cf76 72 73 6f 72 20 2a 70 43 3b 0a 0a 0a 20 20 61 73 rsor *pC;... as
1cf77 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1cf78 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1cf79 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1cf7a 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 [i]!=0 );. if(
1cf7b 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70 (pCrsr = (pC = p
1cf7c 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 ->apCsr[i])->pCu
1cf7d 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 rsor)!=0 ){.
1cf7e 69 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 72 i64 rowid;. r
1cf7f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 c = sqlite3VdbeC
1cf80 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b ursorMoveto(pC);
1cf81 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
1cf82 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1cf83 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 error;. asser
1cf84 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d t( pC->deferredM
1cf85 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 oveto==0 );.
1cf86 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 assert( pC->isTa
1cf87 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 ble==0 );. if
1cf88 28 20 21 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 ( !pC->nullRow )
1cf89 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1cf8a 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 ite3VdbeIdxRowid
1cf8b 28 70 43 72 73 72 2c 20 26 72 6f 77 69 64 29 3b (pCrsr, &rowid);
1cf8c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1cf8d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1cf8e 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1cf8f 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1cf90 20 20 20 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 }. MemSe
1cf91 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1cf92 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 MEM_Int);.
1cf93 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 6f 77 69 pOut->u.i = rowi
1cf94 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 d;. }. }. b
1cf95 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cf96 64 65 3a 20 49 64 78 47 45 20 50 31 20 50 32 20 de: IdxGE P1 P2
1cf97 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 P4 P5.**.** T
1cf98 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76 he P4 register v
1cf99 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 alues beginning
1cf9a 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 with P3 form an
1cf9b 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a unpacked index .
1cf9c 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 ** key that omit
1cf9d 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f s the ROWID. Co
1cf9e 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 mpare this key v
1cf9f 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 alue against the
1cfa0 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 index .** that
1cfa1 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 P1 is currently
1cfa2 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e pointing to, ign
1cfa3 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 oring the ROWID
1cfa4 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e on the P1 index.
1cfa5 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 .**.** If the P1
1cfa6 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 index entry is
1cfa7 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1cfa8 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1cfa9 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a value.** then j
1cfaa 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 ump to P2. Othe
1cfab 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 rwise fall throu
1cfac 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 gh to the next i
1cfad 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1cfae 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a * If P5 is non-z
1cfaf 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 ero then the key
1cfb0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
1cfb1 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f sed by an epsilo
1cfb2 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 n .** prior to t
1cfb3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 he comparison.
1cfb4 54 68 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 This make the op
1cfb5 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 code work like I
1cfb6 64 78 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 dxGT except.** t
1cfb7 68 61 74 20 69 66 20 74 68 65 20 6b 65 79 20 66 hat if the key f
1cfb8 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 20 rom register P3
1cfb9 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 is a prefix of t
1cfba 68 65 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75 he key in the cu
1cfbb 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 rsor,.** the res
1cfbc 75 6c 74 20 69 73 20 66 61 6c 73 65 20 77 68 65 ult is false whe
1cfbd 72 65 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 reas it would be
1cfbe 20 74 72 75 65 20 77 69 74 68 20 49 64 78 47 54 true with IdxGT
1cfbf 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1cfc0 49 64 78 4c 54 20 50 31 20 50 32 20 50 33 20 2a IdxLT P1 P2 P3 *
1cfc1 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 P5.**.** The P4
1cfc2 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 register values
1cfc3 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
1cfc4 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 P3 form an unpac
1cfc5 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 ked index .** ke
1cfc6 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 y that omits the
1cfc7 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 ROWID. Compare
1cfc8 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 this key value
1cfc9 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 against the inde
1cfca 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 x .** that P1 is
1cfcb 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1cfcc 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 ing to, ignoring
1cfcd 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 the ROWID on th
1cfce 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a e P1 index..**.*
1cfcf 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 * If the P1 inde
1cfd0 78 20 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20 x entry is less
1cfd1 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c than the key val
1cfd2 75 65 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 ue then jump to
1cfd3 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 P2..** Otherwise
1cfd4 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f fall through to
1cfd5 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 the next instru
1cfd6 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
1cfd7 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 P5 is non-zero t
1cfd8 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 hen the key valu
1cfd9 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 e is increased b
1cfda 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 y an epsilon pri
1cfdb 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f or .** to the co
1cfdc 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 mparison. This
1cfdd 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65 makes the opcode
1cfde 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 work like IdxLE
1cfdf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1cfe0 4c 54 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 LT: /*
1cfe1 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1cfe2 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 e OP_IdxGE: {
1cfe3 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1cfe4 33 20 2a 2f 0a 20 20 69 6e 74 20 69 3d 20 70 4f 3 */. int i= pO
1cfe5 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1cfe6 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 sor *pC;.. asse
1cfe7 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1cfe8 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1cfe9 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1cfea 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1cfeb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1cfec 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a ->pCursor!=0 ){.
1cfed 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1cfee 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1cfef 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 r;. assert( p
1cff0 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1cff1 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 o==0 );. asse
1cff2 72 74 28 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c rt( pOp->p5==0 |
1cff3 7c 20 70 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a | pOp->p5==1 );.
1cff4 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1cff5 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 >p4type==P4_INT3
1cff6 32 20 29 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 2 );. r.pKeyI
1cff7 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e nfo = pC->pKeyIn
1cff8 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 fo;. r.nField
1cff9 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e = (u16)pOp->p4.
1cffa 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e i;. if( pOp->
1cffb 70 35 20 29 7b 0a 20 20 20 20 20 20 72 2e 66 6c p5 ){. r.fl
1cffc 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 ags = UNPACKED_I
1cffd 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b 45 NCRKEY | UNPACKE
1cffe 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a D_IGNORE_ROWID;.
1cfff 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1d000 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 r.flags = UNPAC
1d001 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 KED_IGNORE_ROWID
1d002 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 2e 61 4d ;. }. r.aM
1d003 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1d004 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 3d p->p3];. rc =
1d005 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b sqlite3VdbeIdxK
1d006 65 79 43 6f 6d 70 61 72 65 28 70 43 2c 20 26 72 eyCompare(pC, &r
1d007 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 , &res);. if(
1d008 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1d009 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20 _IdxLT ){.
1d00a 72 65 73 20 3d 20 2d 72 65 73 3b 0a 20 20 20 20 res = -res;.
1d00b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1d00c 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 ert( pOp->opcode
1d00d 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 ==OP_IdxGE );.
1d00e 20 20 20 20 72 65 73 2b 2b 3b 0a 20 20 20 20 7d res++;. }
1d00f 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 29 . if( res>0 )
1d010 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1d011 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 7d ->p2 - 1 ;. }
1d012 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d013 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 74 ./* Opcode: Dest
1d014 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a roy P1 P2 P3 * *
1d015 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e .**.** Delete an
1d016 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 entire database
1d017 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1d018 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 whose root page
1d019 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a in the database.
1d01a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e ** file is given
1d01b 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 by P1..**.** Th
1d01c 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65 e table being de
1d01d 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68 stroyed is in th
1d01e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1d01f 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 20 file if P3==0.
1d020 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 6e If.** P3==1 then
1d021 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 the table to be
1d022 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 clear is in the
1d023 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 auxiliary datab
1d024 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 ase file.** that
1d025 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 is used to stor
1d026 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 e tables create
1d027 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d using CREATE TEM
1d028 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a PORARY TABLE..**
1d029 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 .** If AUTOVACUU
1d02a 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65 M is enabled the
1d02b 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 n it is possible
1d02c 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f that another ro
1d02d 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 ot page.** might
1d02e 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 be moved into t
1d02f 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64 he newly deleted
1d030 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72 root page in or
1d031 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a der to keep all.
1d032 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f ** root pages co
1d033 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65 20 ntiguous at the
1d034 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
1d035 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 database. The
1d036 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20 former.** value
1d037 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 of the root page
1d038 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74 that moved - it
1d039 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74 s value before t
1d03a 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64 he move occurred
1d03b 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 -.** is stored
1d03c 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1d03d 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a 20 If no page .**
1d03e 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 65 71 movement was req
1d03f 75 69 72 65 64 20 28 62 65 63 61 75 73 65 20 74 uired (because t
1d040 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 he table being d
1d041 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 65 61 ropped was alrea
1d042 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 dy .** the last
1d043 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 one in the datab
1d044 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 72 6f ase) then a zero
1d045 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 is stored in re
1d046 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 66 gister P2..** If
1d047 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 64 AUTOVACUUM is d
1d048 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 20 7a isabled then a z
1d049 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e ero is stored in
1d04a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1d04b 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c .** See also: Cl
1d04c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 ear.*/.case OP_D
1d04d 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f 2a estroy: { /*
1d04e 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1d04f 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 64 */. int iMoved
1d050 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 23 69 ;. int iCnt;.#i
1d051 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d052 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1d053 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20 Vdbe *pVdbe;.
1d054 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 iCnt = 0;. for(
1d055 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65 3b pVdbe=db->pVdbe;
1d056 20 70 56 64 62 65 3b 20 70 56 64 62 65 3d 70 56 pVdbe; pVdbe=pV
1d057 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 dbe->pNext){.
1d058 20 69 66 28 20 70 56 64 62 65 2d 3e 6d 61 67 69 if( pVdbe->magi
1d059 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1d05a 4e 20 26 26 20 70 56 64 62 65 2d 3e 69 6e 56 74 N && pVdbe->inVt
1d05b 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 70 56 abMethod<2 && pV
1d05c 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 dbe->pc>=0 ){.
1d05d 20 20 20 20 69 43 6e 74 2b 2b 3b 0a 20 20 20 20 iCnt++;.
1d05e 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 43 }. }.#else. iC
1d05f 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 nt = db->activeV
1d060 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 dbeCnt;.#endif.
1d061 20 69 66 28 20 69 43 6e 74 3e 31 20 29 7b 0a 20 if( iCnt>1 ){.
1d062 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c rc = SQLITE_L
1d063 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72 OCKED;. p->er
1d064 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 rorAction = OE_A
1d065 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 bort;. }else{.
1d066 20 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 int iDb = pOp
1d067 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 74 ->p3;. assert
1d068 28 20 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20 20 ( iCnt==1 );.
1d069 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1d06a 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 eeMask & (1<<iDb
1d06b 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 ))!=0 );. rc
1d06c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 = sqlite3BtreeDr
1d06d 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b opTable(db->aDb[
1d06e 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 iDb].pBt, pOp->p
1d06f 31 2c 20 26 69 4d 6f 76 65 64 29 3b 0a 20 20 20 1, &iMoved);.
1d070 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1d071 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1d072 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1d073 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 iMoved;.#ifndef
1d074 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1d075 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 VACUUM. if( r
1d076 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1d077 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20 20 iMoved!=0 ){.
1d078 20 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 sqlite3RootPa
1d079 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44 62 geMoved(&db->aDb
1d07a 5b 69 44 62 5d 2c 20 69 4d 6f 76 65 64 2c 20 70 [iDb], iMoved, p
1d07b 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 Op->p1);. }.#
1d07c 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 endif. }. brea
1d07d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d07e 20 43 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a Clear P1 P2 P3.
1d07f 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c **.** Delete all
1d080 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1d081 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
1d082 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 or index whose r
1d083 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 oot page.** in t
1d084 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1d085 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e is given by P1.
1d086 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 But, unlike De
1d087 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a stroy, do not.**
1d088 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c remove the tabl
1d089 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 e or index from
1d08a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1d08b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 e..**.** The tab
1d08c 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 le being clear i
1d08d 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 s in the main da
1d08e 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1d08f 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 2==0. If.** P2=
1d090 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c =1 then the tabl
1d091 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 e to be clear is
1d092 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 in the auxiliar
1d093 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a y database file.
1d094 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 ** that is used
1d095 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 to store tables
1d096 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 create using CRE
1d097 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 ATE TEMPORARY TA
1d098 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 BLE..**.** If th
1d099 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f e P3 value is no
1d09a 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n-zero, then the
1d09b 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 table referred
1d09c 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a to must be an.**
1d09d 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 intkey table (a
1d09e 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 n SQL table, not
1d09f 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 an index). In t
1d0a0 68 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 his case the row
1d0a1 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e change .** coun
1d0a2 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
1d0a3 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
1d0a4 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 f rows in the ta
1d0a5 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 ble being cleare
1d0a6 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 d. .** If P3 is
1d0a7 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
1d0a8 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 o, then the valu
1d0a9 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 e stored in regi
1d0aa 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c ster P3 is.** al
1d0ab 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 so incremented b
1d0ac 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
1d0ad 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c rows in the tabl
1d0ae 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e e being cleared.
1d0af 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1d0b0 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 Destroy.*/.case
1d0b1 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 20 20 69 OP_Clear: {. i
1d0b2 6e 74 20 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a nt nChange = 0;.
1d0b3 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1d0b4 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1d0b5 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 p->p2))!=0 );.
1d0b6 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1d0b7 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20 eClearTable(.
1d0b8 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e db->aDb[pOp->
1d0b9 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 p2].pBt, pOp->p1
1d0ba 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 6e 43 , (pOp->p3 ? &nC
1d0bb 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a hange : 0). );.
1d0bc 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b if( pOp->p3 ){
1d0bd 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 . p->nChange
1d0be 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 += nChange;.
1d0bf 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b if( pOp->p3>0 ){
1d0c0 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70 . p->aMem[p
1d0c1 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 6e Op->p3].u.i += n
1d0c2 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20 Change;. }.
1d0c3 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d0c4 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 Opcode: CreateT
1d0c5 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a able P1 P2 * * *
1d0c6 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
1d0c7 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74 a new table in t
1d0c8 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1d0c9 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f file if P1==0 o
1d0ca 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 r in the.** auxi
1d0cb 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 liary database f
1d0cc 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 ile if P1==1 or
1d0cd 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 in an attached d
1d0ce 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 atabase if.** P1
1d0cf 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 >1. Write the r
1d0d0 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
1d0d1 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
1d0d2 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 into.** registe
1d0d3 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 r P2.**.** The d
1d0d4 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
1d0d5 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e n a table and an
1d0d6 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20 index is this:
1d0d7 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a A table must.**
1d0d8 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69 have a 4-byte i
1d0d9 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63 nteger key and c
1d0da 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72 an have arbitrar
1d0db 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65 y data. An inde
1d0dc 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69 x.** has an arbi
1d0dd 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f trary key but no
1d0de 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 data..**.** See
1d0df 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64 also: CreateInd
1d0e0 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ex.*/./* Opcode:
1d0e1 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20 CreateIndex P1
1d0e2 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1d0e3 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e llocate a new in
1d0e4 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 dex in the main
1d0e5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1d0e6 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 P1==0 or in the
1d0e7 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 .** auxiliary da
1d0e8 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1d0e9 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 1==1 or in an at
1d0ea 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 tached database
1d0eb 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 if.** P1>1. Wri
1d0ec 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 te the root page
1d0ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
1d0ee 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a ew table into.**
1d0ef 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1d0f0 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 .** See document
1d0f1 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 ation on OP_Crea
1d0f2 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 teTable for addi
1d0f3 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1d0f4 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 on..*/.case OP_C
1d0f5 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20 reateIndex:
1d0f6 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1d0f7 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73 rerelease */.cas
1d0f8 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 e OP_CreateTable
1d0f9 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 : { /*
1d0fa 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1d0fb 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 20 3d 20 */. int pgno =
1d0fc 30 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 0;. int flags;.
1d0fd 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 Db *pDb;. ass
1d0fe 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1d0ff 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1d100 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1d101 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1d102 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 (1<<pOp->p1))!=0
1d103 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d );. pDb = &db-
1d104 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 >aDb[pOp->p1];.
1d105 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e 70 42 assert( pDb->pB
1d106 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f t!=0 );. if( pO
1d107 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 p->opcode==OP_Cr
1d108 65 61 74 65 54 61 62 6c 65 20 29 7b 0a 20 20 20 eateTable ){.
1d109 20 2f 2a 20 66 6c 61 67 73 20 3d 20 42 54 52 45 /* flags = BTRE
1d10a 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 E_INTKEY; */.
1d10b 20 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c flags = BTREE_L
1d10c 45 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e EAFDATA|BTREE_IN
1d10d 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 TKEY;. }else{.
1d10e 20 20 20 66 6c 61 67 73 20 3d 20 42 54 52 45 45 flags = BTREE
1d10f 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 _ZERODATA;. }.
1d110 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1d111 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 44 eeCreateTable(pD
1d112 62 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 66 b->pBt, &pgno, f
1d113 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 lags);. pOut->u
1d114 2e 69 20 3d 20 70 67 6e 6f 3b 0a 20 20 4d 65 6d .i = pgno;. Mem
1d115 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1d116 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 , MEM_Int);. br
1d117 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d118 65 3a 20 50 61 72 73 65 53 63 68 65 6d 61 20 50 e: ParseSchema P
1d119 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 1 P2 * P4 *.**.*
1d11a 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 * Read and parse
1d11b 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f all entries fro
1d11c 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 m the SQLITE_MAS
1d11d 54 45 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74 TER table of dat
1d11e 61 62 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 abase P1.** that
1d11f 20 6d 61 74 63 68 20 74 68 65 20 57 48 45 52 45 match the WHERE
1d120 20 63 6c 61 75 73 65 20 50 34 2e 20 20 50 32 20 clause P4. P2
1d121 69 73 20 74 68 65 20 22 66 6f 72 63 65 22 20 66 is the "force" f
1d122 6c 61 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f lag. Always do
1d123 0a 2a 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20 .** the parsing
1d124 69 66 20 50 32 20 69 73 20 74 72 75 65 2e 20 20 if P2 is true.
1d125 49 66 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20 If P2 is false,
1d126 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1d127 65 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 e is a.** no-op
1d128 69 66 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 if the schema is
1d129 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c not currently l
1d12a 6f 61 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 oaded. In other
1d12b 20 77 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a words, if P2.**
1d12c 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53 is false, the S
1d12d 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 QLITE_MASTER tab
1d12e 6c 65 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 le is only parse
1d12f 64 20 69 66 20 74 68 65 20 72 65 73 74 20 6f 66 d if the rest of
1d130 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 the.** schema i
1d131 73 20 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 s already loaded
1d132 20 69 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c into the symbol
1d133 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 table..**.** Th
1d134 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 is opcode invoke
1d135 73 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 s the parser to
1d136 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 create a new vir
1d137 74 75 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a tual machine,.**
1d138 20 74 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e then runs the n
1d139 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ew virtual machi
1d13a 6e 65 2e 20 20 49 74 20 69 73 20 74 68 75 73 20 ne. It is thus
1d13b 61 20 72 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 a re-entrant opc
1d13c 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ode..*/.case OP_
1d13d 50 61 72 73 65 53 63 68 65 6d 61 3a 20 7b 0a 20 ParseSchema: {.
1d13e 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e int iDb = pOp->
1d13f 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 p1;. assert( iD
1d140 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e b>=0 && iDb<db->
1d141 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 nDb );.. /* If
1d142 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74 68 pOp->p2 is 0, th
1d143 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 en this opcode i
1d144 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 s being executed
1d145 20 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a 20 to read a. **
1d146 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72 20 single row, for
1d147 65 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77 20 example the row
1d148 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
1d149 20 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20 2a a new index. *
1d14a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 * created by thi
1d14b 73 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68 65 s VDBE, from the
1d14c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
1d14d 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20 20 able. It only.
1d14e 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66 20 ** does this if
1d14f 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1d150 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65 g in-memory sche
1d151 6d 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a ma is currently.
1d152 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74 68 ** loaded. Oth
1d153 65 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77 20 erwise, the new
1d154 69 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f 6e index definition
1d155 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 61 can be loaded a
1d156 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20 74 long. ** with t
1d157 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 73 he rest of the s
1d158 63 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69 73 chema when it is
1d159 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a required.. **.
1d15a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74 68 ** Although th
1d15b 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 e mutex on the B
1d15c 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 tShared object t
1d15d 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 hat corresponds
1d15e 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 to. ** database
1d15f 20 69 44 62 20 28 74 68 65 20 64 61 74 61 62 61 iDb (the databa
1d160 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 se containing th
1d161 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
1d162 74 61 62 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 table. ** read
1d163 62 79 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 by this instruct
1d164 69 6f 6e 29 20 69 73 20 63 75 72 72 65 6e 74 6c ion) is currentl
1d165 79 20 68 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 y held, it is ne
1d166 63 65 73 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 cessary to. **
1d167 6f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 obtain the mutex
1d168 65 73 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 es on all attach
1d169 65 64 20 64 61 74 61 62 61 73 65 73 20 62 65 66 ed databases bef
1d16a 6f 72 65 20 63 68 65 63 6b 69 6e 67 20 69 66 0a ore checking if.
1d16b 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 ** the schema
1d16c 6f 66 20 69 44 62 20 69 73 20 6c 6f 61 64 65 64 of iDb is loaded
1d16d 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 . This is becaus
1d16e 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 74 20 e, at the start
1d16f 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 of. ** the sqli
1d170 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 te3_exec() call
1d171 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77 69 below, SQLite wi
1d172 6c 6c 20 69 6e 76 6f 6b 65 20 0a 20 20 2a 2a 20 ll invoke . **
1d173 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1d174 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d rAll(). If all m
1d175 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 utexes are not a
1d176 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 65 lready held, the
1d177 0a 20 20 2a 2a 20 69 44 62 20 6d 75 74 65 78 20 . ** iDb mutex
1d178 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72 69 may be temporari
1d179 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20 61 ly released to a
1d17a 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49 void deadlock. I
1d17b 66 20 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70 f . ** this hap
1d17c 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20 pens, then some
1d17d 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61 79 other thread may
1d17e 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d delete the in-m
1d17f 65 6d 6f 72 79 20 0a 20 20 2a 2a 20 73 63 68 65 emory . ** sche
1d180 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 ma of database i
1d181 44 62 20 62 65 66 6f 72 65 20 74 68 65 20 53 51 Db before the SQ
1d182 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75 6e 73 L statement runs
1d183 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20 20 2a . The schema. *
1d184 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 * will not be re
1d185 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65 20 74 loaded becuase t
1d186 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 he db->init.busy
1d187 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 flag is set. Th
1d188 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65 73 75 is. ** can resu
1d189 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75 63 68 lt in a "no such
1d18a 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65 5f 6d table: sqlite_m
1d18b 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c 66 6f aster" or "malfo
1d18c 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61 62 61 rmed. ** databa
1d18d 73 65 20 73 63 68 65 6d 61 22 20 65 72 72 6f 72 se schema" error
1d18e 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 20 being returned
1d18f 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a to the user.. *
1d190 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
1d191 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
1d192 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e ex(db->aDb[iDb].
1d193 70 42 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 pBt) );. sqlite
1d194 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 3BtreeEnterAll(d
1d195 62 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 b);. if( pOp->p
1d196 32 20 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 2 || DbHasProper
1d197 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 53 ty(db, iDb, DB_S
1d198 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a chemaLoaded) ){.
1d199 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1d19a 7a 4d 61 73 74 65 72 20 3d 20 53 43 48 45 4d 41 zMaster = SCHEMA
1d19b 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 _TABLE(iDb);.
1d19c 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 char *zSql;.
1d19d 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 InitData initDa
1d19e 74 61 3b 0a 20 20 20 20 69 6e 69 74 44 61 74 61 ta;. initData
1d19f 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 69 6e .db = db;. in
1d1a0 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 itData.iDb = pOp
1d1a1 2d 3e 70 31 3b 0a 20 20 20 20 69 6e 69 74 44 61 ->p1;. initDa
1d1a2 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 ta.pzErrMsg = &p
1d1a3 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 7a ->zErrMsg;. z
1d1a4 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Sql = sqlite3MPr
1d1a5 69 6e 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 intf(db,.
1d1a6 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f "SELECT name, ro
1d1a7 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d otpage, sql FROM
1d1a8 20 27 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 '%q'.%s WHERE %
1d1a9 73 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 s",. db->a
1d1aa 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a Db[iDb].zName, z
1d1ab 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e Master, pOp->p4.
1d1ac 7a 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c z);. if( zSql
1d1ad 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
1d1ae 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1d1af 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1d1b0 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1d1b1 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
1d1b2 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 assert( db->i
1d1b3 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 nit.busy==0 );.
1d1b4 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 db->init.bu
1d1b5 73 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e sy = 1;. in
1d1b6 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 itData.rc = SQLI
1d1b7 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73 TE_OK;. ass
1d1b8 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 ert( !db->malloc
1d1b9 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 Failed );.
1d1ba 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 rc = sqlite3_exe
1d1bb 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 c(db, zSql, sqli
1d1bc 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c te3InitCallback,
1d1bd 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a &initData, 0);.
1d1be 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1d1bf 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 LITE_OK ) rc = i
1d1c0 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 nitData.rc;.
1d1c1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1d1c2 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 20 20 20 db, zSql);.
1d1c3 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d db->init.busy =
1d1c4 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 0;. (void)
1d1c5 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1d1c6 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 db);. }. }.
1d1c7 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1d1c8 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 veAll(db);. if(
1d1c9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
1d1ca 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f M ){. goto no
1d1cb 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 _mem;. }. brea
1d1cc 6b 3b 20 20 0a 7d 0a 0a 23 69 66 20 21 64 65 66 k; .}..#if !def
1d1cd 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1d1ce 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65 _ANALYZE) && !de
1d1cf 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1d1d0 54 5f 50 41 52 53 45 52 29 0a 2f 2a 20 4f 70 63 T_PARSER)./* Opc
1d1d1 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 ode: LoadAnalysi
1d1d2 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a s P1 * * * *.**.
1d1d3 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69 ** Read the sqli
1d1d4 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 te_stat1 table f
1d1d5 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61 or database P1 a
1d1d6 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 nd load the cont
1d1d7 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 ent.** of that t
1d1d8 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e able into the in
1d1d9 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 ternal index has
1d1da 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77 h table. This w
1d1db 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 ill cause.** the
1d1dc 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20 analysis to be
1d1dd 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72 used when prepar
1d1de 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65 ing all subseque
1d1df 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 nt queries..*/.c
1d1e0 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 ase OP_LoadAnaly
1d1e1 73 69 73 3a 20 7b 0a 20 20 69 6e 74 20 69 44 62 sis: {. int iDb
1d1e2 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 = pOp->p1;. as
1d1e3 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 sert( iDb>=0 &&
1d1e4 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 iDb<db->nDb );.
1d1e5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 rc = sqlite3Ana
1d1e6 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 lysisLoad(db, iD
1d1e7 62 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d b);. break; .}
1d1e8 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
1d1e9 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1d1ea 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65 66 ANALYZE) && !def
1d1eb 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1d1ec 5f 50 41 52 53 45 52 29 20 20 2a 2f 0a 0a 2f 2a _PARSER) */../*
1d1ed 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61 62 Opcode: DropTab
1d1ee 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a le P1 * * P4 *.*
1d1ef 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 *.** Remove the
1d1f0 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d internal (in-mem
1d1f1 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 ory) data struct
1d1f2 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 ures that descri
1d1f3 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 be.** the table
1d1f4 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61 named P4 in data
1d1f5 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69 base P1. This i
1d1f6 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 s called after a
1d1f7 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72 6f table.** is dro
1d1f8 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f pped in order to
1d1f9 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e keep the intern
1d1fa 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f al representatio
1d1fb 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 n of the.** sche
1d1fc 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 ma consistent wi
1d1fd 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 th what is on di
1d1fe 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 sk..*/.case OP_D
1d1ff 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73 71 ropTable: {. sq
1d200 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
1d201 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 4f leteTable(db, pO
1d202 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a p->p1, pOp->p4.z
1d203 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1d204 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49 6e * Opcode: DropIn
1d205 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a dex P1 * * P4 *.
1d206 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 **.** Remove the
1d207 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 internal (in-me
1d208 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 mory) data struc
1d209 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 tures that descr
1d20a 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 ibe.** the index
1d20b 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1d20c 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1d20d 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1d20e 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 64 an index.** is d
1d20f 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 ropped in order
1d210 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 to keep the inte
1d211 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 rnal representat
1d212 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 ion of the.** sc
1d213 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 hema consistent
1d214 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 with what is on
1d215 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 disk..*/.case OP
1d216 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20 20 _DropIndex: {.
1d217 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
1d218 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c 20 DeleteIndex(db,
1d219 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 pOp->p1, pOp->p4
1d21a 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a .z);. break;.}.
1d21b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 ./* Opcode: Drop
1d21c 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20 50 Trigger P1 * * P
1d21d 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 4 *.**.** Remove
1d21e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 the internal (i
1d21f 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 n-memory) data s
1d220 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 tructures that d
1d221 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 escribe.** the t
1d222 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34 20 rigger named P4
1d223 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 in database P1.
1d224 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 This is called
1d225 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72 0a after a trigger.
1d226 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e ** is dropped in
1d227 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 order to keep t
1d228 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 he internal repr
1d229 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
1d22a 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 e.** schema cons
1d22b 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 istent with what
1d22c 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a is on disk..*/.
1d22d 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 case OP_DropTrig
1d22e 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 ger: {. sqlite3
1d22f 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
1d230 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d 3e rigger(db, pOp->
1d231 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a p1, pOp->p4.z);.
1d232 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 break;.}...#if
1d233 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d234 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
1d235 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65 ./* Opcode: Inte
1d236 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50 33 grityCk P1 P2 P3
1d237 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 61 * P5.**.** Do a
1d238 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74 68 n analysis of th
1d239 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e e currently open
1d23a 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f 72 database. Stor
1d23b 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 e in.** register
1d23c 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66 20 P1 the text of
1d23d 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1d23e 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79 20 describing any
1d23f 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66 20 problems..** If
1d240 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 20 no problems are
1d241 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20 4e found, store a N
1d242 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72 20 ULL in register
1d243 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 P1..**.** The re
1d244 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61 69 gister P3 contai
1d245 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e ns the maximum n
1d246 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65 64 umber of allowed
1d247 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20 6d errors..** At m
1d248 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72 6f ost reg(P3) erro
1d249 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 rs will be repor
1d24a 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ted..** In other
1d24b 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61 6c words, the anal
1d24c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73 6f ysis stops as so
1d24d 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65 72 on as reg(P1) er
1d24e 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65 65 rors are .** see
1d24f 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20 75 n. Reg(P1) is u
1d250 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 pdated with the
1d251 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
1d252 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a remaining..**.*
1d253 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
1d254 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20 74 numbers of all t
1d255 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 ables in the dat
1d256 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67 65 abase are intege
1d257 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 72 r.** stored in r
1d258 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31 eg(P1), reg(P1+1
1d259 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e ), reg(P1+2), ..
1d25a 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50 32 .. There are P2
1d25b 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61 6c tables.** total
1d25c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 ..**.** If P5 is
1d25d 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 63 not zero, the c
1d25e 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e 20 heck is done on
1d25f 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 the auxiliary da
1d260 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c 20 tabase.** file,
1d261 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 74 not the main dat
1d262 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
1d263 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 * This opcode is
1d264 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
1d265 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 nt the integrity
1d266 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a 2a _check pragma..*
1d267 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 72 /.case OP_Integr
1d268 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e ityCk: {. int n
1d269 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 Root; /* Nu
1d26a 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 mber of tables t
1d26b 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 o check. (Numbe
1d26c 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e r of root pages.
1d26d 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f ) */. int *aRoo
1d26e 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 t; /* Array
1d26f 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 of rootpage numb
1d270 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 ers for tables t
1d271 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a o be checked */.
1d272 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 int j;
1d273 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1d274 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b r */. int nErr;
1d275 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1d276 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 of errors repor
1d277 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ted */. char *z
1d278 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 ; /* Text
1d279 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 of the error re
1d27a 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 port */. Mem *p
1d27b 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 nErr; /* Reg
1d27c 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 ister keeping tr
1d27d 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 ack of errors re
1d27e 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 maining */. .
1d27f 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b nRoot = pOp->p2;
1d280 0a 20 20 61 73 73 65 72 74 28 20 6e 52 6f 6f 74 . assert( nRoot
1d281 3e 30 20 29 3b 0a 20 20 61 52 6f 6f 74 20 3d 20 >0 );. aRoot =
1d282 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
1d283 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e aw(db, sizeof(in
1d284 74 29 2a 28 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a t)*(nRoot+1) );.
1d285 20 20 69 66 28 20 61 52 6f 6f 74 3d 3d 30 20 29 if( aRoot==0 )
1d286 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1d287 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1d288 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1d289 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 6e 45 72 72 >nMem );. pnErr
1d28a 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1d28b 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p3];. assert(
1d28c 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 (pnErr->flags &
1d28d 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 MEM_Int)!=0 );.
1d28e 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 2d assert( (pnErr-
1d28f 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 >flags & (MEM_St
1d290 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 r|MEM_Blob))==0
1d291 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e );. pIn1 = &p->
1d292 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1d293 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f for(j=0; j<nRoo
1d294 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61 52 6f t; j++){. aRo
1d295 6f 74 5b 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c ot[j] = (int)sql
1d296 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1d297 28 26 70 49 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a (&pIn1[j]);. }.
1d298 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a aRoot[j] = 0;.
1d299 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1d29a 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 5<db->nDb );. a
1d29b 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 ssert( (p->btree
1d29c 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e Mask & (1<<pOp->
1d29d 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 7a 20 3d p5))!=0 );. z =
1d29e 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 sqlite3BtreeInt
1d29f 65 67 72 69 74 79 43 68 65 63 6b 28 64 62 2d 3e egrityCheck(db->
1d2a0 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 aDb[pOp->p5].pBt
1d2a1 2c 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a , aRoot, nRoot,.
1d2a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2a4 20 28 69 6e 74 29 70 6e 45 72 72 2d 3e 75 2e 69 (int)pnErr->u.i
1d2a5 2c 20 26 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 , &nErr);. sqli
1d2a6 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 52 te3DbFree(db, aR
1d2a7 6f 6f 74 29 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 oot);. pnErr->u
1d2a8 2e 69 20 2d 3d 20 6e 45 72 72 3b 0a 20 20 73 71 .i -= nErr;. sq
1d2a9 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1d2aa 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 ull(pIn1);. if(
1d2ab 20 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 nErr==0 ){.
1d2ac 61 73 73 65 72 74 28 20 7a 3d 3d 30 20 29 3b 0a assert( z==0 );.
1d2ad 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 3d 3d 30 }else if( z==0
1d2ae 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f ){. goto no_
1d2af 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 mem;. }else{.
1d2b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d2b1 53 65 74 53 74 72 28 70 49 6e 31 2c 20 7a 2c 20 SetStr(pIn1, z,
1d2b2 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c -1, SQLITE_UTF8,
1d2b3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
1d2b4 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 }. UPDATE_MAX
1d2b5 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b _BLOBSIZE(pIn1);
1d2b6 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
1d2b7 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e angeEncoding(pIn
1d2b8 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 1, encoding);.
1d2b9 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1d2ba 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
1d2bb 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a NTEGRITY_CHECK *
1d2bc 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f /../* Opcode: Ro
1d2bd 77 53 65 74 41 64 64 20 50 31 20 50 32 20 2a 20 wSetAdd P1 P2 *
1d2be 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 * *.**.** Insert
1d2bf 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
1d2c0 75 65 20 68 65 6c 64 20 62 79 20 72 65 67 69 73 ue held by regis
1d2c1 74 65 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f ter P2 into a bo
1d2c2 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 olean index.** h
1d2c3 65 6c 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 eld in register
1d2c4 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 P1..**.** An ass
1d2c5 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 ertion fails if
1d2c6 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 P2 is not an int
1d2c7 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 eger..*/.case OP
1d2c8 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b 20 20 20 _RowSetAdd: {
1d2c9 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 /* in2 */.
1d2ca 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d Mem *pIdx;. Mem
1d2cb 20 2a 70 56 61 6c 3b 0a 20 20 61 73 73 65 72 74 *pVal;. assert
1d2cc 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1d2cd 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1d2ce 29 3b 0a 20 20 70 49 64 78 20 3d 20 26 70 2d 3e );. pIdx = &p->
1d2cf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1d2d0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1d2d1 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 3d 70 >0 && pOp->p2<=p
1d2d2 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 56 61 6c ->nMem );. pVal
1d2d3 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1d2d4 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p2];. assert(
1d2d5 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d (pVal->flags & M
1d2d6 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 EM_Int)!=0 );.
1d2d7 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 73 if( (pIdx->flags
1d2d8 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
1d2d9 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1d2da 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 VdbeMemSetRowSet
1d2db 28 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 (pIdx);. if(
1d2dc 28 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d (pIdx->flags & M
1d2dd 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 20 EM_RowSet)==0 )
1d2de 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1d2df 0a 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 . sqlite3RowSet
1d2e0 49 6e 73 65 72 74 28 70 49 64 78 2d 3e 75 2e 70 Insert(pIdx->u.p
1d2e1 52 6f 77 53 65 74 2c 20 70 56 61 6c 2d 3e 75 2e RowSet, pVal->u.
1d2e2 69 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a i);. break;.}..
1d2e3 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 /* Opcode: RowSe
1d2e4 74 52 65 61 64 20 50 31 20 50 32 20 50 33 20 2a tRead P1 P2 P3 *
1d2e5 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 *.**.** Extract
1d2e6 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 the smallest va
1d2e7 6c 75 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e lue from boolean
1d2e8 20 69 6e 64 65 78 20 50 31 20 61 6e 64 20 70 75 index P1 and pu
1d2e9 74 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 t that value int
1d2ea 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 o.** register P3
1d2eb 2e 20 20 4f 72 2c 20 69 66 20 62 6f 6f 6c 65 61 . Or, if boolea
1d2ec 6e 20 69 6e 64 65 78 20 50 31 20 69 73 20 69 6e n index P1 is in
1d2ed 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c 20 6c itially empty, l
1d2ee 65 61 76 65 20 50 33 0a 2a 2a 20 75 6e 63 68 61 eave P3.** uncha
1d2ef 6e 67 65 64 20 61 6e 64 20 6a 75 6d 70 20 74 6f nged and jump to
1d2f0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e instruction P2.
1d2f1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 .*/.case OP_RowS
1d2f2 65 74 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20 etRead: {
1d2f3 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74 33 20 2a 2f /* jump, out3 */
1d2f4 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 . Mem *pIdx;.
1d2f5 69 36 34 20 76 61 6c 3b 0a 20 20 61 73 73 65 72 i64 val;. asser
1d2f6 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 t( pOp->p1>0 &&
1d2f7 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p1<=p->nMem
1d2f8 20 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f );. CHECK_FOR_
1d2f9 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 49 64 INTERRUPT;. pId
1d2fa 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 x = &p->aMem[pOp
1d2fb 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 ->p1];. pOut =
1d2fc 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1d2fd 5d 3b 0a 20 20 69 66 28 20 28 70 49 64 78 2d 3e ];. if( (pIdx->
1d2fe 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1d2ff 65 74 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 et)==0 . || sq
1d300 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 lite3RowSetNext(
1d301 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c pIdx->u.pRowSet,
1d302 20 26 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 &val)==0. ){.
1d303 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 61 /* The boolea
1d304 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 n index is empty
1d305 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
1d306 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 dbeMemSetNull(pI
1d307 64 78 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f dx);. pc = pO
1d308 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c p->p2 - 1;. }el
1d309 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61 6c se{. /* A val
1d30a 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20 66 72 ue was pulled fr
1d30b 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a om the index */.
1d30c 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1d30d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1d30e 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1d30f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d310 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 76 61 etInt64(pOut, va
1d311 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b l);. }. break;
1d312 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1d313 6f 77 53 65 74 54 65 73 74 20 50 31 20 50 32 20 owSetTest P1 P2
1d314 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 P3 P4.**.** Regi
1d315 73 74 65 72 20 50 33 20 69 73 20 61 73 73 75 6d ster P3 is assum
1d316 65 64 20 74 6f 20 68 6f 6c 64 20 61 20 36 34 2d ed to hold a 64-
1d317 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 bit integer valu
1d318 65 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 50 e. If register P
1d319 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 1.** contains a
1d31a 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 61 6e RowSet object an
1d31b 64 20 74 68 61 74 20 52 6f 77 53 65 74 20 6f 62 d that RowSet ob
1d31c 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a ject contains.**
1d31d 20 74 68 65 20 76 61 6c 75 65 20 68 65 6c 64 20 the value held
1d31e 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74 6f 20 72 in P3, jump to r
1d31f 65 67 69 73 74 65 72 20 50 32 2e 20 4f 74 68 65 egister P2. Othe
1d320 72 77 69 73 65 2c 20 69 6e 73 65 72 74 20 74 68 rwise, insert th
1d321 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 6e 20 e.** integer in
1d322 50 33 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 P3 into the RowS
1d323 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20 et and continue
1d324 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 on to the.** nex
1d325 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 t opcode..**.**
1d326 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 The RowSet objec
1d327 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 t is optimized f
1d328 6f 72 20 74 68 65 20 63 61 73 65 20 77 68 65 72 or the case wher
1d329 65 20 73 75 63 63 65 73 73 69 76 65 20 73 65 74 e successive set
1d32a 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 s.** of integers
1d32b 2c 20 77 68 65 72 65 20 65 61 63 68 20 73 65 74 , where each set
1d32c 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 75 70 contains no dup
1d32d 6c 69 63 61 74 65 73 2e 20 45 61 63 68 20 73 65 licates. Each se
1d32e 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65 73 20 69 t.** of values i
1d32f 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 s identified by
1d330 61 20 75 6e 69 71 75 65 20 50 34 20 76 61 6c 75 a unique P4 valu
1d331 65 2e 20 54 68 65 20 66 69 72 73 74 20 73 65 74 e. The first set
1d332 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 50 34 .** must have P4
1d333 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61 6c 20 73 ==0, the final s
1d334 65 74 20 50 34 3d 2d 31 2e 20 20 50 34 20 6d 75 et P4=-1. P4 mu
1d335 73 74 20 62 65 20 65 69 74 68 65 72 20 2d 31 20 st be either -1
1d336 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 or.** non-negati
1d337 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 ve. For non-neg
1d338 61 74 69 76 65 20 76 61 6c 75 65 73 20 6f 66 20 ative values of
1d339 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c 6f 77 65 P4 only the lowe
1d33a 72 20 34 0a 2a 2a 20 62 69 74 73 20 61 72 65 20 r 4.** bits are
1d33b 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a significant..**.
1d33c 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 6f ** This allows o
1d33d 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a 20 28 61 ptimizations: (a
1d33e 29 20 77 68 65 6e 20 50 34 3d 3d 30 20 74 68 65 ) when P4==0 the
1d33f 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
1d340 20 74 65 73 74 0a 2a 2a 20 74 68 65 20 72 6f 77 test.** the row
1d341 73 65 74 20 6f 62 6a 65 63 74 20 66 6f 72 20 50 set object for P
1d342 33 2c 20 61 73 20 69 74 20 69 73 20 67 75 61 72 3, as it is guar
1d343 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f 20 63 6f anteed not to co
1d344 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 ntain it,.** (b)
1d345 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20 74 68 65 when P4==-1 the
1d346 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
1d347 20 69 6e 73 65 72 74 20 74 68 65 20 76 61 6c 75 insert the valu
1d348 65 2c 20 61 73 20 69 74 20 77 69 6c 6c 0a 2a 2a e, as it will.**
1d349 20 6e 65 76 65 72 20 62 65 20 74 65 73 74 65 64 never be tested
1d34a 20 66 6f 72 2c 20 61 6e 64 20 28 63 29 20 77 68 for, and (c) wh
1d34b 65 6e 20 61 20 76 61 6c 75 65 20 74 68 61 74 20 en a value that
1d34c 69 73 20 70 61 72 74 20 6f 66 20 73 65 74 20 58 is part of set X
1d34d 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74 65 64 2c is.** inserted,
1d34e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
1d34f 64 20 74 6f 20 73 65 61 72 63 68 20 74 6f 20 73 d to search to s
1d350 65 65 20 69 66 20 74 68 65 20 73 61 6d 65 20 76 ee if the same v
1d351 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70 72 65 76 alue was.** prev
1d352 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20 iously inserted
1d353 61 73 20 70 61 72 74 20 6f 66 20 73 65 74 20 58 as part of set X
1d354 20 28 6f 6e 6c 79 20 69 66 20 69 74 20 77 61 73 (only if it was
1d355 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 previously.** i
1d356 6e 73 65 72 74 65 64 20 61 73 20 70 61 72 74 20 nserted as part
1d357 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 65 of some other se
1d358 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 t)..*/.case OP_R
1d359 6f 77 53 65 74 54 65 73 74 3a 20 7b 20 20 20 20 owSetTest: {
1d35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d35b 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 /* jump, in1, i
1d35c 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74 n3 */. int iSet
1d35d 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 = pOp->p4.i;.
1d35e 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c assert( pIn3->fl
1d35f 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a ags&MEM_Int );..
1d360 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 /* If there is
1d361 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20 anything other
1d362 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 6f 62 than a rowset ob
1d363 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 ject in memory c
1d364 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c ell P1,. ** del
1d365 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 20 69 ete it now and i
1d366 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 69 74 nitialize P1 wit
1d367 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 h an empty rowse
1d368 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 t. */. if( (pI
1d369 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1d36a 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 RowSet)==0 ){.
1d36b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d36c 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b SetRowSet(pIn1);
1d36d 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e . if( (pIn1->
1d36e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1d36f 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f et)==0 ) goto no
1d370 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 _mem;. }.. ass
1d371 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1d372 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 ==P4_INT32 );.
1d373 61 73 73 65 72 74 28 20 69 53 65 74 3d 3d 2d 31 assert( iSet==-1
1d374 20 7c 7c 20 69 53 65 74 3e 3d 30 20 29 3b 0a 20 || iSet>=0 );.
1d375 20 69 66 28 20 69 53 65 74 20 29 7b 0a 20 20 20 if( iSet ){.
1d376 20 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 int exists;.
1d377 20 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 65 exists = sqlite
1d378 33 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e 31 3RowSetTest(pIn1
1d379 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 0a 20 20 ->u.pRowSet, .
1d37a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d37b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75 38 (u8
1d37c 29 28 69 53 65 74 3e 3d 30 20 3f 20 69 53 65 74 )(iSet>=0 ? iSet
1d37d 20 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a & 0xf : 0xff),.
1d37e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d37f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1d380 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 In3->u.i);. i
1d381 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20 f( exists ){.
1d382 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1d383 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b - 1;. break
1d384 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1d385 28 20 69 53 65 74 3e 3d 30 20 29 7b 0a 20 20 20 ( iSet>=0 ){.
1d386 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e sqlite3RowSetIn
1d387 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f sert(pIn1->u.pRo
1d388 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e 69 29 wSet, pIn3->u.i)
1d389 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d38a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1d38b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f E_OMIT_TRIGGER./
1d38c 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78 * Opcode: Contex
1d38d 74 50 75 73 68 20 2a 20 2a 20 2a 20 0a 2a 2a 0a tPush * * * .**.
1d38e 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 ** Save the curr
1d38f 65 6e 74 20 56 64 62 65 20 63 6f 6e 74 65 78 74 ent Vdbe context
1d390 20 73 75 63 68 20 74 68 61 74 20 69 74 20 63 61 such that it ca
1d391 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 62 79 n be restored by
1d392 20 61 20 43 6f 6e 74 65 78 74 50 6f 70 0a 2a 2a a ContextPop.**
1d393 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 63 6f 6e opcode. The con
1d394 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 text stores the
1d395 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20 last insert row
1d396 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 id, the last sta
1d397 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 0a 2a 2a tement change.**
1d398 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20 count, and the
1d399 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e current statemen
1d39a 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a t change count..
1d39b 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65 */.case OP_Conte
1d39c 78 74 50 75 73 68 3a 20 7b 0a 20 20 69 6e 74 20 xtPush: {. int
1d39d 69 20 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 i = p->contextSt
1d39e 61 63 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74 ackTop++;. Cont
1d39f 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a ext *pContext;..
1d3a0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 assert( i>=0 )
1d3a1 3b 0a 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54 ;. /* FIX ME: T
1d3a2 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c his should be al
1d3a3 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74 20 located as part
1d3a4 6f 66 20 74 68 65 20 76 64 62 65 20 61 74 20 63 of the vdbe at c
1d3a5 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 ompile-time */.
1d3a6 20 69 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65 if( i>=p->conte
1d3a7 78 74 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a xtStackDepth ){.
1d3a8 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 p->contextSt
1d3a9 61 63 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a ackDepth = i+1;.
1d3aa 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 p->contextSt
1d3ab 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 ack = sqlite3DbR
1d3ac 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c eallocOrFree(db,
1d3ad 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b p->contextStack
1d3ae 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1d3af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3b0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 size
1d3b1 6f 66 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31 of(Context)*(i+1
1d3b2 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 ));. if( p->c
1d3b3 6f 6e 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29 ontextStack==0 )
1d3b4 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1d3b5 7d 0a 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26 }. pContext = &
1d3b6 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b p->contextStack[
1d3b7 69 5d 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e i];. pContext->
1d3b8 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e lastRowid = db->
1d3b9 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f lastRowid;. pCo
1d3ba 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d ntext->nChange =
1d3bb 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 p->nChange;. b
1d3bc 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d3bd 64 65 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a de: ContextPop *
1d3be 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 * * .**.** Rest
1d3bf 6f 72 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e ore the Vdbe con
1d3c0 74 65 78 74 20 74 6f 20 74 68 65 20 73 74 61 74 text to the stat
1d3c1 65 20 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e e it was in when
1d3c2 20 63 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73 contextPush was
1d3c3 20 6c 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 last.** execute
1d3c4 64 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 d. The context s
1d3c5 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 tores the last i
1d3c6 6e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 nsert row id, th
1d3c7 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 e last statement
1d3c8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 .** change count
1d3c9 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e , and the curren
1d3ca 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e t statement chan
1d3cb 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 ge count..*/.cas
1d3cc 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a e OP_ContextPop:
1d3cd 20 7b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 {. Context *pC
1d3ce 6f 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e ontext = &p->con
1d3cf 74 65 78 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 textStack[--p->c
1d3d0 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b ontextStackTop];
1d3d1 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6f . assert( p->co
1d3d2 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 ntextStackTop>=0
1d3d3 20 29 3b 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f );. db->lastRo
1d3d4 77 69 64 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e wid = pContext->
1d3d5 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e lastRowid;. p->
1d3d6 6e 43 68 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 nChange = pConte
1d3d7 78 74 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62 xt->nChange;. b
1d3d8 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1d3d9 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 * #ifndef SQLITE
1d3da 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f _OMIT_TRIGGER */
1d3db 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1d3dc 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1d3dd 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d ENT./* Opcode: M
1d3de 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 emMax P1 P2 * *
1d3df 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 *.**.** Set the
1d3e0 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1d3e1 72 20 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 r P1 to the maxi
1d3e2 6d 75 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 mum of its curre
1d3e3 6e 74 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 nt value.** and
1d3e4 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1d3e5 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1d3e6 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1d3e7 20 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 throws an error
1d3e8 20 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 if the memory c
1d3e9 65 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 ell is not initi
1d3ea 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 ally.** an integ
1d3eb 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d er..*/.case OP_M
1d3ec 65 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 emMax: {
1d3ed 2f 2a 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 /* in1, in2 */.
1d3ee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1d3ef 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b ntegerify(pIn1);
1d3f0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1d3f1 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 mIntegerify(pIn2
1d3f2 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 );. if( pIn1->u
1d3f3 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 .i<pIn2->u.i){.
1d3f4 20 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 pIn1->u.i = p
1d3f5 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 In2->u.i;. }.
1d3f6 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1d3f7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
1d3f8 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a UTOINCREMENT */.
1d3f9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f ./* Opcode: IfPo
1d3fa 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a s P1 P2 * * *.**
1d3fb 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1d3fc 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1d3fd 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c is 1 or greater,
1d3fe 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a jump to P2..**.
1d3ff 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1d400 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 to use this ins
1d401 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 truction on a re
1d402 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 gister that does
1d403 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 .** not contain
1d404 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 an integer. An
1d405 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1d406 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 will result if y
1d407 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 ou try..*/.case
1d408 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 OP_IfPos: {
1d409 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1d40a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e */. assert( pIn
1d40b 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 1->flags&MEM_Int
1d40c 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e );. if( pIn1->
1d40d 75 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 u.i>0 ){. pc
1d40e 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1d40f 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d410 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 /* Opcode: IfNeg
1d411 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1d412 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
1d413 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 of register P1 i
1d414 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f s less than zero
1d415 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a , jump to P2. .*
1d416 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 *.** It is illeg
1d417 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 al to use this i
1d418 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 nstruction on a
1d419 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f register that do
1d41a 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 es.** not contai
1d41b 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 n an integer. A
1d41c 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
1d41d 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 t will result if
1d41e 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 you try..*/.cas
1d41f 65 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 e OP_IfNeg: {
1d420 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1d421 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 1 */. assert( p
1d422 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 In1->flags&MEM_I
1d423 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 nt );. if( pIn1
1d424 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 ->u.i<0 ){.
1d425 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1d426 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d427 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a ../* Opcode: IfZ
1d428 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ero P1 P2 * * *.
1d429 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c **.** If the val
1d42a 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 ue of register P
1d42b 31 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 1 is exactly 0,
1d42c 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a jump to P2. .**.
1d42d 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1d42e 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 to use this ins
1d42f 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 truction on a re
1d430 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 gister that does
1d431 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 .** not contain
1d432 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 an integer. An
1d433 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1d434 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 will result if y
1d435 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 ou try..*/.case
1d436 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 OP_IfZero: {
1d437 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
1d438 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 */. assert( pI
1d439 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e n1->flags&MEM_In
1d43a 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d t );. if( pIn1-
1d43b 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 >u.i==0 ){.
1d43c 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1d43d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d43e 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 ../* Opcode: Agg
1d43f 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 Step * P2 P3 P4
1d440 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 P5.**.** Execute
1d441 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 the step functi
1d442 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 on for an aggreg
1d443 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e ate. The.** fun
1d444 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 ction has P5 arg
1d445 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 uments. P4 is
1d446 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1d447 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 FuncDef.** stru
1d448 63 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69 cture that speci
1d449 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f fies the functio
1d44a 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74 65 72 n. Use register
1d44b 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 .** P3 as the ac
1d44c 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a cumulator..**.**
1d44d 20 54 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 The P5 argument
1d44e 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
1d44f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 register P2 and
1d450 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f its.** successo
1d451 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 rs..*/.case OP_A
1d452 67 67 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 ggStep: {. int
1d453 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 n = pOp->p5;. i
1d454 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 nt i;. Mem *pMe
1d455 6d 2c 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 m, *pRec;. sqli
1d456 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1d457 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1d458 20 2a 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 **apVal;.. ass
1d459 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 ert( n>=0 );. p
1d45a 52 65 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Rec = &p->aMem[p
1d45b 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c Op->p2];. apVal
1d45c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 = p->apArg;. a
1d45d 73 73 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 ssert( apVal ||
1d45e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d n==0 );. for(i=
1d45f 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 0; i<n; i++, pRe
1d460 63 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b c++){. apVal[
1d461 69 5d 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 i] = pRec;. s
1d462 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 toreTypeInfo(pRe
1d463 63 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 c, encoding);.
1d464 7d 0a 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 }. ctx.pFunc =
1d465 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 pOp->p4.pFunc;.
1d466 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1d467 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1d468 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e ->nMem );. ctx.
1d469 70 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 pMem = pMem = &p
1d46a 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1d46b 0a 20 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 . pMem->n++;.
1d46c 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
1d46d 4d 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e M_Null;. ctx.s.
1d46e 7a 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a z = 0;. ctx.s.z
1d46f 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 Malloc = 0;. ct
1d470 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 x.s.xDel = 0;.
1d471 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 ctx.s.db = db;.
1d472 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 ctx.isError = 0
1d473 3b 0a 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 ;. ctx.pColl =
1d474 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 0;. if( ctx.pFu
1d475 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 nc->flags & SQLI
1d476 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
1d477 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1d478 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 pOp>p->aOp );.
1d479 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
1d47a 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c ].p4type==P4_COL
1d47b 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 LSEQ );. asse
1d47c 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f rt( pOp[-1].opco
1d47d 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 de==OP_CollSeq )
1d47e 3b 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 ;. ctx.pColl
1d47f 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f = pOp[-1].p4.pCo
1d480 6c 6c 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 ll;. }. (ctx.p
1d481 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 Func->xStep)(&ct
1d482 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 x, n, apVal);.
1d483 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 if( ctx.isError
1d484 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 ){. sqlite3Se
1d485 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1d486 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 Msg, db, "%s", s
1d487 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1d488 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 t(&ctx.s));.
1d489 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 rc = ctx.isError
1d48a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
1d48b 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 dbeMemRelease(&c
1d48c 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a tx.s);. break;.
1d48d 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 }../* Opcode: Ag
1d48e 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 gFinal P1 P2 * P
1d48f 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 4 *.**.** Execut
1d490 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 e the finalizer
1d491 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 function for an
1d492 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 aggregate. P1 i
1d493 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 s.** the memory
1d494 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 location that is
1d495 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1d496 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 for the aggrega
1d497 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 te..**.** P2 is
1d498 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 the number of ar
1d499 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 guments that the
1d49a 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 step function t
1d49b 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 akes and.** P4 i
1d49c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1d49d 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 he FuncDef for t
1d49e 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 his function. T
1d49f 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e he P2.** argumen
1d4a0 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 t is not used by
1d4a1 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 this opcode. I
1d4a2 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 t is only there
1d4a3 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a to disambiguate.
1d4a4 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ** functions tha
1d4a5 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 t can take varyi
1d4a6 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 ng numbers of ar
1d4a7 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a guments. The.**
1d4a8 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 P4 argument is
1d4a9 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 only needed for
1d4aa 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 the degenerate c
1d4ab 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 ase where.** the
1d4ac 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 step function w
1d4ad 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c as not previousl
1d4ae 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 y called..*/.cas
1d4af 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b e OP_AggFinal: {
1d4b0 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 . Mem *pMem;.
1d4b1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1d4b2 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 0 && pOp->p1<=p-
1d4b3 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 >nMem );. pMem
1d4b4 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1d4b5 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 p1];. assert( (
1d4b6 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 pMem->flags & ~(
1d4b7 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 MEM_Null|MEM_Agg
1d4b8 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 ))==0 );. rc =
1d4b9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 sqlite3VdbeMemFi
1d4ba 6e 61 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 nalize(pMem, pOp
1d4bb 2d 3e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 ->p4.pFunc);. i
1d4bc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 f( rc==SQLITE_ER
1d4bd 52 4f 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ROR ){. sqlit
1d4be 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1d4bf 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1d4c0 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ", sqlite3_value
1d4c1 5f 74 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20 _text(pMem));.
1d4c2 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 }. sqlite3VdbeC
1d4c3 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d hangeEncoding(pM
1d4c4 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 em, encoding);.
1d4c5 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1d4c6 53 49 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 SIZE(pMem);. if
1d4c7 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
1d4c8 54 6f 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a TooBig(pMem) ){.
1d4c9 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1d4ca 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d4cb 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ...#if !defined(
1d4cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 SQLITE_OMIT_VACU
1d4cd 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 UM) && !defined(
1d4ce 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 SQLITE_OMIT_ATTA
1d4cf 43 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 CH)./* Opcode: V
1d4d0 61 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a acuum * * * * *.
1d4d1 2a 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 **.** Vacuum the
1d4d2 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 entire database
1d4d3 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 . This opcode w
1d4d4 69 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 ill cause other
1d4d5 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 virtual.** machi
1d4d6 6e 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 nes to be create
1d4d7 64 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d d and run. It m
1d4d8 61 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 ay not be called
1d4d9 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 from within.**
1d4da 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a a transaction..*
1d4db 2f 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d /.case OP_Vacuum
1d4dc 3a 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 : {. if( sqlite
1d4dd 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1d4de 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1d4df 74 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 to_misuse; . rc
1d4e0 20 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 = sqlite3RunVac
1d4e1 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c uum(&p->zErrMsg,
1d4e2 20 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 db);. if( sqli
1d4e3 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1d4e4 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1d4e5 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 _to_misuse;. br
1d4e6 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 eak;.}.#endif..#
1d4e7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
1d4e8 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1d4e9 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 UM)./* Opcode: I
1d4ea 6e 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 ncrVacuum P1 P2
1d4eb 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 * * *.**.** Perf
1d4ec 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 orm a single ste
1d4ed 70 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 p of the increme
1d4ee 6e 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 ntal vacuum proc
1d4ef 65 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 edure on.** the
1d4f0 50 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 P1 database. If
1d4f1 74 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 the vacuum has f
1d4f2 69 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f inished, jump to
1d4f3 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 instruction.**
1d4f4 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 P2. Otherwise, f
1d4f5 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
1d4f6 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1d4f7 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1d4f8 49 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 IncrVacuum: {
1d4f9 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1d4fa 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 Btree *pBt;..
1d4fb 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1d4fc 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 >=0 && pOp->p1<d
1d4fd 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 b->nDb );. asse
1d4fe 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1d4ff 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 k & (1<<pOp->p1)
1d500 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 )!=0 );. pBt =
1d501 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1d502 2e 70 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c .pBt;. rc = sql
1d503 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 ite3BtreeIncrVac
1d504 75 75 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 uum(pBt);. if(
1d505 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
1d506 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1d507 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 >p2 - 1;. rc
1d508 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
1d509 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1d50a 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 if../* Opcode: E
1d50b 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a xpire P1 * * * *
1d50c 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 .**.** Cause pre
1d50d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 compiled stateme
1d50e 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 nts to become ex
1d50f 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 pired. An expire
1d510 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 d statement.** f
1d511 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 ails with an err
1d512 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 or code of SQLIT
1d513 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 E_SCHEMA if it i
1d514 73 20 65 76 65 72 20 65 78 65 63 75 74 65 64 20 s ever executed
1d515 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 .** (via sqlite3
1d516 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a _step())..** .**
1d517 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 If P1 is 0, the
1d518 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d n all SQL statem
1d519 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 ents become expi
1d51a 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f red. If P1 is no
1d51b 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 n-zero,.** then
1d51c 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 only the current
1d51d 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 ly executing sta
1d51e 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 tement is affect
1d51f 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ed. .*/.case OP_
1d520 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 Expire: {. if(
1d521 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 !pOp->p1 ){.
1d522 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1d523 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1d524 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 db);. }else{.
1d525 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 p->expired = 1
1d526 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d527 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1d528 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1d529 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 HE./* Opcode: Ta
1d52a 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 bleLock P1 P2 P3
1d52b 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 P4 *.**.** Obta
1d52c 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 in a lock on a p
1d52d 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e articular table.
1d52e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1d52f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 n is only used w
1d530 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 hen.** the share
1d531 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 d-cache feature
1d532 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a is enabled. .**.
1d533 2a 2a 20 49 66 20 50 31 20 69 73 20 20 74 68 65 ** If P1 is the
1d534 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 index of the da
1d535 74 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 tabase in sqlite
1d536 33 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 3.aDb[] of the d
1d537 61 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 atabase.** on wh
1d538 69 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ich the lock is
1d539 61 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 acquired. A rea
1d53a 64 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 dlock is obtaine
1d53b 64 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a d if P3==0 or.**
1d53c 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 a write lock if
1d53d 20 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 P3==1..**.** P2
1d53e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f contains the ro
1d53f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 ot-page of the t
1d540 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a able to lock..**
1d541 0a 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 .** P4 contains
1d542 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1d543 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
1d544 6c 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e le being locked.
1d545 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a This is only.**
1d546 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
1d547 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 e an error messa
1d548 67 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 ge if the lock c
1d549 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 annot be obtaine
1d54a 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 d..*/.case OP_Ta
1d54b 62 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 bleLock: {. int
1d54c 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a p1 = pOp->p1; .
1d54d 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b u8 isWriteLock
1d54e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a = (u8)pOp->p3;.
1d54f 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 assert( p1>=0
1d550 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b && p1<db->nDb );
1d551 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1d552 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1d553 31 29 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 1))!=0 );. asse
1d554 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d rt( isWriteLock=
1d555 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 =0 || isWriteLoc
1d556 6b 3d 3d 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 k==1 );. rc = s
1d557 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 qlite3BtreeLockT
1d558 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d able(db->aDb[p1]
1d559 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 .pBt, pOp->p2, i
1d55a 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 69 sWriteLock);. i
1d55b 66 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51 f( (rc&0xFF)==SQ
1d55c 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 LITE_LOCKED ){.
1d55d 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1d55e 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
1d55f 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d560 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d561 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 74 61 db, "database ta
1d562 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 ble is locked: %
1d563 73 22 2c 20 7a 29 3b 0a 20 20 7d 0a 20 20 62 72 s", z);. }. br
1d564 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1d565 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1d566 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
1d567 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d568 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1d569 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e * Opcode: VBegin
1d56a 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1d56b 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f * P4 may be a po
1d56c 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 inter to an sqli
1d56d 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1d56e 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20 re. If so, call
1d56f 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d the .** xBegin m
1d570 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74 ethod for that t
1d571 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f able..**.** Also
1d572 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 , whether or not
1d573 20 50 34 20 69 73 20 73 65 74 2c 20 63 68 65 63 P4 is set, chec
1d574 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 6e k that this is n
1d575 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 ot being called
1d576 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61 from.** within a
1d577 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76 callback to a v
1d578 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79 irtual table xSy
1d579 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20 nc() method. If
1d57a 69 74 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72 it is, the error
1d57b 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 .** code will be
1d57c 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c set to SQLITE_L
1d57d 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f OCKED..*/.case O
1d57e 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 20 20 73 71 P_VBegin: {. sq
1d57f 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1d580 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 b = pOp->p4.pVta
1d581 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 b;. rc = sqlite
1d582 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 70 3VtabBegin(db, p
1d583 56 74 61 62 29 3b 0a 20 20 69 66 28 20 70 56 74 Vtab);. if( pVt
1d584 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ab ){. sqlite
1d585 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1d586 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e ErrMsg);. p->
1d587 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d zErrMsg = pVtab-
1d588 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 >zErrMsg;. pV
1d589 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 tab->zErrMsg = 0
1d58a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d58b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d58c 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d58d 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1d58e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1d58f 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1d590 64 65 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a de: VCreate P1 *
1d591 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 * P4 *.**.** P4
1d592 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
1d593 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1d594 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 in database P1.
1d595 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 Call the xCreate
1d596 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 method.** for t
1d597 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 hat table..*/.ca
1d598 73 65 20 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b se OP_VCreate: {
1d599 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1d59a 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 64 62 tabCallCreate(db
1d59b 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1d59c 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 p4.z, &p->zErrMs
1d59d 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 g);. break;.}.#
1d59e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1d59f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1d5a0 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1d5a1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1d5a2 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1d5a3 3a 20 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20 : VDestroy P1 *
1d5a4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1d5a5 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 is the name of a
1d5a6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
1d5a7 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 n database P1.
1d5a8 43 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f Call the xDestro
1d5a9 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 y method.** of t
1d5aa 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 hat table..*/.ca
1d5ab 73 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 se OP_VDestroy:
1d5ac 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 {. p->inVtabMet
1d5ad 68 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 hod = 2;. rc =
1d5ae 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 sqlite3VtabCallD
1d5af 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e estroy(db, pOp->
1d5b0 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a p1, pOp->p4.z);.
1d5b1 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1d5b2 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a d = 0;. break;.
1d5b3 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d5b4 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1d5b5 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ABLE */..#ifndef
1d5b6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1d5b7 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 TUALTABLE./* Opc
1d5b8 6f 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 ode: VOpen P1 *
1d5b9 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1d5ba 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1d5bb 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1d5bc 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 object, an sqlit
1d5bd 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 e3_vtab structur
1d5be 65 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 e..** P1 is a cu
1d5bf 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 rsor number. Th
1d5c0 69 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 is opcode opens
1d5c1 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 a cursor to the
1d5c2 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 virtual.** table
1d5c3 20 61 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74 and stores that
1d5c4 20 63 75 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a cursor in P1..*
1d5c5 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a /.case OP_VOpen:
1d5c6 20 7b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 {. VdbeCursor
1d5c7 2a 70 43 75 72 20 3d 20 30 3b 0a 20 20 73 71 6c *pCur = 0;. sql
1d5c8 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1d5c9 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 *pVtabCursor =
1d5ca 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 0;.. sqlite3_vt
1d5cb 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d ab *pVtab = pOp-
1d5cc 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c >p4.pVtab;. sql
1d5cd 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1d5ce 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f dule = (sqlite3_
1d5cf 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e module *)pVtab->
1d5d0 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 61 73 73 65 pModule;.. asse
1d5d1 72 74 28 70 56 74 61 62 20 26 26 20 70 4d 6f 64 rt(pVtab && pMod
1d5d2 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 ule);. if( sqli
1d5d3 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1d5d4 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1d5d5 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 e_to_misuse;. r
1d5d6 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 c = pModule->xOp
1d5d7 65 6e 28 70 56 74 61 62 2c 20 26 70 56 74 61 62 en(pVtab, &pVtab
1d5d8 43 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74 Cursor);. sqlit
1d5d9 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1d5da 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a zErrMsg);. p->z
1d5db 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1d5dc 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 zErrMsg;. pVtab
1d5dd 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1d5de 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1d5df 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1d5e0 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1d5e1 75 73 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 use;. if( SQLIT
1d5e2 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK==rc ){.
1d5e3 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 /* Initialize sq
1d5e4 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1d5e5 72 20 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a r base class */.
1d5e6 20 20 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d pVtabCursor-
1d5e7 3e 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a >pVtab = pVtab;.
1d5e8 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 . /* Initiali
1d5e9 73 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f se vdbe cursor o
1d5ea 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75 bject */. pCu
1d5eb 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 r = allocateCurs
1d5ec 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 or(p, pOp->p1, 0
1d5ed 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 , -1, 0);. if
1d5ee 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 20 20 ( pCur ){.
1d5ef 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1d5f0 72 20 3d 20 70 56 74 61 62 43 75 72 73 6f 72 3b r = pVtabCursor;
1d5f1 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4d 6f . pCur->pMo
1d5f2 64 75 6c 65 20 3d 20 70 56 74 61 62 43 75 72 73 dule = pVtabCurs
1d5f3 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 or->pVtab->pModu
1d5f4 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 le;. }else{.
1d5f5 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
1d5f6 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
1d5f7 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 pModule->xClose
1d5f8 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 (pVtabCursor);.
1d5f9 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1d5fa 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1d5fb 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1d5fc 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1d5fd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1d5fe 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1d5ff 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 pcode: VFilter P
1d600 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1d601 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f ** P1 is a curso
1d602 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 r opened using V
1d603 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 Open. P2 is an
1d604 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 address to jump
1d605 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c to if.** the fil
1d606 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 tered result set
1d607 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a is empty..**.**
1d608 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 P4 is either NU
1d609 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 LL or a string t
1d60a 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 hat was generate
1d60b 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e d by the xBestIn
1d60c 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 dex.** method of
1d60d 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 the module. Th
1d60e 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e e interpretation
1d60f 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e of the P4 strin
1d610 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 g is left.** to
1d611 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 the module imple
1d612 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a mentation..**.**
1d613 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1d614 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 okes the xFilter
1d615 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 method on the v
1d616 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 irtual table spe
1d617 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e cified.** by P1.
1d618 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 The integer qu
1d619 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 ery plan paramet
1d61a 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 er to xFilter is
1d61b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1d61c 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 ter.** P3. Regis
1d61d 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 ter P3+1 stores
1d61e 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 the argc paramet
1d61f 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 er to be passed
1d620 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 to the.** xFilte
1d621 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 r method. Regist
1d622 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 ers P3+2..P3+1+a
1d623 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 rgc are the argc
1d624 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 .** additional p
1d625 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 arameters which
1d626 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a are passed to.**
1d627 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 xFilter as argv
1d628 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 . Register P3+2
1d629 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 becomes argv[0]
1d62a 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 when passed to x
1d62b 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 Filter..**.** A
1d62c 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 jump is made to
1d62d 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 P2 if the result
1d62e 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 set after filte
1d62f 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d ring would be em
1d630 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f pty..*/.case OP_
1d631 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 VFilter: { /*
1d632 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 jump */. int nA
1d633 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65 72 79 rg;. int iQuery
1d634 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ;. const sqlite
1d635 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1d636 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 e;. Mem *pQuery
1d637 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1d638 3e 70 33 5d 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 >p3];. Mem *pAr
1d639 67 63 20 3d 20 26 70 51 75 65 72 79 5b 31 5d 3b gc = &pQuery[1];
1d63a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f . sqlite3_vtab_
1d63b 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1d63c 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 sor;. sqlite3_v
1d63d 74 61 62 20 2a 70 56 74 61 62 3b 0a 0a 20 20 56 tab *pVtab;.. V
1d63e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 dbeCursor *pCur
1d63f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1d640 70 31 5d 3b 0a 0a 20 20 52 45 47 49 53 54 45 52 p1];.. REGISTER
1d641 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 _TRACE(pOp->p3,
1d642 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 pQuery);. asser
1d643 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 t( pCur->pVtabCu
1d644 72 73 6f 72 20 29 3b 0a 20 20 70 56 74 61 62 43 rsor );. pVtabC
1d645 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 56 ursor = pCur->pV
1d646 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 70 56 74 tabCursor;. pVt
1d647 61 62 20 3d 20 70 56 74 61 62 43 75 72 73 6f 72 ab = pVtabCursor
1d648 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 ->pVtab;. pModu
1d649 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 le = pVtab->pMod
1d64a 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 ule;.. /* Grab
1d64b 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 the index number
1d64c 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65 and argc parame
1d64d 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 ters */. assert
1d64e 28 20 28 70 51 75 65 72 79 2d 3e 66 6c 61 67 73 ( (pQuery->flags
1d64f 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26 26 20 &MEM_Int)!=0 &&
1d650 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 pArgc->flags==ME
1d651 4d 5f 49 6e 74 20 29 3b 0a 20 20 6e 41 72 67 20 M_Int );. nArg
1d652 3d 20 28 69 6e 74 29 70 41 72 67 63 2d 3e 75 2e = (int)pArgc->u.
1d653 69 3b 0a 20 20 69 51 75 65 72 79 20 3d 20 28 69 i;. iQuery = (i
1d654 6e 74 29 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a nt)pQuery->u.i;.
1d655 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
1d656 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 xFilter method
1d657 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 72 */. {. int r
1d658 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 es = 0;. int
1d659 69 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 i;. Mem **apA
1d65a 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 rg = p->apArg;.
1d65b 20 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 3c for(i = 0; i<
1d65c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nArg; i++){.
1d65d 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 26 70 41 apArg[i] = &pA
1d65e 72 67 63 5b 69 2b 31 5d 3b 0a 20 20 20 20 20 20 rgc[i+1];.
1d65f 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 61 70 storeTypeInfo(ap
1d660 41 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20 20 20 Arg[i], 0);.
1d661 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }.. if( sqlit
1d662 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1d663 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1d664 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 _to_misuse;.
1d665 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
1d666 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e 69 pVtab);. p->i
1d667 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b nVtabMethod = 1;
1d668 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c . rc = pModul
1d669 65 2d 3e 78 46 69 6c 74 65 72 28 70 56 74 61 62 e->xFilter(pVtab
1d66a 43 75 72 73 6f 72 2c 20 69 51 75 65 72 79 2c 20 Cursor, iQuery,
1d66b 70 4f 70 2d 3e 70 34 2e 7a 2c 20 6e 41 72 67 2c pOp->p4.z, nArg,
1d66c 20 61 70 41 72 67 29 3b 0a 20 20 20 20 70 2d 3e apArg);. p->
1d66d 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 inVtabMethod = 0
1d66e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1d66f 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1d670 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 sg);. p->zErr
1d671 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 Msg = pVtab->zEr
1d672 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d rMsg;. pVtab-
1d673 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1d674 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c sqlite3VtabUnl
1d675 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a ock(db, pVtab);.
1d676 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1d677 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1d678 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45 es = pModule->xE
1d679 6f 66 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b of(pVtabCursor);
1d67a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
1d67b 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1d67c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1d67d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a due_to_misuse;..
1d67e 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 if( res ){.
1d67f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1d680 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 2 - 1;. }. }
1d681 0a 20 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 . pCur->nullRow
1d682 20 3d 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a = 0;.. break;.
1d683 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d684 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1d685 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ABLE */..#ifndef
1d686 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1d687 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 TUALTABLE./* Opc
1d688 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 ode: VColumn P1
1d689 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1d68a 53 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 Store the value
1d68b 6f 66 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c of the P2-th col
1d68c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f umn of.** the ro
1d68d 77 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c w of the virtual
1d68e 2d 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 -table that the
1d68f 0a 2a 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73 .** P1 cursor is
1d690 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 pointing to int
1d691 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1d692 2f 0a 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d /.case OP_VColum
1d693 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 n: {. sqlite3_v
1d694 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f tab *pVtab;. co
1d695 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1d696 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d le *pModule;. M
1d697 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 73 71 6c em *pDest;. sql
1d698 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f ite3_context sCo
1d699 6e 74 65 78 74 3b 0a 0a 20 20 56 64 62 65 43 75 ntext;.. VdbeCu
1d69a 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e rsor *pCur = p->
1d69b 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1d69c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1d69d 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 pVtabCursor );.
1d69e 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1d69f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1d6a0 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73 ->nMem );. pDes
1d6a1 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1d6a2 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 70 43 75 ->p3];. if( pCu
1d6a3 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 r->nullRow ){.
1d6a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d6a5 53 65 74 4e 75 6c 6c 28 70 44 65 73 74 29 3b 0a SetNull(pDest);.
1d6a6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1d6a7 20 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 pVtab = pCur->p
1d6a8 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1d6a9 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 b;. pModule = p
1d6aa 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 Vtab->pModule;.
1d6ab 20 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 assert( pModule
1d6ac 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d ->xColumn );. m
1d6ad 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c emset(&sContext,
1d6ae 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 0, sizeof(sCont
1d6af 65 78 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 ext));.. /* The
1d6b0 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 output cell may
1d6b1 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 already have a
1d6b2 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
1d6b3 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 . Move. ** the
1d6b4 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 current contents
1d6b5 20 74 6f 20 73 43 6f 6e 74 65 78 74 2e 73 20 73 to sContext.s s
1d6b6 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 o in case the us
1d6b7 65 72 2d 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a er-function . *
1d6b8 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c * can use the al
1d6b9 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 ready allocated
1d6ba 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f buffer instead o
1d6bb 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 0a f allocating a .
1d6bc 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 ** new one..
1d6bd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1d6be 4d 65 6d 4d 6f 76 65 28 26 73 43 6f 6e 74 65 78 MemMove(&sContex
1d6bf 74 2e 73 2c 20 70 44 65 73 74 29 3b 0a 20 20 4d t.s, pDest);. M
1d6c0 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 73 emSetTypeFlag(&s
1d6c1 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e Context.s, MEM_N
1d6c2 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c ull);.. if( sql
1d6c3 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1d6c4 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1d6c5 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1d6c6 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 rc = pModule->xC
1d6c7 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 olumn(pCur->pVta
1d6c8 62 43 75 72 73 6f 72 2c 20 26 73 43 6f 6e 74 65 bCursor, &sConte
1d6c9 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 xt, pOp->p2);.
1d6ca 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d6cb 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1d6cc 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 p->zErrMsg = pV
1d6cd 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1d6ce 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1d6cf 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 0;.. /* Copy t
1d6d0 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1d6d1 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 function to the
1d6d2 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 P3 register. We
1d6d3 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 . ** do this re
1d6d4 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
1d6d5 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 her or not an er
1d6d6 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20 ror occurred to
1d6d7 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 ensure any. **
1d6d8 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 dynamic allocati
1d6d9 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 78 74 2e 73 on in sContext.s
1d6da 20 28 61 20 4d 65 6d 20 73 74 72 75 63 74 29 20 (a Mem struct)
1d6db 69 73 20 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 is released..
1d6dc 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1d6dd 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 ChangeEncoding(&
1d6de 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f sContext.s, enco
1d6df 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54 45 ding);. REGISTE
1d6e0 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1d6e1 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 pDest);. sqlit
1d6e2 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 44 e3VdbeMemMove(pD
1d6e3 65 73 74 2c 20 26 73 43 6f 6e 74 65 78 74 2e 73 est, &sContext.s
1d6e4 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f );. UPDATE_MAX_
1d6e5 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 3b BLOBSIZE(pDest);
1d6e6 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 .. if( sqlite3S
1d6e7 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 afetyOn(db) ){.
1d6e8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1d6e9 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d e_to_misuse;. }
1d6ea 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1d6eb 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 44 65 73 beMemTooBig(pDes
1d6ec 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 t) ){. goto t
1d6ed 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 oo_big;. }. br
1d6ee 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1d6ef 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1d6f0 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1d6f1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d6f2 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1d6f3 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78 74 20 * Opcode: VNext
1d6f4 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1d6f5 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74 75 61 * Advance virtua
1d6f6 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20 74 68 l table P1 to th
1d6f7 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 69 74 e next row in it
1d6f8 73 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 s result set and
1d6f9 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 .** jump to inst
1d6fa 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f 72 2c ruction P2. Or,
1d6fb 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 if the virtual
1d6fc 74 61 62 6c 65 20 68 61 73 20 72 65 61 63 68 65 table has reache
1d6fd 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 d.** the end of
1d6fe 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 2c 20 its result set,
1d6ff 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 then fall throug
1d700 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e h to the next in
1d701 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1d702 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b 20 20 se OP_VNext: {
1d703 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 73 71 /* jump */. sq
1d704 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1d705 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 b;. const sqlit
1d706 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1d707 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 le;. int res =
1d708 30 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 0;.. VdbeCursor
1d709 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 *pCur = p->apCs
1d70a 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1d70b 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 sert( pCur->pVta
1d70c 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 bCursor );. if(
1d70d 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 pCur->nullRow )
1d70e 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d {. break;. }
1d70f 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 72 2d . pVtab = pCur-
1d710 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 >pVtabCursor->pV
1d711 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d tab;. pModule =
1d712 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b pVtab->pModule;
1d713 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64 75 . assert( pModu
1d714 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 le->xNext );..
1d715 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e /* Invoke the xN
1d716 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 ext() method of
1d717 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 the module. Ther
1d718 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 e is no way for
1d719 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 the. ** underly
1d71a 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
1d71b 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 on to return an
1d71c 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 error if one occ
1d71d 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 urs during. **
1d71e 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64 xNext(). Instead
1d71f 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 , if an error oc
1d720 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65 curs, true is re
1d721 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69 turned (indicati
1d722 6e 67 20 74 68 61 74 20 0a 20 20 2a 2a 20 64 61 ng that . ** da
1d723 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 ta is available)
1d724 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 and the error c
1d725 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 ode returned whe
1d726 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a n xColumn or. *
1d727 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 * some other met
1d728 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f hod is next invo
1d729 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 ked on the save
1d72a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 virtual table cu
1d72b 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rsor.. */. if(
1d72c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1d72d 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1d72e 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1d72f 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c ;. sqlite3VtabL
1d730 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 70 2d ock(pVtab);. p-
1d731 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1d732 31 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 1;. rc = pModul
1d733 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 2d 3e 70 e->xNext(pCur->p
1d734 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 VtabCursor);. p
1d735 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1d736 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 0;. sqlite3DbF
1d737 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1d738 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1d739 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d g = pVtab->zErrM
1d73a 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 sg;. pVtab->zEr
1d73b 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 rMsg = 0;. sqli
1d73c 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 te3VtabUnlock(db
1d73d 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28 20 , pVtab);. if(
1d73e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1d73f 0a 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 75 . res = pModu
1d740 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72 2d 3e 70 le->xEof(pCur->p
1d741 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d VtabCursor);. }
1d742 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1d743 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1d744 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1d745 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21 72 isuse;.. if( !r
1d746 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 es ){. /* If
1d747 74 68 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a there is data, j
1d748 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 ump to P2 */.
1d749 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1d74a 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1d74b 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d74c 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1d74d 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ABLE */..#ifndef
1d74e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1d74f 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 TUALTABLE./* Opc
1d750 6f 64 65 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 ode: VRename P1
1d751 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 * * P4 *.**.** P
1d752 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1d753 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c o a virtual tabl
1d754 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c e object, an sql
1d755 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
1d756 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ure..** This opc
1d757 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ode invokes the
1d758 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 corresponding xR
1d759 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 ename method. Th
1d75a 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 e value.** in re
1d75b 67 69 73 74 65 72 20 50 31 20 69 73 20 70 61 73 gister P1 is pas
1d75c 73 65 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 sed as the zName
1d75d 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1d75e 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e xRename method.
1d75f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e .*/.case OP_VRen
1d760 61 6d 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 ame: {. sqlite3
1d761 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 _vtab *pVtab = p
1d762 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 Op->p4.pVtab;.
1d763 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d 20 26 70 2d Mem *pName = &p-
1d764 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1d765 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 2d assert( pVtab-
1d766 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d >pModule->xRenam
1d767 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f e );. REGISTER_
1d768 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 TRACE(pOp->p1, p
1d769 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 72 69 6e 67 Name);.. String
1d76a 69 66 79 28 70 4e 61 6d 65 2c 20 65 6e 63 6f 64 ify(pName, encod
1d76b 69 6e 67 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c ing);.. if( sql
1d76c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1d76d 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1d76e 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1d76f 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
1d770 70 56 74 61 62 29 3b 0a 20 20 72 63 20 3d 20 70 pVtab);. rc = p
1d771 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 Vtab->pModule->x
1d772 52 65 6e 61 6d 65 28 70 56 74 61 62 2c 20 70 4e Rename(pVtab, pN
1d773 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 ame->z);. sqlit
1d774 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1d775 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a zErrMsg);. p->z
1d776 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e ErrMsg = pVtab->
1d777 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 zErrMsg;. pVtab
1d778 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1d779 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f sqlite3VtabUnlo
1d77a 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 ck(db, pVtab);.
1d77b 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1d77c 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1d77d 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1d77e 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d use;.. break;.}
1d77f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1d780 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1d781 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 TUALTABLE./* Opc
1d782 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50 31 20 ode: VUpdate P1
1d783 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1d784 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1d785 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1d786 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 ble object, an s
1d787 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1d788 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f cture..** This o
1d789 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 pcode invokes th
1d78a 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1d78b 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 xUpdate method.
1d78c 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 P2 values.** are
1d78d 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f contiguous memo
1d78e 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74 69 6e ry cells startin
1d78f 67 20 61 74 20 50 33 20 74 6f 20 70 61 73 73 20 g at P3 to pass
1d790 74 6f 20 74 68 65 20 78 55 70 64 61 74 65 20 0a to the xUpdate .
1d791 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 ** invocation. T
1d792 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d793 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29 20 63 ster (P3+P2-1) c
1d794 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
1d795 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 e .** p2th eleme
1d796 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76 20 61 nt of the argv a
1d797 72 72 61 79 20 70 61 73 73 65 64 20 74 6f 20 78 rray passed to x
1d798 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 Update..**.** Th
1d799 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 e xUpdate method
1d79a 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 will do a DELET
1d79b 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f E or an INSERT o
1d79c 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 r both..** The a
1d79d 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 rgv[0] element (
1d79e 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 which correspond
1d79f 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c s to memory cell
1d7a0 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 P3).** is the r
1d7a1 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20 74 6f owid of a row to
1d7a2 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61 72 67 delete. If arg
1d7a3 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 v[0] is NULL the
1d7a4 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f n no .** deletio
1d7a5 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 61 n occurs. The a
1d7a6 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 rgv[1] element i
1d7a7 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 s the rowid of t
1d7a8 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 he new .** row.
1d7a9 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e 55 4c This can be NUL
1d7aa 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20 76 69 L to have the vi
1d7ab 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 rtual table sele
1d7ac 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 ct the new .** r
1d7ad 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c 66 2e owid for itself.
1d7ae 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 The subsequent
1d7af 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 elements in the
1d7b0 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 array are .** t
1d7b1 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c he values of col
1d7b2 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65 77 20 umns in the new
1d7b3 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 row..**.** If P2
1d7b4 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 ==1 then no inse
1d7b5 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e rt is performed.
1d7b6 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65 argv[0] is the
1d7b7 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 rowid of.** a r
1d7b8 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a ow to delete..**
1d7b9 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f 6f 6c .** P1 is a bool
1d7ba 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69 74 20 ean flag. If it
1d7bb 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 61 is set to true a
1d7bc 6e 64 20 74 68 65 20 78 55 70 64 61 74 65 20 63 nd the xUpdate c
1d7bd 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 all.** is succes
1d7be 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 sful, then the v
1d7bf 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
1d7c0 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e sqlite3_last_in
1d7c1 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a sert_rowid() .**
1d7c2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 is set to the v
1d7c3 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 alue of the rowi
1d7c4 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 d for the row ju
1d7c5 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a st inserted..*/.
1d7c6 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74 65 3a case OP_VUpdate:
1d7c7 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 {. sqlite3_vta
1d7c8 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e b *pVtab = pOp->
1d7c9 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 p4.pVtab;. sqli
1d7ca 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1d7cb 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d ule = (sqlite3_m
1d7cc 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 odule *)pVtab->p
1d7cd 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 Module;. int nA
1d7ce 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 rg = pOp->p2;.
1d7cf 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1d7d0 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a ype==P4_VTAB );.
1d7d1 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 if( pModule->x
1d7d2 55 70 64 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 Update==0 ){.
1d7d3 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1d7d4 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1d7d5 62 2c 20 22 72 65 61 64 2d 6f 6e 6c 79 20 74 61 b, "read-only ta
1d7d6 62 6c 65 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ble");. rc =
1d7d7 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1d7d8 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 }else{. int i
1d7d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 ;. sqlite_int
1d7da 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 4d 65 64 rowid;. Me
1d7db 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 m **apArg = p->a
1d7dc 70 41 72 67 3b 0a 20 20 20 20 4d 65 6d 20 2a 70 pArg;. Mem *p
1d7dd 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 X = &p->aMem[pOp
1d7de 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 69 ->p3];. for(i
1d7df 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 =0; i<nArg; i++)
1d7e0 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 {. storeTyp
1d7e1 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a 20 20 eInfo(pX, 0);.
1d7e2 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 70 apArg[i] = p
1d7e3 58 3b 0a 20 20 20 20 20 20 70 58 2b 2b 3b 0a 20 X;. pX++;.
1d7e4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1d7e5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1d7e6 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1d7e7 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1d7e8 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 sqlite3VtabLoc
1d7e9 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20 72 63 k(pVtab);. rc
1d7ea 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 = pModule->xUpd
1d7eb 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72 67 2c ate(pVtab, nArg,
1d7ec 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64 29 3b apArg, &rowid);
1d7ed 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1d7ee 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1d7ef 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d g);. p->zErrM
1d7f0 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 sg = pVtab->zErr
1d7f1 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e Msg;. pVtab->
1d7f2 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1d7f3 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f sqlite3VtabUnlo
1d7f4 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 ck(db, pVtab);.
1d7f5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1d7f6 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1d7f7 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1d7f8 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 70 isuse;. if( p
1d7f9 4f 70 2d 3e 70 31 20 26 26 20 72 63 3d 3d 53 51 Op->p1 && rc==SQ
1d7fa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d7fb 20 61 73 73 65 72 74 28 20 6e 41 72 67 3e 31 20 assert( nArg>1
1d7fc 26 26 20 61 70 41 72 67 5b 30 5d 20 26 26 20 28 && apArg[0] && (
1d7fd 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 apArg[0]->flags&
1d7fe 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 MEM_Null) );.
1d7ff 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 db->lastRowid
1d800 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a = rowid;. }.
1d801 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b p->nChange++
1d802 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d803 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d804 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d805 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1d806 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 SQLITE_OMIT_PAG
1d807 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70 ER_PRAGMAS./* Op
1d808 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e 74 20 code: Pagecount
1d809 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1d80a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 * Write the curr
1d80b 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 ent number of pa
1d80c 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 ges in database
1d80d 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c P1 to memory cel
1d80e 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 l P2..*/.case OP
1d80f 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20 _Pagecount: {
1d810 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1d811 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1d812 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 int p1 = pOp->p
1d813 31 3b 20 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 1; . int nPage;
1d814 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1d815 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
1d816 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 70 31 5d ager(db->aDb[p1]
1d817 2e 70 42 74 29 3b 0a 0a 20 20 72 63 20 3d 20 73 .pBt);.. rc = s
1d818 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
1d819 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 ount(pPager, &nP
1d81a 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d age);. if( rc==
1d81b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1d81c 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1d81d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74 EM_Int;. pOut
1d81e 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 65 3b 0a 20 ->u.i = nPage;.
1d81f 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1d820 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
1d821 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a LITE_OMIT_TRACE.
1d822 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 /* Opcode: Trace
1d823 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1d824 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73 20 * If tracing is
1d825 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65 20 enabled (by the
1d826 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29 sqlite3_trace())
1d827 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e interface, then
1d828 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74 .** the UTF-8 st
1d829 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 ring contained i
1d82a 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 20 n P4 is emitted
1d82b 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 6c on the trace cal
1d82c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f lback..*/.case O
1d82d 50 5f 54 72 61 63 65 3a 20 7b 0a 20 20 63 68 61 P_Trace: {. cha
1d82e 72 20 2a 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 r *zTrace = (pOp
1d82f 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 ->p4.z ? pOp->p4
1d830 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 .z : p->zSql);.
1d831 20 69 66 28 20 7a 54 72 61 63 65 20 29 7b 0a 20 if( zTrace ){.
1d832 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63 if( db->xTrac
1d833 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 e ){. db->x
1d834 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63 65 Trace(db->pTrace
1d835 41 72 67 2c 20 7a 54 72 61 63 65 29 3b 0a 20 20 Arg, zTrace);.
1d836 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
1d837 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 E_DEBUG. if(
1d838 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c (db->flags & SQL
1d839 49 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 ITE_SqlTrace)!=0
1d83a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1d83b 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 3DebugPrintf("SQ
1d83c 4c 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 L-trace: %s\n",
1d83d 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 zTrace);. }.#
1d83e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1d83f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 DEBUG */. }. b
1d840 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a reak;.}.#endif..
1d841 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 ./* Opcode: Noop
1d842 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a * * * * *.**.**
1d843 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 Do nothing. Th
1d844 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 is instruction i
1d845 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61 s often useful a
1d846 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 s a jump.** dest
1d847 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a ination..*/./*.*
1d848 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c * The magic Expl
1d849 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f ain opcode are o
1d84a 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68 65 nly inserted whe
1d84b 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 n explain==2 (wh
1d84c 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 ich.** is to say
1d84d 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49 when the EXPLAI
1d84e 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e N QUERY PLAN syn
1d84f 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a tax is used.).**
1d850 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 This opcode rec
1d851 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ords information
1d852 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 from the optimi
1d853 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a zer. It is the.
1d854 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 ** the same as a
1d855 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 no-op. This op
1d856 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 codesnever appea
1d857 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 rs in a real VM
1d858 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 program..*/.defa
1d859 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ult: {
1d85a 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c /* This is reall
1d85b 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 y OP_Noop and OP
1d85c 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 _Explain */. br
1d85d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a eak;.}../*******
1d85e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d85f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d862 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ******.** The ca
1d863 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 74 63 ses of the switc
1d864 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 h statement abov
1d865 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 e this line shou
1d866 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 ld all be indent
1d867 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65 ed.** by 6 space
1d868 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66 74 s. But the left
1d869 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20 68 -most 6 spaces h
1d86a 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 ave been removed
1d86b 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a to improve the.
1d86c 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20 ** readability.
1d86d 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 From this point
1d86e 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f on down, the no
1d86f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e rmal indentation
1d870 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 rules are.** re
1d871 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a stored..********
1d872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d875 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d876 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 *****/. }..#i
1d877 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
1d878 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36 E. {. u6
1d879 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 4 elapsed = sqli
1d87a 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74 te3Hwtime() - st
1d87b 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e art;. pOp->
1d87c 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 cycles += elapse
1d87d 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e d;. pOp->cn
1d87e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 t++;.#if 0.
1d87f 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 fprintf(stdou
1d880 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c t, "%10llu ", el
1d881 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 apsed);.
1d882 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
1d883 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 Op(stdout, origP
1d884 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 67 50 c, &p->aOp[origP
1d885 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 c]);.#endif.
1d886 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a }.#endif.. /*
1d887 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
1d888 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e 67 ode adds nothing
1d889 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 to the actual f
1d88a 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20 20 unctionality.
1d88b 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 72 ** of the progr
1d88c 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 am. It is only
1d88d 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 here for testing
1d88e 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e 0a and debugging..
1d88f 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 ** On the ot
1d890 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f 65 her hand, it doe
1d891 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c 65 s burn CPU cycle
1d892 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 68 72 s every time thr
1d893 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 ough. ** the
1d894 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 evaluator loop.
1d895 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 65 So we can leave
1d896 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44 45 it out when NDE
1d897 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a BUG is defined..
1d898 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e */.#ifndef N
1d899 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 72 74 DEBUG. assert
1d89a 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c 70 ( pc>=-1 && pc<p
1d89b 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65 66 ->nOp );..#ifdef
1d89c 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1d89d 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 if( p->trace )
1d89e 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d {. if( rc!=
1d89f 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 0 ) fprintf(p->t
1d8a0 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 race,"rc=%d\n",r
1d8a1 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 c);. if( op
1d8a2 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1d8a3 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 _OUT2_PRERELEASE
1d8a4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 ){. regi
1d8a5 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 sterTrace(p->tra
1d8a6 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 ce, pOp->p2, pOu
1d8a7 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
1d8a8 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 79 if( opProperty
1d8a9 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 29 7b & OPFLG_OUT3 ){
1d8aa 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 . registe
1d8ab 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c rTrace(p->trace,
1d8ac 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b pOp->p3, pOut);
1d8ad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
1d8ae 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 endif /* SQLITE
1d8af 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 66 _DEBUG */.#endif
1d8b0 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 /* NDEBUG */.
1d8b1 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f } /* The end o
1d8b2 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f f the for(;;) lo
1d8b3 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 72 op the loops thr
1d8b4 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0a ough opcodes */.
1d8b5 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 . /* If we reac
1d8b6 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 h this point, it
1d8b7 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63 means that exec
1d8b8 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65 ution is finishe
1d8b9 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 65 d with. ** an e
1d8ba 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e rror of some kin
1d8bb 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 72 d.. */.vdbe_err
1d8bc 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 72 or_halt:. asser
1d8bd 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 63 t( rc );. p->rc
1d8be 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 = rc;. sqlite3
1d8bf 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 69 VdbeHalt(p);. i
1d8c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
1d8c1 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e ERR_NOMEM ) db->
1d8c2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1d8c3 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f ;. rc = SQLITE_
1d8c4 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 69 ERROR;.. /* Thi
1d8c5 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 s is the only wa
1d8c6 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72 y out of this pr
1d8c7 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 76 ocedure. We hav
1d8c8 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73 e to. ** releas
1d8c9 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e e the mutexes on
1d8ca 20 62 74 72 65 65 73 20 74 68 61 74 20 77 65 72 btrees that wer
1d8cb 65 20 61 63 71 75 69 72 65 64 20 61 74 20 74 68 e acquired at th
1d8cc 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 e. ** top. */.v
1d8cd 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71 dbe_return:. sq
1d8ce 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
1d8cf 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d rrayLeave(&p->aM
1d8d0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
1d8d1 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 rc;.. /* Jump t
1d8d2 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 72 69 o here if a stri
1d8d3 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65 ng or blob large
1d8d4 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 r than SQLITE_MA
1d8d5 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73 X_LENGTH. ** is
1d8d6 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20 encountered..
1d8d7 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 */.too_big:. sq
1d8d8 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1d8d9 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1d8da 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 "string or blob
1d8db 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20 too big");. rc
1d8dc 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b = SQLITE_TOOBIG;
1d8dd 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 . goto vdbe_err
1d8de 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a or_halt;.. /* J
1d8df 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 ump to here if a
1d8e0 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e malloc() fails.
1d8e1 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 . */.no_mem:.
1d8e2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d8e3 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53 = 1;. sqlite3S
1d8e4 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1d8e5 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f rMsg, db, "out o
1d8e6 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63 f memory");. rc
1d8e7 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1d8e8 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 . goto vdbe_err
1d8e9 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a or_halt;.. /* J
1d8ea 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 ump to here for
1d8eb 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 an SQLITE_MISUSE
1d8ec 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f error.. */.abo
1d8ed 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1d8ee 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f :. rc = SQLITE_
1d8ef 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c MISUSE;. /* Fal
1d8f0 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72 l thru into abor
1d8f1 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a t_due_to_error *
1d8f2 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 /.. /* Jump to
1d8f3 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 here for any oth
1d8f4 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c er kind of fatal
1d8f5 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63 error. The "rc
1d8f6 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 " variable. **
1d8f7 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 should hold the
1d8f8 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20 error number..
1d8f9 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f */.abort_due_to_
1d8fa 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 error:. assert(
1d8fb 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 p->zErrMsg==0 )
1d8fc 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
1d8fd 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20 ocFailed ) rc =
1d8fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1d8ff 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 if( rc!=SQLITE_I
1d900 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 OERR_NOMEM ){.
1d901 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d902 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d903 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
1d904 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 3ErrStr(rc));.
1d905 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 }. goto vdbe_er
1d906 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 ror_halt;.. /*
1d907 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 Jump to here if
1d908 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 the sqlite3_inte
1d909 72 72 75 70 74 28 29 20 41 50 49 20 73 65 74 73 rrupt() API sets
1d90a 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 the interrupt.
1d90b 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 ** flag.. */.a
1d90c 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 bort_due_to_inte
1d90d 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 rrupt:. assert(
1d90e 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
1d90f 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 upted );. rc =
1d910 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
1d911 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a ;. p->rc = rc;.
1d912 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d913 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d914 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
1d915 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 3ErrStr(rc));.
1d916 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f goto vdbe_error_
1d917 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a halt;.}../******
1d918 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1d919 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c *********
1d91a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d91b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d91c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1d91d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1d91e 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a ile vdbeblob.c *
1d91f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d921 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1d922 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 007 May 1.**.**
1d923 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
1d924 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
1d925 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
1d926 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
1d927 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
1d928 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
1d929 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
1d92a 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
1d92b 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
1d92c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1d92d 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1d92e 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
1d92f 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
1d930 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
1d931 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
1d932 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
1d933 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
1d934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d935 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d936 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d937 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d938 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
1d939 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
1d93a 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 s code used to i
1d93b 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65 mplement increme
1d93c 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a ntal BLOB I/O..*
1d93d 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 62 6c *.** $Id: vdbebl
1d93e 6f 62 2e 63 2c 76 20 31 2e 33 31 20 32 30 30 39 ob.c,v 1.31 2009
1d93f 2f 30 33 2f 32 34 20 31 35 3a 30 38 3a 31 30 20 /03/24 15:08:10
1d940 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 drh Exp $.*/...#
1d941 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1d942 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a 0a IT_INCRBLOB../*.
1d943 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 33 ** Valid sqlite3
1d944 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 70 _blob* handles p
1d945 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f 62 oint to Incrblob
1d946 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a structures..*/.
1d947 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 typedef struct I
1d948 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f 62 ncrblob Incrblob
1d949 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f ;.struct Incrblo
1d94a 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b b {. int flags;
1d94b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d94c 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 22 Copy of "flags"
1d94d 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 passed to sqlit
1d94e 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 2a e3_blob_open() *
1d94f 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 /. int nByte;
1d950 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1d951 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f 62 ize of open blob
1d952 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 , in bytes */.
1d953 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 int iOffset;
1d954 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 /* Byte
1d955 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 69 offset of blob i
1d956 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a 2f n cursor data */
1d957 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 73 . BtCursor *pCs
1d958 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 r; /* Cu
1d959 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 rsor pointing at
1d95a 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 73 blob row */. s
1d95b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1d95c 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 6d mt; /* Statem
1d95d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 73 ent holding curs
1d95e 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c or open */. sql
1d95f 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1d960 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f /* The asso
1d961 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 ciated database
1d962 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 */.};../*.** Ope
1d963 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e n a blob handle.
1d964 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1d965 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
1d966 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a open(. sqlite3*
1d967 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 db,
1d968 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1d969 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
1d96a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c const char *zDb,
1d96b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 /* The a
1d96c 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1d96d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1d96e 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 blob */. const
1d96f 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 20 char *zTable,
1d970 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 63 /* The table c
1d971 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c ontaining the bl
1d972 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ob */. const ch
1d973 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 20 ar *zColumn,
1d974 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 6f /* The column co
1d975 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f ntaining the blo
1d976 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e b */. sqlite_in
1d977 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 2f t64 iRow, /
1d978 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 69 * The row contai
1d979 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a 2f ning the glob */
1d97a 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
1d97b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1d97c 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 65 ue -> read/write
1d97d 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 2d access, false -
1d97e 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 > read-only */.
1d97f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a sqlite3_blob **
1d980 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e 64 ppBlob /* Hand
1d981 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 le for accessing
1d982 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 6e the blob return
1d983 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ed here */.){.
1d984 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 30 int nAttempt = 0
1d985 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 ;. int iCol;
1d986 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1d987 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e 20 ndex of zColumn
1d988 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a 2f in row-record */
1d989 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 45 .. /* This VDBE
1d98a 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 61 program seeks a
1d98b 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 6f btree cursor to
1d98c 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 20 the identified
1d98d 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f 72 . ** db/table/r
1d98e 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 65 ow entry. The re
1d98f 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 61 ason for using a
1d990 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 6e vdbe program in
1d991 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 72 stead. ** of wr
1d992 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 73 iting code to us
1d993 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 e the b-tree lay
1d994 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 74 er directly is t
1d995 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 hat the. ** vdb
1d996 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 74 e program will t
1d997 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 ake advantage of
1d998 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 61 the various tra
1d999 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 6c nsaction,. ** l
1d99a 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f 72 ocking and error
1d99b 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 73 handling infras
1d99c 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 69 tructure built i
1d99d 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 20 nto the vdbe..
1d99e 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 65 **. ** After se
1d99f 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 eking the cursor
1d9a0 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63 75 , the vdbe execu
1d9a1 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c 74 tes an OP_Result
1d9a2 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 65 Row.. ** Code e
1d9a3 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 56 xternal to the V
1d9a4 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f 77 dbe then "borrow
1d9a5 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 s" the b-tree cu
1d9a6 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 73 rsor and. ** us
1d9a7 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d 65 es it to impleme
1d9a8 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 64 nt the blob_read
1d9a9 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 29 (), blob_write()
1d9aa 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 5f and . ** blob_
1d9ab 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f 6e bytes() function
1d9ac 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 s.. **. ** The
1d9ad 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c sqlite3_blob_cl
1d9ae 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 ose() function f
1d9af 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 62 inalizes the vdb
1d9b0 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a 20 e program,. **
1d9b1 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65 which closes the
1d9b2 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 b-tree cursor a
1d9b3 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f nd (possibly) co
1d9b4 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a 20 mmits the . **
1d9b5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a transaction.. *
1d9b6 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 /. static const
1d9b7 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 6e VdbeOpList open
1d9b8 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b Blob[] = {. {
1d9b9 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 OP_Transaction,
1d9ba 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 2a 0, 0, 0}, /*
1d9bb 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 6e 0: Start a tran
1d9bc 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 7b saction */. {
1d9bd 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c OP_VerifyCookie,
1d9be 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f 2a 0, 0, 0}, /*
1d9bf 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 63 1: Check the sc
1d9c0 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 0a hema cookie */..
1d9c1 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
1d9c2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 e following two
1d9c3 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 instructions is
1d9c4 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a 20 replaced by an.
1d9c5 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65 ** OP_Noop be
1d9c6 66 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a 20 fore exection..
1d9c7 20 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 */. {OP_Op
1d9c8 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d enRead, 0, 0, 0}
1d9c9 2c 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 4f , /* 2: O
1d9ca 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 pen cursor 0 for
1d9cb 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 reading */.
1d9cc 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 {OP_OpenWrite, 0
1d9cd 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f , 0, 0}, /
1d9ce 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 * 3: Open cursor
1d9cf 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 0 for read/writ
1d9d0 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 e */.. {OP_Va
1d9d1 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31 7d riable, 1, 1, 1}
1d9d2 2c 20 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 50 , /* 4: P
1d9d3 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f ush the rowid to
1d9d4 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 the stack */.
1d9d5 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c {OP_NotExists,
1d9d6 20 30 2c 20 38 2c 20 31 7d 2c 20 20 20 20 20 20 0, 8, 1},
1d9d7 20 2f 2a 20 35 3a 20 53 65 65 6b 20 74 68 65 20 /* 5: Seek the
1d9d8 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f cursor */. {O
1d9d9 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 P_Column, 0, 0,
1d9da 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 1}, /*
1d9db 36 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65 6 */. {OP_Re
1d9dc 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30 sultRow, 1, 0, 0
1d9dd 7d 2c 20 20 20 20 20 20 20 2f 2a 20 37 20 20 2a }, /* 7 *
1d9de 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c /. {OP_Close,
1d9df 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
1d9e0 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f 0a 20 20 /* 8 */.
1d9e1 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 {OP_Halt, 0, 0
1d9e2 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 , 0},
1d9e3 20 2f 2a 20 39 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 /* 9 */. };..
1d9e4 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 Vdbe *v = 0;.
1d9e5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1d9e6 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b OK;. char zErr[
1d9e7 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d 128];.. zErr[0]
1d9e8 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
1d9e9 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e mutex_enter(db->
1d9ea 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20 7b 0a 20 mutex);. do {.
1d9eb 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65 3b Parse sParse;
1d9ec 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
1d9ed 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 ;.. memset(&s
1d9ee 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 Parse, 0, sizeof
1d9ef 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 50 (Parse));. sP
1d9f0 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 arse.db = db;..
1d9f1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1d9f2 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1d9f3 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1d9f4 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1d9f5 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e x);. return
1d9f6 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1d9f7 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1d9f8 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
1d9f9 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 db);. pTab =
1d9fa 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 sqlite3LocateTab
1d9fb 6c 65 28 26 73 50 61 72 73 65 2c 20 30 2c 20 7a le(&sParse, 0, z
1d9fc 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 Table, zDb);.
1d9fd 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73 56 if( pTab && IsV
1d9fe 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a irtual(pTab) ){.
1d9ff 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a pTab = 0;.
1da00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1da01 6f 72 4d 73 67 28 26 73 50 61 72 73 65 2c 20 22 orMsg(&sParse, "
1da02 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 74 cannot open virt
1da03 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 ual table: %s",
1da04 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 zTable);. }.#
1da05 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1da06 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 20 IT_VIEW. if(
1da07 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 pTab && pTab->pS
1da08 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 70 elect ){. p
1da09 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 Tab = 0;. s
1da0a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 26 qlite3ErrorMsg(&
1da0b 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 sParse, "cannot
1da0c 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20 open view: %s",
1da0d 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 zTable);. }.#
1da0e 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 70 endif. if( !p
1da0f 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Tab ){. if(
1da10 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 20 sParse.zErrMsg
1da11 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1da12 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
1da13 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 of(zErr), zErr,
1da14 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 72 "%s", sParse.zEr
1da15 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 rMsg);. }.
1da16 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1da17 65 65 28 64 62 2c 20 73 50 61 72 73 65 2e 7a 45 ee(db, sParse.zE
1da18 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72 63 rrMsg);. rc
1da19 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1da1a 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c . (void)sql
1da1b 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1da1c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1da1d 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
1da1e 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c );. goto bl
1da1f 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ob_open_out;.
1da20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 }.. /* Now s
1da21 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20 74 earch pTab for t
1da22 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e he exact column.
1da23 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c */. for(iCol
1da24 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d =0; iCol < pTab-
1da25 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b >nCol; iCol++) {
1da26 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1da27 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e e3StrICmp(pTab->
1da28 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 aCol[iCol].zName
1da29 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b , zColumn)==0 ){
1da2a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1da2b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1da2c 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 if( iCol==pTab
1da2d 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ->nCol ){.
1da2e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1da2f 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1da30 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f Err, "no such co
1da31 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a lumn: \"%s\"", z
1da32 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72 Column);. r
1da33 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1da34 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 ;. (void)sq
1da35 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1da36 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 b);. sqlite
1da37 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1da38 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 b);. goto b
1da39 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1da3a 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
1da3b 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e he value is bein
1da3c 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 g opened for wri
1da3d 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74 ting, check that
1da3e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 the. ** colu
1da3f 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65 mn is not indexe
1da40 64 2e 20 49 74 20 69 73 20 61 67 61 69 6e 73 74 d. It is against
1da41 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20 6f 70 the rules to op
1da42 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 en an. ** ind
1da43 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 exed column for
1da44 77 72 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a writing.. */.
1da45 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 29 7b if( flags ){
1da46 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 . Index *pI
1da47 64 78 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 dx;. for(pI
1da48 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
1da49 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
1da4a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
1da4b 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 int j;.
1da4c 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 for(j=0; j<pIdx
1da4d 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b ->nColumn; j++){
1da4e 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1da4f 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d Idx->aiColumn[j]
1da50 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ==iCol ){.
1da51 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1da52 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 printf(sizeof(zE
1da53 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 rr), zErr,.
1da54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da55 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 "cannot
1da56 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c open indexed col
1da57 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22 umn for writing"
1da58 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
1da59 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1da5a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 76 ;. (v
1da5b 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1da5c 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1da5d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1da5e 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1da5f 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
1da60 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a blob_open_out;.
1da61 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1da62 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1da63 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c }.. v = sql
1da64 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 64 ite3VdbeCreate(d
1da65 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29 7b b);. if( v ){
1da66 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d . int iDb =
1da67 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
1da68 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
1da69 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 pSchema);.
1da6a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1da6b 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28 6f List(v, sizeof(o
1da6c 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66 28 penBlob)/sizeof(
1da6d 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70 65 VdbeOpList), ope
1da6e 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20 20 20 2f nBlob);.. /
1da6f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 * Configure the
1da70 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a OP_Transaction *
1da71 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 /. sqlite3V
1da72 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30 dbeChangeP1(v, 0
1da73 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 , iDb);. sq
1da74 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1da75 32 28 76 2c 20 30 2c 20 28 66 6c 61 67 73 20 3f 2(v, 0, (flags ?
1da76 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20 20 20 20 1 : 0));..
1da77 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 /* Configure th
1da78 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 e OP_VerifyCooki
1da79 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 e */. sqlit
1da7a 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 e3VdbeChangeP1(v
1da7b 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 , 1, iDb);.
1da7c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1da7d 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d geP2(v, 1, pTab-
1da7e 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 >pSchema->schema
1da7f 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 _cookie);..
1da80 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 /* Make sure a
1da81 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e mutex is held on
1da82 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 the table to be
1da83 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 accessed */.
1da84 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 sqlite3VdbeUs
1da85 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b esBtree(v, iDb);
1da86 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f .. /* Remo
1da87 76 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 ve either the OP
1da88 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 _OpenWrite or Op
1da89 65 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 enRead. Set the
1da8a 50 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 P2 . ** par
1da8b 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 ameter of the ot
1da8c 68 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 her to pTab->tnu
1da8d 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 m. . */.
1da8e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1da8f 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 28 66 angeToNoop(v, (f
1da90 6c 61 67 73 20 3f 20 32 20 3a 20 33 29 2c 20 31 lags ? 2 : 3), 1
1da91 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1da92 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1da93 28 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 29 2c (flags ? 3 : 2),
1da94 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 pTab->tnum);.
1da95 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1da96 68 61 6e 67 65 50 33 28 76 2c 20 28 66 6c 61 67 hangeP3(v, (flag
1da97 73 20 3f 20 33 20 3a 20 32 29 2c 20 69 44 62 29 s ? 3 : 2), iDb)
1da98 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 ;.. /* Conf
1da99 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 igure the number
1da9a 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f 6e of columns. Con
1da9b 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f figure the curso
1da9c 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 r to. ** th
1da9d 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62 ink that the tab
1da9e 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20 le has one more
1da9f 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72 column than it r
1daa0 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64 eally. ** d
1daa1 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d oes. An OP_Colum
1daa2 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 n to retrieve th
1daa3 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c is imaginary col
1daa4 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a umn will. *
1daa5 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 * always return
1daa6 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69 an SQL NULL. Thi
1daa7 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61 s is useful beca
1daa8 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 use it means.
1daa9 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76 ** we can inv
1daaa 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f oke OP_Column to
1daab 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62 fill in the vdb
1daac 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a e cursors type .
1daad 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66 ** and off
1daae 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75 set cache withou
1daaf 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f t causing any IO
1dab0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1dab1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1dab2 67 65 50 34 28 76 2c 20 66 6c 61 67 73 20 3f 20 geP4(v, flags ?
1dab3 33 20 3a 20 32 2c 20 53 51 4c 49 54 45 5f 49 4e 3 : 2, SQLITE_IN
1dab4 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e T_TO_PTR(pTab->n
1dab5 43 6f 6c 2b 31 29 2c 20 50 34 5f 49 4e 54 33 32 Col+1), P4_INT32
1dab6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1dab7 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1dab8 36 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 6, pTab->nCol);.
1dab9 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1daba 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1dabb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1dabc 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 beMakeReady(v, 1
1dabd 2c 20 31 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 , 1, 1, 0);.
1dabe 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 }. }. .
1dabf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1dac0 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1dac1 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 rc = sqlite3Safe
1dac2 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 tyOff(db);. i
1dac3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1dac4 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1dac5 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f iled ){. go
1dac6 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1dac7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1dac8 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
1dac9 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 (sqlite3_stmt *)
1daca 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 v, 1, iRow);.
1dacb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 rc = sqlite3_st
1dacc 65 70 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ep((sqlite3_stmt
1dacd 20 2a 29 76 29 3b 0a 20 20 20 20 69 66 28 20 72 *)v);. if( r
1dace 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b c!=SQLITE_ROW ){
1dacf 0a 20 20 20 20 20 20 6e 41 74 74 65 6d 70 74 2b . nAttempt+
1dad0 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 +;. rc = sq
1dad1 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 lite3_finalize((
1dad2 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 sqlite3_stmt *)v
1dad3 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1dad4 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
1dad5 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 73 71 (zErr), zErr, sq
1dad6 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 lite3_errmsg(db)
1dad7 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a );. v = 0;.
1dad8 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 }. } while(
1dad9 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 nAttempt<5 && r
1dada 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 c==SQLITE_SCHEMA
1dadb 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 );.. if( rc==S
1dadc 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
1dadd 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f /* The row-reco
1dade 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e rd has been open
1dadf 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e ed successfully.
1dae0 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a Check that the.
1dae1 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e ** column in
1dae2 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 question contai
1dae3 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f ns text or a blo
1dae4 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e b. If it contain
1dae5 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 s. ** text, i
1dae6 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 t is up to the c
1dae7 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 aller to get the
1dae8 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e encoding right.
1dae9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 . */. Incr
1daea 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 blob *pBlob;.
1daeb 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 u32 type = v->a
1daec 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 pCsr[0]->aType[i
1daed 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 Col];.. if( t
1daee 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 ype<12 ){.
1daef 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1daf0 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1daf1 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 Err, "cannot ope
1daf2 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 n value of type
1daf3 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 %s",. t
1daf4 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 ype==0?"null": t
1daf5 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 ype==7?"real": "
1daf6 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 integer". )
1daf7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
1daf8 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
1daf9 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f goto blob_open_
1dafa 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 out;. }. p
1dafb 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 Blob = (Incrblob
1dafc 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c *)sqlite3DbMall
1dafd 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
1dafe 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 f(Incrblob));.
1daff 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1db00 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
1db01 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1db02 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 , pBlob);.
1db03 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1db04 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ut;. }. pB
1db05 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 lob->flags = fla
1db06 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 gs;. pBlob->p
1db07 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b Csr = v->apCsr[
1db08 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 0]->pCursor;.
1db09 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1db0a 65 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e erCursor(pBlob->
1db0b 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 pCsr);. sqlit
1db0c 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 e3BtreeCacheOver
1db0d 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 flow(pBlob->pCsr
1db0e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
1db0f 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 reeLeaveCursor(p
1db10 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 Blob->pCsr);.
1db11 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 pBlob->pStmt =
1db12 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 (sqlite3_stmt *)
1db13 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f v;. pBlob->iO
1db14 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 ffset = v->apCsr
1db15 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f [0]->aOffset[iCo
1db16 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e l];. pBlob->n
1db17 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 Byte = sqlite3Vd
1db18 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1db19 74 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 type);. pBlob
1db1a 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a ->db = db;. *
1db1b 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 ppBlob = (sqlite
1db1c 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 3_blob *)pBlob;.
1db1d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1db1e 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 OK;. }else if(
1db1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1db20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1db21 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 rintf(sizeof(zEr
1db22 72 29 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 r), zErr, "no su
1db23 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c ch rowid: %lld",
1db24 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d iRow);. rc =
1db25 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1db26 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 }..blob_open_ou
1db27 74 3a 0a 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 t:. zErr[sizeof
1db28 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27 (zErr)-1] = '\0'
1db29 3b 0a 20 20 69 66 28 20 76 20 26 26 20 28 72 63 ;. if( v && (rc
1db2a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 !=SQLITE_OK || d
1db2b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 b->mallocFailed)
1db2c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1db2d 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a dbeFinalize(v);.
1db2e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 }. sqlite3Err
1db2f 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63 3d 3d or(db, rc, (rc==
1db30 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a 7a 45 72 72 SQLITE_OK?0:zErr
1db31 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ));. rc = sqlit
1db32 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 e3ApiExit(db, rc
1db33 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
1db34 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 ex_leave(db->mut
1db35 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ex);. return rc
1db36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
1db37 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 a blob handle t
1db38 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 hat was previous
1db39 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 ly created using
1db3a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 .** sqlite3_blob
1db3b 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _open()..*/.SQLI
1db3c 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1db3d 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 e3_blob_close(sq
1db3e 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f lite3_blob *pBlo
1db3f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a b){. Incrblob *
1db40 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 p = (Incrblob *)
1db41 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b pBlob;. int rc;
1db42 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1db43 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 . db = p->db;.
1db44 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1db45 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1db46 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f . rc = sqlite3_
1db47 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d finalize(p->pStm
1db48 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 t);. sqlite3DbF
1db49 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 73 71 ree(db, p);. sq
1db4a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1db4b 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1db4c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1db4d 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 .** Perform a re
1db4e 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 72 ad or write oper
1db4f 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a ation on a blob.
1db50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c */.static int bl
1db51 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 73 obReadWrite(. s
1db52 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c qlite3_blob *pBl
1db53 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 ob, . void *z,
1db54 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 . int n, . int
1db55 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e 74 iOffset, . int
1db56 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73 (*xCall)(BtCurs
1db57 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 or*, u32, u32, v
1db58 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 oid*).){. int r
1db59 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 c;. Incrblob *p
1db5a 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 = (Incrblob *)p
1db5b 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b Blob;. Vdbe *v;
1db5c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1db5d 20 70 2d 3e 64 62 3b 20 20 0a 0a 20 20 73 71 6c p->db; .. sql
1db5e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1db5f 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 (db->mutex);. v
1db60 20 3d 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 = (Vdbe*)p->pSt
1db61 6d 74 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c mt;.. if( n<0 |
1db62 7c 20 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 | iOffset<0 || (
1db63 69 4f 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 iOffset+n)>p->nB
1db64 79 74 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 yte ){. /* Re
1db65 71 75 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 quest is out of
1db66 72 61 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 range. Return a
1db67 74 72 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e transient error.
1db68 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c */. rc = SQL
1db69 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 ITE_ERROR;. s
1db6a 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 qlite3Error(db,
1db6b 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 SQLITE_ERROR, 0)
1db6c 3b 0a 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76 ;. } else if( v
1db6d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
1db6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 there is no sta
1db6f 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 tement handle, t
1db70 68 65 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e hen the blob-han
1db71 64 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 dle has. ** a
1db72 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61 lready been inva
1db73 6c 69 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20 lidated. Return
1db74 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 SQLITE_ABORT in
1db75 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a this case.. *
1db76 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 /. rc = SQLIT
1db77 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 E_ABORT;. }else
1db78 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 {. /* Call ei
1db79 74 68 65 72 20 42 74 72 65 65 44 61 74 61 28 29 ther BtreeData()
1db7a 20 6f 72 20 42 74 72 65 65 50 75 74 44 61 74 61 or BtreePutData
1db7b 28 29 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 (). If SQLITE_AB
1db7c 4f 52 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 ORT is. ** re
1db7d 74 75 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 turned, clean-up
1db7e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 the statement h
1db7f 61 6e 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 andle.. */.
1db80 20 20 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20 assert( db ==
1db81 76 2d 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c v->db );. sql
1db82 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 ite3BtreeEnterCu
1db83 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 rsor(p->pCsr);.
1db84 20 20 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d rc = xCall(p-
1db85 3e 70 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 >pCsr, iOffset+p
1db86 2d 3e 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 ->iOffset, n, z)
1db87 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1db88 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d eeLeaveCursor(p-
1db89 3e 70 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 >pCsr);. if(
1db8a 72 63 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 rc==SQLITE_ABORT
1db8b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1db8c 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3VdbeFinalize(v)
1db8d 3b 0a 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 ;. p->pStmt
1db8e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
1db8f 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f . db->errCo
1db90 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 de = rc;. v
1db91 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d ->rc = rc;. }
1db92 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
1db93 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 te3ApiExit(db, r
1db94 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 c);. sqlite3_mu
1db95 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
1db96 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
1db97 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
1db98 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f data from a blo
1db99 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c b handle..*/.SQL
1db9a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1db9b 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 te3_blob_read(sq
1db9c 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f lite3_blob *pBlo
1db9d 62 2c 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 b, void *z, int
1db9e 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b n, int iOffset){
1db9f 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 . return blobRe
1dba0 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a adWrite(pBlob, z
1dba1 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 , n, iOffset, sq
1dba2 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 29 3b lite3BtreeData);
1dba3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
1dba4 64 61 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 data to a blob h
1dba5 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 andle..*/.SQLITE
1dba6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1dba7 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 _blob_write(sqli
1dba8 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c te3_blob *pBlob,
1dba9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
1dbaa 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 int n, int iOffs
1dbab 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c et){. return bl
1dbac 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f obReadWrite(pBlo
1dbad 62 2c 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c b, (void *)z, n,
1dbae 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 iOffset, sqlite
1dbaf 33 42 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a 3BtreePutData);.
1dbb0 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 }../*.** Query a
1dbb1 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 blob handle for
1dbb2 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1dbb3 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 data..**.** The
1dbb4 20 49 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 Incrblob.nByte
1dbb5 66 69 65 6c 64 20 69 73 20 66 69 78 65 64 20 66 field is fixed f
1dbb6 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 or the lifetime
1dbb7 6f 66 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a of the Incrblob.
1dbb8 2a 2a 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 ** so no mutex i
1dbb9 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 s required for a
1dbba 63 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ccess..*/.SQLITE
1dbbb 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1dbbc 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 _blob_bytes(sqli
1dbbd 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 te3_blob *pBlob)
1dbbe 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 {. Incrblob *p
1dbbf 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 = (Incrblob *)pB
1dbc0 6c 6f 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d lob;. return p-
1dbc1 3e 6e 42 79 74 65 3b 0a 7d 0a 0a 23 65 6e 64 69 >nByte;.}..#endi
1dbc2 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1dbc3 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1dbc4 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a B */../*********
1dbc5 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1dbc6 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1dbc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbc9 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1dbca 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1dbcb 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a journal.c *****
1dbcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbce 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1dbcf 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a August 22.**.**
1dbd0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1dbd1 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1dbd2 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1dbd3 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1dbd4 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1dbd5 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1dbd6 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1dbd7 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1dbd8 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1dbd9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1dbda 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1dbdb 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1dbdc 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1dbdd 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1dbde 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1dbdf 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1dbe0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1dbe1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1dbe6 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61 @(#) $Id: journa
1dbe7 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30 l.c,v 1.9 2009/0
1dbe8 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61 1/20 17:06:27 da
1dbe9 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
1dbea 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
1dbeb 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
1dbec 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 WRITE../*.** Thi
1dbed 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
1dbee 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64 s a special kind
1dbef 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
1dbf0 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20 object used.**
1dbf1 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65 by SQLite to cre
1dbf2 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ate journal file
1dbf3 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d s if the atomic-
1dbf4 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
1dbf5 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 on.** is enabled
1dbf6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74 ..**.** The dist
1dbf7 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65 inctive characte
1dbf8 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73 ristic of this s
1dbf9 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74 qlite3_file is t
1dbfa 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 hat the.** actua
1dbfb 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69 l on disk file i
1dbfc 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79 s created lazily
1dbfd 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20 . When the file
1dbfe 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74 is created,.** t
1dbff 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66 he caller specif
1dc00 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a ies a buffer siz
1dc01 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
1dc02 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20 ry buffer to.**
1dc03 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69 be used to servi
1dc04 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72 ce read() and wr
1dc05 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20 ite() requests.
1dc06 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a The actual file.
1dc07 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f ** on disk is no
1dc08 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70 t created or pop
1dc09 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74 ulated until eit
1dc0a 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 her:.**.** 1)
1dc0b 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 The in-memory re
1dc0c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f presentation gro
1dc0d 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 ws too large for
1dc0e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a the allocated .
1dc0f 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20 ** buffer,
1dc10 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73 or.** 2) The s
1dc11 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 qlite3JournalCre
1dc12 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 ate() function i
1dc13 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a s called..*/....
1dc14 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 /*.** A JournalF
1dc15 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20 ile object is a
1dc16 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
1dc17 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79 te3_file used by
1dc18 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 .** as an open f
1dc19 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a ile handle for j
1dc1a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f ournal files..*/
1dc1b 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 .struct JournalF
1dc1c 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ile {. sqlite3_
1dc1d 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
1dc1e 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d hod; /* I/O m
1dc1f 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 ethods on journa
1dc20 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 l files */. int
1dc21 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 nBuf;
1dc22 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1dc23 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 Size of zBuf[] i
1dc24 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 n bytes */. cha
1dc25 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20 r *zBuf;
1dc26 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1dc27 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20 Space to buffer
1dc28 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a journal writes *
1dc29 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 /. int iSize;
1dc2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc2b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
1dc2c 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c zBuf[] currentl
1dc2d 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 y used */. int
1dc2e 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
1dc2f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1dc30 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 Open flags */.
1dc31 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1dc32 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
1dc33 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e /* The "real" un
1dc34 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a derlying VFS */.
1dc35 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1dc36 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20 pReal;
1dc37 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 /* The "real"
1dc38 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 underlying file
1dc39 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
1dc3a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 const char *zJou
1dc3b 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 rnal;
1dc3c 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
1dc3d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d ournal file */.}
1dc3e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
1dc3f 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 JournalFile Jou
1dc40 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a rnalFile;../*.**
1dc41 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 If it does not
1dc42 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20 already exists,
1dc43 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c create and popul
1dc44 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 ate the on-disk
1dc45 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 file .** for Jou
1dc46 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 rnalFile p..*/.s
1dc47 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 tatic int create
1dc48 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 File(JournalFile
1dc49 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d *p){. int rc =
1dc4a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
1dc4b 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 ( !p->pReal ){.
1dc4c 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 sqlite3_file
1dc4d 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 *pReal = (sqlite
1dc4e 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 3_file *)&p[1];.
1dc4f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dc50 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 OsOpen(p->pVfs,
1dc51 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 p->zJournal, pRe
1dc52 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 al, p->flags, 0)
1dc53 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1dc54 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1dc55 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 p->pReal = pRea
1dc56 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e l;. if( p->
1dc57 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 iSize>0 ){.
1dc58 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69 assert(p->iSi
1dc59 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 ze<=p->nBuf);.
1dc5a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1dc5b 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65 e3OsWrite(p->pRe
1dc5c 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e al, p->zBuf, p->
1dc5d 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 iSize, 0);.
1dc5e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1dc5f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1dc60 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
1dc61 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1dc62 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 jrnlClose(sqlit
1dc63 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a e3_file *pJfd){.
1dc64 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1dc65 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1dc66 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1dc67 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 >pReal ){. sq
1dc68 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e lite3OsClose(p->
1dc69 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 pReal);. }. sq
1dc6a 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 lite3_free(p->zB
1dc6b 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 uf);. return SQ
1dc6c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1dc6d 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
1dc6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 the file..*/.st
1dc6f 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 atic int jrnlRea
1dc70 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c d(. sqlite3_fil
1dc71 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1dc72 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1dc73 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 from which to re
1dc74 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 ad */. void *zB
1dc75 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f uf, /
1dc76 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 * Put the result
1dc77 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 s here */. int
1dc78 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 iAmt,
1dc79 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1dc7a 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f bytes to read */
1dc7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
1dc7c 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 iOfst /* Beg
1dc7d 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 in reading at th
1dc7e 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a is offset */.){.
1dc7f 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1dc80 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 E_OK;. JournalF
1dc81 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1dc82 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1dc83 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1dc84 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dc85 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c OsRead(p->pReal,
1dc86 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 zBuf, iAmt, iOf
1dc87 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 st);. }else if(
1dc88 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d (iAmt+iOfst)>p-
1dc89 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 >iSize ){. rc
1dc8a 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
1dc8b 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65 SHORT_READ;. }e
1dc8c 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 lse{. memcpy(
1dc8d 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 zBuf, &p->zBuf[i
1dc8e 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 Ofst], iAmt);.
1dc8f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1dc90 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
1dc91 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a ta to the file..
1dc92 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1dc93 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1dc94 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1dc95 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1dc96 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1dc97 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1dc98 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1dc99 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1dc9a 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1dc9b 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1dc9c 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1dc9d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1dc9e 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1dc9f 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1dca0 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1dca1 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1dca2 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1dca3 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1dca4 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1dca5 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1dca6 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1dca7 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1dca8 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c . if( !p->pReal
1dca9 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 && (iOfst+iAmt)
1dcaa 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 >p->nBuf ){.
1dcab 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28 rc = createFile(
1dcac 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 p);. }. if( rc
1dcad 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1dcae 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 if( p->pReal
1dcaf 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1dcb0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e lite3OsWrite(p->
1dcb1 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d pReal, zBuf, iAm
1dcb2 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d t, iOfst);. }
1dcb3 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
1dcb4 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 py(&p->zBuf[iOfs
1dcb5 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b t], zBuf, iAmt);
1dcb6 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 . if( p->iS
1dcb7 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29 ize<(iOfst+iAmt)
1dcb8 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
1dcb9 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41 Size = (iOfst+iA
1dcba 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 mt);. }.
1dcbb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1dcbc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 rc;.}../*.** Tru
1dcbd 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1dcbe 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1dcbf 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 nlTruncate(sqlit
1dcc0 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1dcc1 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 qlite_int64 size
1dcc2 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1dcc3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1dcc4 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1dcc5 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1dcc6 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1dcc7 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1dcc8 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d te3OsTruncate(p-
1dcc9 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 >pReal, size);.
1dcca 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c }else if( size<
1dccb 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 p->iSize ){.
1dccc 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b p->iSize = size;
1dccd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1dcce 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 ;.}../*.** Sync
1dccf 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1dcd0 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 tic int jrnlSync
1dcd1 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1dcd2 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b Jfd, int flags){
1dcd3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 . int rc;. Jou
1dcd4 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1dcd5 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1dcd6 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1dcd7 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1dcd8 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70 lite3OsSync(p->p
1dcd9 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Real, flags);.
1dcda 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1dcdb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1dcdc 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1dcdd 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 *.** Query the s
1dcde 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
1dcdf 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 in bytes..*/.sta
1dce0 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 tic int jrnlFile
1dce1 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
1dce2 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f e *pJfd, sqlite_
1dce3 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 int64 *pSize){.
1dce4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1dce5 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 _OK;. JournalFi
1dce6 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1dce7 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1dce8 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1dce9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1dcea 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65 sFileSize(p->pRe
1dceb 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 al, pSize);. }e
1dcec 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 lse{. *pSize
1dced 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1dcee 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 p->iSize;. }.
1dcef 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1dcf0 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 *.** Table of me
1dcf1 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61 thods for Journa
1dcf2 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 lFile sqlite3_fi
1dcf3 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 le object..*/.st
1dcf4 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 atic struct sqli
1dcf5 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a te3_io_methods J
1dcf6 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 ournalFileMethod
1dcf7 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 s = {. 1,
1dcf8 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
1dcf9 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 on */. jrnlClos
1dcfa 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 e, /* xClose
1dcfb 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20 */. jrnlRead,
1dcfc 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
1dcfd 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 . jrnlWrite,
1dcfe 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 /* xWrite */.
1dcff 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 jrnlTruncate,
1dd00 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a /* xTruncate */.
1dd01 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 jrnlSync,
1dd02 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a /* xSync */. j
1dd03 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a rnlFileSize, /*
1dd04 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 xFileSize */.
1dd05 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1dd06 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 * xLock */. 0,
1dd07 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1dd08 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 Unlock */. 0,
1dd09 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
1dd0a 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1dd0b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1dd0c 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e /* xFileCon
1dd0d 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 trol */. 0,
1dd0e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1dd0f 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1dd10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1dd11 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
1dd12 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 stics */.};../*
1dd13 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e .** Open a journ
1dd14 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 al file..*/.SQLI
1dd15 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1dd16 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
1dd17 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
1dd18 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 *pVfs,
1dd19 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73 /* The VFS to us
1dd1a 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c e for actual fil
1dd1b 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 e I/O */. const
1dd1c 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 char *zName,
1dd1d 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1dd1e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1dd1f 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
1dd20 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20 ile *pJfd,
1dd21 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 /* Preallocate
1dd22 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61 d, blank file ha
1dd23 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c ndle */. int fl
1dd24 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
1dd25 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 /* Opening
1dd26 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e flags */. int n
1dd27 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20 Buf
1dd28 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62 /* Bytes b
1dd29 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f uffered before o
1dd2a 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 pening the file
1dd2b 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 */.){. JournalF
1dd2c 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1dd2d 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1dd2e 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c memset(p, 0, sql
1dd2f 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
1dd30 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42 pVfs));. if( nB
1dd31 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a uf>0 ){. p->z
1dd32 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Buf = sqlite3Mal
1dd33 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20 locZero(nBuf);.
1dd34 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20 if( !p->zBuf
1dd35 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1dd36 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1dd37 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1dd38 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f return sqlite3O
1dd39 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d sOpen(pVfs, zNam
1dd3a 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20 e, pJfd, flags,
1dd3b 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 0);. }. p->pMe
1dd3c 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 thod = &JournalF
1dd3d 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d ileMethods;. p-
1dd3e 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 >nBuf = nBuf;.
1dd3f 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 p->flags = flags
1dd40 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 ;. p->zJournal
1dd41 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 = zName;. p->pV
1dd42 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74 fs = pVfs;. ret
1dd43 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1dd44 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 ../*.** If the a
1dd45 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73 rgument p points
1dd46 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c to a JournalFil
1dd47 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64 e structure, and
1dd48 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a the underlying.
1dd49 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 ** file has not
1dd4a 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 yet been created
1dd4b 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e , create it now.
1dd4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1dd4d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
1dd4e 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 urnalCreate(sqli
1dd4f 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 te3_file *p){.
1dd50 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21 if( p->pMethods!
1dd51 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 =&JournalFileMet
1dd52 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 hods ){. retu
1dd53 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1dd54 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 }. return creat
1dd55 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69 eFile((JournalFi
1dd56 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a le *)p);.}../* .
1dd57 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1dd58 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
1dd59 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 quired to store
1dd5a 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 a JournalFile th
1dd5b 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 at uses vfs.** p
1dd5c 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 Vfs to create th
1dd5d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d e underlying on-
1dd5e 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 disk files..*/.S
1dd5f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1dd60 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c t sqlite3Journal
1dd61 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Size(sqlite3_vfs
1dd62 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 *pVfs){. retur
1dd63 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c n (pVfs->szOsFil
1dd64 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c e+sizeof(Journal
1dd65 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 File));.}.#endif
1dd66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1dd67 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c * End of journal
1dd68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1dd69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd6b 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1dd6c 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
1dd6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1dd6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd70 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 /./*.** 2008 Oct
1dd71 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 ober 7.**.** The
1dd72 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1dd73 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1dd74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1dd75 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1dd76 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1dd77 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1dd78 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1dd79 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1dd7a 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1dd7b 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1dd7c 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1dd7d 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1dd7e 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1dd7f 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1dd80 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1dd81 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1dd82 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1dd83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd87 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1dd88 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1dd89 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 ode use to imple
1dd8a 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 ment an in-memor
1dd8b 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
1dd8c 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 al..** The in-me
1dd8d 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f mory rollback jo
1dd8e 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f urnal is used to
1dd8f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 journal transac
1dd90 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d tions for.** ":m
1dd91 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 emory:" database
1dd92 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a s and when the j
1dd93 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f ournal_mode=MEMO
1dd94 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 RY pragma is use
1dd95 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 d..**.** @(#) $I
1dd96 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c d: memjournal.c,
1dd97 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30 v 1.12 2009/05/0
1dd98 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 4 11:42:30 danie
1dd99 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
1dd9a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 ./* Forward refe
1dd9b 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e rences to intern
1dd9c 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f al structures */
1dd9d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1dd9e 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f MemJournal MemJo
1dd9f 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73 urnal;.typedef s
1dda0 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 truct FilePoint
1dda1 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64 FilePoint;.typed
1dda2 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68 ef struct FileCh
1dda3 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a unk FileChunk;..
1dda4 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 /* Space to hold
1dda5 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1dda6 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74 urnal is allocat
1dda7 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73 ed in increments
1dda8 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79 of.** this many
1dda9 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 bytes..**.** Th
1ddaa 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73 e size chosen is
1ddab 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74 a little less t
1ddac 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74 han a power of t
1ddad 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a wo. That way,.*
1ddae 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20 * the FileChunk
1ddaf 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65 object will have
1ddb0 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d a size that alm
1ddb1 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c ost exactly fill
1ddb2 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d s.** a power-of-
1ddb3 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 two allocation.
1ddb4 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20 This mimimizes
1ddb5 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20 wasted space in
1ddb6 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20 power-of-two.**
1ddb7 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
1ddb8 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f s..*/.#define JO
1ddb9 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1ddba 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 ((int)(1024-size
1ddbb 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 of(FileChunk*)))
1ddbc 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 ../* Macro to fi
1ddbd 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f nd the minimum o
1ddbe 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 f two numeric va
1ddbf 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 lues..*/.#ifndef
1ddc0 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 MIN.# define MI
1ddc1 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f N(x,y) ((x)<(y)?
1ddc2 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a (x):(y)).#endif.
1ddc3 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 ./*.** The rollb
1ddc4 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 ack journal is c
1ddc5 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e omposed of a lin
1ddc6 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 ked list of thes
1ddc7 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f e structures..*/
1ddc8 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e .struct FileChun
1ddc9 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 k {. FileChunk
1ddca 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
1ddcb 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
1ddcc 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e unk in the journ
1ddcd 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e al */. u8 zChun
1ddce 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 k[JOURNAL_CHUNKS
1ddcf 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 IZE]; /* Conte
1ddd0 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b nt of this chunk
1ddd1 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
1ddd2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
1ddd3 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20 s object serves
1ddd4 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f as a cursor into
1ddd5 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
1ddd6 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 urnal..** The cu
1ddd7 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68 rsor can be eith
1ddd8 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f er for reading o
1ddd9 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 r writing..*/.st
1ddda 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b ruct FilePoint {
1dddb 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
1dddc 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
1dddd 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
1ddde 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
1dddf 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 of the file */.
1dde0 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 FileChunk *pChu
1dde1 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nk;
1dde2 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75 /* Specific chu
1dde3 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75 nk into which cu
1dde4 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d rsor points */.}
1dde5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 ;../*.** This su
1dde6 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63 bclass is a subc
1dde7 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
1dde8 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e file. Each open
1dde9 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a memory-journal.
1ddea 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ** is an instanc
1ddeb 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e e of this class.
1ddec 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f .*/.struct MemJo
1dded 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 urnal {. sqlite
1ddee 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
1ddef 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 ethod; /* Par
1ddf0 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 ent class. MUST
1ddf1 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 BE FIRST */. Fi
1ddf2 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b leChunk *pFirst;
1ddf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ddf4 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f Head of in-memo
1ddf5 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f ry chunk-list */
1ddf6 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 . FilePoint end
1ddf7 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 point;
1ddf8 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1ddf9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1ddfa 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f file */. FilePo
1ddfb 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 int readpoint;
1ddfc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1ddfd 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 nter to the end
1ddfe 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61 of the last xRea
1ddff 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a d() */.};../*.**
1de00 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
1de01 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f the in-memory jo
1de02 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 urnal file. Thi
1de03 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
1de04 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 ntation.** of th
1de05 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52 e sqlite3_vfs.xR
1de06 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 ead method..*/.s
1de07 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1de08 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1de09 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1de0a 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1de0b 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1de0c 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1de0d 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1de0e 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1de0f 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1de10 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1de11 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1de12 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1de13 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1de14 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1de15 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1de16 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1de17 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c .){. MemJournal
1de18 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1de19 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a l *)pJfd;. u8 *
1de1a 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 zOut = zBuf;. i
1de1b 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b nt nRead = iAmt;
1de1c 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 . int iChunkOff
1de1d 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b set;. FileChunk
1de1e 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20 *pChunk;.. /*
1de1f 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69 SQLite never tri
1de20 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20 es to read past
1de21 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c the end of a rol
1de22 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 lback journal fi
1de23 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 le */. assert(
1de24 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 iOfst+iAmt<=p->e
1de25 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1de26 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 );.. if( p->rea
1de27 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d dpoint.iOffset!=
1de28 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d iOfst || iOfst==
1de29 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1de2a 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b _int64 iOff = 0;
1de2b 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d . for(pChunk=
1de2c 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20 p->pFirst; .
1de2d 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e ALWAYS(pChun
1de2e 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 k) && (iOff+JOUR
1de2f 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d NAL_CHUNKSIZE)<=
1de30 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70 iOfst;. p
1de31 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e Chunk=pChunk->pN
1de32 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ext. ){.
1de33 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c iOff += JOURNAL
1de34 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 _CHUNKSIZE;.
1de35 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 }. }else{. p
1de36 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 Chunk = p->readp
1de37 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d oint.pChunk;. }
1de38 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 .. iChunkOffset
1de39 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a = (int)(iOfst%J
1de3a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1de3b 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e );. do {. in
1de3c 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e t iSpace = JOURN
1de3d 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 AL_CHUNKSIZE - i
1de3e 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 ChunkOffset;.
1de3f 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e int nCopy = MIN
1de40 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c (nRead, (JOURNAL
1de41 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 _CHUNKSIZE - iCh
1de42 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 unkOffset));.
1de43 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 memcpy(zOut, &p
1de44 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 Chunk->zChunk[iC
1de45 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f hunkOffset], nCo
1de46 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d py);. zOut +=
1de47 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 nCopy;. nRea
1de48 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 d -= iSpace;.
1de49 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 iChunkOffset =
1de4a 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 0;. } while( nR
1de4b 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e ead>=0 && (pChun
1de4c 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 k=pChunk->pNext)
1de4d 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29 !=0 && nRead>0 )
1de4e 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 ;. p->readpoint
1de4f 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 .iOffset = iOfst
1de50 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 +iAmt;. p->read
1de51 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 point.pChunk = p
1de52 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e Chunk;.. return
1de53 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1de54 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1de55 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a to the file..*/.
1de56 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1de57 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 nlWrite(. sqlit
1de58 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1de59 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1de5a 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 file into which
1de5b 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 to write */. c
1de5c 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
1de5d 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 /* Take da
1de5e 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e ta to be written
1de5f 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 from here */.
1de60 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1de61 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1de62 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
1de63 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 te */. sqlite_i
1de64 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1de65 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 * Begin writing
1de66 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 at this offset i
1de67 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a nto the file */.
1de68 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1de69 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1de6a 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e *)pJfd;. int n
1de6b 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 Write = iAmt;.
1de6c 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 u8 *zWrite = (u8
1de6d 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 *)zBuf;.. /* A
1de6e 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
1de6f 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 nal file should
1de70 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 only ever be app
1de71 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d ended to. Random
1de72 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 . ** access wri
1de73 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 tes are not requ
1de74 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a ired by sqlite..
1de75 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
1de76 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e Ofst==p->endpoin
1de77 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55 t.iOffset );. U
1de78 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1de79 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 iOfst);.. while
1de7a 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 ( nWrite>0 ){.
1de7b 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 FileChunk *pCh
1de7c 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e unk = p->endpoin
1de7d 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e t.pChunk;. in
1de7e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d t iChunkOffset =
1de7f 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 (int)(p->endpoi
1de80 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e nt.iOffset%JOURN
1de81 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 AL_CHUNKSIZE);.
1de82 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 int iSpace =
1de83 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 MIN(nWrite, JOUR
1de84 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 NAL_CHUNKSIZE -
1de85 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a iChunkOffset);..
1de86 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 if( iChunkOf
1de87 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 fset==0 ){.
1de88 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 /* New chunk is
1de89 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74 required to ext
1de8a 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f end the file. */
1de8b 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b . FileChunk
1de8c 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 *pNew = sqlite3
1de8d 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 _malloc(sizeof(F
1de8e 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 ileChunk));.
1de8f 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 if( !pNew ){.
1de90 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
1de91 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
1de92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1de93 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b pNew->pNext = 0;
1de94 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e . if( pChun
1de95 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 k ){. ass
1de96 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29 ert( p->pFirst )
1de97 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b ;. pChunk
1de98 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a ->pNext = pNew;.
1de99 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1de9a 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d assert( !p-
1de9b 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 >pFirst );.
1de9c 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 p->pFirst = p
1de9d 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
1de9e 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 p->endpoint.p
1de9f 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 Chunk = pNew;.
1dea0 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 }.. memcpy(
1dea1 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 &p->endpoint.pCh
1dea2 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 unk->zChunk[iChu
1dea3 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 nkOffset], zWrit
1dea4 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20 e, iSpace);.
1dea5 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65 zWrite += iSpace
1dea6 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 ;. nWrite -=
1dea7 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 iSpace;. p->e
1dea8 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1dea9 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a += iSpace;. }..
1deaa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1deab 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
1deac 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a ncate the file..
1dead 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
1deae 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 mjrnlTruncate(sq
1deaf 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1deb0 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 , sqlite_int64 s
1deb1 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e ize){. MemJourn
1deb2 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 al *p = (MemJour
1deb3 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 nal *)pJfd;. Fi
1deb4 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b leChunk *pChunk;
1deb5 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d . assert(size==
1deb6 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0);. UNUSED_PAR
1deb7 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 AMETER(size);.
1deb8 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 pChunk = p->pFir
1deb9 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68 st;. while( pCh
1deba 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 unk ){. FileC
1debb 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 hunk *pTmp = pCh
1debc 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 unk;. pChunk
1debd 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b = pChunk->pNext;
1debe 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1debf 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 e(pTmp);. }. s
1dec0 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
1dec1 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 Open(pJfd);. re
1dec2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1dec3 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
1dec4 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1dec5 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c ic int memjrnlCl
1dec6 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
1dec7 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 *pJfd){. memjr
1dec8 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c nlTruncate(pJfd,
1dec9 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 0);. return SQ
1deca 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1decb 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 ** Sync the file
1decc 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 ..**.** Syncing
1decd 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1dece 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e rnal is a no-op.
1decf 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 And, in fact,
1ded0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1ded1 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 is never called
1ded2 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70 in a working imp
1ded3 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
1ded4 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
1ded5 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65 n.** exists pure
1ded6 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65 ly as a continge
1ded7 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d ncy, in case som
1ded8 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e e malfunction in
1ded9 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 some other.** p
1deda 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61 art of SQLite ca
1dedb 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20 uses Sync to be
1dedc 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b called by mistak
1dedd 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1dede 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c memjrnlSync(sql
1dedf 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
1dee0 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 ed, int NotUsed2
1dee1 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f ){ /*NO_TEST*/
1dee2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1dee3 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
1dee4 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20 tUsed2);
1dee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dee6 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73 /*NO_TEST*/. as
1dee7 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20 sert( 0 );
1dee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1deea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1deeb 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20 TEST*/. return
1deec 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
1deed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1deee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1deef 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1def0 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 /.}
1def1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1def2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1def3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1def4 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a /*NO_TEST*/../*
1def5 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 .** Query the si
1def6 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
1def7 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 n bytes..*/.stat
1def8 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 ic int memjrnlFi
1def9 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 leSize(sqlite3_f
1defa 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 ile *pJfd, sqlit
1defb 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b e_int64 *pSize){
1defc 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1defd 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1defe 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 )pJfd;. *pSize
1deff 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 = (sqlite_int64)
1df00 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 p->endpoint.iOf
1df01 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 fset;. return S
1df02 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1df03 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 ** Table of meth
1df04 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e ods for MemJourn
1df05 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al sqlite3_file
1df06 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 object..*/.stati
1df07 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 c struct sqlite3
1df08 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a _io_methods MemJ
1df09 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 ournalMethods =
1df0a 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 {. 1,
1df0b 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
1df0c 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c n */. memjrnlCl
1df0d 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f ose, /* xClo
1df0e 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 se */. memjrnlR
1df0f 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 ead, /* xRe
1df10 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 ad */. memjrnlW
1df11 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 rite, /* xWr
1df12 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c ite */. memjrnl
1df13 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 Truncate, /* xT
1df14 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d runcate */. mem
1df15 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f jrnlSync, /
1df16 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d * xSync */. mem
1df17 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f jrnlFileSize, /
1df18 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 * xFileSize */.
1df19 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1df1a 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1df1b 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1df1c 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
1df1d 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1df1e 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1df1f 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1df20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1df21 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f /* xFileContro
1df22 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 l */. 0,
1df23 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 /* xSec
1df24 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 torSize */. 0
1df25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1df26 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 * xDeviceCharact
1df27 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a eristics */.};..
1df28 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f /* .** Open a jo
1df29 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1df2a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1df2b 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 id sqlite3MemJou
1df2c 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
1df2d 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 _file *pJfd){.
1df2e 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1df2f 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1df30 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 fd;. assert( EI
1df31 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
1df32 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 NT(p) );. memse
1df33 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d t(p, 0, sqlite3M
1df34 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 emJournalSize())
1df35 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d ;. p->pMethod =
1df36 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 &MemJournalMeth
1df37 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ods;.}../*.** Re
1df38 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
1df39 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73 file-handle pas
1df3a 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 sed as an argume
1df3b 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d nt is .** an in-
1df3c 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a memory journal .
1df3d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1df3e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
1df3f 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
1df40 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 3_file *pJfd){.
1df41 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d return pJfd->pM
1df42 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72 ethods==&MemJour
1df43 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f nalMethods;.}../
1df44 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
1df45 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1df46 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1df47 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 re a MemJournal
1df48 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a that uses vfs.**
1df49 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 pVfs to create
1df4a 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
1df4b 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f n-disk files..*/
1df4c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1df4d 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f int sqlite3MemJo
1df4e 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b urnalSize(void){
1df4f 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 . return sizeof
1df50 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a (MemJournal);.}.
1df51 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1df52 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e End of memjourn
1df53 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a al.c ***********
1df54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1df56 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1df57 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b Begin file walk
1df58 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
1df59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1df5b 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 ./*.** 2008 Augu
1df5c 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 16.**.** The
1df5d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1df5e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1df5f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1df60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1df61 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1df62 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1df63 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
1df64 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
1df65 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
1df66 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
1df67 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
1df68 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
1df69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
1df6a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
1df6b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
1df6c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
1df6d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1df6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df72 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
1df73 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 e contains routi
1df74 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c nes used for wal
1df75 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 king the parser
1df76 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 tree for.** an S
1df77 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a QL statement..**
1df78 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e .** $Id: walker.
1df79 63 2c 76 20 31 2e 34 20 32 30 30 39 2f 30 34 2f c,v 1.4 2009/04/
1df7a 30 38 20 31 33 3a 35 31 3a 35 32 20 64 72 68 20 08 13:51:52 drh
1df7b 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a Exp $.*/.../*.**
1df7c 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
1df7d 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b ion tree. Invok
1df7e 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
1df7f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64 nce for each nod
1df80 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 e.** of the expr
1df81 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65 ession, while de
1df82 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74 cending. (In ot
1df83 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 her words, the c
1df84 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e allback.** is in
1df85 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73 voked before vis
1df86 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29 iting children.)
1df87 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1df88 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 n value from the
1df89 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 callback should
1df8a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57 be one of the W
1df8b 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 RC_*.** constant
1df8c 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77 s to specify how
1df8d 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68 to proceed with
1df8e 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a the walk..**.**
1df8f 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 WRC_Continue
1df90 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64 Continue d
1df91 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74 escending down t
1df92 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20 he tree..**.**
1df93 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 WRC_Prune
1df94 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 Do not desce
1df95 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f nd into child no
1df96 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a des. But allow.
1df97 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1df98 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b the walk
1df99 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
1df9a 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e h sibling nodes.
1df9b 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62 .**.** WRC_Ab
1df9c 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e ort Do n
1df9d 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73 o more callbacks
1df9e 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74 . Unwind the st
1df9f 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ack and.**
1dfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dfa1 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c return the top-l
1dfa2 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a evel walk call..
1dfa3 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e **.** The return
1dfa4 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 value from this
1dfa5 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f routine is WRC_
1dfa6 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e Abort to abandon
1dfa7 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a the tree walk.*
1dfa8 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e * and WRC_Contin
1dfa9 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a ue to continue..
1dfaa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1dfab 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
1dfac 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 kExpr(Walker *pW
1dfad 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1dfae 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 pr){. int rc;.
1dfaf 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 if( pExpr==0 )
1dfb0 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1dfb1 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28 nue;. testcase(
1dfb2 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1dfb3 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e (pExpr, EP_Token
1dfb4 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63 Only) );. testc
1dfb5 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 ase( ExprHasProp
1dfb6 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 53 erty(pExpr, EP_S
1dfb7 70 61 6e 54 6f 6b 65 6e 29 20 29 3b 0a 20 20 74 panToken) );. t
1dfb8 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61 73 estcase( ExprHas
1dfb9 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1dfba 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 EP_Reduced) );.
1dfbb 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 rc = pWalker->x
1dfbc 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 ExprCallback(pWa
1dfbd 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 lker, pExpr);.
1dfbe 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 if( rc==WRC_Cont
1dfbf 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20 20 inue.
1dfc0 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41 6e && !ExprHasAn
1dfc1 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
1dfc2 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f EP_TokenOnly|EP_
1dfc3 53 70 61 6e 54 6f 6b 65 6e 29 20 29 7b 0a 20 20 SpanToken) ){.
1dfc4 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1dfc5 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 kExpr(pWalker, p
1dfc6 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 Expr->pLeft) ) r
1dfc7 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1dfc8 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1dfc9 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1dfca 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 , pExpr->pRight)
1dfcb 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1dfcc 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 ort;. if( Exp
1dfcd 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
1dfce 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pr, EP_xIsSelect
1dfcf 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ) ){. if( s
1dfd0 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1dfd1 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d (pWalker, pExpr-
1dfd2 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 >x.pSelect) ) re
1dfd3 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1dfd4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dfd5 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1dfd6 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1dfd7 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 , pExpr->x.pList
1dfd8 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1dfd9 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a bort;. }. }.
1dfda 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 return rc & WR
1dfdb 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a C_Abort;.}../*.*
1dfdc 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 * Call sqlite3Wa
1dfdd 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 lkExpr() for eve
1dfde 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ry expression in
1dfdf 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c list p or until
1dfe0 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 .** an abort req
1dfe1 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f uest is seen..*/
1dfe2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1dfe3 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
1dfe4 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a xprList(Walker *
1dfe5 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 pWalker, ExprLis
1dfe6 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20 t *p){. int i,
1dfe7 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 rc = WRC_Continu
1dfe8 65 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 e;. struct Expr
1dfe9 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1dfea 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
1dfeb 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c for(i=p->nExpr,
1dfec 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 pItem=p->a; i>0
1dfed 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
1dfee 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1dfef 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b e3WalkExpr(pWalk
1dff0 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 er, pItem->pExpr
1dff1 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1dff2 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a bort;. }. }.
1dff3 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 return rc & WR
1dff4 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f C_Continue;.}../
1dff5 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78 *.** Walk all ex
1dff6 70 72 65 73 73 69 6f 6e 73 20 61 73 73 6f 63 69 pressions associ
1dff7 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 43 54 ated with SELECT
1dff8 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 statement p. D
1dff9 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 o.** not invoke
1dffa 74 68 65 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 the SELECT callb
1dffb 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f ack on p, but do
1dffc 20 28 6f 66 20 63 6f 75 72 73 65 29 20 69 6e 76 (of course) inv
1dffd 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20 oke.** any expr
1dffe 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64 20 53 45 callbacks and SE
1dfff 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 73 20 74 LECT callbacks t
1e000 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75 hat come from su
1e001 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74 bqueries..** Ret
1e002 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 urn WRC_Abort or
1e003 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a WRC_Continue..*
1e004 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1e005 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
1e006 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c 6b 65 SelectExpr(Walke
1e007 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1e008 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 73 71 ct *p){. if( sq
1e009 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1e00a 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 t(pWalker, p->pE
1e00b 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 List) ) return W
1e00c 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 RC_Abort;. if(
1e00d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1e00e 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65 pWalker, p->pWhe
1e00f 72 65 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 re) ) return WRC
1e010 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1e011 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1e012 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 t(pWalker, p->pG
1e013 72 6f 75 70 42 79 29 20 29 20 72 65 74 75 72 6e roupBy) ) return
1e014 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 WRC_Abort;. if
1e015 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 ( sqlite3WalkExp
1e016 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 r(pWalker, p->pH
1e017 61 76 69 6e 67 29 20 29 20 72 65 74 75 72 6e 20 aving) ) return
1e018 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 WRC_Abort;. if(
1e019 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1e01a 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d List(pWalker, p-
1e01b 3e 70 4f 72 64 65 72 42 79 29 20 29 20 72 65 74 >pOrderBy) ) ret
1e01c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1e01d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1e01e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d Expr(pWalker, p-
1e01f 3e 70 4c 69 6d 69 74 29 20 29 20 72 65 74 75 72 >pLimit) ) retur
1e020 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 n WRC_Abort;. i
1e021 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1e022 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 pr(pWalker, p->p
1e023 4f 66 66 73 65 74 29 20 29 20 72 65 74 75 72 6e Offset) ) return
1e024 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65 WRC_Abort;. re
1e025 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1e026 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b e;.}../*.** Walk
1e027 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 73 the parse trees
1e028 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1e029 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 20 all subqueries
1e02a 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 in the.** FROM c
1e02b 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20 lause of SELECT
1e02c 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f statement p. Do
1e02d 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 not invoke the
1e02e 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 select.** callba
1e02f 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 ck on p, but do
1e030 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63 invoke it on eac
1e031 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 73 75 h FROM clause su
1e032 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e bquery.** and on
1e033 20 61 6e 79 20 73 75 62 71 75 65 72 69 65 73 20 any subqueries
1e034 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69 6e 20 further down in
1e035 74 68 65 20 74 72 65 65 2e 20 20 52 65 74 75 72 the tree. Retur
1e036 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20 n .** WRC_Abort
1e037 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b or WRC_Continue;
1e038 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1e039 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1e03a 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c lkSelectFrom(Wal
1e03b 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
1e03c 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c lect *p){. SrcL
1e03d 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 ist *pSrc;. int
1e03e 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 i;. struct Src
1e03f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1e040 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 ;.. pSrc = p->p
1e041 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72 63 20 Src;. if( pSrc
1e042 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 ){. for(i=pSr
1e043 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 c->nSrc, pItem=p
1e044 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d Src->a; i>0; i--
1e045 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1e046 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1e047 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c kSelect(pWalker,
1e048 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 pItem->pSelect)
1e049 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1e04a 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1e04b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1e04c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
1e04d 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a tinue;.} ../*.**
1e04e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c Call sqlite3Wal
1e04f 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 kExpr() for ever
1e050 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 y expression in
1e051 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 Select statement
1e052 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 p..** Invoke sq
1e053 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1e054 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 ) for subqueries
1e055 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
1e056 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 use and.** on th
1e057 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 e compound selec
1e058 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 t chain, p->pPri
1e059 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e or..**.** Return
1e05a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e WRC_Continue un
1e05b 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 der normal condi
1e05c 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 tions. Return W
1e05d 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 RC_Abort if.** t
1e05e 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 here is an abort
1e05f 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 request..**.**
1e060 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f If the Walker do
1e061 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 es not have an x
1e062 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 SelectCallback()
1e063 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
1e064 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 ne.** is a no-op
1e065 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 returning WRC_C
1e066 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ontinue..*/.SQLI
1e067 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1e068 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1e069 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1e06a 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 , Select *p){.
1e06b 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d int rc;. if( p=
1e06c 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 =0 || pWalker->x
1e06d 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d SelectCallback==
1e06e 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 0 ) return WRC_C
1e06f 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 ontinue;. rc =
1e070 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1e071 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 while( p ){.
1e072 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 rc = pWalker->x
1e073 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 SelectCallback(p
1e074 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 Walker, p);.
1e075 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a if( rc ) break;.
1e076 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1e077 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 alkSelectExpr(pW
1e078 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 alker, p) ) retu
1e079 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1e07a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1e07b 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c kSelectFrom(pWal
1e07c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e ker, p) ) return
1e07d 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1e07e 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 p = p->pPrior;.
1e07f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 }. return rc &
1e080 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f WRC_Abort;.}../
1e081 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1e082 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a nd of walker.c *
1e083 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1e086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1e087 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 egin file resolv
1e088 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1e089 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e08a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1e08b 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 *.** 2008 August
1e08c 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 18.**.** The au
1e08d 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1e08e 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1e08f 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1e090 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1e091 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1e092 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1e093 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1e094 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1e095 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1e096 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1e097 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1e098 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1e099 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1e09a 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1e09b 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1e09c 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1e09d 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1e09e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e09f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e0a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e0a2 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
1e0a3 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 le contains rout
1e0a4 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 ines used for wa
1e0a5 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 lking the parser
1e0a6 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 tree and.** res
1e0a7 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 olve all identif
1e0a8 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 iers by associat
1e0a9 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 ing them with a
1e0aa 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 particular.** ta
1e0ab 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a ble and column..
1e0ac 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c **.** $Id: resol
1e0ad 76 65 2e 63 2c 76 20 31 2e 32 32 20 32 30 30 39 ve.c,v 1.22 2009
1e0ae 2f 30 35 2f 30 35 20 31 35 3a 34 36 3a 34 33 20 /05/05 15:46:43
1e0af 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
1e0b0 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 .** Turn the pEx
1e0b1 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e pr expression in
1e0b2 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 to an alias for
1e0b3 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 the iCol-th colu
1e0b4 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 mn of the.** res
1e0b5 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 ult set in pELis
1e0b6 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
1e0b7 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d result set colum
1e0b8 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f n is a simple co
1e0b9 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 lumn reference,
1e0ba 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1e0bb 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 e.** makes an ex
1e0bc 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 act copy. But f
1e0bd 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e or any other kin
1e0be 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c d of expression,
1e0bf 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
1e0c0 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 make a copy of
1e0c1 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 the result set c
1e0c2 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 olumn as the arg
1e0c3 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 ument to the.**
1e0c4 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 TK_AS operator.
1e0c5 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 The TK_AS opera
1e0c6 74 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 tor causes the e
1e0c7 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a xpression to be.
1e0c8 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 ** evaluated jus
1e0c9 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 t once and then
1e0ca 72 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 reused for each
1e0cb 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 alias..**.** The
1e0cc 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 reason for supp
1e0cd 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 ressing the TK_A
1e0ce 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 S term when the
1e0cf 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
1e0d0 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e simple.** column
1e0d1 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f reference is so
1e0d2 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
1e0d3 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 reference will
1e0d4 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 be recognized as
1e0d5 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e .** usable by in
1e0d6 64 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 dices within the
1e0d7 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 WHERE clause pr
1e0d8 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 ocessing logic.
1e0d9 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 .**.** Hack: Th
1e0da 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 e TK_AS operator
1e0db 20 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 is inhibited if
1e0dc 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 zType[0]=='G'.
1e0dd 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 This means.** t
1e0de 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 hat in a GROUP B
1e0df 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 Y clause, the ex
1e0e0 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c pression is eval
1e0e1 75 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 uated twice. He
1e0e2 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 nce:.**.** S
1e0e3 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 ELECT random()%5
1e0e4 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 AS x, count(*)
1e0e5 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 FROM tab GROUP B
1e0e6 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 Y x.**.** Is equ
1e0e7 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a ivalent to:.**.*
1e0e8 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e * SELECT ran
1e0e9 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f dom()%5 AS x, co
1e0ea 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 unt(*) FROM tab
1e0eb 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 GROUP BY random(
1e0ec 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 )%5.**.** The re
1e0ed 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 sult of random()
1e0ee 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 %5 in the GROUP
1e0ef 42 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f BY clause is pro
1e0f0 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a bably different.
1e0f1 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 ** from the resu
1e0f2 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 lt in the result
1e0f3 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 -set. We might
1e0f4 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 fix this someday
1e0f5 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 . Or.** then ag
1e0f6 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f ain, we might no
1e0f7 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 t....*/.static v
1e0f8 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 oid resolveAlias
1e0f9 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1e0fa 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 e, /* Pa
1e0fb 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1e0fc 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
1e0fd 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72 ist, /* A r
1e0fe 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 esult set */. i
1e0ff 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 nt iCol,
1e100 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d /* A colum
1e101 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 n in the result
1e102 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d set. 0..pEList-
1e103 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78 >nExpr-1 */. Ex
1e104 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 pr *pExpr,
1e105 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72 /* Transfor
1e106 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61 m this into an a
1e107 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75 lias to the resu
1e108 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73 lt set */. cons
1e109 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 t char *zType
1e10a 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72 /* "GROUP" or
1e10b 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a "ORDER" or "" *
1e10c 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72 /.){. Expr *pOr
1e10d 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ig; /*
1e10e 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c The iCol-th col
1e10f 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c umn of the resul
1e110 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 t set */. Expr
1e111 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 *pDup;
1e112 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72 /* Copy of pOr
1e113 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ig */. sqlite3
1e114 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f *db; /
1e115 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1e116 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 onnection */..
1e117 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 assert( iCol>=0
1e118 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e && iCol<pEList->
1e119 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67 nExpr );. pOrig
1e11a 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f = pEList->a[iCo
1e11b 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 l].pExpr;. asse
1e11c 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a rt( pOrig!=0 );.
1e11d 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d assert( pOrig-
1e11e 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f >flags & EP_Reso
1e11f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70 lved );. db = p
1e120 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 44 75 Parse->db;. pDu
1e121 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1e122 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 up(db, pOrig, 0)
1e123 3b 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 ;. if( pDup==0
1e124 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 ) return;. sqli
1e125 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c te3TokenCopy(db,
1e126 20 26 70 44 75 70 2d 3e 74 6f 6b 65 6e 2c 20 26 &pDup->token, &
1e127 70 4f 72 69 67 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 pOrig->token);.
1e128 20 69 66 28 20 70 44 75 70 2d 3e 6f 70 21 3d 54 if( pDup->op!=T
1e129 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79 70 K_COLUMN && zTyp
1e12a 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 20 20 e[0]!='G' ){.
1e12b 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 50 pDup = sqlite3P
1e12c 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f Expr(pParse, TK_
1e12d 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b AS, pDup, 0, 0);
1e12e 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 . if( pDup==0
1e12f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 ) return;. i
1e130 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f f( pEList->a[iCo
1e131 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a l].iAlias==0 ){.
1e132 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b pEList->a[
1e133 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28 iCol].iAlias = (
1e134 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e u16)(++pParse->n
1e135 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20 Alias);. }.
1e136 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d pDup->iTable =
1e137 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d pEList->a[iCol]
1e138 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 69 .iAlias;. }. i
1e139 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 f( pExpr->flags
1e13a 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 & EP_ExpCollate
1e13b 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70 43 6f ){. pDup->pCo
1e13c 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c ll = pExpr->pCol
1e13d 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 l;. pDup->fla
1e13e 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
1e13f 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ate;. }. sqlit
1e140 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20 e3ExprClear(db,
1e141 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79 pExpr);. memcpy
1e142 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20 73 69 (pExpr, pDup, si
1e143 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b 0a 20 zeof(*pExpr));.
1e144 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1e145 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a b, pDup);.}../*.
1e146 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d ** Given the nam
1e147 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 e of a column of
1e148 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 the form X.Y.Z
1e149 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a or Y.Z or just Z
1e14a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 , look up.** tha
1e14b 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 65 t name in the se
1e14c 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61 62 6c t of source tabl
1e14d 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20 61 es in pSrcList a
1e14e 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45 78 70 nd make the pExp
1e14f 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e r .** expression
1e150 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61 63 6b node refer back
1e151 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63 65 20 to that source
1e152 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c column. The fol
1e153 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a lowing changes.*
1e154 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 45 * are made to pE
1e155 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 xpr:.**.** pE
1e156 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20 xpr->iDb
1e157 20 20 20 53 65 74 20 74 68 65 20 69 6e 64 65 78 Set the index
1e158 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 in db->aDb[] of
1e159 20 74 68 65 20 64 61 74 61 62 61 73 65 20 58 0a the database X.
1e15a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1e15b 20 20 20 20 20 20 20 20 20 20 20 28 65 76 65 6e (even
1e15c 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64 if X is implied
1e15d 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e )..** pExpr->
1e15e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 53 65 iTable Se
1e15f 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 20 t to the cursor
1e160 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 number for the t
1e161 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a able obtained.**
1e162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e163 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 70 53 from pS
1e164 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 rcList..** pE
1e165 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20 20 20 xpr->pTab
1e166 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 Points to the
1e167 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 Table structure
1e168 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 69 66 of X.Y (even if
1e169 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1e16a 20 20 20 20 20 20 20 20 20 20 20 20 58 20 61 6e X an
1e16b 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 6c 69 d/or Y are impli
1e16c 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 ed.).** pExpr
1e16d 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 ->iColumn
1e16e 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d Set to the colum
1e16f 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 n number within
1e170 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 the table..**
1e171 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 pExpr->op
1e172 20 20 20 20 20 20 53 65 74 20 74 6f 20 54 4b 5f Set to TK_
1e173 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 COLUMN..** pE
1e174 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 xpr->pLeft
1e175 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f Any expressio
1e176 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f n this points to
1e177 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 is deleted.**
1e178 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 pExpr->pRight
1e179 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 Any expre
1e17a 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 ssion this point
1e17b 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 2e s to is deleted.
1e17c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 62 54 6f .**.** The pDbTo
1e17d 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ken is the name
1e17e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1e17f 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 (the "X"). This
1e180 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a value may be.**
1e181 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 NULL meaning th
1e182 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 at name is of th
1e183 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e e form Y.Z or Z.
1e184 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 Any available
1e185 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 database.** can
1e186 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 70 54 be used. The pT
1e187 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 74 68 65 ableToken is the
1e188 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
1e189 6c 65 20 28 74 68 65 20 22 59 22 29 2e 20 20 54 le (the "Y"). T
1e18a 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e his.** value can
1e18b 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 44 62 54 be NULL if pDbT
1e18c 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c oken is also NUL
1e18d 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 54 6f 6b L. If pTableTok
1e18e 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a en is NULL it.**
1e18f 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
1e190 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65 form of the name
1e191 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74 20 63 is Z and that c
1e192 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20 olumns from any
1e193 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20 table.** can be
1e194 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 used..**.** If t
1e195 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 he name cannot b
1e196 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 e resolved unamb
1e197 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 iguously, leave
1e198 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1e199 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e .** in pParse an
1e19a 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 d return non-zer
1e19b 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 o. Return zero
1e19c 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 on success..*/.s
1e19d 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 tatic int lookup
1e19e 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 Name(. Parse *p
1e19f 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 Parse, /*
1e1a0 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 The parsing cont
1e1a1 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ext */. Token *
1e1a2 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 20 2f 2a pDbToken, /*
1e1a3 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
1e1a4 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 abase containing
1e1a5 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 table, or NULL
1e1a6 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 */. Token *pTab
1e1a7 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 4e 61 6d leToken, /* Nam
1e1a8 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 e of table conta
1e1a9 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 ining column, or
1e1aa 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e NULL */. Token
1e1ab 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2c 20 *pColumnToken,
1e1ac 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
1e1ad 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 olumn. */. Name
1e1ae 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 Context *pNC,
1e1af 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e /* The name con
1e1b0 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 text used to res
1e1b1 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f olve the name */
1e1b2 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 . Expr *pExpr
1e1b3 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 /* Make
1e1b4 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 this EXPR node p
1e1b5 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 oint to the sele
1e1b6 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 cted column */.)
1e1b7 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 {. char *zDb =
1e1b8 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 0; /* Name
1e1b9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1e1ba 2e 20 20 54 68 65 20 22 58 22 20 69 6e 20 58 2e . The "X" in X.
1e1bb 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a Y.Z */. char *z
1e1bc 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a Tab = 0; /*
1e1bd 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
1e1be 6c 65 2e 20 20 54 68 65 20 22 59 22 20 69 6e 20 le. The "Y" in
1e1bf 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 2a 2f 0a X.Y.Z or Y.Z */.
1e1c0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 30 char *zCol = 0
1e1c1 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f ; /* Name o
1e1c2 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 f the column. T
1e1c3 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 6e 74 20 he "Z" */. int
1e1c4 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 i, j;
1e1c5 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1e1c6 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20 3d s */. int cnt =
1e1c7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1e1c8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1e1c9 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63 er of matching c
1e1ca 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 olumn names */.
1e1cb 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 30 3b int cntTab = 0;
1e1cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e1cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e1ce 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20 6e matching table n
1e1cf 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ames */. sqlite
1e1d0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1e1d1 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 db; /* T
1e1d2 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1e1d3 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 ection */. stru
1e1d4 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1e1d5 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 2f 2a *pItem; /*
1e1d6 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 Use for looping
1e1d7 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20 69 over pSrcList i
1e1d8 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 tems */. struct
1e1d9 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1e1da 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54 Match = 0; /* T
1e1db 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 72 63 he matching pSrc
1e1dc 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 20 4e List item */. N
1e1dd 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70 ameContext *pTop
1e1de 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 20 20 NC = pNC;
1e1df 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 63 6f /* First nameco
1e1e0 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c 69 73 ntext in the lis
1e1e1 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 t */. Schema *p
1e1e2 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 20 20 Schema = 0;
1e1e3 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 /* Sche
1e1e4 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 ma of the expres
1e1e5 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 sion */.. asser
1e1e6 74 28 20 70 4e 43 20 29 3b 20 2f 2a 20 74 68 65 t( pNC ); /* the
1e1e7 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 61 name context ca
1e1e8 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f nnot be NULL. */
1e1e9 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 75 . assert( pColu
1e1ea 6d 6e 54 6f 6b 65 6e 20 26 26 20 70 43 6f 6c 75 mnToken && pColu
1e1eb 6d 6e 54 6f 6b 65 6e 2d 3e 7a 20 29 3b 20 2f 2a mnToken->z ); /*
1e1ec 20 54 68 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 The Z in X.Y.Z
1e1ed 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a cannot be NULL *
1e1ee 2f 0a 0a 20 20 2f 2a 20 44 65 71 75 6f 74 65 20 /.. /* Dequote
1e1ef 61 6e 64 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 and zero-termina
1e1f0 74 65 20 74 68 65 20 6e 61 6d 65 73 20 2a 2f 0a te the names */.
1e1f1 20 20 7a 44 62 20 3d 20 73 71 6c 69 74 65 33 4e zDb = sqlite3N
1e1f2 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
1e1f3 20 70 44 62 54 6f 6b 65 6e 29 3b 0a 20 20 7a 54 pDbToken);. zT
1e1f4 61 62 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 ab = sqlite3Name
1e1f5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 FromToken(db, pT
1e1f6 61 62 6c 65 54 6f 6b 65 6e 29 3b 0a 20 20 7a 43 ableToken);. zC
1e1f7 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 ol = sqlite3Name
1e1f8 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 FromToken(db, pC
1e1f9 6f 6c 75 6d 6e 54 6f 6b 65 6e 29 3b 0a 20 20 69 olumnToken);. i
1e1fa 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1e1fb 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
1e1fc 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a lookupname_end;.
1e1fd 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 }.. /* Initia
1e1fe 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20 74 6f lize the node to
1e1ff 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 20 70 no-match */. p
1e200 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 2d Expr->iTable = -
1e201 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54 61 62 1;. pExpr->pTab
1e202 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 = 0;.. /* Star
1e203 74 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d t at the inner-m
1e204 6f 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 ost context and
1e205 6d 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74 move outward unt
1e206 69 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f il a match is fo
1e207 75 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 und */. while(
1e208 70 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b pNC && cnt==0 ){
1e209 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 . ExprList *p
1e20a 45 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69 EList;. SrcLi
1e20b 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 st *pSrcList = p
1e20c 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20 NC->pSrcList;..
1e20d 20 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 if( pSrcList
1e20e 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 ){. for(i=0
1e20f 2c 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74 , pItem=pSrcList
1e210 2d 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d ->a; i<pSrcList-
1e211 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 >nSrc; i++, pIte
1e212 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61 m++){. Ta
1e213 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 ble *pTab;.
1e214 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 int iDb;.
1e215 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c Column *pCol
1e216 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61 ;. . pTa
1e217 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b b = pItem->pTab;
1e218 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1e219 20 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62 pTab!=0 && pTab
1e21a 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 ->zName!=0 );.
1e21b 20 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 iDb = sqli
1e21c 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
1e21d 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 (db, pTab->pSche
1e21e 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ma);. ass
1e21f 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e ert( pTab->nCol>
1e220 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0 );. if(
1e221 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 zTab ){.
1e222 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 if( pItem->zA
1e223 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 lias ){.
1e224 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 char *zTabNa
1e225 6d 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 me = pItem->zAli
1e226 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 as;.
1e227 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1e228 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 mp(zTabName, zTa
1e229 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 b)!=0 ) continue
1e22a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
1e22b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 e{. c
1e22c 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 har *zTabName =
1e22d 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 pTab->zName;.
1e22e 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 if( zTa
1e22f 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 bName==0 || sqli
1e230 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e te3StrICmp(zTabN
1e231 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 ame, zTab)!=0 )
1e232 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
1e233 20 20 20 20 20 20 69 66 28 20 7a 44 62 21 3d 30 if( zDb!=0
1e234 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1e235 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e mp(db->aDb[iDb].
1e236 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29 zName, zDb)!=0 )
1e237 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1e238 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
1e239 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e23a 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1e23b 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 63 6e if( 0==(cn
1e23c 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20 20 tTab++) ){.
1e23d 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1e23e 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 le = pItem->iCur
1e23f 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 sor;. p
1e240 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 Expr->pTab = pTa
1e241 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 63 b;. pSc
1e242 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1e243 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 hema;.
1e244 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a pMatch = pItem;.
1e245 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e246 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d for(j=0, pCol=
1e247 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 pTab->aCol; j<pT
1e248 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 ab->nCol; j++, p
1e249 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 Col++){.
1e24a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
1e24b 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 ICmp(pCol->zName
1e24c 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 , zCol)==0 ){.
1e24d 20 20 20 20 20 20 20 20 20 20 49 64 4c 69 73 74 IdList
1e24e 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 *pUsing;.
1e24f 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
1e250 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1e251 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e iTable = pItem->
1e252 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 iCursor;.
1e253 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 pExpr->pTab
1e254 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 = pTab;.
1e255 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 pMatch = pI
1e256 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 tem;.
1e257 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d pSchema = pTab-
1e258 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 >pSchema;.
1e259 20 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 /* Substit
1e25a 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 28 63 ute the rowid (c
1e25b 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 olumn -1) for th
1e25c 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 e INTEGER PRIMAR
1e25d 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20 20 20 20 Y KEY */.
1e25e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1e25f 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 umn = j==pTab->i
1e260 50 4b 65 79 20 3f 20 2d 31 20 3a 20 6a 3b 0a 20 PKey ? -1 : j;.
1e261 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 if( i
1e262 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d <pSrcList->nSrc-
1e263 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 1 ){.
1e264 20 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e if( pItem[1].
1e265 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 jointype & JT_NA
1e266 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 TURAL ){.
1e267 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 /* If t
1e268 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 his match occurr
1e269 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 ed in the left t
1e26a 61 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 able of a natura
1e26b 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 l join,.
1e26c 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ** then
1e26d 73 6b 69 70 20 74 68 65 20 72 69 67 68 74 20 74 skip the right t
1e26e 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 able to avoid a
1e26f 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 duplicate match
1e270 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
1e271 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 pItem++;.
1e272 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b i++;
1e273 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d . }
1e274 65 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 else if( (pUsing
1e275 20 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 = pItem[1].pUsi
1e276 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ng)!=0 ){.
1e277 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
1e278 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 this match occur
1e279 73 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 s on a column th
1e27a 61 74 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 at is in the USI
1e27b 4e 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 NG clause.
1e27c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 ** of
1e27d 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 a join, skip the
1e27e 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 search of the r
1e27f 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 ight table of th
1e280 65 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 e join.
1e281 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f ** to avo
1e282 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d id a duplicate m
1e283 61 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 atch there. */.
1e284 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
1e285 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 nt k;.
1e286 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b for(k=0; k
1e287 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b <pUsing->nId; k+
1e288 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 +){.
1e289 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1e28a 33 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3StrICmp(pUsing-
1e28b 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f >a[k].zName, zCo
1e28c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
1e28d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 pIt
1e28e 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 em++;.
1e28f 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 i++;.
1e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e291 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1e292 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1e293 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
1e294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
1e295 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1e296 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
1e297 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1e298 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1e299 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 }..#ifndef SQ
1e29a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1e29b 52 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 R. /* If we h
1e29c 61 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 ave not already
1e29d 72 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d resolved the nam
1e29e 65 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 e, then maybe .
1e29f 20 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 ** it is a ne
1e2a0 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 w.* or old.* tri
1e2a1 67 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 gger argument re
1e2a2 66 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 ference. */.
1e2a3 20 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 if( zDb==0 &&
1e2a4 20 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d zTab!=0 && cnt=
1e2a5 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 74 72 =0 && pParse->tr
1e2a6 69 67 53 74 61 63 6b 21 3d 30 20 29 7b 0a 20 20 igStack!=0 ){.
1e2a7 20 20 20 20 54 72 69 67 67 65 72 53 74 61 63 6b TriggerStack
1e2a8 20 2a 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 *pTriggerStack
1e2a9 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 = pParse->trigSt
1e2aa 61 63 6b 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 ack;. Table
1e2ab 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 *pTab = 0;.
1e2ac 20 20 75 33 32 20 2a 70 69 43 6f 6c 4d 61 73 6b u32 *piColMask
1e2ad 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 = 0;. if(
1e2ae 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e pTriggerStack->n
1e2af 65 77 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73 ewIdx != -1 && s
1e2b0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e qlite3StrICmp("n
1e2b1 65 77 22 2c 20 7a 54 61 62 29 20 3d 3d 20 30 20 ew", zTab) == 0
1e2b2 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 ){. pExpr
1e2b3 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72 69 67 ->iTable = pTrig
1e2b4 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 gerStack->newIdx
1e2b5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1e2b6 28 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d ( pTriggerStack-
1e2b7 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 20 >pTab );.
1e2b8 20 70 54 61 62 20 3d 20 70 54 72 69 67 67 65 72 pTab = pTrigger
1e2b9 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20 Stack->pTab;.
1e2ba 20 20 20 20 20 70 69 43 6f 6c 4d 61 73 6b 20 3d piColMask =
1e2bb 20 26 28 70 54 72 69 67 67 65 72 53 74 61 63 6b &(pTriggerStack
1e2bc 2d 3e 6e 65 77 43 6f 6c 4d 61 73 6b 29 3b 0a 20 ->newColMask);.
1e2bd 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1e2be 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c TriggerStack->ol
1e2bf 64 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73 71 dIdx != -1 && sq
1e2c0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c lite3StrICmp("ol
1e2c1 64 22 2c 20 7a 54 61 62 29 3d 3d 30 20 29 7b 0a d", zTab)==0 ){.
1e2c2 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1e2c3 54 61 62 6c 65 20 3d 20 70 54 72 69 67 67 65 72 Table = pTrigger
1e2c4 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 3b 0a 20 Stack->oldIdx;.
1e2c5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1e2c6 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 TriggerStack->pT
1e2c7 61 62 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54 ab );. pT
1e2c8 61 62 20 3d 20 70 54 72 69 67 67 65 72 53 74 61 ab = pTriggerSta
1e2c9 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 ck->pTab;.
1e2ca 20 20 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 piColMask = &(
1e2cb 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f pTriggerStack->o
1e2cc 6c 64 43 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 ldColMask);.
1e2cd 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 }.. if( p
1e2ce 54 61 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20 Tab ){ .
1e2cf 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 int iCol;.
1e2d0 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d Column *pCol =
1e2d1 20 70 54 61 62 2d 3e 61 43 6f 6c 3b 0a 0a 20 20 pTab->aCol;..
1e2d2 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 pSchema =
1e2d3 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 pTab->pSchema;.
1e2d4 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b 3b cntTab++;
1e2d5 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f . for(iCo
1e2d6 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 l=0; iCol < pTab
1e2d7 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 2c 20 ->nCol; iCol++,
1e2d8 70 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 20 pCol++) {.
1e2d9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1e2da 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 trICmp(pCol->zNa
1e2db 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a me, zCol)==0 ){.
1e2dc 20 20 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b cnt+
1e2dd 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 +;. p
1e2de 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
1e2df 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 iCol==pTab->iPKe
1e2e0 79 20 3f 20 2d 31 20 3a 20 69 43 6f 6c 3b 0a 20 y ? -1 : iCol;.
1e2e1 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
1e2e2 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 ->pTab = pTab;.
1e2e3 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 if( i
1e2e4 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 Col>=0 ){.
1e2e5 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
1e2e6 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 ( iCol==31 );.
1e2e7 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 test
1e2e8 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 case( iCol==32 )
1e2e9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1e2ea 69 66 28 20 69 43 6f 6c 3e 3d 33 32 20 29 7b 0a if( iCol>=32 ){.
1e2eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e2ec 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 78 66 *piColMask = 0xf
1e2ed 66 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 fffffff;.
1e2ee 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1e2ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 *p
1e2f0 69 43 6f 6c 4d 61 73 6b 20 7c 3d 20 28 28 75 33 iColMask |= ((u3
1e2f1 32 29 31 29 3c 3c 69 43 6f 6c 3b 0a 20 20 20 20 2)1)<<iCol;.
1e2f2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1e2f3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e2f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e2f5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1e2f6 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1e2f7 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
1e2f8 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1e2f9 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20 20 TRIGGER) */..
1e2fa 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68 61 /*. ** Perha
1e2fb 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20 61 ps the name is a
1e2fc 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 reference to th
1e2fd 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a 20 e ROWID. */.
1e2fe 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 if( cnt==0 &&
1e2ff 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73 71 cntTab==1 && sq
1e300 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f lite3IsRowid(zCo
1e301 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 20 l) ){. cnt
1e302 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 = 1;. pExpr
1e303 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a ->iColumn = -1;.
1e304 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 pExpr->aff
1e305 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1e306 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
1e307 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a }.. /*. **
1e308 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 If the input is
1e309 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20 28 of the form Z (
1e30a 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a not Y.Z or X.Y.Z
1e30b 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20 ) then the name
1e30c 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 72 Z. ** might r
1e30d 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75 6c efer to an resul
1e30e 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54 68 t-set alias. Th
1e30f 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20 is happens, for
1e310 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20 20 example, when.
1e311 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73 6f ** we are reso
1e312 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 74 lving names in t
1e313 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1e314 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
1e315 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a command:. **
1e316 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 . ** SELE
1e317 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f 4d CT a+b AS x FROM
1e318 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c 31 table WHERE x<1
1e319 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 0;. **. **
1e31a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20 74 In cases like t
1e31b 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45 78 his, replace pEx
1e31c 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f pr with a copy o
1e31d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1e31e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f 72 that. ** for
1e31f 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 ms the result se
1e320 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20 69 t entry ("a+b" i
1e321 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20 61 n the example) a
1e322 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 nd return immedi
1e323 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f ately.. ** No
1e324 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72 te that the expr
1e325 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65 ession in the re
1e326 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64 20 sult set should
1e327 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
1e328 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 n. ** resolve
1e329 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74 68 d by the time th
1e32a 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 e WHERE clause i
1e32b 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 s resolved..
1e32c 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d */. if( cnt==
1e32d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20 70 0 && (pEList = p
1e32e 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20 26 NC->pEList)!=0 &
1e32f 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 & zTab==0 ){.
1e330 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45 for(j=0; j<pE
1e331 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b List->nExpr; j++
1e332 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 ){. char
1e333 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 *zAs = pEList->a
1e334 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [j].zName;.
1e335 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 if( zAs!=0 &&
1e336 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1e337 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b zAs, zCol)==0 ){
1e338 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 . Expr
1e339 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 20 *pOrig;.
1e33a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1e33b 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 >pLeft==0 && pEx
1e33c 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b pr->pRight==0 );
1e33d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1e33e 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 t( pExpr->x.pLis
1e33f 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 t==0 );.
1e340 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1e341 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b >x.pSelect==0 );
1e342 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67 . pOrig
1e343 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e = pEList->a[j].
1e344 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 pExpr;.
1e345 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 if( !pNC->allow
1e346 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72 Agg && ExprHasPr
1e347 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 operty(pOrig, EP
1e348 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 _Agg) ){.
1e349 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1e34a 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
1e34b 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20 suse of aliased
1e34c 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a aggregate %s", z
1e34d 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 As);.
1e34e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1e34f 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b, zCol);.
1e350 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a return 2;.
1e351 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1e352 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 resolveAli
1e353 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 as(pParse, pELis
1e354 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 t, j, pExpr, "")
1e355 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 ;. cnt
1e356 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 = 1;. p
1e357 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 Match = 0;.
1e358 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 assert( zTa
1e359 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 b==0 && zDb==0 )
1e35a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
1e35b 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f lookupname_end_
1e35c 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
1e35d 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 } . }..
1e35e 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 /* Advance to t
1e35f 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e he next name con
1e360 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 text. The loop
1e361 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 will exit when e
1e362 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 ither. ** we
1e363 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e have a match (cn
1e364 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 t>0) or when we
1e365 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 run out of name
1e366 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f contexts.. */
1e367 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 . if( cnt==0
1e368 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 ){. pNC = p
1e369 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d NC->pNext;. }
1e36a 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
1e36b 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e If X and Y are N
1e36c 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f ULL (in other wo
1e36d 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 rds if only the
1e36e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 column name Z is
1e36f 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 . ** supplied)
1e370 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 and the value of
1e371 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 Z is enclosed i
1e372 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c n double-quotes,
1e373 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 then. ** Z is
1e374 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c a string literal
1e375 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d if it doesn't m
1e376 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 atch any column
1e377 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a names. In that.
1e378 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 ** case, we ne
1e379 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 ed to return rig
1e37a 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 ht away and not
1e37b 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 make any changes
1e37c 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a to. ** pExpr..
1e37d 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 **. ** Becaus
1e37e 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 e no reference w
1e37f 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 as made to outer
1e380 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 contexts, the p
1e381 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 NC->nRef. ** fi
1e382 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 elds are not cha
1e383 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 nged in any cont
1e384 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ext.. */. if(
1e385 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d cnt==0 && zTab==
1e386 30 20 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 0 && ExprHasProp
1e387 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 44 62 erty(pExpr,EP_Db
1e388 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 20 20 20 lQuoted) ){.
1e389 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1e38a 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 70 45 78 , zCol);. pEx
1e38b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 pr->op = TK_STRI
1e38c 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 NG;. pExpr->p
1e38d 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Tab = 0;. ret
1e38e 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
1e38f 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 . ** cnt==0 mea
1e390 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 ns there was not
1e391 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d match. cnt>1 m
1e392 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 eans there were
1e393 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 two or. ** more
1e394 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 matches. Eithe
1e395 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 r way, we have a
1e396 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 n error.. */.
1e397 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 if( cnt!=1 ){.
1e398 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 const char *zE
1e399 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 rr;. zErr = c
1e39a 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 nt==0 ? "no such
1e39b 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 column" : "ambi
1e39c 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d guous column nam
1e39d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 e";. if( zDb
1e39e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e39f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1e3a0 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c "%s: %s.%s.%s",
1e3a1 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 zErr, zDb, zTab
1e3a2 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c , zCol);. }el
1e3a3 73 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 se if( zTab ){.
1e3a4 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1e3a5 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 rMsg(pParse, "%s
1e3a6 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 : %s.%s", zErr,
1e3a7 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 zTab, zCol);.
1e3a8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
1e3a9 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1e3aa 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 arse, "%s: %s",
1e3ab 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 zErr, zCol);.
1e3ac 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e }. pTopNC->n
1e3ad 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Err++;. }.. /*
1e3ae 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f If a column fro
1e3af 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 m a table in pSr
1e3b0 63 4c 69 73 74 20 69 73 20 72 65 66 65 72 65 6e cList is referen
1e3b1 63 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 ced, then record
1e3b2 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 . ** this fact
1e3b3 69 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e in the pSrcList.
1e3b4 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d a[].colUsed bitm
1e3b5 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 ask. Column 0 c
1e3b6 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 auses. ** bit 0
1e3b7 20 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c to be set. Col
1e3b8 75 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 umn 1 sets bit 1
1e3b9 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e . And so forth.
1e3ba 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f If the. ** co
1e3bb 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 lumn number is g
1e3bc 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
1e3bd 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 number of bits i
1e3be 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 n the bitmask.
1e3bf 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 ** then set the
1e3c0 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f high-order bit o
1e3c1 66 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 f the bitmask..
1e3c2 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d */. if( pExpr-
1e3c3 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 >iColumn>=0 && p
1e3c4 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 Match!=0 ){.
1e3c5 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 int n = pExpr->i
1e3c6 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 Column;. test
1e3c7 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 case( n==BMS-1 )
1e3c8 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 ;. if( n>=BMS
1e3c9 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d ){. n = BM
1e3ca 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 S-1;. }. a
1e3cb 73 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 ssert( pMatch->i
1e3cc 43 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 Cursor==pExpr->i
1e3cd 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 Table );. pMa
1e3ce 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 tch->colUsed |=
1e3cf 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b ((Bitmask)1)<<n;
1e3d0 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 . }..lookupname
1e3d1 5f 65 6e 64 3a 0a 20 20 2f 2a 20 43 6c 65 61 6e _end:. /* Clean
1e3d2 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 up and return.
1e3d3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 */. sqlite3DbF
1e3d4 72 65 65 28 64 62 2c 20 7a 44 62 29 3b 0a 20 20 ree(db, zDb);.
1e3d5 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1e3d6 2c 20 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 , zTab);. sqlit
1e3d7 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1e3d8 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a pExpr->pLeft);.
1e3d9 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d pExpr->pLeft =
1e3da 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 0;. sqlite3Exp
1e3db 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 rDelete(db, pExp
1e3dc 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 45 r->pRight);. pE
1e3dd 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b xpr->pRight = 0;
1e3de 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 . pExpr->op = T
1e3df 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c 6f 6f 6b 75 70 K_COLUMN;.lookup
1e3e0 6e 61 6d 65 5f 65 6e 64 5f 32 3a 0a 20 20 73 71 name_end_2:. sq
1e3e1 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1e3e2 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 63 6e 74 zCol);. if( cnt
1e3e3 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==1 ){. asser
1e3e4 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 t( pNC!=0 );.
1e3e5 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 sqlite3AuthRead
1e3e6 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
1e3e7 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53 pSchema, pNC->pS
1e3e8 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20 rcList);. /*
1e3e9 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 Increment the nR
1e3ea 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 ef value on all
1e3eb 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66 72 name contexts fr
1e3ec 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 om TopNC up to.
1e3ed 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 ** the point
1e3ee 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20 6d where the name m
1e3ef 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 atched. */. f
1e3f0 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 or(;;){. as
1e3f1 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 sert( pTopNC!=0
1e3f2 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 2d );. pTopNC-
1e3f3 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 >nRef++;. i
1e3f4 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 f( pTopNC==pNC )
1e3f5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 break;. pT
1e3f6 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 opNC = pTopNC->p
1e3f7 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
1e3f8 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 65 6c return 0;. } el
1e3f9 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 se {. return
1e3fa 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 1;. }.}../*.**
1e3fb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1e3fc 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c callback for sql
1e3fd 69 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a ite3WalkExpr()..
1e3fe 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 **.** Resolve sy
1e3ff 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 mbolic names int
1e400 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 o TK_COLUMN oper
1e401 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 75 ators for the cu
1e402 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e rrent.** node in
1e403 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1e404 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30 20 tree. Return 0
1e405 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 to continue the
1e406 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 search down.** t
1e407 68 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f 20 he tree or 2 to
1e408 61 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 77 abort the tree w
1e409 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 alk..**.** This
1e40a 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 routine also doe
1e40b 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 s error checking
1e40c 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 and name resolu
1e40d 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 tion for.** func
1e40e 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 tion names. The
1e40f 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61 67 operator for ag
1e410 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
1e411 73 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 s is changed.**
1e412 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 to TK_AGG_FUNCTI
1e413 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ON..*/.static in
1e414 74 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 t resolveExprSte
1e415 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 p(Walker *pWalke
1e416 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b r, Expr *pExpr){
1e417 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
1e418 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 pNC;. Parse *pP
1e419 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 arse;.. pNC = p
1e41a 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 Walker->u.pNC;.
1e41b 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 assert( pNC!=0
1e41c 29 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e );. pParse = pN
1e41d 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73 73 C->pParse;. ass
1e41e 65 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57 61 ert( pParse==pWa
1e41f 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a lker->pParse );.
1e420 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e . if( ExprHasAn
1e421 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
1e422 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 EP_Resolved) )
1e423 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 return WRC_Prune
1e424 3b 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ;. ExprSetPrope
1e425 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 rty(pExpr, EP_Re
1e426 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 solved);.#ifndef
1e427 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 4e NDEBUG. if( pN
1e428 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 70 C->pSrcList && p
1e429 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 NC->pSrcList->nA
1e42a 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 lloc>0 ){. Sr
1e42b 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 cList *pSrcList
1e42c 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b = pNC->pSrcList;
1e42d 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1e42e 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e for(i=0; i<pNC->
1e42f 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 pSrcList->nSrc;
1e430 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 i++){. asse
1e431 72 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b rt( pSrcList->a[
1e432 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 i].iCursor>=0 &&
1e433 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e pSrcList->a[i].
1e434 69 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e iCursor<pParse->
1e435 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d nTab);. }. }
1e436 0a 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 68 .#endif. switch
1e437 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a ( pExpr->op ){..
1e438 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
1e439 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 TE_ENABLE_UPDATE
1e43a 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 _DELETE_LIMIT) &
1e43b 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
1e43c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 E_OMIT_SUBQUERY)
1e43d 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 . /* The spec
1e43e 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f ial operator TK_
1e43f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74 68 ROW means use th
1e440 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 e rowid for the
1e441 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c first. ** col
1e442 75 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 umn in the FROM
1e443 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69 73 clause. This is
1e444 20 75 73 65 64 20 62 79 20 74 68 65 20 4c 49 4d used by the LIM
1e445 49 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59 0a IT and ORDER BY.
1e446 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 ** clause pr
1e447 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 ocessing on UPDA
1e448 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 TE and DELETE st
1e449 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f atements.. */
1e44a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 . case TK_ROW
1e44b 3a 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69 73 : {. SrcLis
1e44c 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e t *pSrcList = pN
1e44d 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 C->pSrcList;.
1e44e 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 struct SrcLis
1e44f 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
1e450 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 assert( pSr
1e451 63 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69 73 cList && pSrcLis
1e452 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 t->nSrc==1 );.
1e453 20 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72 63 pItem = pSrc
1e454 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 List->a; .
1e455 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 pExpr->op = TK_C
1e456 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 OLUMN;. pEx
1e457 70 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d pr->pTab = pItem
1e458 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 45 ->pTab;. pE
1e459 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 xpr->iTable = pI
1e45a 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1e45b 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
1e45c 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 mn = -1;. p
1e45d 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d Expr->affinity =
1e45e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
1e45f 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b GER;. break
1e460 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
1e461 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 * defined(SQLITE
1e462 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 _ENABLE_UPDATE_D
1e463 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 ELETE_LIMIT) &&
1e464 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1e465 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a OMIT_SUBQUERY) *
1e466 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 /.. /* A lone
1e467 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 74 identifier is t
1e468 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c he name of a col
1e469 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 umn.. */.
1e46a 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 case TK_ID: {.
1e46b 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 lookupName(p
1e46c 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 26 70 45 Parse, 0, 0, &pE
1e46d 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 70 4e 43 2c xpr->token, pNC,
1e46e 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 72 pExpr);. r
1e46f 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b eturn WRC_Prune;
1e470 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
1e471 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e A table name an
1e472 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 d column name:
1e473 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 ID.ID. **
1e474 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 Or a database, t
1e475 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a able and column:
1e476 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a ID.ID.ID. *
1e477 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f /. case TK_DO
1e478 54 3a 20 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e T: {. Token
1e479 20 2a 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 *pColumn;.
1e47a 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 3b 0a Token *pTable;.
1e47b 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 44 62 Token *pDb
1e47c 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 ;. Expr *pR
1e47d 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ight;.. /*
1e47e 69 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 if( pSrcList==0
1e47f 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 ) break; */.
1e480 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 pRight = pExpr
1e481 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 ->pRight;.
1e482 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d if( pRight->op==
1e483 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20 TK_ID ){.
1e484 20 70 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pDb = 0;.
1e485 20 20 70 54 61 62 6c 65 20 3d 20 26 70 45 78 70 pTable = &pExp
1e486 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b r->pLeft->token;
1e487 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e . pColumn
1e488 20 3d 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 = &pRight->toke
1e489 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a n;. }else{.
1e48a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1e48b 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 pRight->op==TK_D
1e48c 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44 OT );. pD
1e48d 62 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c 65 66 b = &pExpr->pLef
1e48e 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 t->token;.
1e48f 20 20 70 54 61 62 6c 65 20 3d 20 26 70 52 69 67 pTable = &pRig
1e490 68 74 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e ht->pLeft->token
1e491 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d ;. pColum
1e492 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e 70 52 69 n = &pRight->pRi
1e493 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 ght->token;.
1e494 20 20 7d 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 }. lookup
1e495 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 44 62 Name(pParse, pDb
1e496 2c 20 70 54 61 62 6c 65 2c 20 70 43 6f 6c 75 6d , pTable, pColum
1e497 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a n, pNC, pExpr);.
1e498 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1e499 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 0a 20 _Prune;. }..
1e49a 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 /* Resolve fu
1e49b 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 nction names.
1e49c 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1e49d 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 CONST_FUNC:.
1e49e 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
1e49f 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 : {. ExprLi
1e4a0 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 st *pList = pExp
1e4a1 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f r->x.pList; /
1e4a2 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c * The argument l
1e4a3 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ist */. int
1e4a4 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 n = pList ? pLi
1e4a5 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 st->nExpr : 0;
1e4a6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
1e4a7 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 rguments */.
1e4a8 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 int no_such_fu
1e4a9 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a nc = 0; /*
1e4aa 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 True if no such
1e4ab 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 function exists
1e4ac 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 */. int wr
1e4ad 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 ong_num_args = 0
1e4ae 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
1e4af 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 wrong number of
1e4b0 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1e4b1 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d int is_agg =
1e4b2 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1e4b3 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e /* True if is an
1e4b4 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1e4b5 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ion */. int
1e4b6 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 auth;
1e4b7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 /* Auth
1e4b8 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 orization to use
1e4b9 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1e4ba 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 . int nId;
1e4bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4bc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e4bd 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 characters in fu
1e4be 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
1e4bf 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1e4c0 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 *zId;
1e4c1 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1e4c2 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 name. */.
1e4c3 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 FuncDef *pDef;
1e4c4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1e4c5 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1e4c6 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1e4c7 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 . u8 enc =
1e4c8 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b ENC(pParse->db);
1e4c9 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1e4ca 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a se encoding */..
1e4cb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
1e4cc 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1e4cd 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
1e4ce 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 ct) );. zId
1e4cf 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d = (char*)pExpr-
1e4d0 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 >token.z;.
1e4d1 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b nId = pExpr->tok
1e4d2 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66 en.n;. pDef
1e4d3 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
1e4d4 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 nction(pParse->d
1e4d5 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 b, zId, nId, n,
1e4d6 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 enc, 0);. i
1e4d7 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 f( pDef==0 ){.
1e4d8 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c pDef = sql
1e4d9 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e ite3FindFunction
1e4da 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 (pParse->db, zId
1e4db 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 , nId, -1, enc,
1e4dc 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
1e4dd 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pDef==0 ){.
1e4de 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e no_such_fun
1e4df 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
1e4e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1e4e1 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d wrong_num_args =
1e4e2 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
1e4e3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e4e4 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 65 66 is_agg = pDef
1e4e5 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 ->xFunc==0;.
1e4e6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1e4e7 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
1e4e8 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 28 20 ATION. if(
1e4e9 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 pDef ){.
1e4ea 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 auth = sqlite3Au
1e4eb 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
1e4ec 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c SQLITE_FUNCTION,
1e4ed 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 0, pDef->zName,
1e4ee 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
1e4ef 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b auth!=SQLITE_OK
1e4f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
1e4f1 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 ( auth==SQLITE_D
1e4f2 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ENY ){.
1e4f3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1e4f4 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 sg(pParse, "not
1e4f5 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 75 73 authorized to us
1e4f6 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c e function: %s",
1e4f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e4f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4f9 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 pDef->zName
1e4fa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 );. p
1e4fb 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 NC->nErr++;.
1e4fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e4fd 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
1e4fe 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 _NULL;.
1e4ff 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
1e500 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
1e501 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1e502 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 26 20 if( is_agg &&
1e503 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 !pNC->allowAgg )
1e504 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1e505 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e506 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 , "misuse of agg
1e507 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
1e508 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 %.*s()", nId,zId
1e509 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e );. pNC->
1e50a 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
1e50b 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 20 20 is_agg = 0;.
1e50c 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73 }else if( no_s
1e50d 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20 uch_func ){.
1e50e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1e50f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 Msg(pParse, "no
1e510 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25 such function: %
1e511 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b .*s", nId, zId);
1e512 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 . pNC->nE
1e513 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 rr++;. }els
1e514 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f e if( wrong_num_
1e515 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20 args ){.
1e516 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e517 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75 pParse,"wrong nu
1e518 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1e519 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e s to function %.
1e51a 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 20 20 *s()",.
1e51b 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 nId, zId);.
1e51c 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 pNC->nErr
1e51d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
1e51e 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 7b 0a if( is_agg ){.
1e51f 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f pExpr->o
1e520 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 p = TK_AGG_FUNCT
1e521 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 ION;. pNC
1e522 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 ->hasAgg = 1;.
1e523 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1e524 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c is_agg ) pNC->al
1e525 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 lowAgg = 0;.
1e526 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
1e527 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 rList(pWalker, p
1e528 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 List);. if(
1e529 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 is_agg ) pNC->a
1e52a 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 llowAgg = 1;.
1e52b 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43 /* FIX ME: C
1e52c 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66 ompute pExpr->af
1e52d 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20 finity based on
1e52e 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65 74 the expected ret
1e52f 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 urn. ** typ
1e530 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f e of the functio
1e531 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n . */.
1e532 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1e533 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ne;. }.#ifnde
1e534 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 f SQLITE_OMIT_SU
1e535 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 BQUERY. case
1e536 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 TK_SELECT:. c
1e537 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 23 ase TK_EXISTS:.#
1e538 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 endif. case T
1e539 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 K_IN: {. if
1e53a 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1e53b 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
1e53c 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
1e53d 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70 4e 43 int nRef = pNC
1e53e 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65 66 20 ->nRef;.#ifndef
1e53f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 SQLITE_OMIT_CHEC
1e540 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e K. if( pN
1e541 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 C->isCheck ){.
1e542 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1e543 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 rrorMsg(pParse,"
1e544 73 75 62 71 75 65 72 69 65 73 20 70 72 6f 68 69 subqueries prohi
1e545 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 63 bited in CHECK c
1e546 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 onstraints");.
1e547 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
1e548 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 sqlite3Wa
1e549 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 lkSelect(pWalker
1e54a 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 , pExpr->x.pSele
1e54b 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ct);. ass
1e54c 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d ert( pNC->nRef>=
1e54d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 20 20 nRef );.
1e54e 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e if( nRef!=pNC->n
1e54f 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Ref ){.
1e550 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1e551 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65 (pExpr, EP_VarSe
1e552 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d lect);. }
1e553 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1e554 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e reak;. }.#ifn
1e555 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1e556 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 20 54 CHECK. case T
1e557 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 K_VARIABLE: {.
1e558 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 if( pNC->isC
1e559 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 heck ){.
1e55a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e55b 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 74 65 pParse,"paramete
1e55c 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e rs prohibited in
1e55d 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e CHECK constrain
1e55e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ts");. }.
1e55f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1e560 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 .#endif. }. re
1e561 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 turn (pParse->nE
1e562 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 rr || pParse->db
1e563 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 ->mallocFailed)
1e564 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 ? WRC_Abort : WR
1e565 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f C_Continue;.}../
1e566 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 61 *.** pEList is a
1e567 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
1e568 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65 20 72 ions which are r
1e569 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75 6c 74 eally the result
1e56a 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 set of the.** a
1e56b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1e56c 74 2e 20 20 70 45 20 69 73 20 61 20 74 65 72 6d t. pE is a term
1e56d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
1e56e 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
1e56f 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 se..** This rout
1e570 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 73 65 ine checks to se
1e571 65 20 69 66 20 70 45 20 69 73 20 61 20 73 69 6d e if pE is a sim
1e572 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20 77 ple identifier w
1e573 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 hich corresponds
1e574 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 .** to the AS-na
1e575 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 me of one of the
1e576 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 65 78 terms of the ex
1e577 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 pression list.
1e578 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69 If it is,.** thi
1e579 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
1e57a 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
1e57b 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68 65 72 een 1 and N wher
1e57c 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
1e57d 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 r of.** elements
1e57e 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f 72 72 in pEList, corr
1e57f 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
1e580 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e matching entry.
1e581 20 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a If there is.**
1e582 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 no match, or if
1e583 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73 69 6d pE is not a sim
1e584 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 20 ple identifier,
1e585 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1e586 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a e.** return 0..*
1e587 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61 73 20 *.** pEList has
1e588 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 been resolved.
1e589 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 pE has not..*/.s
1e58a 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
1e58b 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 eAsName(. Parse
1e58c 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a *pParse, /*
1e58d 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1e58e 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 for error messa
1e58f 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ges */. ExprLis
1e590 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c t *pEList, /* L
1e591 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
1e592 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 ns to scan */.
1e593 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 Expr *pE
1e594 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
1e595 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
1e596 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 o match */.){.
1e597 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1e598 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1e599 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 70 45 2d er */.. if( pE-
1e59a 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c 20 28 70 >op==TK_ID || (p
1e59b 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 E->op==TK_STRING
1e59c 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 5b && pE->token.z[
1e59d 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a 20 20 20 0]!='\'') ){.
1e59e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1e59f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 63 Parse->db;. c
1e5a0 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69 har *zCol = sqli
1e5a1 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
1e5a2 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b 65 6e 29 (db, &pE->token)
1e5a3 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c 3d 3d ;. if( zCol==
1e5a4 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
1e5a5 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n -1;. }.
1e5a6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 for(i=0; i<pELis
1e5a7 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
1e5a8 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 char *zAs
1e5a9 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a = pEList->a[i].z
1e5aa 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Name;. if(
1e5ab 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 zAs!=0 && sqlite
1e5ac 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 3StrICmp(zAs, zC
1e5ad 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1e5ae 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1e5af 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 db, zCol);.
1e5b0 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 return i+1;.
1e5b1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1e5b2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1e5b3 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 b, zCol);. }.
1e5b4 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
1e5b5 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69 6e 74 ** pE is a point
1e5b6 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 er to an express
1e5b7 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61 20 73 ion which is a s
1e5b8 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 ingle term in th
1e5b9 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 6f 66 e.** ORDER BY of
1e5ba 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
1e5bb 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 CT. The express
1e5bc 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ion has not been
1e5bd 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65 .** name resolve
1e5be 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 d..**.** At the
1e5bf 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 74 69 point this routi
1e5c0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 ne is called, we
1e5c1 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 already know th
1e5c2 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 at the.** ORDER
1e5c3 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 BY term is not a
1e5c4 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65 78 20 n integer index
1e5c5 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 into the result
1e5c6 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61 set. That.** ca
1e5c7 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 se is handled by
1e5c8 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 the calling rou
1e5c9 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65 tine..**.** Atte
1e5ca 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70 45 20 mpt to match pE
1e5cb 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 against result s
1e5cc 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 et columns in th
1e5cd 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53 e left-most.** S
1e5ce 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
1e5cf 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 Return the ind
1e5d0 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 74 63 ex i of the matc
1e5d1 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 hing column,.**
1e5d2 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e as an indication
1e5d3 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 74 to the caller t
1e5d4 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 73 6f hat it should so
1e5d5 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 20 63 rt by the i-th c
1e5d6 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65 olumn..** The le
1e5d7 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 ft-most column i
1e5d8 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 s 1. In other w
1e5d9 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75 65 20 ords, the value
1e5da 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 0a returned is the.
1e5db 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20 ** same integer
1e5dc 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 value that would
1e5dd 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 65 20 be used in the
1e5de 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f SQL statement to
1e5df 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 65 indicate.** the
1e5e0 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 column..**.** I
1e5e1 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
1e5e2 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 tch, return 0.
1e5e3 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61 6e 20 Return -1 if an
1e5e4 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f error occurs..*/
1e5e5 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
1e5e6 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f lveOrderByTermTo
1e5e7 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 ExprList(. Pars
1e5e8 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
1e5e9 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1e5ea 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 t for error mess
1e5eb 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 ages */. Select
1e5ec 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 *pSelect, /*
1e5ed 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 The SELECT state
1e5ee 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20 4f 52 ment with the OR
1e5ef 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
1e5f0 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 . Expr *pE
1e5f1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 /* The spe
1e5f2 63 69 66 69 63 20 4f 52 44 45 52 20 42 59 20 74 cific ORDER BY t
1e5f3 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 erm */.){. int
1e5f4 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f i; /
1e5f5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1e5f6 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
1e5f7 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f List; /* The co
1e5f8 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 65 73 lumns of the res
1e5f9 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d ult set */. Nam
1e5fa 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 20 20 eContext nc;
1e5fb 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 /* Name context
1e5fc 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 70 45 for resolving pE
1e5fd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
1e5fe 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
1e5ff 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 ger(pE, &i)==0 )
1e600 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 ;. pEList = pSe
1e601 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 lect->pEList;..
1e602 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 /* Resolve all
1e603 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f 52 44 names in the ORD
1e604 45 52 20 42 59 20 74 65 72 6d 20 65 78 70 72 65 ER BY term expre
1e605 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d ssion. */. mem
1e606 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65 set(&nc, 0, size
1e607 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50 of(nc));. nc.pP
1e608 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 arse = pParse;.
1e609 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 nc.pSrcList = p
1e60a 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 Select->pSrc;.
1e60b 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 nc.pEList = pELi
1e60c 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67 st;. nc.allowAg
1e60d 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72 g = 1;. nc.nErr
1e60e 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
1e60f 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1e610 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a mes(&nc, pE) ){.
1e611 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1e612 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 Clear(pParse);.
1e613 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1e614 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61 .. /* Try to ma
1e615 74 63 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 tch the ORDER BY
1e616 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 61 69 expression agai
1e617 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f nst an expressio
1e618 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 n. ** in the re
1e619 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74 75 72 sult set. Retur
1e61a 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69 6e 64 n an 1-based ind
1e61b 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 ex of the matchi
1e61c 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 ng. ** result-s
1e61d 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20 et entry.. */.
1e61e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 for(i=0; i<pELi
1e61f 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
1e620 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1e621 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 4c 69 ExprCompare(pELi
1e622 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 st->a[i].pExpr,
1e623 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 pE) ){. ret
1e624 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20 urn i+1;. }.
1e625 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d }.. /* If no m
1e626 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 atch, return 0.
1e627 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
1e628 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1e629 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 an ORDER BY or
1e62a 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20 6f 75 GROUP BY term ou
1e62b 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72 6f 72 t-of-range error
1e62c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1e62d 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e resolveOutOfRan
1e62e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65 geError(. Parse
1e62f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1e630 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 63 /* The error c
1e631 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68 69 63 ontext into whic
1e632 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 h to write the e
1e633 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rror */. const
1e634 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 20 20 char *zType,
1e635 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 /* "ORDER" or "
1e636 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20 GROUP" */. int
1e637 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i,
1e638 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 /* The index
1e639 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74 68 65 (1-based) of the
1e63a 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e term out of ran
1e63b 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20 ge */. int mx
1e63c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e63d 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d 69 73 * Largest permis
1e63e 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66 20 69 sible value of i
1e63f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
1e640 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1e641 20 0a 20 20 20 20 22 25 72 20 25 73 20 42 59 20 . "%r %s BY
1e642 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 term out of rang
1e643 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 22 0a e - should be ".
1e644 20 20 20 20 22 62 65 74 77 65 65 6e 20 31 20 61 "between 1 a
1e645 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79 70 65 nd %d", i, zType
1e646 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 , mx);.}../*.**
1e647 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 44 45 Analyze the ORDE
1e648 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 61 R BY clause in a
1e649 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1e64a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f statement. Mo
1e64b 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74 65 72 dify.** each ter
1e64c 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 m of the ORDER B
1e64d 59 20 63 6c 61 75 73 65 20 69 73 20 61 20 63 6f Y clause is a co
1e64e 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 62 nstant integer b
1e64f 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20 etween 1.** and
1e650 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 N where N is the
1e651 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1e652 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 ns in the compou
1e653 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a nd SELECT..**.**
1e654 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 ORDER BY terms
1e655 74 68 61 74 20 61 72 65 20 61 6c 72 65 61 64 79 that are already
1e656 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
1e657 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72 65 0a een 1 and N are.
1e658 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20 ** unmodified.
1e659 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 ORDER BY terms t
1e65a 68 61 74 20 61 72 65 20 69 6e 74 65 67 65 72 73 hat are integers
1e65b 20 6f 75 74 73 69 64 65 20 74 68 65 20 72 61 6e outside the ran
1e65c 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75 ge of.** 1 throu
1e65d 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20 61 6e gh N generate an
1e65e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52 20 42 error. ORDER B
1e65f 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 Y terms that are
1e660 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 expressions.**
1e661 61 72 65 20 6d 61 74 63 68 65 64 20 61 67 61 69 are matched agai
1e662 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 65 nst result set e
1e663 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f xpressions of co
1e664 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a mpound SELECT.**
1e665 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
1e666 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45 the left-most SE
1e667 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67 LECT and working
1e668 20 74 6f 77 61 72 64 20 74 68 65 20 72 69 67 68 toward the righ
1e669 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66 69 72 t..** At the fir
1e66a 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20 4f 52 st match, the OR
1e66b 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f DER BY expressio
1e66c 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 n is transformed
1e66d 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74 into.** the int
1e66e 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 eger column numb
1e66f 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e er..**.** Return
1e670 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1e671 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73 rrors seen..*/.s
1e672 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
1e673 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 eCompoundOrderBy
1e674 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1e675 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
1e676 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c sing context. L
1e677 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 eave error messa
1e678 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ges here */. Se
1e679 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 lect *pSelect
1e67a 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1e67b 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 T statement cont
1e67c 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 45 52 aining the ORDER
1e67d 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 BY */.){. int
1e67e 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 i;. ExprList *p
1e67f 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70 72 4c OrderBy;. ExprL
1e680 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 ist *pEList;. s
1e681 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e qlite3 *db;. in
1e682 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a t moreToDo = 1;.
1e683 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 53 . pOrderBy = pS
1e684 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b elect->pOrderBy;
1e685 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d . if( pOrderBy=
1e686 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1e687 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1e688 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
1e689 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f _COLUMN. if( pO
1e68a 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 rderBy->nExpr>db
1e68b 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1e68c 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b LIMIT_COLUMN] ){
1e68d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1e68e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
1e68f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 o many terms in
1e690 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 22 ORDER BY clause"
1e691 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
1e692 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f . }.#endif. fo
1e693 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 r(i=0; i<pOrderB
1e694 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a y->nExpr; i++){.
1e695 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b pOrderBy->a[
1e696 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d i].done = 0;. }
1e697 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 . pSelect->pNex
1e698 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 t = 0;. while(
1e699 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 pSelect->pPrior
1e69a 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d 3e ){. pSelect->
1e69b 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20 pPrior->pNext =
1e69c 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 53 65 pSelect;. pSe
1e69d 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e lect = pSelect->
1e69e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68 pPrior;. }. wh
1e69f 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 26 20 ile( pSelect &&
1e6a0 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20 moreToDo ){.
1e6a1 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1e6a2 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 item *pItem;.
1e6a3 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20 moreToDo = 0;.
1e6a4 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c pEList = pSel
1e6a5 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 ect->pEList;.
1e6a6 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 assert( pEList!
1e6a7 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d =0 );. for(i=
1e6a8 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 0, pItem=pOrderB
1e6a9 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 y->a; i<pOrderBy
1e6aa 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
1e6ab 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e tem++){. in
1e6ac 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 t iCol = -1;.
1e6ad 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a 70 44 Expr *pE, *pD
1e6ae 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 up;. if( pI
1e6af 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74 tem->done ) cont
1e6b0 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d inue;. pE =
1e6b1 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 pItem->pExpr;.
1e6b2 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1e6b3 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 ExprIsInteger(pE
1e6b4 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 , &iCol) ){.
1e6b5 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 7c if( iCol<0 |
1e6b6 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e | iCol>pEList->n
1e6b7 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
1e6b8 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 resolveOutOfRa
1e6b9 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c ngeError(pParse,
1e6ba 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70 "ORDER", i+1, p
1e6bb 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 EList->nExpr);.
1e6bc 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1e6bd 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 1;. }.
1e6be 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e6bf 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 iCol = resolve
1e6c0 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 AsName(pParse, p
1e6c1 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 EList, pE);.
1e6c2 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20 if( iCol==0
1e6c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 ){. pDu
1e6c4 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1e6c5 75 70 28 64 62 2c 20 70 45 2c 20 30 29 3b 0a 20 up(db, pE, 0);.
1e6c6 20 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62 if( !db
1e6c7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1e6c8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 {. as
1e6c9 73 65 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20 sert(pDup);.
1e6ca 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 iCol = r
1e6cb 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 esolveOrderByTer
1e6cc 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72 mToExprList(pPar
1e6cd 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75 se, pSelect, pDu
1e6ce 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a p);. }.
1e6cf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1e6d0 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
1e6d1 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d pDup);. }
1e6d2 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1e6d3 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 l<0 ){.
1e6d4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
1e6d5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1e6d6 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
1e6d7 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
1e6d8 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 *pColl = pE->pC
1e6d9 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 oll;. int
1e6da 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 flags = pE->fla
1e6db 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
1e6dc 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 te;. sqli
1e6dd 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1e6de 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 , pE);. p
1e6df 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 Item->pExpr = pE
1e6e0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 = sqlite3Expr(d
1e6e1 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 b, TK_INTEGER, 0
1e6e2 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 , 0, 0);.
1e6e3 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74 if( pE==0 ) ret
1e6e4 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70 urn 1;. p
1e6e5 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c E->pColl = pColl
1e6e6 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c ;. pE->fl
1e6e7 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c ags |= EP_IntVal
1e6e8 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 ue | flags;.
1e6e9 20 20 20 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d pE->iTable =
1e6ea 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 iCol;. p
1e6eb 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 Item->iCol = (u1
1e6ec 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 6)iCol;.
1e6ed 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b pItem->done = 1;
1e6ee 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1e6ef 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d moreToDo =
1e6f0 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1e6f1 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 }. pSelect =
1e6f2 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a pSelect->pNext;.
1e6f3 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1e6f4 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
1e6f5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
1e6f6 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 pOrderBy->a[i].d
1e6f7 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 one==0 ){.
1e6f8 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e6f9 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45 pParse, "%r ORDE
1e6fa 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e R BY term does n
1e6fb 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20 ot match any ".
1e6fc 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 "colu
1e6fd 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 mn in the result
1e6fe 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20 set", i+1);.
1e6ff 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1e700 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1e701 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 0;.}../*.** Chec
1e702 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20 k every term in
1e703 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 the ORDER BY or
1e704 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1e705 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74 pOrderBy of.** t
1e706 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1e707 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66 ent pSelect. If
1e708 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66 any term is ref
1e709 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72 erence to a.** r
1e70a 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
1e70b 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69 sion (as determi
1e70c 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c ned by the ExprL
1e70d 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64 ist.a.iCol field
1e70e 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72 ).** then conver
1e70f 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f t that term into
1e710 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 a copy of the c
1e711 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 orresponding res
1e712 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d ult set.** colum
1e713 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 n..**.** If any
1e714 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63 errors are detec
1e715 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f ted, add an erro
1e716 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 r message to pPa
1e717 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 rse and.** retur
1e718 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 n non-zero. Ret
1e719 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65 urn zero if no e
1e71a 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a rrors are seen..
1e71b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e71c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
1e71d 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
1e71e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1e71f 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
1e720 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c sing context. L
1e721 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 eave error messa
1e722 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ges here */. Se
1e723 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
1e724 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1e725 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 T statement cont
1e726 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73 aining the claus
1e727 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 e */. ExprList
1e728 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 *pOrderBy, /*
1e729 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 The ORDER BY or
1e72a 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1e72b 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20 to be processed
1e72c 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1e72d 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f *zType /* "O
1e72e 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 RDER" or "GROUP"
1e72f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
1e730 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1e731 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78 pParse->db;. Ex
1e732 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
1e733 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1e734 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
1e735 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d if( pOrderBy==
1e736 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 0 || pParse->db-
1e737 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
1e738 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 return 0;.#if SQ
1e739 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a LITE_MAX_COLUMN.
1e73a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e if( pOrderBy->
1e73b 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 nExpr>db->aLimit
1e73c 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
1e73d 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c LUMN] ){. sql
1e73e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1e73f 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 rse, "too many t
1e740 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c erms in %s BY cl
1e741 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 ause", zType);.
1e742 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
1e743 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 .#endif. pEList
1e744 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 = pSelect->pELi
1e745 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 st;. assert( pE
1e746 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73 List!=0 ); /* s
1e747 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 qlite3SelectNew(
1e748 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 ) guarantees thi
1e749 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 s */. for(i=0,
1e74a 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e pItem=pOrderBy->
1e74b 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e a; i<pOrderBy->n
1e74c 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1e74d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 ++){. if( pIt
1e74e 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20 em->iCol ){.
1e74f 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f if( pItem->iCo
1e750 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l>pEList->nExpr
1e751 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c ){. resol
1e752 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
1e753 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c r(pParse, zType,
1e754 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 i+1, pEList->nE
1e755 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 xpr);. re
1e756 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a turn 1;. }.
1e757 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 resolveAli
1e758 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 as(pParse, pELis
1e759 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 t, pItem->iCol-1
1e75a 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 , pItem->pExpr,
1e75b 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 zType);. }.
1e75c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1e75d 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 ./*.** pOrderBy
1e75e 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f is an ORDER BY o
1e75f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
1e760 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 e in SELECT stat
1e761 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a ement pSelect..*
1e762 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65 * The Name conte
1e763 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 xt of the SELECT
1e764 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e statement is pN
1e765 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74 C. zType is eit
1e766 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f her.** "ORDER" o
1e767 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64 r "GROUP" depend
1e768 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70 ing on which typ
1e769 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64 e of clause pOrd
1e76a 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 erBy is..**.** T
1e76b 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f his routine reso
1e76c 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f lves each term o
1e76d 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74 f the clause int
1e76e 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e o an expression.
1e76f 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72 .** If the order
1e770 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 -by term is an i
1e771 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e nteger I between
1e772 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20 1 and N (where
1e773 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 N is the.** numb
1e774 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
1e775 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1e776 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74 of the SELECT) t
1e777 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
1e778 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 on.** in the res
1e779 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 olution is a cop
1e77a 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65 y of the I-th re
1e77b 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1e77c 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 ion. If.** the
1e77d 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 order-by term is
1e77e 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61 an identify tha
1e77f 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f t corresponds to
1e780 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a the AS-name of.
1e781 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20 ** a result-set
1e782 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e expression, then
1e783 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76 the term resolv
1e784 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 es to a copy of
1e785 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 the.** result-se
1e786 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f t expression. O
1e787 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78 therwise, the ex
1e788 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f pression is reso
1e789 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 lved in.** the u
1e78a 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67 sual way - using
1e78b 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1e78c 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a xprNames()..**.*
1e78d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
1e78e 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
1e78f 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 r of errors. If
1e790 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
1e791 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 hen.** an approp
1e792 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 riate error mess
1e793 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66 age might be lef
1e794 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f t in pParse. (O
1e795 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63 OM errors.** exc
1e796 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 epted.).*/.stati
1e797 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 c int resolveOrd
1e798 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d erGroupBy(. Nam
1e799 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 eContext *pNC,
1e79a 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 /* The name c
1e79b 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 ontext of the SE
1e79c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a LECT statement *
1e79d 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
1e79e 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ect, /* The
1e79f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1e7a0 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72 t holding pOrder
1e7a1 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 By */. ExprList
1e7a2 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a *pOrderBy, /*
1e7a3 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 An ORDER BY or
1e7a4 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1e7a5 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 to resolve */.
1e7a6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
1e7a7 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 e /* Either
1e7a8 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 "ORDER" or "GROU
1e7a9 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 P", as appropria
1e7aa 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 te */.){. int i
1e7ab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1e7ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1e7ad 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
1e7ae 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt iCol;
1e7af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e7b0 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a Column number *
1e7b1 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c /. struct ExprL
1e7b2 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1e7b3 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 /* A term of
1e7b4 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
1e7b5 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a use */. Parse *
1e7b6 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
1e7b7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
1e7b8 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1e7b9 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20 int nResult;
1e7ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e7bb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
1e7bc 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
1e7bd 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f set */.. if( pO
1e7be 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 rderBy==0 ) retu
1e7bf 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20 rn 0;. nResult
1e7c0 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 = pSelect->pELis
1e7c1 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72 t->nExpr;. pPar
1e7c2 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1e7c3 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 ;. for(i=0, pIt
1e7c4 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
1e7c5 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
1e7c6 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
1e7c7 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d {. Expr *pE =
1e7c8 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 pItem->pExpr;.
1e7c9 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 iCol = resolv
1e7ca 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 eAsName(pParse,
1e7cb 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c pSelect->pEList,
1e7cc 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43 pE);. if( iC
1e7cd 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 ol<0 ){. re
1e7ce 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f 4d 20 turn 1; /* OOM
1e7cf 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d 0a 20 error */. }.
1e7d0 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
1e7d1 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 . /* If an
1e7d2 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 69 73 AS-name match is
1e7d3 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69 found, mark thi
1e7d4 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 75 6d s ORDER BY colum
1e7d5 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 20 20 n as being.
1e7d6 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ** a copy of th
1e7d7 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 e iCol-th result
1e7d8 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 -set column. Th
1e7d9 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c e subsequent cal
1e7da 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71 l to. ** sq
1e7db 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
1e7dc 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 rGroupBy() will
1e7dd 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 convert the expr
1e7de 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20 ession to a.
1e7df 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 ** copy of the
1e7e0 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d iCol-th result-
1e7e1 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 set expression.
1e7e2 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e */. pItem->
1e7e3 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c iCol = (u16)iCol
1e7e4 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 ;. continue
1e7e5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1e7e6 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
1e7e7 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 eger(pE, &iCol)
1e7e8 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
1e7e9 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 ORDER BY term is
1e7ea 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 an integer cons
1e7eb 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 tant. Again, se
1e7ec 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 t the column.
1e7ed 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 ** number so
1e7ee 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 73 6f that sqlite3Reso
1e7ef 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
1e7f0 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 ) will convert t
1e7f1 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 he. ** orde
1e7f2 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 20 63 r-by term to a c
1e7f3 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c opy of the resul
1e7f4 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
1e7f5 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43 */. if( iC
1e7f6 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 20 20 ol<1 ){.
1e7f7 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 resolveOutOfRang
1e7f8 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a eError(pParse, z
1e7f9 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 Type, i+1, nResu
1e7fa 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 lt);. ret
1e7fb 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 1;. }.
1e7fc 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c pItem->iCol
1e7fd 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 = (u16)iCol;.
1e7fe 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 continue;.
1e7ff 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 }.. /* Othe
1e800 72 77 69 73 65 2c 20 74 72 65 61 74 20 74 68 65 rwise, treat the
1e801 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 61 ORDER BY term a
1e802 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 s an ordinary ex
1e803 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
1e804 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b pItem->iCol = 0;
1e805 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1e806 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1e807 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 (pNC, pE) ){.
1e808 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1e809 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1e80a 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
1e80b 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 derGroupBy(pPars
1e80c 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 e, pSelect, pOrd
1e80d 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a erBy, zType);.}.
1e80e 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e ./*.** Resolve n
1e80f 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 ames in the SELE
1e810 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61 CT statement p a
1e811 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 nd all of its de
1e812 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 scendents..*/.st
1e813 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 atic int resolve
1e814 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65 SelectStep(Walke
1e815 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1e816 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f ct *p){. NameCo
1e817 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b ntext *pOuterNC;
1e818 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 /* Context tha
1e819 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 t contains this
1e81a 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 SELECT */. Name
1e81b 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 Context sNC;
1e81c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 /* Name cont
1e81d 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45 ext of this SELE
1e81e 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f CT */. int isCo
1e81f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 mpound;
1e820 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73 20 /* True if p is
1e821 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 a compound selec
1e822 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 t */. int nComp
1e823 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f ound; /
1e824 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 * Number of comp
1e825 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65 ound terms proce
1e826 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ssed so far */.
1e827 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
1e828 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
1e829 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
1e82a 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
1e82b 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 t; /* Resu
1e82c 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f lt set expressio
1e82d 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 n list */. int
1e82e 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1e82f 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1e830 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ter */. ExprLis
1e831 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 t *pGroupBy;
1e832 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 /* The GROUP BY
1e833 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c clause */. Sel
1e834 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 ect *pLeftmost;
1e835 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 /* Left-mos
1e836 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61 t of SELECT of a
1e837 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 compound */. s
1e838 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
1e839 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
1e83a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1e83b 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70 . .. assert( p
1e83c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e !=0 );. if( p->
1e83d 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 selFlags & SF_Re
1e83e 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65 solved ){. re
1e83f 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
1e840 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d }. pOuterNC =
1e841 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b pWalker->u.pNC;
1e842 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c . pParse = pWal
1e843 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 ker->pParse;. d
1e844 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1e845 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 . /* Normally s
1e846 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
1e847 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c nd() will be cal
1e848 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 77 69 led first and wi
1e849 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 ll have. ** alr
1e84a 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 74 68 eady expanded th
1e84b 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 is SELECT. Howe
1e84c 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 ver, if this is
1e84d 61 20 73 75 62 71 75 65 72 79 20 77 69 74 68 69 a subquery withi
1e84e 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 n. ** an expres
1e84f 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 sion, sqlite3Res
1e850 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 olveExprNames()
1e851 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 will be called w
1e852 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 ithout a. ** pr
1e853 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ior call to sqli
1e854 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
1e855 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 ). When that ha
1e856 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 ppens, let. **
1e857 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 sqlite3SelectPre
1e858 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 p() do all of th
1e859 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 e processing for
1e85a 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 this SELECT..
1e85b 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
1e85c 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f Prep() will invo
1e85d 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53 ke both sqlite3S
1e85e 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e electExpand() an
1e85f 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 d. ** this rout
1e860 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65 ine in the corre
1e861 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 ct order.. */.
1e862 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 if( (p->selFlag
1e863 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 s & SF_Expanded)
1e864 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1e865 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 e3SelectPrep(pPa
1e866 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 rse, p, pOuterNC
1e867 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 );. return (p
1e868 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 Parse->nErr || d
1e869 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 b->mallocFailed)
1e86a 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 ? WRC_Abort : W
1e86b 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 RC_Prune;. }..
1e86c 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d isCompound = p-
1e86d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 >pPrior!=0;. nC
1e86e 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 ompound = 0;. p
1e86f 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 Leftmost = p;.
1e870 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 while( p ){.
1e871 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 assert( (p->selF
1e872 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 lags & SF_Expand
1e873 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 ed)!=0 );. as
1e874 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 sert( (p->selFla
1e875 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 gs & SF_Resolved
1e876 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 )==0 );. p->s
1e877 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 elFlags |= SF_Re
1e878 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 solved;.. /*
1e879 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 Resolve the expr
1e87a 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c essions in the L
1e87b 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 IMIT and OFFSET
1e87c 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20 clauses. These.
1e87d 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c ** are not al
1e87e 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 lowed to refer t
1e87f 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 o any names, so
1e880 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 pass an empty Na
1e881 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a meContext.. *
1e882 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e /. memset(&sN
1e883 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 C, 0, sizeof(sNC
1e884 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 ));. sNC.pPar
1e885 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 se = pParse;.
1e886 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f if( sqlite3Reso
1e887 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e lveExprNames(&sN
1e888 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c C, p->pLimit) ||
1e889 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1e88a 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1e88b 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 (&sNC, p->pOffse
1e88c 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 t) ){. retu
1e88d 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1e88e 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 }. . /* Se
1e88f 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e t up the local n
1e890 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 ame-context to p
1e891 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65 ass to sqlite3Re
1e892 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 solveExprNames()
1e893 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c to. ** resol
1e894 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 ve the result-se
1e895 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 t expression lis
1e896 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e t.. */. sN
1e897 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a C.allowAgg = 1;.
1e898 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 sNC.pSrcList
1e899 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 = p->pSrc;.
1e89a 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 sNC.pNext = pOut
1e89b 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 erNC;. . /*
1e89c 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e Resolve names in
1e89d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e the result set.
1e89e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d */. pEList =
1e89f 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 p->pEList;.
1e8a0 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d assert( pEList!=
1e8a1 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 0 );. for(i=0
1e8a2 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 ; i<pEList->nExp
1e8a3 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 r; i++){. E
1e8a4 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 xpr *pX = pEList
1e8a5 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[i].pExpr;.
1e8a6 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 if( sqlite3R
1e8a7 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
1e8a8 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 &sNC, pX) ){.
1e8a9 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1e8aa 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 Abort;. }.
1e8ab 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 }. . /* R
1e8ac 65 63 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c ecursively resol
1e8ad 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 ve names in all
1e8ae 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a subqueries. *
1e8af 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
1e8b0 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 <p->pSrc->nSrc;
1e8b1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 i++){. stru
1e8b2 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1e8b3 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 *pItem = &p->pSr
1e8b4 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 c->a[i];. i
1e8b5 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 f( pItem->pSelec
1e8b6 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e t ){. con
1e8b7 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43 st char *zSavedC
1e8b8 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d ontext = pParse-
1e8b9 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 >zAuthContext;.
1e8ba 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
1e8bb 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65 ->zName ) pParse
1e8bc 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d ->zAuthContext =
1e8bd 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 pItem->zName;.
1e8be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1e8bf 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 solveSelectNames
1e8c0 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e (pParse, pItem->
1e8c1 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e pSelect, pOuterN
1e8c2 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 C);. pPar
1e8c3 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
1e8c4 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 = zSavedContext
1e8c5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 ;. if( pP
1e8c6 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 arse->nErr || db
1e8c7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1e8c8 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1e8c9 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d t;. }. }
1e8ca 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . . /* If th
1e8cb 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67 72 65 ere are no aggre
1e8cc 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 gate functions i
1e8cd 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 n the result-set
1e8ce 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42 , and no GROUP B
1e8cf 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 Y . ** expres
1e8d0 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c sion, do not all
1e8d1 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 69 6e ow aggregates in
1e8d2 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 any of the othe
1e8d3 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 r expressions..
1e8d4 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1e8d5 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 ( (p->selFlags &
1e8d6 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 3d 3d SF_Aggregate)==
1e8d7 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42 0 );. pGroupB
1e8d8 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b y = p->pGroupBy;
1e8d9 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 . if( pGroupB
1e8da 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67 67 20 y || sNC.hasAgg
1e8db 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 ){. p->selF
1e8dc 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 lags |= SF_Aggre
1e8dd 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b gate;. }else{
1e8de 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 . sNC.allow
1e8df 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Agg = 0;. }.
1e8e0 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 48 41 . /* If a HA
1e8e1 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20 70 VING clause is p
1e8e2 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 resent, then the
1e8e3 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52 4f re must be a GRO
1e8e4 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 UP BY clause..
1e8e5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
1e8e6 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 72 6f pHaving && !pGro
1e8e7 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 71 upBy ){. sq
1e8e8 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1e8e9 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20 42 arse, "a GROUP B
1e8ea 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71 75 Y clause is requ
1e8eb 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56 49 ired before HAVI
1e8ec 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 NG");. retu
1e8ed 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1e8ee 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64 }. . /* Ad
1e8ef 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e d the expression
1e8f0 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61 6d list to the nam
1e8f1 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 65 e-context before
1e8f2 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20 20 parsing the.
1e8f3 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 65 73 ** other expres
1e8f4 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c sions in the SEL
1e8f5 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 ECT statement. T
1e8f6 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a 20 his is so that.
1e8f7 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
1e8f8 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 s in the WHERE c
1e8f9 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61 6e lause (etc.) can
1e8fa 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65 73 refer to expres
1e8fb 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20 sions by. **
1e8fc 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20 72 aliases in the r
1e8fd 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a esult set.. *
1e8fe 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70 *. ** Minor p
1e8ff 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69 73 oint: If this is
1e900 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 the case, then
1e901 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 77 the expression w
1e902 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 72 65 ill be. ** re
1e903 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20 65 -evaluated for e
1e904 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74 6f ach reference to
1e905 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
1e906 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e sNC.pEList = p->
1e907 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20 pEList;. if(
1e908 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
1e909 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
1e90a 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 20 20 >pWhere) ||.
1e90b 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 sqlite3Resolv
1e90c 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
1e90d 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 20 20 p->pHaving).
1e90e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1e90f 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1e910 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 }.. /* The OR
1e911 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f 55 50 DER BY and GROUP
1e912 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 79 20 BY clauses may
1e913 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 65 72 not refer to ter
1e914 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74 ms in. ** out
1e915 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 20 20 er queries .
1e916 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 */. sNC.pNext
1e917 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c = 0;. sNC.al
1e918 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20 lowAgg = 1;..
1e919 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20 /* Process the
1e91a 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
1e91b 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45 for singleton SE
1e91c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e LECT statements.
1e91d 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 44 45 . ** The ORDE
1e91e 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 R BY clause for
1e91f 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 43 54 compounds SELECT
1e920 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 68 statements is h
1e921 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65 andled. ** be
1e922 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f low, after all o
1e923 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 f the result-set
1e924 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 s for all of the
1e925 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20 elements of.
1e926 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 ** the compound
1e927 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
1e928 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ved.. */.
1e929 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20 if( !isCompound
1e92a 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 && resolveOrderG
1e92b 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20 roupBy(&sNC, p,
1e92c 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 p->pOrderBy, "OR
1e92d 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20 20 72 DER") ){. r
1e92e 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1e92f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 . }. if( d
1e930 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1e931 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1e932 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1e933 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c . . /* Resol
1e934 76 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 ve the GROUP BY
1e935 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68 65 20 clause. At the
1e936 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20 same time, make
1e937 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 sure . ** the
1e938 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1e939 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 does not contai
1e93a 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
1e93b 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 tions.. */.
1e93c 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 if( pGroupBy )
1e93d 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 {. struct E
1e93e 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
1e93f 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 tem;. .
1e940 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 if( resolveOrder
1e941 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c GroupBy(&sNC, p,
1e942 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52 4f 55 pGroupBy, "GROU
1e943 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f P") || db->mallo
1e944 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
1e945 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1e946 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ort;. }.
1e947 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
1e948 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69 m=pGroupBy->a; i
1e949 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 <pGroupBy->nExpr
1e94a 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1e94b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 . if( Exp
1e94c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 49 74 rHasProperty(pIt
1e94d 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f 41 67 em->pExpr, EP_Ag
1e94e 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 g) ){.
1e94f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e950 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 67 61 pParse, "aggrega
1e951 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 te functions are
1e952 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 not allowed in
1e953 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
1e954 22 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c "the GROUP BY cl
1e955 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 ause");.
1e956 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1e957 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rt;. }.
1e958 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1e959 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 /* Advance to t
1e95a 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 he next term of
1e95b 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20 the compound.
1e95c 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 */. p = p->p
1e95d 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70 Prior;. nComp
1e95e 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f ound++;. }.. /
1e95f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 4f 52 * Resolve the OR
1e960 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70 DER BY on a comp
1e961 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66 74 65 ound SELECT afte
1e962 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20 r all terms of.
1e963 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 ** the compound
1e964 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
1e965 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ved.. */. if(
1e966 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 isCompound && re
1e967 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 solveCompoundOrd
1e968 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 4c 65 erBy(pParse, pLe
1e969 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20 20 72 ftmost) ){. r
1e96a 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1e96b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57 . }.. return W
1e96c 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a RC_Prune;.}../*.
1e96d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1e96e 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73 walks an express
1e96f 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73 ion tree and res
1e970 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73 olves references
1e971 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c to.** table col
1e972 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c 74 2d umns and result-
1e973 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74 set columns. At
1e974 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 the same time,
1e975 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63 do error.** chec
1e976 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e king on function
1e977 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 20 61 usage and set a
1e978 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 67 67 flag if any agg
1e979 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
1e97a 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a .** are seen..**
1e97b 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 74 .** To resolve t
1e97c 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66 able columns ref
1e97d 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20 erences we look
1e97e 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20 73 75 for nodes (or su
1e97f 62 74 72 65 65 73 29 20 6f 66 20 74 68 65 20 0a btrees) of the .
1e980 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 ** form X.Y.Z or
1e981 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77 Y.Z or just Z w
1e982 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 here.**.**
1e983 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 X: The name of
1e984 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 45 78 a database. Ex
1e985 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22 74 65 : "main" or "te
1e986 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 mp" or.**
1e987 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 the symbolic
1e988 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 name assigned t
1e989 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64 20 64 o an ATTACH-ed d
1e98a 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 atabase..**.**
1e98b 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e 61 6d Y: The nam
1e98c 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 6e 20 e of a table in
1e98d 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 a FROM clause.
1e98e 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65 72 0a Or in a trigger.
1e98f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 6e 65 ** one
1e990 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 of the special
1e991 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72 20 22 names "old" or "
1e992 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 new"..**.**
1e993 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f Z: The name o
1e994 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 f a column in ta
1e995 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ble Y..**.** The
1e996 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72 6f 6f node at the roo
1e997 74 20 6f 66 20 74 68 65 20 73 75 62 74 72 65 65 t of the subtree
1e998 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61 73 20 is modified as
1e999 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
1e99a 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 20 20 Expr.op
1e99b 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43 Changed to TK_C
1e99c 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72 OLUMN.** Expr
1e99d 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69 6e 74 .pTab Point
1e99e 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f s to the Table o
1e99f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a bject for X.Y.**
1e9a0 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e Expr.iColumn
1e9a1 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e The column in
1e9a2 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20 dex in X.Y. -1
1e9a3 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a for the rowid..*
1e9a4 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62 6c 65 * Expr.iTable
1e9a5 20 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72 The VDBE cur
1e9a6 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 sor number for X
1e9a7 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 .Y.**.**.** To r
1e9a8 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d 73 65 esolve result-se
1e9a9 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f t references, lo
1e9aa 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ok for expressio
1e9ab 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a n nodes of the.*
1e9ac 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e * form Z (with n
1e9ad 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66 69 78 o X and Y prefix
1e9ae 29 20 77 68 65 72 65 20 74 68 65 20 5a 20 6d 61 ) where the Z ma
1e9af 74 63 68 65 73 20 74 68 65 20 72 69 67 68 74 2d tches the right-
1e9b0 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 hand.** size of
1e9b1 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69 6e 20 an AS clause in
1e9b2 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 6f the result-set o
1e9b3 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54 68 65 f a SELECT. The
1e9b4 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a Z expression.**
1e9b5 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
1e9b6 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 65 a copy of the le
1e9b7 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ft-hand side of
1e9b8 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 the result-set e
1e9b9 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61 xpression..** Ta
1e9ba 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e ble-name and fun
1e9bb 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e ction resolution
1e9bc 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 73 occurs on the s
1e9bd 75 62 73 74 69 74 75 74 65 64 20 65 78 70 72 65 ubstituted expre
1e9be 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20 ssion.** tree.
1e9bf 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a For example, in:
1e9c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 .**.** SELE
1e9c1 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 CT a+b AS x, c+d
1e9c2 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 AS y FROM t1 OR
1e9c3 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 DER BY x;.**.**
1e9c4 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f 66 20 The "x" term of
1e9c5 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 73 20 the order by is
1e9c6 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 2b 62 replaced by "a+b
1e9c7 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a " to render:.**.
1e9c8 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 ** SELECT a
1e9c9 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 +b AS x, c+d AS
1e9ca 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 y FROM t1 ORDER
1e9cb 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 BY a+b;.**.** Fu
1e9cc 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65 nction calls are
1e9cd 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65 checked to make
1e9ce 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 66 sure that the f
1e9cf 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64 unction is .** d
1e9d0 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61 74 20 efined and that
1e9d1 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 the correct numb
1e9d2 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1e9d3 61 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a are specified..*
1e9d4 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f * If the functio
1e9d5 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 n is an aggregat
1e9d6 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e e function, then
1e9d7 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41 67 67 the pNC->hasAgg
1e9d8 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74 is.** set and t
1e9d9 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63 68 61 he opcode is cha
1e9da 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e nged from TK_FUN
1e9db 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f CTION to TK_AGG_
1e9dc 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20 FUNCTION..** If
1e9dd 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f an expression co
1e9de 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 ntains aggregate
1e9df 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 functions then
1e9e0 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 the EP_Agg.** pr
1e9e1 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78 operty on the ex
1e9e2 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 74 2e pression is set.
1e9e3 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 .**.** An error
1e9e4 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 message is left
1e9e5 69 6e 20 70 50 61 72 73 65 20 69 66 20 61 6e 79 in pParse if any
1e9e6 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73 2e 20 thing is amiss.
1e9e7 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69 The number.** i
1e9e8 66 20 65 72 72 6f 72 73 20 69 73 20 72 65 74 75 f errors is retu
1e9e9 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
1e9ea 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1e9eb 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1e9ec 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 mes( . NameCont
1e9ed 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 ext *pNC,
1e9ee 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 /* Namespace to
1e9ef 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 resolve expressi
1e9f0 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 ons in. */. Exp
1e9f1 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1e9f2 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 /* The expr
1e9f3 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 ession to be ana
1e9f4 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69 lyzed. */.){. i
1e9f5 6e 74 20 73 61 76 65 64 48 61 73 41 67 67 3b 0a nt savedHasAgg;.
1e9f6 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69 Walker w;.. i
1e9f7 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 f( pExpr==0 ) re
1e9f8 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 turn 0;.#if SQLI
1e9f9 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
1e9fa 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61 72 73 H>0. {. Pars
1e9fb 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d e *pParse = pNC-
1e9fc 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 >pParse;. if(
1e9fd 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 sqlite3ExprChec
1e9fe 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 kHeight(pParse,
1e9ff 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70 pExpr->nHeight+p
1ea00 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 NC->pParse->nHei
1ea01 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ght) ){. re
1ea02 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
1ea03 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 pParse->nHeigh
1ea04 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 t += pExpr->nHei
1ea05 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ght;. }.#endif.
1ea06 20 20 73 61 76 65 64 48 61 73 41 67 67 20 3d 20 savedHasAgg =
1ea07 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70 pNC->hasAgg;. p
1ea08 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a NC->hasAgg = 0;.
1ea09 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
1ea0a 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 k = resolveExprS
1ea0b 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 tep;. w.xSelect
1ea0c 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c Callback = resol
1ea0d 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 veSelectStep;.
1ea0e 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e w.pParse = pNC->
1ea0f 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e pParse;. w.u.pN
1ea10 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 C = pNC;. sqlit
1ea11 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 e3WalkExpr(&w, p
1ea12 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c 49 54 Expr);.#if SQLIT
1ea13 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
1ea14 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 >0. pNC->pParse
1ea15 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 45 78 ->nHeight -= pEx
1ea16 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e pr->nHeight;.#en
1ea17 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e dif. if( pNC->n
1ea18 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 Err>0 ){. Exp
1ea19 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 rSetProperty(pEx
1ea1a 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 pr, EP_Error);.
1ea1b 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 }. if( pNC->ha
1ea1c 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72 sAgg ){. Expr
1ea1d 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 SetProperty(pExp
1ea1e 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 r, EP_Agg);. }e
1ea1f 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73 lse if( savedHas
1ea20 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e Agg ){. pNC->
1ea21 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a hasAgg = 1;. }.
1ea22 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73 return ExprHas
1ea23 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1ea24 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f EP_Error);.}.../
1ea25 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c *.** Resolve all
1ea26 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 names in all ex
1ea27 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 pressions of a S
1ea28 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c ELECT and in all
1ea29 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f .** decendents o
1ea2a 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e f the SELECT, in
1ea2b 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 cluding compound
1ea2c 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 s off of p->pPri
1ea2d 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 or,.** subquerie
1ea2e 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 s in expressions
1ea2f 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 , and subqueries
1ea30 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c used as FROM cl
1ea31 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a ause.** terms..*
1ea32 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33 *.** See sqlite3
1ea33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1ea34 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 () for a descrip
1ea35 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 tion of the kind
1ea36 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 s of.** transfor
1ea37 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 mations that occ
1ea38 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 ur..**.** All SE
1ea39 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
1ea3a 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e should have been
1ea3b 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a expanded using.
1ea3c 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
1ea3d 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74 Expand() prior t
1ea3e 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 o invoking this
1ea3f 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 routine..*/.SQLI
1ea40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1ea41 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 sqlite3ResolveSe
1ea42 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 lectNames(. Par
1ea43 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
1ea44 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 /* The parse
1ea45 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 r context */. S
1ea46 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 elect *p,
1ea47 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
1ea48 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 ECT statement be
1ea49 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 ing coded. */.
1ea4a 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 NameContext *pOu
1ea4b 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 terNC /* Name c
1ea4c 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e ontext for paren
1ea4d 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 t SELECT stateme
1ea4e 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 nt */.){. Walke
1ea4f 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 r w;.. assert(
1ea50 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 p!=0 );. w.xExp
1ea51 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f rCallback = reso
1ea52 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 lveExprStep;. w
1ea53 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b .xSelectCallback
1ea54 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 = resolveSelect
1ea55 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 Step;. w.pParse
1ea56 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 = pParse;. w.u
1ea57 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b .pNC = pOuterNC;
1ea58 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 . sqlite3WalkSe
1ea59 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a lect(&w, p);.}..
1ea5a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1ea5b 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 End of resolve.c
1ea5c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1ea5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1ea5f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1ea60 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e Begin file expr.
1ea61 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1ea62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1ea64 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
1ea65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
1ea66 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1ea67 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1ea68 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1ea69 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1ea6a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1ea6b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1ea6c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1ea6d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1ea6e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1ea6f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1ea70 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1ea71 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1ea72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1ea73 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1ea74 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1ea75 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1ea76 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1ea77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea7b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1ea7c 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 ile contains rou
1ea7d 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61 tines used for a
1ea7e 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73 nalyzing express
1ea7f 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 ions and.** for
1ea80 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20 generating VDBE
1ea81 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61 code that evalua
1ea82 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20 tes expressions
1ea83 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a in SQLite..**.**
1ea84 20 24 49 64 3a 20 65 78 70 72 2e 63 2c 76 20 31 $Id: expr.c,v 1
1ea85 2e 34 33 32 2e 32 2e 31 20 32 30 30 39 2f 30 35 .432.2.1 2009/05
1ea86 2f 32 35 20 31 32 3a 30 32 3a 32 34 20 64 72 68 /25 12:02:24 drh
1ea87 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
1ea88 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 Return the 'aff
1ea89 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 inity' of the ex
1ea8a 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 pression pExpr i
1ea8b 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f any..**.** If
1ea8c 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d pExpr is a colum
1ea8d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 n, a reference t
1ea8e 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 o a column via a
1ea8f 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a n 'AS' alias,.**
1ea90 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 or a sub-select
1ea91 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 with a column a
1ea92 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c s the return val
1ea93 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a ue, then the .**
1ea94 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 affinity of tha
1ea95 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 t column is retu
1ea96 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
1ea97 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 0x00 is returne
1ea98 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 d,.** indicating
1ea99 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 no affinity for
1ea9a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e the expression.
1ea9b 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 .**.** i.e. the
1ea9c 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 WHERE clause exp
1ea9d 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 resssions in the
1ea9e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 following state
1ea9f 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 ments all.** hav
1eaa0 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a e an affinity:.*
1eaa1 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
1eaa2 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 E t1(a);.** SELE
1eaa3 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 CT * FROM t1 WHE
1eaa4 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 RE a;.** SELECT
1eaa5 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 a AS b FROM t1 W
1eaa6 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 HERE b;.** SELEC
1eaa7 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 T * FROM t1 WHER
1eaa8 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d E (select a from
1eaa9 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f t1);.*/.SQLITE_
1eaaa 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
1eaab 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
1eaac 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 (Expr *pExpr){.
1eaad 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d int op = pExpr-
1eaae 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 >op;. if( op==T
1eaaf 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 K_SELECT ){.
1eab0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 66 assert( pExpr->f
1eab1 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 63 lags&EP_xIsSelec
1eab2 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 t );. return
1eab3 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
1eab4 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 ity(pExpr->x.pSe
1eab5 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b lect->pEList->a[
1eab6 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 23 0].pExpr);. }.#
1eab7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1eab8 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f 70 IT_CAST. if( op
1eab9 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 20 ==TK_CAST ){.
1eaba 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 return sqlite3A
1eabb 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78 ffinityType(&pEx
1eabc 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d 0a pr->token);. }.
1eabd 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f 70 #endif. if( (op
1eabe 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 ==TK_AGG_COLUMN
1eabf 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e || op==TK_COLUMN
1eac0 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 || op==TK_REGIS
1eac1 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 70 TER) . && pExp
1eac2 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b 0a r->pTab!=0. ){.
1eac3 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 /* op==TK_RE
1eac4 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d GISTER && pExpr-
1eac5 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 >pTab!=0 happens
1eac6 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 when pExpr was
1eac7 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 2a originally. *
1eac8 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 * a TK_COLUMN bu
1eac9 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
1eaca 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 evaluated and c
1eacb 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 ached in a regis
1eacc 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a ter */. int j
1eacd 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d = pExpr->iColum
1eace 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 29 n;. if( j<0 )
1eacf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 return SQLITE_A
1ead0 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
1ead1 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 assert( pExpr->p
1ead2 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d 3e Tab && j<pExpr->
1ead3 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 pTab->nCol );.
1ead4 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e return pExpr->
1ead5 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 66 pTab->aCol[j].af
1ead6 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 65 finity;. }. re
1ead7 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 69 turn pExpr->affi
1ead8 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 nity;.}../*.** S
1ead9 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 et the collating
1eada 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 78 sequence for ex
1eadb 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 74 pression pExpr t
1eadc 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 o be the collati
1eadd 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6e ng.** sequence n
1eade 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e 20 amed by pToken.
1eadf 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
1eae0 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 65 er to the revise
1eae1 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a d expression..**
1eae2 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 The collating s
1eae3 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b 65 equence is marke
1eae4 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 20 d as "explicit"
1eae5 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 70 using the EP_Exp
1eae6 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 2e Collate.** flag.
1eae7 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 6f An explicit co
1eae8 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
1eae9 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 69 will override i
1eaea 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c 61 mplicit.** colla
1eaeb 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e 0a ting sequences..
1eaec 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1eaed 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
1eaee 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 xprSetColl(Parse
1eaef 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
1eaf0 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 43 pExpr, Token *pC
1eaf1 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 ollName){. char
1eaf2 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20 *zColl = 0;
1eaf3 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f /* Dequo
1eaf4 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c ted name of coll
1eaf5 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a ation sequence *
1eaf6 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f /. CollSeq *pCo
1eaf7 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ll;. sqlite3 *d
1eaf8 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1eaf9 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 zColl = sqlite
1eafa 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
1eafb 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a 20 b, pCollName);.
1eafc 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a 43 if( pExpr && zC
1eafd 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c oll ){. pColl
1eafe 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
1eaff 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
1eb00 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 zColl, -1);.
1eb01 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 if( pColl ){.
1eb02 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 pExpr->pColl
1eb03 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 = pColl;. p
1eb04 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 Expr->flags |= E
1eb05 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
1eb06 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
1eb07 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 3DbFree(db, zCol
1eb08 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 78 l);. return pEx
1eb09 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 pr;.}../*.** Ret
1eb0a 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 urn the default
1eb0b 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
1eb0c 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 ce for the expre
1eb0d 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a ssion pExpr. If.
1eb0e 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 ** there is no d
1eb0f 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e efault collation
1eb10 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30 2e type, return 0.
1eb11 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1eb12 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
1eb13 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 te3ExprCollSeq(P
1eb14 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
1eb15 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f pr *pExpr){. Co
1eb16 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 llSeq *pColl = 0
1eb17 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45 ;. Expr *p = pE
1eb18 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 xpr;. while( p
1eb19 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20 ){. int op;.
1eb1a 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43 pColl = p->pC
1eb1b 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f oll;. if( pCo
1eb1c 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 ll ) break;.
1eb1d 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20 op = p->op;.
1eb1e 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f if( (op==TK_AGG_
1eb1f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b COLUMN || op==TK
1eb20 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
1eb21 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20 70 K_REGISTER) && p
1eb22 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 ->pTab!=0 ){.
1eb23 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47 /* op==TK_REG
1eb24 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61 62 ISTER && p->pTab
1eb25 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e !=0 happens when
1eb26 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 pExpr was origi
1eb27 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 61 nally. ** a
1eb28 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 TK_COLUMN but w
1eb29 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76 as previously ev
1eb2a 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63 68 aluated and cach
1eb2b 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 ed in a register
1eb2c 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
1eb2d 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 char *zColl;.
1eb2e 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 43 int j = p->iC
1eb2f 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 28 olumn;. if(
1eb30 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 j>=0 ){.
1eb31 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1eb32 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 Parse->db;.
1eb33 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54 zColl = p->pT
1eb34 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c ab->aCol[j].zCol
1eb35 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c l;. pColl
1eb36 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f = sqlite3FindCo
1eb37 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 llSeq(db, ENC(db
1eb38 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29 ), zColl, -1, 0)
1eb39 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d ;. pExpr-
1eb3a 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a >pColl = pColl;.
1eb3b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1eb3c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
1eb3d 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26 f( op!=TK_CAST &
1eb3e 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29 & op!=TK_UPLUS )
1eb3f 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 {. break;.
1eb40 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e }. p = p->
1eb41 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28 pLeft;. }. if(
1eb42 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c sqlite3CheckCol
1eb43 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f lSeq(pParse, pCo
1eb44 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c ll) ){ . pCol
1eb45 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 l = 0;. }. ret
1eb46 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a urn pColl;.}../*
1eb47 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20 .** pExpr is an
1eb48 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d operand of a com
1eb49 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
1eb4a 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a . aff2 is the.*
1eb4b 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 * type affinity
1eb4c 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65 of the other ope
1eb4d 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 rand. This rout
1eb4e 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ine returns the.
1eb4f 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
1eb50 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
1eb51 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d used for the com
1eb52 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
1eb53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1eb54 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
1eb55 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
1eb56 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
1eb57 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20 r aff2){. char
1eb58 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78 aff1 = sqlite3Ex
1eb59 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
1eb5a 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 );. if( aff1 &&
1eb5b 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 aff2 ){. /*
1eb5c 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 Both sides of th
1eb5d 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 e comparison are
1eb5e 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 columns. If one
1eb5f 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 has numeric.
1eb60 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73 ** affinity, us
1eb61 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73 e that. Otherwis
1eb62 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 e use no affinit
1eb63 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 y.. */. if
1eb64 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 ( sqlite3IsNumer
1eb65 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29 icAffinity(aff1)
1eb66 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d || sqlite3IsNum
1eb67 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 ericAffinity(aff
1eb68 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 2) ){. retu
1eb69 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 rn SQLITE_AFF_NU
1eb6a 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 MERIC;. }else
1eb6b 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1eb6c 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
1eb6d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1eb6e 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32 ( !aff1 && !aff2
1eb6f 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 ){. /* Neith
1eb70 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 er side of the c
1eb71 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63 omparison is a c
1eb72 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 olumn. Compare
1eb73 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c the. ** resul
1eb74 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 ts directly..
1eb75 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
1eb76 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
1eb77 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1eb78 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f One side is a co
1eb79 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 lumn, the other
1eb7a 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 is not. Use the
1eb7b 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 columns affinity
1eb7c 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
1eb7d 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 aff1==0 || aff2
1eb7e 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ==0 );. retur
1eb7f 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b n (aff1 + aff2);
1eb80 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 . }.}../*.** pE
1eb81 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 xpr is a compari
1eb82 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 son operator. R
1eb83 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61 eturn the type a
1eb84 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f ffinity that sho
1eb85 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 uld.** be applie
1eb86 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e d to both operan
1eb87 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e ds prior to doin
1eb88 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e g the comparison
1eb89 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
1eb8a 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
1eb8b 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
1eb8c 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20 {. char aff;.
1eb8d 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
1eb8e 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 p==TK_EQ || pExp
1eb8f 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 r->op==TK_IN ||
1eb90 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 pExpr->op==TK_LT
1eb91 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 ||. pE
1eb92 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c xpr->op==TK_GT |
1eb93 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
1eb94 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d GE || pExpr->op=
1eb95 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 =TK_LE ||.
1eb96 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 pExpr->op==T
1eb97 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 K_NE );. assert
1eb98 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29 ( pExpr->pLeft )
1eb99 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 ;. aff = sqlite
1eb9a 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 3ExprAffinity(pE
1eb9b 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 xpr->pLeft);. i
1eb9c 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 f( pExpr->pRight
1eb9d 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71 ){. aff = sq
1eb9e 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
1eb9f 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67 nity(pExpr->pRig
1eba0 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 ht, aff);. }els
1eba1 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f e if( ExprHasPro
1eba2 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1eba3 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
1eba4 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 aff = sqlite3C
1eba5 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 ompareAffinity(p
1eba6 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d Expr->x.pSelect-
1eba7 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
1eba8 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c xpr, aff);. }el
1eba9 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 se if( !aff ){.
1ebaa 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
1ebab 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 AFF_NONE;. }.
1ebac 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f return aff;.}../
1ebad 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 *.** pExpr is a
1ebae 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 comparison expre
1ebaf 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 ssion, eg. '=',
1ebb0 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 '<', IN(...) etc
1ebb1 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 ..** idx_affinit
1ebb2 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 y is the affinit
1ebb3 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 y of an indexed
1ebb4 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 column. Return t
1ebb5 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e rue.** if the in
1ebb6 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 dex with affinit
1ebb7 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d y idx_affinity m
1ebb8 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d ay be used to im
1ebb9 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 plement.** the c
1ebba 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 omparison in pEx
1ebbb 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pr..*/.SQLITE_PR
1ebbc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1ebbd 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 3IndexAffinityOk
1ebbe 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
1ebbf 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 ar idx_affinity)
1ebc0 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 {. char aff = c
1ebc1 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 omparisonAffinit
1ebc2 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 y(pExpr);. swit
1ebc3 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 ch( aff ){. c
1ebc4 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e ase SQLITE_AFF_N
1ebc5 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 ONE:. retur
1ebc6 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 n 1;. case SQ
1ebc7 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 LITE_AFF_TEXT:.
1ebc8 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f return idx_
1ebc9 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
1ebca 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 _AFF_TEXT;. d
1ebcb 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 efault:. re
1ebcc 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 turn sqlite3IsNu
1ebcd 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 mericAffinity(id
1ebce 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d x_affinity);. }
1ebcf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1ebd0 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 the P5 value th
1ebd1 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 at should be use
1ebd2 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 d for a binary c
1ebd3 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 omparison.** opc
1ebd4 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 ode (OP_Eq, OP_G
1ebd5 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 e etc.) used to
1ebd6 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 compare pExpr1 a
1ebd7 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 nd pExpr2..*/.st
1ebd8 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f atic u8 binaryCo
1ebd9 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 mpareP5(Expr *pE
1ebda 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 xpr1, Expr *pExp
1ebdb 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 r2, int jumpIfNu
1ebdc 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 ll){. u8 aff =
1ebdd 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 (char)sqlite3Exp
1ebde 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 rAffinity(pExpr2
1ebdf 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 );. aff = (u8)s
1ebe0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
1ebe1 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 inity(pExpr1, af
1ebe2 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e f) | (u8)jumpIfN
1ebe3 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 ull;. return af
1ebe4 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 f;.}../*.** Retu
1ebe5 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1ebe6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
1ebe7 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 quence that shou
1ebe8 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a ld be used by.**
1ebe9 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 a binary compar
1ebea 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f ison operator co
1ebeb 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e mparing pLeft an
1ebec 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 d pRight..**.**
1ebed 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 If the left hand
1ebee 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 expression has
1ebef 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 a collating sequ
1ebf0 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 ence type, then
1ebf1 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f it is.** used. O
1ebf2 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c therwise the col
1ebf3 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1ebf4 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 for the right ha
1ebf5 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a nd expression.**
1ebf6 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 is used, or the
1ebf7 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 default (BINARY
1ebf8 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 ) if neither exp
1ebf9 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f ression has a co
1ebfa 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e llating.** type.
1ebfb 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 .**.** Argument
1ebfc 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 pRight (but not
1ebfd 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 pLeft) may be a
1ebfe 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e null pointer. In
1ebff 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 this case,.** i
1ec00 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 t is not conside
1ec01 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
1ec02 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
1ec03 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
1ec04 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 pareCollSeq(. P
1ec05 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 arse *pParse, .
1ec06 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 Expr *pLeft, .
1ec07 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b Expr *pRight.){
1ec08 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
1ec09 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 l;. assert( pLe
1ec0a 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 ft );. if( pLef
1ec0b 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 t->flags & EP_Ex
1ec0c 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 pCollate ){.
1ec0d 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 assert( pLeft->p
1ec0e 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c Coll );. pCol
1ec0f 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c l = pLeft->pColl
1ec10 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 ;. }else if( pR
1ec11 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e ight && pRight->
1ec12 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
1ec13 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 llate ){. ass
1ec14 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f ert( pRight->pCo
1ec15 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 ll );. pColl
1ec16 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b = pRight->pColl;
1ec17 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 . }else{. pC
1ec18 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
1ec19 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
1ec1a 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 pLeft);. if(
1ec1b 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 !pColl ){.
1ec1c 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
1ec1d 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
1ec1e 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 se, pRight);.
1ec1f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1ec20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pColl;.}../*.**
1ec21 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 Generate the ope
1ec22 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 rands for a comp
1ec23 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e arison operation
1ec24 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e . Before.** gen
1ec25 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 erating the code
1ec26 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e for each operan
1ec27 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e d, set the EP_An
1ec28 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 yAff.** flag on
1ec29 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
1ec2a 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 o that it will b
1ec2b 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 e able to used a
1ec2c 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d .** cached colum
1ec2d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 n value that has
1ec2e 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 previously unde
1ec2f 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 rgone an.** affi
1ec30 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a nity change..*/.
1ec31 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 static void code
1ec32 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
1ec33 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1ec34 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
1ec35 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 and code generat
1ec36 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
1ec37 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 Expr *pLeft,
1ec38 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f /* The left o
1ec39 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
1ec3a 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a *pRegLeft, /*
1ec3b 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 Register where
1ec3c 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 left operand is
1ec3d 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 stored */. int
1ec3e 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a *pFreeLeft, /*
1ec3f 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 Free this regis
1ec40 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f ter when done */
1ec41 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c . Expr *pRight,
1ec42 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 /* The righ
1ec43 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 t operand */. i
1ec44 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 nt *pRegRight,
1ec45 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 /* Register whe
1ec46 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 re right operand
1ec47 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 is stored */.
1ec48 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 int *pFreeRight
1ec49 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 /* Write temp
1ec4a 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 register for rig
1ec4b 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 ht operand there
1ec4c 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 */.){. while(
1ec4d 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 pLeft->op==TK_UP
1ec4e 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c LUS ) pLeft = pL
1ec4f 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c eft->pLeft;. pL
1ec50 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 eft->flags |= EP
1ec51 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 _AnyAff;. *pReg
1ec52 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 Left = sqlite3Ex
1ec53 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1ec54 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c e, pLeft, pFreeL
1ec55 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 eft);. while( p
1ec56 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 Right->op==TK_UP
1ec57 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 LUS ) pRight = p
1ec58 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 Right->pLeft;.
1ec59 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d pRight->flags |=
1ec5a 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 EP_AnyAff;. *p
1ec5b 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 RegRight = sqlit
1ec5c 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
1ec5d 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 Parse, pRight, p
1ec5e 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f FreeRight);.}../
1ec5f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1ec60 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 de for a compari
1ec61 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f son operator..*/
1ec62 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 .static int code
1ec63 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 Compare(. Parse
1ec64 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 *pParse, /*
1ec65 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 The parsing (and
1ec66 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
1ec67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 ) context */. E
1ec68 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 xpr *pLeft,
1ec69 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 /* The left ope
1ec6a 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a rand */. Expr *
1ec6b 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 pRight, /* T
1ec6c 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 he right operand
1ec6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 */. int opcode
1ec6e 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 , /* The c
1ec6f 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 omparison opcode
1ec70 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 */. int in1, i
1ec71 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 nt in2, /* Regis
1ec72 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 ter holding oper
1ec73 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 ands */. int de
1ec74 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a st, /* J
1ec75 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 ump here if true
1ec76 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 . */. int jump
1ec77 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 IfNull /* If
1ec78 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 true, jump if ei
1ec79 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1ec7a 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 NULL */.){. int
1ec7b 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b p5;. int addr;
1ec7c 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a . CollSeq *p4;.
1ec7d 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 . p4 = sqlite3B
1ec7e 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
1ec7f 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 Seq(pParse, pLef
1ec80 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 t, pRight);. p5
1ec81 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 = binaryCompare
1ec82 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 P5(pLeft, pRight
1ec83 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
1ec84 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
1ec85 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 dbeAddOp4(pParse
1ec86 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c ->pVdbe, opcode,
1ec87 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c in2, dest, in1,
1ec88 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ec89 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 (voi
1ec8a 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 d*)p4, P4_COLLSE
1ec8b 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 Q);. sqlite3Vdb
1ec8c 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 eChangeP5(pParse
1ec8d 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 ->pVdbe, (u8)p5)
1ec8e 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 ;. if( (p5 & SQ
1ec8f 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d LITE_AFF_MASK)!=
1ec90 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 SQLITE_AFF_NONE
1ec91 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
1ec92 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
1ec93 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e hange(pParse, in
1ec94 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 1, 1);. sqlit
1ec95 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e e3ExprCacheAffin
1ec96 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 ityChange(pParse
1ec97 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 , in2, 1);. }.
1ec98 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a return addr;.}.
1ec99 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
1ec9a 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a EXPR_DEPTH>0./*.
1ec9b 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 ** Check that ar
1ec9c 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 gument nHeight i
1ec9d 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
1ec9e 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 qual to the maxi
1ec9f 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f mum.** expressio
1eca0 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e n depth allowed.
1eca1 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c If it is not, l
1eca2 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 eave an error me
1eca3 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 ssage in.** pPar
1eca4 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
1eca5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1eca6 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 3ExprCheckHeight
1eca7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1eca8 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 int nHeight){.
1eca9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1ecaa 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 OK;. int mxHeig
1ecab 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d ht = pParse->db-
1ecac 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1ecad 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d IMIT_EXPR_DEPTH]
1ecae 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e ;. if( nHeight>
1ecaf 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 mxHeight ){.
1ecb0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1ecb1 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
1ecb2 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 "Expression tree
1ecb3 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d is too large (m
1ecb4 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 aximum depth %d)
1ecb5 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 ", mxHeight.
1ecb6 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1ecb7 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
1ecb8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ecb9 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
1ecba 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 hree functions,
1ecbb 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 heightOfExpr(),
1ecbc 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
1ecbd 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 ().** and height
1ecbe 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 OfSelect(), are
1ecbf 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1ecc0 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 e the maximum he
1ecc1 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 ight.** of any e
1ecc2 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 xpression tree r
1ecc3 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 eferenced by the
1ecc4 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 structure passe
1ecc5 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 d as the.** firs
1ecc6 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
1ecc7 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 * If this maximu
1ecc8 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 m height is grea
1ecc9 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 ter than the cur
1ecca 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 rent value point
1eccb 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 ed.** to by pnHe
1eccc 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 ight, the second
1eccd 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e parameter, then
1ecce 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 set *pnHeight t
1eccf 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e o that.** value.
1ecd0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1ecd1 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 heightOfExpr(Exp
1ecd2 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 r *p, int *pnHei
1ecd3 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b ght){. if( p ){
1ecd4 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 . if( p->nHei
1ecd5 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b ght>*pnHeight ){
1ecd6 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 . *pnHeight
1ecd7 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 = p->nHeight;.
1ecd8 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 }. }.}.stati
1ecd9 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 c void heightOfE
1ecda 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 xprList(ExprList
1ecdb 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 *p, int *pnHeig
1ecdc 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a ht){. if( p ){.
1ecdd 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 int i;. f
1ecde 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 or(i=0; i<p->nEx
1ecdf 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
1ece0 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
1ece1 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 a[i].pExpr, pnHe
1ece2 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ight);. }. }
1ece3 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 .}.static void h
1ece4 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 eightOfSelect(Se
1ece5 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e lect *p, int *pn
1ece6 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 Height){. if( p
1ece7 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 ){. heightOf
1ece8 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 Expr(p->pWhere,
1ece9 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
1ecea 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
1eceb 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 Having, pnHeight
1ecec 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 );. heightOfE
1eced 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 xpr(p->pLimit, p
1ecee 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
1ecef 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f ightOfExpr(p->pO
1ecf0 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 ffset, pnHeight)
1ecf1 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1ecf2 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 prList(p->pEList
1ecf3 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
1ecf4 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
1ecf5 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 t(p->pGroupBy, p
1ecf6 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
1ecf7 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 ightOfExprList(p
1ecf8 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 ->pOrderBy, pnHe
1ecf9 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
1ecfa 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 tOfSelect(p->pPr
1ecfb 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a ior, pnHeight);.
1ecfc 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 }.}../*.** Set
1ecfd 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 the Expr.nHeigh
1ecfe 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 t variable in th
1ecff 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 e structure pass
1ed00 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 ed as an .** arg
1ed01 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 ument. An expres
1ed02 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 sion with no chi
1ed03 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 ldren, Expr.pLis
1ed04 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 t or .** Expr.pS
1ed05 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 elect member has
1ed06 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 a height of 1.
1ed07 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 Any other expres
1ed08 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 sion.** has a he
1ed09 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 ight equal to th
1ed0a 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 e maximum height
1ed0b 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a of any other .*
1ed0c 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 * referenced Exp
1ed0d 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 r plus one..*/.s
1ed0e 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 tatic void exprS
1ed0f 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 etHeight(Expr *p
1ed10 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 ){. int nHeight
1ed11 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 = 0;. heightOf
1ed12 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 Expr(p->pLeft, &
1ed13 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 nHeight);. heig
1ed14 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 htOfExpr(p->pRig
1ed15 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 ht, &nHeight);.
1ed16 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
1ed17 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 erty(p, EP_xIsSe
1ed18 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69 lect) ){. hei
1ed19 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78 ghtOfSelect(p->x
1ed1a 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 .pSelect, &nHeig
1ed1b 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ht);. }else{.
1ed1c 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 heightOfExprLi
1ed1d 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 st(p->x.pList, &
1ed1e 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 nHeight);. }.
1ed1f 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 p->nHeight = nHe
1ed20 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a ight + 1;.}../*.
1ed21 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e ** Set the Expr.
1ed22 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 nHeight variable
1ed23 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 using the exprS
1ed24 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 etHeight() funct
1ed25 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 ion. If.** the h
1ed26 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 eight is greater
1ed27 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 than the maximu
1ed28 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 m allowed expres
1ed29 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c sion depth,.** l
1ed2a 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e eave an error in
1ed2b 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
1ed2c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1ed2d 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
1ed2e 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 ight(Parse *pPar
1ed2f 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 se, Expr *p){.
1ed30 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29 exprSetHeight(p)
1ed31 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
1ed32 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 heckHeight(pPars
1ed33 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a e, p->nHeight);.
1ed34 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1ed35 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 the maximum heig
1ed36 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 ht of any expres
1ed37 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 sion tree refere
1ed38 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 nced.** by the s
1ed39 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
1ed3a 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 passed as an arg
1ed3b 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
1ed3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ed3d 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 ite3SelectExprHe
1ed3e 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b ight(Select *p){
1ed3f 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d . int nHeight =
1ed40 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 0;. heightOfSe
1ed41 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 lect(p, &nHeight
1ed42 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 );. return nHei
1ed43 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 ght;.}.#else. #
1ed44 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 define exprSetHe
1ed45 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f ight(y).#endif /
1ed46 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 * SQLITE_MAX_EXP
1ed47 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a R_DEPTH>0 */../*
1ed48 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 .** Construct a
1ed49 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e new expression n
1ed4a 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ode and return a
1ed4b 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
1ed4c 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 Memory.** for t
1ed4d 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 61 his node is obta
1ed4e 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1ed4f 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 3_malloc(). The
1ed50 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
1ed51 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 n.** is responsi
1ed52 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 ble for making s
1ed53 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 ure the node eve
1ed54 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 ntually gets fre
1ed55 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1ed56 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
1ed57 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74 te3Expr(. sqlit
1ed58 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
1ed59 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
1ed5a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1ed5b 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e Zero() (may be n
1ed5c 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 ull) */. int op
1ed5d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1ed5e 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
1ed5f 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 opcode */. Expr
1ed60 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 *pLeft,
1ed61 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 /* Left oper
1ed62 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 and */. Expr *p
1ed63 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 Right,
1ed64 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e /* Right operan
1ed65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b d */. const Tok
1ed66 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f en *pToken /
1ed67 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e * Argument token
1ed68 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
1ed69 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 New;. pNew = sq
1ed6a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
1ed6b 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 o(db, sizeof(Exp
1ed6c 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d r));. if( pNew=
1ed6d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 =0 ){. /* Whe
1ed6e 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 n malloc fails,
1ed6f 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e 64 delete pLeft and
1ed70 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 73 pRight. Express
1ed71 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 0a ions passed to .
1ed72 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 ** this func
1ed73 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 73 tion must always
1ed74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 be allocated wi
1ed75 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 29 th sqlite3Expr()
1ed76 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 2a for this . *
1ed77 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 2a * reason. . *
1ed78 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 /. sqlite3Exp
1ed79 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 rDelete(db, pLef
1ed7a 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 t);. sqlite3E
1ed7b 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52 xprDelete(db, pR
1ed7c 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72 ight);. retur
1ed7d 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d n 0;. }. pNew-
1ed7e 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 >op = (u8)op;.
1ed7f 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 4c pNew->pLeft = pL
1ed80 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 eft;. pNew->pRi
1ed81 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 ght = pRight;.
1ed82 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b pNew->iAgg = -1;
1ed83 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 . pNew->span.z
1ed84 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66 28 = (u8*)"";. if(
1ed85 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 pToken ){. i
1ed86 6e 74 20 63 3b 0a 20 20 20 20 61 73 73 65 72 74 nt c;. assert
1ed87 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 ( pToken->dyn==0
1ed88 20 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73 70 );. pNew->sp
1ed89 61 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 an = *pToken;.
1ed8a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e if( pToken->n>
1ed8b 3d 32 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 =2 . &&
1ed8c 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a 5b ((c = pToken->z[
1ed8d 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d 3d 0])=='\'' || c==
1ed8e 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c 7c '"' || c=='[' ||
1ed8f 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20 20 c=='`') ){.
1ed90 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f sqlite3TokenCo
1ed91 70 79 28 64 62 2c 20 26 70 4e 65 77 2d 3e 74 6f py(db, &pNew->to
1ed92 6b 65 6e 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 ken, pToken);.
1ed93 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 74 6f if( pNew->to
1ed94 6b 65 6e 2e 7a 20 29 7b 0a 20 20 20 20 20 20 20 ken.z ){.
1ed95 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d pNew->token.n =
1ed96 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 sqlite3Dequote(
1ed97 28 63 68 61 72 2a 29 70 4e 65 77 2d 3e 74 6f 6b (char*)pNew->tok
1ed98 65 6e 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 en.z);. a
1ed99 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 74 6f 6b ssert( pNew->tok
1ed9a 65 6e 2e 6e 3d 3d 28 75 6e 73 69 67 6e 65 64 29 en.n==(unsigned)
1ed9b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1ed9c 28 63 68 61 72 2a 29 70 4e 65 77 2d 3e 74 6f 6b (char*)pNew->tok
1ed9d 65 6e 2e 7a 29 20 29 3b 0a 20 20 20 20 20 20 7d en.z) );. }
1ed9e 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22 . if( c=='"
1ed9f 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 ' ) pNew->flags
1eda0 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b |= EP_DblQuoted;
1eda1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1eda2 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20 pNew->token =
1eda3 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 7d 0a 20 *pToken;. }.
1eda4 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 71 pNew->token.q
1eda5 75 6f 74 65 64 20 3d 20 30 3b 0a 20 20 7d 65 6c uoted = 0;. }el
1eda6 73 65 20 69 66 28 20 70 4c 65 66 74 20 29 7b 0a se if( pLeft ){.
1eda7 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20 29 if( pRight )
1eda8 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 {. if( pRig
1eda9 68 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 ht->span.dyn==0
1edaa 26 26 20 70 4c 65 66 74 2d 3e 73 70 61 6e 2e 64 && pLeft->span.d
1edab 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 yn==0 ){.
1edac 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e sqlite3ExprSpan
1edad 28 70 4e 65 77 2c 20 26 70 4c 65 66 74 2d 3e 73 (pNew, &pLeft->s
1edae 70 61 6e 2c 20 26 70 52 69 67 68 74 2d 3e 73 70 pan, &pRight->sp
1edaf 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 an);. }.
1edb0 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 if( pRight->f
1edb1 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
1edb2 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 late ){.
1edb3 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 pNew->flags |= E
1edb4 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
1edb5 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c pNew->pCol
1edb6 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c l = pRight->pCol
1edb7 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d l;. }. }
1edb8 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e . if( pLeft->
1edb9 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
1edba 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 70 llate ){. p
1edbb 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 New->flags |= EP
1edbc 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 _ExpCollate;.
1edbd 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d pNew->pColl =
1edbe 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 pLeft->pColl;.
1edbf 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 65 78 70 72 }. }.. expr
1edc0 53 65 74 48 65 69 67 68 74 28 70 4e 65 77 29 3b SetHeight(pNew);
1edc1 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
1edc2 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f 72 6b 73 20 6c }../*.** Works l
1edc3 69 6b 65 20 73 71 6c 69 74 65 33 45 78 70 72 28 ike sqlite3Expr(
1edc4 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 ) except that it
1edc5 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20 takes an extra
1edc6 50 61 72 73 65 2a 0a 2a 2a 20 61 72 67 75 6d 65 Parse*.** argume
1edc7 6e 74 20 61 6e 64 20 6e 6f 74 69 66 69 65 73 20 nt and notifies
1edc8 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 63 the associated c
1edc9 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 onnection object
1edca 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 if malloc fails
1edcb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1edcc 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
1edcd 33 50 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 3PExpr(. Parse
1edce 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
1edcf 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1edd0 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 text */. int op
1edd1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1edd2 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
1edd3 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 opcode */. Expr
1edd4 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 *pLeft,
1edd5 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 /* Left oper
1edd6 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 and */. Expr *p
1edd7 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 Right,
1edd8 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e /* Right operan
1edd9 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b d */. const Tok
1edda 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f en *pToken /
1eddb 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e * Argument token
1eddc 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
1eddd 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 = sqlite3Expr(p
1edde 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c 20 70 Parse->db, op, p
1eddf 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 70 54 Left, pRight, pT
1ede0 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 oken);. if( p )
1ede1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 {. sqlite3Exp
1ede2 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 rCheckHeight(pPa
1ede3 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 rse, p->nHeight)
1ede4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1ede5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 ;.}../*.** When
1ede6 64 6f 69 6e 67 20 61 20 6e 65 73 74 65 64 20 70 doing a nested p
1ede7 61 72 73 65 2c 20 79 6f 75 20 63 61 6e 20 69 6e arse, you can in
1ede8 63 6c 75 64 65 20 74 65 72 6d 73 20 69 6e 20 61 clude terms in a
1ede9 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 n expression.**
1edea 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 that look like t
1edeb 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e 2e 2e his: #1 #2 ...
1edec 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 72 65 These terms re
1eded 66 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 73 fer to registers
1edee 0a 2a 2a 20 69 6e 20 74 68 65 20 76 69 72 74 75 .** in the virtu
1edef 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20 al machine. #N
1edf0 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65 67 69 is the N-th regi
1edf1 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ster..**.** This
1edf2 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
1edf3 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 ed by the parser
1edf4 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6f 6e to deal with on
1edf5 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 2e of those terms.
1edf6 0a 2a 2a 20 49 74 20 69 6d 6d 65 64 69 61 74 65 .** It immediate
1edf7 6c 79 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 ly generates cod
1edf8 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 e to store the v
1edf9 61 6c 75 65 20 69 6e 20 61 20 6d 65 6d 6f 72 79 alue in a memory
1edfa 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 location..** Th
1edfb 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 78 70 e returns an exp
1edfc 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 69 6c ression that wil
1edfd 6c 20 63 6f 64 65 20 74 6f 20 65 78 74 72 61 63 l code to extrac
1edfe 74 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d t the value from
1edff 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 .** that memory
1ee00 6c 6f 63 61 74 69 6f 6e 20 61 73 20 6e 65 65 64 location as need
1ee01 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1ee02 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
1ee03 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72 28 te3RegisterExpr(
1ee04 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
1ee05 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 oken *pToken){.
1ee06 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 Vdbe *v = pPars
1ee07 65 2d 3e 70 56 64 62 65 3b 0a 20 20 45 78 70 72 e->pVdbe;. Expr
1ee08 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 *p;. if( pPars
1ee09 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a e->nested==0 ){.
1ee0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1ee0b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61 Msg(pParse, "nea
1ee0c 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78 r \"%T\": syntax
1ee0d 20 65 72 72 6f 72 22 2c 20 70 54 6f 6b 65 6e 29 error", pToken)
1ee0e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c ;. return sql
1ee0f 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 ite3PExpr(pParse
1ee10 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c , TK_NULL, 0, 0,
1ee11 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 0);. }. if( v
1ee12 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1ee13 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 p = sqlite3PEx
1ee14 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 45 pr(pParse, TK_RE
1ee15 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20 70 54 GISTER, 0, 0, pT
1ee16 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 3d 3d oken);. if( p==
1ee17 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1ee18 30 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 66 61 0; /* Malloc fa
1ee19 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 2d iled */. }. p-
1ee1a 3e 69 54 61 62 6c 65 20 3d 20 61 74 6f 69 28 28 >iTable = atoi((
1ee1b 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a char*)&pToken->z
1ee1c 5b 31 5d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 [1]);. return p
1ee1d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20 ;.}../*.** Join
1ee1e 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 two expressions
1ee1f 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f 70 65 using an AND ope
1ee20 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 68 65 rator. If eithe
1ee21 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a r expression is.
1ee22 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 6a 75 ** NULL, then ju
1ee23 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 6f 74 st return the ot
1ee24 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a her expression..
1ee25 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1ee26 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
1ee27 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33 20 2a xprAnd(sqlite3 *
1ee28 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 74 2c db, Expr *pLeft,
1ee29 20 45 78 70 72 20 2a 70 52 69 67 68 74 29 7b 0a Expr *pRight){.
1ee2a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 20 29 if( pLeft==0 )
1ee2b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 52 69 {. return pRi
1ee2c 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ght;. }else if(
1ee2d 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 pRight==0 ){.
1ee2e 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a return pLeft;.
1ee2f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
1ee30 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 28 urn sqlite3Expr(
1ee31 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 70 4c 65 66 db, TK_AND, pLef
1ee32 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 t, pRight, 0);.
1ee33 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 }.}../*.** Set
1ee34 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20 66 69 the Expr.span fi
1ee35 65 6c 64 20 6f 66 20 74 68 65 20 67 69 76 65 6e eld of the given
1ee36 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 expression to s
1ee37 70 61 6e 20 61 6c 6c 0a 2a 2a 20 74 65 78 74 20 pan all.** text
1ee38 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 between the two
1ee39 67 69 76 65 6e 20 74 6f 6b 65 6e 73 2e 20 20 42 given tokens. B
1ee3a 6f 74 68 20 74 6f 6b 65 6e 73 20 6d 75 73 74 20 oth tokens must
1ee3b 62 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 be pointing.** a
1ee3c 74 20 74 68 65 20 73 61 6d 65 20 73 74 72 69 6e t the same strin
1ee3d 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
1ee3e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1ee3f 33 45 78 70 72 53 70 61 6e 28 45 78 70 72 20 2a 3ExprSpan(Expr *
1ee40 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 4c pExpr, Token *pL
1ee41 65 66 74 2c 20 54 6f 6b 65 6e 20 2a 70 52 69 67 eft, Token *pRig
1ee42 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 ht){. assert( p
1ee43 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 61 73 Right!=0 );. as
1ee44 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 20 29 sert( pLeft!=0 )
1ee45 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b ;. if( pExpr ){
1ee46 0a 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e . pExpr->span
1ee47 2e 7a 20 3d 20 70 4c 65 66 74 2d 3e 7a 3b 0a 20 .z = pLeft->z;.
1ee48 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /* The follow
1ee49 69 6e 67 20 61 73 73 65 72 74 28 29 20 6d 61 79 ing assert() may
1ee4a 20 66 61 69 6c 20 77 68 65 6e 20 74 68 69 73 20 fail when this
1ee4b 69 73 20 63 61 6c 6c 65 64 20 0a 20 20 20 20 2a is called . *
1ee4c 2a 20 76 69 61 20 73 71 6c 69 74 65 33 50 45 78 * via sqlite3PEx
1ee4d 70 72 28 29 2f 73 71 6c 69 74 65 33 45 78 70 72 pr()/sqlite3Expr
1ee4e 28 29 20 66 72 6f 6d 20 61 64 64 57 68 65 72 65 () from addWhere
1ee4f 54 65 72 6d 28 29 2e 20 2a 2f 0a 20 20 20 20 2f Term(). */. /
1ee50 2a 20 61 73 73 65 72 74 28 70 52 69 67 68 74 2d * assert(pRight-
1ee51 3e 7a 20 3e 3d 20 70 4c 65 66 74 2d 3e 7a 29 3b >z >= pLeft->z);
1ee52 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 73 */. pExpr->s
1ee53 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 2d 3e pan.n = pRight->
1ee54 6e 20 2b 20 28 75 6e 73 69 67 6e 65 64 29 28 70 n + (unsigned)(p
1ee55 52 69 67 68 74 2d 3e 7a 20 2d 20 70 4c 65 66 74 Right->z - pLeft
1ee56 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ->z);. }.}../*.
1ee57 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e ** Construct a n
1ee58 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f ew expression no
1ee59 64 65 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f de for a functio
1ee5a 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 0a n with multiple.
1ee5b 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f ** arguments..*/
1ee5c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ee5d 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
1ee5e 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 65 20 rFunction(Parse
1ee5f 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c 69 73 *pParse, ExprLis
1ee60 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 t *pList, Token
1ee61 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78 70 72 *pToken){. Expr
1ee62 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c 69 74 65 *pNew;. sqlite
1ee63 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1ee64 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 db;. assert( pT
1ee65 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 77 20 3d oken );. pNew =
1ee66 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1ee67 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
1ee68 45 78 70 72 29 20 29 3b 0a 20 20 69 66 28 20 70 Expr) );. if( p
1ee69 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 New==0 ){. sq
1ee6a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
1ee6b 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 ete(db, pList);
1ee6c 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 /* Avoid memory
1ee6d 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 leak when malloc
1ee6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 fails */. re
1ee6f 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e turn 0;. }. pN
1ee70 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 ew->op = TK_FUNC
1ee71 54 49 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e 78 2e TION;. pNew->x.
1ee72 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 pList = pList;.
1ee73 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
1ee74 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 sProperty(pNew,
1ee75 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
1ee76 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 . assert( pToke
1ee77 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 70 n->dyn==0 );. p
1ee78 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70 54 6f New->span = *pTo
1ee79 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f ken;. sqlite3To
1ee7a 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 4e 65 kenCopy(db, &pNe
1ee7b 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e w->token, pToken
1ee7c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
1ee7d 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 SetHeight(pParse
1ee7e 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 , pNew);. retur
1ee7f 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
1ee80 20 41 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 Assign a variab
1ee81 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 le number to an
1ee82 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
1ee83 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 encodes a wildca
1ee84 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 rd.** in the ori
1ee85 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d ginal SQL statem
1ee86 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c ent. .**.** Wil
1ee87 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e dcards consistin
1ee88 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f g of a single "?
1ee89 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 " are assigned t
1ee8a 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 he next sequenti
1ee8b 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e al.** variable n
1ee8c 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c umber..**.** Wil
1ee8d 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f dcards of the fo
1ee8e 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 rm "?nnn" are as
1ee8f 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 signed the numbe
1ee90 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b r "nnn". We mak
1ee91 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 e.** sure "nnn"
1ee92 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f is not too be to
1ee93 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 avoid a denial
1ee94 6f 66 20 73 65 72 76 69 63 65 20 61 74 74 61 63 of service attac
1ee95 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 k when.** the SQ
1ee96 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 L statement come
1ee97 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e s from an extern
1ee98 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a al source..**.**
1ee99 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 Wildcards of th
1ee9a 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72 e form ":aaa" or
1ee9b 20 22 24 61 61 61 22 20 61 72 65 20 61 73 73 69 "$aaa" are assi
1ee9c 67 6e 65 64 20 74 68 65 20 73 61 6d 65 20 6e 75 gned the same nu
1ee9d 6d 62 65 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 mber.** as the p
1ee9e 72 65 76 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 revious instance
1ee9f 20 6f 66 20 74 68 65 20 73 61 6d 65 20 77 69 6c of the same wil
1eea0 64 63 61 72 64 2e 20 20 4f 72 20 69 66 20 74 68 dcard. Or if th
1eea1 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 0a is is the first.
1eea2 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
1eea3 68 65 20 77 69 6c 64 63 61 72 64 2c 20 74 68 65 he wildcard, the
1eea4 20 6e 65 78 74 20 73 65 71 75 65 6e 69 61 6c 20 next sequenial
1eea5 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
1eea6 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a is.** assigned..
1eea7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1eea8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1eea9 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 prAssignVarNumbe
1eeaa 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c r(Parse *pParse,
1eeab 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1eeac 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a Token *pToken;.
1eead 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1eeae 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 pParse->db;.. i
1eeaf 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 f( pExpr==0 ) re
1eeb0 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e 20 3d turn;. pToken =
1eeb1 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a &pExpr->token;.
1eeb2 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e assert( pToken
1eeb3 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 ->n>=1 );. asse
1eeb4 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21 3d 30 rt( pToken->z!=0
1eeb5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 );. assert( pT
1eeb6 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20 29 3b oken->z[0]!=0 );
1eeb7 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e . if( pToken->n
1eeb8 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 ==1 ){. /* Wi
1eeb9 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f ldcard of the fo
1eeba 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e 20 rm "?". Assign
1eebb 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62 6c the next variabl
1eebc 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
1eebd 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1eebe 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a ++pParse->nVar;.
1eebf 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 6b }else if( pTok
1eec0 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b en->z[0]=='?' ){
1eec1 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 . /* Wildcard
1eec2 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e of the form "?n
1eec3 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 22 6e nn". Convert "n
1eec4 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 nn" to an intege
1eec5 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73 65 r and. ** use
1eec6 20 69 74 20 61 73 20 74 68 65 20 76 61 72 69 61 it as the varia
1eec7 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ble number */.
1eec8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 45 78 int i;. pEx
1eec9 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 20 3d pr->iTable = i =
1eeca 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54 atoi((char*)&pT
1eecb 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 20 oken->z[1]);.
1eecc 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 testcase( i==0
1eecd 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1eece 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73 i==1 );. tes
1eecf 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c tcase( i==db->aL
1eed0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1eed1 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
1eed2 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 R]-1 );. test
1eed3 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 case( i==db->aLi
1eed4 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1eed5 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
1eed6 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31 ] );. if( i<1
1eed7 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 || i>db->aLimit
1eed8 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 [SQLITE_LIMIT_VA
1eed9 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 RIABLE_NUMBER] )
1eeda 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1eedb 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1eedc 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 "variable number
1eedd 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
1eede 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20 ?1 and ?%d",.
1eedf 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d db->aLim
1eee0 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1eee1 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d VARIABLE_NUMBER]
1eee2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
1eee3 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 i>pParse->nVar
1eee4 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d ){. pParse-
1eee5 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d >nVar = i;. }
1eee6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1eee7 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 Wildcards of th
1eee8 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72 e form ":aaa" or
1eee9 20 22 24 61 61 61 22 2e 20 20 52 65 75 73 65 20 "$aaa". Reuse
1eeea 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c the same variabl
1eeeb 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 e. ** number
1eeec 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 70 70 as the prior app
1eeed 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20 73 earance of the s
1eeee 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 ame name, or if
1eeef 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 the name. **
1eef0 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 72 has never appear
1eef1 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65 ed before, reuse
1eef2 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 the same variab
1eef3 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f le number. */
1eef4 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1eef5 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 70 u32 n;. n = p
1eef6 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 66 6f Token->n;. fo
1eef7 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
1eef8 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b >nVarExpr; i++){
1eef9 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 3b . Expr *pE;
1eefa 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 20 3d . if( (pE =
1eefb 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1eefc 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 20 20 pr[i])!=0.
1eefd 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e && pE->token
1eefe 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 20 20 .n==n.
1eeff 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 74 6f && memcmp(pE->to
1ef00 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 7a ken.z, pToken->z
1ef01 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 , n)==0 ){.
1ef02 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1ef03 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20 = pE->iTable;.
1ef04 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1ef05 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1ef06 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e if( i>=pParse->n
1ef07 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 VarExpr ){.
1ef08 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1ef09 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b ++pParse->nVar;
1ef0a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 . if( pPars
1ef0b 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61 e->nVarExpr>=pPa
1ef0c 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c rse->nVarExprAll
1ef0d 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 oc-1 ){.
1ef0e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
1ef0f 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d Alloc += pParse-
1ef10 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b >nVarExprAlloc +
1ef11 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 10;. pPa
1ef12 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d rse->apVarExpr =
1ef13 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1ef14 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
1ef15 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 ree(.
1ef16 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20 db,.
1ef17 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 pParse->apV
1ef18 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 arExpr,.
1ef19 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 pParse->nV
1ef1a 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 arExprAlloc*size
1ef1b 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 of(pParse->apVar
1ef1c 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20 Expr[0]).
1ef1d 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a );. }.
1ef1e 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1ef1f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1ef20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1ef21 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1ef22 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 !=0 );. p
1ef23 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1ef24 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 [pParse->nVarExp
1ef25 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20 r++] = pExpr;.
1ef26 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 }. }. }
1ef27 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
1ef28 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e nErr && pParse->
1ef29 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b nVar>db->aLimit[
1ef2a 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
1ef2b 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b IABLE_NUMBER] ){
1ef2c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1ef2d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
1ef2e 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61 o many SQL varia
1ef2f 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bles");. }.}../
1ef30 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78 *.** Clear an ex
1ef31 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 pression structu
1ef32 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 re without delet
1ef33 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 ing the structur
1ef34 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62 e itself..** Sub
1ef35 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c structure is del
1ef36 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eted..*/.SQLITE_
1ef37 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ef38 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 ite3ExprClear(sq
1ef39 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
1ef3a 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f *p){. if( p->to
1ef3b 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 ken.dyn ) sqlite
1ef3c 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 3DbFree(db, (cha
1ef3d 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a r*)p->token.z);.
1ef3e 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e if( !ExprHasAn
1ef3f 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f yProperty(p, EP_
1ef40 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 TokenOnly|EP_Spa
1ef41 6e 54 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 69 nToken) ){. i
1ef42 66 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29 f( p->span.dyn )
1ef43 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ef44 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 b, (char*)p->spa
1ef45 6e 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 45 78 n.z);. if( Ex
1ef46 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
1ef47 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a EP_Reduced) ){.
1ef48 20 20 20 20 20 20 2f 2a 20 53 75 62 74 72 65 65 /* Subtree
1ef49 73 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 s are part of th
1ef4a 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c e same memory al
1ef4b 6c 6f 63 61 74 69 6f 6e 20 77 68 65 6e 20 45 50 location when EP
1ef4c 5f 52 65 64 75 63 65 64 20 73 65 74 20 2a 2f 0a _Reduced set */.
1ef4d 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 if( p->pLe
1ef4e 66 74 20 29 20 73 71 6c 69 74 65 33 45 78 70 72 ft ) sqlite3Expr
1ef4f 43 6c 65 61 72 28 64 62 2c 20 70 2d 3e 70 4c 65 Clear(db, p->pLe
1ef50 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ft);. if( p
1ef51 2d 3e 70 52 69 67 68 74 20 29 20 73 71 6c 69 74 ->pRight ) sqlit
1ef52 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20 e3ExprClear(db,
1ef53 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 p->pRight);.
1ef54 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
1ef55 53 75 62 74 72 65 65 73 20 61 72 65 20 73 65 70 Subtrees are sep
1ef56 61 72 61 74 65 20 61 6c 6c 6f 63 61 74 69 6f 6e arate allocation
1ef57 73 20 77 68 65 6e 20 45 50 5f 52 65 64 75 63 65 s when EP_Reduce
1ef58 64 20 69 73 20 63 6c 65 61 72 20 2a 2f 0a 20 20 d is clear */.
1ef59 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
1ef5a 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 elete(db, p->pLe
1ef5b 66 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ft);. sqlit
1ef5c 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1ef5d 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 p->pRight);.
1ef5e 20 7d 0a 20 20 20 20 2f 2a 20 78 2e 70 53 65 6c }. /* x.pSel
1ef5f 65 63 74 20 61 6e 64 20 78 2e 70 4c 69 73 74 20 ect and x.pList
1ef60 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72 are always separ
1ef61 61 74 65 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ately allocated
1ef62 2a 2f 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 */. if( ExprH
1ef63 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
1ef64 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
1ef65 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
1ef66 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e ctDelete(db, p->
1ef67 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 x.pSelect);.
1ef68 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
1ef69 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
1ef6a 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 te(db, p->x.pLis
1ef6b 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a t);. }. }.}.
1ef6c 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 ./*.** Recursive
1ef6d 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70 ly delete an exp
1ef6e 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f ression tree..*/
1ef6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ef70 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1ef71 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a Delete(sqlite3 *
1ef72 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 db, Expr *p){.
1ef73 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1ef74 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 n;. sqlite3Expr
1ef75 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20 Clear(db, p);.
1ef76 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1ef77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , p);.}../*.** R
1ef78 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1ef79 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 of bytes alloca
1ef7a 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 ted for the expr
1ef7b 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 ession structure
1ef7c 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 .** passed as t
1ef7d 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1ef7e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 t. This is alway
1ef7f 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 s one of EXPR_FU
1ef80 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f LLSIZE,.** EXPR_
1ef81 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 REDUCEDSIZE or E
1ef82 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a XPR_TOKENONLYSIZ
1ef83 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 E..*/.static int
1ef84 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 exprStructSize(
1ef85 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 Expr *p){. if(
1ef86 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1ef87 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 p, EP_TokenOnly)
1ef88 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 ) return EXPR_T
1ef89 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 OKENONLYSIZE;.
1ef8a 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
1ef8b 72 74 79 28 70 2c 20 45 50 5f 53 70 61 6e 54 6f rty(p, EP_SpanTo
1ef8c 6b 65 6e 29 20 29 20 72 65 74 75 72 6e 20 45 58 ken) ) return EX
1ef8d 50 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 PR_SPANTOKENSIZE
1ef8e 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 ;. if( ExprHasP
1ef8f 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 roperty(p, EP_Re
1ef90 64 75 63 65 64 29 20 29 20 72 65 74 75 72 6e 20 duced) ) return
1ef91 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 EXPR_REDUCEDSIZE
1ef92 3b 0a 20 20 72 65 74 75 72 6e 20 45 58 50 52 5f ;. return EXPR_
1ef93 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a FULLSIZE;.}../*.
1ef94 2a 2a 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 ** sqlite3ExprDu
1ef95 70 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c p() has been cal
1ef96 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 led to create a
1ef97 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 copy of expressi
1ef98 6f 6e 20 70 20 77 69 74 68 0a 2a 2a 20 74 68 65 on p with.** the
1ef99 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 EXPRDUP_XXX fla
1ef9a 67 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 gs passed as the
1ef9b 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1ef9c 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
1ef9d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 .** returns the
1ef9e 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 space required f
1ef9f 6f 72 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 or the copy of t
1efa0 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 he Expr structur
1efa1 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 69 73 20 e only..** This
1efa2 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 is always one of
1efa3 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 EXPR_FULLSIZE,
1efa4 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 EXPR_REDUCEDSIZE
1efa5 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e or EXPR_TOKENON
1efa6 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 LYSIZE..*/.stati
1efa7 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 c int dupedExprS
1efa8 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a tructSize(Expr *
1efa9 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
1efaa 20 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 69 66 int nSize;. if
1efab 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 ( 0==(flags&EXPR
1efac 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 DUP_REDUCE) ){.
1efad 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f nSize = EXPR_
1efae 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 FULLSIZE;. }els
1efaf 65 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c e if( p->pLeft |
1efb0 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70 | p->pRight || p
1efb1 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e ->pColl || p->x.
1efb2 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 6e 53 69 pList ){. nSi
1efb3 7a 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45 ze = EXPR_REDUCE
1efb4 44 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 20 69 DSIZE;. }else i
1efb5 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 f( flags&EXPRDUP
1efb6 5f 53 50 41 4e 20 29 7b 0a 20 20 20 20 6e 53 69 _SPAN ){. nSi
1efb7 7a 65 20 3d 20 45 58 50 52 5f 53 50 41 4e 54 4f ze = EXPR_SPANTO
1efb8 4b 45 4e 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 KENSIZE;. }else
1efb9 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 {. nSize = EX
1efba 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
1efbb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
1efbc 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 Size;.}../*.** s
1efbd 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 20 qlite3ExprDup()
1efbe 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 has been called
1efbf 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1efc0 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 of expression p
1efc1 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45 58 50 with.** the EXP
1efc2 52 44 55 50 5f 58 58 58 20 70 61 73 73 65 64 20 RDUP_XXX passed
1efc3 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
1efc4 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66 75 6e gument. This fun
1efc5 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a ction returns.**
1efc6 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 62 79 the space in by
1efc7 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 tes required to
1efc8 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f store the copy o
1efc9 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 f the Expr struc
1efca 74 75 72 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ture.** and the
1efcb 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 45 78 copies of the Ex
1efcc 70 72 2e 74 6f 6b 65 6e 2e 7a 20 61 6e 64 20 45 pr.token.z and E
1efcd 78 70 72 2e 73 70 61 6e 2e 7a 20 28 69 66 20 61 xpr.span.z (if a
1efce 70 70 6c 69 63 61 62 6c 65 29 0a 2a 2a 20 73 74 pplicable).** st
1efcf 72 69 6e 67 20 62 75 66 66 65 72 73 2e 0a 2a 2f ring buffers..*/
1efd0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 .static int dupe
1efd1 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 dExprNodeSize(Ex
1efd2 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 pr *p, int flags
1efd3 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d ){. int nByte =
1efd4 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
1efd5 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 2b Size(p, flags) +
1efd6 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70 (p->token.z ? p
1efd7 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20 ->token.n + 1 :
1efd8 30 29 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73 0);. if( (flags
1efd9 26 45 58 50 52 44 55 50 5f 53 50 41 4e 29 21 3d &EXPRDUP_SPAN)!=
1efda 30 0a 20 20 20 26 26 20 28 70 2d 3e 74 6f 6b 65 0. && (p->toke
1efdb 6e 2e 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c n.z!=p->span.z |
1efdc 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d | p->token.n!=p-
1efdd 3e 73 70 61 6e 2e 6e 29 0a 20 20 29 7b 0a 20 20 >span.n). ){.
1efde 20 20 6e 42 79 74 65 20 2b 3d 20 70 2d 3e 73 70 nByte += p->sp
1efdf 61 6e 2e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 an.n;. }. retu
1efe0 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 rn ROUND8(nByte)
1efe1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1efe2 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1efe3 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 bytes required t
1efe4 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c 69 o create a dupli
1efe5 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 cate of the .**
1efe6 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 65 expression passe
1efe7 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
1efe8 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 rgument. The sec
1efe9 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
1efea 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61 69 a.** mask contai
1efeb 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58 58 ning EXPRDUP_XXX
1efec 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 flags..**.** Th
1efed 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
1efee 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65 20 includes space
1efef 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1eff0 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 of the Expr str
1eff1 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61 6e uct.** itself an
1eff2 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65 66 d the buffer ref
1eff3 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70 72 erred to by Expr
1eff4 2e 74 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 20 .token, if any.
1eff5 49 66 20 74 68 65 20 0a 2a 2a 20 45 58 50 52 44 If the .** EXPRD
1eff6 55 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20 UP_SPAN flag is
1eff7 73 65 74 2c 20 74 68 65 6e 20 73 70 61 63 65 20 set, then space
1eff8 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79 to create a copy
1eff9 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a of the buffer.*
1effa 2a 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20 * refered to by
1effb 45 78 70 72 2e 73 70 61 6e 20 69 73 20 61 6c 73 Expr.span is als
1effc 6f 20 69 6e 63 6c 75 64 65 64 2e 0a 2a 2a 0a 2a o included..**.*
1effd 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 * If the EXPRDUP
1effe 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 _REDUCE flag is
1efff 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 set, then the re
1f000 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 turn value inclu
1f001 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f des .** space to
1f002 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 duplicate all E
1f003 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 xpr nodes in the
1f004 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 tree formed by
1f005 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 Expr.pLeft .** a
1f006 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 nd Expr.pRight v
1f007 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f ariables (but no
1f008 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 t for any struct
1f009 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 ures pointed to
1f00a 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 or .** descended
1f00b 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 from the Expr.x
1f00c 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 .pList or Expr.x
1f00d 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c .pSelect variabl
1f00e 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 es)..*/.static i
1f00f 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 nt dupedExprSize
1f010 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c (Expr *p, int fl
1f011 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ags){. int nByt
1f012 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 e = 0;. if( p )
1f013 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 {. nByte = du
1f014 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 pedExprNodeSize(
1f015 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 p, flags);. i
1f016 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 f( flags&EXPRDUP
1f017 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 _REDUCE ){.
1f018 20 69 6e 74 20 66 20 3d 20 66 6c 61 67 73 26 28 int f = flags&(
1f019 7e 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a ~EXPRDUP_SPAN);.
1f01a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 nByte += d
1f01b 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e upedExprSize(p->
1f01c 70 4c 65 66 74 2c 20 66 29 20 2b 20 64 75 70 65 pLeft, f) + dupe
1f01d 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 dExprSize(p->pRi
1f01e 67 68 74 2c 20 66 29 3b 0a 20 20 20 20 7d 0a 20 ght, f);. }.
1f01f 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79 74 }. return nByt
1f020 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 e;.}../*.** This
1f021 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d function is sim
1f022 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 45 ilar to sqlite3E
1f023 78 70 72 44 75 70 28 29 2c 20 65 78 63 65 70 74 xprDup(), except
1f024 20 74 68 61 74 20 69 66 20 70 7a 42 75 66 66 65 that if pzBuffe
1f025 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c r .** is not NUL
1f026 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65 72 L then *pzBuffer
1f027 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70 is assumed to p
1f028 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
1f029 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a 2a large enough .*
1f02a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 * to store the c
1f02b 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f opy of expressio
1f02c 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73 20 n p, the copies
1f02d 6f 66 20 70 2d 3e 74 6f 6b 65 6e 20 61 6e 64 20 of p->token and
1f02e 70 2d 3e 73 70 61 6e 20 0a 2a 2a 20 28 69 66 20 p->span .** (if
1f02f 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61 6e 64 applicable), and
1f030 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 74 the copies of t
1f031 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e 64 20 he p->pLeft and
1f032 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72 65 73 p->pRight expres
1f033 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61 6e 79 sions,.** if any
1f034 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 . Before returni
1f035 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 69 73 ng, *pzBuffer is
1f036 20 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 set to the firs
1f037 74 20 62 79 74 65 20 70 61 73 73 65 64 20 74 68 t byte passed th
1f038 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66 20 e.** portion of
1f039 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 69 65 the buffer copie
1f03a 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 20 66 d into by this f
1f03b 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 unction..*/.stat
1f03c 69 63 20 45 78 70 72 20 2a 65 78 70 72 44 75 70 ic Expr *exprDup
1f03d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 (sqlite3 *db, Ex
1f03e 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 pr *p, int flags
1f03f 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65 72 29 , u8 **pzBuffer)
1f040 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d {. Expr *pNew =
1f041 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1f042 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
1f043 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 e to return */.
1f044 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 6f if( p ){. co
1f045 6e 73 74 20 69 6e 74 20 69 73 52 65 71 75 69 72 nst int isRequir
1f046 65 53 70 61 6e 20 3d 20 28 66 6c 61 67 73 26 45 eSpan = (flags&E
1f047 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20 20 XPRDUP_SPAN);.
1f048 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 52 65 const int isRe
1f049 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 26 45 duced = (flags&E
1f04a 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a XPRDUP_REDUCE);.
1f04b 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a u8 *zAlloc;.
1f04c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42 . assert( pzB
1f04d 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 uffer==0 || isRe
1f04e 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a duced );.. /*
1f04f 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 Figure out wher
1f050 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e e to write the n
1f051 65 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 ew Expr structur
1f052 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a e. */. if( pz
1f053 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 Buffer ){.
1f054 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 zAlloc = *pzBuff
1f055 65 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 er;. }else{.
1f056 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 zAlloc = sq
1f057 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
1f058 28 64 62 2c 20 64 75 70 65 64 45 78 70 72 53 69 (db, dupedExprSi
1f059 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 ze(p, flags));.
1f05a 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 }. pNew =
1f05b 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a (Expr *)zAlloc;.
1f05c 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b . if( pNew ){
1f05d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e . /* Set nN
1f05e 65 77 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 ewSize to the si
1f05f 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 ze allocated for
1f060 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 the structure p
1f061 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20 20 20 20 ointed to.
1f062 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54 68 69 73 ** by pNew. This
1f063 20 69 73 20 65 69 74 68 65 72 20 45 58 50 52 5f is either EXPR_
1f064 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 FULLSIZE, EXPR_R
1f065 45 44 55 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 EDUCEDSIZE or.
1f066 20 20 20 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 ** EXPR_TOKE
1f067 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 NONLYSIZE. nToke
1f068 6e 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 n is set to the
1f069 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1f06a 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20 20 20 2a consumed. *
1f06b 2a 20 62 79 20 74 68 65 20 63 6f 70 79 20 6f 66 * by the copy of
1f06c 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 the p->token.z
1f06d 73 74 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e string (if any).
1f06e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1f06f 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 const int nNewSi
1f070 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 ze = dupedExprSt
1f071 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 ructSize(p, flag
1f072 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 s);. const
1f073 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 28 70 2d int nToken = (p-
1f074 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70 2d 3e 74 6f >token.z ? p->to
1f075 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20 30 29 3b 0a ken.n + 1 : 0);.
1f076 20 20 20 20 20 20 69 66 28 20 69 73 52 65 64 75 if( isRedu
1f077 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ced ){. a
1f078 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 ssert( ExprHasPr
1f079 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 operty(p, EP_Red
1f07a 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 uced)==0 );.
1f07b 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f memcpy(zAllo
1f07c 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 3b c, p, nNewSize);
1f07d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1f07e 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 20 int nSize
1f07f 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 = exprStructSize
1f080 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d (p);. mem
1f081 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e cpy(zAlloc, p, n
1f082 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 6d Size);. m
1f083 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53 emset(&zAlloc[nS
1f084 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46 55 ize], 0, EXPR_FU
1f085 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20 LLSIZE-nSize);.
1f086 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
1f087 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 64 75 Set the EP_Redu
1f088 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e ced and EP_Token
1f089 4f 6e 6c 79 20 66 6c 61 67 73 20 61 70 70 72 6f Only flags appro
1f08a 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 priately. */.
1f08b 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 pNew->flags &
1f08c 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45 = ~(EP_Reduced|E
1f08d 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 P_TokenOnly|EP_S
1f08e 70 61 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 panToken);.
1f08f 20 73 77 69 74 63 68 28 20 6e 4e 65 77 53 69 7a switch( nNewSiz
1f090 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 e ){. cas
1f091 65 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 e EXPR_REDUCEDSI
1f092 5a 45 3a 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 ZE: pNew->flag
1f093 73 20 7c 3d 20 45 50 5f 52 65 64 75 63 65 64 3b s |= EP_Reduced;
1f094 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1f095 63 61 73 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f case EXPR_TOKENO
1f096 4e 4c 59 53 49 5a 45 3a 20 70 4e 65 77 2d 3e 66 NLYSIZE: pNew->f
1f097 6c 61 67 73 20 7c 3d 20 45 50 5f 54 6f 6b 65 6e lags |= EP_Token
1f098 4f 6e 6c 79 3b 20 62 72 65 61 6b 3b 0a 20 20 20 Only; break;.
1f099 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f 53 case EXPR_S
1f09a 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 3a 20 70 4e PANTOKENSIZE: pN
1f09b 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ew->flags |= EP_
1f09c 53 70 61 6e 54 6f 6b 65 6e 3b 20 62 72 65 61 6b SpanToken; break
1f09d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1f09e 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e /* Copy the p->
1f09f 74 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 token string, if
1f0a0 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 any. */. i
1f0a1 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 f( nToken ){.
1f0a2 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
1f0a3 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 26 7a 41 ar *zToken = &zA
1f0a4 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a lloc[nNewSize];.
1f0a5 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
1f0a6 54 6f 6b 65 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e Token, p->token.
1f0a7 7a 2c 20 6e 54 6f 6b 65 6e 2d 31 29 3b 0a 20 20 z, nToken-1);.
1f0a8 20 20 20 20 20 20 7a 54 6f 6b 65 6e 5b 6e 54 6f zToken[nTo
1f0a9 6b 65 6e 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 ken-1] = '\0';.
1f0aa 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b pNew->tok
1f0ab 65 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20 en.dyn = 0;.
1f0ac 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e pNew->token.
1f0ad 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 z = zToken;.
1f0ae 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 }.. if( 0
1f0af 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 ==((p->flags|pNe
1f0b0 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 w->flags) & EP_T
1f0b1 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 okenOnly) ){.
1f0b2 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 /* Fill in
1f0b3 74 68 65 20 70 4e 65 77 2d 3e 73 70 61 6e 20 74 the pNew->span t
1f0b4 6f 6b 65 6e 2c 20 69 66 20 72 65 71 75 69 72 65 oken, if require
1f0b5 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 d. */. if
1f0b6 28 20 69 73 52 65 71 75 69 72 65 53 70 61 6e 20 ( isRequireSpan
1f0b7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1f0b8 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 70 2d 3e p->token.z!=p->
1f0b9 73 70 61 6e 2e 7a 20 7c 7c 20 70 2d 3e 74 6f 6b span.z || p->tok
1f0ba 65 6e 2e 6e 21 3d 70 2d 3e 73 70 61 6e 2e 6e 20 en.n!=p->span.n
1f0bb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
1f0bc 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 26 7a New->span.z = &z
1f0bd 41 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 2b 6e Alloc[nNewSize+n
1f0be 54 6f 6b 65 6e 5d 3b 0a 20 20 20 20 20 20 20 20 Token];.
1f0bf 20 20 20 20 6d 65 6d 63 70 79 28 28 63 68 61 72 memcpy((char
1f0c0 20 2a 29 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 2c *)pNew->span.z,
1f0c1 20 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 p->span.z, p->s
1f0c2 70 61 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20 pan.n);.
1f0c3 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 64 pNew->span.d
1f0c4 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 yn = 0;.
1f0c5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1f0c6 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e pNew->span.
1f0c7 7a 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e z = pNew->token.
1f0c8 7a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 z;. p
1f0c9 4e 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 4e New->span.n = pN
1f0ca 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 ew->token.n;.
1f0cb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f0cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1f0cd 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d pNew->span.z =
1f0ce 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 0;. pN
1f0cf 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 30 3b 0a ew->span.n = 0;.
1f0d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1f0d1 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d }.. if( 0==
1f0d2 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d ((p->flags|pNew-
1f0d3 3e 66 6c 61 67 73 29 20 26 20 28 45 50 5f 54 6f >flags) & (EP_To
1f0d4 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 kenOnly|EP_SpanT
1f0d5 6f 6b 65 6e 29 29 20 29 7b 0a 20 20 20 20 20 20 oken)) ){.
1f0d6 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
1f0d7 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 pNew->x.pSelect
1f0d8 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 or pNew->x.pLis
1f0d9 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 t member. */.
1f0da 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
1f0db 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 Property(p, EP_x
1f0dc 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
1f0dd 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 pNew->x.p
1f0de 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 Select = sqlite3
1f0df 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d SelectDup(db, p-
1f0e0 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 >x.pSelect, isRe
1f0e1 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 duced);.
1f0e2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1f0e3 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d pNew->x.pList =
1f0e4 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
1f0e5 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 Dup(db, p->x.pLi
1f0e6 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a st, isReduced);.
1f0e7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1f0e8 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c }.. /* Fill
1f0e9 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 in pNew->pLeft
1f0ea 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 and pNew->pRight
1f0eb 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 . */. if( E
1f0ec 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
1f0ed 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 y(pNew, EP_Reduc
1f0ee 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c ed|EP_TokenOnly|
1f0ef 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 7b EP_SpanToken) ){
1f0f0 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 . zAlloc
1f0f1 2b 3d 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 += dupedExprNode
1f0f2 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a Size(p, flags);.
1f0f3 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72 if( Expr
1f0f4 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 HasProperty(pNew
1f0f5 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b , EP_Reduced) ){
1f0f6 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d . pNew-
1f0f7 3e 70 4c 65 66 74 20 3d 20 65 78 70 72 44 75 70 >pLeft = exprDup
1f0f8 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 (db, p->pLeft, E
1f0f9 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 XPRDUP_REDUCE, &
1f0fa 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 zAlloc);.
1f0fb 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 pNew->pRight
1f0fc 3d 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d = exprDup(db, p-
1f0fd 3e 70 52 69 67 68 74 2c 20 45 58 50 52 44 55 50 >pRight, EXPRDUP
1f0fe 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 _REDUCE, &zAlloc
1f0ff 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1f100 20 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 if( pzBuffe
1f101 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a r ){. *
1f102 70 7a 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f pzBuffer = zAllo
1f103 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
1f104 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 45 78 }else if( !Ex
1f105 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
1f106 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 (p, EP_TokenOnly
1f107 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 |EP_SpanToken) )
1f108 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e {. pNew->
1f109 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 pLeft = sqlite3E
1f10a 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c xprDup(db, p->pL
1f10b 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 eft, 0);.
1f10c 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 pNew->pRight =
1f10d 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
1f10e 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 b, p->pRight, 0)
1f10f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
1f111 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 w;.}../*.** The
1f112 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f 75 70 20 following group
1f113 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 of routines make
1f114 20 64 65 65 70 20 63 6f 70 69 65 73 20 6f 66 20 deep copies of
1f115 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 expressions,.**
1f116 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 73 expression lists
1f117 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 6e 64 20 , ID lists, and
1f118 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 select statement
1f119 73 2e 20 20 54 68 65 20 63 6f 70 69 65 73 20 63 s. The copies c
1f11a 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 74 65 64 an.** be deleted
1f11b 20 28 62 79 20 62 65 69 6e 67 20 70 61 73 73 65 (by being passe
1f11c 64 20 74 6f 20 74 68 65 69 72 20 72 65 73 70 65 d to their respe
1f11d 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 74 65 28 ctive ...Delete(
1f11e 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a 2a 20 77 ) routines).** w
1f11f 69 74 68 6f 75 74 20 65 66 66 65 63 74 69 6e 67 ithout effecting
1f120 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 73 2e 0a the originals..
1f121 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 **.** The expres
1f122 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 2c 20 61 sion list, ID, a
1f123 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 74 73 20 nd source lists
1f124 72 65 74 75 72 6e 20 62 79 20 73 71 6c 69 74 65 return by sqlite
1f125 33 45 78 70 72 4c 69 73 74 44 75 70 28 29 2c 0a 3ExprListDup(),.
1f126 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 ** sqlite3IdList
1f127 44 75 70 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 Dup(), and sqlit
1f128 65 33 53 72 63 4c 69 73 74 44 75 70 28 29 20 63 e3SrcListDup() c
1f129 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 74 68 65 an not be furthe
1f12a 72 20 65 78 70 61 6e 64 65 64 20 0a 2a 2a 20 62 r expanded .** b
1f12b 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c y subsequent cal
1f12c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a 4c 69 73 ls to sqlite*Lis
1f12d 74 41 70 70 65 6e 64 28 29 20 72 6f 75 74 69 6e tAppend() routin
1f12e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 es..**.** Any ta
1f12f 62 6c 65 73 20 74 68 61 74 20 74 68 65 20 53 72 bles that the Sr
1f130 63 4c 69 73 74 20 6d 69 67 68 74 20 70 6f 69 6e cList might poin
1f131 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 64 75 70 t to are not dup
1f132 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 licated..**.** T
1f133 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
1f134 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f er contains a co
1f135 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 mbination of the
1f136 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 EXPRDUP_XXX fla
1f137 67 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 45 58 gs. If.** the EX
1f138 50 52 44 55 50 5f 53 50 41 4e 20 66 6c 61 67 20 PRDUP_SPAN flag
1f139 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 61 72 is set in the ar
1f13a 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 gument parameter
1f13b 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 45 , then the .** E
1f13c 78 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f xpr.span field o
1f13d 66 20 74 68 65 20 69 6e 70 75 74 20 65 78 70 72 f the input expr
1f13e 65 73 73 69 6f 6e 20 69 73 20 63 6f 70 69 65 64 ession is copied
1f13f 2e 20 49 66 20 45 58 50 52 44 55 50 5f 53 50 41 . If EXPRDUP_SPA
1f140 4e 20 69 73 0a 2a 2a 20 63 6c 65 61 72 2c 20 74 N is.** clear, t
1f141 68 65 6e 20 74 68 65 20 45 78 70 72 2e 73 70 61 hen the Expr.spa
1f142 6e 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72 n field of the r
1f143 65 74 75 72 6e 65 64 20 65 78 70 72 65 73 73 69 eturned expressi
1f144 6f 6e 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 on structure.**
1f145 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a is zeroed..**.**
1f146 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f If the EXPRDUP_
1f147 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 REDUCE flag is s
1f148 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
1f149 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 ucture returned
1f14a 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 is a.** truncate
1f14b 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 d version of the
1f14c 20 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 usual Expr stru
1f14d 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 cture that will
1f14e 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 be stored as.**
1f14f 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d part of the in-m
1f150 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 emory representa
1f151 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 tion of the data
1f152 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a base schema..*/.
1f153 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
1f154 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 xpr *sqlite3Expr
1f155 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
1f156 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c Expr *p, int fl
1f157 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 ags){. return e
1f158 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c xprDup(db, p, fl
1f159 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 ags, 0);.}.SQLIT
1f15a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1f15b 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 qlite3TokenCopy(
1f15c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b sqlite3 *db, Tok
1f15d 65 6e 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 54 en *pTo, const T
1f15e 6f 6b 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 oken *pFrom){.
1f15f 69 66 28 20 70 54 6f 2d 3e 64 79 6e 20 29 20 73 if( pTo->dyn ) s
1f160 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f161 20 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b (char*)pTo->z);
1f162 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 . if( pFrom->z
1f163 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 ){. pTo->n =
1f164 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 pFrom->n;. pT
1f165 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 o->z = (u8*)sqli
1f166 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c te3DbStrNDup(db,
1f167 20 28 63 68 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a (char*)pFrom->z
1f168 2c 20 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 , pFrom->n);.
1f169 20 70 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 pTo->dyn = 1;.
1f16a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 2d }else{. pTo-
1f16b 3e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 >z = 0;. }.}.SQ
1f16c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
1f16d 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 rList *sqlite3Ex
1f16e 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 prListDup(sqlite
1f16f 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 3 *db, ExprList
1f170 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
1f171 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 ExprList *pNew
1f172 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c ;. struct ExprL
1f173 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c ist_item *pItem,
1f174 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e *pOldItem;. in
1f175 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 t i;. if( p==0
1f176 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
1f177 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
1f178 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
1f179 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 of(*pNew) );. i
1f17a 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 f( pNew==0 ) ret
1f17b 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 urn 0;. pNew->i
1f17c 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 ECursor = 0;. p
1f17d 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 New->nExpr = pNe
1f17e 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e w->nAlloc = p->n
1f17f 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 Expr;. pNew->a
1f180 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 = pItem = sqlite
1f181 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1f182 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f p->nExpr*sizeo
1f183 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 f(p->a[0]) );.
1f184 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a if( pItem==0 ){.
1f185 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f186 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 e(db, pNew);.
1f187 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a return 0;. } .
1f188 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e pOldItem = p->
1f189 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c a;. for(i=0; i<
1f18a 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 p->nExpr; i++, p
1f18b 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d Item++, pOldItem
1f18c 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 ++){. Expr *p
1f18d 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 45 78 70 NewExpr;. Exp
1f18e 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20 70 4f r *pOldExpr = pO
1f18f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 ldItem->pExpr;.
1f190 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 pItem->pExpr
1f191 3d 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c = pNewExpr = sql
1f192 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
1f193 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29 pOldExpr, flags)
1f194 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 ;. pItem->zNa
1f195 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
1f196 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
1f197 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 m->zName);. p
1f198 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 Item->sortOrder
1f199 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 = pOldItem->sort
1f19a 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d Order;. pItem
1f19b 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 ->done = 0;.
1f19c 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f pItem->iCol = pO
1f19d 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 ldItem->iCol;.
1f19e 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 pItem->iAlias
1f19f 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 = pOldItem->iAli
1f1a0 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e as;. }. return
1f1a1 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
1f1a2 49 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67 If cursors, trig
1f1a3 67 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20 gers, views and
1f1a4 73 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61 subqueries are a
1f1a5 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a ll omitted from.
1f1a6 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68 ** the build, th
1f1a7 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 en none of the f
1f1a8 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
1f1a9 73 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a s, except for .*
1f1aa 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 * sqlite3SelectD
1f1ab 75 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c up(), can be cal
1f1ac 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65 led. sqlite3Sele
1f1ad 63 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74 ctDup() is somet
1f1ae 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 imes.** called w
1f1af 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d ith a NULL argum
1f1b0 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ent..*/.#if !def
1f1b1 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1f1b2 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e _VIEW) || !defin
1f1b3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ed(SQLITE_OMIT_T
1f1b4 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64 RIGGER) \. || !d
1f1b5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1f1b6 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c IT_SUBQUERY).SQL
1f1b7 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c ITE_PRIVATE SrcL
1f1b8 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
1f1b9 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a istDup(sqlite3 *
1f1ba 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 db, SrcList *p,
1f1bb 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72 int flags){. Sr
1f1bc 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 cList *pNew;. i
1f1bd 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 nt i;. int nByt
1f1be 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 e;. if( p==0 )
1f1bf 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 return 0;. nByt
1f1c0 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b e = sizeof(*p) +
1f1c1 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 (p->nSrc>0 ? si
1f1c2 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 zeof(p->a[0]) *
1f1c3 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 (p->nSrc-1) : 0)
1f1c4 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
1f1c5 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
1f1c6 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 , nByte );. if(
1f1c7 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 pNew==0 ) retur
1f1c8 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 n 0;. pNew->nSr
1f1c9 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 c = pNew->nAlloc
1f1ca 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f = p->nSrc;. fo
1f1cb 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 r(i=0; i<p->nSrc
1f1cc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
1f1cd 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
1f1ce 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 *pNewItem = &pNe
1f1cf 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 w->a[i];. str
1f1d0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1f1d1 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d *pOldItem = &p-
1f1d2 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 >a[i];. Table
1f1d3 20 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 *pTab;. pNew
1f1d4 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 Item->zDatabase
1f1d5 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1f1d6 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
1f1d7 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 zDatabase);.
1f1d8 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pNewItem->zName
1f1d9 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1f1da 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
1f1db 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 zName);. pNew
1f1dc 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 Item->zAlias = s
1f1dd 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
1f1de 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c b, pOldItem->zAl
1f1df 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ias);. pNewIt
1f1e0 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 em->jointype = p
1f1e1 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 OldItem->jointyp
1f1e2 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d e;. pNewItem-
1f1e3 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 >iCursor = pOldI
1f1e4 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1f1e5 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f pNewItem->isPo
1f1e6 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 pulated = pOldIt
1f1e7 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b em->isPopulated;
1f1e8 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a . pNewItem->z
1f1e9 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 Index = sqlite3D
1f1ea 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
1f1eb 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 Item->zIndex);.
1f1ec 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 pNewItem->not
1f1ed 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 Indexed = pOldIt
1f1ee 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a em->notIndexed;.
1f1ef 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 pNewItem->pI
1f1f0 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d ndex = pOldItem-
1f1f1 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 >pIndex;. pTa
1f1f2 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 b = pNewItem->pT
1f1f3 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 ab = pOldItem->p
1f1f4 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 Tab;. if( pTa
1f1f5 62 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d b ){. pTab-
1f1f6 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 >nRef++;. }.
1f1f7 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 pNewItem->pSe
1f1f8 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 lect = sqlite3Se
1f1f9 6c 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 lectDup(db, pOld
1f1fa 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66 Item->pSelect, f
1f1fb 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 lags);. pNewI
1f1fc 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 tem->pOn = sqlit
1f1fd 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f e3ExprDup(db, pO
1f1fe 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 ldItem->pOn, fla
1f1ff 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 gs);. pNewIte
1f200 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 m->pUsing = sqli
1f201 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c te3IdListDup(db,
1f202 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e pOldItem->pUsin
1f203 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d g);. pNewItem
1f204 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 ->colUsed = pOld
1f205 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 Item->colUsed;.
1f206 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
1f207 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1f208 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 TE IdList *sqlit
1f209 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 e3IdListDup(sqli
1f20a 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 te3 *db, IdList
1f20b 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 *p){. IdList *p
1f20c 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 New;. int i;.
1f20d 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1f20e 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
1f20f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
1f210 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 (db, sizeof(*pNe
1f211 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 w) );. if( pNew
1f212 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1f213 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e pNew->nId = pN
1f214 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
1f215 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d nId;. pNew->a =
1f216 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1f217 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 Raw(db, p->nId*s
1f218 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 izeof(p->a[0]) )
1f219 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d ;. if( pNew->a=
1f21a 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1f21b 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 3DbFree(db, pNew
1f21c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
1f21d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
1f21e 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a i<p->nId; i++){.
1f21f 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
1f220 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d t_item *pNewItem
1f221 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a = &pNew->a[i];.
1f222 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 struct IdLis
1f223 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d t_item *pOldItem
1f224 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 = &p->a[i];.
1f225 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pNewItem->zName
1f226 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
1f227 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
1f228 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 >zName);. pNe
1f229 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c wItem->idx = pOl
1f22a 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a dItem->idx;. }.
1f22b 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
1f22c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f22d 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 Select *sqlite3S
1f22e 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 electDup(sqlite3
1f22f 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c *db, Select *p,
1f230 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 int flags){. S
1f231 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 elect *pNew;. i
1f232 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
1f233 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 0;. pNew = sql
1f234 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1f235 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 db, sizeof(*p) )
1f236 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
1f237 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 2f 2a ) return 0;. /*
1f238 20 41 6c 77 61 79 73 20 6d 61 6b 65 20 61 20 63 Always make a c
1f239 6f 70 79 20 6f 66 20 74 68 65 20 73 70 61 6e 20 opy of the span
1f23a 66 6f 72 20 74 6f 70 2d 6c 65 76 65 6c 20 65 78 for top-level ex
1f23b 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 pressions in the
1f23c 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e . ** expression
1f23d 20 6c 69 73 74 2e 20 20 54 68 65 20 6c 6f 67 69 list. The logi
1f23e 63 20 69 6e 20 53 45 4c 45 43 54 20 70 72 6f 63 c in SELECT proc
1f23f 65 73 73 69 6e 67 20 74 68 61 74 20 64 65 74 65 essing that dete
1f240 72 6d 69 6e 65 73 0a 20 20 2a 2a 20 74 68 65 20 rmines. ** the
1f241 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 names of columns
1f242 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1f243 65 74 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e et needs this in
1f244 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 70 formation */. p
1f245 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 New->pEList = sq
1f246 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
1f247 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 (db, p->pEList,
1f248 66 6c 61 67 73 7c 45 58 50 52 44 55 50 5f 53 50 flags|EXPRDUP_SP
1f249 41 4e 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72 AN);. pNew->pSr
1f24a 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 c = sqlite3SrcLi
1f24b 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 72 stDup(db, p->pSr
1f24c 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 c, flags);. pNe
1f24d 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 w->pWhere = sqli
1f24e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1f24f 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67 73 29 ->pWhere, flags)
1f250 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 ;. pNew->pGroup
1f251 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 By = sqlite3Expr
1f252 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 ListDup(db, p->p
1f253 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73 29 3b GroupBy, flags);
1f254 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67 . pNew->pHaving
1f255 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
1f256 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 p(db, p->pHaving
1f257 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
1f258 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c ->pOrderBy = sql
1f259 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
1f25a 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c db, p->pOrderBy,
1f25b 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
1f25c 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 >op = p->op;. p
1f25d 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 73 71 New->pPrior = sq
1f25e 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 lite3SelectDup(d
1f25f 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 66 6c b, p->pPrior, fl
1f260 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c ags);. pNew->pL
1f261 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 78 imit = sqlite3Ex
1f262 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 69 prDup(db, p->pLi
1f263 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 mit, flags);. p
1f264 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 73 New->pOffset = s
1f265 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1f266 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 66 6c , p->pOffset, fl
1f267 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c ags);. pNew->iL
1f268 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 imit = 0;. pNew
1f269 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 ->iOffset = 0;.
1f26a 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 pNew->selFlags
1f26b 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 = p->selFlags &
1f26c 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 ~SF_UsesEphemera
1f26d 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 l;. pNew->pRigh
1f26e 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 tmost = 0;. pNe
1f26f 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b w->addrOpenEphm[
1f270 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 0] = -1;. pNew-
1f271 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d >addrOpenEphm[1]
1f272 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 = -1;. pNew->a
1f273 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d ddrOpenEphm[2] =
1f274 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e -1;. return pN
1f275 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 ew;.}.#else.SQLI
1f276 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 TE_PRIVATE Selec
1f277 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 t *sqlite3Select
1f278 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
1f279 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 Select *p, int
1f27a 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 72 74 flags){. assert
1f27b 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 ( p==0 );. retu
1f27c 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a rn 0;.}.#endif..
1f27d 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
1f27e 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 element to the
1f27f 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72 65 73 end of an expres
1f280 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 70 sion list. If p
1f281 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 74 69 List is.** initi
1f282 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 ally NULL, then
1f283 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 78 70 create a new exp
1f284 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f ression list..*/
1f285 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f286 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 ExprList *sqlite
1f287 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 3ExprListAppend(
1f288 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1f289 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
1f28a 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1f28b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
1f28c 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 st, /* Li
1f28d 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 st to which to a
1f28e 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 ppend. Might be
1f28f 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a NULL */. Expr *
1f290 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 pExpr,
1f291 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
1f292 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 2a to be appended *
1f293 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
1f294 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1f295 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 20 74 68 S keyword for th
1f296 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a e expression */.
1f297 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1f298 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1f299 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b if( pList==0 ){
1f29a 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c . pList = sql
1f29b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1f29c 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 (db, sizeof(Expr
1f29d 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28 List) );. if(
1f29e 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
1f29f 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1f2a0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1f2a1 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d ( pList->nAlloc=
1f2a2 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 =0 );. }. if(
1f2a3 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 pList->nAlloc<=p
1f2a4 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 List->nExpr ){.
1f2a5 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
1f2a6 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20 st_item *a;.
1f2a7 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e int n = pList->n
1f2a8 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20 Alloc*2 + 4;.
1f2a9 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 a = sqlite3DbRe
1f2aa 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d alloc(db, pList-
1f2ab 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 >a, n*sizeof(pLi
1f2ac 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20 st->a[0]));.
1f2ad 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20 if( a==0 ){.
1f2ae 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1f2af 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e }. pList->
1f2b0 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73 74 a = a;. pList
1f2b1 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 ->nAlloc = sqlit
1f2b2 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 e3DbMallocSize(d
1f2b3 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30 b, a)/sizeof(a[0
1f2b4 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ]);. }. assert
1f2b5 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b ( pList->a!=0 );
1f2b6 0a 20 20 69 66 28 20 70 45 78 70 72 20 7c 7c 20 . if( pExpr ||
1f2b7 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73 74 72 pName ){. str
1f2b8 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1f2b9 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 m *pItem = &pLis
1f2ba 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 t->a[pList->nExp
1f2bb 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 r++];. memset
1f2bc 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f (pItem, 0, sizeo
1f2bd 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 f(*pItem));.
1f2be 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 pItem->zName = s
1f2bf 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1f2c0 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a ken(db, pName);.
1f2c1 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 pItem->pExpr
1f2c2 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 70 49 = pExpr;. pI
1f2c3 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b tem->iAlias = 0;
1f2c4 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c . }. return pL
1f2c5 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 ist;..no_mem:
1f2c6 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 . /* Avoid le
1f2c7 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 aking memory if
1f2c8 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 malloc has faile
1f2c9 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 d. */. sqlite3E
1f2ca 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 xprDelete(db, pE
1f2cb 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 xpr);. sqlite3E
1f2cc 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
1f2cd 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 , pList);. retu
1f2ce 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 rn 0;.}../*.** I
1f2cf 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1f2d0 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f 6e list pEList con
1f2d1 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 tains more than
1f2d2 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c iLimit elements,
1f2d3 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 .** leave an err
1f2d4 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 or message in pP
1f2d5 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f arse..*/.SQLITE_
1f2d6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f2d7 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 ite3ExprListChec
1f2d8 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 65 kLength(. Parse
1f2d9 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 72 *pParse,. Expr
1f2da 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 List *pEList,.
1f2db 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a const char *zObj
1f2dc 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 ect.){. int mx
1f2dd 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c = pParse->db->aL
1f2de 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1f2df 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 T_COLUMN];. tes
1f2e0 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 tcase( pEList &&
1f2e1 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d pEList->nExpr==
1f2e2 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 mx );. testcase
1f2e3 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 ( pEList && pELi
1f2e4 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 st->nExpr==mx+1
1f2e5 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 );. if( pEList
1f2e6 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 && pEList->nExpr
1f2e7 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >mx ){. sqlit
1f2e8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1f2e9 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c e, "too many col
1f2ea 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 umns in %s", zOb
1f2eb 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ject);. }.}../*
1f2ec 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e .** Delete an en
1f2ed 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 tire expression
1f2ee 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f list..*/.SQLITE_
1f2ef 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f2f0 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
1f2f1 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 te(sqlite3 *db,
1f2f2 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 ExprList *pList)
1f2f3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 {. int i;. str
1f2f4 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
1f2f5 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 m *pItem;. if(
1f2f6 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 pList==0 ) retur
1f2f7 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 n;. assert( pLi
1f2f8 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69 st->a!=0 || (pLi
1f2f9 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20 st->nExpr==0 &&
1f2fa 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 pList->nAlloc==0
1f2fb 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1f2fc 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 List->nExpr<=pLi
1f2fd 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 st->nAlloc );.
1f2fe 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d for(pItem=pList-
1f2ff 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 >a, i=0; i<pList
1f300 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
1f301 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 tem++){. sqli
1f302 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1f303 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b , pItem->pExpr);
1f304 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1f305 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e ee(db, pItem->zN
1f306 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ame);. }. sqli
1f307 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
1f308 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 ist->a);. sqlit
1f309 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 e3DbFree(db, pLi
1f30a 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 st);.}../*.** Th
1f30b 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
1f30c 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b Walker callback
1f30d 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20 s. Walker.u.pi
1f30e 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 is a pointer.**
1f30f 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 to an integer.
1f310 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
1f311 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 65 re checking an e
1f312 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 65 xpression to see
1f313 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 63 .** if it is a c
1f314 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a 57 onstant. Set *W
1f315 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 20 alker.u.pi to 0
1f316 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
1f317 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 n is.** not cons
1f318 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tant..**.** Thes
1f319 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 e callback routi
1f31a 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 nes are used to
1f31b 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f implement the fo
1f31c 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
1f31d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 sqlite3ExprIs
1f31e 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20 Constant().**
1f31f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 sqlite3ExprIsC
1f320 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 onstantNotJoin()
1f321 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 .** sqlite3E
1f322 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 xprIsConstantOrF
1f323 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a unction().**.*/.
1f324 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 4e static int exprN
1f325 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 odeIsConstant(Wa
1f326 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 lker *pWalker, E
1f327 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20 xpr *pExpr){..
1f328 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75 /* If pWalker->u
1f329 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e 79 .i is 3 then any
1f32a 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 70 term of the exp
1f32b 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d ression that com
1f32c 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 es from. ** the
1f32d 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
1f32e 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 uses of a join d
1f32f 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 20 isqualifies the
1f330 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 expression. **
1f331 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 from being consi
1f332 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 dered constant.
1f333 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 72 */. if( pWalker
1f334 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 72 ->u.i==3 && Expr
1f335 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
1f336 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 Expr, EP_FromJoi
1f337 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65 n) ){. pWalke
1f338 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 r->u.i = 0;.
1f339 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1f33a 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 ;. }.. switch(
1f33b 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
1f33c 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 /* Consider fu
1f33d 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f nctions to be co
1f33e 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 nstant if all th
1f33f 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72 eir arguments ar
1f340 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a e constant. *
1f341 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 * and pWalker->u
1f342 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 .i==2 */. cas
1f343 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 e TK_FUNCTION:.
1f344 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72 if( pWalker
1f345 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72 ->u.i==2 ) retur
1f346 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 n 0;. /* Fa
1f347 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 ll through */.
1f348 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 case TK_ID:.
1f349 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e case TK_COLUMN
1f34a 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 :. case TK_AG
1f34b 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 G_FUNCTION:.
1f34c 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 case TK_AGG_COLU
1f34d 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 MN:.#ifndef SQLI
1f34e 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1f34f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c . case TK_SEL
1f350 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b ECT:. case TK
1f351 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 74 _EXISTS:. t
1f352 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
1f353 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b op==TK_SELECT );
1f354 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f355 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 pExpr->op==TK_E
1f356 58 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a XISTS );.#endif.
1f357 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1f358 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 pExpr->op==TK_ID
1f359 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f35a 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
1f35b 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 K_COLUMN );.
1f35c 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
1f35d 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 r->op==TK_AGG_FU
1f35e 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 NCTION );.
1f35f 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
1f360 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 >op==TK_AGG_COLU
1f361 4d 4e 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c MN );. pWal
1f362 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 ker->u.i = 0;.
1f363 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1f364 62 6f 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c bort;. defaul
1f365 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 t:. return
1f366 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1f367 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 }.}.static int s
1f368 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 electNodeIsConst
1f369 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ant(Walker *pWal
1f36a 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 ker, Select *Not
1f36b 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
1f36c 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
1f36d 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 d);. pWalker->u
1f36e 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e .i = 0;. return
1f36f 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 WRC_Abort;.}.st
1f370 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73 43 atic int exprIsC
1f371 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e onst(Expr *p, in
1f372 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 t initFlag){. W
1f373 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 alker w;. w.u.i
1f374 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 = initFlag;. w
1f375 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d .xExprCallback =
1f376 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 exprNodeIsConst
1f377 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 ant;. w.xSelect
1f378 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 Callback = selec
1f379 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b tNodeIsConstant;
1f37a 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 . sqlite3WalkEx
1f37b 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 pr(&w, p);. ret
1f37c 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a urn w.u.i;.}../*
1f37d 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 .** Walk an expr
1f37e 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
1f37f 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 turn 1 if the ex
1f380 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 pression is cons
1f381 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 tant.** and 0 if
1f382 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 it involves var
1f383 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 iables or functi
1f384 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 on calls..**.**
1f385 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
1f386 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
1f387 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 n, a double-quot
1f388 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 ed string (ex: "
1f389 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 abc").** is cons
1f38a 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62 6c idered a variabl
1f38b 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 e but a single-q
1f38c 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 uoted string (ex
1f38d 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 : 'abc') is.** a
1f38e 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 constant..*/.SQ
1f38f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1f390 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
1f391 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b nstant(Expr *p){
1f392 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 . return exprIs
1f393 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a Const(p, 1);.}..
1f394 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
1f395 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
1f396 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
1f397 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f expression is co
1f398 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 nstant.** that d
1f399 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 oes no originate
1f39a 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 from the ON or
1f39b 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 USING clauses of
1f39c 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 a join..** Retu
1f39d 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c rn 0 if it invol
1f39e 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 ves variables or
1f39f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 function calls
1f3a0 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a or terms from.**
1f3a1 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 an ON or USING
1f3a2 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 clause..*/.SQLIT
1f3a3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f3a4 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1f3a5 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 antNotJoin(Expr
1f3a6 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 *p){. return ex
1f3a7 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b prIsConst(p, 3);
1f3a8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 .}../*.** Walk a
1f3a9 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 n expression tre
1f3aa 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 e. Return 1 if
1f3ab 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1f3ac 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 s constant.** or
1f3ad 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c a function call
1f3ae 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 with constant a
1f3af 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 rguments. Retur
1f3b0 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72 65 n and 0 if there
1f3b1 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69 .** are any vari
1f3b2 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ables..**.** For
1f3b3 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
1f3b4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
1f3b5 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 a double-quoted
1f3b6 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 string (ex: "abc
1f3b7 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 ").** is conside
1f3b8 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 red a variable b
1f3b9 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 ut a single-quot
1f3ba 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 ed string (ex: '
1f3bb 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f abc') is.** a co
1f3bc 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 nstant..*/.SQLIT
1f3bd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f3be 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1f3bf 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 antOrFunction(Ex
1f3c0 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e pr *p){. return
1f3c1 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 exprIsConst(p,
1f3c2 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2);.}../*.** If
1f3c3 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 the expression p
1f3c4 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e codes a constan
1f3c5 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 t integer that i
1f3c6 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a s small enough.*
1f3c7 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32 * to fit in a 32
1f3c8 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 -bit integer, re
1f3c9 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74 turn 1 and put t
1f3ca 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1f3cb 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 integer.** in *p
1f3cc 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65 Value. If the e
1f3cd 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 xpression is not
1f3ce 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69 an integer or i
1f3cf 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a f it is too big.
1f3d0 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73 ** to fit in a s
1f3d1 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74 igned 32-bit int
1f3d2 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61 eger, return 0 a
1f3d3 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 nd leave *pValue
1f3d4 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 unchanged..*/.S
1f3d5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1f3d6 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 t sqlite3ExprIsI
1f3d7 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20 nteger(Expr *p,
1f3d8 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 int *pValue){.
1f3d9 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 int rc = 0;. if
1f3da 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f ( p->flags & EP_
1f3db 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 IntValue ){.
1f3dc 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 69 54 61 *pValue = p->iTa
1f3dd 62 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ble;. return
1f3de 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 1;. }. switch(
1f3df 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 p->op ){. ca
1f3e0 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b se TK_INTEGER: {
1f3e1 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1f3e2 74 65 33 47 65 74 49 6e 74 33 32 28 28 63 68 61 te3GetInt32((cha
1f3e3 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 r*)p->token.z, p
1f3e4 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72 Value);. br
1f3e5 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1f3e6 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a ase TK_UPLUS: {.
1f3e7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1f3e8 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
1f3e9 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 p->pLeft, pValue
1f3ea 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1f3eb 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1f3ec 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 K_UMINUS: {.
1f3ed 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 int v;. i
1f3ee 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
1f3ef 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 Integer(p->pLeft
1f3f0 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20 , &v) ){.
1f3f1 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 *pValue = -v;.
1f3f2 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 rc = 1;.
1f3f3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
1f3f4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
1f3f5 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 fault: break;.
1f3f6 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 }. if( rc ){.
1f3f7 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 p->op = TK_INT
1f3f8 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 EGER;. p->fla
1f3f9 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 gs |= EP_IntValu
1f3fa 65 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 e;. p->iTable
1f3fb 20 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a = *pValue;. }.
1f3fc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f3fd 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
1f3fe 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 73 E if the given s
1f3ff 74 72 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 tring is a row-i
1f400 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a d column name..*
1f401 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f402 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f int sqlite3IsRo
1f403 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a wid(const char *
1f404 7a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 z){. if( sqlite
1f405 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 3StrICmp(z, "_RO
1f406 57 49 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 WID_")==0 ) retu
1f407 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 rn 1;. if( sqli
1f408 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 te3StrICmp(z, "R
1f409 4f 57 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 OWID")==0 ) retu
1f40a 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 rn 1;. if( sqli
1f40b 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f te3StrICmp(z, "O
1f40c 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e ID")==0 ) return
1f40d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 1;. return 0;.
1f40e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1f40f 74 72 75 65 20 69 66 20 74 68 65 20 49 4e 20 6f true if the IN o
1f410 70 65 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 perator optimiza
1f411 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
1f412 61 6e 64 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 and.** the SELEC
1f413 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 65 78 T statement p ex
1f414 69 73 74 73 20 61 6e 64 20 69 73 20 6f 66 20 74 ists and is of t
1f415 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 he.** simple for
1f416 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c m:.**.** SEL
1f417 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f ECT <column> FRO
1f418 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 M <table>.**.**
1f419 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1f41a 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70 ase, it may be p
1f41b 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 ossible to use a
1f41c 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 n existing table
1f41d 0a 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 .** or index ins
1f41e 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69 tead of generati
1f41f 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 ng an epheremal
1f420 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 table..*/.#ifnde
1f421 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 f SQLITE_OMIT_SU
1f422 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 6e BQUERY.static in
1f423 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 t isCandidateFor
1f424 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 29 InOpt(Select *p)
1f425 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 {. SrcList *pSr
1f426 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 c;. ExprList *p
1f427 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a EList;. Table *
1f428 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 pTab;. if( p==0
1f429 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1f42a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f42b 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 * right-hand sid
1f42c 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 43 e of IN is SELEC
1f42d 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 T */. if( p->pP
1f42e 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 3b rior ) return 0;
1f42f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f430 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 Not a compound
1f431 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 SELECT */. if(
1f432 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 p->selFlags & (S
1f433 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 F_Distinct|SF_Ag
1f434 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 20 gregate) ){.
1f435 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e return 0; /* N
1f436 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f o DISTINCT keywo
1f437 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 rd and no aggreg
1f438 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f ate functions */
1f439 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 . }. if( p->pG
1f43a 72 6f 75 70 42 79 20 29 20 72 65 74 75 72 6e 20 roupBy ) return
1f43b 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
1f43c 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 Has no GROUP BY
1f43d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 clause */. if(
1f43e 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 p->pLimit ) ret
1f43f 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
1f440 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 /* Has no LI
1f441 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 MIT clause */.
1f442 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 if( p->pOffset )
1f443 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1f444 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 p->pWhere ) ret
1f445 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
1f446 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 /* Has no WH
1f447 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ERE clause */.
1f448 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a pSrc = p->pSrc;.
1f449 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 3d assert( pSrc!=
1f44a 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 0 );. if( pSrc-
1f44b 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 72 >nSrc!=1 ) retur
1f44c 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a n 0; /*
1f44d 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 Single term in
1f44e 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
1f44f 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e if( pSrc->a[0].
1f450 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e pSelect ) return
1f451 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 0; /* FROM
1f452 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20 clause is not a
1f453 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70 54 subquery */. pT
1f454 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e ab = pSrc->a[0].
1f455 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62 pTab;. if( pTab
1f456 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1f457 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c if( pTab->pSel
1f458 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ect ) return 0;
1f459 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d /* FROM
1f45a 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 clause is not a
1f45b 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 view */. if( I
1f45c 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
1f45d 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 return 0;
1f45e 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 /* FROM clause
1f45f 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 not a virtual t
1f460 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 able */. pEList
1f461 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 = p->pEList;.
1f462 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 if( pEList->nExp
1f463 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b r!=1 ) return 0;
1f464 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f /* One co
1f465 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 lumn in the resu
1f466 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 lt set */. if(
1f467 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
1f468 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pr->op!=TK_COLUM
1f469 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a N ) return 0; /*
1f46a 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c Result is a col
1f46b 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 umn */. return
1f46c 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 1;.}.#endif /* S
1f46d 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1f46e 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 ERY */../*.** Th
1f46f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
1f470 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 sed by the imple
1f471 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
1f472 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 IN (...) operat
1f473 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 or..** It's job
1f474 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 is to find or cr
1f475 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74 eate a b-tree st
1f476 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 ructure that may
1f477 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 be used.** eith
1f478 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d er to test for m
1f479 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65 embership of the
1f47a 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f (...) set or to
1f47b 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 iterate through
1f47c 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c .** its members,
1f47d 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63 skipping duplic
1f47e 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ates..**.** The
1f47f 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e cursor opened on
1f480 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 28 the structure (
1f481 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 database table,
1f482 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a database index .
1f483 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 ** or ephermal t
1f484 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20 able) is stored
1f485 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 in pX->iTable be
1f486 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 fore this functi
1f487 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 on returns..** T
1f488 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 he returned valu
1f489 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 e indicates the
1f48a 73 74 72 75 63 74 75 72 65 20 74 79 70 65 2c 20 structure type,
1f48b 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
1f48c 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 * IN_INDEX_ROW
1f48d 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 ID - The cursor
1f48e 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 was opened on a
1f48f 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a database table..
1f490 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e ** IN_INDEX_IN
1f491 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f 72 DEX - The cursor
1f492 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 was opened on a
1f493 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 2e database index.
1f494 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 .** IN_INDEX_E
1f495 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 6f PH - The curso
1f496 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
1f497 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 61 a specially crea
1f498 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ted and.**
1f499 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f po
1f49a 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d 61 pulated epherema
1f49b 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 l table..**.** A
1f49c 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 75 63 n existing struc
1f49d 74 75 72 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 ture may only be
1f49e 20 75 73 65 64 20 69 66 20 74 68 65 20 53 45 4c used if the SEL
1f49f 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 ECT is of the si
1f4a0 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a mple.** form:.**
1f4a1 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c .** SELECT <
1f4a2 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 column> FROM <ta
1f4a3 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 ble>.**.** If pr
1f4a4 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 NotFound paramet
1f4a5 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 er is 0, then th
1f4a6 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c e structure will
1f4a7 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 be used to iter
1f4a8 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 ate.** through t
1f4a9 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 he set members,
1f4aa 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 skipping any dup
1f4ab 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 licates. In this
1f4ac 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 case an.** ephe
1f4ad 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 remal table must
1f4ae 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 be used unless
1f4af 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f the selected <co
1f4b0 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 lumn> is guarant
1f4b1 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 eed.** to be uni
1f4b2 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 que - either bec
1f4b3 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e ause it is an IN
1f4b4 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
1f4b5 59 20 6f 72 20 69 74 0a 2a 2a 20 69 73 20 75 6e Y or it.** is un
1f4b6 69 71 75 65 20 62 79 20 76 69 72 74 75 65 20 6f ique by virtue o
1f4b7 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f f a constraint o
1f4b8 72 20 69 6d 70 6c 69 63 69 74 20 69 6e 64 65 78 r implicit index
1f4b9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
1f4ba 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 rNotFound parame
1f4bb 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 ter is not 0, th
1f4bc 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 en the structure
1f4bd 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a will be used .*
1f4be 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d * for fast set m
1f4bf 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2e embership tests.
1f4c0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e In this case an
1f4c1 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 epheremal table
1f4c2 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 must .** be use
1f4c3 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e d unless <column
1f4c4 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 > is an INTEGER
1f4c5 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 PRIMARY KEY or a
1f4c6 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 n index can .**
1f4c7 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 be found with <c
1f4c8 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 olumn> as its le
1f4c9 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a ft-most column..
1f4ca 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 **.** When the s
1f4cb 74 72 75 63 74 75 72 65 20 69 73 20 62 65 69 6e tructure is bein
1f4cc 67 20 75 73 65 64 20 66 6f 72 20 73 65 74 20 6d g used for set m
1f4cd 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2c embership tests,
1f4ce 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 6e 65 65 the user.** nee
1f4cf 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 ds to know wheth
1f4d0 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 er or not the st
1f4d1 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
1f4d2 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a an SQL NULL .**
1f4d3 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 value in order
1f4d4 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61 to correctly eva
1f4d5 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e luate expression
1f4d6 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c s like "X IN (Y,
1f4d7 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 Z)"..** If ther
1f4d8 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 e is a chance th
1f4d9 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65 at the structure
1f4da 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20 4e may contain a N
1f4db 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 ULL value at.**
1f4dc 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 runtime, then a
1f4dd 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f register is allo
1f4de 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 72 65 cated and the re
1f4df 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72 gister number wr
1f4e0 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e itten.** to *prN
1f4e1 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 otFound. If ther
1f4e2 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 e is no chance t
1f4e3 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 hat the structur
1f4e4 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 e contains a.**
1f4e5 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e NULL value, then
1f4e6 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 *prNotFound is
1f4e7 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a left unchanged..
1f4e8 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 **.** If a regis
1f4e9 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ter is allocated
1f4ea 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f and its locatio
1f4eb 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e n stored in *prN
1f4ec 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a otFound, then.**
1f4ed 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c its initial val
1f4ee 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 ue is NULL. If t
1f4ef 68 65 20 73 74 72 75 63 74 75 72 65 20 64 6f 65 he structure doe
1f4f0 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e s not remain con
1f4f1 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65 stant.** for the
1f4f2 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 duration of the
1f4f3 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68 65 query (i.e. the
1f4f4 20 73 65 74 20 69 73 20 61 20 63 6f 72 72 65 6c set is a correl
1f4f5 61 74 65 64 20 73 75 62 2d 73 65 6c 65 63 74 29 ated sub-select)
1f4f6 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 , .** the value
1f4f7 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 of the allocated
1f4f8 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 73 register is res
1f4f9 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 et to NULL each
1f4fa 74 69 6d 65 20 74 68 65 20 0a 2a 2a 20 73 74 72 time the .** str
1f4fb 75 63 74 75 72 65 20 69 73 20 72 65 70 6f 70 75 ucture is repopu
1f4fc 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f lated. This allo
1f4fd 77 73 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f ws the caller to
1f4fe 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 0a use vdbe code .
1f4ff 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f ** equivalent to
1f500 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
1f501 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67 69 **.** if( regi
1f502 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a ster==NULL ){.**
1f503 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 has_null =
1f504 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73 74 <test if data st
1f505 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
1f506 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 null>.** re
1f507 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20 20 gister = 1.**
1f508 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 }.**.** in order
1f509 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69 6e to avoid runnin
1f50a 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20 64 g the <test if d
1f50b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f ata structure co
1f50c 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 ntains null>.**
1f50d 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e 20 test more often
1f50e 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61 72 than is necessar
1f50f 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 y..*/.#ifndef SQ
1f510 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1f511 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 RY.SQLITE_PRIVAT
1f512 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e E int sqlite3Fin
1f513 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a dInIndex(Parse *
1f514 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 58 pParse, Expr *pX
1f515 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 6e , int *prNotFoun
1f516 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 3b d){. Select *p;
1f517 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30 . int eType = 0
1f518 3b 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 ;. int iTab = p
1f519 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
1f51a 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 int mustBeUniqu
1f51b 65 20 3d 20 21 70 72 4e 6f 74 46 6f 75 6e 64 3b e = !prNotFound;
1f51c 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 .. /* The follw
1f51d 69 6e 67 20 69 66 28 2e 2e 2e 29 20 65 78 70 72 ing if(...) expr
1f51e 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 69 ession is true i
1f51f 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 20 f the SELECT is
1f520 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d of the . ** sim
1f521 70 6c 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 ple form:. **.
1f522 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c ** SELECT <
1f523 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 column> FROM <ta
1f524 62 6c 65 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ble>. **. ** I
1f525 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 f this is the ca
1f526 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70 6f se, it may be po
1f527 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e ssible to use an
1f528 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 0a existing table.
1f529 20 20 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e ** or index in
1f52a 73 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74 stead of generat
1f52b 69 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c ing an epheremal
1f52c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 table.. */. p
1f52d 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f 70 65 = (ExprHasPrope
1f52e 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 rty(pX, EP_xIsSe
1f52f 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e 70 53 lect) ? pX->x.pS
1f530 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 69 66 elect : 0);. if
1f531 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 ( isCandidateFor
1f532 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 InOpt(p) ){.
1f533 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1f534 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 arse->db;
1f535 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
1f536 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1f537 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 . Expr *pExpr
1f538 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b = p->pEList->a[
1f539 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 0].pExpr; /* E
1f53a 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d xpression <colum
1f53b 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 n> */. int iC
1f53c 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c ol = pExpr->iCol
1f53d 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 umn;
1f53e 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
1f53f 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a umn <column> */.
1f540 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 Vdbe *v = sq
1f541 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
1f542 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 rse); /* Vi
1f543 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 rtual machine be
1f544 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 ing coded */.
1f545 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
1f546 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 ->pSrc->a[0].pTa
1f547 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 b; /* Table
1f548 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 <table>. */.
1f549 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
1f54a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f54b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
1f54c 61 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 ase idx for pTab
1f54d 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 */. . /* C
1f54e 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 ode an OP_Verify
1f54f 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 Cookie and OP_Ta
1f550 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 bleLock for <tab
1f551 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 le>. */. iDb
1f552 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1f553 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
1f554 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 >pSchema);. s
1f555 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
1f556 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 Schema(pParse, i
1f557 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Db);. sqlite3
1f558 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 TableLock(pParse
1f559 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 , iDb, pTab->tnu
1f55a 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d m, 0, pTab->zNam
1f55b 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 e);.. /* This
1f55c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
1f55d 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 y called from tw
1f55e 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 o places. In bot
1f55f 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65 h cases the vdbe
1f560 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 . ** has alre
1f561 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 ady been allocat
1f562 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 ed. So assume sq
1f563 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69 lite3GetVdbe() i
1f564 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 s always. **
1f565 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e successful here.
1f566 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1f567 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 rt(v);. if( i
1f568 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 Col<0 ){. i
1f569 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 nt iMem = ++pPar
1f56a 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
1f56b 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 int iAddr;.
1f56c 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1f56d 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 0a Btree(v, iDb);..
1f56e 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 iAddr = sq
1f56f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1f570 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b v, OP_If, iMem);
1f571 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f572 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1f573 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 nteger, 1, iMem)
1f574 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ;.. sqlite3
1f575 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 OpenTable(pParse
1f576 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61 , iTab, iDb, pTa
1f577 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b b, OP_OpenRead);
1f578 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 . eType = I
1f579 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a N_INDEX_ROWID;..
1f57a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f57b 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 eJumpHere(v, iAd
1f57c 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a dr);. }else{.
1f57d 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 Index *pId
1f57e 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
1f57f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
1f580 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
1f581 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 */.. /* The
1f582 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
1f583 6e 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 nce used by the
1f584 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 comparison. If a
1f585 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 20 0a 20 n index is to .
1f586 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20 ** be used
1f587 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65 in place of a te
1f588 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 mp-table, it mus
1f589 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 63 63 t be ordered acc
1f58a 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 ording. **
1f58b 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f to this collatio
1f58c 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a n sequence. */.
1f58d 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 CollSeq *p
1f58e 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 69 6e Req = sqlite3Bin
1f58f 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 aryCompareCollSe
1f590 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c q(pParse, pX->pL
1f591 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20 eft, pExpr);..
1f592 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 /* Check tha
1f593 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 t the affinity t
1f594 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 hat will be used
1f595 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 to perform the
1f596 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 . ** compar
1f597 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65 ison is the same
1f598 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 74 79 as the affinity
1f599 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 of the column.
1f59a 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69 If. ** it i
1f59b 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74 s not, it is not
1f59c 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 possible to use
1f59d 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 20 20 any index..
1f59e 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 */. char
1f59f 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e aff = comparison
1f5a0 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 Affinity(pX);.
1f5a1 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79 int affinity
1f5a2 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f _ok = (pTab->aCo
1f5a3 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 l[iCol].affinity
1f5a4 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 ==aff||aff==SQLI
1f5a5 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 TE_AFF_NONE);..
1f5a6 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 for(pIdx=pT
1f5a7 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
1f5a8 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 && eType==0 &&
1f5a9 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 affinity_ok; pId
1f5aa 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
1f5ab 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 if( (pId
1f5ac 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d x->aiColumn[0]==
1f5ad 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26 iCol). &
1f5ae 26 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33 & (pReq==sqlite3
1f5af 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 FindCollSeq(db,
1f5b0 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 ENC(db), pIdx->a
1f5b1 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29 zColl[0], -1, 0)
1f5b2 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 ). && (!
1f5b3 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 mustBeUnique ||
1f5b4 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d (pIdx->nColumn==
1f5b5 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 1 && pIdx->onErr
1f5b6 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 or!=OE_None)).
1f5b7 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1f5b8 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b int iMem = ++
1f5b9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
1f5ba 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 int iAdd
1f5bb 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 r;. cha
1f5bc 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20 r *pKey;. .
1f5bd 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68 pKey = (ch
1f5be 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65 ar *)sqlite3Inde
1f5bf 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c xKeyinfo(pParse,
1f5c0 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 pIdx);.
1f5c1 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1f5c2 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1f5c3 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 29 3b pIdx->pSchema);
1f5c4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1f5c5 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 e3VdbeUsesBtree(
1f5c6 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 v, iDb);..
1f5c7 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 iAddr = sqli
1f5c8 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
1f5c9 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 OP_If, iMem);.
1f5ca 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1f5cb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1f5cc 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 _Integer, 1, iMe
1f5cd 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 m);. .
1f5ce 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f5cf 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 p4(v, OP_OpenRea
1f5d0 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 d, iTab, pIdx->t
1f5d1 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 num, iDb,.
1f5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5d3 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 pKey,P4
1f5d4 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
1f5d5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 );. Vdb
1f5d6 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 eComment((v, "%s
1f5d7 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 ", pIdx->zName))
1f5d8 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70 ;. eTyp
1f5d9 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 e = IN_INDEX_IND
1f5da 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 EX;.. s
1f5db 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
1f5dc 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 re(v, iAddr);.
1f5dd 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f if( prNo
1f5de 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d tFound && !pTab-
1f5df 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e >aCol[iCol].notN
1f5e0 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ull ){.
1f5e1 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d *prNotFound =
1f5e2 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
1f5e3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1f5e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1f5e5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1f5e6 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 eType==0 ){.
1f5e7 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c int rMayHaveNull
1f5e8 20 3d 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20 = 0;. eType
1f5e9 3d 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a = IN_INDEX_EPH;.
1f5ea 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 if( prNotFou
1f5eb 6e 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e nd ){. *prN
1f5ec 6f 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61 otFound = rMayHa
1f5ed 76 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 veNull = ++pPars
1f5ee 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c e->nMem;. }el
1f5ef 73 65 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 se if( pX->pLeft
1f5f0 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 ->iColumn<0 && !
1f5f1 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
1f5f2 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c ty(pX, EP_xIsSel
1f5f3 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54 ect) ){. eT
1f5f4 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 ype = IN_INDEX_R
1f5f5 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OWID;. }.
1f5f6 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 sqlite3CodeSubse
1f5f7 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c lect(pParse, pX,
1f5f8 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 rMayHaveNull, e
1f5f9 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 Type==IN_INDEX_R
1f5fa 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a OWID);. }else{.
1f5fb 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d pX->iTable =
1f5fc 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 iTab;. }. ret
1f5fd 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e urn eType;.}.#en
1f5fe 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 dif../*.** Gener
1f5ff 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 ate code for sca
1f600 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 lar subqueries u
1f601 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 sed as an expres
1f602 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f sion.** and IN o
1f603 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 perators. Examp
1f604 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 les:.**.** (
1f605 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 SELECT a FROM b)
1f606 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 -- sub
1f607 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 query.** EXI
1f608 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 STS (SELECT a FR
1f609 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 OM b) -- EXIST
1f60a 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 S subquery.**
1f60b 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 x IN (4,5,11)
1f60c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 --
1f60d 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 IN operator with
1f60e 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 list on right-h
1f60f 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 and side.**
1f610 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 x IN (SELECT a F
1f611 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e ROM b) -- IN
1f612 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 operator with s
1f613 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 ubquery on the r
1f614 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ight.**.** The p
1f615 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 Expr parameter d
1f616 65 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 escribes the exp
1f617 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e ression that con
1f618 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 tains the IN.**
1f619 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 operator or subq
1f61a 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 uery..**.** If p
1f61b 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 arameter isRowid
1f61c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
1f61d 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 en expression pE
1f61e 78 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 xpr is guarantee
1f61f 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 d.** to be of th
1f620 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 e form "<rowid>
1f621 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 IN (?, ?, ?)", w
1f622 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 here <rowid> is
1f623 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 a reference.** t
1f624 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b o some integer k
1f625 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 ey column of a t
1f626 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 able B-Tree. In
1f627 74 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 this case, use a
1f628 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 n.** intkey B-Tr
1f629 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ee to store the
1f62a 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 set of IN(...) v
1f62b 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 alues instead of
1f62c 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 the usual.** (s
1f62d 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 lower) variable
1f62e 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 length keys B-Tr
1f62f 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ee..*/.#ifndef S
1f630 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1f631 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ERY.SQLITE_PRIVA
1f632 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
1f633 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 odeSubselect(.
1f634 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a Parse *pParse, .
1f635 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a Expr *pExpr, .
1f636 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 int rMayHaveNu
1f637 6c 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 ll,. int isRowi
1f638 64 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 d.){. int testA
1f639 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ddr = 0;
1f63a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f63b 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 * One-time test
1f63c 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 address */. Vdb
1f63d 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 e *v = sqlite3Ge
1f63e 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1f63f 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
1f640 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 rn;. sqlite3Exp
1f641 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 rCachePush(pPars
1f642 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 e);.. /* This c
1f643 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 ode must be run
1f644 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 in its entirety
1f645 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 every time it is
1f646 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a encountered. *
1f647 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 * if any of the
1f648 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 following is tru
1f649 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 e:. **. **
1f64a 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e * The right-han
1f64b 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 d side is a corr
1f64c 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a elated subquery.
1f64d 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 ** * The r
1f64e 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 ight-hand side i
1f64f 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1f650 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 list containing
1f651 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 variables. **
1f652 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 * We are insi
1f653 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a de a trigger. *
1f654 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 *. ** If all of
1f655 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 the above are f
1f656 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 alse, then we ca
1f657 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 n run this code
1f658 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 just once. ** s
1f659 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c ave the results,
1f65a 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 and reuse the s
1f65b 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 ame result on su
1f65c 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 bsequent invocat
1f65d 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ions.. */. if(
1f65e 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 !ExprHasAnyProp
1f65f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 erty(pExpr, EP_V
1f660 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 arSelect) && !pP
1f661 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 arse->trigStack
1f662 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d ){. int mem =
1f663 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
1f664 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f665 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c AddOp1(v, OP_If,
1f666 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 mem);. testA
1f667 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
1f668 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
1f669 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a teger, 1, mem);.
1f66a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74 assert( test
1f66b 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 Addr>0 || pParse
1f66c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1f66d 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 ed );. }.. swi
1f66e 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
1f66f 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e {. case TK_IN
1f670 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 : {. char a
1f671 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b ffinity;. K
1f672 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a eyInfo keyInfo;.
1f673 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 int addr;
1f674 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 /* Addres
1f675 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 s of OP_OpenEphe
1f676 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f meral instructio
1f677 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 n */. Expr
1f678 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e *pLeft = pExpr->
1f679 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 pLeft;.. if
1f67a 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 ( rMayHaveNull )
1f67b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1f67c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1f67d 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 P_Null, 0, rMayH
1f67e 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 aveNull);.
1f67f 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 }.. affinit
1f680 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 y = sqlite3ExprA
1f681 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a ffinity(pLeft);.
1f682 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 . /* Whethe
1f683 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 r this is an 'x
1f684 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f IN(SELECT...)' o
1f685 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 r an 'x IN(<expr
1f686 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a list>)'. **
1f687 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 expression it i
1f688 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 s handled the sa
1f689 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 me way. A virtua
1f68a 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 l table is .
1f68b 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 ** filled with
1f68c 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e single-field in
1f68d 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 dex keys represe
1f68e 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 nting the result
1f68f 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 s. ** from
1f690 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 the SELECT or th
1f691 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 e <exprlist>..
1f692 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1f693 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65 If the 'x' expre
1f694 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d ssion is a colum
1f695 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 n value, or the
1f696 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 SELECT....
1f697 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 ** statement ret
1f698 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 urns a column va
1f699 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 lue, then the af
1f69a 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 finity of that.
1f69b 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 ** column i
1f69c 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 s used to build
1f69d 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 the index keys.
1f69e 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 If both 'x' and
1f69f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c the. ** SEL
1f6a0 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 ECT... statement
1f6a1 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 are columns, th
1f6a2 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e en numeric affin
1f6a3 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 ity is used.
1f6a4 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 ** if either c
1f6a5 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 olumn has NUMERI
1f6a6 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 C or INTEGER aff
1f6a7 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 inity. If neithe
1f6a8 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e r. ** 'x' n
1f6a9 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e or the SELECT...
1f6aa 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
1f6ab 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d olumns, then num
1f6ac 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 eric affinity.
1f6ad 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a ** is used..
1f6ae 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 */. p
1f6af 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1f6b0 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
1f6b1 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 addr = sqli
1f6b2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1f6b3 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
1f6b4 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 l, pExpr->iTable
1f6b5 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 , !isRowid);.
1f6b6 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e memset(&keyIn
1f6b7 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 fo, 0, sizeof(ke
1f6b8 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b yInfo));. k
1f6b9 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 eyInfo.nField =
1f6ba 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78 1;.. if( Ex
1f6bb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
1f6bc 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
1f6bd 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a t) ){. /*
1f6be 20 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 Case 1: exp
1f6bf 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e r IN (SELECT ...
1f6c0 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 ). **.
1f6c1 20 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 ** Generate
1f6c2 20 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 code to write t
1f6c3 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1f6c4 65 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 e select into th
1f6c5 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 e temporary.
1f6c6 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c ** table all
1f6c7 6f 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 ocated and opene
1f6c8 64 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 d above..
1f6c9 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 */. Sele
1f6ca 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 ctDest dest;.
1f6cb 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ExprList *p
1f6cc 45 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 EList;..
1f6cd 61 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 assert( !isRowid
1f6ce 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1f6cf 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 te3SelectDestIni
1f6d0 74 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 t(&dest, SRT_Set
1f6d1 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 , pExpr->iTable)
1f6d2 3b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 ;. dest.a
1f6d3 66 66 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66 ffinity = (u8)af
1f6d4 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 finity;.
1f6d5 61 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e assert( (pExpr->
1f6d6 69 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 iTable&0x0000FFF
1f6d7 46 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c F)==pExpr->iTabl
1f6d8 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
1f6d9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
1f6da 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e Parse, pExpr->x.
1f6db 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 pSelect, &dest)
1f6dc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
1f6dd 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 urn;. }.
1f6de 20 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 pEList =
1f6df 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
1f6e0 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 ->pEList;.
1f6e1 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 if( pEList &&
1f6e2 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 pEList->nExpr>0
1f6e3 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 ){ . ke
1f6e4 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d yInfo.aColl[0] =
1f6e5 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f sqlite3BinaryCo
1f6e6 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 mpareCollSeq(pPa
1f6e7 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1f6e8 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
1f6e9 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 pEList->a[0].pE
1f6ea 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a xpr);. }.
1f6eb 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1f6ec 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 pExpr->x.pList )
1f6ed 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 {. /* Cas
1f6ee 65 20 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e e 2: expr IN
1f6ef 20 28 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20 (exprlist).
1f6f0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
1f6f1 2a 20 46 6f 72 20 65 61 63 68 20 65 78 70 72 65 * For each expre
1f6f2 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20 ssion, build an
1f6f3 69 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74 index key from t
1f6f4 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e he evaluation an
1f6f5 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f d. ** sto
1f6f6 72 65 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d re it in the tem
1f6f7 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66 porary table. If
1f6f8 20 3c 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c <expr> is a col
1f6f9 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 umn, then use.
1f6fa 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f ** that co
1f6fb 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77 lumns affinity w
1f6fc 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64 hen building ind
1f6fd 65 78 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70 ex keys. If <exp
1f6fe 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 r> is not.
1f6ff 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 ** a column, u
1f700 73 65 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e se numeric affin
1f701 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ity.. */.
1f702 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 int i;.
1f703 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 ExprList
1f704 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e *pList = pExpr->
1f705 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 x.pList;.
1f706 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
1f707 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
1f708 20 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32 int r1, r2
1f709 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69 , r3;.. i
1f70a 66 28 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a f( !affinity ){.
1f70b 20 20 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 affini
1f70c 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ty = SQLITE_AFF_
1f70d 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a NONE;. }.
1f70e 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e keyInfo.
1f70f 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 aColl[0] = sqlit
1f710 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 e3ExprCollSeq(pP
1f711 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
1f712 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a ft);.. /*
1f713 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 Loop through ea
1f714 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ch expression in
1f715 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a <exprlist>. */.
1f716 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c r1 = sql
1f717 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1f718 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
1f719 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r2 = sqlite3GetT
1f71a 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
1f71b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f71c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1f71d 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20 Null, 0, r2);.
1f71e 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73 for(i=pLis
1f71f 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d t->nExpr, pItem=
1f720 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 pList->a; i>0; i
1f721 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 --, pItem++){.
1f722 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 Expr *pE
1f723 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2 = pItem->pExpr
1f724 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ;.. /*
1f725 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
1f726 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e n is not constan
1f727 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e t then we will n
1f728 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 eed to.
1f729 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20 ** disable the
1f72a 74 65 73 74 20 74 68 61 74 20 77 61 73 20 67 65 test that was ge
1f72b 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 74 68 nerated above th
1f72c 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20 at makes sure.
1f72d 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 ** this
1f72e 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74 code only execut
1f72f 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73 es once. Becaus
1f730 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 e for a non-cons
1f731 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a tant. *
1f732 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20 * expression we
1f733 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68 need to rerun th
1f734 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d is code each tim
1f735 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
1f736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 74 65 if( te
1f737 73 74 41 64 64 72 20 26 26 20 21 73 71 6c 69 74 stAddr && !sqlit
1f738 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1f739 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20 (pE2) ){.
1f73a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f73b 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 ChangeToNoop(v,
1f73c 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a testAddr-1, 2);.
1f73d 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 test
1f73e 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 Addr = 0;.
1f73f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 }..
1f740 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 /* Evaluate the
1f741 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 expression and
1f742 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74 insert it into t
1f743 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f he temp table */
1f744 0a 20 20 20 20 20 20 20 20 20 20 72 33 20 3d 20 . r3 =
1f745 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1f746 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
1f747 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 2, r1);.
1f748 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 29 7b if( isRowid ){
1f749 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1f74a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f74b 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 , OP_MustBeInt,
1f74c 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 r3, sqlite3VdbeC
1f74d 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29 urrentAddr(v)+2)
1f74e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 ;. sq
1f74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1f750 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 v, OP_Insert, pE
1f751 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c xpr->iTable, r2,
1f752 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r3);.
1f753 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1f754 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1f755 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 dOp4(v, OP_MakeR
1f756 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 ecord, r3, 1, r2
1f757 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b , &affinity, 1);
1f758 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1f759 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
1f75a 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 inityChange(pPar
1f75b 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 se, r3, 1);.
1f75c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f75d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1f75e 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78 70 72 IdxInsert, pExpr
1f75f 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 ->iTable, r2);.
1f760 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1f761 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c }. sql
1f762 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
1f763 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
1f764 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1f765 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
1f766 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 arse, r2);.
1f767 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 }. if( !is
1f768 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
1f769 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f76a 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 76 geP4(v, addr, (v
1f76b 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 oid *)&keyInfo,
1f76c 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 P4_KEYINFO);.
1f76d 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
1f76e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 ;. }.. cas
1f76f 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 e TK_EXISTS:.
1f770 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a case TK_SELECT:
1f771 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 {. /* This
1f772 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 61 has to be a sca
1f773 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 6e lar SELECT. Gen
1f774 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 75 erate code to pu
1f775 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 t the. ** v
1f776 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 6c alue of this sel
1f777 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 ect in a memory
1f778 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 cell and record
1f779 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 the number.
1f77a 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ** of the memor
1f77b 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d y cell in iColum
1f77c 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
1f77d 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 54 static const T
1f77e 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38 oken one = { (u8
1f77f 2a 29 22 31 22 2c 20 30 2c 20 30 2c 20 31 20 7d *)"1", 0, 0, 1 }
1f780 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a ;. Select *
1f781 70 53 65 6c 3b 0a 20 20 20 20 20 20 53 65 6c 65 pSel;. Sele
1f782 63 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 ctDest dest;..
1f783 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 assert( Expr
1f784 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1f785 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1f786 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d );. pSel =
1f787 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 pExpr->x.pSelec
1f788 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 t;. sqlite3
1f789 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 SelectDestInit(&
1f78a 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73 dest, 0, ++pPars
1f78b 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 e->nMem);.
1f78c 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 if( pExpr->op==T
1f78d 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 K_SELECT ){.
1f78e 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d dest.eDest =
1f78f 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 SRT_Mem;.
1f790 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f791 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1f792 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 0, dest.iParm);.
1f793 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
1f794 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75 ent((v, "Init su
1f795 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 29 29 bquery result"))
1f796 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1f797 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 dest.eDes
1f798 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0a t = SRT_Exists;.
1f799 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1f79a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1f79b 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 73 74 Integer, 0, dest
1f79c 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 .iParm);.
1f79d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
1f79e 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 72 65 "Init EXISTS re
1f79f 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d sult"));. }
1f7a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1f7a1 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d prDelete(pParse-
1f7a2 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 >db, pSel->pLimi
1f7a3 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e t);. pSel->
1f7a4 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 pLimit = sqlite3
1f7a5 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b PExpr(pParse, TK
1f7a6 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 _INTEGER, 0, 0,
1f7a7 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 66 28 &one);. if(
1f7a8 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
1f7a9 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 64 65 Parse, pSel, &de
1f7aa 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 st) ){. r
1f7ab 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
1f7ac 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1f7ad 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61 72 6d umn = dest.iParm
1f7ae 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1f7af 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1f7b0 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20 testAddr ){.
1f7b1 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1f7b2 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 72 2d ere(v, testAddr-
1f7b3 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 1);. }. sqlite
1f7b4 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 3ExprCachePop(pP
1f7b5 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72 65 74 arse, 1);.. ret
1f7b6 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a urn;.}.#endif /*
1f7b7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 SQLITE_OMIT_SUB
1f7b8 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 QUERY */../*.**
1f7b9 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 Duplicate an 8-b
1f7ba 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 yte value.*/.sta
1f7bb 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 tic char *dup8by
1f7bc 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e tes(Vdbe *v, con
1f7bd 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 st char *in){.
1f7be 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 char *out = sqli
1f7bf 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
1f7c0 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c qlite3VdbeDb(v),
1f7c1 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 8);. if( out )
1f7c2 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 {. memcpy(out
1f7c3 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 , in, 8);. }.
1f7c4 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
1f7c5 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
1f7c6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
1f7c7 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 t will put the f
1f7c8 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a loating point.**
1f7c9 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 value described
1f7ca 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e by z[0..n-1] in
1f7cb 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d to register iMem
1f7cc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 ..**.** The z[]
1f7cd 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 string will prob
1f7ce 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f ably not be zero
1f7cf 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 -terminated. Bu
1f7d0 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 t the .** z[n] c
1f7d1 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 haracter is guar
1f7d2 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d anteed to be som
1f7d3 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 ething that does
1f7d4 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b not look.** lik
1f7d5 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 e the continuati
1f7d6 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 on of the number
1f7d7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1f7d8 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a codeReal(Vdbe *
1f7d9 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a v, const char *z
1f7da 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67 , int n, int neg
1f7db 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 ateFlag, int iMe
1f7dc 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20 m){. assert( z
1f7dd 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 || v==0 || sqlit
1f7de 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c e3VdbeDb(v)->mal
1f7df 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 locFailed );. a
1f7e0 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 73 71 ssert( !z || !sq
1f7e1 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 6e lite3Isdigit(z[n
1f7e2 5d 29 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ]) );. UNUSED_P
1f7e3 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 69 ARAMETER(n);. i
1f7e4 66 28 20 7a 20 29 7b 0a 20 20 20 20 64 6f 75 62 f( z ){. doub
1f7e5 6c 65 20 76 61 6c 75 65 3b 0a 20 20 20 20 63 68 le value;. ch
1f7e6 61 72 20 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69 ar *zV;. sqli
1f7e7 74 65 33 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75 te3AtoF(z, &valu
1f7e8 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 e);. if( sqli
1f7e9 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 te3IsNaN(value)
1f7ea 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1f7eb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1f7ec 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 29 3b _Null, 0, iMem);
1f7ed 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f7ee 20 20 69 66 28 20 6e 65 67 61 74 65 46 6c 61 67 if( negateFlag
1f7ef 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 ) value = -valu
1f7f0 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75 e;. zV = du
1f7f1 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72 p8bytes(v, (char
1f7f2 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 *)&value);.
1f7f3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f7f4 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 p4(v, OP_Real, 0
1f7f5 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 , iMem, 0, zV, P
1f7f6 34 5f 52 45 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 4_REAL);. }.
1f7f7 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}.../*.** Gen
1f7f8 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 erate an instruc
1f7f9 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 tion that will p
1f7fa 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64 ut the integer d
1f7fb 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 escribe by.** te
1f7fc 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 xt z[0..n-1] int
1f7fd 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e o register iMem.
1f7fe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 .**.** The z[] s
1f7ff 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 tring will proba
1f800 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d bly not be zero-
1f801 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 terminated. But
1f802 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 the .** z[n] ch
1f803 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61 aracter is guara
1f804 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 nteed to be some
1f805 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 thing that does
1f806 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 not look.** like
1f807 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f the continuatio
1f808 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e n of the number.
1f809 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1f80a 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62 65 codeInteger(Vdbe
1f80b 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72 *v, Expr *pExpr
1f80c 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 , int negFlag, i
1f80d 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 63 6f 6e 73 nt iMem){. cons
1f80e 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 t char *z;. if(
1f80f 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 pExpr->flags &
1f810 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 EP_IntValue ){.
1f811 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72 int i = pExpr
1f812 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 ->iTable;. if
1f813 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 ( negFlag ) i =
1f814 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 -i;. sqlite3V
1f815 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1f816 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d Integer, i, iMem
1f817 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 );. }else if( (
1f818 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 z = (char*)pExpr
1f819 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d 30 20 29 7b ->token.z)!=0 ){
1f81a 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1f81b 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74 int n = pExpr->t
1f81c 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 61 73 73 65 oken.n;. asse
1f81d 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 rt( !sqlite3Isdi
1f81e 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 git(z[n]) );.
1f81f 20 69 66 28 20 73 71 6c 69 74 65 33 47 65 74 49 if( sqlite3GetI
1f820 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b 0a 20 nt32(z, &i) ){.
1f821 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67 if( negFlag
1f822 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 20 20 ) i = -i;.
1f823 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f824 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1f825 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 , i, iMem);.
1f826 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 }else if( sqlite
1f827 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 7a 2c 3FitsIn64Bits(z,
1f828 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 20 20 negFlag) ){.
1f829 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 i64 value;.
1f82a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 char *zV;.
1f82b 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 69 36 sqlite3Atoi6
1f82c 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 4(z, &value);.
1f82d 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 if( negFlag
1f82e 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 ) value = -value
1f82f 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75 70 ;. zV = dup
1f830 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72 2a 8bytes(v, (char*
1f831 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 )&value);.
1f832 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f833 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 4(v, OP_Int64, 0
1f834 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 , iMem, 0, zV, P
1f835 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 7d 65 4_INT64);. }e
1f836 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 lse{. codeR
1f837 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e 65 67 eal(v, z, n, neg
1f838 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 Flag, iMem);.
1f839 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1f83a 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 65 6e Clear a cache en
1f83b 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 try..*/.static v
1f83c 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c oid cacheEntryCl
1f83d 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 ear(Parse *pPars
1f83e 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 e, struct yColCa
1f83f 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 che *p){. if( p
1f840 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20 ->tempReg ){.
1f841 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 if( pParse->nTe
1f842 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 mpReg<ArraySize(
1f843 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 pParse->aTempReg
1f844 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 ) ){. pPars
1f845 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 e->aTempReg[pPar
1f846 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 se->nTempReg++]
1f847 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d = p->iReg;. }
1f848 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 . p->tempReg
1f849 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a = 0;. }.}.../*.
1f84a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 ** Record in the
1f84b 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 column cache th
1f84c 61 74 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 at a particular
1f84d 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a column from a.**
1f84e 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c particular tabl
1f84f 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
1f850 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 particular regi
1f851 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ster..*/.SQLITE_
1f852 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f853 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f ite3ExprCacheSto
1f854 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 re(Parse *pParse
1f855 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20 , int iTab, int
1f856 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b iCol, int iReg){
1f857 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1f858 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64 minLru;. int id
1f859 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79 xLru;. struct y
1f85a 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 ColCache *p;..
1f85b 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63 65 /* First replace
1f85c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65 6e any existing en
1f85d 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 try */. for(i=0
1f85e 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
1f85f 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
1f860 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
1f861 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
1f862 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54 61 ->iReg && p->iTa
1f863 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d 3e ble==iTab && p->
1f864 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b iColumn==iCol ){
1f865 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 . cacheEntr
1f866 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 yClear(pParse, p
1f867 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 );. p->iLev
1f868 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 el = pParse->iCa
1f869 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 cheLevel;.
1f86a 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a p->iReg = iReg;.
1f86b 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e p->affChan
1f86c 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d ge = 0;. p-
1f86d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 >lru = pParse->i
1f86e 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 CacheCnt++;.
1f86f 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
1f870 20 20 7d 0a 20 20 69 66 28 20 69 52 65 67 3c 3d }. if( iReg<=
1f871 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 0 ) return;.. /
1f872 2a 20 46 69 6e 64 20 61 6e 20 65 6d 70 74 79 20 * Find an empty
1f873 73 6c 6f 74 20 61 6e 64 20 72 65 70 6c 61 63 65 slot and replace
1f874 20 69 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 it */. for(i=0
1f875 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
1f876 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
1f877 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
1f878 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
1f879 2d 3e 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 ->iReg==0 ){.
1f87a 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 p->iLevel = p
1f87b 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 Parse->iCacheLev
1f87c 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 54 61 el;. p->iTa
1f87d 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 ble = iTab;.
1f87e 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 p->iColumn = i
1f87f 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 Col;. p->iR
1f880 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 eg = iReg;.
1f881 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 p->affChange =
1f882 30 3b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 0;. p->temp
1f883 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 Reg = 0;. p
1f884 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e ->lru = pParse->
1f885 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 iCacheCnt++;.
1f886 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
1f887 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 . }.. /* Repla
1f888 63 65 20 74 68 65 20 6c 61 73 74 20 72 65 63 65 ce the last rece
1f889 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d ntly used */. m
1f88a 69 6e 4c 72 75 20 3d 20 30 78 37 66 66 66 66 66 inLru = 0x7fffff
1f88b 66 66 3b 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d ff;. idxLru = -
1f88c 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 1;. for(i=0, p=
1f88d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
1f88e 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f e; i<SQLITE_N_CO
1f88f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b LCACHE; i++, p++
1f890 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6c 72 ){. if( p->lr
1f891 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20 u<minLru ){.
1f892 20 20 69 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20 idxLru = i;.
1f893 20 20 20 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e minLru = p->
1f894 6c 72 75 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 lru;. }. }.
1f895 20 69 66 28 20 69 64 78 4c 72 75 3e 3d 30 20 29 if( idxLru>=0 )
1f896 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 {. p = &pPars
1f897 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 78 e->aColCache[idx
1f898 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c 65 Lru];. p->iLe
1f899 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 vel = pParse->iC
1f89a 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 70 acheLevel;. p
1f89b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b ->iTable = iTab;
1f89c 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 . p->iColumn
1f89d 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 = iCol;. p->i
1f89e 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 Reg = iReg;.
1f89f 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 p->affChange = 0
1f8a0 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 ;. p->tempReg
1f8a1 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 75 = 0;. p->lru
1f8a2 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
1f8a3 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 75 eCnt++;. retu
1f8a4 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rn;. }.}../*.**
1f8a5 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 61 Indicate that a
1f8a6 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65 69 register is bei
1f8a7 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
1f8a8 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69 73 Purge the regis
1f8a9 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ter.** from the
1f8aa 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f column cache..*/
1f8ab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f8ac 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1f8ad 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73 CacheRemove(Pars
1f8ae 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
1f8af 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 Reg){. int i;.
1f8b0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
1f8b1 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c e *p;. for(i=0,
1f8b2 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
1f8b3 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
1f8b4 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
1f8b5 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
1f8b6 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 >iReg==iReg ){.
1f8b7 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 cacheEntryC
1f8b8 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b lear(pParse, p);
1f8b9 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d . p->iReg =
1f8ba 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
1f8bb 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 ./*.** Remember
1f8bc 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c 75 the current colu
1f8bd 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78 74 mn cache context
1f8be 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72 69 . Any new entri
1f8bf 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64 65 es added.** adde
1f8c0 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 d to the column
1f8c1 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 cache after this
1f8c2 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 65 call are remove
1f8c3 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63 6f d when the.** co
1f8c4 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 20 rresponding pop
1f8c5 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 occurs..*/.SQLIT
1f8c6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1f8c7 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
1f8c8 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 ush(Parse *pPars
1f8c9 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 e){. pParse->iC
1f8ca 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a acheLevel++;.}..
1f8cb 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72 6f /*.** Remove fro
1f8cc 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 m the column cac
1f8cd 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74 he any entries t
1f8ce 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20 73 hat were added s
1f8cf 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 ince the.** the
1f8d0 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68 20 previous N Push
1f8d1 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e 20 operations. In
1f8d2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 73 other words, res
1f8d3 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a 2a tore the cache.*
1f8d4 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69 * to the state i
1f8d5 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68 65 t was in N Pushe
1f8d6 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 s ago..*/.SQLITE
1f8d7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f8d8 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
1f8d9 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c p(Parse *pParse,
1f8da 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20 69 int N){. int i
1f8db 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
1f8dc 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 ache *p;. asser
1f8dd 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73 65 t( N>0 );. asse
1f8de 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 rt( pParse->iCac
1f8df 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20 heLevel>=N );.
1f8e0 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
1f8e1 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28 vel -= N;. for(
1f8e2 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
1f8e3 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
1f8e4 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
1f8e5 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
1f8e6 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e ( p->iReg && p->
1f8e7 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e 69 iLevel>pParse->i
1f8e8 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20 20 CacheLevel ){.
1f8e9 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c cacheEntryCl
1f8ea 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a ear(pParse, p);.
1f8eb 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 p->iReg =
1f8ec 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
1f8ed 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 63 /*.** When a cac
1f8ee 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 hed column is re
1f8ef 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 used, make sure
1f8f0 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74 65 that its registe
1f8f1 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 r is.** no longe
1f8f2 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 61 r available as a
1f8f3 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e 20 temp register.
1f8f4 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20 20 ticket #3879:
1f8f5 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65 67 that same.** reg
1f8f6 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20 69 ister might be i
1f8f7 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 6d n the cache in m
1f8f8 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c 20 ultiple places,
1f8f9 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a 2a so be sure to.**
1f8fa 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a get them all..*
1f8fb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
1f8fc 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69 lite3ExprCachePi
1f8fd 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65 20 nRegister(Parse
1f8fe 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 *pParse, int iRe
1f8ff 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 g){. int i;. s
1f900 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
1f901 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 *p;. for(i=0, p
1f902 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
1f903 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
1f904 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
1f905 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 +){. if( p->i
1f906 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 Reg==iReg ){.
1f907 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 p->tempReg =
1f908 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
1f909 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
1f90a 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78 ode that will ex
1f90b 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d tract the iColum
1f90c 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d n-th column from
1f90d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61 .** table pTab a
1f90e 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c nd store the col
1f90f 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72 umn value in a r
1f910 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66 egister. An eff
1f911 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 ort.** is made t
1f912 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 o store the colu
1f913 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 mn value in regi
1f914 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74 ster iReg, but t
1f915 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 his is.** not gu
1f916 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c aranteed. The l
1f917 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 ocation of the c
1f918 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 olumn value is r
1f919 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
1f91a 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 here must be an
1f91b 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 open cursor to p
1f91c 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 Tab in iTable wh
1f91d 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
1f91e 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 ** is called. I
1f91f 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e f iColumn<0 then
1f920 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 code is generat
1f921 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73 ed that extracts
1f922 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a the rowid..**.*
1f923 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
1f924 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20 ight attempt to
1f925 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 reuse the value
1f926 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 of the column th
1f927 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 at.** has alread
1f928 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e y been loaded in
1f929 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 to a register.
1f92a 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 The value will a
1f92b 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 lways.** be used
1f92c 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75 if it has not u
1f92d 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 ndergone any aff
1f92e 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20 inity changes.
1f92f 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 But if.** an aff
1f930 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 inity change has
1f931 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 occurred, then
1f932 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65 the cached value
1f933 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a will only be.**
1f934 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 used if allowAf
1f935 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a fChng is true..*
1f936 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f937 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
1f938 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 CodeGetColumn(.
1f939 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1f93a 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 /* Parsing and
1f93b 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
1f93c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 context */. Ta
1f93d 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f ble *pTab, /
1f93e 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 * Description of
1f93f 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 the table we ar
1f940 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a e reading from *
1f941 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c /. int iColumn,
1f942 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1f943 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d the table colum
1f944 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c n */. int iTabl
1f945 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 e, /* The c
1f946 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
1f947 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 o the table */.
1f948 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20 int iReg,
1f949 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c /* Store resul
1f94a 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ts here */. int
1f94b 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a allowAffChng /*
1f94c 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61 True if prior a
1f94d 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20 ffinity changes
1f94e 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 are OK */.){. V
1f94f 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
1f950 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b >pVdbe;. int i;
1f951 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 . struct yColCa
1f952 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 che *p;.. for(i
1f953 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
1f954 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
1f955 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
1f956 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, p++){. if(
1f957 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70 2d p->iReg>0 && p-
1f958 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 >iTable==iTable
1f959 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 && p->iColumn==i
1f95a 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 Column.
1f95b 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61 && (!p->affCha
1f95c 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 nge || allowAffC
1f95d 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20 hng) ){.#if 0.
1f95e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f95f 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70 ddOp0(v, OP_Noop
1f960 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d );. VdbeCom
1f961 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74 ment((v, "OPT: t
1f962 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25 ab%d.col%d -> r%
1f963 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c d", iTable, iCol
1f964 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a umn, p->iReg));.
1f965 23 65 6e 64 69 66 0a 20 20 20 20 20 20 70 2d 3e #endif. p->
1f966 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 lru = pParse->iC
1f967 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 acheCnt++;.
1f968 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
1f969 65 50 69 6e 52 65 67 69 73 74 65 72 28 70 50 61 ePinRegister(pPa
1f96a 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b 0a 20 rse, p->iReg);.
1f96b 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 return p->i
1f96c 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 20 Reg;. }. }
1f96d 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 . assert( v!=0
1f96e 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e );. if( iColumn
1f96f 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 <0 ){. sqlite
1f970 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1f971 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c 65 2c P_Rowid, iTable,
1f972 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 20 iReg);. }else
1f973 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 if( pTab==0 ){.
1f974 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1f975 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d dOp3(v, OP_Colum
1f976 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 n, iTable, iColu
1f977 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c mn, iReg);. }el
1f978 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d se{. int op =
1f979 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1f97a 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20 ? OP_VColumn :
1f97b 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 OP_Column;. s
1f97c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1f97d 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 (v, op, iTable,
1f97e 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a iColumn, iReg);.
1f97f 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d sqlite3Colum
1f980 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 nDefault(v, pTab
1f981 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66 6e , iColumn);.#ifn
1f982 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f983 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 FLOATING_POINT.
1f984 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43 6f if( pTab->aCo
1f985 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e l[iColumn].affin
1f986 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
1f987 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 REAL ){. sq
1f988 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1f989 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 v, OP_RealAffini
1f98a 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d ty, iReg);. }
1f98b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 .#endif. }. sq
1f98c 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 lite3ExprCacheSt
1f98d 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 ore(pParse, iTab
1f98e 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 le, iColumn, iRe
1f98f 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65 g);. return iRe
1f990 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 g;.}../*.** Clea
1f991 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 r all column cac
1f992 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53 he entries..*/.S
1f993 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f994 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
1f995 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a cheClear(Parse *
1f996 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 pParse){. int i
1f997 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
1f998 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 ache *p;.. for(
1f999 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
1f99a 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
1f99b 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
1f99c 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
1f99d 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20 ( p->iReg ){.
1f99e 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 cacheEntryCle
1f99f 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 ar(pParse, p);.
1f9a0 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 p->iReg = 0
1f9a1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
1f9a2 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 *.** Record the
1f9a3 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 fact that an aff
1f9a4 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 inity change has
1f9a5 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f occurred on iCo
1f9a6 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 unt.** registers
1f9a7 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 starting with i
1f9a8 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Start..*/.SQLITE
1f9a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f9aa 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
1f9ab 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 finityChange(Par
1f9ac 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1f9ad 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 iStart, int iCou
1f9ae 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 nt){. int iEnd
1f9af 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e = iStart + iCoun
1f9b0 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a t - 1;. int i;.
1f9b1 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
1f9b2 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 he *p;. for(i=0
1f9b3 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
1f9b4 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
1f9b5 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
1f9b6 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 p++){. int r
1f9b7 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 = p->iReg;.
1f9b8 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26 if( r>=iStart &&
1f9b9 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 r<=iEnd ){.
1f9ba 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d p->affChange =
1f9bb 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 1;. }. }.}.
1f9bc 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1f9bd 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e code to move con
1f9be 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 tent from regist
1f9bf 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f ers iFrom...iFro
1f9c0 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 m+nReg-1.** over
1f9c1 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 to iTo..iTo+nRe
1f9c2 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f g-1. Keep the co
1f9c3 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f lumn cache up-to
1f9c4 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 -date..*/.SQLITE
1f9c5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f9c6 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 lite3ExprCodeMov
1f9c7 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
1f9c8 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 int iFrom, int
1f9c9 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a iTo, int nReg){.
1f9ca 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
1f9cb 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
1f9cc 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f if( iFrom==iTo
1f9cd 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c ) return;. sql
1f9ce 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 ite3VdbeAddOp3(p
1f9cf 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 Parse->pVdbe, OP
1f9d0 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 54 _Move, iFrom, iT
1f9d1 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72 28 o, nReg);. for(
1f9d2 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
1f9d3 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
1f9d4 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
1f9d5 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e ++, p++){. in
1f9d6 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 t x = p->iReg;.
1f9d7 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d 20 if( x>=iFrom
1f9d8 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 20 && x<iFrom+nReg
1f9d9 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 ){. p->iReg
1f9da 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 += iTo-iFrom;.
1f9db 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1f9dc 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1f9dd 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20 to copy content
1f9de 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 from registers i
1f9df 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 From...iFrom+nRe
1f9e0 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 g-1.** over to i
1f9e1 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a To..iTo+nReg-1..
1f9e2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f9e3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1f9e4 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 prCodeCopy(Parse
1f9e5 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
1f9e6 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e rom, int iTo, in
1f9e7 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t nReg){. int i
1f9e8 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 ;. if( iFrom==i
1f9e9 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 To ) return;. f
1f9ea 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 or(i=0; i<nReg;
1f9eb 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
1f9ec 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 3VdbeAddOp2(pPar
1f9ed 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f se->pVdbe, OP_Co
1f9ee 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f py, iFrom+i, iTo
1f9ef 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a +i);. }.}../*.*
1f9f0 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
1f9f1 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e any register in
1f9f2 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d the range iFrom
1f9f3 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 ..iTo (inclusive
1f9f4 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 ).** is used as
1f9f5 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 part of the colu
1f9f6 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 mn cache..*/.sta
1f9f7 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f tic int usedAsCo
1f9f8 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 lumnCache(Parse
1f9f9 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 *pParse, int iFr
1f9fa 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 om, int iTo){.
1f9fb 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1f9fc 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 yColCache *p;.
1f9fd 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
1f9fe 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
1f9ff 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
1fa00 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
1fa01 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 int r = p->iRe
1fa02 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 g;. if( r>=iF
1fa03 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 rom && r<=iTo )
1fa04 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
1fa05 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
1fa06 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69 ** If the last i
1fa07 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 nstruction coded
1fa08 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c is an ephemeral
1fa09 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a copy of any of.
1fa0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73 ** the registers
1fa0b 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67 in the nReg reg
1fa0c 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 isters beginning
1fa0d 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e with iReg, then
1fa0e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20 .** convert the
1fa0f 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e last instruction
1fa10 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 from OP_SCopy t
1fa11 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 o OP_Copy..*/.SQ
1fa12 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1fa13 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 d sqlite3ExprHar
1fa14 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 dCopy(Parse *pPa
1fa15 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 rse, int iReg, i
1fa16 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt nReg){. int
1fa17 61 64 64 72 3b 0a 20 20 56 64 62 65 4f 70 20 2a addr;. VdbeOp *
1fa18 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a pOp;. Vdbe *v;.
1fa19 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 . v = pParse->p
1fa1a 56 64 62 65 3b 0a 20 20 61 64 64 72 20 3d 20 73 Vdbe;. addr = s
1fa1b 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
1fa1c 74 41 64 64 72 28 76 29 3b 0a 20 20 70 4f 70 20 tAddr(v);. pOp
1fa1d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 = sqlite3VdbeGet
1fa1e 4f 70 28 76 2c 20 61 64 64 72 2d 31 29 3b 0a 20 Op(v, addr-1);.
1fa1f 20 61 73 73 65 72 74 28 20 70 4f 70 20 7c 7c 20 assert( pOp ||
1fa20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
1fa21 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 ocFailed );. if
1fa22 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 ( pOp && pOp->op
1fa23 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26 code==OP_SCopy &
1fa24 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20 & pOp->p1>=iReg
1fa25 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b && pOp->p1<iReg+
1fa26 6e 52 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d nReg ){. pOp-
1fa27 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70 >opcode = OP_Cop
1fa28 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 y;. }.}../*.**
1fa29 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1fa2a 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 store the value
1fa2b 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 of the iAlias-t
1fa2c 68 20 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73 h alias in regis
1fa2d 74 65 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20 ter.** target.
1fa2e 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
1fa2f 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70 his is called, p
1fa30 45 78 70 72 20 69 73 20 65 76 61 6c 75 61 74 65 Expr is evaluate
1fa31 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 d to compute.**
1fa32 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1fa33 20 61 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c alias. The val
1fa34 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ue is stored in
1fa35 61 6e 20 61 75 78 69 6c 69 61 72 79 20 72 65 67 an auxiliary reg
1fa36 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 ister.** and the
1fa37 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20 number of that
1fa38 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74 75 register is retu
1fa39 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71 rned. On subseq
1fa3a 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74 uent calls,.** t
1fa3b 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 he register numb
1fa3c 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 er is returned w
1fa3d 69 74 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e ithout generatin
1fa3e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a g any code..**.*
1fa3f 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f * Note that in o
1fa40 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f rder for this to
1fa41 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74 work, code must
1fa42 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e be generated in
1fa43 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64 the.** same ord
1fa44 65 72 20 74 68 61 74 20 69 74 20 69 73 20 65 78 er that it is ex
1fa45 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c ecuted..**.** Al
1fa46 69 61 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72 iases are number
1fa47 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 ed starting with
1fa48 20 31 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69 1. So iAlias i
1fa49 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a s in the range.*
1fa4a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65 * of 1 to pParse
1fa4b 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69 ->nAlias inclusi
1fa4c 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72 ve. .**.** pPar
1fa4d 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 se->aAlias[iAlia
1fa4e 73 2d 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65 s-1] records the
1fa4f 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
1fa50 20 77 68 65 72 65 20 74 68 65 20 76 61 6c 75 65 where the value
1fa51 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61 .** of the iAlia
1fa52 73 2d 74 68 20 61 6c 69 61 73 20 69 73 20 73 74 s-th alias is st
1fa53 6f 72 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20 ored. If zero,
1fa54 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
1fa55 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73 the.** alias has
1fa56 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f not yet been co
1fa57 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 mputed..*/.stati
1fa58 63 20 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28 c int codeAlias(
1fa59 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
1fa5a 6e 74 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20 nt iAlias, Expr
1fa5b 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 *pExpr, int targ
1fa5c 65 74 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c et){.#if 0. sql
1fa5d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1fa5e 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 e->db;. int iRe
1fa5f 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d g;. if( pParse-
1fa60 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 >nAliasAlloc<pPa
1fa61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 rse->nAlias ){.
1fa62 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 pParse->aAlia
1fa63 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 s = sqlite3DbRea
1fa64 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 llocOrFree(db, p
1fa65 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 Parse->aAlias,.
1fa66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa68 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 sizeof(pParse->a
1fa69 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 Alias[0])*pParse
1fa6a 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 ->nAlias );.
1fa6b 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 testcase( db->ma
1fa6c 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 llocFailed && pP
1fa6d 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f arse->nAliasAllo
1fa6e 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 c>0 );. if( d
1fa6f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1fa70 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 ) return 0;.
1fa71 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e memset(&pParse->
1fa72 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e aAlias[pParse->n
1fa73 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a AliasAlloc], 0,.
1fa74 20 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72 (pPar
1fa75 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 se->nAlias-pPars
1fa76 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a e->nAliasAlloc)*
1fa77 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 sizeof(pParse->a
1fa78 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 Alias[0]));.
1fa79 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c pParse->nAliasAl
1fa7a 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 loc = pParse->nA
1fa7b 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 lias;. }. asse
1fa7c 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 rt( iAlias>0 &&
1fa7d 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e iAlias<=pParse->
1fa7e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 nAlias );. iReg
1fa7f 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 = pParse->aAlia
1fa80 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 s[iAlias-1];. i
1fa81 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 f( iReg==0 ){.
1fa82 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43 if( pParse->iC
1fa83 61 63 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20 acheLevel>0 ){.
1fa84 20 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 iReg = sqli
1fa85 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
1fa86 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c t(pParse, pExpr,
1fa87 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 target);. }e
1fa88 6c 73 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20 lse{. iReg
1fa89 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
1fa8a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1fa8b 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 xprCode(pParse,
1fa8c 70 45 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 pExpr, iReg);.
1fa8d 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 pParse->aAli
1fa8e 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 as[iAlias-1] = i
1fa8f 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 Reg;. }. }.
1fa90 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65 return iReg;.#e
1fa91 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
1fa92 41 4d 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a AMETER(iAlias);.
1fa93 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1fa94 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1fa95 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 Parse, pExpr, ta
1fa96 72 67 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a rget);.#endif.}.
1fa97 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1fa98 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75 code into the cu
1fa99 72 72 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76 rrent Vdbe to ev
1fa9a 61 6c 75 61 74 65 20 74 68 65 20 67 69 76 65 6e aluate the given
1fa9b 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 .** expression.
1fa9c 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 Attempt to stor
1fa9d 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e e the results in
1fa9e 20 72 65 67 69 73 74 65 72 20 22 74 61 72 67 65 register "targe
1fa9f 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 t"..** Return th
1faa0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 e register where
1faa1 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f results are sto
1faa2 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 red..**.** With
1faa3 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 this routine, th
1faa4 65 72 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e ere is no guaran
1faa5 74 65 65 20 74 68 61 74 20 72 65 73 75 6c 74 73 tee that results
1faa6 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 will.** be stor
1faa7 65 64 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54 ed in target. T
1faa8 68 65 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20 he result might
1faa9 62 65 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d be stored in som
1faaa 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 e other.** regis
1faab 74 65 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e ter if it is con
1faac 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f venient to do so
1faad 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 . The calling f
1faae 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 unction.** must
1faaf 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e check the return
1fab0 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 code and move t
1fab1 68 65 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68 he results to th
1fab2 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 e desired.** reg
1fab3 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ister..*/.SQLITE
1fab4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1fab5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
1fab6 65 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 et(Parse *pParse
1fab7 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 , Expr *pExpr, i
1fab8 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 nt target){. Vd
1fab9 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e be *v = pParse->
1faba 70 56 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 pVdbe; /* The V
1fabb 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 M under construc
1fabc 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 tion */. int op
1fabd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1fabe 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 /* The opcod
1fabf 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f e being coded */
1fac0 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 . int inReg = t
1fac1 61 72 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 arget; /*
1fac2 52 65 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69 Results stored i
1fac3 6e 20 72 65 67 69 73 74 65 72 20 69 6e 52 65 67 n register inReg
1fac4 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 */. int regFre
1fac5 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 e1 = 0;
1fac6 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 /* If non-zero f
1fac7 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 ree this tempora
1fac8 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 ry register */.
1fac9 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 int regFree2 =
1faca 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 0; /* If
1facb 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 non-zero free t
1facc 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 his temporary re
1facd 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 gister */. int
1face 72 31 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 r1, r2, r3, r4;
1facf 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 /* Various
1fad0 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
1fad1 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a s */. sqlite3 *
1fad2 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 db;.. db = pPar
1fad3 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 se->db;. assert
1fad4 28 20 76 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 ( v!=0 || db->ma
1fad5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1fad6 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 assert( target>0
1fad7 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 && target<=pPar
1fad8 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66 se->nMem );. if
1fad9 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( v==0 ) return
1fada 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 0;.. if( pExpr=
1fadb 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 54 =0 ){. op = T
1fadc 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b K_NULL;. }else{
1fadd 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72 2d . op = pExpr-
1fade 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 >op;. }. switc
1fadf 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
1fae0 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a e TK_AGG_COLUMN:
1fae1 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f {. AggInfo
1fae2 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 45 78 *pAggInfo = pEx
1fae3 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 pr->pAggInfo;.
1fae4 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e struct AggIn
1fae5 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 fo_col *pCol = &
1fae6 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 pAggInfo->aCol[p
1fae7 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 Expr->iAgg];.
1fae8 20 20 20 69 66 28 20 21 70 41 67 67 49 6e 66 6f if( !pAggInfo
1fae9 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 7b 0a ->directMode ){.
1faea 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1faeb 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a pCol->iMem>0 );.
1faec 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 inReg =
1faed 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 pCol->iMem;.
1faee 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1faef 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67 49 }else if( pAggI
1faf0 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67 49 nfo->useSortingI
1faf1 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 dx ){. sq
1faf2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1faf3 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 v, OP_Column, pA
1faf4 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 ggInfo->sortingI
1faf5 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 dx,.
1faf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1faf7 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 pCol->iSorterC
1faf8 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a olumn, target);.
1faf9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1fafa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 }. /*
1fafb 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 Otherwise, fall
1fafc 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 4b thru into the TK
1fafd 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a _COLUMN case */.
1fafe 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1faff 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 K_COLUMN: {.
1fb00 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 if( pExpr->iTa
1fb01 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 ble<0 ){.
1fb02 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 /* This only ha
1fb03 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e ppens when codin
1fb04 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 g check constrai
1fb05 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 nts */. a
1fb06 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63 ssert( pParse->c
1fb07 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20 20 kBase>0 );.
1fb08 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 inReg = pExpr
1fb09 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72 ->iColumn + pPar
1fb0a 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20 se->ckBase;.
1fb0b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1fb0c 20 74 65 73 74 63 61 73 65 28 20 28 70 45 78 70 testcase( (pExp
1fb0d 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e r->flags & EP_An
1fb0e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 20 20 yAff)!=0 );.
1fb0f 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 inReg = sqli
1fb10 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f te3ExprCodeGetCo
1fb11 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 45 78 lumn(pParse, pEx
1fb12 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 20 20 pr->pTab,.
1fb13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb14 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
1fb15 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 ->iColumn, pExpr
1fb16 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74 ->iTable, target
1fb17 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1fb18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb19 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 pExpr->flags
1fb1a 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a 20 20 & EP_AnyAff);.
1fb1b 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1fb1c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1fb1d 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a e TK_INTEGER: {.
1fb1e 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 codeIntege
1fb1f 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c 20 74 r(v, pExpr, 0, t
1fb20 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 arget);. br
1fb21 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1fb22 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a ase TK_FLOAT: {.
1fb23 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 codeReal(v
1fb24 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e , (char*)pExpr->
1fb25 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e token.z, pExpr->
1fb26 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74 61 72 67 token.n, 0, targ
1fb27 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b et);. break
1fb28 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1fb29 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 TK_STRING: {.
1fb2a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fb2b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 ddOp4(v, OP_Stri
1fb2c 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 ng8, 0, target,
1fb2d 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
1fb2e 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 (char
1fb2f 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
1fb30 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e , pExpr->token.n
1fb31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1fb32 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1fb33 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 K_NULL: {.
1fb34 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fb35 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
1fb36 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1fb37 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 break;. }.#if
1fb38 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1fb39 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 _BLOB_LITERAL.
1fb3a 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 case TK_BLOB:
1fb3b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 {. int n;.
1fb3c 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1fb3d 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a *z;. char *
1fb3e 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73 73 zBlob;. ass
1fb3f 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 ert( pExpr->toke
1fb40 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 20 20 n.n>=3 );.
1fb41 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 assert( pExpr->t
1fb42 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c oken.z[0]=='x' |
1fb43 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a | pExpr->token.z
1fb44 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 [0]=='X' );.
1fb45 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1fb46 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 >token.z[1]=='\'
1fb47 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ' );. asser
1fb48 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
1fb49 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e z[pExpr->token.n
1fb4a 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 -1]=='\'' );.
1fb4b 20 20 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f n = pExpr->to
1fb4c 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 20 ken.n - 3;.
1fb4d 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 z = (char*)pExp
1fb4e 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a r->token.z + 2;.
1fb4f 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 zBlob = sq
1fb50 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 lite3HexToBlob(s
1fb51 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c qlite3VdbeDb(v),
1fb52 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 z, n);. sq
1fb53 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
1fb54 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c v, OP_Blob, n/2,
1fb55 20 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f target, 0, zBlo
1fb56 62 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a b, P4_DYNAMIC);.
1fb57 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fb58 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 }.#endif. ca
1fb59 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 se TK_VARIABLE:
1fb5a 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 50 72 69 {. int iPri
1fb5b 6f 72 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 or;. VdbeOp
1fb5c 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 69 66 28 *pOp;. if(
1fb5d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3c pExpr->token.n<
1fb5e 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 =1. && (
1fb5f 69 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 iPrior = sqlite3
1fb60 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
1fb61 76 29 2d 31 29 3e 3d 30 0a 20 20 20 20 20 20 20 v)-1)>=0.
1fb62 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71 6c 69 && (pOp = sqli
1fb63 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 te3VdbeGetOp(v,
1fb64 69 50 72 69 6f 72 29 29 2d 3e 6f 70 63 6f 64 65 iPrior))->opcode
1fb65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 0a 20 20 ==OP_Variable.
1fb66 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 && pOp->p
1fb67 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72 1+pOp->p3==pExpr
1fb68 2d 3e 69 54 61 62 6c 65 0a 20 20 20 20 20 20 20 ->iTable.
1fb69 20 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 && pOp->p2+pOp
1fb6a 2d 3e 70 33 3d 3d 74 61 72 67 65 74 0a 20 20 20 ->p3==target.
1fb6b 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 34 && pOp->p4
1fb6c 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 .z==0. ){.
1fb6d 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /* If the
1fb6e 20 70 72 65 76 69 6f 75 73 20 69 6e 73 74 72 75 previous instru
1fb6f 63 74 69 6f 6e 20 77 61 73 20 61 20 63 6f 70 79 ction was a copy
1fb70 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 of the previous
1fb71 20 75 6e 6e 61 6d 65 64 0a 20 20 20 20 20 20 20 unnamed.
1fb72 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e ** parameter in
1fb73 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 to the previous
1fb74 72 65 67 69 73 74 65 72 2c 20 74 68 65 6e 20 73 register, then s
1fb75 69 6d 70 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20 imply increment
1fb76 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 the. ** r
1fb77 65 70 65 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74 epeat count on t
1fb78 68 65 20 70 72 69 6f 72 20 69 6e 73 74 72 75 63 he prior instruc
1fb79 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e tion rather than
1fb7a 20 6d 61 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20 making a new.
1fb7b 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 ** instruc
1fb7c 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f tion.. */
1fb7d 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 . pOp->p3
1fb7e 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ++;. }else{
1fb7f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1fb80 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1fb81 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 _Variable, pExpr
1fb82 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74 ->iTable, target
1fb83 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 1);. if
1fb84 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e ( pExpr->token.n
1fb85 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >1 ){.
1fb86 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1fb87 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 eP4(v, -1, (char
1fb88 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
1fb89 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e , pExpr->token.n
1fb8a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1fb8b 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
1fb8c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1fb8d 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a TK_REGISTER: {.
1fb8e 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 inReg = pE
1fb8f 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 xpr->iTable;.
1fb90 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1fb91 20 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 case TK_AS:
1fb92 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 {. inReg =
1fb93 63 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 codeAlias(pParse
1fb94 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
1fb95 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 pExpr->pLeft, t
1fb96 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 arget);. br
1fb97 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 eak;. }.#ifnd
1fb98 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1fb99 41 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f AST. case TK_
1fb9a 43 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a CAST: {. /*
1fb9b 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 Expressions of
1fb9c 74 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 the form: CAST
1fb9d 28 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 (pLeft AS token)
1fb9e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 */. int af
1fb9f 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 f, to_op;.
1fba0 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 inReg = sqlite3E
1fba1 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
1fba2 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
1fba3 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 ft, target);.
1fba4 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
1fba5 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 AffinityType(&pE
1fba6 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 xpr->token);.
1fba7 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d to_op = aff -
1fba8 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1fba9 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 + OP_ToText;.
1fbaa 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f assert( to_o
1fbab 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 p==OP_ToText
1fbac 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 || aff!=SQLITE_A
1fbad 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 FF_TEXT );.
1fbae 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f assert( to_o
1fbaf 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 p==OP_ToBlob
1fbb0 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 || aff!=SQLITE_A
1fbb1 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 FF_NONE );.
1fbb2 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f assert( to_o
1fbb3 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 p==OP_ToNumeric
1fbb4 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 || aff!=SQLITE_A
1fbb5 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 FF_NUMERIC );.
1fbb6 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f assert( to_o
1fbb7 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 p==OP_ToInt
1fbb8 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 || aff!=SQLITE_A
1fbb9 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 FF_INTEGER );.
1fbba 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f assert( to_o
1fbbb 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 p==OP_ToReal
1fbbc 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 || aff!=SQLITE_A
1fbbd 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 FF_REAL );.
1fbbe 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f testcase( to
1fbbf 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 _op==OP_ToText )
1fbc0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fbc1 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c ( to_op==OP_ToBl
1fbc2 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ob );. test
1fbc3 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f case( to_op==OP_
1fbc4 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 ToNumeric );.
1fbc5 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f testcase( to_
1fbc6 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a op==OP_ToInt );.
1fbc7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fbc8 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c to_op==OP_ToReal
1fbc9 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e );. if( in
1fbca 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 Reg!=target ){.
1fbcb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1fbcc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 beAddOp2(v, OP_S
1fbcd 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 Copy, inReg, tar
1fbce 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e get);. in
1fbcf 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 Reg = target;.
1fbd0 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1fbd1 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
1fbd2 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a to_op, inReg);.
1fbd3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fbd4 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 usedAsColumnCach
1fbd5 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c e(pParse, inReg,
1fbd6 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 inReg) );.
1fbd7 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
1fbd8 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
1fbd9 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 pParse, inReg, 1
1fbda 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1fbdb 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
1fbdc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 SQLITE_OMIT_CAST
1fbdd 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1fbde 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f LT:. case TK_
1fbdf 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f LE:. case TK_
1fbe0 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f GT:. case TK_
1fbe1 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f GE:. case TK_
1fbe2 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f NE:. case TK_
1fbe3 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 EQ: {. asse
1fbe4 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 rt( TK_LT==OP_Lt
1fbe5 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1fbe6 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 ( TK_LE==OP_Le )
1fbe7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1fbe8 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a TK_GT==OP_Gt );.
1fbe9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1fbea 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 _GE==OP_Ge );.
1fbeb 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 assert( TK_E
1fbec 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 Q==OP_Eq );.
1fbed 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d assert( TK_NE=
1fbee 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 =OP_Ne );.
1fbef 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1fbf0 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _LT );. tes
1fbf1 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 tcase( op==TK_LE
1fbf2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fbf3 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b se( op==TK_GT );
1fbf4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fbf5 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 op==TK_GE );.
1fbf6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1fbf7 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 ==TK_EQ );.
1fbf8 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1fbf9 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f K_NE );. co
1fbfa 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 deCompareOperand
1fbfb 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d s(pParse, pExpr-
1fbfc 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 >pLeft, &r1, &re
1fbfd 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 gFree1,.
1fbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbff 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1fc00 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 >pRight, &r2, &r
1fc01 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
1fc02 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 codeCompare(pPar
1fc03 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
1fc04 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
1fc05 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 op,.
1fc06 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 r1, r2, i
1fc07 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f nReg, SQLITE_STO
1fc08 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 REP2);. tes
1fc09 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
1fc0a 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
1fc0b 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
1fc0c 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
1fc0d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1fc0e 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61 73 TK_AND:. cas
1fc0f 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 73 e TK_OR:. cas
1fc10 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 63 e TK_PLUS:. c
1fc11 61 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20 20 ase TK_STAR:.
1fc12 20 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a case TK_MINUS:.
1fc13 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d 3a case TK_REM:
1fc14 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 . case TK_BIT
1fc15 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b AND:. case TK
1fc16 5f 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73 65 _BITOR:. case
1fc17 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20 63 TK_SLASH:. c
1fc18 61 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 ase TK_LSHIFT:.
1fc19 20 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49 46 case TK_RSHIF
1fc1a 54 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b 5f T: . case TK_
1fc1b 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20 20 CONCAT: {.
1fc1c 61 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d assert( TK_AND==
1fc1d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 OP_And );.
1fc1e 61 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f assert( TK_OR==O
1fc1f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Or );. as
1fc20 73 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f sert( TK_PLUS==O
1fc21 50 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20 61 P_Add );. a
1fc22 73 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53 3d ssert( TK_MINUS=
1fc23 3d 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b 0a =OP_Subtract );.
1fc24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1fc25 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 _REM==OP_Remaind
1fc26 65 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 er );. asse
1fc27 72 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f rt( TK_BITAND==O
1fc28 50 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20 20 P_BitAnd );.
1fc29 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 assert( TK_BIT
1fc2a 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b 0a OR==OP_BitOr );.
1fc2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1fc2c 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 64 _SLASH==OP_Divid
1fc2d 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 e );. asser
1fc2e 74 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 t( TK_LSHIFT==OP
1fc2f 5f 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20 20 _ShiftLeft );.
1fc30 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 assert( TK_R
1fc31 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 52 SHIFT==OP_ShiftR
1fc32 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61 73 ight );. as
1fc33 73 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54 3d sert( TK_CONCAT=
1fc34 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 20 =OP_Concat );.
1fc35 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1fc36 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 ==TK_AND );.
1fc37 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1fc38 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 TK_OR );. t
1fc39 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1fc3a 50 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 PLUS );. te
1fc3b 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d stcase( op==TK_M
1fc3c 49 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 INUS );. te
1fc3d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 stcase( op==TK_R
1fc3e 45 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 EM );. test
1fc3f 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 case( op==TK_BIT
1fc40 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 AND );. tes
1fc41 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 tcase( op==TK_BI
1fc42 54 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 TOR );. tes
1fc43 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c tcase( op==TK_SL
1fc44 41 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ASH );. tes
1fc45 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 tcase( op==TK_LS
1fc46 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65 HIFT );. te
1fc47 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 stcase( op==TK_R
1fc48 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 SHIFT );. t
1fc49 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1fc4a 43 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20 20 CONCAT );.
1fc4b 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r1 = sqlite3Expr
1fc4c 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
1fc4d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 pExpr->pLeft, &
1fc4e 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
1fc4f 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r2 = sqlite3Exp
1fc50 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
1fc51 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
1fc52 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 ®Free2);.
1fc53 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1fc54 64 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c 20 dOp3(v, op, r2,
1fc55 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 r1, target);.
1fc56 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
1fc57 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
1fc58 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
1fc59 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
1fc5a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1fc5b 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 case TK_UMINUS
1fc5c 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a : {. Expr *
1fc5d 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 pLeft = pExpr->p
1fc5e 4c 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73 65 Left;. asse
1fc5f 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 20 rt( pLeft );.
1fc60 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 if( pLeft->op
1fc61 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 ==TK_FLOAT ){.
1fc62 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 codeReal(v
1fc63 2c 20 28 63 68 61 72 2a 29 70 4c 65 66 74 2d 3e , (char*)pLeft->
1fc64 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65 66 74 2d 3e token.z, pLeft->
1fc65 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20 74 61 72 67 token.n, 1, targ
1fc66 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 et);. }else
1fc67 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d if( pLeft->op==
1fc68 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 TK_INTEGER ){.
1fc69 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 codeIntege
1fc6a 72 28 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 r(v, pLeft, 1, t
1fc6b 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 arget);. }e
1fc6c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 lse{. reg
1fc6d 46 72 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c Free1 = r1 = sql
1fc6e 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1fc6f 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
1fc70 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fc71 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1fc72 20 30 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 0, r1);.
1fc73 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r2 = sqlite3Exp
1fc74 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
1fc75 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
1fc76 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
1fc77 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fc78 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 ddOp3(v, OP_Subt
1fc79 72 61 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 ract, r2, r1, ta
1fc7a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 rget);. t
1fc7b 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
1fc7c 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 2==0 );. }.
1fc7d 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 inReg = ta
1fc7e 72 67 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 rget;. brea
1fc7f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1fc80 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 e TK_BITNOT:.
1fc81 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a case TK_NOT: {.
1fc82 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1fc83 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e _BITNOT==OP_BitN
1fc84 6f 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ot );. asse
1fc85 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e rt( TK_NOT==OP_N
1fc86 6f 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ot );. test
1fc87 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 case( op==TK_BIT
1fc88 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 NOT );. tes
1fc89 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f tcase( op==TK_NO
1fc8a 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 T );. r1 =
1fc8b 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1fc8c 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
1fc8d 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
1fc8e 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee1);. test
1fc8f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1fc90 30 20 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 0 );. inReg
1fc91 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 = target;.
1fc92 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1fc93 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e p2(v, op, r1, in
1fc94 52 65 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 Reg);. brea
1fc95 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1fc96 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 e TK_ISNULL:.
1fc97 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c case TK_NOTNULL
1fc98 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 : {. int ad
1fc99 64 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 dr;. assert
1fc9a 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f ( TK_ISNULL==OP_
1fc9b 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 IsNull );.
1fc9c 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 assert( TK_NOTNU
1fc9d 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 LL==OP_NotNull )
1fc9e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fc9f 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ( op==TK_ISNULL
1fca0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1fca1 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c e( op==TK_NOTNUL
1fca2 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 L );. sqlit
1fca3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fca4 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 OP_Integer, 1, t
1fca5 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 arget);. r1
1fca6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1fca7 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1fca8 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
1fca9 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 gFree1);. t
1fcaa 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
1fcab 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 1==0 );. ad
1fcac 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
1fcad 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 AddOp1(v, op, r1
1fcae 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1fcaf 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1fcb0 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c _AddImm, target,
1fcb1 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 -1);. sqli
1fcb2 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1fcb3 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 v, addr);.
1fcb4 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1fcb5 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e case TK_AGG_FUN
1fcb6 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 CTION: {. A
1fcb7 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 ggInfo *pInfo =
1fcb8 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b pExpr->pAggInfo;
1fcb9 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f . if( pInfo
1fcba 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1fcbb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1fcbc 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f Parse, "misuse o
1fcbd 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 54 22 f aggregate: %T"
1fcbe 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 70 ,. &p
1fcbf 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 Expr->span);.
1fcc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fcc1 20 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d inReg = pInfo-
1fcc2 3e 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 >aFunc[pExpr->iA
1fcc3 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 gg].iMem;.
1fcc4 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1fcc5 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1fcc6 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 _CONST_FUNC:.
1fcc7 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f case TK_FUNCTIO
1fcc8 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c N: {. ExprL
1fcc9 69 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 ist *pFarg;
1fcca 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e /* List of fun
1fccb 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
1fccc 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 */. int nFa
1fccd 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
1fcce 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e /* Number of fun
1fccf 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
1fcd0 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 */. FuncDef
1fcd1 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 *pDef;
1fcd2 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 /* The function
1fcd3 64 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 definition objec
1fcd4 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e t */. int n
1fcd5 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Id;
1fcd6 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 /* Length of t
1fcd7 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 he function name
1fcd8 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 in bytes */.
1fcd9 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1fcda 49 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 Id; /* The
1fcdb 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
1fcdc 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 /. int cons
1fcdd 74 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f tMask = 0; /
1fcde 2a 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 * Mask of functi
1fcdf 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 on arguments tha
1fce0 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a t are constant *
1fce1 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 /. int i;
1fce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fce3 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1fce4 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d /. u8 enc =
1fce5 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f ENC(db); /
1fce6 2a 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 * The text encod
1fce7 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 ing used by this
1fce8 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 database */.
1fce9 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c CollSeq *pCol
1fcea 6c 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 l = 0; /* A c
1fceb 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1fcec 65 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 e */.. asse
1fced 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
1fcee 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
1fcef 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 IsSelect) );.
1fcf0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1fcf1 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 =TK_CONST_FUNC )
1fcf2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fcf3 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f ( op==TK_FUNCTIO
1fcf4 4e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 N );. if( E
1fcf5 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
1fcf6 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 y(pExpr, EP_Toke
1fcf7 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f 6b nOnly|EP_SpanTok
1fcf8 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 en) ){. p
1fcf9 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 Farg = 0;.
1fcfa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
1fcfb 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e Farg = pExpr->x.
1fcfc 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pList;. }.
1fcfd 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 nFarg = pFa
1fcfe 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 rg ? pFarg->nExp
1fcff 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 7a 49 64 r : 0;. zId
1fd00 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d = (char*)pExpr-
1fd01 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 >token.z;.
1fd02 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b nId = pExpr->tok
1fd03 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66 en.n;. pDef
1fd04 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
1fd05 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 nction(db, zId,
1fd06 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c nId, nFarg, enc,
1fd07 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0);. asser
1fd08 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a 20 20 t( pDef!=0 );.
1fd09 20 20 20 20 69 66 28 20 70 46 61 72 67 20 29 7b if( pFarg ){
1fd0a 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 . r1 = sq
1fd0b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 lite3GetTempRang
1fd0c 65 28 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29 e(pParse, nFarg)
1fd0d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1fd0e 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
1fd0f 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c t(pParse, pFarg,
1fd10 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d r1, 1);. }
1fd11 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 else{. r1
1fd12 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 = 0;. }.#i
1fd13 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1fd14 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1fd15 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 /* Possibly
1fd16 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75 overload the fu
1fd17 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 66 69 nction if the fi
1fd18 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 0a rst argument is.
1fd19 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 74 75 ** a virtu
1fd1a 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e al table column.
1fd1b 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1fd1c 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75 6e ** For infix fun
1fd1d 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47 4c ctions (LIKE, GL
1fd1e 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64 20 OB, REGEXP, and
1fd1f 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 0a 20 MATCH) use the.
1fd20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 61 ** second a
1fd21 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68 65 rgument, not the
1fd22 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 20 61 first, as the a
1fd23 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74 20 rgument to test
1fd24 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 65 20 to. ** see
1fd25 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c 75 6d if it is a colum
1fd26 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 n in a virtual t
1fd27 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20 64 able. This is d
1fd28 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 20 20 one because.
1fd29 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 6f 70 ** the left op
1fd2a 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 20 66 erand of infix f
1fd2b 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 6f 70 unctions (the op
1fd2c 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f erand we want to
1fd2d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 72 6f . ** contro
1fd2e 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 20 65 l overloading) e
1fd2f 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 73 65 nds up as the se
1fd30 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
1fd31 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 75 the. ** fu
1fd32 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 78 70 nction. The exp
1fd33 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f 62 20 ression "A glob
1fd34 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 B" is equivalent
1fd35 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 22 67 to . ** "g
1fd36 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 77 61 lob(B,A). We wa
1fd37 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 41 20 nt to use the A
1fd38 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 74 6f in "A glob B" to
1fd39 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 test. ** f
1fd3a 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72 or function over
1fd3b 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 77 65 loading. But we
1fd3c 20 75 73 65 20 74 68 65 20 42 20 74 65 72 6d 20 use the B term
1fd3d 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 2e 0a in "glob(B,A)"..
1fd3e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
1fd3f 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 20 28 f( nFarg>=2 && (
1fd40 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 pExpr->flags & E
1fd41 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 7b 0a P_InfixFunc) ){.
1fd42 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 pDef = s
1fd43 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f qlite3VtabOverlo
1fd44 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 adFunction(db, p
1fd45 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 Def, nFarg, pFar
1fd46 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 3b 0a g->a[1].pExpr);.
1fd47 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1fd48 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 20 20 nFarg>0 ){.
1fd49 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 pDef = sqlite
1fd4a 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 3VtabOverloadFun
1fd4b 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 ction(db, pDef,
1fd4c 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b nFarg, pFarg->a[
1fd4d 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 0].pExpr);.
1fd4e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
1fd4f 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72 67 for(i=0; i<nFarg
1fd50 20 26 26 20 69 3c 33 32 3b 20 69 2b 2b 29 7b 0a && i<32; i++){.
1fd51 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
1fd52 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
1fd53 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 t(pFarg->a[i].pE
1fd54 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 xpr) ){.
1fd55 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20 28 constMask |= (
1fd56 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 1<<i);. }
1fd57 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 44 . if( (pD
1fd58 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 ef->flags & SQLI
1fd59 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
1fd5a 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c 20 29 )!=0 && !pColl )
1fd5b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c {. pCol
1fd5c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 l = sqlite3ExprC
1fd5d 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
1fd5e 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 Farg->a[i].pExpr
1fd5f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1fd60 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
1fd61 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c Def->flags & SQL
1fd62 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c ITE_FUNC_NEEDCOL
1fd63 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 L ){. if(
1fd64 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 !pColl ) pColl
1fd65 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b = db->pDfltColl;
1fd66 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
1fd67 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1fd68 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c P_CollSeq, 0, 0,
1fd69 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 0, (char *)pCol
1fd6a 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a l, P4_COLLSEQ);.
1fd6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
1fd6c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
1fd6d 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 v, OP_Function,
1fd6e 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c 20 74 constMask, r1, t
1fd6f 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20 arget,.
1fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1fd71 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 5f 46 char*)pDef, P4_F
1fd72 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20 73 UNCDEF);. s
1fd73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1fd74 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72 67 29 P5(v, (u8)nFarg)
1fd75 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 ;. if( nFar
1fd76 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c g ){. sql
1fd77 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
1fd78 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c ange(pParse, r1,
1fd79 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 7d nFarg);. }
1fd7a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1fd7b 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
1fd7c 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 hange(pParse, r1
1fd7d 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 , nFarg);.
1fd7e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 break;. }.#if
1fd7f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1fd80 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 _SUBQUERY. ca
1fd81 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 se TK_EXISTS:.
1fd82 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 case TK_SELECT
1fd83 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 : {. testca
1fd84 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 se( op==TK_EXIST
1fd85 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 S );. testc
1fd86 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 ase( op==TK_SELE
1fd87 43 54 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 CT );. if(
1fd88 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d pExpr->iColumn==
1fd89 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 0 ){. sql
1fd8a 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 ite3CodeSubselec
1fd8b 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c t(pParse, pExpr,
1fd8c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0, 0);. }.
1fd8d 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 inReg = pE
1fd8e 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 xpr->iColumn;.
1fd8f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1fd90 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a . case TK_IN:
1fd91 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 4e 6f {. int rNo
1fd92 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20 20 tFound = 0;.
1fd93 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 int rMayHaveNu
1fd94 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e ll = 0;. in
1fd95 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 t j2, j3, j4, j5
1fd96 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 ;. char aff
1fd97 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 69 6e 74 inity;. int
1fd98 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 56 eType;.. V
1fd99 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 dbeNoopComment((
1fd9a 76 2c 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70 v, "begin IN exp
1fd9b 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 r r%d", target))
1fd9c 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 ;. eType =
1fd9d 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 sqlite3FindInInd
1fd9e 65 78 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ex(pParse, pExpr
1fd9f 2c 20 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 , &rMayHaveNull)
1fda0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 ;. if( rMay
1fda1 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 HaveNull ){.
1fda2 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 rNotFound =
1fda3 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1fda4 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
1fda5 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 * Figure out the
1fda6 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 affinity to use
1fda7 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 to create a key
1fda8 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 from the result
1fda9 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 s. ** of th
1fdaa 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 e expression. af
1fdab 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 finityStr stores
1fdac 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 a static string
1fdad 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 suitable for.
1fdae 20 20 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50 5f ** P4 of OP_
1fdaf 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 20 MakeRecord..
1fdb0 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 6e */. affin
1fdb1 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e ity = comparison
1fdb2 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b Affinity(pExpr);
1fdb3 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 ... /* Code
1fdb4 20 74 68 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d the <expr> from
1fdb5 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e "<expr> IN (...
1fdb6 29 22 2e 20 54 68 65 20 74 65 6d 70 6f 72 61 72 )". The temporar
1fdb7 79 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a y table. **
1fdb8 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 63 pExpr->iTable c
1fdb9 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 ontains the valu
1fdba 65 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 es that make up
1fdbb 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a 20 the (...) set..
1fdbc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
1fdbd 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
1fdbe 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 sh(pParse);.
1fdbf 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
1fdc0 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1fdc1 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
1fdc2 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 . j2 = sqli
1fdc3 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
1fdc4 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61 72 67 OP_IsNull, targ
1fdc5 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 et);. if( e
1fdc6 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 Type==IN_INDEX_R
1fdc7 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 OWID ){.
1fdc8 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j3 = sqlite3Vdbe
1fdc9 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 AddOp1(v, OP_Mus
1fdca 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74 29 3b tBeInt, target);
1fdcb 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71 . j4 = sq
1fdcc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1fdcd 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c v, OP_NotExists,
1fdce 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
1fdcf 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1fdd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fdd1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
1fdd2 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b ger, 1, target);
1fdd3 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71 . j5 = sq
1fdd4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
1fdd5 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 v, OP_Goto);.
1fdd6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1fdd7 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b JumpHere(v, j3);
1fdd8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1fdd9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
1fdda 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c j4);. sql
1fddb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1fddc 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
1fddd 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1fdde 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1fddf 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d 20 73 2 = regFree2 = s
1fde0 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
1fde1 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 (pParse);..
1fde2 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 72 /* Create a r
1fde3 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74 20 66 ecord and test f
1fde4 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 or set membershi
1fde5 70 2e 20 49 66 20 74 68 65 20 73 65 74 20 63 6f p. If the set co
1fde6 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20 20 2a ntains. *
1fde7 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74 68 65 * the value, the
1fde8 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e n jump to the en
1fde9 64 20 6f 66 20 74 68 65 20 74 65 73 74 20 63 6f d of the test co
1fdea 64 65 2e 20 54 68 65 20 74 61 72 67 65 74 0a 20 de. The target.
1fdeb 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73 74 ** regist
1fdec 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e er still contain
1fded 73 20 74 68 65 20 74 72 75 65 20 28 31 29 20 76 s the true (1) v
1fdee 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f 20 alue written to
1fdef 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20 20 20 it earlier..
1fdf0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 */. s
1fdf1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1fdf2 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 (v, OP_MakeRecor
1fdf3 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20 72 32 d, target, 1, r2
1fdf4 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b , &affinity, 1);
1fdf5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1fdf6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1fdf7 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 _Integer, 1, tar
1fdf8 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 get);. j5
1fdf9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1fdfa 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 dOp3(v, OP_Found
1fdfb 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
1fdfc 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20 20 20 0, r2);..
1fdfd 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 74 20 /* If the set
1fdfe 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 20 membership test
1fdff 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20 fails, then the
1fe00 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 0a 20 result of the .
1fe01 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49 4e 20 ** "x IN
1fe02 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69 6f (...)" expressio
1fe03 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 n must be either
1fe04 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 74 0 or NULL. If t
1fe05 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20 2a he set. *
1fe06 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 * contains no NU
1fe07 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 LL values, then
1fe08 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e the result is 0.
1fe09 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20 If the set .
1fe0a 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 ** contains
1fe0b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c one or more NUL
1fe0c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 L values, then t
1fe0d 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1fe0e 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 . ** expr
1fe0f 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20 4e ession is also N
1fe10 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ULL.. */.
1fe11 20 20 20 20 20 20 20 20 69 66 28 20 72 4e 6f 74 if( rNot
1fe12 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20 20 20 Found==0 ){.
1fe13 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 /* This br
1fe14 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 74 20 anch runs if it
1fe15 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f 6d 70 is known at comp
1fe16 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29 20 74 ile time (now) t
1fe17 68 61 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a hat . *
1fe18 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69 * the set contai
1fe19 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 ns no NULL value
1fe1a 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 s. This happens
1fe1b 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 as the result.
1fe1c 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 ** of a
1fe1d 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 "NOT NULL" const
1fe1e 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64 61 74 raint in the dat
1fe1f 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 4e 6f abase schema. No
1fe20 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20 20 20 need.
1fe21 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 64 ** to test the d
1fe22 61 74 61 20 73 74 72 75 63 74 75 72 65 20 61 74 ata structure at
1fe23 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68 69 73 runtime in this
1fe24 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 case..
1fe25 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 */. sq
1fe26 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1fe27 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
1fe28 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1fe29 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fe2a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 /* This bloc
1fe2b 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65 20 k populates the
1fe2c 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69 73 74 rNotFound regist
1fe2d 65 72 20 77 69 74 68 20 65 69 74 68 65 72 20 4e er with either N
1fe2e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ULL. **
1fe2f 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65 67 65 or 0 (an intege
1fe30 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74 68 65 r value). If the
1fe31 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
1fe32 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20 20 20 contains one.
1fe33 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 ** or mor
1fe34 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20 73 65 e NULLs, then se
1fe35 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f 20 4e t rNotFound to N
1fe36 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ULL. Otherwise,
1fe37 73 65 74 20 69 74 0a 20 20 20 20 20 20 20 20 20 set it.
1fe38 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72 65 67 ** to 0. If reg
1fe39 69 73 74 65 72 20 72 4d 61 79 48 61 76 65 4e 75 ister rMayHaveNu
1fe3a 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20 73 65 ll is already se
1fe3b 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65 0a t to some value.
1fe3c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 ** oth
1fe3d 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20 74 68 er than NULL, th
1fe3e 65 6e 20 74 68 65 20 74 65 73 74 20 68 61 73 20 en the test has
1fe3f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 75 6e already been run
1fe40 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 20 20 and .
1fe41 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 ** rNotFound is
1fe42 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 already populate
1fe43 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a d.. */.
1fe44 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 static
1fe45 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 6c 6c const char null
1fe46 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30 78 30 Record[] = { 0x0
1fe47 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20 20 20 2, 0x00 };.
1fe48 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 j3 = sqlite
1fe49 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
1fe4a 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61 79 48 P_NotNull, rMayH
1fe4b 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 aveNull);.
1fe4c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fe4d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
1fe4e 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b , 0, rNotFound);
1fe4f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1fe50 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1fe51 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d 61 79 OP_Blob, 2, rMay
1fe52 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20 HaveNull, 0, .
1fe53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe54 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c 6c 52 nullR
1fe55 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54 49 43 ecord, P4_STATIC
1fe56 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 34 20 );. j4
1fe57 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
1fe58 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c Op3(v, OP_Found,
1fe59 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
1fe5a 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 0, rMayHaveNull)
1fe5b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1fe5c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1fe5d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 OP_Integer, 0,
1fe5e 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 rNotFound);.
1fe5f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1fe60 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 eJumpHere(v, j4)
1fe61 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1fe62 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1fe63 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20 20 20 v, j3);..
1fe64 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 76 /* Copy the v
1fe65 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1fe66 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68 69 63 rNotFound (whic
1fe67 68 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c 4c h is either NULL
1fe68 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20 20 20 or 0).
1fe69 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74 61 72 ** into the tar
1fe6a 67 65 74 20 72 65 67 69 73 74 65 72 2e 20 54 68 get register. Th
1fe6b 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 72 is will be the r
1fe6c 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 esult of the.
1fe6d 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 ** expres
1fe6e 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 sion..
1fe6f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c */. sql
1fe70 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1fe71 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f 74 46 , OP_Copy, rNotF
1fe72 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b 0a 20 ound, target);.
1fe73 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1fe74 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1fe75 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 beJumpHere(v, j2
1fe76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1fe77 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
1fe78 6a 35 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 j5);. sqlit
1fe79 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 e3ExprCachePop(p
1fe7a 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 Parse, 1);.
1fe7b 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
1fe7c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 20 72 25 "end IN expr r%
1fe7d 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 d", target));.
1fe7e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1fe7f 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a 20 .#endif. /*.
1fe80 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45 ** x BETWE
1fe81 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a EN y AND z. *
1fe82 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 *. ** This is
1fe83 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 equivalent to.
1fe84 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
1fe85 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 x>=y AND x<=z.
1fe86 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 **. ** X is
1fe87 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
1fe88 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 ->pLeft.. **
1fe89 59 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 Y is stored in p
1fe8a 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 Expr->pList->a[0
1fe8b 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 ].pExpr.. **
1fe8c 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 Z is stored in p
1fe8d 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 Expr->pList->a[1
1fe8e 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a ].pExpr.. */.
1fe8f 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 case TK_BETW
1fe90 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 EEN: {. Exp
1fe91 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 r *pLeft = pExpr
1fe92 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 ->pLeft;. s
1fe93 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1fe94 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 tem *pLItem = pE
1fe95 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b xpr->x.pList->a;
1fe96 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 . Expr *pRi
1fe97 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 ght = pLItem->pE
1fe98 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65 xpr;.. code
1fe99 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
1fe9a 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 26 pParse, pLeft, &
1fe9b 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 r1, ®Free1,.
1fe9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe9e 20 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 pRight, &r2, &r
1fe9f 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
1fea0 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1fea1 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 e1==0 );. t
1fea2 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
1fea3 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 33 2==0 );. r3
1fea4 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1fea5 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
1fea6 20 20 20 20 72 34 20 3d 20 73 71 6c 69 74 65 33 r4 = sqlite3
1fea7 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
1fea8 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f e);. codeCo
1fea9 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c mpare(pParse, pL
1feaa 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f eft, pRight, OP_
1feab 47 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 Ge,.
1feac 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72 33 r1, r2, r3
1fead 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
1feae 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2b );. pLItem+
1feaf 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 +;. pRight
1feb0 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pLItem->pExpr;
1feb1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
1feb2 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
1feb3 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a rse, regFree2);.
1feb4 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 r2 = sqlit
1feb5 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
1feb6 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 26 Parse, pRight, &
1feb7 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
1feb8 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1feb9 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee2==0 );.
1feba 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 codeCompare(pPar
1febb 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 se, pLeft, pRigh
1febc 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32 t, OP_Le, r1, r2
1febd 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f , r4, SQLITE_STO
1febe 52 45 50 32 29 3b 0a 20 20 20 20 20 20 73 71 6c REP2);. sql
1febf 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fec0 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34 , OP_And, r3, r4
1fec1 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1fec2 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1fec3 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
1fec4 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 3);. sqlite
1fec5 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1fec6 70 50 61 72 73 65 2c 20 72 34 29 3b 0a 20 20 20 pParse, r4);.
1fec7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1fec8 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 case TK_UPLU
1fec9 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 S: {. inReg
1feca 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1fecb 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
1fecc 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 pExpr->pLeft, t
1fecd 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 arget);. br
1fece 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 eak;. }..
1fecf 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 /*. ** Form A
1fed0 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 :. ** CASE
1fed1 78 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 x WHEN e1 THEN r
1fed2 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 1 WHEN e2 THEN r
1fed3 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 2 ... WHEN eN TH
1fed4 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 EN rN ELSE y END
1fed5 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 . **. ** F
1fed6 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 orm B:. **
1fed7 43 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 CASE WHEN e1 THE
1fed8 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 N r1 WHEN e2 THE
1fed9 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e N r2 ... WHEN eN
1feda 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 THEN rN ELSE y
1fedb 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a END. **. *
1fedc 2a 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 * Form A is can
1fedd 62 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 be transformed i
1fede 6e 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 nto the equivale
1fedf 6e 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c nt form B as fol
1fee0 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 lows:. ** C
1fee1 41 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 ASE WHEN x=e1 TH
1fee2 45 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 EN r1 WHEN x=e2
1fee3 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 THEN r2 ....
1fee4 2a 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 ** WHEN x
1fee5 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 =eN THEN rN ELSE
1fee6 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 y END. **.
1fee7 20 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 ** X (if it ex
1fee8 69 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 ists) is in pExp
1fee9 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a r->pLeft.. **
1feea 20 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e Y is in pExpr->
1feeb 70 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 pRight. The Y i
1feec 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e s also optional.
1feed 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f If there is no
1feee 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 . ** ELSE cla
1feef 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 use and no other
1fef0 20 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 term matches, t
1fef1 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f hen the result o
1fef2 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 f the. ** exp
1fef3 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a rssion is NULL..
1fef4 20 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 ** Ei is in
1fef5 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
1fef6 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 i*2] and Ri is p
1fef7 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 Expr->pList->a[i
1fef8 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 *2+1].. **.
1fef9 20 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 ** The result
1fefa 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
1fefb 6e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 n is the Ri for
1fefc 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 the first matchi
1fefd 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 ng Ei,. ** or
1fefe 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
1feff 6d 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 matching Ei, the
1ff00 20 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 ELSE term Y, or
1ff01 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 if there is.
1ff02 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d ** no ELSE term
1ff03 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 , NULL.. */.
1ff04 20 20 20 63 61 73 65 20 54 4b 5f 43 41 53 45 3a case TK_CASE:
1ff05 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 64 {. int end
1ff06 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 Label;
1ff07 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f /* GO
1ff08 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 TO label for end
1ff09 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a 2f of CASE stmt */
1ff0a 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74 43 . int nextC
1ff0b 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ase;
1ff0c 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f /* GOTO
1ff0d 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 label for next
1ff0e 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 WHEN clause */.
1ff0f 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 int nExpr;
1ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff11 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d /* 2x num
1ff12 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72 6d ber of WHEN term
1ff13 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 s */. int i
1ff14 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1ff15 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ff16 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
1ff17 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a ExprList *
1ff18 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 pEList;
1ff19 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
1ff1a 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f of WHEN terms */
1ff1b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 . struct Ex
1ff1c 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 prList_item *aLi
1ff1d 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 stelem; /* Arra
1ff1e 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 y of WHEN terms
1ff1f 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f 70 */. Expr op
1ff20 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20 20 Compare;
1ff21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1ff22 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73 69 e X==Ei expressi
1ff23 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 on */. Expr
1ff24 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 20 20 cacheX;
1ff25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ff26 20 43 61 63 68 65 64 20 65 78 70 72 65 73 73 69 Cached expressi
1ff27 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 on X */. Ex
1ff28 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 pr *pX;
1ff29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff2a 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 73 73 /* The X express
1ff2b 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 ion */. Exp
1ff2c 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20 20 r *pTest = 0;
1ff2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ff2e 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 * X==Ei (form A)
1ff2f 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f 72 or just Ei (for
1ff30 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56 56 m B) */. VV
1ff31 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61 63 A_ONLY( int iCac
1ff32 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 heLevel = pParse
1ff33 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 29 ->iCacheLevel; )
1ff34 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 .. assert(
1ff35 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
1ff36 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
1ff37 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 2d 3e lect) && pExpr->
1ff38 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 x.pList );.
1ff39 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e assert((pExpr->
1ff3a 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 x.pList->nExpr %
1ff3b 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 2) == 0);.
1ff3c 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e 78 assert(pExpr->x
1ff3d 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 .pList->nExpr >
1ff3e 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 0);. pEList
1ff3f 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 = pExpr->x.pLis
1ff40 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c t;. aListel
1ff41 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a em = pEList->a;.
1ff42 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 nExpr = pE
1ff43 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 List->nExpr;.
1ff44 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 endLabel = sq
1ff45 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1ff46 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 el(v);. if(
1ff47 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c (pX = pExpr->pL
1ff48 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 eft)!=0 ){.
1ff49 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b cacheX = *pX;
1ff4a 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1ff4b 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f e( pX->op==TK_CO
1ff4c 4c 55 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d LUMN || pX->op==
1ff4d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 TK_REGISTER );.
1ff4e 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 cacheX.iT
1ff4f 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 able = sqlite3Ex
1ff50 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
1ff51 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 e, pX, ®Free1
1ff52 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1ff53 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1ff54 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 );. cach
1ff55 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 eX.op = TK_REGIS
1ff56 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 TER;. opC
1ff57 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 ompare.op = TK_E
1ff58 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d Q;. opCom
1ff59 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 pare.pLeft = &ca
1ff5a 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 cheX;. pT
1ff5b 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 est = &opCompare
1ff5c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1ff5d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 for(i=0; i<nExpr
1ff5e 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 ; i=i+2){.
1ff5f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1ff60 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a hePush(pParse);.
1ff61 20 20 20 20 20 20 20 20 69 66 28 20 70 58 20 29 if( pX )
1ff62 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
1ff63 72 74 28 20 70 54 65 73 74 21 3d 30 20 29 3b 0a rt( pTest!=0 );.
1ff64 20 20 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 opComp
1ff65 61 72 65 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 are.pRight = aLi
1ff66 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b stelem[i].pExpr;
1ff67 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1ff68 20 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20 pTest
1ff69 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 = aListelem[i].p
1ff6a 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a Expr;. }.
1ff6b 20 20 20 20 20 20 20 20 6e 65 78 74 43 61 73 65 nextCase
1ff6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
1ff6d 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
1ff6e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 testcase( pT
1ff6f 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 est->op==TK_COLU
1ff70 4d 4e 20 7c 7c 20 70 54 65 73 74 2d 3e 6f 70 3d MN || pTest->op=
1ff71 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a =TK_REGISTER );.
1ff72 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1ff73 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 xprIfFalse(pPars
1ff74 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61 e, pTest, nextCa
1ff75 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 se, SQLITE_JUMPI
1ff76 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 FNULL);.
1ff77 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 testcase( aListe
1ff78 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e lem[i+1].pExpr->
1ff79 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b op==TK_COLUMN );
1ff7a 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1ff7b 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 e( aListelem[i+1
1ff7c 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ].pExpr->op==TK_
1ff7d 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 REGISTER );.
1ff7e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
1ff7f 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73 ode(pParse, aLis
1ff80 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 telem[i+1].pExpr
1ff81 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1ff82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ff83 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
1ff84 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 0, endLabel);.
1ff85 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
1ff86 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
1ff87 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 e, 1);. s
1ff88 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
1ff89 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 eLabel(v, nextCa
1ff8a 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 se);. }.
1ff8b 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 if( pExpr->pR
1ff8c 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 ight ){.
1ff8d 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
1ff8e 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 Push(pParse);.
1ff8f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1ff90 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
1ff91 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 xpr->pRight, tar
1ff92 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 get);. sq
1ff93 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
1ff94 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 p(pParse, 1);.
1ff95 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ff96 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ff97 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c dOp2(v, OP_Null,
1ff98 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 0, target);.
1ff99 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
1ff9a 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
1ff9b 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e led || pParse->n
1ff9c 45 72 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20 Err>0 .
1ff9d 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61 || pParse->iCa
1ff9e 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65 cheLevel==iCache
1ff9f 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73 Level );. s
1ffa0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
1ffa1 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62 eLabel(v, endLab
1ffa2 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b el);. break
1ffa3 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1ffa4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1ffa5 47 45 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f GER. case TK_
1ffa6 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 RAISE: {. i
1ffa7 66 28 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 f( !pParse->trig
1ffa8 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 Stack ){.
1ffa9 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1ffaa 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 (pParse,.
1ffab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffac 22 52 41 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c "RAISE() may onl
1ffad 79 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e y be used within
1ffae 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 a trigger-progr
1ffaf 61 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 am");. re
1ffb0 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a turn 0;. }.
1ffb1 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1ffb2 3e 61 66 66 69 6e 69 74 79 21 3d 4f 45 5f 49 67 >affinity!=OE_Ig
1ffb3 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 nore ){.
1ffb4 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1ffb5 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c affinity==OE_Rol
1ffb6 6c 62 61 63 6b 20 7c 7c 0a 20 20 20 20 20 20 20 lback ||.
1ffb7 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1ffb8 3e 61 66 66 69 6e 69 74 79 20 3d 3d 20 4f 45 5f >affinity == OE_
1ffb9 41 62 6f 72 74 20 7c 7c 0a 20 20 20 20 20 20 20 Abort ||.
1ffba 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1ffbb 3e 61 66 66 69 6e 69 74 79 20 3d 3d 20 4f 45 5f >affinity == OE_
1ffbc 46 61 69 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 Fail );.
1ffbd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ffbe 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 p4(v, OP_Halt, S
1ffbf 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
1ffc0 2c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 , pExpr->affinit
1ffc1 79 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 y, 0,.
1ffc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 (c
1ffc3 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1ffc4 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 n.z, pExpr->toke
1ffc5 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c n.n);. } el
1ffc6 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73 se {. as
1ffc7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66 sert( pExpr->aff
1ffc8 69 6e 69 74 79 20 3d 3d 20 4f 45 5f 49 67 6e 6f inity == OE_Igno
1ffc9 72 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73 re );. s
1ffca 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1ffcb 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f (v, OP_ContextPo
1ffcc 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 p, 0, 0);.
1ffcd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ffce 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
1ffcf 20 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67 0, pParse->trig
1ffd0 53 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d Stack->ignoreJum
1ffd1 70 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62 p);. Vdb
1ffd2 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61 eComment((v, "ra
1ffd3 69 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a ise(IGNORE)"));.
1ffd4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1ffd5 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
1ffd6 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 f. }. sqlite3R
1ffd7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
1ffd8 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b arse, regFree1);
1ffd9 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
1ffda 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
1ffdb 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 regFree2);. re
1ffdc 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f turn inReg;.}../
1ffdd 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1ffde 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 de to evaluate a
1ffdf 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 n expression and
1ffe0 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1ffe1 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 ts.** into a reg
1ffe2 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 ister. Return t
1ffe3 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 he register numb
1ffe4 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 er where the res
1ffe5 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 ults.** are stor
1ffe6 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
1ffe7 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 register is a t
1ffe8 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
1ffe9 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 r that can be de
1ffea 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 allocated,.** th
1ffeb 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d en write its num
1ffec 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 ber into *pReg.
1ffed 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 If the result r
1ffee 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a egister is not.*
1ffef 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 * a temporary, t
1fff0 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f hen set *pReg to
1fff1 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 zero..*/.SQLITE
1fff2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1fff3 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
1fff4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1fff5 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
1fff6 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 *pReg){. int r
1fff7 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 1 = sqlite3GetTe
1fff8 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
1fff9 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 int r2 = sqlite
1fffa 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
1fffb 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 pParse, pExpr, r
1fffc 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 1);. if( r2==r1
1fffd 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 ){. *pReg =
1fffe 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r1;. }else{.
1ffff 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
20000 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
20001 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 1);. *pReg =
20002 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
20003 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e r2;.}../*.** Gen
20004 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
20005 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 will evaluate ex
20006 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 pression pExpr a
20007 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 nd store the.**
20008 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 results in regis
20009 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 ter target. The
2000a 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 results are gua
2000b 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 ranteed to appea
2000c 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 r.** in register
2000d 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 target..*/.SQLI
2000e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
2000f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 qlite3ExprCode(P
20010 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
20011 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 pr *pExpr, int t
20012 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e arget){. int in
20013 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 Reg;.. assert(
20014 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 target>0 && targ
20015 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d et<=pParse->nMem
20016 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 );. inReg = sq
20017 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
20018 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
20019 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 r, target);. as
2001a 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 sert( pParse->pV
2001b 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 dbe || pParse->d
2001c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
2001d 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d );. if( inReg!=
2001e 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 target && pParse
2001f 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 ->pVdbe ){. s
20020 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20021 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 (pParse->pVdbe,
20022 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c OP_SCopy, inReg,
20023 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 target);. }.
20024 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d return target;.}
20025 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
20026 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 code that evalu
20027 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 tes the given ex
20028 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 pression and put
20029 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 s the result.**
2002a 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 in register targ
2002b 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d et..**.** Also m
2002c 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
2002d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 e expression res
2002e 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 ults into anothe
2002f 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 r "cache" regist
20030 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 er.** and modify
20031 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20032 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 so that the next
20033 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c time it is eval
20034 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 uated,.** the re
20035 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f sult is a copy o
20036 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 f the cache regi
20037 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ster..**.** This
20038 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
20039 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 for expressions
2003a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d that are used m
2003b 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 ultiple .** time
2003c 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 s. They are eva
2003d 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 luated once and
2003e 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
2003f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
20040 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a are reused..*/.
20041 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20042 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
20043 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 deAndCache(Parse
20044 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
20045 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 pExpr, int targe
20046 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 t){. Vdbe *v =
20047 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
20048 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e int inReg;. in
20049 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Reg = sqlite3Exp
2004a 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
2004b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
2004c 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 assert( target>0
2004d 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d );. if( pExpr-
2004e 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 >op!=TK_REGISTER
2004f 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d ){ . int iM
20050 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b em;. iMem = +
20051 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
20052 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20053 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c dOp2(v, OP_Copy,
20054 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 inReg, iMem);.
20055 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
20056 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 = iMem;. pEx
20057 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 pr->op = TK_REGI
20058 53 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 STER;. }. retu
20059 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a rn inReg;.}../*.
2005a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
2005b 66 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f f pExpr is an co
2005c 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f nstant expressio
2005d 6e 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 n that is approp
2005e 72 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 riate.** for fac
2005f 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 toring out of a
20060 6c 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 loop. Appropria
20061 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 te expressions a
20062 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 re:.**.** *
20063 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 Any expression t
20064 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f hat evaluates to
20065 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 two or more opc
20066 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a odes..**.** *
20067 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 Any OP_Integer
20068 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 , OP_Real, OP_St
20069 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f ring, OP_Blob, O
2006a 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 P_Null, .**
2006b 20 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 or OP_Variable
2006c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e that does not n
2006d 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 eed to be placed
2006e 20 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 in a .**
2006f 73 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 specific registe
20070 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 r..**.** There i
20071 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 s no point in fa
20072 63 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 ctoring out sing
20073 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 le-instruction c
20074 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 onstant.** expre
20075 73 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 ssions that need
20076 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e to be placed in
20077 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 a particular re
20078 67 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 gister. .** We
20079 63 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 could factor the
2007a 6d 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 m out, but then
2007b 77 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 we would end up
2007c 61 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f adding an.** OP_
2007d 53 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f SCopy instructio
2007e 6e 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 n to move the va
2007f 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 lue into the cor
20080 72 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a rect register.**
20081 20 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 later. We migh
20082 74 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 t as well just u
20083 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 se the original
20084 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a instruction and.
20085 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f ** avoid the OP_
20086 53 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 SCopy..*/.static
20087 20 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 int isAppropria
20088 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 teForFactoring(E
20089 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 xpr *p){. if( !
2008a 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
2008b 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 stantNotJoin(p)
2008c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
2008d 20 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 /* Only consta
2008e 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 nt expressions a
2008f 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 re appropriate f
20090 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a or factoring */.
20091 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c }. if( (p->fl
20092 61 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 ags & EP_FixedDe
20093 73 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 st)==0 ){. re
20094 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 turn 1; /* Any
20095 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 constant without
20096 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 a fixed destina
20097 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 tion is appropri
20098 61 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 ate */. }. whi
20099 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 le( p->op==TK_UP
2009a 4c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 LUS ) p = p->pLe
2009b 66 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d ft;. switch( p-
2009c 3e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 >op ){.#ifndef S
2009d 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
2009e 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 LITERAL. case
2009f 20 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 TK_BLOB:.#endif
200a0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 . case TK_VAR
200a1 49 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 IABLE:. case
200a2 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 TK_INTEGER:.
200a3 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 case TK_FLOAT:.
200a4 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a case TK_NULL:
200a5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 . case TK_STR
200a6 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 ING: {. tes
200a7 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b tcase( p->op==TK
200a8 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 _BLOB );. t
200a9 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d estcase( p->op==
200aa 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 TK_VARIABLE );.
200ab 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
200ac 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 ->op==TK_INTEGER
200ad 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
200ae 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c se( p->op==TK_FL
200af 4f 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 OAT );. tes
200b0 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b tcase( p->op==TK
200b1 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 _NULL );. t
200b2 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d estcase( p->op==
200b3 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 TK_STRING );.
200b4 20 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 /* Single-ins
200b5 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e truction constan
200b6 74 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 ts with a fixed
200b7 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a destination are.
200b8 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 ** better
200b9 64 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 done in-line. I
200ba 66 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d f we factor them
200bb 2c 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 , they will just
200bc 20 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 end. ** up
200bd 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f generating an O
200be 50 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 P_SCopy to move
200bf 74 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 the value to the
200c0 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 destination.
200c1 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 ** register.
200c2 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
200c3 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 0;. }. cas
200c4 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 e TK_UMINUS: {.
200c5 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 if( p->pLe
200c6 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 ft->op==TK_FLOAT
200c7 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 || p->pLeft->op
200c8 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a ==TK_INTEGER ){.
200c9 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
200ca 30 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
200cb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
200cc 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
200cd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
200ce 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 }. }. return 1
200cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 ;.}../*.** If pE
200d0 78 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e xpr is a constan
200d1 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 t expression tha
200d2 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 t is appropriate
200d3 20 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e for.** factorin
200d4 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c g out of a loop,
200d5 20 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 then evaluate t
200d6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
200d7 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 into a register
200d8 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 and convert the
200d9 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f expression into
200da 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a a TK_REGISTER.*
200db 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f * expression..*/
200dc 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c .static int eval
200dd 43 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 ConstExpr(Walker
200de 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 *pWalker, Expr
200df 2a 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 *pExpr){. Parse
200e0 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b *pParse = pWalk
200e1 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 er->pParse;. sw
200e2 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 itch( pExpr->op
200e3 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 ){. case TK_R
200e4 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 EGISTER: {.
200e5 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
200e6 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
200e7 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 CTION:. case
200e8 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a TK_AGG_FUNCTION:
200e9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e . case TK_CON
200ea 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 ST_FUNC: {.
200eb 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 /* The argument
200ec 73 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 s to a function
200ed 68 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 have a fixed des
200ee 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 tination..
200ef 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 ** Mark them thi
200f0 73 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 s way to avoid g
200f1 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 enerated unneede
200f2 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 d OP_SCopy.
200f3 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ** instructions
200f4 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . . */.
200f5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
200f6 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 t = pExpr->x.pLi
200f7 73 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 st;. assert
200f8 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
200f9 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
200fa 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
200fb 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 if( pList ){.
200fc 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c int i = pL
200fd 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 ist->nExpr;.
200fe 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
200ff 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 ist_item *pItem
20100 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 = pList->a;.
20101 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69 for(; i>0; i
20102 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 --, pItem++){.
20103 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
20104 6d 2d 3e 70 45 78 70 72 20 29 20 70 49 74 65 6d m->pExpr ) pItem
20105 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c ->pExpr->flags |
20106 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a = EP_FixedDest;.
20107 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
20108 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
20109 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 }. }. if( i
2010a 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 sAppropriateForF
2010b 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20 actoring(pExpr)
2010c 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 ){. int r1 =
2010d 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
2010e 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20 int r2;.
2010f 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
20110 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
20111 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 e, pExpr, r1);.
20112 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29 20 if( r1!=r2 )
20113 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
20114 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
20115 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 );. pExpr->op
20116 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a = TK_REGISTER;.
20117 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
20118 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75 e = r2;. retu
20119 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
2011a 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 }. return WRC_C
2011b 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
2011c 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f * Preevaluate co
2011d 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 nstant subexpres
2011e 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78 sions within pEx
2011f 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 pr and store the
20120 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72 .** results in r
20121 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66 egisters. Modif
20122 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 y pExpr so that
20123 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 the constant sub
20124 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 expresions.** ar
20125 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70 e TK_REGISTER op
20126 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65 72 codes that refer
20127 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75 to the precompu
20128 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 ted values..*/.S
20129 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
2012a 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f id sqlite3ExprCo
2012b 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 deConstants(Pars
2012c 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
2012d 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 *pExpr){. Walke
2012e 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 r w;. w.xExprCa
2012f 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e llback = evalCon
20130 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c stExpr;. w.xSel
20131 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b ectCallback = 0;
20132 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 . w.pParse = pP
20133 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 arse;. sqlite3W
20134 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 alkExpr(&w, pExp
20135 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 r);.}.../*.** Ge
20136 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
20137 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c 75 pushes the valu
20138 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 e of every eleme
20139 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a nt of the given.
2013a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 ** expression li
2013b 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e st into a sequen
2013c 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 ce of registers
2013d 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 beginning at tar
2013e 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 get..**.** Retur
2013f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
20140 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74 elements evaluat
20141 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
20142 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20143 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
20144 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
20145 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
20146 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
20147 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c ExprList *pList,
20148 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
20149 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20 sion list to be
2014a 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 coded */. int t
2014b 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a arget, /*
2014c 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 Where to write
2014d 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 results */. int
2014e 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20 doHardCopy
2014f 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63 /* Make a hard c
20150 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65 opy of every ele
20151 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 ment */.){. str
20152 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
20153 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 m *pItem;. int
20154 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 i, n;. assert(
20155 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73 pList!=0 );. as
20156 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 sert( target>0 )
20157 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e ;. n = pList->n
20158 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65 Expr;. for(pIte
20159 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b m=pList->a, i=0;
2015a 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d i<n; i++, pItem
2015b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 ++){. if( pIt
2015c 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20 em->iAlias ){.
2015d 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63 int iReg = c
2015e 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c odeAlias(pParse,
2015f 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 pItem->iAlias,
20160 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 pItem->pExpr, ta
20161 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56 rget+i);. V
20162 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 dbe *v = sqlite3
20163 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
20164 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67 21 . if( iReg!
20165 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 =target+i ){.
20166 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20167 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f AddOp2(v, OP_SCo
20168 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74 py, iReg, target
20169 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 +i);. }.
2016a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
2016b 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
2016c 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 arse, pItem->pEx
2016d 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 pr, target+i);.
2016e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48 }. if( doH
2016f 61 72 64 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 ardCopy ){.
20170 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 sqlite3ExprHard
20171 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61 72 Copy(pParse, tar
20172 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 get, n);. }.
20173 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d }. return n;.}
20174 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
20175 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c code for a bool
20176 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 ean expression s
20177 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 uch that a jump
20178 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 is made.** to th
20179 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 e label "dest" i
2017a 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
2017b 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 65 is true but exe
2017c 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e cution.** contin
2017d 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72 ues straight thr
2017e 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 u if the express
2017f 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a ion is false..**
20180 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
20181 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 ssion evaluates
20182 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 to NULL (neither
20183 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 true nor false)
20184 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74 , then.** take t
20185 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a he jump if the j
20186 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69 umpIfNull flag i
20187 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e s SQLITE_JUMPIFN
20188 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ULL..**.** This
20189 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20 code depends on
2018a 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 65 the fact that ce
2018b 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75 rtain token valu
2018c 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a es (ex: TK_EQ).*
2018d 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 * are the same a
2018e 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 s opcode values
2018f 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74 (ex: OP_Eq) that
20190 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 implement the c
20191 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
20192 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63 operation. Spec
20193 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 ial comments in
20194 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d vdbe.c and the m
20195 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 kopcodeh.awk scr
20196 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61 ipt in.** the ma
20197 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 65 ke process cause
20198 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f these values to
20199 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28 align. Assert(
2019a 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a )s in the code.*
2019b 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74 * below verify t
2019c 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20 hat the numbers
2019d 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72 are aligned corr
2019e 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ectly..*/.SQLITE
2019f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
201a0 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
201a1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
201a2 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
201a3 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66 dest, int jumpIf
201a4 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76 Null){. Vdbe *v
201a5 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
201a6 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a ;. int op = 0;.
201a7 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d int regFree1 =
201a8 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 0;. int regFre
201a9 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31 e2 = 0;. int r1
201aa 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 , r2;.. assert(
201ab 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c jumpIfNull==SQL
201ac 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c ITE_JUMPIFNULL |
201ad 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 | jumpIfNull==0
201ae 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c );. if( v==0 ||
201af 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 pExpr==0 ) retu
201b0 72 6e 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 rn;. op = pExpr
201b1 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 ->op;. switch(
201b2 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
201b3 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 K_AND: {. i
201b4 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 nt d2 = sqlite3V
201b5 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
201b6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
201b7 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
201b8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
201b9 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 xprCachePush(pPa
201ba 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 rse);. sqli
201bb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 te3ExprIfFalse(p
201bc 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
201bd 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 eft, d2,jumpIfNu
201be 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 ll^SQLITE_JUMPIF
201bf 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c NULL);. sql
201c0 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
201c1 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 Parse, pExpr->pR
201c2 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 ight, dest, jump
201c3 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 IfNull);. s
201c4 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
201c5 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 eLabel(v, d2);.
201c6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
201c7 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c CachePop(pParse,
201c8 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 1);. break
201c9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
201ca 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 TK_OR: {.
201cb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 testcase( jumpIf
201cc 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Null==0 );.
201cd 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 sqlite3ExprIfTr
201ce 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ue(pParse, pExpr
201cf 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a ->pLeft, dest, j
201d0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
201d1 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 sqlite3ExprIfT
201d2 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 rue(pParse, pExp
201d3 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c r->pRight, dest,
201d4 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
201d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
201d6 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
201d7 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 : {. testca
201d8 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
201d9 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
201da 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 e3ExprIfFalse(pP
201db 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
201dc 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 ft, dest, jumpIf
201dd 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Null);. bre
201de 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
201df 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 se TK_LT:. ca
201e0 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 se TK_LE:. ca
201e1 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 se TK_GT:. ca
201e2 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 se TK_GE:. ca
201e3 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 se TK_NE:. ca
201e4 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 se TK_EQ: {.
201e5 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d assert( TK_LT=
201e6 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 =OP_Lt );.
201e7 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f assert( TK_LE==O
201e8 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Le );. as
201e9 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f sert( TK_GT==OP_
201ea 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Gt );. asse
201eb 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 rt( TK_GE==OP_Ge
201ec 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
201ed 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 ( TK_EQ==OP_Eq )
201ee 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
201ef 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a TK_NE==OP_Ne );.
201f0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
201f1 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 op==TK_LT );.
201f2 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
201f3 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 =TK_LE );.
201f4 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
201f5 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _GT );. tes
201f6 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 tcase( op==TK_GE
201f7 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
201f8 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b se( op==TK_EQ );
201f9 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
201fa 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 op==TK_NE );.
201fb 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 testcase( ju
201fc 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 mpIfNull==0 );.
201fd 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
201fe 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c Operands(pParse,
201ff 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 pExpr->pLeft, &
20200 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 r1, ®Free1,.
20201 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20203 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
20204 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b &r2, ®Free2);
20205 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
20206 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 re(pParse, pExpr
20207 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e ->pLeft, pExpr->
20208 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 pRight, op,.
20209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 r1
2020a 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 , r2, dest, jump
2020b 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 IfNull);. t
2020c 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
2020d 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
2020e 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
2020f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
20210 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
20211 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 se TK_ISNULL:.
20212 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c case TK_NOTNUL
20213 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 L: {. asser
20214 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 t( TK_ISNULL==OP
20215 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 _IsNull );.
20216 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e assert( TK_NOTN
20217 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 ULL==OP_NotNull
20218 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
20219 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c e( op==TK_ISNULL
2021a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2021b 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 se( op==TK_NOTNU
2021c 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d LL );. r1 =
2021d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
2021e 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
2021f 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
20220 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ree1);. sql
20221 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
20222 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b , op, r1, dest);
20223 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20224 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
20225 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
20226 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 }. case TK_B
20227 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 ETWEEN: {.
20228 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 /* x BETWEEN
20229 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a y AND z. **
2022a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 . ** Is equ
2022b 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 ivalent to .
2022c 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 **. **
2022d 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 x>=y AND x<=z.
2022e 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
2022f 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 Code it as such
20230 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f , taking care to
20231 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 do the common s
20232 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 ubexpression.
20233 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 ** elementati
20234 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a on of x.. *
20235 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 /. Expr exp
20236 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 rAnd;. Expr
20237 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 compLeft;.
20238 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b Expr compRight;
20239 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 . Expr expr
2023a 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 X;.. assert
2023b 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
2023c 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
2023d 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
2023e 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d exprX = *pExpr-
2023f 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 >pLeft;. ex
20240 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e prAnd.op = TK_AN
20241 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 D;. exprAnd
20242 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 .pLeft = &compLe
20243 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e ft;. exprAn
20244 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 d.pRight = &comp
20245 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d Right;. com
20246 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 pLeft.op = TK_GE
20247 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 ;. compLeft
20248 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b .pLeft = &exprX;
20249 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e . compLeft.
2024a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e pRight = pExpr->
2024b 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 x.pList->a[0].pE
2024c 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 xpr;. compR
2024d 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b ight.op = TK_LE;
2024e 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
2024f 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b .pLeft = &exprX;
20250 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
20251 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d .pRight = pExpr-
20252 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 >x.pList->a[1].p
20253 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 Expr;. expr
20254 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 X.iTable = sqlit
20255 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
20256 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 Parse, &exprX, &
20257 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
20258 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
20259 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
2025a 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 exprX.op = TK_RE
2025b 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 GISTER;. te
2025c 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
2025d 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 ll==0 );. s
2025e 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 qlite3ExprIfTrue
2025f 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e (pParse, &exprAn
20260 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e d, dest, jumpIfN
20261 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ull);. brea
20262 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
20263 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 ault: {. r1
20264 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
20265 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
20266 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 Expr, ®Free1)
20267 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20268 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
20269 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75 If, r1, dest, ju
2026a 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20 mpIfNull!=0);.
2026b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
2026c 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
2026d 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
2026e 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
2026f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
20270 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 . }. sqlite3Re
20271 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
20272 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a rse, regFree1);.
20273 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
20274 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
20275 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a 0a regFree2); .}..
20276 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
20277 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 ode for a boolea
20278 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 63 n expression suc
20279 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 73 h that a jump is
2027a 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 made.** to the
2027b 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 20 label "dest" if
2027c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
2027d 73 20 66 61 6c 73 65 20 62 75 74 20 65 78 65 63 s false but exec
2027e 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 ution.** continu
2027f 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72 75 es straight thru
20280 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
20281 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a on is true..**.*
20282 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
20283 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f ion evaluates to
20284 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 NULL (neither t
20285 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20 74 rue nor false) t
20286 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 6a hen.** jump if j
20287 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51 4c umpIfNull is SQL
20288 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 6f ITE_JUMPIFNULL o
20289 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 r fall through i
2028a 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a 20 f jumpIfNull.**
2028b 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f is 0..*/.SQLITE_
2028c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
2028d 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
2028e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
2028f 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
20290 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66 dest, int jumpIf
20291 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76 Null){. Vdbe *v
20292 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
20293 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a ;. int op = 0;.
20294 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d int regFree1 =
20295 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 0;. int regFre
20296 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31 e2 = 0;. int r1
20297 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 , r2;.. assert(
20298 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c jumpIfNull==SQL
20299 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c ITE_JUMPIFNULL |
2029a 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 | jumpIfNull==0
2029b 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c );. if( v==0 ||
2029c 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 pExpr==0 ) retu
2029d 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 76 61 rn;.. /* The va
2029e 6c 75 65 20 6f 66 20 70 45 78 70 72 2d 3e 6f 70 lue of pExpr->op
2029f 20 61 6e 64 20 6f 70 20 61 72 65 20 72 65 6c 61 and op are rela
202a0 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ted as follows:.
202a1 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 **. **
202a2 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 pExpr->op
202a3 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20 20 20 op. **
202a4 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 ---------
202a5 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------
202a6 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 49 . ** TK_I
202a7 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 4f SNULL O
202a8 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a 20 20 P_NotNull. **
202a9 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 TK_NOTNULL
202aa 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e 75 6c OP_IsNul
202ab 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f l. ** TK_
202ac 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NE
202ad 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20 20 20 OP_Eq. **
202ae 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 TK_EQ
202af 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a 20 20 OP_Ne. **
202b0 20 20 20 20 20 54 4b 5f 47 54 20 20 20 20 20 20 TK_GT
202b1 20 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a 20 20 OP_Le.
202b2 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45 20 20 ** TK_LE
202b3 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 OP_G
202b4 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f t. ** TK_
202b5 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 GE
202b6 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20 20 20 OP_Lt. **
202b7 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 TK_LT
202b8 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a 0a 20 OP_Ge. **.
202b9 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20 76 61 ** For other va
202ba 6c 75 65 73 20 6f 66 20 70 45 78 70 72 2d 3e 6f lues of pExpr->o
202bb 70 2c 20 6f 70 20 69 73 20 75 6e 64 65 66 69 6e p, op is undefin
202bc 65 64 20 61 6e 64 20 75 6e 75 73 65 64 2e 0a 20 ed and unused..
202bd 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 ** The value of
202be 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e TK_ and OP_ con
202bf 73 74 61 6e 74 73 20 61 72 65 20 61 72 72 61 6e stants are arran
202c0 67 65 64 20 73 75 63 68 20 74 68 61 74 20 77 65 ged such that we
202c1 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70 75 74 . ** can comput
202c2 65 20 74 68 65 20 6d 61 70 70 69 6e 67 20 61 62 e the mapping ab
202c3 6f 76 65 20 75 73 69 6e 67 20 74 68 65 20 66 6f ove using the fo
202c4 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69 llowing expressi
202c5 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72 74 28 on.. ** Assert(
202c6 29 73 20 76 65 72 69 66 79 20 74 68 61 74 20 74 )s verify that t
202c7 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 he computation i
202c8 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a s correct.. */.
202c9 20 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e op = ((pExpr->
202ca 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 op+(TK_ISNULL&1)
202cb 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c 26 )^1)-(TK_ISNULL&
202cc 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 1);.. /* Verify
202cd 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d 65 correct alignme
202ce 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 nt of TK_ and OP
202cf 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f _ constants. */
202d0 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
202d1 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ->op!=TK_ISNULL
202d2 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c || op==OP_NotNul
202d3 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 l );. assert( p
202d4 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 4f 54 Expr->op!=TK_NOT
202d5 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 49 NULL || op==OP_I
202d6 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 sNull );. asser
202d7 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b t( pExpr->op!=TK
202d8 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 45 71 _NE || op==OP_Eq
202d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
202da 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51 20 7c xpr->op!=TK_EQ |
202db 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 | op==OP_Ne );.
202dc 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
202dd 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f 70 3d op!=TK_LT || op=
202de 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73 73 65 =OP_Ge );. asse
202df 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 rt( pExpr->op!=T
202e0 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 K_LE || op==OP_G
202e1 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
202e2 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 54 20 Expr->op!=TK_GT
202e3 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29 3b 0a || op==OP_Le );.
202e4 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
202e5 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 6f 70 >op!=TK_GE || op
202e6 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20 73 77 ==OP_Lt );.. sw
202e7 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 itch( pExpr->op
202e8 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 ){. case TK_A
202e9 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 ND: {. test
202ea 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
202eb 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
202ec 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
202ed 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
202ee 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 Left, dest, jump
202ef 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 IfNull);. s
202f0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
202f1 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
202f2 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a >pRight, dest, j
202f3 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
202f4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
202f5 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b case TK_OR: {
202f6 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 . int d2 =
202f7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
202f8 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74 abel(v);. t
202f9 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e estcase( jumpIfN
202fa 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ull==0 );.
202fb 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
202fc 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 Push(pParse);.
202fd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
202fe 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 fTrue(pParse, pE
202ff 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 20 xpr->pLeft, d2,
20300 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 jumpIfNull^SQLIT
20301 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 E_JUMPIFNULL);.
20302 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
20303 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
20304 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 pExpr->pRight, d
20305 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
20306 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20307 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
20308 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 73 71 v, d2);. sq
20309 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
2030a 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 p(pParse, 1);.
2030b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
2030c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 . case TK_NOT
2030d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
2030e 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 3ExprIfTrue(pPar
2030f 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
20310 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
20311 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ll);. break
20312 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
20313 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 TK_LT:. case
20314 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 TK_LE:. case
20315 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 TK_GT:. case
20316 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 TK_GE:. case
20317 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 TK_NE:. case
20318 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 TK_EQ: {.
20319 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
2031a 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _LT );. tes
2031b 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 tcase( op==TK_LE
2031c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2031d 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b se( op==TK_GT );
2031e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
2031f 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 op==TK_GE );.
20320 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
20321 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 ==TK_EQ );.
20322 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
20323 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_NE );. te
20324 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
20325 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 ll==0 );. c
20326 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
20327 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ds(pParse, pExpr
20328 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 ->pLeft, &r1, &r
20329 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 egFree1,.
2032a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2032b 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
2032c 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 ->pRight, &r2, &
2032d 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
2032e 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 codeCompare(pPa
2032f 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
20330 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 t, pExpr->pRight
20331 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 , op,.
20332 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 r1, r2,
20333 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
20334 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
20335 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
20336 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
20337 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
20338 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
20339 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
2033a 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 ISNULL:. case
2033b 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 TK_NOTNULL: {.
2033c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
2033d 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a p==TK_ISNULL );.
2033e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
2033f 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 op==TK_NOTNULL )
20340 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c ;. r1 = sql
20341 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
20342 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
20343 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 pLeft, ®Free1
20344 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
20345 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 VdbeAddOp2(v, op
20346 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 , r1, dest);.
20347 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
20348 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
20349 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
2034a 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 case TK_BETWE
2034b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 EN: {. /*
2034c 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e x BETWEEN y AN
2034d 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 D z. **.
2034e 20 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c ** Is equival
2034f 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a ent to . **
20350 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d . ** x>=
20351 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 y AND x<=z.
20352 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 **. ** Cod
20353 65 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61 e it as such, ta
20354 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20 king care to do
20355 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 the common subex
20356 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a pression. *
20357 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f * elementation o
20358 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 f x.. */.
20359 20 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 Expr exprAnd
2035a 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d ;. Expr com
2035b 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 pLeft;. Exp
2035c 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 r compRight;.
2035d 20 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a Expr exprX;..
2035e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
2035f 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20360 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
20361 63 74 29 20 29 3b 0a 20 20 20 20 20 20 65 78 70 ct) );. exp
20362 72 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 rX = *pExpr->pLe
20363 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e ft;. exprAn
20364 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 d.op = TK_AND;.
20365 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 exprAnd.pLe
20366 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a ft = &compLeft;.
20367 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 exprAnd.pR
20368 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 ight = &compRigh
20369 74 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 t;. compLef
2036a 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 t.op = TK_GE;.
2036b 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 compLeft.pLe
2036c 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 ft = &exprX;.
2036d 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 compLeft.pRig
2036e 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c ht = pExpr->x.pL
2036f 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b ist->a[0].pExpr;
20370 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 . compRight
20371 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 .op = TK_LE;.
20372 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 compRight.pLe
20373 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 ft = &exprX;.
20374 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 compRight.pRi
20375 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 ght = pExpr->x.p
20376 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 List->a[1].pExpr
20377 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 69 54 ;. exprX.iT
20378 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 able = sqlite3Ex
20379 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
2037a 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67 46 e, &exprX, ®F
2037b 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 ree1);. tes
2037c 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
2037d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 =0 );. expr
2037e 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 X.op = TK_REGIST
2037f 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 ER;. testca
20380 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
20381 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
20382 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 e3ExprIfFalse(pP
20383 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20 arse, &exprAnd,
20384 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
20385 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
20386 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
20387 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20 3d 20 t: {. r1 =
20388 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
20389 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
2038a 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 r, ®Free1);.
2038b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
2038c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 4e AddOp3(v, OP_IfN
2038d 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75 ot, r1, dest, ju
2038e 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20 mpIfNull!=0);.
2038f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
20390 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
20391 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
20392 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
20393 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
20394 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 . }. sqlite3Re
20395 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
20396 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a rse, regFree1);.
20397 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
20398 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
20399 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a 2f 2a regFree2);.}../*
2039a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20 63 6f .** Do a deep co
2039b 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 mparison of two
2039c 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 expression trees
2039d 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 . Return TRUE (
2039e 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69 66 20 non-zero).** if
2039f 74 68 65 79 20 61 72 65 20 69 64 65 6e 74 69 63 they are identic
203a0 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 46 41 al and return FA
203a1 4c 53 45 20 69 66 20 74 68 65 79 20 64 69 66 66 LSE if they diff
203a2 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e 0a 2a er in any way..*
203a3 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 74 *.** Sometimes t
203a4 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
203a5 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 65 76 return FALSE ev
203a6 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20 65 78 en if the two ex
203a7 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72 65 61 pressions.** rea
203a8 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61 6c 65 lly are equivale
203a9 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e 6e 6f nt. If we canno
203aa 74 20 70 72 6f 76 65 20 74 68 61 74 20 74 68 65 t prove that the
203ab 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 expressions are
203ac 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c 20 77 .** identical, w
203ad 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 6a e return FALSE j
203ae 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20 ust to be safe.
203af 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 So if this rout
203b0 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66 ine.** returns f
203b1 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75 20 64 alse, then you d
203b2 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b 6e 6f o not really kno
203b3 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20 69 66 w for certain if
203b4 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78 70 72 the two.** expr
203b5 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 essions are the
203b6 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20 79 6f same. But if yo
203b7 75 20 67 65 74 20 61 20 54 52 55 45 20 72 65 74 u get a TRUE ret
203b8 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a 2a 2a urn, then you.**
203b9 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 65 can be sure the
203ba 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 expressions are
203bb 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e 20 74 the same. In t
203bc 68 65 20 70 6c 61 63 65 73 20 77 68 65 72 65 0a he places where.
203bd 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
203be 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f 65 73 is used, it does
203bf 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67 65 74 not hurt to get
203c0 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53 45 20 an extra FALSE
203c1 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74 20 6d - that.** just m
203c2 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 73 ight result in s
203c3 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73 6c 6f ome slightly slo
203c4 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74 20 72 wer code. But r
203c5 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e 20 69 eturning.** an i
203c6 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20 63 6f ncorrect TRUE co
203c7 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 6d 61 uld lead to a ma
203c8 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 lfunction..*/.SQ
203c9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
203ca 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 sqlite3ExprComp
203cb 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20 45 78 are(Expr *pA, Ex
203cc 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74 20 69 pr *pB){. int i
203cd 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c 7c 70 ;. if( pA==0||p
203ce 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 B==0 ){. retu
203cf 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d 0a 20 rn pB==pA;. }.
203d0 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
203d1 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53 erty(pA, EP_xIsS
203d2 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61 elect) || ExprHa
203d3 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50 sProperty(pB, EP
203d4 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
203d5 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
203d6 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 . if( (pA->flag
203d7 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 s & EP_Distinct)
203d8 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45 !=(pB->flags & E
203d9 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65 P_Distinct) ) re
203da 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 turn 0;. if( pA
203db 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 ->op!=pB->op ) r
203dc 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 eturn 0;. if( !
203dd 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
203de 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42 re(pA->pLeft, pB
203df 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 ->pLeft) ) retur
203e0 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 n 0;. if( !sqli
203e1 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
203e2 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70 A->pRight, pB->p
203e3 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 Right) ) return
203e4 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e 0;.. if( pA->x.
203e5 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70 pList && pB->x.p
203e6 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 List ){. if(
203e7 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 pA->x.pList->nEx
203e8 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d pr!=pB->x.pList-
203e9 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e 20 >nExpr ) return
203ea 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 0;. for(i=0;
203eb 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e i<pA->x.pList->n
203ec 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
203ed 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d Expr *pExprA =
203ee 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b pA->x.pList->a[
203ef 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 i].pExpr;.
203f0 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70 Expr *pExprB = p
203f1 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d B->x.pList->a[i]
203f2 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 .pExpr;. if
203f3 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f ( !sqlite3ExprCo
203f4 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70 45 mpare(pExprA, pE
203f5 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20 30 xprB) ) return 0
203f6 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
203f7 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20 if( pA->x.pList
203f8 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29 || pB->x.pList )
203f9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
203fa 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69 }.. if( pA->i
203fb 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c Table!=pB->iTabl
203fc 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e e || pA->iColumn
203fd 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 !=pB->iColumn )
203fe 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
203ff 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d pA->op!=TK_COLUM
20400 4e 20 26 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a N && pA->token.z
20401 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e ){. if( pB->
20402 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74 token.z==0 ) ret
20403 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 70 urn 0;. if( p
20404 42 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e B->token.n!=pA->
20405 74 6f 6b 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e token.n ) return
20406 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
20407 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 te3StrNICmp((cha
20408 72 2a 29 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 r*)pA->token.z,(
20409 63 68 61 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e char*)pB->token.
2040a 7a 2c 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d z,pB->token.n)!=
2040b 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
2040c 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 0;. }. }.
2040d 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f return 1;.}.../
2040e 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 *.** Add a new e
2040f 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 lement to the pA
20410 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 ggInfo->aCol[] a
20411 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 rray. Return th
20412 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 e index of.** th
20413 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 e new element.
20414 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 Return a negativ
20415 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c e number if mall
20416 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 oc fails..*/.sta
20417 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e tic int addAggIn
20418 66 6f 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 foColumn(sqlite3
20419 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 *db, AggInfo *p
2041a 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a Info){. int i;.
2041b 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 pInfo->aCol =
2041c 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f sqlite3ArrayAllo
2041d 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c cate(. db,
2041e 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 . pInfo->a
2041f 43 6f 6c 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 Col,. size
20420 6f 66 28 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 of(pInfo->aCol[0
20421 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 ]),. 3,.
20422 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f &pInfo->nCo
20423 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 lumn,. &pI
20424 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f nfo->nColumnAllo
20425 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 c,. &i. )
20426 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 ;. return i;.}
20427 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 ../*.** Add a
20428 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 new element to
20429 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 the pAggInfo->aF
2042a 75 6e 63 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 unc[] array. Re
2042b 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f turn the index o
2042c 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 f.** the new ele
2042d 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 ment. Return a
2042e 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 negative number
2042f 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e if malloc fails.
20430 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
20431 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 ddAggInfoFunc(sq
20432 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e lite3 *db, AggIn
20433 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e fo *pInfo){. in
20434 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 t i;. pInfo->aF
20435 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 41 72 72 unc = sqlite3Arr
20436 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 ayAllocate(.
20437 20 20 20 64 62 2c 20 0a 20 20 20 20 20 20 20 70 db, . p
20438 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 Info->aFunc,.
20439 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f sizeof(pInfo
2043a 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 ->aFunc[0]),.
2043b 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 3,. &p
2043c 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 Info->nFunc,.
2043d 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e &pInfo->nFun
2043e 63 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 cAlloc,. &
2043f 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 i. );. return
20440 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 i;.} ../*.**
20441 54 68 69 73 20 69 73 20 74 68 65 20 78 45 78 70 This is the xExp
20442 72 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 rCallback for a
20443 74 72 65 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 tree walker. It
20444 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 is used to.** i
20445 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 mplement sqlite3
20446 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 ExprAnalyzeAggre
20447 67 61 74 65 73 28 29 2e 20 20 53 65 65 20 73 71 gates(). See sq
20448 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 lite3ExprAnalyze
20449 41 67 67 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f Aggregates.** fo
2044a 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
2044b 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 ormation..*/.sta
2044c 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 tic int analyzeA
2044d 67 67 72 65 67 61 74 65 28 57 61 6c 6b 65 72 20 ggregate(Walker
2044e 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
2044f 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b pExpr){. int i;
20450 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
20451 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 pNC = pWalker->u
20452 2e 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 .pNC;. Parse *p
20453 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 Parse = pNC->pPa
20454 72 73 65 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a rse;. SrcList *
20455 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e pSrcList = pNC->
20456 70 53 72 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 pSrcList;. AggI
20457 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 nfo *pAggInfo =
20458 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a pNC->pAggInfo;..
20459 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
2045a 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
2045b 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 TK_AGG_COLUMN:.
2045c 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d case TK_COLUM
2045d 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 N: {. testc
2045e 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
2045f 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b TK_AGG_COLUMN );
20460 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20461 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 pExpr->op==TK_C
20462 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f OLUMN );. /
20463 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
20464 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 f the column is
20465 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 in one of the ta
20466 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d bles in the FROM
20467 0a 20 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 . ** clause
20468 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 of the aggregat
20469 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 e query */.
2046a 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 7b if( pSrcList ){
2046b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
2046c 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
2046d 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e tem = pSrcList->
2046e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 a;. for(i
2046f 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e =0; i<pSrcList->
20470 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d nSrc; i++, pItem
20471 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ++){. s
20472 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f truct AggInfo_co
20473 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 l *pCol;.
20474 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 if( pExpr->iT
20475 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 able==pItem->iCu
20476 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 rsor ){.
20477 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 /* If we rea
20478 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 ch this point, i
20479 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78 t means that pEx
2047a 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 pr refers to a t
2047b 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 able.
2047c 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74 ** that is in t
2047d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f he FROM clause o
2047e 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 f the aggregate
2047f 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20 query. .
20480 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
20481 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 ** Make an e
20482 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c ntry for the col
20483 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d umn in pAggInfo-
20484 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 >aCol[] if there
20485 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
20486 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 is not an entry
20487 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 there already..
20488 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
20489 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b int k;
2048a 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f . pCo
2048b 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 l = pAggInfo->aC
2048c 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ol;.
2048d 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 for(k=0; k<pAggI
2048e 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b nfo->nColumn; k+
2048f 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 +, pCol++){.
20490 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 if( pC
20491 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 ol->iTable==pExp
20492 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 r->iTable &&.
20493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
20494 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 Col->iColumn==pE
20495 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a xpr->iColumn ){.
20496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20497 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
20498 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
20499 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
2049a 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 if( (k>=pAggInf
2049b 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 o->nColumn).
2049c 20 20 20 20 20 20 20 20 20 26 26 20 28 6b 20 3d && (k =
2049d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d addAggInfoColum
2049e 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 n(pParse->db, pA
2049f 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 ggInfo))>=0 .
204a0 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 ){.
204a1 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d pCol =
204a2 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c &pAggInfo->aCol
204a3 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 [k];.
204a4 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 pCol->pTab =
204a5 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 pExpr->pTab;.
204a6 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d pCol-
204a7 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d >iTable = pExpr-
204a8 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 >iTable;.
204a9 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f pCol->iCo
204aa 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 lumn = pExpr->iC
204ab 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 olumn;.
204ac 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 pCol->iMem
204ad 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
204ae 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
204af 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c pCol->iSorterCol
204b0 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 umn = -1;.
204b1 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 pCol->pE
204b2 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 xpr = pExpr;.
204b3 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
204b4 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 AggInfo->pGroupB
204b5 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 y ){.
204b6 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 int j, n;.
204b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
204b8 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 xprList *pGB = p
204b9 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 AggInfo->pGroupB
204ba 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y;.
204bb 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
204bc 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d st_item *pTerm =
204bd 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 pGB->a;.
204be 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42 n = pGB
204bf 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ->nExpr;.
204c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 for(j=0
204c1 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 ; j<n; j++, pTer
204c2 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 m++){.
204c3 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 Expr *pE
204c4 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b = pTerm->pExpr;
204c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
204c6 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 if( pE->op==T
204c7 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e K_COLUMN && pE->
204c8 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 iTable==pExpr->i
204c9 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 Table &&.
204ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
204cb 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 E->iColumn==pExp
204cc 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 r->iColumn ){.
204cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
204ce 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 pCol->iSorterC
204cf 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 olumn = j;.
204d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
204d1 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
204d2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
204d3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
204d4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
204d5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 if( pC
204d6 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d ol->iSorterColum
204d7 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 n<0 ){.
204d8 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f pCol->iSo
204d9 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 rterColumn = pAg
204da 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 gInfo->nSortingC
204db 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 olumn++;.
204dc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
204dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
204de 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e /* There is n
204df 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 ow an entry for
204e0 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66 pExpr in pAggInf
204e1 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 o->aCol[] (eithe
204e2 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a r. **
204e3 20 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20 because it was
204e4 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20 there before or
204e5 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20 because we just
204e6 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20 20 created it)..
204e7 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 ** Conv
204e8 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f ert the pExpr to
204e9 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c be a TK_AGG_COL
204ea 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f UMN referring to
204eb 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20 that.
204ec 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 ** pAggInfo->a
204ed 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 Col[] entry..
204ee 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
204ef 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
204f0 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e AggInfo = pAggIn
204f1 66 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 fo;.
204f2 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 pExpr->op = TK_A
204f3 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 GG_COLUMN;.
204f4 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 pExpr->iA
204f5 67 67 20 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 gg = k;.
204f6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
204f7 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 } /* endif
204f8 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 pExpr->iTable==p
204f9 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f Item->iCursor */
204fa 0a 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e . } /* en
204fb 64 20 6c 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 d loop over pSrc
204fc 4c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a List */. }.
204fd 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
204fe 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 _Prune;. }.
204ff 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 case TK_AGG_FU
20500 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 NCTION: {.
20501 2f 2a 20 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 /* The pNC->nDep
20502 74 68 3d 3d 30 20 74 65 73 74 20 63 61 75 73 65 th==0 test cause
20503 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 s aggregate func
20504 74 69 6f 6e 73 20 69 6e 20 73 75 62 71 75 65 72 tions in subquer
20505 69 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ies. ** to
20506 62 65 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 be ignored */.
20507 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 if( pNC->nDe
20508 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pth==0 ){.
20509 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
2050a 65 20 69 66 20 70 45 78 70 72 20 69 73 20 61 20 e if pExpr is a
2050b 64 75 70 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f duplicate of ano
2050c 74 68 65 72 20 61 67 67 72 65 67 61 74 65 20 0a ther aggregate .
2050d 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
2050e 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6c 72 65 ion that is alre
2050f 61 64 79 20 69 6e 20 74 68 65 20 70 41 67 67 49 ady in the pAggI
20510 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 nfo structure.
20511 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
20512 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f struct AggInfo_
20513 66 75 6e 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 func *pItem = pA
20514 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 ggInfo->aFunc;.
20515 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
20516 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e i<pAggInfo->nFun
20517 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 c; i++, pItem++)
20518 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
20519 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
2051a 72 65 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c re(pItem->pExpr,
2051b 20 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 pExpr) ){.
2051c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
2051d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2051e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
2051f 69 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 i>=pAggInfo->nFu
20520 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 nc ){.
20521 2f 2a 20 70 45 78 70 72 20 69 73 20 6f 72 69 67 /* pExpr is orig
20522 69 6e 61 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 inal. Make a ne
20523 77 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 w entry in pAggI
20524 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 nfo->aFunc[].
20525 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
20526 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 u8 enc = ENC
20527 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 (pParse->db);.
20528 20 20 20 20 20 20 20 20 69 20 3d 20 61 64 64 41 i = addA
20529 67 67 49 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 ggInfoFunc(pPars
2052a 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 e->db, pAggInfo)
2052b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
2052c 69 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 i>=0 ){.
2052d 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
2052e 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
2052f 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pr, EP_xIsSelect
20530 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ) );.
20531 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e pItem = &pAggIn
20532 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 fo->aFunc[i];.
20533 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d pItem-
20534 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a >pExpr = pExpr;.
20535 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 pIte
20536 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 m->iMem = ++pPar
20537 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
20538 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75 pItem->pFu
20539 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 nc = sqlite3Find
2053a 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
2053b 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 >db,.
2053c 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 (char*)p
2053d 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 Expr->token.z, p
2053e 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 Expr->token.n,.
2053f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20540 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 pExpr->x.pList
20541 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 ? pExpr->x.pLis
20542 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e t->nExpr : 0, en
20543 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 c, 0);.
20544 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c if( pExpr->fl
20545 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 ags & EP_Distinc
20546 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
20547 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 pItem->iDisti
20548 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 nct = pParse->nT
20549 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 ab++;.
2054a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2054b 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 pItem->iD
2054c 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 istinct = -1;.
2054d 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
2054e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2054f 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b }. /* Mak
20550 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f e pExpr point to
20551 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
20552 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 pAggInfo->aFunc
20553 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20 [] entry.
20554 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 45 78 70 */. pExp
20555 72 2d 3e 69 41 67 67 20 3d 20 69 3b 0a 20 20 20 r->iAgg = i;.
20556 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 pExpr->pAgg
20557 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b Info = pAggInfo;
20558 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
20559 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 WRC_Prune;.
2055a 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
2055b 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
2055c 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 ue;.}.static int
2055d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 analyzeAggregat
2055e 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65 esInSelect(Walke
2055f 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
20560 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 ct *pSelect){.
20561 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 NameContext *pNC
20562 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e = pWalker->u.pN
20563 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 C;. if( pNC->nD
20564 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70 epth==0 ){. p
20565 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 NC->nDepth++;.
20566 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c sqlite3WalkSel
20567 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 ect(pWalker, pSe
20568 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e lect);. pNC->
20569 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65 nDepth--;. re
2056a 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
2056b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
2056c 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 urn WRC_Continue
2056d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
2056e 6e 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e nalyze the given
2056f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b expression look
20570 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 ing for aggregat
20571 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a e functions and.
20572 2a 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73 ** for variables
20573 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 that need to be
20574 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50 added to the pP
20575 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 arse->aAgg[] arr
20576 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69 ay..** Make addi
20577 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74 tional entries t
20578 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 o the pParse->aA
20579 67 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 gg[] array as ne
2057a 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 cessary..**.** T
2057b 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
2057c 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ld only be calle
2057d 64 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72 d after the expr
2057e 65 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a ession has been.
2057f 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 ** analyzed by s
20580 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
20581 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c rNames()..*/.SQL
20582 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20583 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c sqlite3ExprAnal
20584 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 yzeAggregates(Na
20585 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 meContext *pNC,
20586 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 Expr *pExpr){.
20587 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 Walker w;. w.xE
20588 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e xprCallback = an
20589 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 3b 0a alyzeAggregate;.
2058a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 w.xSelectCallb
2058b 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 ack = analyzeAgg
2058c 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 3b regatesInSelect;
2058d 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 . w.u.pNC = pNC
2058e 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 ;. sqlite3WalkE
2058f 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a xpr(&w, pExpr);.
20590 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 }../*.** Call sq
20591 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 lite3ExprAnalyze
20592 41 67 67 72 65 67 61 74 65 73 28 29 20 66 6f 72 Aggregates() for
20593 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f every expressio
20594 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65 n in an.** expre
20595 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 ssion list. Ret
20596 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
20597 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 f errors..**.**
20598 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 66 If an error is f
20599 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79 73 ound, the analys
2059a 69 73 20 69 73 20 63 75 74 20 73 68 6f 72 74 2e is is cut short.
2059b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
2059c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
2059d 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 xprAnalyzeAggLis
2059e 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 t(NameContext *p
2059f 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c NC, ExprList *pL
205a0 69 73 74 29 7b 0a 20 20 73 74 72 75 63 74 20 45 ist){. struct E
205a1 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
205a2 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 tem;. int i;.
205a3 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 if( pList ){.
205a4 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 for(pItem=pList
205a5 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 ->a, i=0; i<pLis
205a6 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 t->nExpr; i++, p
205a7 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 Item++){. s
205a8 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a qlite3ExprAnalyz
205a9 65 41 67 67 72 65 67 61 74 65 73 28 70 4e 43 2c eAggregates(pNC,
205aa 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a pItem->pExpr);.
205ab 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
205ac 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 73 69 ** Allocate a si
205ad 6e 67 6c 65 20 6e 65 77 20 72 65 67 69 73 74 65 ngle new registe
205ae 72 20 66 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c r for use to hol
205af 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69 d some intermedi
205b0 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53 ate result..*/.S
205b1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
205b2 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 t sqlite3GetTemp
205b3 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 Reg(Parse *pPars
205b4 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 e){. if( pParse
205b5 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b ->nTempReg==0 ){
205b6 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 50 . return ++pP
205b7 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a arse->nMem;. }.
205b8 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d return pParse-
205b9 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72 >aTempReg[--pPar
205ba 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d se->nTempReg];.}
205bb 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 ../*.** Dealloca
205bc 74 65 20 61 20 72 65 67 69 73 74 65 72 2c 20 6d te a register, m
205bd 61 6b 69 6e 67 20 61 76 61 69 6c 61 62 6c 65 20 aking available
205be 66 6f 72 20 72 65 75 73 65 20 66 6f 72 20 73 6f for reuse for so
205bf 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 me other.** purp
205c0 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ose..**.** If a
205c1 72 65 67 69 73 74 65 72 20 69 73 20 63 75 72 72 register is curr
205c2 65 6e 74 6c 79 20 62 65 69 6e 67 20 75 73 65 64 ently being used
205c3 20 62 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 by the column c
205c4 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 ache, then.** th
205c5 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 e dallocation is
205c6 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 deferred until
205c7 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 the column cache
205c8 20 6c 69 6e 65 20 74 68 61 74 20 75 73 65 73 0a line that uses.
205c9 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 ** the register
205ca 62 65 63 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a becomes stale..*
205cb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
205cc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c void sqlite3Rel
205cd 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 easeTempReg(Pars
205ce 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
205cf 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67 Reg){. if( iReg
205d0 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d && pParse->nTem
205d1 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 pReg<ArraySize(p
205d2 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 Parse->aTempReg)
205d3 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
205d4 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 struct yColCa
205d5 63 68 65 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 che *p;. for(
205d6 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
205d7 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
205d8 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
205d9 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 ++, p++){.
205da 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 if( p->iReg==iRe
205db 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e g ){. p->
205dc 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a 20 20 20 tempReg = 1;.
205dd 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
205de 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
205df 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b Parse->aTempReg[
205e0 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 pParse->nTempReg
205e1 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a ++] = iReg;. }.
205e2 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
205e3 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 e or deallocate
205e4 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20 a block of nReg
205e5 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69 consecutive regi
205e6 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f sters.*/.SQLITE_
205e7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
205e8 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 te3GetTempRange(
205e9 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
205ea 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt nReg){. int
205eb 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72 i, n;. i = pPar
205ec 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20 se->iRangeReg;.
205ed 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61 n = pParse->nRa
205ee 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e 52 ngeReg;. if( nR
205ef 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 73 eg<=n && !usedAs
205f0 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 ColumnCache(pPar
205f1 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b se, i, i+n-1) ){
205f2 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61 . pParse->iRa
205f3 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a ngeReg += nReg;.
205f4 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e pParse->nRan
205f5 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20 geReg -= nReg;.
205f6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20 }else{. i =
205f7 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a pParse->nMem+1;.
205f8 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d pParse->nMem
205f9 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20 += nReg;. }.
205fa 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 return i;.}.SQLI
205fb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
205fc 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
205fd 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 mpRange(Parse *p
205fe 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c Parse, int iReg,
205ff 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 66 int nReg){. if
20600 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e ( nReg>pParse->n
20601 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 20 RangeReg ){.
20602 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 pParse->nRangeRe
20603 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 g = nReg;. pP
20604 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 arse->iRangeReg
20605 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f = iReg;. }.}../
20606 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
20607 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a nd of expr.c ***
20608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2060a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
2060b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
2060c 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e egin file alter.
2060d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
2060e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2060f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
20610 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 61 *.** 2005 Februa
20611 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ry 15.**.** The
20612 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
20613 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
20614 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
20615 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
20616 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
20617 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
20618 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
20619 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
2061a 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
2061b 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
2061c 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
2061d 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
2061e 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
2061f 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
20620 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
20621 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
20622 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
20623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20627 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
20628 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 e contains C cod
20629 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 e routines that
2062a 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
2062b 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68 VDBE code.** th
2062c 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
2062d 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f e ALTER TABLE co
2062e 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 mmand..**.** $Id
2062f 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 35 37 : alter.c,v 1.57
20630 20 32 30 30 39 2f 30 34 2f 31 36 20 31 36 3a 33 2009/04/16 16:3
20631 30 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 0:18 drh Exp $.*
20632 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 /../*.** The cod
20633 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f e in this file o
20634 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 65 nly exists if we
20635 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 6e are not omittin
20636 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 g the.** ALTER T
20637 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d 20 ABLE logic from
20638 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 the build..*/.#i
20639 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
2063a 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a 2f T_ALTERTABLE.../
2063b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
2063c 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51 on is used by SQ
2063d 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 L generated to i
2063e 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a 2a mplement the .**
2063f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d ALTER TABLE com
20640 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20 mand. The first
20641 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
20642 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45 text of a CREATE
20643 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52 45 TABLE or.** CRE
20644 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61 6e ATE INDEX comman
20645 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 d. The second is
20646 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 a table name. T
20647 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e he table name in
20648 20 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45 20 .** the CREATE
20649 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 TABLE or CREATE
2064a 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 INDEX statement
2064b 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 is replaced with
2064c 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 the third.** ar
2064d 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 gument and the r
2064e 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e 20 esult returned.
2064f 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 Examples:.**.**
20650 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 sqlite_rename_ta
20651 62 6c 65 28 27 43 52 45 41 54 45 20 54 41 42 4c ble('CREATE TABL
20652 45 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27 2c E abc(a, b, c)',
20653 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 'def').** -
20654 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 > 'CREATE TABLE
20655 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2a def(a, b, c)'.**
20656 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d .** sqlite_renam
20657 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20 e_table('CREATE
20658 49 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28 61 INDEX i ON abc(a
20659 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 )', 'def').**
2065a 20 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e 44 -> 'CREATE IND
2065b 45 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20 62 EX i ON def(a, b
2065c 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20 , c)'.*/.static
2065d 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c 65 void renameTable
2065e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
2065f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
20660 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
20661 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
20662 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 **argv.){. uns
20663 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 igned char const
20664 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 *zSql = sqlite3
20665 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
20666 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 [0]);. unsigned
20667 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 char const *zTa
20668 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 bleName = sqlite
20669 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
2066a 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f v[1]);.. int to
2066b 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 ken;. Token tna
2066c 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 me;. unsigned c
2066d 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 har const *zCsr
2066e 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 = zSql;. int le
2066f 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a n = 0;. char *z
20670 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 20 Ret;.. sqlite3
20671 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
20672 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
20673 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 context);.. UNU
20674 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
20675 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 tUsed);.. /* Th
20676 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 e principle used
20677 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 to locate the t
20678 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 able name in the
20679 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a 20 CREATE TABLE .
2067a 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ** statement is
2067b 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 that the table
2067c 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 name is the firs
2067d 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b 65 t non-space toke
2067e 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20 69 n that. ** is i
2067f 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f mmediately follo
20680 77 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 6f wed by a TK_LP o
20681 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 6e r TK_USING token
20682 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71 .. */. if( zSq
20683 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 l ){. do {.
20684 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 29 if( !*zCsr )
20685 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e {. /* Ran
20686 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65 out of input be
20687 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e 20 fore finding an
20688 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74 2e opening bracket.
20689 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f Return NULL. */
2068a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b . return;
2068b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
2068c 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b /* Store the tok
2068d 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 en that zCsr poi
2068e 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e nts to in tname.
2068f 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e */. tname.
20690 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 z = zCsr;.
20691 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a tname.n = len;..
20692 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 /* Advance
20693 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 zCsr to the nex
20694 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 t token. Store t
20695 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 hat token type i
20696 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 n 'token',.
20697 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 ** and its leng
20698 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 th in 'len' (to
20699 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 be used next ite
2069a 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c ration of this l
2069b 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 oop).. */.
2069c 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 do {.
2069d 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 zCsr += len;.
2069e 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c len = sql
2069f 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 ite3GetToken(zCs
206a0 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 r, &token);.
206a1 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e } while( token
206a2 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 ==TK_SPACE );.
206a3 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e assert( len>
206a4 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65 0 );. } while
206a5 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 ( token!=TK_LP &
206a6 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e & token!=TK_USIN
206a7 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d G );.. zRet =
206a8 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
206a9 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 db, "%.*s\"%w\"%
206aa 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 s", tname.z - zS
206ab 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 ql, zSql, .
206ac 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e zTableName, tn
206ad 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a ame.z+tname.n);.
206ae 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
206af 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
206b0 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 zRet, -1, SQLIT
206b1 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a E_DYNAMIC);. }.
206b2 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
206b3 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f E_OMIT_TRIGGER./
206b4 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
206b5 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 is used by SQL g
206b6 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c enerated to impl
206b7 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 ement the.** ALT
206b8 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 ER TABLE command
206b9 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 . The first argu
206ba 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 ment is the text
206bb 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49 of a CREATE TRI
206bc 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65 GGER .** stateme
206bd 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 nt. The second i
206be 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 s a table name.
206bf 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 The table name i
206c0 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a n the CREATE .**
206c1 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
206c2 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 nt is replaced w
206c3 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72 ith the third ar
206c4 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 gument and the r
206c5 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e esult .** return
206c6 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c ed. This is anal
206c7 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54 agous to renameT
206c8 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65 ableFunc() above
206c9 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45 , except for CRE
206ca 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20 ATE.** TRIGGER,
206cb 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 not CREATE INDEX
206cc 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c and CREATE TABL
206cd 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 E..*/.static voi
206ce 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 d renameTriggerF
206cf 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
206d0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
206d1 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
206d2 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
206d3 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 **argv.){. unsi
206d4 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
206d5 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f *zSql = sqlite3_
206d6 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
206d7 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 0]);. unsigned
206d8 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 char const *zTab
206d9 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 leName = sqlite3
206da 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
206db 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b [1]);.. int tok
206dc 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d en;. Token tnam
206dd 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20 e;. int dist =
206de 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 3;. unsigned ch
206df 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d ar const *zCsr =
206e0 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e zSql;. int len
206e1 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 = 0;. char *zR
206e2 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 et;. sqlite3 *d
206e3 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
206e4 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
206e5 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 ntext);.. UNUSE
206e6 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
206e7 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 sed);.. /* The
206e8 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 principle used t
206e9 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 o locate the tab
206ea 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 le name in the C
206eb 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20 REATE TRIGGER .
206ec 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ** statement is
206ed 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 that the table
206ee 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 name is the firs
206ef 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 t token that is
206f0 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a immediatedly. *
206f1 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69 * preceded by ei
206f2 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b ther TK_ON or TK
206f3 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 _DOT and immedia
206f4 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 tedly followed b
206f5 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b y one. ** of TK
206f6 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 _WHEN, TK_BEGIN
206f7 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a or TK_FOR.. */.
206f8 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 if( zSql ){.
206f9 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66 do {.. if
206fa 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 ( !*zCsr ){.
206fb 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f /* Ran out o
206fc 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 f input before f
206fd 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65 inding the table
206fe 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55 name. Return NU
206ff 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 LL. */. r
20700 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a eturn;. }..
20701 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 /* Store t
20702 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 he token that zC
20703 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 sr points to in
20704 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 tname. */.
20705 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a tname.z = zCsr;.
20706 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 tname.n =
20707 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 len;.. /* A
20708 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 dvance zCsr to t
20709 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 he next token. S
2070a 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 tore that token
2070b 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c type in 'token',
2070c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 . ** and it
2070d 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e s length in 'len
2070e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 ' (to be used ne
2070f 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 xt iteration of
20710 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 this loop)..
20711 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a */. do {.
20712 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 zCsr +=
20713 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e len;. len
20714 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b = sqlite3GetTok
20715 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 en(zCsr, &token)
20716 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 ;. }while(
20717 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 token==TK_SPACE
20718 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
20719 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20 len>0 );..
2071a 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69 /* Variable 'di
2071b 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e st' stores the n
2071c 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 umber of tokens
2071d 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d read since the m
2071e 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 ost. ** rec
2071f 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b ent TK_DOT or TK
20720 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 _ON. This means
20721 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e that when a WHEN
20722 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a , FOR or BEGIN .
20723 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 ** token i
20724 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74 s read and 'dist
20725 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20 ' equals 2, the
20726 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64 condition stated
20727 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 above. **
20728 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20 to be met..
20729 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
2072a 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74 e that ON cannot
2072b 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20 be a database,
2072c 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 table or column
2072d 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a name, so. *
2072e 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 * there is no ne
2072f 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 ed to worry abou
20730 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20 t syntax like .
20731 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20 ** "CREATE
20732 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f TRIGGER ... ON O
20733 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20 N.ON BEGIN ..."
20734 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 etc.. */.
20735 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20 dist++;.
20736 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f if( token==TK_
20737 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b DOT || token==TK
20738 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64 _ON ){. d
20739 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d ist = 0;. }
2073a 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69 . } while( di
2073b 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21 st!=2 || (token!
2073c 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65 =TK_WHEN && toke
2073d 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b n!=TK_FOR && tok
2073e 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b en!=TK_BEGIN) );
2073f 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c .. /* Variabl
20740 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74 e tname now cont
20741 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 ains the token t
20742 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74 hat is the old t
20743 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a able-name. **
20744 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 in the CREATE T
20745 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 RIGGER statement
20746 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65 .. */. zRe
20747 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e t = sqlite3MPrin
20748 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 tf(db, "%.*s\"%w
20749 5c 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d \"%s", tname.z -
2074a 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 zSql, zSql, .
2074b 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c zTableName,
2074c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e tname.z+tname.n
2074d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
2074e 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
2074f 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 xt, zRet, -1, SQ
20750 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 LITE_DYNAMIC);.
20751 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a }.}.#endif /*
20752 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 !SQLITE_OMIT_TR
20753 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 IGGER */../*.**
20754 52 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69 Register built-i
20755 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 n functions used
20756 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 to help impleme
20757 6e 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a nt ALTER TABLE.*
20758 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20759 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
2075a 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 erFunctions(sqli
2075b 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 te3 *db){. sqli
2075c 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 te3CreateFunc(db
2075d 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 , "sqlite_rename
2075e 5f 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49 _table", 2, SQLI
2075f 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 TE_UTF8, 0,.
20760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20761 20 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c 65 renameTable
20762 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66 Func, 0, 0);.#if
20763 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
20764 5f 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69 74 _TRIGGER. sqlit
20765 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c e3CreateFunc(db,
20766 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f "sqlite_rename_
20767 74 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51 4c trigger", 2, SQL
20768 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 ITE_UTF8, 0,.
20769 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2076a 20 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69 67 renameTrig
2076b 67 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a gerFunc, 0, 0);.
2076c 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
2076d 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 Generate the tex
2076e 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 t of a WHERE exp
2076f 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 ression which ca
20770 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c n be used to sel
20771 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f ect all.** tempo
20772 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e rary triggers on
20773 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d table pTab from
20774 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 the sqlite_temp
20775 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 _master table. I
20776 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 f.** table pTab
20777 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 has no temporary
20778 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 triggers, or is
20779 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 itself stored i
2077a 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 n the .** tempor
2077b 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 ary database, NU
2077c 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a LL is returned..
2077d 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a */.static char *
2077e 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 whereTempTrigger
2077f 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c s(Parse *pParse,
20780 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 Table *pTab){.
20781 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b Trigger *pTrig;
20782 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 . char *zWhere
20783 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70 = 0;. char *tmp
20784 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 = 0;. const Sc
20785 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d hema *pTempSchem
20786 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e a = pParse->db->
20787 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 aDb[1].pSchema;
20788 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d /* Temp db schem
20789 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 a */.. /* If th
2078a 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c e table is not l
2078b 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 ocated in the te
2078c 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20 mp-db (in which
2078d 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 case NULL is .
2078e 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f ** returned, loo
2078f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 p through the ta
20790 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69 bles list of tri
20791 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20 ggers. For each
20792 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 trigger. ** tha
20793 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 t is not part of
20794 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68 the temp-db sch
20795 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73 ema, add a claus
20796 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a e to the WHERE .
20797 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
20798 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69 being built up i
20799 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 n zWhere.. */.
2079a 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 if( pTab->pSche
2079b 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 ma!=pTempSchema
2079c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a ){. sqlite3 *
2079d 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2079e 0a 20 20 20 20 66 6f 72 28 70 54 72 69 67 3d 73 . for(pTrig=s
2079f 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 qlite3TriggerLis
207a0 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b t(pParse, pTab);
207a1 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 pTrig; pTrig=pT
207a2 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 rig->pNext){.
207a3 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53 if( pTrig->pS
207a4 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 chema==pTempSche
207a5 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ma ){. if
207a6 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 ( !zWhere ){.
207a7 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 zWhere =
207a8 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
207a9 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 54 b, "name=%Q", pT
207aa 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 rig->name);.
207ab 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
207ac 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 72 tmp = zWher
207ad 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 68 e;. zWh
207ae 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ere = sqlite3MPr
207af 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 20 intf(db, "%s OR
207b0 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 65 name=%Q", zWhere
207b1 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a , pTrig->name);.
207b2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
207b3 33 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70 29 3DbFree(db, tmp)
207b4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
207b5 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
207b6 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d return zWhere;.}
207b7 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
207b8 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 6e code to drop an
207b9 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 d reload the int
207ba 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
207bb 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a tion of table.**
207bc 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 64 pTab from the d
207bd 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 atabase, includi
207be 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 ng triggers and
207bf 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 temporary trigge
207c0 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 rs..** Argument
207c1 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d zName is the nam
207c2 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 e of the table i
207c3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 n the database s
207c4 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 chema at.** the
207c5 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 74 time the generat
207c6 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 75 ed code is execu
207c7 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 ted. This can be
207c8 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a different from.
207c9 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 ** pTab->zName i
207ca 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 f this function
207cb 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 is being called
207cc 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 20 to code part of
207cd 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 an .** "ALTER TA
207ce 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 BLE RENAME TO" s
207cf 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 tatement..*/.sta
207d0 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 tic void reloadT
207d1 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65 ableSchema(Parse
207d2 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 *pParse, Table
207d3 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 *pTab, const cha
207d4 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 r *zName){. Vdb
207d5 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 e *v;. char *zW
207d6 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b here;. int iDb;
207d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
207d8 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 /* Index of d
207d9 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
207da 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 ng pTab */.#ifnd
207db 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
207dc 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 72 RIGGER. Trigger
207dd 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a *pTrig;.#endif.
207de 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
207df 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
207e0 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 if( NEVER(v==0)
207e1 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
207e2 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
207e3 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 eHoldsAllMutexes
207e4 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a (pParse->db) );.
207e5 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
207e6 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 chemaToIndex(pPa
207e7 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 rse->db, pTab->p
207e8 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 Schema);. asser
207e9 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 t( iDb>=0 );..#i
207ea 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
207eb 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 T_TRIGGER. /* D
207ec 72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 rop any table tr
207ed 69 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 iggers from the
207ee 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e internal schema.
207ef 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d */. for(pTrig=
207f0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 sqlite3TriggerLi
207f1 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 st(pParse, pTab)
207f2 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 ; pTrig; pTrig=p
207f3 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 Trig->pNext){.
207f4 20 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20 int iTrigDb =
207f5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
207f6 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c ndex(pParse->db,
207f7 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 pTrig->pSchema)
207f8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54 ;. assert( iT
207f9 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 rigDb==iDb || iT
207fa 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 rigDb==1 );.
207fb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
207fc 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 4(v, OP_DropTrig
207fd 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c ger, iTrigDb, 0,
207fe 20 30 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 0, pTrig->name,
207ff 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0);. }.#endif.
20800 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 . /* Drop the t
20801 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66 able and index f
20802 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c rom the internal
20803 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c schema */. sql
20804 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
20805 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 , OP_DropTable,
20806 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d iDb, 0, 0, pTab-
20807 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f >zName, 0);.. /
20808 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62 * Reload the tab
20809 6c 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65 le, index and pe
2080a 72 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20 rmanent trigger
2080b 73 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57 schemas. */. zW
2080c 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 here = sqlite3MP
2080d 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 rintf(pParse->db
2080e 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c , "tbl_name=%Q",
2080f 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 zName);. if( !
20810 7a 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b zWhere ) return;
20811 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
20812 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 dOp4(v, OP_Parse
20813 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 Schema, iDb, 0,
20814 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 0, zWhere, P4_DY
20815 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66 NAMIC);..#ifndef
20816 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 SQLITE_OMIT_TRI
20817 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69 GGER. /* Now, i
20818 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e f the table is n
20819 6f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ot stored in the
2081a 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20 temp database,
2081b 72 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20 reload any temp
2081c 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 . ** triggers.
2081d 44 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e Don't use IN(...
2081e 29 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45 ) in case SQLITE
2081f 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69 _OMIT_SUBQUERY i
20820 73 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f s defined. . */
20821 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 . if( (zWhere=w
20822 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 hereTempTriggers
20823 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 (pParse, pTab))!
20824 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
20825 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
20826 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31 P_ParseSchema, 1
20827 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 , 0, 0, zWhere,
20828 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d P4_DYNAMIC);. }
20829 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
2082a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
2082b 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
2082c 22 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78 "ALTER TABLE xxx
2082d 20 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 RENAME TO yyy"
2082e 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f .** command. .*/
2082f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20830 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
20831 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 rRenameTable(.
20832 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
20833 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
20834 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a ser context. */.
20835 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c SrcList *pSrc,
20836 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
20837 68 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 he table to rena
20838 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a me. */. Token *
20839 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 pName
2083a 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 /* The new ta
2083b 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a ble name. */.){.
2083c 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
2083d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
2083e 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e atabase that con
2083f 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 tains the table
20840 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 */. char *zDb;
20841 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20842 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
20843 73 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c se iDb */. Tabl
20844 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
20845 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 /* Table b
20846 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a eing renamed */.
20847 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 char *zName =
20848 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 0; /* N
20849 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 ULL-terminated v
2084a 65 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 ersion of pName
2084b 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */ . sqlite3 *d
2084c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 b = pParse->db;
2084d 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
2084e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ection */. int
2084f 6e 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 nTabName;
20850 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
20851 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 of UTF-8 charact
20852 65 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 ers in zTabName
20853 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
20854 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f *zTabName; /
20855 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 * Original name
20856 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a of the table */.
20857 20 20 56 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 Vdbe *v;.#ifnd
20858 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
20859 52 49 47 47 45 52 0a 20 20 63 68 61 72 20 2a 7a RIGGER. char *z
2085a 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20 Where = 0;
2085b 20 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 /* Where clau
2085c 73 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d se to locate tem
2085d 70 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 p triggers */.#e
2085e 6e 64 69 66 0a 20 20 69 6e 74 20 69 73 56 69 72 ndif. int isVir
2085f 74 75 61 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 tualRename = 0;
20860 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
20861 20 69 73 20 61 20 76 2d 74 61 62 6c 65 20 77 69 is a v-table wi
20862 74 68 20 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 th an xRename()
20863 2a 2f 0a 20 20 0a 20 20 69 66 28 20 4e 45 56 45 */. . if( NEVE
20864 52 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c R(db->mallocFail
20865 65 64 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f ed) ) goto exit_
20866 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
20867 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 assert( pSrc->nS
20868 72 63 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 rc==1 );. asser
20869 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
2086a 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 oldsAllMutexes(p
2086b 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 Parse->db) );..
2086c 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c pTab = sqlite3L
2086d 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 ocateTable(pPars
2086e 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d e, 0, pSrc->a[0]
2086f 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b .zName, pSrc->a[
20870 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 0].zDatabase);.
20871 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 if( !pTab ) got
20872 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
20873 62 6c 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c ble;. iDb = sql
20874 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
20875 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 x(pParse->db, pT
20876 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
20877 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 zDb = db->aDb[iD
20878 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 b].zName;.. /*
20879 47 65 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 Get a NULL termi
2087a 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 nated version of
2087b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e the new table n
2087c 61 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 ame. */. zName
2087d 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
2087e 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 mToken(db, pName
2087f 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 );. if( !zName
20880 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 ) goto exit_rena
20881 6d 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 me_table;.. /*
20882 43 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62 Check that a tab
20883 6c 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 le or index name
20884 64 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e d 'zName' does n
20885 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
20886 0a 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 . ** in databas
20887 65 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 e iDb. If so, th
20888 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a is is an error..
20889 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
2088a 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
2088b 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 zName, zDb) || s
2088c 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
2088d 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 db, zName, zDb)
2088e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
2088f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a rorMsg(pParse, .
20890 20 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69 "there i
20891 73 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 s already anothe
20892 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 r table or index
20893 20 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a with this name:
20894 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 %s", zName);.
20895 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 goto exit_rena
20896 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 me_table;. }..
20897 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74 /* Make sure it
20898 20 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d is not a system
20899 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 table being alt
2089a 65 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72 ered, or a reser
2089b 76 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 ved name. ** th
2089c 61 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 at the table is
2089d 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f being renamed to
2089e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c .. */. if( sql
2089f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 ite3Strlen30(pTa
208a0 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20 0a 20 20 20 b->zName)>6 .
208a1 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 && 0==sqlite3Str
208a2 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d NICmp(pTab->zNam
208a3 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 e, "sqlite_", 7)
208a4 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 . ){. sqlite
208a5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
208a6 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 , "table %s may
208a7 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c not be altered",
208a8 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
208a9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e goto exit_ren
208aa 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 ame_table;. }.
208ab 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
208ac 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 sqlite3CheckObje
208ad 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a ctName(pParse, z
208ae 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 Name) ){. got
208af 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
208b0 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 ble;. }..#ifnde
208b1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
208b2 45 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 EW. if( pTab->p
208b3 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 Select ){. sq
208b4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
208b5 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d arse, "view %s m
208b6 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 ay not be altere
208b7 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 d", pTab->zName)
208b8 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f ;. goto exit_
208b9 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
208ba 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
208bb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
208bc 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a THORIZATION. /*
208bd 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 Invoke the auth
208be 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 orization callba
208bf 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c ck. */. if( sql
208c0 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
208c1 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 arse, SQLITE_ALT
208c2 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 ER_TABLE, zDb, p
208c3 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 Tab->zName, 0) )
208c4 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f {. goto exit_
208c5 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 rename_table;.
208c6 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
208c7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
208c8 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 RTUALTABLE. if(
208c9 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
208ca 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 olumnNames(pPars
208cb 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 e, pTab) ){.
208cc 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 goto exit_rename
208cd 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 _table;. }. if
208ce 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 ( IsVirtual(pTab
208cf 29 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d ) && pTab->pMod-
208d0 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d >pModule->xRenam
208d1 65 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75 e ){. isVirtu
208d2 61 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20 alRename = 1;.
208d3 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 }.#endif.. /* B
208d4 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 egin a transacti
208d5 6f 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 on and code the
208d6 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 VerifyCookie for
208d7 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a database iDb. .
208d8 20 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 ** Then modify
208d9 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b the schema cook
208da 69 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c ie (since the AL
208db 54 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 TER TABLE modifi
208dc 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 es the. ** sche
208dd 6d 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 ma). Open a stat
208de 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
208df 6e 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 n if the table i
208e0 73 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a s a virtual. **
208e1 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 table.. */. v
208e2 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
208e3 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
208e4 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 v==0 ){. got
208e5 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
208e6 62 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ble;. }. sqlit
208e7 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
208e8 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73 ation(pParse, is
208e9 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69 VirtualRename, i
208ea 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 Db);. sqlite3Ch
208eb 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 angeCookie(pPars
208ec 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 e, iDb);.. /* I
208ed 66 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74 f this is a virt
208ee 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b ual table, invok
208ef 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 e the xRename()
208f0 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a function if. **
208f1 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e one is defined.
208f2 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 The xRename() c
208f3 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 allback will mod
208f4 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 ify the names.
208f5 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 ** of any resour
208f6 63 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 ces used by the
208f7 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e v-table implemen
208f8 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e tation (includin
208f9 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c g other. ** SQL
208fa 69 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 ite tables) that
208fb 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 are identified
208fc 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 by the name of t
208fd 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
208fe 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .. */.#ifndef S
208ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
20900 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 ALTABLE. if( is
20901 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b VirtualRename ){
20902 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 . int i = ++p
20903 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
20904 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20905 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 p4(v, OP_String8
20906 2c 20 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 , 0, i, 0, zName
20907 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
20908 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
20909 50 5f 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c P_VRename, i, 0,
2090a 20 30 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 0,(const char*)
2090b 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f pTab->pVtab, P4_
2090c 56 54 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 VTAB);. }.#endi
2090d 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f f.. /* figure o
2090e 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d ut how many UTF-
2090f 38 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 8 characters are
20910 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a in zName */. z
20911 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e TabName = pTab->
20912 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d zName;. nTabNam
20913 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 e = sqlite3Utf8C
20914 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c harLen(zTabName,
20915 20 2d 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 -1);.. /* Modi
20916 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 fy the sqlite_ma
20917 73 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 ster table to us
20918 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 e the new table
20919 6e 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 name. */. sqlit
2091a 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 e3NestedParse(pP
2091b 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 arse,. "UPD
2091c 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a ATE %Q.%s SET ".
2091d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
2091e 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 IT_TRIGGER.
2091f 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 "sql = sqli
20920 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 te_rename_table(
20921 73 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 sql, %Q), ".#els
20922 65 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c e. "sql
20923 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 = CASE ".
20924 20 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 "WHEN type
20925 20 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45 = 'trigger' THE
20926 4e 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f N sqlite_rename_
20927 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 trigger(sql, %Q)
20928 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 ". "E
20929 4c 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d LSE sqlite_renam
2092a 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 e_table(sql, %Q)
2092b 20 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 END, ".#endif.
2092c 20 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 "tbl_na
2092d 6d 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 me = %Q, ".
2092e 20 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 "name = CAS
2092f 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 E ".
20930 22 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c "WHEN type='tabl
20931 65 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 e' THEN %Q ".
20932 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e "WHEN n
20933 61 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 ame LIKE 'sqlite
20934 5f 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e _autoindex%%' AN
20935 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 D type='index' T
20936 48 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 HEN ".
20937 20 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f "'sqlite_auto
20938 69 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c index_' || %Q ||
20939 20 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b substr(name,%d+
2093a 31 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 18) ".
2093b 20 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 "ELSE name END
2093c 20 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 ". "WHERE
2093d 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 tbl_name=%Q AND
2093e 22 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79 ". "(ty
2093f 70 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 pe='table' OR ty
20940 70 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 pe='index' OR ty
20941 70 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c pe='trigger');",
20942 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 . zDb, SCH
20943 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 EMA_TABLE(iDb),
20944 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e zName, zName, zN
20945 61 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 ame, .#ifndef SQ
20946 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
20947 52 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 R. zName,.#
20948 65 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d endif. zNam
20949 65 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 e, nTabName, zTa
2094a 62 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e bName. );..#ifn
2094b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
2094c 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
2094d 2f 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 /* If the sqlite
2094e 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
2094f 65 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 exists in this d
20950 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 atabase, then up
20951 64 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 date . ** it wi
20952 74 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 th the new table
20953 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 name.. */. if
20954 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 ( sqlite3FindTab
20955 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 le(db, "sqlite_s
20956 65 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 equence", zDb) )
20957 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 {. sqlite3Nes
20958 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
20959 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 . "UPDATE
2095a 20 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 \"%w\".sqlite_s
2095b 65 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 equence set name
2095c 20 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 = %Q WHERE name
2095d 20 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 = %Q",.
2095e 7a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 zDb, zName, pTab
2095f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 ->zName);. }.#e
20960 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
20961 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
20962 52 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 R. /* If there
20963 61 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 are TEMP trigger
20964 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c s on this table,
20965 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 modify the sqli
20966 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 te_temp_master.
20967 20 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 ** table. Don't
20968 20 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 do this if the
20969 74 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 table being ALTE
2096a 52 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f Red is itself lo
2096b 63 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 cated in. ** th
2096c 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e e temp database.
2096d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 . */. if( (zWh
2096e 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 ere=whereTempTri
2096f 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 ggers(pParse, pT
20970 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 ab))!=0 ){. s
20971 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
20972 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 e(pParse, .
20973 20 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 "UPDATE sqlit
20974 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 e_temp_master SE
20975 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 T ".
20976 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 "sql = sqlite_re
20977 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c name_trigger(sql
20978 2c 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 , %Q), ".
20979 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d "tbl_name =
2097a 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 %Q ".
2097b 20 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a "WHERE %s;", z
2097c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 Name, zName, zWh
2097d 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ere);. sqlite
2097e 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 3DbFree(db, zWhe
2097f 72 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a re);. }.#endif.
20980 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 . /* Drop and r
20981 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e eload the intern
20982 61 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e al table schema.
20983 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c */. reloadTabl
20984 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 eSchema(pParse,
20985 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 pTab, zName);..e
20986 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
20987 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 :. sqlite3SrcLi
20988 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 stDelete(db, pSr
20989 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 c);. sqlite3DbF
2098a 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a ree(db, zName);.
2098b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 }.../*.** This f
2098c 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
2098d 64 20 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 d after an "ALTE
2098e 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 R TABLE ... ADD"
2098f 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 statement.** ha
20990 73 20 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 s been parsed. A
20991 72 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 rgument pColDef
20992 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 contains the tex
20993 74 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 t of the new.**
20994 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f column definitio
20995 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 n..**.** The Tab
20996 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 50 61 le structure pPa
20997 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 rse->pNewTable w
20998 61 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 as extended to i
20999 6e 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 nclude.** the ne
2099a 77 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 w column during
2099b 70 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 parsing..*/.SQLI
2099c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2099d 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 sqlite3AlterFini
2099e 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 shAddColumn(Pars
2099f 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e e *pParse, Token
209a0 20 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 *pColDef){. Ta
209a1 62 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 ble *pNew;
209a2 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
209a3 6f 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 of pParse->pNewT
209a4 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 able */. Table
209a5 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 *pTab;
209a6 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 /* Table bei
209a7 6e 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 ng altered */.
209a8 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
209a9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
209aa 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a abase number */.
209ab 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
209ac 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 b; /* D
209ad 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a atabase name */.
209ae 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
209af 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ab; /* T
209b0 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 able name */. c
209b1 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 har *zCol;
209b2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c /* Null
209b3 2d 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 -terminated colu
209b4 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f mn definition */
209b5 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b . Column *pCol;
209b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
209b7 54 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a The new column *
209b8 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b /. Expr *pDflt;
209b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
209ba 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 Default value f
209bb 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d or the new colum
209bc 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
209bd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 db;
209be 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
209bf 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a connection; */.
209c0 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e . db = pParse->
209c1 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 db;. if( pParse
209c2 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 ->nErr || db->ma
209c3 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 llocFailed ) ret
209c4 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 urn;. pNew = pP
209c5 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
209c6 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 . assert( pNew
209c7 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 );.. assert( sq
209c8 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
209c9 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b llMutexes(db) );
209ca 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 . iDb = sqlite3
209cb 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
209cc 2c 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 , pNew->pSchema)
209cd 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 ;. zDb = db->aD
209ce 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 b[iDb].zName;.
209cf 7a 54 61 62 20 3d 20 26 70 4e 65 77 2d 3e 7a 4e zTab = &pNew->zN
209d0 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a 20 53 6b 69 ame[16]; /* Ski
209d1 70 20 74 68 65 20 22 73 71 6c 69 74 65 5f 61 6c p the "sqlite_al
209d2 74 65 72 74 61 62 5f 22 20 70 72 65 66 69 78 20 tertab_" prefix
209d3 6f 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 on the name */.
209d4 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 pCol = &pNew->a
209d5 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 Col[pNew->nCol-1
209d6 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 70 43 6f ];. pDflt = pCo
209d7 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 54 61 62 l->pDflt;. pTab
209d8 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 = sqlite3FindTa
209d9 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 7a 44 ble(db, zTab, zD
209da 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 b);. assert( pT
209db 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 ab );..#ifndef S
209dc 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
209dd 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e RIZATION. /* In
209de 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 voke the authori
209df 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e zation callback.
209e0 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
209e1 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
209e2 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f e, SQLITE_ALTER_
209e3 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 TABLE, zDb, pTab
209e4 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 ->zName, 0) ){.
209e5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 return;. }.#
209e6 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 endif.. /* If t
209e7 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
209e8 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c for the new col
209e9 75 6d 6e 20 77 61 73 20 73 70 65 63 69 66 69 65 umn was specifie
209ea 64 20 77 69 74 68 20 61 20 0a 20 20 2a 2a 20 6c d with a . ** l
209eb 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 74 68 65 iteral NULL, the
209ec 6e 20 73 65 74 20 70 44 66 6c 74 20 74 6f 20 30 n set pDflt to 0
209ed 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 66 69 65 . This simplifie
209ee 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 2a 2a 20 s checking. **
209ef 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 for an SQL NULL
209f0 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 2e 0a 20 default below..
209f1 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20 */. if( pDflt
209f2 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b && pDflt->op==TK
209f3 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 44 66 _NULL ){. pDf
209f4 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f lt = 0;. }.. /
209f5 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
209f6 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e new column is n
209f7 6f 74 20 73 70 65 63 69 66 69 65 64 20 61 73 20 ot specified as
209f8 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 PRIMARY KEY or U
209f9 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49 66 20 74 NIQUE.. ** If t
209fa 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 here is a NOT NU
209fb 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 74 LL constraint, t
209fc 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 hen the default
209fd 76 61 6c 75 65 20 66 6f 72 20 74 68 65 0a 20 20 value for the.
209fe 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e ** column must n
209ff 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f ot be NULL.. */
20a00 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 73 50 . if( pCol->isP
20a01 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 rimKey ){. sq
20a02 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20a03 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 arse, "Cannot ad
20a04 64 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 d a PRIMARY KEY
20a05 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 column");. re
20a06 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 turn;. }. if(
20a07 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20 29 7b 0a pNew->pIndex ){.
20a08 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
20a09 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e Msg(pParse, "Can
20a0a 6e 6f 74 20 61 64 64 20 61 20 55 4e 49 51 55 45 not add a UNIQUE
20a0b 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72 column");. r
20a0c 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 eturn;. }. if(
20a0d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 pCol->notNull &
20a0e 26 20 21 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 & !pDflt ){.
20a0f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20a10 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
20a11 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 4e "Cannot add a N
20a12 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 OT NULL column w
20a13 69 74 68 20 64 65 66 61 75 6c 74 20 76 61 6c 75 ith default valu
20a14 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65 e NULL");. re
20a15 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 turn;. }.. /*
20a16 45 6e 73 75 72 65 20 74 68 65 20 64 65 66 61 75 Ensure the defau
20a17 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 lt expression is
20a18 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 something that
20a19 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d sqlite3ValueFrom
20a1a 45 78 70 72 28 29 0a 20 20 2a 2a 20 63 61 6e 20 Expr(). ** can
20a1b 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 handle (i.e. not
20a1c 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 65 74 CURRENT_TIME et
20a1d 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 c.). */. if( p
20a1e 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 Dflt ){. sqli
20a1f 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b te3_value *pVal;
20a20 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
20a21 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 ValueFromExpr(db
20a22 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49 54 45 5f , pDflt, SQLITE_
20a23 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 41 46 46 UTF8, SQLITE_AFF
20a24 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c 29 20 29 7b _NONE, &pVal) ){
20a25 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f . db->mallo
20a26 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
20a27 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
20a28 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c 20 29 . if( !pVal )
20a29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
20a2a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
20a2b 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f "Cannot add a co
20a2c 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f lumn with non-co
20a2d 6e 73 74 61 6e 74 20 64 65 66 61 75 6c 74 22 29 nstant default")
20a2e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
20a2f 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
20a30 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3ValueFree(pVal)
20a31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 ;. }.. /* Modi
20a32 66 79 20 74 68 65 20 43 52 45 41 54 45 20 54 41 fy the CREATE TA
20a33 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a BLE statement. *
20a34 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 /. zCol = sqlit
20a35 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 e3DbStrNDup(db,
20a36 28 63 68 61 72 2a 29 70 43 6f 6c 44 65 66 2d 3e (char*)pColDef->
20a37 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a z, pColDef->n);.
20a38 20 20 69 66 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 if( zCol ){.
20a39 20 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 char *zEnd = &
20a3a 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d zCol[pColDef->n-
20a3b 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 1];. while( z
20a3c 45 6e 64 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a 45 End>zCol && (*zE
20a3d 6e 64 3d 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74 nd==';' || sqlit
20a3e 65 33 49 73 73 70 61 63 65 28 2a 7a 45 6e 64 29 e3Isspace(*zEnd)
20a3f 29 20 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 ) ){. *zEnd
20a40 2d 2d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d -- = '\0';. }
20a41 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 . sqlite3Nest
20a42 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 edParse(pParse,
20a43 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 . "UPDATE
20a44 20 5c 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22 \"%w\".%s SET "
20a45 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 . "sql
20a46 3d 20 73 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 = substr(sql,1,%
20a47 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 d) || ', ' || %Q
20a48 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 25 || substr(sql,%
20a49 64 29 20 22 0a 20 20 20 20 20 20 20 20 22 57 48 d) ". "WH
20a4a 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c ERE type = 'tabl
20a4b 65 27 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 e' AND name = %Q
20a4c 22 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 ", . zDb, S
20a4d 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 CHEMA_TABLE(iDb)
20a4e 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 , pNew->addColOf
20a4f 66 73 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 fset, zCol, pNew
20a50 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 ->addColOffset+1
20a51 2c 0a 20 20 20 20 20 20 7a 54 61 62 0a 20 20 20 ,. zTab.
20a52 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 );. sqlite3D
20a53 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b bFree(db, zCol);
20a54 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
20a55 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 e default value
20a56 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d of the new colum
20a57 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 n is NULL, then
20a58 73 65 74 20 74 68 65 20 66 69 6c 65 0a 20 20 2a set the file. *
20a59 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 * format to 2. I
20a5a 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 f the default va
20a5b 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 lue of the new c
20a5c 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c olumn is not NUL
20a5d 4c 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 L,. ** the file
20a5e 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 format becomes
20a5f 33 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 3.. */. sqlite
20a60 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 3MinimumFileForm
20a61 61 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 at(pParse, iDb,
20a62 70 44 66 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a pDflt ? 3 : 2);.
20a63 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 . /* Reload the
20a64 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d schema of the m
20a65 6f 64 69 66 69 65 64 20 74 61 62 6c 65 2e 20 2a odified table. *
20a66 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 /. reloadTableS
20a67 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 chema(pParse, pT
20a68 61 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 ab, pTab->zName)
20a69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
20a6a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
20a6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 ed by the parser
20a6c 20 61 66 74 65 72 20 74 68 65 20 74 61 62 6c 65 after the table
20a6d 2d 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 -name in.** an "
20a6e 41 4c 54 45 52 20 54 41 42 4c 45 20 3c 74 61 62 ALTER TABLE <tab
20a6f 6c 65 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 le-name> ADD" st
20a70 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 atement is parse
20a71 64 2e 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 d. Argument .**
20a72 70 53 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c pSrc is the full
20a73 2d 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 -name of the tab
20a74 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 le being altered
20a75 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
20a76 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61 tine makes a (pa
20a77 72 74 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 rtial) copy of t
20a78 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
20a79 72 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 re.** for the ta
20a7a 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 ble being altere
20a7b 64 20 61 6e 64 20 73 65 74 73 20 50 61 72 73 65 d and sets Parse
20a7c 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f 20 70 6f .pNewTable to po
20a7d 69 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f int.** to it. Ro
20a7e 75 74 69 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 utines called by
20a7f 20 74 68 65 20 70 61 72 73 65 72 20 61 73 20 74 the parser as t
20a80 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 he column defini
20a81 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 tion.** is parse
20a82 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 d (i.e. sqlite3A
20a83 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 ddColumn()) add
20a84 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 the new Column d
20a85 61 74 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 ata to .** the c
20a86 6f 70 79 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 opy. The copy of
20a87 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 the Table struc
20a88 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 20 ture is deleted
20a89 62 79 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a by tokenize.c .*
20a8a 2a 20 61 66 74 65 72 20 70 61 72 73 69 6e 67 20 * after parsing
20a8b 69 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a is finished..**.
20a8c 2a 2a 20 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 ** Routine sqlit
20a8d 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 e3AlterFinishAdd
20a8e 43 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 Column() will be
20a8f 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c called to compl
20a90 65 74 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 ete.** coding th
20a91 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e e "ALTER TABLE .
20a92 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e .. ADD" statemen
20a93 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
20a94 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20a95 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 3AlterBeginAddCo
20a96 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 lumn(Parse *pPar
20a97 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 se, SrcList *pSr
20a98 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 c){. Table *pNe
20a99 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 w;. Table *pTab
20a9a 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 ;. Vdbe *v;. i
20a9b 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b nt iDb;. int i;
20a9c 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 . int nAlloc;.
20a9d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
20a9e 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a Parse->db;.. /*
20a9f 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 Look up the tab
20aa0 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 le being altered
20aa1 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 . */. assert( p
20aa2 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
20aa3 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
20aa4 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
20aa5 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 dsAllMutexes(db)
20aa6 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
20aa7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
20aa8 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
20aa9 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 _column;. pTab
20aaa 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 = sqlite3LocateT
20aab 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 able(pParse, 0,
20aac 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 pSrc->a[0].zName
20aad 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 , pSrc->a[0].zDa
20aae 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 tabase);. if( !
20aaf 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 pTab ) goto exit
20ab0 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d _begin_add_colum
20ab1 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n;..#ifndef SQLI
20ab2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
20ab3 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 ABLE. if( IsVir
20ab4 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 tual(pTab) ){.
20ab5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20ab6 67 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 g(pParse, "virtu
20ab7 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f al tables may no
20ab8 74 20 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a t be altered");.
20ab9 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 goto exit_be
20aba 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a gin_add_column;.
20abb 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
20abc 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 Make sure this
20abd 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 is not an attemp
20abe 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65 t to ALTER a vie
20abf 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 w. */. if( pTab
20ac0 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 ->pSelect ){.
20ac1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
20ac2 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 (pParse, "Cannot
20ac3 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f add a column to
20ac4 20 61 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67 a view");. g
20ac5 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 oto exit_begin_a
20ac6 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a dd_column;. }..
20ac7 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
20ac8 61 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 addColOffset>0 )
20ac9 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 ;. iDb = sqlite
20aca 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
20acb 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
20acc 29 3b 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 );.. /* Put a c
20acd 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 opy of the Table
20ace 20 73 74 72 75 63 74 20 69 6e 20 50 61 72 73 65 struct in Parse
20acf 2e 70 4e 65 77 54 61 62 6c 65 20 66 6f 72 20 74 .pNewTable for t
20ad0 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 he. ** sqlite3A
20ad1 64 64 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 ddColumn() funct
20ad2 69 6f 6e 20 61 6e 64 20 66 72 69 65 6e 64 73 20 ion and friends
20ad3 74 6f 20 6d 6f 64 69 66 79 2e 20 20 42 75 74 20 to modify. But
20ad4 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 modify. ** the
20ad5 6e 61 6d 65 20 62 79 20 61 64 64 69 6e 67 20 61 name by adding a
20ad6 6e 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 n "sqlite_altert
20ad7 61 62 5f 22 20 70 72 65 66 69 78 2e 20 20 42 79 ab_" prefix. By
20ad8 20 61 64 64 69 6e 67 20 74 68 69 73 0a 20 20 2a adding this. *
20ad9 2a 20 70 72 65 66 69 78 2c 20 77 65 20 69 6e 73 * prefix, we ins
20ada 75 72 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d ure that the nam
20adb 65 20 77 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69 e will not colli
20adc 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 de with an exist
20add 69 6e 67 0a 20 20 2a 2a 20 74 61 62 6c 65 20 62 ing. ** table b
20ade 65 63 61 75 73 65 20 75 73 65 72 20 74 61 62 6c ecause user tabl
20adf 65 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 e are not allowe
20ae0 64 20 74 6f 20 68 61 76 65 20 74 68 65 20 22 73 d to have the "s
20ae1 71 6c 69 74 65 5f 22 0a 20 20 2a 2a 20 70 72 65 qlite_". ** pre
20ae2 66 69 78 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d fix on their nam
20ae3 65 2e 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d e.. */. pNew =
20ae4 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 (Table*)sqlite3
20ae5 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
20ae6 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b sizeof(Table));
20ae7 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 67 . if( !pNew ) g
20ae8 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 oto exit_begin_a
20ae9 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 dd_column;. pPa
20aea 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d rse->pNewTable =
20aeb 20 70 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e pNew;. pNew->n
20aec 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d Ref = 1;. pNew-
20aed 3e 64 62 4d 65 6d 20 3d 20 70 54 61 62 2d 3e 64 >dbMem = pTab->d
20aee 62 4d 65 6d 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43 bMem;. pNew->nC
20aef 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b ol = pTab->nCol;
20af0 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d . assert( pNew-
20af1 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c >nCol>0 );. nAl
20af2 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d 3e 6e loc = (((pNew->n
20af3 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38 3b 0a Col-1)/8)*8)+8;.
20af4 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c 6f 63 assert( nAlloc
20af5 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26 20 >=pNew->nCol &&
20af6 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26 20 6e nAlloc%8==0 && n
20af7 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c Alloc-pNew->nCol
20af8 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 43 <8 );. pNew->aC
20af9 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73 71 ol = (Column*)sq
20afa 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
20afb 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43 6f 6c o(db, sizeof(Col
20afc 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20 umn)*nAlloc);.
20afd 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 pNew->zName = sq
20afe 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
20aff 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 "sqlite_alterta
20b00 62 5f 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 b_%s", pTab->zNa
20b01 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 me);. if( !pNew
20b02 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d ->aCol || !pNew-
20b03 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 64 62 >zName ){. db
20b04 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
20b05 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 1;. goto exi
20b06 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 t_begin_add_colu
20b07 6d 6e 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 mn;. }. memcpy
20b08 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61 (pNew->aCol, pTa
20b09 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28 b->aCol, sizeof(
20b0a 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43 Column)*pNew->nC
20b0b 6f 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ol);. for(i=0;
20b0c 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b i<pNew->nCol; i+
20b0d 2b 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a +){. Column *
20b0e 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 pCol = &pNew->aC
20b0f 6f 6c 5b 69 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d ol[i];. pCol-
20b10 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 >zName = sqlite3
20b11 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f DbStrDup(db, pCo
20b12 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 l->zName);. p
20b13 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a Col->zColl = 0;.
20b14 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 pCol->zType
20b15 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 70 = 0;. pCol->p
20b16 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Dflt = 0;. }.
20b17 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 pNew->pSchema =
20b18 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
20b19 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 hema;. pNew->ad
20b1a 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 dColOffset = pTa
20b1b 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b b->addColOffset;
20b1c 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 . pNew->nRef =
20b1d 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 1;.. /* Begin a
20b1e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
20b1f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 increment the s
20b20 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a chema cookie. *
20b21 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e /. sqlite3Begin
20b22 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 WriteOperation(p
20b23 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a Parse, 0, iDb);.
20b24 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 v = sqlite3Get
20b25 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
20b26 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78 if( !v ) goto ex
20b27 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c it_begin_add_col
20b28 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 umn;. sqlite3Ch
20b29 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 angeCookie(pPars
20b2a 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 e, iDb);..exit_b
20b2b 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a egin_add_column:
20b2c 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 . sqlite3SrcLis
20b2d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 tDelete(db, pSrc
20b2e 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 );. return;.}.#
20b2f 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 endif /* SQLITE
20b30 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a _ALTER_TABLE */.
20b31 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
20b32 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 End of alter.c
20b33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
20b36 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
20b37 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c Begin file anal
20b38 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a yze.c **********
20b39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
20b3b 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 ./*.** 2005 July
20b3c 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 8.**.** The aut
20b3d 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
20b3e 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
20b3f 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
20b40 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
20b41 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
20b42 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
20b43 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
20b44 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
20b45 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
20b46 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
20b47 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
20b48 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
20b49 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
20b4a 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
20b4b 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
20b4c 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
20b4d 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
20b4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b52 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
20b53 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73 ontains code ass
20b54 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
20b55 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 ANALYZE command
20b56 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
20b57 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e : analyze.c,v 1.
20b58 35 32 20 32 30 30 39 2f 30 34 2f 31 36 20 31 37 52 2009/04/16 17
20b59 3a 34 35 3a 34 38 20 64 72 68 20 45 78 70 20 24 :45:48 drh Exp $
20b5a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
20b5b 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a TE_OMIT_ANALYZE.
20b5c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
20b5d 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f ine generates co
20b5e 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68 de that opens th
20b5f 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 e sqlite_stat1 t
20b60 61 62 6c 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a able on cursor.*
20b61 2a 20 69 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a * iStatCur..**.*
20b62 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f * If the sqlite_
20b63 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65 stat1 tables doe
20b64 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 s not previously
20b65 20 65 78 69 73 74 2c 20 69 74 20 69 73 20 63 72 exist, it is cr
20b66 65 61 74 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 eated..** If it
20b67 64 6f 65 73 20 70 72 65 76 69 6f 75 73 6c 79 20 does previously
20b68 65 78 69 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72 exist, all entir
20b69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
20b6a 74 68 20 74 61 62 6c 65 20 7a 57 68 65 72 65 0a th table zWhere.
20b6b 2a 2a 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 20 ** are removed.
20b6c 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 20 74 68 If zWhere==0 th
20b6d 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 en all entries a
20b6e 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 re removed..*/.s
20b6f 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 tatic void openS
20b70 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 tatTable(. Pars
20b71 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
20b72 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
20b73 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
20b74 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 iDb,
20b75 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
20b76 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 ase we are looki
20b77 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 ng in */. int i
20b78 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20 20 StatCur,
20b79 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 /* Open the s
20b7a 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
20b7b 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 e on this cursor
20b7c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
20b7d 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a *zWhere /*
20b7e 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20 Delete entries
20b7f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
20b80 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b this table */.){
20b81 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20b82 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 pParse->db;. D
20b83 62 20 2a 70 44 62 3b 0a 20 20 69 6e 74 20 69 52 b *pDb;. int iR
20b84 6f 6f 74 50 61 67 65 3b 0a 20 20 75 38 20 63 72 ootPage;. u8 cr
20b85 65 61 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 eateStat1 = 0;.
20b86 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 Table *pStat;.
20b87 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
20b88 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
20b89 29 3b 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 );.. if( v==0 )
20b8a 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 return;. asser
20b8b 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
20b8c 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 oldsAllMutexes(d
20b8d 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 b) );. assert(
20b8e 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 sqlite3VdbeDb(v)
20b8f 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 ==db );. pDb =
20b90 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 &db->aDb[iDb];.
20b91 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 if( (pStat = sq
20b92 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 lite3FindTable(d
20b93 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 b, "sqlite_stat1
20b94 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d ", pDb->zName))=
20b95 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =0 ){. /* The
20b96 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
20b97 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 bles does not ex
20b98 69 73 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e ist. Create it.
20b99 20 20 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 . ** Note t
20b9a 68 61 74 20 61 20 73 69 64 65 2d 65 66 66 65 63 hat a side-effec
20b9b 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 t of the CREATE
20b9c 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 TABLE statement
20b9d 69 73 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 is to leave.
20b9e 2a 2a 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 ** the rootpage
20b9f 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
20ba0 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 in register pPa
20ba1 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 rse->regRoot. T
20ba2 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d his is. ** im
20ba3 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 portant because
20ba4 74 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 the OpenWrite op
20ba5 63 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 code below will
20ba6 62 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a be needing it. *
20ba7 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 /. sqlite3Nes
20ba8 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
20ba9 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54 . "CREATE T
20baa 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 ABLE %Q.sqlite_s
20bab 74 61 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 tat1(tbl,idx,sta
20bac 74 29 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e t)",. pDb->
20bad 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 zName. );.
20bae 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 iRootPage = pPa
20baf 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 rse->regRoot;.
20bb0 20 20 63 72 65 61 74 65 53 74 61 74 31 20 3d 20 createStat1 =
20bb1 31 3b 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 1; /* Cause roo
20bb2 74 70 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65 tpage to be take
20bb3 6e 20 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 n from top of st
20bb4 61 63 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 ack */. }else i
20bb5 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 f( zWhere ){.
20bb6 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 /* The sqlite_s
20bb7 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 tat1 table exist
20bb8 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 s. Delete all e
20bb9 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 ntries associate
20bba 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 d with. ** th
20bbb 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 e table zWhere.
20bbc 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 */. sqlite3Ne
20bbd 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 stedParse(pParse
20bbe 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 ,. "DELETE
20bbf 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f FROM %Q.sqlite_
20bc0 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d stat1 WHERE tbl=
20bc1 25 51 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d %Q",. pDb-
20bc2 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 >zName, zWhere.
20bc3 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 );. iRootP
20bc4 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 age = pStat->tnu
20bc5 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 m;. }else{.
20bc6 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 /* The sqlite_st
20bc7 61 74 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64 at1 table alread
20bc8 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 y exists. Delet
20bc9 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 e all rows. */.
20bca 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 iRootPage = p
20bcb 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 Stat->tnum;.
20bcc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20bcd 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 2(v, OP_Clear, p
20bce 53 74 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 Stat->tnum, iDb)
20bcf 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e ;. }.. /* Open
20bd0 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
20bd1 31 20 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74 1 table for writ
20bd2 69 6e 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 ing. Unless it w
20bd3 61 73 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 as created. **
20bd4 62 79 20 74 68 69 73 20 76 64 62 65 20 70 72 6f by this vdbe pro
20bd5 67 72 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f gram, lock it fo
20bd6 72 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 65 r writing at the
20bd7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 shared-cache le
20bd8 76 65 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 vel. . ** If th
20bd9 69 73 20 76 64 62 65 20 64 69 64 20 63 72 65 61 is vdbe did crea
20bda 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 te the sqlite_st
20bdb 61 74 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 at1 table, then
20bdc 69 74 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 it must have .
20bdd 2a 2a 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69 ** already obtai
20bde 6e 65 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 ned a schema-loc
20bdf 6b 2c 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 k, making the wr
20be0 69 74 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 ite-lock redunda
20be1 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 nt.. */. if( !
20be2 63 72 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20 createStat1 ){.
20be3 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c sqlite3TableL
20be4 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c ock(pParse, iDb,
20be5 20 69 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 iRootPage, 1, "
20be6 73 71 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a sqlite_stat1");.
20be7 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
20be8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 eAddOp3(v, OP_Op
20be9 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 enWrite, iStatCu
20bea 72 2c 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 r, iRootPage, iD
20beb 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 b);. sqlite3Vdb
20bec 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c eChangeP4(v, -1,
20bed 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49 (char *)3, P4_I
20bee 4e 54 33 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 NT32);. sqlite3
20bef 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
20bf0 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a 7d 0a createStat1);.}.
20bf1 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
20bf2 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e code to do an an
20bf3 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e alysis of all in
20bf4 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 dices associated
20bf5 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c with.** a singl
20bf6 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 e table..*/.stat
20bf7 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f ic void analyzeO
20bf8 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 neTable(. Parse
20bf9 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 *pParse, /* P
20bfa 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f arser context */
20bfb 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 . Table *pTab,
20bfc 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f /* Table who
20bfd 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74 se indices are t
20bfe 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f o be analyzed */
20bff 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c . int iStatCur,
20c00 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
20c01 56 64 62 65 43 75 72 73 6f 72 20 74 68 61 74 20 VdbeCursor that
20c02 77 72 69 74 65 73 20 74 68 65 20 73 71 6c 69 74 writes the sqlit
20c03 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f e_stat1 table */
20c04 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 . int iMem
20c05 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 /* Available
20c06 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
20c07 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a s begin here */.
20c08 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 ){. Index *pIdx
20c09 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 ; /* An inde
20c0a 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79 x to being analy
20c0b 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 zed */. int iId
20c0c 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49 6e 64 xCur; /* Ind
20c0d 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72 ex of VdbeCursor
20c0e 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 6e 67 for index being
20c0f 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 analyzed */. i
20c10 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt nCol;
20c11 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c /* Number of col
20c12 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 umns in the inde
20c13 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 x */. Vdbe *v;
20c14 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 /* The v
20c15 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 irtual machine b
20c16 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f eing built up */
20c17 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
20c18 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
20c19 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 ter */. int top
20c1a 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 OfLoop; /* The
20c1b 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 top of the loop
20c1c 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c */. int endOfL
20c1d 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 65 6e oop; /* The en
20c1e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f d of the loop */
20c1f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 . int addr;
20c20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65 /* The addre
20c21 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63 ss of an instruc
20c22 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 tion */. int iD
20c23 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e b; /* In
20c24 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 dex of database
20c25 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 containing pTab
20c26 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */.. v = sqlite
20c27 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
20c28 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20 ;. if( v==0 ||
20c29 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 7c NEVER(pTab==0) |
20c2a 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d | pTab->pIndex==
20c2b 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 0 ){. /* Do n
20c2c 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f 72 20 74 o analysis for t
20c2d 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65 20 ables that have
20c2e 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 no indices */.
20c2f 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
20c30 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
20c31 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
20c32 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 xes(pParse->db)
20c33 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 );. iDb = sqlit
20c34 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
20c35 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 pParse->db, pTab
20c36 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 ->pSchema);. as
20c37 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a sert( iDb>=0 );.
20c38 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
20c39 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
20c3a 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 N. if( sqlite3A
20c3b 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
20c3c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 2c SQLITE_ANALYZE,
20c3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c pTab->zName, 0,
20c3e 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
20c3f 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
20c40 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 e ) ){. retur
20c41 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 n;. }.#endif..
20c42 20 2f 2a 20 45 73 74 61 62 6c 69 73 68 20 61 20 /* Establish a
20c43 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 read-lock on the
20c44 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 73 68 table at the sh
20c45 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c ared-cache level
20c46 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 . */. sqlite3Ta
20c47 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 bleLock(pParse,
20c48 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c iDb, pTab->tnum,
20c49 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 0, pTab->zName)
20c4a 3b 0a 0a 20 20 69 49 64 78 43 75 72 20 3d 20 70 ;.. iIdxCur = p
20c4b 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
20c4c 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e for(pIdx=pTab->
20c4d 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
20c4e 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b dx=pIdx->pNext){
20c4f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b . KeyInfo *pK
20c50 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 ey = sqlite3Inde
20c51 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c xKeyinfo(pParse,
20c52 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e 74 20 pIdx);. int
20c53 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 2f 2a regFields; /*
20c54 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 Register block
20c55 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 65 63 for building rec
20c56 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ords */. int
20c57 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 2f 2a regRec; /*
20c58 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e Register holdin
20c59 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f g completed reco
20c5a 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 rd */. int re
20c5b 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a 20 54 gTemp; /* T
20c5c 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67 emporary use reg
20c5d 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 ister */. int
20c5e 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 20 2f regCol; /
20c5f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 * Content of a c
20c60 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20 74 olumn from the t
20c61 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 6c 79 able being analy
20c62 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 zed */. int r
20c63 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 egRowid; /*
20c64 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69 6e Rowid for the in
20c65 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a 2f serted record */
20c66 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 3b 0a . int regF2;.
20c67 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63 . /* Open a c
20c68 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 6e 64 ursor to the ind
20c69 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 ex to be analyze
20c6a 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 d. */. ass
20c6b 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74 65 ert( iDb==sqlite
20c6c 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 3SchemaToIndex(p
20c6d 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78 2d Parse->db, pIdx-
20c6e 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 20 >pSchema) );.
20c6f 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 nCol = pIdx->nC
20c70 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 olumn;. sqlit
20c71 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
20c72 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 OP_OpenRead, iId
20c73 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d xCur, pIdx->tnum
20c74 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28 , iDb,. (
20c75 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f char *)pKey, P4_
20c76 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 KEYINFO_HANDOFF)
20c77 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e ;. VdbeCommen
20c78 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 t((v, "%s", pIdx
20c79 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 72 ->zName));. r
20c7a 65 67 46 69 65 6c 64 73 20 3d 20 69 4d 65 6d 2b egFields = iMem+
20c7b 6e 43 6f 6c 2a 32 3b 0a 20 20 20 20 72 65 67 54 nCol*2;. regT
20c7c 65 6d 70 20 3d 20 72 65 67 52 6f 77 69 64 20 3d emp = regRowid =
20c7d 20 72 65 67 43 6f 6c 20 3d 20 72 65 67 46 69 65 regCol = regFie
20c7e 6c 64 73 2b 33 3b 0a 20 20 20 20 72 65 67 52 65 lds+3;. regRe
20c7f 63 20 3d 20 72 65 67 43 6f 6c 2b 31 3b 0a 20 20 c = regCol+1;.
20c80 20 20 69 66 28 20 72 65 67 52 65 63 3e 70 50 61 if( regRec>pPa
20c81 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 rse->nMem ){.
20c82 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 pParse->nMem
20c83 3d 20 72 65 67 52 65 63 3b 0a 20 20 20 20 7d 0a = regRec;. }.
20c84 0a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 . /* Memory c
20c85 65 6c 6c 73 20 61 72 65 20 75 73 65 64 20 61 73 ells are used as
20c86 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a follows:. **
20c87 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 . ** mem[i
20c88 4d 65 6d 5d 3a 20 20 20 20 20 20 20 20 20 20 20 Mem]:
20c89 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 The total numb
20c8a 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 er of rows in th
20c8b 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 e table.. **
20c8c 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 31 5d 3a 20 mem[iMem+1]:
20c8d 20 20 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 Number
20c8e 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c of distinct val
20c8f 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 31 0a ues in column 1.
20c90 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 ** ....
20c91 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d ** mem[iMem
20c92 2b 6e 43 6f 6c 5d 3a 20 20 20 20 20 20 20 20 4e +nCol]: N
20c93 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 umber of distinc
20c94 74 20 76 61 6c 75 65 73 20 69 6e 20 63 6f 6c 75 t values in colu
20c95 6d 6e 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 6d mn N. ** m
20c96 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 5d 20 em[iMem+nCol+1]
20c97 20 20 20 20 20 20 4c 61 73 74 20 6f 62 73 65 72 Last obser
20c98 76 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c ved value of col
20c99 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 umn 1. **
20c9a 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 .... ** me
20c9b 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c m[iMem+nCol+nCol
20c9c 5d 3a 20 20 20 4c 61 73 74 20 6f 62 73 65 72 76 ]: Last observ
20c9d 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 ed value of colu
20c9e 6d 6e 20 4e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 mn N. **.
20c9f 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20 74 68 ** Cells iMem th
20ca0 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c 20 rough iMem+nCol
20ca1 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 are initialized
20ca2 74 6f 20 30 2e 20 20 54 68 65 20 6f 74 68 65 72 to 0. The other
20ca3 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 69 s. ** are ini
20ca4 74 69 61 6c 69 7a 65 64 20 74 6f 20 4e 55 4c 4c tialized to NULL
20ca5 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
20ca6 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69 (i=0; i<=nCol; i
20ca7 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
20ca8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20ca9 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 OP_Integer, 0, i
20caa 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 Mem+i);. }.
20cab 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f for(i=0; i<nCo
20cac 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 l; i++){. s
20cad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20cae 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
20caf 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a iMem+nCol+i+1);.
20cb0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f }.. /* Do
20cb1 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 0a 20 the analysis..
20cb2 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c */. endOfL
20cb3 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 oop = sqlite3Vdb
20cb4 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
20cb5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20cb6 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e dOp2(v, OP_Rewin
20cb7 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f d, iIdxCur, endO
20cb8 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f fLoop);. topO
20cb9 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 fLoop = sqlite3V
20cba 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
20cbb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
20cbc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 beAddOp2(v, OP_A
20cbd 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b ddImm, iMem, 1);
20cbe 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
20cbf 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
20cc0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20cc1 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
20cc2 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65 , iIdxCur, i, re
20cc3 67 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c gCol);. sql
20cc4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
20cc5 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c , OP_Ne, regCol,
20cc6 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 0, iMem+nCol+i+
20cc7 31 29 3b 0a 20 20 20 20 20 20 2f 2a 2a 2a 2a 20 1);. /****
20cc8 54 4f 44 4f 3a 20 20 61 64 64 20 63 6f 6c 6c 61 TODO: add colla
20cc9 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2a ting sequence **
20cca 2a 2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 ***/. sqlit
20ccb 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
20ccc 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e , SQLITE_JUMPIFN
20ccd 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ULL);. }.
20cce 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20ccf 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 2(v, OP_Goto, 0,
20cd0 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 endOfLoop);.
20cd1 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c for(i=0; i<nCol
20cd2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
20cd3 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
20cd4 65 28 76 2c 20 74 6f 70 4f 66 4c 6f 6f 70 20 2b e(v, topOfLoop +
20cd5 20 32 2a 28 69 20 2b 20 31 29 29 3b 0a 20 20 20 2*(i + 1));.
20cd6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20cd7 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
20cd8 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b m, iMem+i+1, 1);
20cd9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
20cda 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 beAddOp3(v, OP_C
20cdb 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 olumn, iIdxCur,
20cdc 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 i, iMem+nCol+i+1
20cdd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
20cde 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
20cdf 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f abel(v, endOfLoo
20ce0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 p);. sqlite3V
20ce1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
20ce2 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74 Next, iIdxCur, t
20ce3 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 opOfLoop);. s
20ce4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
20ce5 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 (v, OP_Close, iI
20ce6 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 dxCur);.. /*
20ce7 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
20ce8 73 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 s. . **.
20ce9 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 ** The result is
20cea 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 a single row of
20ceb 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
20cec 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69 1 table. The fi
20ced 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 rst. ** two c
20cee 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e olumns are the n
20cef 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c ames of the tabl
20cf0 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 e and index. Th
20cf1 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20 e third column.
20cf2 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e ** is a strin
20cf3 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 g composed of a
20cf4 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20 list of integer
20cf5 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74 statistics about
20cf6 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 the. ** inde
20cf7 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e x. The first in
20cf8 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 teger in the lis
20cf9 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e t is the total n
20cfa 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
20cfb 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 . ** in the i
20cfc 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 20 ndex. There is
20cfd 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 one additional i
20cfe 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 nteger in the li
20cff 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20 st for each.
20d00 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 ** column of the
20d01 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64 table. This ad
20d02 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 ditional integer
20d03 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20 68 is a guess of h
20d04 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72 ow many. ** r
20d05 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ows of the table
20d06 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 the index will
20d07 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73 select. If D is
20d08 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69 the count of di
20d09 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61 stinct. ** va
20d0a 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68 lues and K is th
20d0b 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
20d0c 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65 f rows, then the
20d0d 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70 integer is comp
20d0e 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a uted. ** as:.
20d0f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
20d10 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29 I = (K+D-1)
20d11 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a /D. **. **
20d12 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f If K==0 then no
20d13 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 entry is made i
20d14 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 nto the sqlite_s
20d15 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20 tat1 table. .
20d16 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e ** If K>0 then
20d17 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74 68 it is always th
20d18 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20 73 e case the D>0 s
20d19 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 o division by ze
20d1a 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76 ro. ** is nev
20d1b 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 er possible..
20d1c 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73 */. addr = s
20d1d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
20d1e 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d (v, OP_IfNot, iM
20d1f 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 em);. sqlite3
20d20 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
20d21 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 _String8, 0, reg
20d22 46 69 65 6c 64 73 2c 20 30 2c 20 70 54 61 62 2d Fields, 0, pTab-
20d23 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 >zName, 0);.
20d24 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20d25 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
20d26 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2b 31 2c 0, regFields+1,
20d27 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 0, pIdx->zName,
20d28 20 30 29 3b 0a 20 20 20 20 72 65 67 46 32 20 3d 0);. regF2 =
20d29 20 72 65 67 46 69 65 6c 64 73 2b 32 3b 0a 20 20 regFields+2;.
20d2a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20d2b 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c Op2(v, OP_SCopy,
20d2c 20 69 4d 65 6d 2c 20 72 65 67 46 32 29 3b 0a 20 iMem, regF2);.
20d2d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
20d2e 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; i++){.
20d2f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20d30 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
20d31 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c 20 0, regTemp, 0,
20d32 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 " ", 0);. s
20d33 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
20d34 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 (v, OP_Concat, r
20d35 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 72 egTemp, regF2, r
20d36 65 67 46 32 29 3b 0a 20 20 20 20 20 20 73 71 6c egF2);. sql
20d37 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
20d38 2c 20 4f 50 5f 41 64 64 2c 20 69 4d 65 6d 2c 20 , OP_Add, iMem,
20d39 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d iMem+i+1, regTem
20d3a 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
20d3b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
20d3c 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d P_AddImm, regTem
20d3d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 p, -1);. sq
20d3e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
20d3f 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 69 4d v, OP_Divide, iM
20d40 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d 70 2c em+i+1, regTemp,
20d41 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 regTemp);.
20d42 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20d43 70 31 28 76 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20 p1(v, OP_ToInt,
20d44 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 regTemp);.
20d45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20d46 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 3(v, OP_Concat,
20d47 72 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 regTemp, regF2,
20d48 72 65 67 46 32 29 3b 0a 20 20 20 20 7d 0a 20 20 regF2);. }.
20d49 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20d4a 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 Op4(v, OP_MakeRe
20d4b 63 6f 72 64 2c 20 72 65 67 46 69 65 6c 64 73 2c cord, regFields,
20d4c 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61 61 3, regRec, "aaa
20d4d 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 ", 0);. sqlit
20d4e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20d4f 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 OP_NewRowid, iSt
20d50 61 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29 atCur, regRowid)
20d51 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
20d52 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
20d53 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20 sert, iStatCur,
20d54 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 regRec, regRowid
20d55 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
20d56 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 beChangeP5(v, OP
20d57 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 FLAG_APPEND);.
20d58 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
20d59 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a pHere(v, addr);.
20d5a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
20d5b 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
20d5c 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d will cause the m
20d5d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65 78 ost recent index
20d5e 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a 20 analysis to.**
20d5f 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20 69 be laoded into i
20d60 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 nternal hash tab
20d61 6c 65 73 20 77 68 65 72 65 20 69 73 20 63 61 6e les where is can
20d62 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 be used..*/.sta
20d63 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e 61 tic void loadAna
20d64 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70 50 61 lysis(Parse *pPa
20d65 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 rse, int iDb){.
20d66 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
20d67 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
20d68 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 );. if( v ){.
20d69 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20d6a 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e Op1(v, OP_LoadAn
20d6b 61 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20 20 alysis, iDb);.
20d6c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
20d6d 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 ate code that wi
20d6e 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 ll do an analysi
20d6f 73 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20 64 s of an entire d
20d70 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69 atabase.*/.stati
20d71 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44 61 c void analyzeDa
20d72 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50 tabase(Parse *pP
20d73 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a arse, int iDb){.
20d74 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20d75 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53 63 pParse->db;. Sc
20d76 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 hema *pSchema =
20d77 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
20d78 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68 65 hema; /* Sche
20d79 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 ma of database i
20d7a 44 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d Db */. HashElem
20d7b 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61 74 *k;. int iStat
20d7c 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d 3b Cur;. int iMem;
20d7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e .. sqlite3Begin
20d7e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 WriteOperation(p
20d7f 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a Parse, 0, iDb);.
20d80 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 61 iStatCur = pPa
20d81 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f rse->nTab++;. o
20d82 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61 penStatTable(pPa
20d83 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43 rse, iDb, iStatC
20d84 75 72 2c 20 30 29 3b 0a 20 20 69 4d 65 6d 20 3d ur, 0);. iMem =
20d85 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b pParse->nMem+1;
20d86 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 . for(k=sqliteH
20d87 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d ashFirst(&pSchem
20d88 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b 3b 20 a->tblHash); k;
20d89 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 k=sqliteHashNext
20d8a 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 (k)){. Table
20d8b 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29 *pTab = (Table*)
20d8c 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b sqliteHashData(k
20d8d 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65 4f 6e );. analyzeOn
20d8e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 eTable(pParse, p
20d8f 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 69 Tab, iStatCur, i
20d90 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64 Mem);. }. load
20d91 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c Analysis(pParse,
20d92 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iDb);.}../*.**
20d93 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 Generate code th
20d94 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e at will do an an
20d95 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69 6e 67 alysis of a sing
20d96 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61 le table in.** a
20d97 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 database..*/.st
20d98 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a atic void analyz
20d99 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 eTable(Parse *pP
20d9a 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 arse, Table *pTa
20d9b 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 b){. int iDb;.
20d9c 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 0a int iStatCur;..
20d9d 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d assert( pTab!=
20d9e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
20d9f 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
20da0 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 AllMutexes(pPars
20da1 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20 e->db) );. iDb
20da2 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
20da3 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 oIndex(pParse->d
20da4 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
20da5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 );. sqlite3Begi
20da6 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
20da7 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b pParse, 0, iDb);
20da8 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 . iStatCur = pP
20da9 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 arse->nTab++;.
20daa 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 openStatTable(pP
20dab 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 arse, iDb, iStat
20dac 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 Cur, pTab->zName
20dad 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 );. analyzeOneT
20dae 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 able(pParse, pTa
20daf 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50 61 b, iStatCur, pPa
20db0 72 73 65 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20 rse->nMem+1);.
20db1 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61 loadAnalysis(pPa
20db2 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a rse, iDb);.}../*
20db3 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
20db4 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a e for the ANALYZ
20db5 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20 E command. The
20db6 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 parser calls thi
20db7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65 s routine.** whe
20db8 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20 n it recognizes
20db9 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 an ANALYZE comma
20dba 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 nd..**.**
20dbb 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 ANALYZE
20dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20dbd 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 -- 1.**
20dbe 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 ANALYZE <dat
20dbf 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20 abase>
20dc0 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 -- 2.**
20dc1 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c ANALYZE ?<
20dc2 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c database>.?<tabl
20dc3 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a ename> -- 3.**.
20dc4 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73 ** Form 1 causes
20dc5 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 all indices in
20dc6 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 all attached dat
20dc7 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61 abases to be ana
20dc8 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 lyzed..** Form 2
20dc9 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e analyzes all in
20dca 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65 dices the single
20dcb 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e database named.
20dcc 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79 .** Form 3 analy
20dcd 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 zes all indices
20dce 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
20dcf 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e the named table.
20dd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20dd1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
20dd2 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50 nalyze(Parse *pP
20dd3 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 arse, Token *pNa
20dd4 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d me1, Token *pNam
20dd5 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a e2){. sqlite3 *
20dd6 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
20dd7 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e . int iDb;. in
20dd8 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 t i;. char *z,
20dd9 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 *zDb;. Table *p
20dda 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 Tab;. Token *pT
20ddb 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 ableName;.. /*
20ddc 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 Read the databas
20ddd 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 e schema. If an
20dde 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 error occurs, le
20ddf 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
20de0 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f sage. ** and co
20de1 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 de in pParse and
20de2 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f return NULL. */
20de3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
20de4 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
20de5 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 utexes(pParse->d
20de6 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 b) );. if( SQLI
20de7 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 TE_OK!=sqlite3Re
20de8 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 adSchema(pParse)
20de9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
20dea 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
20deb 4e 61 6d 65 32 21 3d 30 20 7c 7c 20 70 4e 61 6d Name2!=0 || pNam
20dec 65 31 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 e1==0 );. if( p
20ded 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 Name1==0 ){.
20dee 2f 2a 20 46 6f 72 6d 20 31 3a 20 20 41 6e 61 6c /* Form 1: Anal
20def 79 7a 65 20 65 76 65 72 79 74 68 69 6e 67 20 2a yze everything *
20df0 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 /. for(i=0; i
20df1 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
20df2 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 if( i==1 )
20df3 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 44 continue; /* D
20df4 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a 65 20 74 68 o not analyze th
20df5 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 e TEMP database
20df6 2a 2f 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 */. analyze
20df7 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 2c Database(pParse,
20df8 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c i);. }. }el
20df9 73 65 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e se if( pName2->n
20dfa 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f ==0 ){. /* Fo
20dfb 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20 74 rm 2: Analyze t
20dfc 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74 he database or t
20dfd 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20 20 able named */.
20dfe 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 iDb = sqlite3F
20dff 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 indDb(db, pName1
20e00 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d );. if( iDb>=
20e01 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c 79 0 ){. analy
20e02 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72 73 zeDatabase(pPars
20e03 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65 6c e, iDb);. }el
20e04 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 se{. z = sq
20e05 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
20e06 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a en(db, pName1);.
20e07 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 if( z ){.
20e08 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71 pTab = sq
20e09 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
20e0a 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 30 (pParse, 0, z, 0
20e0b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
20e0c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b e3DbFree(db, z);
20e0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61 . if( pTa
20e0e 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 b ){. a
20e0f 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 nalyzeTable(pPar
20e10 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 se, pTab);.
20e11 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
20e12 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
20e13 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c 79 /* Form 3: Analy
20e14 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75 61 ze the fully qua
20e15 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d lified table nam
20e16 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 e */. iDb = s
20e17 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d qlite3TwoPartNam
20e18 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 e(pParse, pName1
20e19 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62 6c , pName2, &pTabl
20e1a 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 eName);. if(
20e1b 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 iDb>=0 ){.
20e1c 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 zDb = db->aDb[iD
20e1d 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 b].zName;.
20e1e 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 z = sqlite3NameF
20e1f 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 romToken(db, pTa
20e20 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 bleName);.
20e21 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 if( z ){.
20e22 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c pTab = sqlite3L
20e23 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 ocateTable(pPars
20e24 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 e, 0, z, zDb);.
20e25 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
20e26 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 Free(db, z);.
20e27 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b if( pTab ){
20e28 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79 . analy
20e29 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 zeTable(pParse,
20e2a 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d pTab);. }
20e2b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 20 . }. }
20e2c 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 . }.}../*.** U
20e2d 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66 6f sed to pass info
20e2e 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 rmation from the
20e2f 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 65 72 analyzer reader
20e30 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 0a through to the.
20e31 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 ** callback rout
20e32 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ine..*/.typedef
20e33 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73 49 struct analysisI
20e34 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f nfo analysisInfo
20e35 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 ;.struct analysi
20e36 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 sInfo {. sqlite
20e37 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 3 *db;. const c
20e38 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b 0a har *zDatabase;.
20e39 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 63 };../*.** This c
20e3a 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
20e3b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
20e3c 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 61 64 index when read
20e3d 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 ing the.** sqlit
20e3e 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20 e_stat1 table.
20e3f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b .**.** argv[
20e40 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68 65 0] = name of the
20e41 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 61 72 index.** ar
20e42 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 73 20 gv[1] = results
20e43 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 6f 6e of analysis - on
20e44 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 61 63 integer for eac
20e45 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 h column.*/.stat
20e46 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 73 4c ic int analysisL
20e47 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 61 74 oader(void *pDat
20e48 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61 a, int argc, cha
20e49 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a r **argv, char *
20e4a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 6e 61 *NotUsed){. ana
20e4b 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e 66 6f lysisInfo *pInfo
20e4c 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e 66 6f = (analysisInfo
20e4d 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 65 78 *)pData;. Index
20e4e 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 *pIndex;. int
20e4f 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 i, c;. unsigned
20e50 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 74 20 int v;. const
20e51 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 char *z;.. asse
20e52 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 rt( argc==2 );.
20e53 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
20e54 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 63 R2(NotUsed, argc
20e55 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 3d 3d );.. if( argv==
20e56 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 0 || argv[0]==0
20e57 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b || argv[1]==0 ){
20e58 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
20e59 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 }. pIndex = sq
20e5a 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70 lite3FindIndex(p
20e5b 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30 Info->db, argv[0
20e5c 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 ], pInfo->zDatab
20e5d 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 ase);. if( pInd
20e5e 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ex==0 ){. ret
20e5f 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d urn 0;. }. z =
20e60 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28 argv[1];. for(
20e61 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49 i=0; *z && i<=pI
20e62 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 ndex->nColumn; i
20e63 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a ++){. v = 0;.
20e64 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b while( (c=z[
20e65 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 0])>='0' && c<='
20e66 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 9' ){. v =
20e67 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a v*10 + c - '0';.
20e68 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d z++;. }
20e69 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 . pIndex->aiR
20e6a 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20 owEst[i] = v;.
20e6b 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20 if( *z==' ' )
20e6c 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 z++;. }. retur
20e6d 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f n 0;.}../*.** Lo
20e6e 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f ad the content o
20e6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 f the sqlite_sta
20e70 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 t1 table into th
20e71 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 e index hash tab
20e72 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
20e73 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
20e74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 e3AnalysisLoad(s
20e75 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
20e76 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 iDb){. analysis
20e77 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 61 Info sInfo;. Ha
20e78 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 61 shElem *i;. cha
20e79 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 72 r *zSql;. int r
20e7a 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 c;.. assert( iD
20e7b 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e b>=0 && iDb<db->
20e7c 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
20e7d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 db->aDb[iDb].pB
20e7e 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t!=0 );. assert
20e7f 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
20e80 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 ldsMutex(db->aDb
20e81 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 [iDb].pBt) );..
20e82 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 72 /* Clear any pr
20e83 69 6f 72 20 73 74 61 74 69 73 74 69 63 73 20 2a ior statistics *
20e84 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 /. for(i=sqlite
20e85 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 HashFirst(&db->a
20e86 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d Db[iDb].pSchema-
20e87 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 71 >idxHash);i;i=sq
20e88 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 liteHashNext(i))
20e89 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 {. Index *pId
20e8a 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 x = sqliteHashDa
20e8b 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 ta(i);. sqlit
20e8c 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 e3DefaultRowEst(
20e8d 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pIdx);. }.. /*
20e8e 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 Check to make s
20e8f 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 ure the sqlite_s
20e90 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 tat1 table exist
20e91 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 ss */. sInfo.db
20e92 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a = db;. sInfo.z
20e93 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61 Database = db->a
20e94 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 Db[iDb].zName;.
20e95 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 if( sqlite3Find
20e96 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 Table(db, "sqlit
20e97 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e e_stat1", sInfo.
20e98 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b zDatabase)==0 ){
20e99 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c . return SQL
20e9a 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a ITE_ERROR;. }..
20e9b 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73 . /* Load new s
20e9c 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f 66 tatistics out of
20e9d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
20e9e 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71 1 table */. zSq
20e9f 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e l = sqlite3MPrin
20ea0 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20 69 tf(db, "SELECT i
20ea1 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51 dx, stat FROM %Q
20ea2 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 0a .sqlite_stat1",.
20ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20ea4 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a 44 sInfo.zD
20ea5 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 atabase);. if(
20ea6 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 zSql==0 ){. r
20ea7 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
20ea8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28 ;. }else{. (
20ea9 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
20eaa 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 tyOff(db);. r
20eab 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 c = sqlite3_exec
20eac 28 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 (db, zSql, analy
20ead 73 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 6e 66 sisLoader, &sInf
20eae 6f 2c 20 30 29 3b 0a 20 20 20 20 28 76 6f 69 64 o, 0);. (void
20eaf 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
20eb0 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (db);. sqlite
20eb1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 3DbFree(db, zSql
20eb2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
20eb3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 QLITE_NOMEM ) db
20eb4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
20eb5 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
20eb6 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 rc;.}...#endif
20eb7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
20eb8 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a NALYZE */../****
20eb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
20eba 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a f analyze.c ****
20ebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ebc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
20ebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
20ebf 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20 2a file attach.c *
20ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
20ec3 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 2003 April 6.**
20ec4 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
20ec5 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
20ec6 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
20ec7 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
20ec8 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
20ec9 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
20eca 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
20ecb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
20ecc 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
20ecd 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
20ece 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
20ecf 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
20ed0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
20ed1 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
20ed2 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
20ed3 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
20ed4 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
20ed5 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
20ed6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
20eda 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
20edb 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 ns code used to
20edc 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41 54 implement the AT
20edd 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48 20 TACH and DETACH
20ede 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 commands..**.**
20edf 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76 20 $Id: attach.c,v
20ee0 31 2e 39 30 20 32 30 30 39 2f 30 35 2f 30 31 20 1.90 2009/05/01
20ee1 30 36 3a 31 39 3a 32 31 20 64 61 6e 69 65 6c 6b 06:19:21 danielk
20ee2 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 1977 Exp $.*/..#
20ee3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20ee4 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20 IT_ATTACH./*.**
20ee5 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 Resolve an expre
20ee6 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 ssion that was p
20ee7 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 art of an ATTACH
20ee8 20 6f 72 20 44 45 54 41 43 48 20 73 74 61 74 65 or DETACH state
20ee9 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 ment. This.** is
20eea 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 slightly differ
20eeb 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69 ent from resolvi
20eec 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20 ng a normal SQL
20eed 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61 expression, beca
20eee 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64 use simple.** id
20eef 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 74 72 entifiers are tr
20ef0 65 61 74 65 64 20 61 73 20 73 74 72 69 6e 67 73 eated as strings
20ef1 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63 , not possible c
20ef2 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61 olumn names or a
20ef3 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e liases..**.** i.
20ef4 65 2e 20 69 66 20 74 68 65 20 70 61 72 73 65 72 e. if the parser
20ef5 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 sees:.**.**
20ef6 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45 ATTACH DATABASE
20ef7 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a abc AS def.**.*
20ef8 2a 20 69 74 20 74 72 65 61 74 73 20 74 68 65 20 * it treats the
20ef9 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 two expressions
20efa 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e as literal strin
20efb 67 73 20 27 61 62 63 27 20 61 6e 64 20 27 64 65 gs 'abc' and 'de
20efc 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a f' instead of.**
20efd 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c looking for col
20efe 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 umns of the same
20eff 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 name..**.** Thi
20f00 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 s only applies t
20f01 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 o the root node
20f02 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65 of pExpr, so the
20f03 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a statement:.**.*
20f04 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 41 54 * ATTACH DAT
20f05 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41 ABASE abc||def A
20f06 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69 S 'db2'.**.** wi
20f07 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 ll fail because
20f08 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 neither abc or d
20f09 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 ef can be resolv
20f0a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
20f0b 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 t resolveAttachE
20f0c 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 xpr(NameContext
20f0d 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45 *pName, Expr *pE
20f0e 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20 xpr).{. int rc
20f0f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
20f10 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 f( pExpr ){.
20f11 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 if( pExpr->op!=T
20f12 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63 K_ID ){. rc
20f13 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 = sqlite3Resolv
20f14 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d 65 eExprNames(pName
20f15 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 , pExpr);.
20f16 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
20f17 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 K && !sqlite3Exp
20f18 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70 rIsConstant(pExp
20f19 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 r) ){. sq
20f1a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e lite3ErrorMsg(pN
20f1b 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e ame->pParse, "in
20f1c 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 54 valid name: \"%T
20f1d 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 \"", &pExpr->spa
20f1e 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 n);. retu
20f1f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
20f20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
20f21 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d se{. pExpr-
20f22 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b >op = TK_STRING;
20f23 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
20f24 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
20f25 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e An SQL user-fun
20f26 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
20f27 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 to do the work
20f28 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 of an ATTACH sta
20f29 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 tement. The.** t
20f2a 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 hree arguments t
20f2b 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 o the function c
20f2c 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f ome directly fro
20f2d 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61 74 m an attach stat
20f2e 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ement:.**.**
20f2f 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45 ATTACH DATABASE
20f30 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a 2a x AS y KEY z.**
20f31 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 .** SELECT s
20f32 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c 20 qlite_attach(x,
20f33 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 y, z).**.** If t
20f34 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45 59 he optional "KEY
20f35 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f 6d z" syntax is om
20f36 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e 55 itted, an SQL NU
20f37 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73 20 LL is passed as
20f38 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 the.** third arg
20f39 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ument..*/.static
20f3a 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e 63 void attachFunc
20f3b 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
20f3c 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
20f3d 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
20f3e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20f3f 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a rgv.){. int i;.
20f40 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 int rc = 0;.
20f41 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
20f42 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
20f43 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
20f44 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
20f45 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 zName;. const c
20f46 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62 har *zFile;. Db
20f47 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a *aNew;. char *
20f48 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 0a 20 20 zErrDyn = 0;..
20f49 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
20f4a 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a 46 (NotUsed);.. zF
20f4b 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 ile = (const cha
20f4c 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 r *)sqlite3_valu
20f4d 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
20f4e 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 . zName = (cons
20f4f 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 t char *)sqlite3
20f50 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
20f51 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c [1]);. if( zFil
20f52 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20 22 e==0 ) zFile = "
20f53 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d ";. if( zName==
20f54 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 0 ) zName = "";.
20f55 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 . /* Check for
20f56 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 the following er
20f57 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rors:. **. **
20f58 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20 61 * Too many a
20f59 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
20f5a 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 72 s,. ** * Tr
20f5b 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e ansaction curren
20f5c 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20 20 tly open. **
20f5d 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 64 61 * Specified da
20f5e 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c 72 65 tabase name alre
20f5f 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 2e 0a ady being used..
20f60 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e */. if( db->n
20f61 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 Db>=db->aLimit[S
20f62 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 QLITE_LIMIT_ATTA
20f63 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20 7a CHED]+2 ){. z
20f64 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 ErrDyn = sqlite3
20f65 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 6f 6f MPrintf(db, "too
20f66 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 many attached d
20f67 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20 25 atabases - max %
20f68 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e 61 d", . db->a
20f69 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
20f6a 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20 20 IT_ATTACHED].
20f6b 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 );. goto att
20f6c 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 ach_error;. }.
20f6d 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f if( !db->autoCo
20f6e 6d 6d 69 74 20 29 7b 0a 20 20 20 20 7a 45 72 72 mmit ){. zErr
20f6f 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Dyn = sqlite3MPr
20f70 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74 intf(db, "cannot
20f71 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65 ATTACH database
20f72 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 within transact
20f73 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 ion");. goto
20f74 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 attach_error;.
20f75 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 }. for(i=0; i<d
20f76 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
20f77 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e char *z = db->
20f78 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 aDb[i].zName;.
20f79 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20 7a assert( z && z
20f7a 4e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28 20 Name );. if(
20f7b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
20f7c 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 , zName)==0 ){.
20f7d 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 zErrDyn = s
20f7e 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
20f7f 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20 69 , "database %s i
20f80 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65 s already in use
20f81 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 ", zName);.
20f82 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 goto attach_err
20f83 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 or;. }. }..
20f84 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 /* Allocate the
20f85 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74 68 new entry in th
20f86 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 e db->aDb[] arra
20f87 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65 y and initialise
20f88 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a the schema. **
20f89 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20 20 hash tables..
20f8a 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 */. if( db->aDb
20f8b 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 ==db->aDbStatic
20f8c 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 ){. aNew = sq
20f8d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
20f8e 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e (db, sizeof(db->
20f8f 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20 20 aDb[0])*3 );.
20f90 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72 if( aNew==0 ) r
20f91 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 eturn;. memcp
20f92 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62 2c y(aNew, db->aDb,
20f93 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b sizeof(db->aDb[
20f94 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0])*2);. }else{
20f95 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 . aNew = sqli
20f96 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c te3DbRealloc(db,
20f97 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 db->aDb, sizeof
20f98 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64 62 (db->aDb[0])*(db
20f99 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20 20 ->nDb+1) );.
20f9a 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72 65 if( aNew==0 ) re
20f9b 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e turn;. }. db->
20f9c 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61 4e aDb = aNew;. aN
20f9d 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62 ew = &db->aDb[db
20f9e 2d 3e 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65 74 ->nDb];. memset
20f9f 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 (aNew, 0, sizeof
20fa0 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a 20 (*aNew));.. /*
20fa1 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 Open the databas
20fa2 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 62 e file. If the b
20fa3 74 72 65 65 20 69 73 20 73 75 63 63 65 73 73 66 tree is successf
20fa4 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73 65 ully opened, use
20fa5 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74 61 . ** it to obta
20fa6 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
20fa7 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73 20 schema. At this
20fa8 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d 61 point the schema
20fa9 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61 79 may. ** or may
20faa 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 not be initiali
20fab 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d sed.. */. rc =
20fac 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 sqlite3BtreeFac
20fad 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 2c 20 tory(db, zFile,
20fae 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 0, SQLITE_DEFAUL
20faf 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 0a 20 20 T_CACHE_SIZE,.
20fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20fb1 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6f 70 65 db->ope
20fb2 6e 46 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f nFlags | SQLITE_
20fb3 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 OPEN_MAIN_DB,.
20fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20fb5 20 20 20 20 20 20 20 20 20 26 61 4e 65 77 2d 3e &aNew->
20fb6 70 42 74 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 2b pBt);. db->nDb+
20fb7 2b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c +;. if( rc==SQL
20fb8 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 ITE_CONSTRAINT )
20fb9 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
20fba 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7a 45 72 E_ERROR;. zEr
20fbb 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 rDyn = sqlite3MP
20fbc 72 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61 62 rintf(db, "datab
20fbd 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ase is already a
20fbe 74 74 61 63 68 65 64 22 29 3b 0a 20 20 7d 65 6c ttached");. }el
20fbf 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 se if( rc==SQLIT
20fc0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 61 67 65 E_OK ){. Page
20fc1 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 61 r *pPager;. a
20fc2 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 New->pSchema = s
20fc3 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 qlite3SchemaGet(
20fc4 64 62 2c 20 61 4e 65 77 2d 3e 70 42 74 29 3b 0a db, aNew->pBt);.
20fc5 20 20 20 20 69 66 28 20 21 61 4e 65 77 2d 3e 70 if( !aNew->p
20fc6 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 Schema ){.
20fc7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
20fc8 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 M;. }else if(
20fc9 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e aNew->pSchema->
20fca 66 69 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20 61 file_format && a
20fcb 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e New->pSchema->en
20fcc 63 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20 20 c!=ENC(db) ){.
20fcd 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 zErrDyn = sq
20fce 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
20fcf 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61 63 . "attac
20fd0 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75 hed databases mu
20fd1 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 st use the same
20fd2 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 text encoding as
20fd3 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 main database")
20fd4 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
20fd5 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d ITE_ERROR;. }
20fd6 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 . pPager = sq
20fd7 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 lite3BtreePager(
20fd8 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 aNew->pBt);.
20fd9 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b sqlite3PagerLock
20fda 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 ingMode(pPager,
20fdb 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 db->dfltLockMode
20fdc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
20fdd 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 gerJournalMode(p
20fde 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4a Pager, db->dfltJ
20fdf 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d ournalMode);. }
20fe0 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d . aNew->zName =
20fe1 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
20fe2 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 61 (db, zName);. a
20fe3 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 New->safety_leve
20fe4 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c 49 l = 3;..#if SQLI
20fe5 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 7b TE_HAS_CODEC. {
20fe6 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 . extern int
20fe7 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 sqlite3CodecAtta
20fe8 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ch(sqlite3*, int
20fe9 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
20fea 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 nt);. extern
20feb 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
20fec 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a cGetKey(sqlite3*
20fed 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 , int, void**, i
20fee 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b nt*);. int nK
20fef 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b ey;. char *zK
20ff0 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 ey;. int t =
20ff1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
20ff2 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 pe(argv[2]);.
20ff3 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20 switch( t ){.
20ff4 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
20ff5 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63 INTEGER:. c
20ff6 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
20ff7 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 79 :. zErrDy
20ff8 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 n = sqlite3DbStr
20ff9 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 Dup(db, "Invalid
20ffa 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 key value");.
20ffb 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
20ffc 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 E_ERROR;.
20ffd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
20ffe 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 . case SQLI
20fff 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63 TE_TEXT:. c
21000 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a ase SQLITE_BLOB:
21001 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 . nKey =
21002 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
21003 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 tes(argv[2]);.
21004 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 zKey = (ch
21005 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c ar *)sqlite3_val
21006 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 ue_blob(argv[2])
21007 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21008 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 3CodecAttach(db,
21009 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 db->nDb-1, zKey
2100a 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 , nKey);.
2100b 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 break;.. c
2100c 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a ase SQLITE_NULL:
2100d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b . /* No k
2100e 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 55 ey specified. U
2100f 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 se the key from
21010 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
21011 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c e */. sql
21012 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 ite3CodecGetKey(
21013 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 db, 0, (void**)&
21014 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 zKey, &nKey);.
21015 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 sqlite3Cod
21016 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d ecAttach(db, db-
21017 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b >nDb-1, zKey, nK
21018 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ey);. bre
21019 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 ak;. }. }.#e
2101a 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ndif.. /* If th
2101b 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 e file was opene
2101c 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 d successfully,
2101d 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 read the schema
2101e 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 for the new data
2101f 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 base.. ** If th
21020 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 20 is fails, or if
21021 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 opening the file
21022 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 6c failed, then cl
21023 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e 64 ose the file and
21024 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68 . ** remove th
21025 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 e entry from the
21026 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79 db->aDb[] array
21027 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72 79 . i.e. put every
21028 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20 77 thing back the w
21029 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e 64 ay. ** we found
2102a 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
2102b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
2102c 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
2102d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a e3SafetyOn(db);.
2102e 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
2102f 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 EnterAll(db);.
21030 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e rc = sqlite3In
21031 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e 29 it(db, &zErrDyn)
21032 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
21033 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
21034 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
21035 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
21036 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a }. if( rc ){.
21037 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64 62 int iDb = db
21038 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20 61 ->nDb - 1;. a
21039 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29 3b ssert( iDb>=2 );
2103a 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 . if( db->aDb
2103b 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20 20 [iDb].pBt ){.
2103c 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
2103d 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44 62 lose(db->aDb[iDb
2103e 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 ].pBt);. db
2103f 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 3d ->aDb[iDb].pBt =
21040 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 0;. db->aD
21041 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 3d b[iDb].pSchema =
21042 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0;. }. sq
21043 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
21044 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b alSchema(db, 0);
21045 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 69 . db->nDb = i
21046 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d Db;. if( rc==
21047 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
21048 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
21049 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 _NOMEM ){.
2104a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
2104b 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 = 1;. sqli
2104c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
2104d 72 72 44 79 6e 29 3b 0a 20 20 20 20 20 20 7a 45 rrDyn);. zE
2104e 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d rrDyn = sqlite3M
2104f 50 72 69 6e 74 66 28 64 62 2c 20 22 6f 75 74 20 Printf(db, "out
21050 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 of memory");.
21051 20 7d 65 6c 73 65 20 69 66 28 20 7a 45 72 72 44 }else if( zErrD
21052 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a yn==0 ){. z
21053 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 ErrDyn = sqlite3
21054 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 61 MPrintf(db, "una
21055 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 ble to open data
21056 62 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 base: %s", zFile
21057 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 );. }. got
21058 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a o attach_error;.
21059 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b }. . return;
2105a 0a 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a ..attach_error:.
2105b 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 /* Return an e
2105c 72 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 rror if we get h
2105d 65 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 ere */. if( zEr
2105e 72 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 rDyn ){. sqli
2105f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
21060 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 (context, zErrDy
21061 6e 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 n, -1);. sqli
21062 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
21063 72 72 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 rrDyn);. }. if
21064 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f 72 ( rc ) sqlite3_r
21065 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
21066 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 7d (context, rc);.}
21067 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 ../*.** An SQL u
21068 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67 ser-function reg
21069 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68 istered to do th
2106a 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54 e work of an DET
2106b 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 ACH statement. T
2106c 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75 he.** three argu
2106d 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e ments to the fun
2106e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63 ction come direc
2106f 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63 tly from a detac
21070 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a h statement:.**.
21071 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44 41 ** DETACH DA
21072 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20 TABASE x.**.**
21073 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 SELECT sqlite
21074 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74 _detach(x).*/.st
21075 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63 68 atic void detach
21076 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
21077 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
21078 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
21079 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
2107a 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e **argv.){. con
2107b 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d st char *zName =
2107c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 (const char *)s
2107d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2107e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 t(argv[0]);. sq
2107f 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
21080 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
21081 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
21082 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20 2a 70 int i;. Db *p
21083 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a Db = 0;. char z
21084 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 4e 55 Err[128];.. UNU
21085 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
21086 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 7a tUsed);.. if( z
21087 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 Name==0 ) zName
21088 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = "";. for(i=0;
21089 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
2108a 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d {. pDb = &db-
2108b 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 >aDb[i];. if(
2108c 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20 63 pDb->pBt==0 ) c
2108d 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 ontinue;. if(
2108e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
2108f 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d pDb->zName, zNam
21090 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 e)==0 ) break;.
21091 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d }.. if( i>=db-
21092 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 >nDb ){. sqli
21093 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
21094 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 eof(zErr),zErr,
21095 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61 73 "no such databas
21096 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a e: %s", zName);.
21097 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f goto detach_
21098 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 error;. }. if(
21099 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 i<2 ){. sqli
2109a 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
2109b 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 eof(zErr),zErr,
2109c 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20 64 "cannot detach d
2109d 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e 61 atabase %s", zNa
2109e 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 me);. goto de
2109f 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
210a0 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 if( !db->autoC
210a1 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c ommit ){. sql
210a2 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
210a3 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 zeof(zErr), zErr
210a4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
210a5 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 44 "cannot D
210a6 45 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77 ETACH database w
210a7 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f ithin transactio
210a8 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 n");. goto de
210a9 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
210aa 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 if( sqlite3Btr
210ab 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 eeIsInReadTrans(
210ac 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73 71 6c pDb->pBt) || sql
210ad 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 ite3BtreeIsInBac
210ae 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20 29 7b kup(pDb->pBt) ){
210af 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
210b0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 rintf(sizeof(zEr
210b1 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 r),zErr, "databa
210b2 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 se %s is locked"
210b3 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f , zName);. go
210b4 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b to detach_error;
210b5 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 . }.. sqlite3B
210b6 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 treeClose(pDb->p
210b7 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 Bt);. pDb->pBt
210b8 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 = 0;. pDb->pSch
210b9 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 ema = 0;. sqlit
210ba 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 e3ResetInternalS
210bb 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 chema(db, 0);.
210bc 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f return;..detach_
210bd 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 error:. sqlite3
210be 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f _result_error(co
210bf 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 ntext, zErr, -1)
210c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
210c1 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 procedure genera
210c2 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f tes VDBE code fo
210c3 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 r a single invoc
210c4 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 ation of either
210c5 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 the.** sqlite_de
210c6 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 tach() or sqlite
210c7 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 _attach() SQL us
210c8 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f er functions..*/
210c9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 .static void cod
210ca 65 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 eAttach(. Parse
210cb 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
210cc 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f /* The parser co
210cd 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 ntext */. int t
210ce 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ype,
210cf 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 /* Either SQLITE
210d0 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 _ATTACH or SQLIT
210d1 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 E_DETACH */. Fu
210d2 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 ncDef *pFunc,
210d3 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 /* FuncDef wr
210d4 61 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 apper for detach
210d5 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 Func() or attach
210d6 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 Func() */. Expr
210d7 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 *pAuthArg,
210d8 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 /* Expression t
210d9 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 o pass to author
210da 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b ization callback
210db 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c */. Expr *pFil
210dc 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 ename, /* Na
210dd 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 me of database f
210de 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ile */. Expr *p
210df 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a Dbname, /*
210e0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
210e1 61 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 abase to use int
210e2 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 ernally */. Exp
210e3 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 r *pKey
210e4 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 /* Database ke
210e5 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e y for encryption
210e6 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b extension */.){
210e7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d . int rc;. Nam
210e8 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a eContext sName;.
210e9 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c Vdbe *v;. sql
210ea 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 ite3* db = pPars
210eb 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 e->db;. int reg
210ec 41 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 Args;..#ifndef S
210ed 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
210ee 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 RIZATION. asser
210ef 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
210f0 6c 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 led || pAuthArg
210f1 29 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 );. if( pAuthAr
210f2 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a g ){. char *z
210f3 41 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 AuthArg = sqlite
210f4 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
210f5 62 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 b, &pAuthArg->sp
210f6 61 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 an);. if( !zA
210f7 75 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 uthArg ){.
210f8 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b goto attach_end;
210f9 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
210fa 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
210fb 28 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a (pParse, type, z
210fc 41 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a AuthArg, 0, 0);.
210fd 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
210fe 65 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b e(db, zAuthArg);
210ff 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 . if(rc!=SQLI
21100 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
21101 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a oto attach_end;.
21102 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
21103 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
21104 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f AUTHORIZATION */
21105 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d .. memset(&sNam
21106 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d e, 0, sizeof(Nam
21107 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e eContext));. sN
21108 61 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 ame.pParse = pPa
21109 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 rse;.. if( .
2110a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 SQLITE_OK!=(r
2110b 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 c = resolveAttac
2110c 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 hExpr(&sName, pF
2110d 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 ilename)) ||.
2110e 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 SQLITE_OK!=(r
2110f 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 c = resolveAttac
21110 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 hExpr(&sName, pD
21111 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 bname)) ||.
21112 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
21113 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 = resolveAttachE
21114 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 xpr(&sName, pKey
21115 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 )). ){. pPar
21116 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 se->nErr++;.
21117 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b goto attach_end;
21118 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 . }.. v = sqli
21119 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
2111a 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 e);. regArgs =
2111b 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 sqlite3GetTempRa
2111c 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a nge(pParse, 4);.
2111d 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
2111e 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e e(pParse, pFilen
2111f 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 ame, regArgs);.
21120 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21121 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 (pParse, pDbname
21122 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 , regArgs+1);.
21123 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
21124 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 pParse, pKey, re
21125 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 gArgs+2);.. ass
21126 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 ert( v || db->ma
21127 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
21128 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c if( v ){. sql
21129 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
2112a 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 , OP_Function, 0
2112b 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e , regArgs+3-pFun
2112c 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 c->nArg, regArgs
2112d 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 +3);. assert(
2112e 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 pFunc->nArg==-1
2112f 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67 || (pFunc->nArg
21130 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e &0xff)==pFunc->n
21131 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 Arg );. sqlit
21132 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
21133 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41 , (u8)(pFunc->nA
21134 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rg));. sqlite
21135 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 3VdbeChangeP4(v,
21136 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75 -1, (char *)pFu
21137 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b nc, P4_FUNCDEF);
21138 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e .. /* Code an
21139 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 OP_Expire. For
2113a 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d an ATTACH statem
2113b 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 ent, set P1 to t
2113c 72 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73 rue (expire this
2113d 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e . ** statemen
2113e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 t only). For DET
2113f 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 ACH, set it to f
21140 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c alse (expire all
21141 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a existing. **
21142 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20 statements)..
21143 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
21144 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
21145 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d _Expire, (type==
21146 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b SQLITE_ATTACH));
21147 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65 . }. .attach_e
21148 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 nd:. sqlite3Exp
21149 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c rDelete(db, pFil
2114a 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 ename);. sqlite
2114b 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
2114c 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 pDbname);. sqli
2114d 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
2114e 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , pKey);.}../*.*
2114f 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 * Called by the
21150 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c parser to compil
21151 65 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65 e a DETACH state
21152 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ment..**.**
21153 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a DETACH pDbname.*
21154 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21155 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 void sqlite3Det
21156 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 ach(Parse *pPars
21157 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 e, Expr *pDbname
21158 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 ){. static Func
21159 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20 Def detach_func
2115a 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 = {. 1,
2115b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 /* nAr
2115c 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f g */. SQLITE_
2115d 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 UTF8, /* iP
2115e 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c refEnc */. 0,
2115f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21160 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 /* flags */.
21161 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
21162 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a /* pUserData *
21163 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
21164 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
21165 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75 */. detachFu
21166 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 nc, /* xFu
21167 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 nc */. 0,
21168 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
21169 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Step */. 0,
2116a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2116b 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 xFinalize */.
2116c 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 "sqlite_detach
2116d 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a ", /* zName */.
2116e 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
2116f 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a /* pHash *
21170 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 /. };. codeAtt
21171 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 ach(pParse, SQLI
21172 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 TE_DETACH, &deta
21173 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 ch_func, pDbname
21174 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 , 0, 0, pDbname)
21175 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 ;.}../*.** Calle
21176 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
21177 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 to compile an AT
21178 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a TACH statement..
21179 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 **.** ATTACH
2117a 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 p AS pDbname KE
2117b 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 Y pKey.*/.SQLITE
2117c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
2117d 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 lite3Attach(Pars
2117e 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
2117f 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d *p, Expr *pDbnam
21180 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a e, Expr *pKey){.
21181 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 static FuncDef
21182 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b attach_func = {
21183 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20 . 3,
21184 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a /* nArg *
21185 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 /. SQLITE_UTF
21186 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 8, /* iPref
21187 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 Enc */. 0,
21188 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21189 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 flags */. 0,
2118a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2118b 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 * pUserData */.
2118c 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
2118d 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f /* pNext */
2118e 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c . attachFunc,
2118f 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 /* xFunc
21190 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
21191 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 /* xSte
21192 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 p */. 0,
21193 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 /* xF
21194 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 inalize */. "
21195 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 sqlite_attach",
21196 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
21197 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
21198 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 /* pHash */.
21199 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 };. codeAttach
2119a 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f (pParse, SQLITE_
2119b 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f ATTACH, &attach_
2119c 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e func, p, p, pDbn
2119d 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 ame, pKey);.}.#e
2119e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
2119f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f MIT_ATTACH */../
211a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
211a1 61 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 a DbFixer struct
211a2 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ure. This routi
211a3 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 ne must be calle
211a4 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 d prior.** to pa
211a5 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 ssing the struct
211a6 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 ure to one of th
211a7 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 e sqliteFixAAAA(
211a8 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 ) routines below
211a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
211aa 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 rn value indicat
211ab 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f es whether or no
211ac 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 t fixation is re
211ad 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a quired. TRUE.**
211ae 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 means we do nee
211af 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 d to fix the dat
211b0 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 abase references
211b1 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 , FALSE means we
211b2 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 do not..*/.SQLI
211b3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
211b4 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 qlite3FixInit(.
211b5 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 DbFixer *pFix,
211b6 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 /* The fixe
211b7 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 r to be initiali
211b8 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a zed */. Parse *
211b9 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 pParse, /*
211ba 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 Error messages w
211bb 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 ill be written h
211bc 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 ere */. int iDb
211bd 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
211be 54 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 This is the data
211bf 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 base that must b
211c0 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 e used */. cons
211c1 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 t char *zType,
211c2 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 /* "view", "trig
211c3 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 ger", or "index"
211c4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 */. const Toke
211c5 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d n *pName /* Nam
211c6 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 e of the view, t
211c7 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 rigger, or index
211c8 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
211c9 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 *db;.. if( NEV
211ca 45 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44 62 ER(iDb<0) || iDb
211cb 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==1 ) return 0;.
211cc 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
211cd 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d b;. assert( db-
211ce 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46 >nDb>iDb );. pF
211cf 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 ix->pParse = pPa
211d0 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62 rse;. pFix->zDb
211d1 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
211d2 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a zName;. pFix->z
211d3 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20 Type = zType;.
211d4 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e pFix->pName = pN
211d5 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b ame;. return 1;
211d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
211d7 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 llowing set of r
211d8 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72 outines walk thr
211d9 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74 ough the parse t
211da 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a ree and assign.*
211db 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 * a specific dat
211dc 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62 abase to all tab
211dd 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 le references wh
211de 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ere the database
211df 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66 name.** was lef
211e0 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e t unspecified in
211e1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
211e2 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 L statement. Th
211e3 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65 e pFix structure
211e4 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 .** must have be
211e5 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 en initialized b
211e6 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 y a prior call t
211e7 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 o sqlite3FixInit
211e8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ()..**.** These
211e9 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
211ea 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
211eb 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72 hat an index, tr
211ec 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 igger, or.** vie
211ed 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 w in one databas
211ee 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72 e does not refer
211ef 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61 to objects in a
211f0 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 different datab
211f1 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69 ase..** (Excepti
211f2 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69 on: indices, tri
211f3 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73 ggers, and views
211f4 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74 in the TEMP dat
211f5 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c abase are.** all
211f6 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f owed to refer to
211f7 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20 anything.) If
211f8 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65 a reference is e
211f9 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a xplicitly made.*
211fa 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69 * to an object i
211fb 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 n a different da
211fc 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72 tabase, an error
211fd 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65 message is adde
211fe 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e d to.** pParse->
211ff 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73 zErrMsg and thes
21200 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
21201 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 n non-zero. If
21202 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68 everything.** ch
21203 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20 ecks out, these
21204 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
21205 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
21206 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21207 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62 FixSrcList(. Db
21208 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 Fixer *pFix,
21209 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 /* Context of
2120a 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f the fixation */
2120b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 . SrcList *pLis
2120c 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 t /* The S
2120d 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68 ource list to ch
2120e 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a eck and modify *
2120f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
21210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
21211 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
21212 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
21213 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 if( NEVER(pLis
21214 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 t==0) ) return 0
21215 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 3e ;. zDb = pFix->
21216 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 zDb;. for(i=0,
21217 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 pItem=pList->a;
21218 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 i<pList->nSrc; i
21219 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
2121a 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 if( pItem->zDa
2121b 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 20 tabase==0 ){.
2121c 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 pItem->zDatab
2121d 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ase = sqlite3DbS
2121e 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 72 trDup(pFix->pPar
2121f 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 20 se->db, zDb);.
21220 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 }else if( sqli
21221 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d te3StrICmp(pItem
21222 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29 ->zDatabase,zDb)
21223 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=0 ){. sql
21224 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69 ite3ErrorMsg(pFi
21225 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20 x->pParse,.
21226 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f "%s %T canno
21227 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65 t reference obje
21228 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 cts in database
21229 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46 %s",. pF
2122a 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d ix->zType, pFix-
2122b 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a >pName, pItem->z
2122c 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 Database);.
2122d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
2122e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
2122f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
21230 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
21231 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
21232 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
21233 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 FixSelect(pFix,
21234 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 pItem->pSelect)
21235 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
21236 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
21237 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e pr(pFix, pItem->
21238 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b pOn) ) return 1;
21239 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 .#endif. }. re
2123a 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64 turn 0;.}.#if !d
2123b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
2123c 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 IT_VIEW) || !def
2123d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
2123e 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45 _TRIGGER).SQLITE
2123f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
21240 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20 ite3FixSelect(.
21241 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 DbFixer *pFix,
21242 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
21243 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e of the fixation
21244 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
21245 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68 elect /* Th
21246 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
21247 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 nt to be fixed t
21248 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a o one database *
21249 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 /.){. while( pS
2124a 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28 elect ){. if(
2124b 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c sqlite3FixExprL
2124c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 ist(pFix, pSelec
2124d 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20 t->pEList) ){.
2124e 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
2124f 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
21250 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46 te3FixSrcList(pF
21251 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 ix, pSelect->pSr
21252 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 c) ){. retu
21253 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
21254 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
21255 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 pr(pFix, pSelect
21256 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 ->pWhere) ){.
21257 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
21258 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
21259 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 e3FixExpr(pFix,
2125a 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67 pSelect->pHaving
2125b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
2125c 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 n 1;. }. p
2125d 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 Select = pSelect
2125e 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 ->pPrior;. }.
2125f 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 return 0;.}.SQLI
21260 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
21261 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a 20 qlite3FixExpr(.
21262 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 DbFixer *pFix,
21263 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f /* Context o
21264 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a f the fixation *
21265 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 /. Expr *pExpr
21266 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 /* The ex
21267 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 pression to be f
21268 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 ixed to one data
21269 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 base */.){. whi
2126a 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 le( pExpr ){.
2126b 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 if( ExprHasAnyP
2126c 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
2126d 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 P_TokenOnly|EP_S
2126e 70 61 6e 54 6f 6b 65 6e 29 20 29 20 62 72 65 61 panToken) ) brea
2126f 6b 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 k;. if( ExprH
21270 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
21271 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
21272 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
21273 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 ite3FixSelect(pF
21274 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 ix, pExpr->x.pSe
21275 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 lect) ) return 1
21276 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
21277 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
21278 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 xExprList(pFix,
21279 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 20 pExpr->x.pList)
2127a 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
2127b 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
2127c 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 3FixExpr(pFix, p
2127d 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b Expr->pRight) ){
2127e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
2127f 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 . }. pExpr
21280 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
21281 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
21282 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
21283 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
21284 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69 ExprList(. DbFi
21285 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f xer *pFix, /
21286 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
21287 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 fixation */. E
21288 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 xprList *pList
21289 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
2128a 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 ion to be fixed
2128b 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 to one database
2128c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 */.){. int i;.
2128d 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
2128e 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
2128f 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 if( pList==0 ) r
21290 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 eturn 0;. for(i
21291 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d =0, pItem=pList-
21292 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 >a; i<pList->nEx
21293 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b pr; i++, pItem++
21294 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
21295 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 e3FixExpr(pFix,
21296 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b pItem->pExpr) ){
21297 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
21298 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
21299 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
2129a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2129b 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c OMIT_TRIGGER.SQL
2129c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
2129d 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 sqlite3FixTrigge
2129e 72 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72 rStep(. DbFixer
2129f 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 *pFix, /* C
212a0 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 ontext of the fi
212a1 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 xation */. Trig
212a2 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f gerStep *pStep /
212a3 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74 * The trigger st
212a4 65 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f ep be fixed to o
212a5 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 ne database */.)
212a6 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 {. while( pStep
212a7 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
212a8 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 te3FixSelect(pFi
212a9 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 x, pStep->pSelec
212aa 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 t) ){. retu
212ab 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
212ac 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
212ad 70 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e pr(pFix, pStep->
212ae 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20 pWhere) ){.
212af 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
212b0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
212b1 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 FixExprList(pFix
212b2 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 , pStep->pExprLi
212b3 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 st) ){. ret
212b4 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
212b5 20 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e pStep = pStep->
212b6 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 pNext;. }. ret
212b7 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
212b8 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
212b9 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63 End of attach.c
212ba 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
212bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
212bd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
212be 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68 Begin file auth
212bf 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
212c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
212c2 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75 ./*.** 2003 Janu
212c3 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 11.**.** The
212c4 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
212c5 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
212c6 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
212c7 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
212c8 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
212c9 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
212ca 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
212cb 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
212cc 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
212cd 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
212ce 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
212cf 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
212d0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
212d1 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
212d2 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
212d3 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
212d4 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
212d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212d9 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
212da 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
212db 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
212dc 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 nt the sqlite3_s
212dd 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a et_authorizer().
212de 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61 ** API. This fa
212df 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74 cility is an opt
212e0 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66 ional feature of
212e1 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 the library. E
212e2 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65 mbedded.** syste
212e3 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e ms that do not n
212e4 65 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74 eed this facilit
212e5 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79 y may omit it by
212e6 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 recompiling.**
212e7 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68 the library with
212e8 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 -DSQLITE_OMIT_A
212e9 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a UTHORIZATION=1.*
212ea 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68 2e 63 *.** $Id: auth.c
212eb 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 35 2f ,v 1.31 2009/05/
212ec 30 34 20 31 38 3a 30 31 3a 34 30 20 64 72 68 20 04 18:01:40 drh
212ed 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
212ee 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20 All of the code
212ef 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79 in this file may
212f0 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64 be omitted by d
212f1 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65 efining a single
212f2 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 .** macro..*/.#i
212f3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
212f4 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
212f5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 6c ./*.** Set or cl
212f6 65 61 72 20 74 68 65 20 61 63 63 65 73 73 20 61 ear the access a
212f7 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e uthorization fun
212f8 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
212f9 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a access authoriz
212fa 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69 ation function i
212fb 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72 69 s be called duri
212fc 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 ng the compilati
212fd 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20 76 on.** phase to v
212fe 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 75 erify that the u
212ff 73 65 72 20 68 61 73 20 72 65 61 64 20 61 6e 64 ser has read and
21300 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65 73 73 /or write access
21301 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a permission on.*
21302 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64 73 * various fields
21303 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
21304 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
21305 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75 74 ument to the aut
21306 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 h function.** is
21307 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33 a copy of the 3
21308 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rd argument to t
21309 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 his routine. Th
2130a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
2130b 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68 t.** to the auth
2130c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65 function is one
2130d 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61 of these consta
2130e 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 nts:.**.**
2130f 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 SQLITE_CREATE_I
21310 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 NDEX.** SQ
21311 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c LITE_CREATE_TABL
21312 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 E.** SQLIT
21313 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e E_CREATE_TEMP_IN
21314 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c DEX.** SQL
21315 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
21316 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 TABLE.** S
21317 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
21318 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 P_TRIGGER.**
21319 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
2131a 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 _TEMP_VIEW.**
2131b 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 SQLITE_CREAT
2131c 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 E_TRIGGER.**
2131d 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
2131e 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 _VIEW.** S
2131f 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 QLITE_DELETE.**
21320 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f SQLITE_DRO
21321 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 P_INDEX.**
21322 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 SQLITE_DROP_TAB
21323 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 LE.** SQLI
21324 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 TE_DROP_TEMP_IND
21325 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 EX.** SQLI
21326 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 TE_DROP_TEMP_TAB
21327 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 LE.** SQLI
21328 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 TE_DROP_TEMP_TRI
21329 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 GGER.** SQ
2132a 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 LITE_DROP_TEMP_V
2132b 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c IEW.** SQL
2132c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 ITE_DROP_TRIGGER
2132d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
2132e 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20 20 _DROP_VIEW.**
2132f 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 SQLITE_INSER
21330 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 T.** SQLIT
21331 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20 20 E_PRAGMA.**
21332 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a 2a SQLITE_READ.**
21333 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 45 SQLITE_SE
21334 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 LECT.** SQ
21335 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e LITE_TRANSACTION
21336 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
21337 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 _UPDATE.**.** Th
21338 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 e third and four
21339 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 th arguments to
2133a 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f the auth functio
2133b 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f n are the name o
2133c 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61 f.** the table a
2133d 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 nd the column th
2133e 61 74 20 61 72 65 20 62 65 69 6e 67 20 61 63 63 at are being acc
2133f 65 73 73 65 64 2e 20 20 54 68 65 20 61 75 74 68 essed. The auth
21340 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f function.** sho
21341 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 uld return eithe
21342 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c r SQLITE_OK, SQL
21343 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51 4c ITE_DENY, or SQL
21344 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66 0a ITE_IGNORE. If.
21345 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
21346 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65 61 returned, it mea
21347 6e 73 20 74 68 61 74 20 61 63 63 65 73 73 20 69 ns that access i
21348 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c 49 s allowed. SQLI
21349 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 TE_DENY.** means
2134a 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 that the SQL st
2134b 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76 atement will nev
2134c 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71 6c er-run - the sql
2134d 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c ite3_exec() call
2134e 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 .** will return
2134f 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 with an error.
21350 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65 SQLITE_IGNORE me
21351 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c ans that the SQL
21352 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 statement.** sh
21353 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74 ould run but att
21354 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68 empts to read th
21355 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 e specified colu
21356 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e mn will return N
21357 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d ULL.** and attem
21358 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65 pts to write the
21359 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20 column will be
2135a 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 ignored..**.** S
2135b 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 68 20 etting the auth
2135c 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c function to NULL
2135d 20 64 69 73 61 62 6c 65 73 20 74 68 69 73 20 68 disables this h
2135e 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75 6c ook. The defaul
2135f 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66 20 t.** setting of
21360 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f the auth functio
21361 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 n is NULL..*/.SQ
21362 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
21363 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
21364 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a zer(. sqlite3 *
21365 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 db,. int (*xAut
21366 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e h)(void*,int,con
21367 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
21368 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
21369 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20 ,const char*),.
2136a 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 void *pArg.){.
2136b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
2136c 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
2136d 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 . db->xAuth = x
2136e 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41 75 74 Auth;. db->pAut
2136f 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 hArg = pArg;. s
21370 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
21371 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 aredStatements(d
21372 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 b);. sqlite3_mu
21373 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
21374 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 tex);. return S
21375 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
21376 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72 72 6f ** Write an erro
21377 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 r message into p
21378 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 74 Parse->zErrMsg t
21379 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74 68 61 hat explains tha
2137a 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 73 75 t the.** user-su
2137b 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69 7a 61 pplied authoriza
2137c 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 tion function re
2137d 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65 67 61 turned an illega
2137e 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 l value..*/.stat
2137f 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 41 75 ic void sqliteAu
21380 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 thBadReturnCode(
21381 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a Parse *pParse){.
21382 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21383 67 28 70 50 61 72 73 65 2c 20 22 61 75 74 68 6f g(pParse, "autho
21384 72 69 7a 65 72 20 6d 61 6c 66 75 6e 63 74 69 6f rizer malfunctio
21385 6e 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 n");. pParse->r
21386 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
21387 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 ;.}../*.** The p
21388 45 78 70 72 20 73 68 6f 75 6c 64 20 62 65 20 61 Expr should be a
21389 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 TK_COLUMN expre
2138a 73 73 69 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c ssion. The tabl
2138b 65 20 72 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a e referred to.**
2138c 20 69 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20 is in pTabList
2138d 6f 72 20 65 6c 73 65 20 69 74 20 69 73 20 74 68 or else it is th
2138e 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 e NEW or OLD tab
2138f 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e le of a trigger.
21390 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 .** Check to s
21391 65 65 20 69 66 20 69 74 20 69 73 20 4f 4b 20 74 ee if it is OK t
21392 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 72 74 o read this part
21393 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a icular column..*
21394 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 75 74 68 *.** If the auth
21395 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
21396 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c s SQLITE_IGNORE,
21397 20 63 68 61 6e 67 65 20 74 68 65 20 54 4b 5f 43 change the TK_C
21398 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 OLUMN .** instru
21399 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f ction into a TK_
2139a 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 61 75 NULL. If the au
2139b 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 th function retu
2139c 72 6e 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c rns SQLITE_DENY,
2139d 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72 61 74 .** then generat
2139e 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 e an error..*/.S
2139f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
213a0 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 id sqlite3AuthRe
213a1 61 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ad(. Parse *pPa
213a2 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 rse, /* T
213a3 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 he parser contex
213a4 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 t */. Expr *pEx
213a5 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pr, /*
213a6 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 The expression t
213a7 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a o check authoriz
213a8 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63 ation on */. Sc
213a9 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20 20 hema *pSchema,
213aa 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d /* The schem
213ab 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 a of the express
213ac 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ion */. SrcList
213ad 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f *pTabList /
213ae 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 * All table that
213af 20 70 45 78 70 72 20 6d 69 67 68 74 20 72 65 66 pExpr might ref
213b0 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 er to */.){. sq
213b1 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
213b2 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 se->db;. int rc
213b3 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 ;. Table *pTab
213b4 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 = 0; /* The
213b5 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 table being rea
213b6 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
213b7 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 r *zCol; /*
213b8 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 Name of the colu
213b9 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 mn of the table
213ba 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20 */. int iSrc;
213bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
213bc 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d dex in pTabList-
213bd 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 >a[] of table be
213be 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f ing read */. co
213bf 6e 73 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65 nst char *zDBase
213c0 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 ; /* Name of d
213c1 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 61 63 atabase being ac
213c2 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 cessed */. Trig
213c3 67 65 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b gerStack *pStack
213c4 3b 20 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f ; /* The stack o
213c5 66 20 63 75 72 72 65 6e 74 20 74 72 69 67 67 65 f current trigge
213c6 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b rs */. int iDb;
213c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
213c8 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 The index of th
213c9 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 65 e database the e
213ca 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 xpression refers
213cb 20 74 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 to */.. if( db
213cc 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 ->xAuth==0 ) ret
213cd 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
213ce 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c Expr->op==TK_COL
213cf 55 4d 4e 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 UMN );. iDb = s
213d0 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
213d1 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 dex(pParse->db,
213d2 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 pSchema);. if(
213d3 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 iDb<0 ){. /*
213d4 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 An attempt to re
213d5 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20 ad a column out
213d6 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f 72 of a subquery or
213d7 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 65 other. ** te
213d8 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 2a mporary table. *
213d9 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 /. return;.
213da 7d 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 }. if( pTabList
213db 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 53 72 63 ){. for(iSrc
213dc 3d 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c =0; ALWAYS(iSrc<
213dd 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b pTabList->nSrc);
213de 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20 iSrc++){.
213df 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c if( pExpr->iTabl
213e0 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 e==pTabList->a[i
213e1 53 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 20 62 Src].iCursor ) b
213e2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
213e3 61 73 73 65 72 74 28 20 69 53 72 63 3c 70 54 61 assert( iSrc<pTa
213e4 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 bList->nSrc );.
213e5 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 pTab = pTabLi
213e6 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62 st->a[iSrc].pTab
213e7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
213e8 53 74 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e Stack = pParse->
213e9 74 72 69 67 53 74 61 63 6b 3b 0a 20 20 20 20 69 trigStack;. i
213ea 66 28 20 41 4c 57 41 59 53 28 70 53 74 61 63 6b f( ALWAYS(pStack
213eb 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 ) ){. /* Th
213ec 69 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74 is must be an at
213ed 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 tempt to read th
213ee 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 e NEW or OLD pse
213ef 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 20 udo-tables.
213f0 20 2a 2a 20 6f 66 20 61 20 74 72 69 67 67 65 72 ** of a trigger
213f1 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
213f2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
213f3 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e iTable==pStack->
213f4 6e 65 77 49 64 78 20 7c 7c 20 70 45 78 70 72 2d newIdx || pExpr-
213f5 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b 2d >iTable==pStack-
213f6 3e 6f 6c 64 49 64 78 20 29 3b 0a 20 20 20 20 20 >oldIdx );.
213f7 20 70 54 61 62 20 3d 20 70 53 74 61 63 6b 2d 3e pTab = pStack->
213f8 70 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a pTab;. }. }.
213f9 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 if( NEVER(pTab
213fa 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 ==0) ) return;.
213fb 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c if( pExpr->iCol
213fc 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 umn>=0 ){. as
213fd 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f sert( pExpr->iCo
213fe 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 lumn<pTab->nCol
213ff 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 );. zCol = pT
21400 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e ab->aCol[pExpr->
21401 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a iColumn].zName;.
21402 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62 }else if( pTab
21403 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 ->iPKey>=0 ){.
21404 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
21405 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c iPKey<pTab->nCol
21406 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 );. zCol = p
21407 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e Tab->aCol[pTab->
21408 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 iPKey].zName;.
21409 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6c 20 }else{. zCol
2140a 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 7d 0a 20 = "ROWID";. }.
2140b 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
2140c 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
2140d 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20 64 62 2d ;. zDBase = db-
2140e 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b >aDb[iDb].zName;
2140f 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 . rc = db->xAut
21410 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 h(db->pAuthArg,
21411 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 70 54 61 SQLITE_READ, pTa
21412 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20 b->zName, zCol,
21413 7a 44 42 61 73 65 2c 20 0a 20 20 20 20 20 20 20 zDBase, .
21414 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 pParse
21415 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b ->zAuthContext);
21416 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
21417 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 E_IGNORE ){.
21418 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e pExpr->op = TK_N
21419 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ULL;. }else if(
2141a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 rc==SQLITE_DENY
2141b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e ){. if( db->
2141c 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30 20 nDb>2 || iDb!=0
2141d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
2141e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
2141f 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 "access to %s.%
21420 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74 s.%s is prohibit
21421 65 64 22 2c 20 0a 20 20 20 20 20 20 20 20 20 7a ed", . z
21422 44 42 61 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 DBase, pTab->zNa
21423 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d me, zCol);. }
21424 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
21425 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
21426 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 se, "access to %
21427 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74 s.%s is prohibit
21428 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c ed",pTab->zName,
21429 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 zCol);. }.
2142a 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 pParse->rc = SQ
2142b 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c LITE_AUTH;. }el
2142c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 se if( rc!=SQLIT
2142d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
2142e 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 teAuthBadReturnC
2142f 6f 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d ode(pParse);. }
21430 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 .}../*.** Do an
21431 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 authorization ch
21432 65 63 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f eck using the co
21433 64 65 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 de and arguments
21434 20 67 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a given. Return.
21435 2a 2a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 ** either SQLITE
21436 5f 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 _OK (zero) or SQ
21437 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 LITE_IGNORE or S
21438 51 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 QLITE_DENY. If
21439 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 SQLITE_DENY.** i
2143a 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e s returned, then
2143b 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 the error count
2143c 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 and error messa
2143d 67 65 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 ge in pParse are
2143e 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 .** modified app
2143f 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 ropriately..*/.S
21440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
21441 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 t sqlite3AuthChe
21442 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ck(. Parse *pPa
21443 72 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c rse,. int code,
21444 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
21445 41 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Arg1,. const ch
21446 61 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e ar *zArg2,. con
21447 73 74 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 st char *zArg3.)
21448 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
21449 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
2144a 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f int rc;.. /* Do
2144b 6e 27 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f n't do any autho
2144c 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 rization checks
2144d 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 if the database
2144e 69 73 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a is initialising.
2144f 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 ** or if the p
21450 61 72 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 arser is being i
21451 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 nvoked from with
21452 69 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 in sqlite3_decla
21453 72 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 re_vtab.. */.
21454 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 if( db->init.bus
21455 79 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f y || IN_DECLARE_
21456 56 54 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 VTAB ){. retu
21457 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
21458 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 }.. if( db->xAu
21459 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 th==0 ){. ret
2145a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
2145b 20 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 }. rc = db->xA
2145c 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 uth(db->pAuthArg
2145d 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a , code, zArg1, z
2145e 41 72 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 Arg2, zArg3, pPa
2145f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
21460 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
21461 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 LITE_DENY ){.
21462 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
21463 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 (pParse, "not au
21464 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 thorized");.
21465 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c pParse->rc = SQL
21466 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 ITE_AUTH;. }els
21467 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 e if( rc!=SQLITE
21468 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 _OK && rc!=SQLIT
21469 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 E_IGNORE ){.
2146a 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 rc = SQLITE_DENY
2146b 3b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 ;. sqliteAuth
2146c 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 BadReturnCode(pP
2146d 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 arse);. }. ret
2146e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
2146f 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72 69 Push an authori
21470 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 zation context.
21471 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 After this rout
21472 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ine is called, t
21473 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67 75 he.** zArg3 argu
21474 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69 7a ment to authoriz
21475 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20 ation callbacks
21476 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74 will be zContext
21477 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 until.** popped
21478 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 3d . Or if pParse=
21479 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 =0, this routine
2147a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
2147b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
2147c 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 oid sqlite3AuthC
2147d 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 50 61 ontextPush(. Pa
2147e 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 41 rse *pParse,. A
2147f 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e uthContext *pCon
21480 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 text, . const c
21481 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b har *zContext.){
21482 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
21483 65 20 29 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d e );. pContext-
21484 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 >pParse = pParse
21485 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 ;. pContext->zA
21486 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 uthContext = pPa
21487 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
21488 74 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 t;. pParse->zAu
21489 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e thContext = zCon
2148a 74 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 text;.}../*.** P
2148b 6f 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 op an authorizat
2148c 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 ion context that
2148d 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 was previously
2148e 70 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c pushed.** by sql
2148f 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
21490 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ush.*/.SQLITE_PR
21491 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21492 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 e3AuthContextPop
21493 28 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 (AuthContext *pC
21494 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70 ontext){. if( p
21495 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 Context->pParse
21496 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d ){. pContext-
21497 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f >pParse->zAuthCo
21498 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 ntext = pContext
21499 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a ->zAuthContext;.
2149a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 pContext->pP
2149b 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a arse = 0;. }.}.
2149c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
2149d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
2149e 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a TION */../******
2149f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
214a0 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a auth.c *********
214a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214a3 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
214a4 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
214a5 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a ile build.c ****
214a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214a8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
214a9 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
214aa 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
214ab 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
214ac 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
214ad 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
214ae 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
214af 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
214b0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
214b1 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
214b2 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
214b3 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
214b4 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
214b5 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
214b6 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
214b7 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
214b8 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
214b9 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
214ba 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
214bb 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
214bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
214bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
214c0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
214c1 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 tains C code rou
214c2 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 tines that are c
214c3 61 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c alled by the SQL
214c4 69 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 ite parser.** wh
214c5 65 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 en syntax rules
214c6 61 72 65 20 72 65 64 75 63 65 64 2e 20 20 54 68 are reduced. Th
214c7 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 e routines in th
214c8 69 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 is file handle t
214c9 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 he.** following
214ca 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e kinds of SQL syn
214cb 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 tax:.**.** C
214cc 52 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 REATE TABLE.**
214cd 20 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a DROP TABLE.**
214ce 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 CREATE INDE
214cf 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e X.** DROP IN
214d0 44 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 DEX.** creat
214d1 69 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 ing ID lists.**
214d2 20 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41 BEGIN TRANSA
214d3 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d CTION.** COM
214d4 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 MIT.** ROLLB
214d5 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 ACK.**.** $Id: b
214d6 75 69 6c 64 2e 63 2c 76 20 31 2e 35 33 37 20 32 uild.c,v 1.537 2
214d7 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 34 32 3a 009/05/06 18:42:
214d8 32 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 21 drh Exp $.*/.
214d9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
214da 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 ine is called wh
214db 65 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 en a new SQL sta
214dc 74 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e tement is beginn
214dd 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 ing to.** be par
214de 73 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 sed. Initialize
214df 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 the pParse stru
214e0 63 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e cture as needed.
214e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
214e2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
214e3 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20 eginParse(Parse
214e4 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70 *pParse, int exp
214e5 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 lainFlag){. pPa
214e6 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 28 rse->explain = (
214e7 75 38 29 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a u8)explainFlag;.
214e8 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d pParse->nVar =
214e9 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 0;.}..#ifndef S
214ea 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
214eb 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68 D_CACHE./*.** Th
214ec 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75 e TableLock stru
214ed 63 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73 cture is only us
214ee 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 ed by the sqlite
214ef 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 3TableLock() and
214f0 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 .** codeTableLoc
214f1 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a ks() functions..
214f2 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c */.struct TableL
214f3 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b ock {. int iDb;
214f4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
214f5 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
214f6 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c taining the tabl
214f7 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
214f8 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 20 /. int iTab;
214f9 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
214fa 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
214fb 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 table to be loc
214fc 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 ked */. u8 isWr
214fd 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a iteLock; /*
214fe 20 54 72 75 65 20 66 6f 72 20 77 72 69 74 65 20 True for write
214ff 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72 lock. False for
21500 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a a read lock */.
21501 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
21502 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f ame; /* Name o
21503 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d f the table */.}
21504 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 ;../*.** Record
21505 74 68 65 20 66 61 63 74 20 74 68 61 74 20 77 65 the fact that we
21506 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20 want to lock a
21507 74 61 62 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d table at run-tim
21508 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 e. .**.** The t
21509 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 able to be locke
2150a 64 20 68 61 73 20 72 6f 6f 74 20 70 61 67 65 20 d has root page
2150b 69 54 61 62 20 61 6e 64 20 69 73 20 66 6f 75 6e iTab and is foun
2150c 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 d in database iD
2150d 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 b..** A read or
2150e 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e a write lock can
2150f 20 62 65 20 74 61 6b 65 6e 20 64 65 70 65 6e 64 be taken depend
21510 69 6e 67 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f ing on isWritelo
21511 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ck..**.** This r
21512 6f 75 74 69 6e 65 20 6a 75 73 74 20 72 65 63 6f outine just reco
21513 72 64 73 20 74 68 65 20 66 61 63 74 20 74 68 61 rds the fact tha
21514 74 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 65 t the lock is de
21515 73 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63 sired. The.** c
21516 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 ode to make the
21517 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73 20 67 65 lock occur is ge
21518 6e 65 72 61 74 65 64 20 62 79 20 61 20 6c 61 74 nerated by a lat
21519 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f er call to.** co
2151a 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77 deTableLocks() w
2151b 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 hich occurs duri
2151c 6e 67 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 ng sqlite3Finish
2151d 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c Coding()..*/.SQL
2151e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
2151f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
21520 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 k(. Parse *pPar
21521 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
21522 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
21523 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 int iDb,
21524 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
21525 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 he database cont
21526 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 aining the table
21527 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e to lock */. in
21528 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 t iTab,
21529 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75 /* Root page nu
2152a 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c mber of the tabl
2152b 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
2152c 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f /. u8 isWriteLo
2152d 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66 ck, /* True f
2152e 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 or a write lock
2152f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
21530 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 *zName /* Name
21531 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
21532 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a be locked */.){.
21533 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
21534 42 79 74 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f Bytes;. TableLo
21535 63 6b 20 2a 70 3b 0a 0a 20 20 69 66 28 20 69 44 ck *p;.. if( iD
21536 62 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 b<0 ){. retur
21537 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d n;. }.. for(i=
21538 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 0; i<pParse->nTa
21539 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 bleLock; i++){.
2153a 20 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e p = &pParse->
2153b 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 aTableLock[i];.
2153c 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69 if( p->iDb==i
2153d 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69 Db && p->iTab==i
2153e 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Tab ){. p->
2153f 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70 isWriteLock = (p
21540 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c ->isWriteLock ||
21541 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 isWriteLock);.
21542 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
21543 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73 }. }.. nBytes
21544 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c = sizeof(TableL
21545 6f 63 6b 29 20 2a 20 28 70 50 61 72 73 65 2d 3e ock) * (pParse->
21546 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 nTableLock+1);.
21547 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c pParse->aTableL
21548 6f 63 6b 20 3d 20 0a 20 20 20 20 20 20 73 71 6c ock = . sql
21549 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
2154a 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ree(pParse->db,
2154b 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f pParse->aTableLo
2154c 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 ck, nBytes);. i
2154d 66 28 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c f( pParse->aTabl
2154e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d eLock ){. p =
2154f 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 &pParse->aTable
21550 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 54 61 Lock[pParse->nTa
21551 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 bleLock++];.
21552 70 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 p->iDb = iDb;.
21553 20 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62 p->iTab = iTab
21554 3b 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 ;. p->isWrite
21555 4c 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f Lock = isWriteLo
21556 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 ck;. p->zName
21557 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 = zName;. }els
21558 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e e{. pParse->n
21559 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 TableLock = 0;.
2155a 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d pParse->db->m
2155b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
2155c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f . }.}../*.** Co
2155d 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f de an OP_TableLo
2155e 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 ck instruction f
2155f 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f or each table lo
21560 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 cked by the.** s
21561 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 tatement (config
21562 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f ured by calls to
21563 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
21564 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 k())..*/.static
21565 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f void codeTableLo
21566 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 cks(Parse *pPars
21567 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 e){. int i;. V
21568 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20 dbe *pVdbe; ..
21569 69 66 28 20 30 3d 3d 28 70 56 64 62 65 20 3d 20 if( 0==(pVdbe =
2156a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
2156b 50 61 72 73 65 29 29 20 29 7b 0a 20 20 20 20 72 Parse)) ){. r
2156c 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f eturn;. }.. fo
2156d 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
2156e 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b >nTableLock; i++
2156f 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b ){. TableLock
21570 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 *p = &pParse->a
21571 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 TableLock[i];.
21572 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 int p1 = p->iD
21573 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 b;. sqlite3Vd
21574 62 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 beAddOp4(pVdbe,
21575 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 OP_TableLock, p1
21576 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 , p->iTab, p->is
21577 57 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 WriteLock,.
21578 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21579 20 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 p->zName, P4_ST
2157a 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c ATIC);. }.}.#el
2157b 73 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 se. #define cod
2157c 65 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 eTableLocks(x).#
2157d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
2157e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
2157f 6c 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 led after a sing
21580 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 le SQL statement
21581 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 has been.** par
21582 73 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 sed and a VDBE p
21583 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 rogram to execut
21584 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 e that statement
21585 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 has been.** pre
21586 70 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 pared. This rou
21587 74 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 tine puts the fi
21588 6e 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 nishing touches
21589 6f 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 on the.** VDBE p
2158a 72 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 rogram and reset
2158b 73 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 s the pParse str
2158c 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e ucture for the n
2158d 65 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a ext.** parse..**
2158e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 .** Note that if
2158f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 an error occurr
21590 65 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 ed, it might be
21591 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a the case that.**
21592 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 no VDBE code wa
21593 73 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a s generated..*/.
21594 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21595 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 oid sqlite3Finis
21596 68 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 hCoding(Parse *p
21597 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 Parse){. sqlite
21598 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3 *db;. Vdbe *v
21599 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 ;.. db = pParse
2159a 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e ->db;. if( db->
2159b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 mallocFailed ) r
2159c 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 eturn;. if( pPa
2159d 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 rse->nested ) re
2159e 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 turn;. if( pPar
2159f 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 se->nErr ) retur
215a0 6e 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 n;.. /* Begin b
215a1 79 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d y generating som
215a2 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f e termination co
215a3 64 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 de at the end of
215a4 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 the. ** vdbe p
215a5 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 rogram. */. v
215a6 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
215a7 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
215a8 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 v ){. sqlite3
215a9 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 VdbeAddOp0(v, OP
215aa 5f 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 _Halt);.. /*
215ab 54 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 The cookie mask
215ac 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 contains one bit
215ad 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 for each databa
215ae 73 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 se file open..
215af 20 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66 ** (Bit 0 is f
215b0 6f 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 or main, bit 1 i
215b1 73 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 s for temp, and
215b2 73 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73 so forth.) Bits
215b3 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 are. ** set
215b4 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 for each databas
215b5 65 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20 e that is used.
215b6 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
215b7 6f 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a o start a. **
215b8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 transaction on
215b9 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61 each used databa
215ba 73 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79 se and to verify
215bb 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b the schema cook
215bc 69 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 ie. ** on eac
215bd 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e h used database.
215be 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
215bf 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f pParse->cookieGo
215c0 74 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33 to>0 ){. u3
215c1 32 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e 2 mask;. in
215c2 74 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c t iDb;. sql
215c3 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
215c4 28 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b (v, pParse->cook
215c5 69 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20 ieGoto-1);.
215c6 20 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b for(iDb=0, mask
215c7 3d 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b =1; iDb<db->nDb;
215c8 20 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b mask<<=1, iDb++
215c9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 ){. if( (
215ca 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63 mask & pParse->c
215cb 6f 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20 ookieMask)==0 )
215cc 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
215cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 sqlite3VdbeUse
215ce 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a sBtree(v, iDb);.
215cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
215d0 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54 dbeAddOp2(v,OP_T
215d1 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c ransaction, iDb,
215d2 20 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d (mask & pParse-
215d3 3e 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b >writeMask)!=0);
215d4 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d . if( db-
215d5 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b >init.busy==0 ){
215d6 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
215d7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 4f e3VdbeAddOp2(v,O
215d8 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20 P_VerifyCookie,
215d9 69 44 62 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f iDb, pParse->coo
215da 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a kieValue[iDb]);.
215db 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
215dc 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
215dd 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
215de 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 LE. {.
215df 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
215e0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
215e1 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 rse->nVtabLock;
215e2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i++){.
215e3 63 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68 char *vtab = (ch
215e4 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 61 70 56 ar *)pParse->apV
215e5 74 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61 tabLock[i]->pVta
215e6 62 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c b;. sql
215e7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
215e8 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20 , OP_VBegin, 0,
215e9 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56 0, 0, vtab, P4_V
215ea 54 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a TAB);. }.
215eb 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
215ec 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 nVtabLock = 0;.
215ed 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }.#endif..
215ee 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c /* Once all
215ef 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76 the cookies hav
215f0 65 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 e been verified
215f1 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 and transactions
215f2 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20 opened, .
215f3 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65 ** obtain the re
215f4 71 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63 quired table-loc
215f5 6b 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f ks. This is a no
215f6 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a -op unless the .
215f7 20 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d ** shared-
215f8 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 cache feature is
215f9 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20 enabled..
215fa 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62 */. codeTab
215fb 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b leLocks(pParse);
215fc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
215fd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 beAddOp2(v, OP_G
215fe 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e oto, 0, pParse->
215ff 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20 20 cookieGoto);.
21600 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 }. }... /* Ge
21601 74 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 t the VDBE progr
21602 61 6d 20 72 65 61 64 79 20 66 6f 72 20 65 78 65 am ready for exe
21603 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 cution. */. if
21604 28 20 76 20 26 26 20 70 50 61 72 73 65 2d 3e 6e ( v && pParse->n
21605 45 72 72 3d 3d 30 20 26 26 20 21 64 62 2d 3e 6d Err==0 && !db->m
21606 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23 allocFailed ){.#
21607 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
21608 55 47 0a 20 20 20 20 46 49 4c 45 20 2a 74 72 61 UG. FILE *tra
21609 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 ce = (db->flags
2160a 26 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 & SQLITE_VdbeTra
2160b 63 65 29 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 ce)!=0 ? stdout
2160c 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 : 0;. sqlite3
2160d 56 64 62 65 54 72 61 63 65 28 76 2c 20 74 72 61 VdbeTrace(v, tra
2160e 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 ce);.#endif.
2160f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
21610 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 30 20 29 iCacheLevel==0 )
21611 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61 ; /* Disables a
21612 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61 nd re-enables ma
21613 74 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 tch */. sqlit
21614 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
21615 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c v, pParse->nVar,
21616 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c 0a 20 pParse->nMem,.
21617 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21618 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
21619 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e 65 78 nTab, pParse->ex
2161a 70 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72 plain);. pPar
2161b 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
2161c 44 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65 DONE;. pParse
2161d 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 ->colNamesSet =
2161e 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 0;. }else if( p
2161f 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 Parse->rc==SQLIT
21620 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 E_OK ){. pPar
21621 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
21622 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 ERROR;. }. pPa
21623 72 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 rse->nTab = 0;.
21624 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 pParse->nMem =
21625 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 0;. pParse->nSe
21626 74 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d t = 0;. pParse-
21627 3e 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 >nVar = 0;. pPa
21628 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 rse->cookieMask
21629 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 = 0;. pParse->c
2162a 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d ookieGoto = 0;.}
2162b 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 ../*.** Run the
2162c 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 parser and code
2162d 67 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 generator recurs
2162e 69 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 ively in order t
2162f 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f o generate.** co
21630 64 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 de for the SQL s
21631 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f tatement given o
21632 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 nto the end of t
21633 68 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 he pParse contex
21634 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 t.** currently u
21635 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
21636 6e 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 n. When the par
21637 73 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 ser is run recur
21638 73 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 sively.** this w
21639 61 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 ay, the final OP
2163a 5f 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 _Halt is not app
2163b 65 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 ended and other
2163c 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a initialization.*
2163d 2a 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 * and finalizati
2163e 6f 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 on steps are omi
2163f 74 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f tted because tho
21640 73 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 se are handling
21641 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d by the.** outerm
21642 6f 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a ost parser..**.*
21643 2a 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 * Not everything
21644 20 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 is nestable. T
21645 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 his facility is
21646 64 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d designed to perm
21647 69 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 it.** INSERT, UP
21648 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 DATE, and DELETE
21649 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 operations agai
2164a 6e 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 nst SQLITE_MASTE
2164b 52 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 R. Use.** care
2164c 69 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f if you decide to
2164d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 try to use this
2164e 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d routine for som
2164f 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 e other purposes
21650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21651 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
21652 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 NestedParse(Pars
21653 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
21654 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
21655 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
21656 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c ap;. char *zSql
21657 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 ;. char *zErrMs
21658 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 g = 0;. sqlite3
21659 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
2165a 62 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 b;.# define SAVE
2165b 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 _SZ (sizeof(Par
2165c 73 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 se) - offsetof(P
2165d 61 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 arse,nVar)). ch
2165e 61 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f ar saveBuf[SAVE_
2165f 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 SZ];.. if( pPar
21660 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 se->nErr ) retur
21661 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 n;. assert( pPa
21662 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 rse->nested<10 )
21663 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 ; /* Nesting sh
21664 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 ould only be of
21665 6c 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f limited depth */
21666 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
21667 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c zFormat);. zSql
21668 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
21669 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
2166a 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
2166b 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 );. if( zSql==0
2166c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 ){. return;
2166d 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 /* A malloc mu
2166e 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a st have failed *
2166f 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e /. }. pParse->
21670 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 nested++;. memc
21671 70 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 py(saveBuf, &pPa
21672 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f rse->nVar, SAVE_
21673 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 SZ);. memset(&p
21674 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 Parse->nVar, 0,
21675 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 SAVE_SZ);. sqli
21676 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 te3RunParser(pPa
21677 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 rse, zSql, &zErr
21678 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 Msg);. sqlite3D
21679 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 bFree(db, zErrMs
2167a 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 g);. sqlite3DbF
2167b 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 ree(db, zSql);.
2167c 20 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d memcpy(&pParse-
2167d 3e 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 >nVar, saveBuf,
2167e 53 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 SAVE_SZ);. pPar
2167f 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a se->nested--;.}.
21680 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 ./*.** Locate th
21681 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 e in-memory stru
21682 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
21683 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 ibes a particula
21684 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 r database.** ta
21685 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 ble given the na
21686 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 me of that table
21687 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 and (optionally
21688 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 ) the name of th
21689 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
2168a 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 ntaining the tab
2168b 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c le. Return NULL
2168c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
2168d 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 *.** If zDatabas
2168e 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 e is 0, all data
2168f 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 bases are search
21690 65 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ed for the table
21691 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 and the.** firs
21692 74 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 t matching table
21693 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 is returned. (
21694 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 No checking for
21695 64 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a duplicate table.
21696 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 ** names is done
21697 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f .) The search o
21698 72 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 rder is TEMP fir
21699 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 st, then MAIN, t
2169a 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c hen any.** auxil
2169b 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 iary databases a
2169c 64 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 dded using the A
2169d 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a TTACH command..*
2169e 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
2169f 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
216a0 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
216a1 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
216a2 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 ite3FindTable(sq
216a3 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
216a4 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f char *zName, co
216a5 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 nst char *zDatab
216a6 61 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 ase){. Table *p
216a7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 = 0;. int i;.
216a8 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 int nName;. as
216a9 73 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 sert( zName!=0 )
216aa 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 ;. nName = sqli
216ab 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d te3Strlen30(zNam
216ac 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 e);. for(i=OMIT
216ad 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e _TEMPDB; i<db->n
216ae 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e Db; i++){. in
216af 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e t j = (i<2) ? i^
216b0 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61 72 1 : i; /* Sear
216b1 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d ch TEMP before M
216b2 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a AIN */. if( z
216b3 44 61 74 61 62 61 73 65 21 3d 30 20 26 26 20 73 Database!=0 && s
216b4 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 qlite3StrICmp(zD
216b5 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 atabase, db->aDb
216b6 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e [j].zName) ) con
216b7 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20 73 tinue;. p = s
216b8 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 qlite3HashFind(&
216b9 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 db->aDb[j].pSche
216ba 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 ma->tblHash, zNa
216bb 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 me, nName);.
216bc 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20 if( p ) break;.
216bd 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
216be 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 ../*.** Locate t
216bf 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 he in-memory str
216c0 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 ucture that desc
216c1 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c ribes a particul
216c2 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 ar database.** t
216c3 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e able given the n
216c4 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c ame of that tabl
216c5 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c e and (optionall
216c6 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 y) the name of t
216c7 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 he.** database c
216c8 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
216c9 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c ble. Return NUL
216ca 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 L if not found.
216cb 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a Also leave an.*
216cc 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 * error message
216cd 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d in pParse->zErrM
216ce 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 sg..**.** The di
216cf 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
216d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e this routine an
216d1 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 d sqlite3FindTab
216d2 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 le() is that thi
216d3 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61 s.** routine lea
216d4 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ves an error mes
216d5 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e sage in pParse->
216d6 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a zErrMsg where.**
216d7 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c sqlite3FindTabl
216d8 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f e() does not..*/
216d9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
216da 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f Table *sqlite3Lo
216db 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50 61 72 cateTable(. Par
216dc 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
216dd 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69 /* context i
216de 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70 6f 72 n which to repor
216df 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e t errors */. in
216e0 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 20 t isView,
216e1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
216e2 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 56 49 looking for a VI
216e3 45 57 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 EW rather than a
216e4 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73 TABLE */. cons
216e5 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
216e6 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
216e7 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 6c e table we are l
216e8 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 ooking for */.
216e9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 61 const char *zDba
216ea 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f se /* Name o
216eb 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 f the database.
216ec 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a Might be NULL *
216ed 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b /.){. Table *p;
216ee 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 .. /* Read the
216ef 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
216f0 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
216f1 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 urs, leave an er
216f2 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a ror message. **
216f3 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 and code in pPa
216f4 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e rse and return N
216f5 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 ULL. */. if( SQ
216f6 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 LITE_OK!=sqlite3
216f7 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 ReadSchema(pPars
216f8 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e e) ){. return
216f9 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 0;. }.. p = s
216fa 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
216fb 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d pParse->db, zNam
216fc 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20 69 66 e, zDbase);. if
216fd 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f ( p==0 ){. co
216fe 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d nst char *zMsg =
216ff 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20 73 75 isView ? "no su
21700 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f 20 73 ch view" : "no s
21701 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20 20 20 uch table";.
21702 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a 20 20 if( zDbase ){.
21703 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
21704 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a Msg(pParse, "%s:
21705 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a %s.%s", zMsg, z
21706 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 Dbase, zName);.
21707 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21708 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21709 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 pParse, "%s: %s"
2170a 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b 0a , zMsg, zName);.
2170b 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 }. pParse
2170c 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 ->checkSchema =
2170d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
2170e 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 p;.}../*.** Loca
2170f 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 te the in-memory
21710 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
21711 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 61 20 describes .** a
21712 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 particular index
21713 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 given the name
21714 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a 2a 2a of that index.**
21715 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 and the name of
21716 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 the database th
21717 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
21718 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 72 6e index..** Return
21719 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 NULL if not fou
2171a 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 nd..**.** If zDa
2171b 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c tabase is 0, all
2171c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73 databases are s
2171d 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65 0a earched for the.
2171e 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 ** table and the
2171f 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 first matching
21720 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 6e 65 index is returne
21721 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 d. (No checking
21722 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 61 74 .** for duplicat
21723 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 69 73 e index names is
21724 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61 done.) The sea
21725 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a 2a 20 rch order is.**
21726 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e TEMP first, then
21727 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 20 MAIN, then any
21728 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
21729 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 73 69 ses added.** usi
2172a 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63 6f ng the ATTACH co
2172b 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 mmand..*/.SQLITE
2172c 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a _PRIVATE Index *
2172d 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
2172e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
2172f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
21730 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
21731 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d 20 ){. Index *p =
21732 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 0;. int i;. in
21733 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 t nName = sqlite
21734 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3Strlen30(zName)
21735 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 ;. for(i=OMIT_T
21736 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 EMPDB; i<db->nDb
21737 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 ; i++){. int
21738 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 j = (i<2) ? i^1
21739 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 : i; /* Search
2173a 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e TEMP before MAIN
2173b 20 2a 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 2a */. Schema *
2173c 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 pSchema = db->aD
2173d 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 b[j].pSchema;.
2173e 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c if( zDb && sql
2173f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c ite3StrICmp(zDb,
21740 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d db->aDb[j].zNam
21741 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 e) ) continue;.
21742 20 20 20 61 73 73 65 72 74 28 20 70 53 63 68 65 assert( pSche
21743 6d 61 20 7c 7c 20 28 6a 3d 3d 31 20 26 26 20 21 ma || (j==1 && !
21744 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20 db->aDb[1].pBt)
21745 29 3b 0a 20 20 20 20 69 66 28 20 70 53 63 68 65 );. if( pSche
21746 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 ma ){. p =
21747 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
21748 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 &pSchema->idxHas
21749 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 h, zName, nName)
2174a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
2174b 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 p ) break;. }.
2174c 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
2174d 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 74 68 65 20 .** Reclaim the
2174e 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61 memory used by a
2174f 6e 20 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74 69 n index.*/.stati
21750 63 20 76 6f 69 64 20 66 72 65 65 49 6e 64 65 78 c void freeIndex
21751 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73 71 (Index *p){. sq
21752 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 70 lite3 *db = p->p
21753 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 Table->dbMem;.
21754 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
21755 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 , p->zColAff);.
21756 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
21757 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 b, p);.}../*.**
21758 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76 65 6e Remove the given
21759 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 index from the
2175a 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 index hash table
2175b 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69 74 , and free.** it
2175c 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 s memory structu
2175d 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 res..**.** The i
2175e 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65 64 20 ndex is removed
2175f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
21760 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 62 75 e hash tables bu
21761 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 75 t.** it is not u
21762 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 nlinked from the
21763 20 54 61 62 6c 65 20 74 68 61 74 20 69 74 20 69 Table that it i
21764 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c 69 6e ndexes..** Unlin
21765 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 54 61 king from the Ta
21766 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 ble must be done
21767 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
21768 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
21769 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
2176a 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e 64 65 DeleteIndex(Inde
2176b 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20 2a x *p){. Index *
2176c 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 pOld;. const ch
2176d 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a ar *zName = p->z
2176e 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d 20 Name;.. pOld =
2176f 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 sqlite3HashInser
21770 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 t(&p->pSchema->i
21771 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 0a 20 dxHash, zName,.
21772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21773 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21774 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3Strlen30(zName)
21775 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 , 0);. assert(
21776 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d pOld==0 || pOld=
21777 3d 70 20 29 3b 0a 20 20 66 72 65 65 49 6e 64 65 =p );. freeInde
21778 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 x(p);.}../*.** F
21779 6f 72 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c or the index cal
2177a 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 led zIdxName whi
2177b 63 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 ch is found in t
2177c 68 65 20 64 61 74 61 62 61 73 65 20 69 44 62 2c he database iDb,
2177d 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 .** unlike that
2177e 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54 index from its T
2177f 61 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 able then remove
21780 20 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a the index from.
21781 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 ** the index has
21782 68 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65 h table and free
21783 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 all memory stru
21784 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 ctures associate
21785 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e d.** with the in
21786 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 dex..*/.SQLITE_P
21787 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21788 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
21789 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 teIndex(sqlite3
2178a 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f *db, int iDb, co
2178b 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 nst char *zIdxNa
2178c 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 me){. Index *pI
2178d 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b ndex;. int len;
2178e 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 20 3d . Hash *pHash =
2178f 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 &db->aDb[iDb].p
21790 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b Schema->idxHash;
21791 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 .. len = sqlite
21792 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e 61 3Strlen30(zIdxNa
21793 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 me);. pIndex =
21794 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 sqlite3HashInser
21795 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d t(pHash, zIdxNam
21796 65 2c 20 6c 65 6e 2c 20 30 29 3b 0a 20 20 69 66 e, len, 0);. if
21797 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ( pIndex ){.
21798 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 if( pIndex->pTab
21799 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64 le->pIndex==pInd
2179a 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 ex ){. pInd
2179b 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 ex->pTable->pInd
2179c 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 ex = pIndex->pNe
2179d 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
2179e 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 Index *p;.
2179f 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65 for(p=pInde
217a0 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 x->pTable->pInde
217a1 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 x; p && p->pNext
217a2 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70 !=pIndex; p=p->p
217a3 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66 Next){}. if
217a4 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d ( p && p->pNext=
217a5 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 =pIndex ){.
217a6 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49 p->pNext = pI
217a7 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 ndex->pNext;.
217a8 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 }. }. f
217a9 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 reeIndex(pIndex)
217aa 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 ;. }. db->flag
217ab 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 s |= SQLITE_Inte
217ac 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a rnChanges;.}../*
217ad 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63 .** Erase all sc
217ae 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hema information
217af 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d from the in-mem
217b0 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73 20 ory hash tables
217b1 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 of.** a single d
217b2 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 atabase. This r
217b3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
217b4 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f to reclaim memo
217b5 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 ry.** before the
217b6 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73 database closes
217b7 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61 . It is also ca
217b8 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f lled during a ro
217b9 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65 llback.** if the
217ba 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20 63 re were schema c
217bb 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68 hanges during th
217bc 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 e transaction or
217bd 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d if a.** schema-
217be 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20 cookie mismatch
217bf 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 occurs..**.** If
217c0 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72 65 73 iDb==0 then res
217c1 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 et the internal
217c2 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f schema tables fo
217c3 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a r all database.*
217c4 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62 * files. If iDb
217c5 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74 20 74 >=1 then reset t
217c6 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 he internal sche
217c7 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a ma for only the.
217c8 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69 ** single file i
217c9 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c ndicated..*/.SQL
217ca 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
217cb 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
217cc 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 ernalSchema(sqli
217cd 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 te3 *db, int iDb
217ce 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
217cf 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
217d0 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
217d1 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 ;.. if( iDb==0
217d2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
217d3 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b reeEnterAll(db);
217d4 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44 62 . }. for(i=iDb
217d5 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
217d6 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d ){. Db *pDb =
217d7 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 &db->aDb[i];.
217d8 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 if( pDb->pSche
217d9 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ma ){. asse
217da 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62 2d rt(i==1 || (pDb-
217db 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42 >pBt && sqlite3B
217dc 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
217dd 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20 20 Db->pBt)));.
217de 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 sqlite3SchemaF
217df 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65 6d 61 ree(pDb->pSchema
217e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
217e1 20 69 44 62 3e 30 20 29 20 72 65 74 75 72 6e 3b iDb>0 ) return;
217e2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 . }. assert( i
217e3 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 66 Db==0 );. db->f
217e4 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f lags &= ~SQLITE_
217e5 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 InternChanges;.
217e6 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
217e7 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a veAll(db);.. /*
217e8 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 If one or more
217e9 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 of the auxiliary
217ea 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 database files
217eb 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c has been closed,
217ec 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76 . ** then remov
217ed 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 e them from the
217ee 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
217ef 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b se list. We tak
217f0 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 e the. ** oppor
217f1 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69 tunity to do thi
217f2 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20 s here since we
217f3 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65 have just delete
217f4 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a d all of the. *
217f5 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 * schema hash ta
217f6 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f bles and therefo
217f7 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 re do not have t
217f8 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 o make any chang
217f9 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f es. ** to any o
217fa 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a f those tables..
217fb 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
217fc 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
217fd 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a . struct Db *
217fe 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 pDb = &db->aDb[i
217ff 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e ];. if( pDb->
21800 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pBt==0 ){.
21801 69 66 28 20 70 44 62 2d 3e 70 41 75 78 20 26 26 if( pDb->pAux &&
21802 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 20 29 pDb->xFreeAux )
21803 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 28 70 pDb->xFreeAux(p
21804 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 Db->pAux);.
21805 20 70 44 62 2d 3e 70 41 75 78 20 3d 20 30 3b 0a pDb->pAux = 0;.
21806 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 }. }. for(
21807 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62 i=j=2; i<db->nDb
21808 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
21809 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 ct Db *pDb = &db
2180a 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 ->aDb[i];. if
2180b 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b ( pDb->pBt==0 ){
2180c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
2180d 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a 4e Free(db, pDb->zN
2180e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 2d ame);. pDb-
2180f 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 >zName = 0;.
21810 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
21811 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b }. if( j<i ){
21812 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a . db->aDb[j
21813 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a ] = db->aDb[i];.
21814 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 }. j++;.
21815 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 2d }. memset(&db-
21816 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 2d >aDb[j], 0, (db-
21817 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 64 >nDb-j)*sizeof(d
21818 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 64 b->aDb[j]));. d
21819 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 66 b->nDb = j;. if
2181a 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 20 ( db->nDb<=2 &&
2181b 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 62 db->aDb!=db->aDb
2181c 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d 65 Static ){. me
2181d 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 74 mcpy(db->aDbStat
2181e 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a 73 ic, db->aDb, 2*s
2181f 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d izeof(db->aDb[0]
21820 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ));. sqlite3D
21821 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 bFree(db, db->aD
21822 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20 b);. db->aDb
21823 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b = db->aDbStatic;
21824 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
21825 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
21826 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d lled when a comm
21827 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 it occurs..*/.SQ
21828 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21829 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 d sqlite3CommitI
2182a 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 nternalChanges(s
2182b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 qlite3 *db){. d
2182c 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c b->flags &= ~SQL
2182d 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
2182e 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 s;.}../*.** Clea
2182f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d r the column nam
21830 65 73 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 es from a table
21831 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74 or view..*/.stat
21832 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65 ic void sqliteRe
21833 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54 setColumnNames(T
21834 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 able *pTable){.
21835 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e int i;. Column
21836 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 *pCol;. sqlite
21837 33 20 2a 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 3 *db = pTable->
21838 64 62 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 dbMem;. assert(
21839 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 pTable!=0 );.
2183a 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 if( (pCol = pTab
2183b 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a le->aCol)!=0 ){.
2183c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
2183d 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b Table->nCol; i++
2183e 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 , pCol++){.
2183f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
21840 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b b, pCol->zName);
21841 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21842 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f prDelete(db, pCo
21843 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 l->pDflt);.
21844 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
21845 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b b, pCol->zType);
21846 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
21847 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a Free(db, pCol->z
21848 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Coll);. }.
21849 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2184a 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 b, pTable->aCol)
2184b 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e ;. }. pTable->
2184c 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 aCol = 0;. pTab
2184d 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a le->nCol = 0;.}.
2184e 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 ./*.** Remove th
2184f 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 e memory data st
21850 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 ructures associa
21851 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 ted with the giv
21852 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f en.** Table. No
21853 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 changes are mad
21854 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 e to disk by thi
21855 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a s routine..**.**
21856 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 This routine ju
21857 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 st deletes the d
21858 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 ata structure.
21859 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 It does not unli
2185a 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 nk.** the table
2185b 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66 data structure f
2185c 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 rom the hash tab
2185d 6c 65 2e 20 20 42 75 74 20 69 74 20 64 6f 65 73 le. But it does
2185e 20 64 65 73 74 72 6f 79 0a 2a 2a 20 6d 65 6d 6f destroy.** memo
2185f 72 79 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 ry structures of
21860 20 74 68 65 20 69 6e 64 69 63 65 73 20 61 6e 64 the indices and
21861 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 foreign keys as
21862 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a sociated with .*
21863 2a 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a * the table..*/.
21864 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21865 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
21866 65 54 61 62 6c 65 28 54 61 62 6c 65 20 2a 70 54 eTable(Table *pT
21867 61 62 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a able){. Index *
21868 70 49 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b 0a pIndex, *pNext;.
21869 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 2a FKey *pFKey, *
2186a 70 4e 65 78 74 46 4b 65 79 3b 0a 20 20 73 71 6c pNextFKey;. sql
2186b 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 ite3 *db;.. if(
2186c 20 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 pTable==0 ) ret
2186d 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 54 61 62 urn;. db = pTab
2186e 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 0a 20 20 2f 2a le->dbMem;.. /*
2186f 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 Do not delete t
21870 68 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74 he table until t
21871 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
21872 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e nt reaches zero.
21873 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 */. pTable->nR
21874 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62 ef--;. if( pTab
21875 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20 le->nRef>0 ){.
21876 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
21877 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e assert( pTable->
21878 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a nRef==0 );.. /*
21879 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 Delete all indi
2187a 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
2187b 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20 ith this table.
2187c 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 */. for(pIndex
2187d 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 = pTable->pInde
2187e 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 x; pIndex; pInde
2187f 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e x=pNext){. pN
21880 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e ext = pIndex->pN
21881 65 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ext;. assert(
21882 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 pIndex->pSchema
21883 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d ==pTable->pSchem
21884 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 a );. sqlite3
21885 44 65 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64 DeleteIndex(pInd
21886 65 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 ex);. }..#ifnde
21887 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f f SQLITE_OMIT_FO
21888 52 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a 20 44 REIGN_KEY. /* D
21889 65 6c 65 74 65 20 61 6c 6c 20 66 6f 72 65 69 67 elete all foreig
2188a 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74 65 n keys associate
2188b 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c d with this tabl
2188c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 e. */. for(pFKe
2188d 79 3d 70 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b y=pTable->pFKey;
2188e 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e pFKey; pFKey=pN
2188f 65 78 74 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e extFKey){. pN
21890 65 78 74 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d extFKey = pFKey-
21891 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 >pNextFrom;.
21892 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
21893 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d 0a 23 65 , pFKey);. }.#e
21894 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 ndif.. /* Delet
21895 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 e the Table stru
21896 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 20 20 cture itself..
21897 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 73 65 74 */. sqliteReset
21898 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 ColumnNames(pTab
21899 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 le);. sqlite3Db
2189a 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d Free(db, pTable-
2189b 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 >zName);. sqlit
2189c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 e3DbFree(db, pTa
2189d 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 ble->zColAff);.
2189e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
2189f 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d lete(db, pTable-
218a0 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64 >pSelect);.#ifnd
218a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
218a2 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 33 45 78 HECK. sqlite3Ex
218a3 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 prDelete(db, pTa
218a4 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 23 65 ble->pCheck);.#e
218a5 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 56 74 ndif. sqlite3Vt
218a6 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65 29 3b abClear(pTable);
218a7 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
218a8 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 7d 0a (db, pTable);.}.
218a9 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
218aa 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72 e given table fr
218ab 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
218ac 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74 es and the delet
218ad 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73 e the.** table s
218ae 74 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6c tructure with al
218af 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e l its indices an
218b0 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a d foreign keys..
218b1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
218b2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e E void sqlite3Un
218b3 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 linkAndDeleteTab
218b4 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 le(sqlite3 *db,
218b5 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 int iDb, const c
218b6 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a har *zTabName){.
218b7 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 44 62 Table *p;. Db
218b8 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 *pDb;.. assert
218b9 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 ( db!=0 );. ass
218ba 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 ert( iDb>=0 && i
218bb 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 Db<db->nDb );.
218bc 61 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d 65 assert( zTabName
218bd 20 26 26 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 20 && zTabName[0]
218be 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e );. pDb = &db->
218bf 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20 aDb[iDb];. p =
218c0 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 sqlite3HashInser
218c1 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d t(&pDb->pSchema-
218c2 3e 74 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61 >tblHash, zTabNa
218c3 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 me,.
218c4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
218c5 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 61 62 te3Strlen30(zTab
218c6 4e 61 6d 65 29 2c 30 29 3b 0a 20 20 73 71 6c 69 Name),0);. sqli
218c7 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 te3DeleteTable(p
218c8 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c );. db->flags |
218c9 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 = SQLITE_InternC
218ca 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a hanges;.}../*.**
218cb 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 Given a token,
218cc 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20 return a string
218cd 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 that consists of
218ce 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61 the text of tha
218cf 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61 t.** token. Spa
218d0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
218d1 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a eturned string.*
218d2 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 * is obtained fr
218d3 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
218d4 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 ) and must be fr
218d5 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 eed by the calli
218d6 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a ng.** function..
218d7 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74 **.** Any quotat
218d8 69 6f 6e 20 6d 61 72 6b 73 20 28 65 78 3a 20 20 ion marks (ex:
218d9 22 6e 61 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20 "name", 'name',
218da 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65 [name], or `name
218db 60 29 20 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f `) that.** surro
218dc 75 6e 64 20 74 68 65 20 62 6f 64 79 20 6f 66 20 und the body of
218dd 74 68 65 20 74 6f 6b 65 6e 20 61 72 65 20 72 65 the token are re
218de 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b moved..**.** Tok
218df 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75 ens are often ju
218e0 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f st pointers into
218e1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
218e2 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a L text and so.**
218e3 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65 are not \000 te
218e4 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65 rminated and are
218e5 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e not persistent.
218e6 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 The returned s
218e7 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30 tring.** is \000
218e8 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 terminated and
218e9 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a is persistent..*
218ea 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
218eb 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 char *sqlite3Na
218ec 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 meFromToken(sqli
218ed 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a te3 *db, Token *
218ee 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a pName){. char *
218ef 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61 zName;. if( pNa
218f0 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 me ){. zName
218f1 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 = sqlite3DbStrND
218f2 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e up(db, (char*)pN
218f3 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
218f4 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65 );. if( pName
218f5 2d 3e 71 75 6f 74 65 64 20 29 20 73 71 6c 69 74 ->quoted ) sqlit
218f6 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29 e3Dequote(zName)
218f7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a ;. }else{. z
218f8 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Name = 0;. }.
218f9 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a return zName;.}.
218fa 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
218fb 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
218fc 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 ble stored in da
218fd 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44 tabase number iD
218fe 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 b for.** writing
218ff 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f . The table is o
21900 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73 pened using curs
21901 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f or 0..*/.SQLITE_
21902 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21903 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 ite3OpenMasterTa
21904 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e ble(Parse *p, in
21905 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a t iDb){. Vdbe *
21906 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
21907 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 be(p);. sqlite3
21908 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62 TableLock(p, iDb
21909 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 , MASTER_ROOT, 1
2190a 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
2190b 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 Db));. sqlite3V
2190c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
2190d 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41 OpenWrite, 0, MA
2190e 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b STER_ROOT, iDb);
2190f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
21910 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 angeP4(v, -1, (c
21911 68 61 72 20 2a 29 35 2c 20 50 34 5f 49 4e 54 33 har *)5, P4_INT3
21912 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c 75 6d 6e 2); /* 5 column
21913 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 table */. if(
21914 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a 20 20 p->nTab==0 ){.
21915 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 p->nTab = 1;.
21916 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 }.}../*.** Para
21917 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e meter zName poin
21918 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d ts to a nul-term
21919 69 6e 61 74 65 64 20 62 75 66 66 65 72 20 63 6f inated buffer co
2191a 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 61 6d ntaining the nam
2191b 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61 62 61 e.** of a databa
2191c 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d se ("main", "tem
2191d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f p" or the name o
2191e 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 f an attached db
2191f 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 ). This.** funct
21920 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
21921 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d index of the nam
21922 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 ed database in d
21923 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a 2a 20 b->aDb[], or.**
21924 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 -1 if the named
21925 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 db cannot be fou
21926 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nd..*/.SQLITE_PR
21927 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21928 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 3FindDbName(sqli
21929 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
2192a 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 har *zName){. i
2192b 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 20 20 nt i = -1;
2192c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e /* Database n
2192d 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 28 20 7a umber */. if( z
2192e 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 62 20 2a Name ){. Db *
2192f 70 44 62 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d pDb;. int n =
21930 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
21931 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72 (zName);. for
21932 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 2c 20 (i=(db->nDb-1),
21933 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 5d 3b pDb=&db->aDb[i];
21934 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 62 2d i>=0; i--, pDb-
21935 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 21 -){. if( (!
21936 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c 20 69 OMIT_TEMPDB || i
21937 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 71 6c 69 !=1 ) && n==sqli
21938 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 62 2d te3Strlen30(pDb-
21939 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 >zName) && .
2193a 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 0==sqlite3
2193b 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 StrICmp(pDb->zNa
2193c 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 me, zName) ){.
2193d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2193e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
2193f 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a return i;.}../*
21940 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70 .** The token *p
21941 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 Name contains th
21942 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 e name of a data
21943 62 61 73 65 20 28 65 69 74 68 65 72 20 22 6d 61 base (either "ma
21944 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22 in" or.** "temp"
21945 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 or the name of
21946 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 29 2e an attached db).
21947 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
21948 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 turns the.** ind
21949 65 78 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 ex of the named
2194a 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e database in db->
2194b 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20 aDb[], or -1 if
2194c 74 68 65 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a the named db .**
2194d 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e does not exist.
2194e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
2194f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
21950 6e 64 44 62 28 73 71 6c 69 74 65 33 20 2a 64 62 ndDb(sqlite3 *db
21951 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b , Token *pName){
21952 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
21953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21954 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
21955 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ase number */.
21956 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
21957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21958 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20 /* Name we
21959 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f are searching fo
2195a 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 r */. zName = s
2195b 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
2195c 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a ken(db, pName);.
2195d 20 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e i = sqlite3Fin
2195e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d dDbName(db, zNam
2195f 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e);. sqlite3DbF
21960 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a ree(db, zName);.
21961 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f return i;.}../
21962 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 * The table or v
21963 69 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e iew or trigger n
21964 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f ame is passed to
21965 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 this routine vi
21966 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d a tokens.** pNam
21967 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 e1 and pName2. I
21968 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 f the table name
21969 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 was fully quali
2196a 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c fied, for exampl
2196b 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 e:.**.** CREATE
2196c 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e TABLE xxx.yyy (.
2196d 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e ..);.** .** Then
2196e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 pName1 is set t
2196f 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d o "xxx" and pNam
21970 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 e2 "yyy". On the
21971 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a other hand if.*
21972 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 * the table name
21973 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 is not fully qu
21974 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a alified, i.e.:.*
21975 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c *.** CREATE TABL
21976 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a E yyy(...);.**.*
21977 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 * Then pName1 is
21978 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e set to "yyy" an
21979 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a d pName2 is ""..
2197a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
2197b 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55 ne sets the *ppU
2197c 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f nqual pointer to
2197d 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f point at the to
2197e 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a ken (pName1 or.*
2197f 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 * pName2) that s
21980 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c tores the unqual
21981 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 ified table name
21982 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 . The index of
21983 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
21984 22 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65 "xxx" is returne
21985 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
21986 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21987 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50 TwoPartName(. P
21988 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
21989 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e /* Parsing an
2198a 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
2198b 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 g context */. T
2198c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 oken *pName1,
2198d 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20 /* The "xxx"
2198e 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 in the name "xxx
2198f 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a .yyy" or "xxx" *
21990 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
21991 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 2, /* The "
21992 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 yyy" in the name
21993 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20 "xxx.yyy" */.
21994 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20 Token **pUnqual
21995 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
21996 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a unqualified obj
21997 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f ect name here */
21998 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 .){. int iDb;
21999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2199a 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f /* Database ho
2199b 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74 lding the object
2199c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
2199d 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
2199e 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 20 26 26 . if( pName2 &&
2199f 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a pName2->n>0 ){.
219a0 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 if( db->init
219a1 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20 .busy ) {.
219a2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
219a3 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74 pParse, "corrupt
219a4 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 database");.
219a5 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b pParse->nErr+
219a6 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 +;. return
219a7 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 -1;. }. *p
219a8 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b Unqual = pName2;
219a9 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 . iDb = sqlit
219aa 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 e3FindDb(db, pNa
219ab 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 me1);. if( iD
219ac 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c b<0 ){. sql
219ad 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
219ae 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 rse, "unknown da
219af 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d tabase %T", pNam
219b0 65 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 e1);. pPars
219b1 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 e->nErr++;.
219b2 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 return -1;.
219b3 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 }. }else{. a
219b4 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e ssert( db->init.
219b5 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e iDb==0 || db->in
219b6 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 it.busy );. i
219b7 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 Db = db->init.iD
219b8 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 b;. *pUnqual
219b9 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 = pName1;. }.
219ba 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f return iDb;.}../
219bb 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
219bc 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 e is used to che
219bd 63 6b 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 ck if the UTF-8
219be 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 string zName is
219bf 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 a legal.** unqua
219c0 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 lified name for
219c1 61 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a a new schema obj
219c2 65 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 ect (table, inde
219c3 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 x, view or.** tr
219c4 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 igger). All name
219c5 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 s are legal exce
219c6 70 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 pt those that be
219c7 67 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 gin with the str
219c8 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 ing.** "sqlite_"
219c9 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 (in upper, lowe
219ca 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 r or mixed case)
219cb 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f . This portion o
219cc 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a f the namespace.
219cd 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64 20 66 ** is reserved f
219ce 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e or internal use.
219cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
219d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 TE int sqlite3Ch
219d1 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 eckObjectName(Pa
219d2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
219d3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b st char *zName){
219d4 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
219d5 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 db->init.busy &&
219d6 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d pParse->nested=
219d7 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 =0 . &&
219d8 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c (pParse->db->fl
219d9 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 ags & SQLITE_Wri
219da 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 teSchema)==0.
219db 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c && 0==sql
219dc 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 ite3StrNICmp(zNa
219dd 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 me, "sqlite_", 7
219de 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
219df 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
219e0 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 "object name re
219e1 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 served for inter
219e2 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e nal use: %s", zN
219e3 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
219e4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
219e5 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
219e6 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
219e7 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 Begin constructi
219e8 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 ng a new table r
219e9 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e epresentation in
219ea 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 memory. This i
219eb 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f s.** the first o
219ec 66 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e f several action
219ed 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 routines that g
219ee 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 et called in res
219ef 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 ponse.** to a CR
219f0 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
219f1 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 ment. In partic
219f2 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 ular, this routi
219f3 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 ne is called.**
219f4 61 66 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b after seeing tok
219f5 65 6e 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 ens "CREATE" and
219f6 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 "TABLE" and the
219f7 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 table name. The
219f8 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 isTemp.** flag
219f9 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 is true if the t
219fa 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 able should be s
219fb 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 tored in the aux
219fc 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a iliary database.
219fd 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 ** file instead
219fe 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 of in the main d
219ff 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
21a00 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 his is normally
21a01 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e the case.** when
21a02 20 74 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 the "TEMP" or "
21a03 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f TEMPORARY" keywo
21a04 72 64 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 rd occurs in bet
21a05 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 ween.** CREATE a
21a06 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 nd TABLE..**.**
21a07 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 The new table re
21a08 63 6f 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 cord is initiali
21a09 7a 65 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 zed and put in p
21a0a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
21a0b 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 ..** As more of
21a0c 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
21a0d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 statement is pa
21a0e 72 73 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c rsed, additional
21a0f 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 action.** routi
21a10 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c nes will be call
21a11 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 ed to add more i
21a12 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 nformation to th
21a13 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 is record..** At
21a14 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
21a15 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 CREATE TABLE sta
21a16 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 tement, the sqli
21a17 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f te3EndTable() ro
21a18 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c utine.** is call
21a19 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 ed to complete t
21a1a 68 65 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 he construction
21a1b 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
21a1c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 record..*/.SQLI
21a1d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
21a1e 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c sqlite3StartTabl
21a1f 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
21a20 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 se, /* Parser
21a21 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b context */. Tok
21a22 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a en *pName1, /*
21a23 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 First part of t
21a24 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
21a25 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a able or view */.
21a26 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c Token *pName2,
21a27 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 /* Second par
21a28 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 t of the name of
21a29 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 the table or vi
21a2a 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 ew */. int isTe
21a2b 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 mp, /* True
21a2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 54 45 if this is a TE
21a2d 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e MP table */. in
21a2e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f t isView, /
21a2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
21a30 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e s a VIEW */. in
21a31 74 20 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f t isVirtual, /
21a32 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
21a33 73 20 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c s a VIRTUAL tabl
21a34 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 e */. int noErr
21a35 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
21a36 74 68 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 thing if table a
21a37 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f lready exists */
21a38 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 .){. Table *pTa
21a39 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 ble;. char *zNa
21a3a 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e me = 0; /* The n
21a3b 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 ame of the new t
21a3c 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 able */. sqlite
21a3d 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
21a3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 db;. Vdbe *v;.
21a3f 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
21a40 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 /* Database nu
21a41 6d 62 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 mber to create t
21a42 68 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 he table in */.
21a43 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 Token *pName;
21a44 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 /* Unqualified
21a45 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
21a46 6c 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a le to create */.
21a47 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 . /* The table
21a48 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 or view name to
21a49 63 72 65 61 74 65 20 69 73 20 70 61 73 73 65 64 create is passed
21a4a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
21a4b 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a via tokens. **
21a4c 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d pName1 and pNam
21a4d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 e2. If the table
21a4e 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 name was fully
21a4f 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 qualified, for e
21a50 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a xample:. **. *
21a51 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 * CREATE TABLE x
21a52 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 xx.yyy (...);.
21a53 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e ** . ** Then pN
21a54 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 ame1 is set to "
21a55 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 xxx" and pName2
21a56 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 "yyy". On the ot
21a57 68 65 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a her hand if. **
21a58 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
21a59 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 is not fully qua
21a5a 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 lified, i.e.:.
21a5b 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 **. ** CREATE T
21a5c 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 ABLE yyy(...);.
21a5d 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e **. ** Then pN
21a5e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 ame1 is set to "
21a5f 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 yyy" and pName2
21a60 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a is "".. **. **
21a61 20 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 The call below
21a62 73 65 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 sets the pName p
21a63 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 ointer to point
21a64 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e at the token (pN
21a65 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 ame1 or. ** pNa
21a66 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 me2) that stores
21a67 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 the unqualified
21a68 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 table name. The
21a69 20 76 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 variable iDb is
21a6a 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 . ** set to the
21a6b 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 index of the da
21a6c 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 tabase that the
21a6d 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 table or view is
21a6e 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 to be. ** crea
21a6f 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 ted in.. */. i
21a70 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 Db = sqlite3TwoP
21a71 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 artName(pParse,
21a72 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 pName1, pName2,
21a73 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 &pName);. if( i
21a74 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 Db<0 ) return;.
21a75 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 if( !OMIT_TEMPD
21a76 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 B && isTemp && i
21a77 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Db>1 ){. /* I
21a78 66 20 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d f creating a tem
21a79 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d p table, the nam
21a7a 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 e may not be qua
21a7b 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 lified */. sq
21a7c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
21a7d 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 arse, "temporary
21a7e 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 table name must
21a7f 20 62 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 be unqualified"
21a80 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
21a81 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 }. if( !OMIT_T
21a82 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 EMPDB && isTemp
21a83 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 ) iDb = 1;.. pP
21a84 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e arse->sNameToken
21a85 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 = *pName;. zNa
21a86 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 me = sqlite3Name
21a87 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e FromToken(db, pN
21a88 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d ame);. if( zNam
21a89 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
21a8a 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
21a8b 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 sqlite3CheckObje
21a8c 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a ctName(pParse, z
21a8d 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 Name) ){. got
21a8e 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 o begin_table_er
21a8f 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 ror;. }. if( d
21a90 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 b->init.iDb==1 )
21a91 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 isTemp = 1;.#if
21a92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21a93 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
21a94 20 61 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 assert( (isTemp
21a95 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b & 1)==isTemp );
21a96 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 . {. int cod
21a97 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 e;. char *zDb
21a98 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
21a99 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 zName;. if( s
21a9a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
21a9b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 pParse, SQLITE_I
21a9c 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 NSERT, SCHEMA_TA
21a9d 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 BLE(isTemp), 0,
21a9e 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
21a9f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
21aa0 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
21aa1 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 if( isView ){.
21aa2 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 if( !OMIT_TE
21aa3 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 MPDB && isTemp )
21aa4 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d {. code =
21aa5 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
21aa6 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 EMP_VIEW;.
21aa7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
21aa8 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 ode = SQLITE_CRE
21aa9 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 ATE_VIEW;.
21aaa 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
21aab 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d if( !OMIT_TEM
21aac 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b PDB && isTemp ){
21aad 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 . code =
21aae 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
21aaf 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 MP_TABLE;.
21ab0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
21ab1 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 ode = SQLITE_CRE
21ab2 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 ATE_TABLE;.
21ab3 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
21ab4 20 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 !isVirtual && s
21ab5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
21ab6 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e pParse, code, zN
21ab7 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a ame, 0, zDb) ){.
21ab8 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e goto begin
21ab9 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
21aba 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
21abb 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
21abc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d he new table nam
21abd 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 e does not colli
21abe 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 de with an exist
21abf 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f ing. ** index o
21ac0 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 r table name in
21ac1 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
21ac2 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 e. Issue an err
21ac3 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 or message if.
21ac4 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 ** it does. The
21ac5 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 exception is if
21ac6 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 the statement be
21ac7 69 6e 67 20 70 61 72 73 65 64 20 77 61 73 20 70 ing parsed was p
21ac8 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e assed. ** to an
21ac9 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 sqlite3_declare
21aca 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e _vtab() call. In
21acb 20 74 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 that case only
21acc 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 the column names
21acd 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 . ** and types
21ace 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f will be used, so
21acf 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
21ad0 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 d to test for na
21ad1 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c mespace. ** col
21ad2 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 lisions.. */.
21ad3 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f if( !IN_DECLARE_
21ad4 56 54 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 VTAB ){. if(
21ad5 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
21ad6 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 e3ReadSchema(pPa
21ad7 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f rse) ){. go
21ad8 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 to begin_table_e
21ad9 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
21ada 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 pTable = sqlite3
21adb 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e FindTable(db, zN
21adc 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 ame, db->aDb[iDb
21add 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 ].zName);. if
21ade 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 ( pTable ){.
21adf 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a if( !noErr ){.
21ae0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21ae1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
21ae2 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 "table %T alread
21ae3 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 y exists", pName
21ae4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21ae5 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
21ae6 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 e_error;. }.
21ae7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
21ae8 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d ndIndex(db, zNam
21ae9 65 2c 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 e, 0)!=0 && (iDb
21aea 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 ==0 || !db->init
21aeb 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 .busy) ){.
21aec 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21aed 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 pParse, "there i
21aee 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 s already an ind
21aef 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e ex named %s", zN
21af0 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f ame);. goto
21af1 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
21af2 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 or;. }. }..
21af3 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 pTable = sqlite
21af4 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
21af5 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 , sizeof(Table))
21af6 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d ;. if( pTable==
21af7 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 0 ){. db->mal
21af8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
21af9 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
21afa 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
21afb 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
21afc 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e ;. goto begin
21afd 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 _table_error;.
21afe 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d }. pTable->zNam
21aff 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 e = zName;. pTa
21b00 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b ble->iPKey = -1;
21b01 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 . pTable->pSche
21b02 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 ma = db->aDb[iDb
21b03 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 ].pSchema;. pTa
21b04 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 ble->nRef = 1;.
21b05 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 20 3d pTable->dbMem =
21b06 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 db->lookaside.b
21b07 45 6e 61 62 6c 65 64 20 3f 20 64 62 20 3a 20 30 Enabled ? db : 0
21b08 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
21b09 70 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 pNewTable ) sqli
21b0a 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 te3DeleteTable(p
21b0b 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 Parse->pNewTable
21b0c 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 );. pParse->pNe
21b0d 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b wTable = pTable;
21b0e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
21b0f 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 s the magic sqli
21b10 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c te_sequence tabl
21b11 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e e used by autoin
21b12 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 crement,. ** th
21b13 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e en record a poin
21b14 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c ter to this tabl
21b15 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 e in the main da
21b16 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 tabase structure
21b17 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e . ** so that IN
21b18 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 SERT can find th
21b19 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a e table easily..
21b1a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
21b1b 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
21b1c 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 REMENT. if( !pP
21b1d 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 arse->nested &&
21b1e 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 strcmp(zName, "s
21b1f 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 qlite_sequence")
21b20 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c ==0 ){. pTabl
21b21 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 e->pSchema->pSeq
21b22 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 Tab = pTable;.
21b23 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 }.#endif.. /* B
21b24 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 egin generating
21b25 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 the code that wi
21b26 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 ll insert the ta
21b27 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a ble record into.
21b28 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f ** the SQLITE_
21b29 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e MASTER table. N
21b2a 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 ote in particula
21b2b 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 r that we must g
21b2c 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 o ahead. ** and
21b2d 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 allocate the re
21b2e 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 cord number for
21b2f 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 the table entry
21b30 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 now. Before any
21b31 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 . ** PRIMARY KE
21b32 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 Y or UNIQUE keyw
21b33 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e ords are parsed.
21b34 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 Those keywords
21b35 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a will cause. **
21b36 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 indices to be c
21b37 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 reated and the t
21b38 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 able record must
21b39 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 come before the
21b3a 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 . ** indices.
21b3b 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f Hence, the reco
21b3c 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 rd number for th
21b3d 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 e table must be
21b3e 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e allocated. ** n
21b3f 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ow.. */. if( !
21b40 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 db->init.busy &&
21b41 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 (v = sqlite3Get
21b42 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 Vdbe(pParse))!=0
21b43 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a ){. int j1;.
21b44 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d int fileForm
21b45 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31 at;. int reg1
21b46 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 , reg2, reg3;.
21b47 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 sqlite3BeginWr
21b48 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 iteOperation(pPa
21b49 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 rse, 0, iDb);..#
21b4a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21b4b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
21b4c 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75 61 if( isVirtua
21b4d 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
21b4e 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 e3VdbeAddOp0(v,
21b4f 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 OP_VBegin);.
21b50 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a }.#endif.. /*
21b51 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 If the file for
21b52 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 mat and encoding
21b53 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
21b54 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 have not been s
21b55 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 et, . ** set
21b56 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f them now.. */
21b57 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72 . reg1 = pPar
21b58 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b se->regRowid = +
21b59 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
21b5a 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73 65 reg2 = pParse
21b5b 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 ->regRoot = ++pP
21b5c 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
21b5d 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d reg3 = ++pParse-
21b5e 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 >nMem;. sqlit
21b5f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21b60 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 OP_ReadCookie, i
21b61 44 62 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20 Db, reg3, 1);
21b62 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a /* file_format *
21b63 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
21b64 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
21b65 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c b);. j1 = sql
21b66 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21b67 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a , OP_If, reg3);.
21b68 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d fileFormat =
21b69 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 (db->flags & SQ
21b6a 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 LITE_LegacyFileF
21b6b 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 mt)!=0 ?.
21b6c 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20 53 1 : S
21b6d 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 QLITE_MAX_FILE_F
21b6e 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 ORMAT;. sqlit
21b6f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21b70 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 OP_Integer, file
21b71 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 Format, reg3);.
21b72 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21b73 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f dOp3(v, OP_SetCo
21b74 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65 okie, iDb, 1, re
21b75 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g3);. sqlite3
21b76 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21b77 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 _Integer, ENC(db
21b78 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 ), reg3);. sq
21b79 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21b7a 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c v, OP_SetCookie,
21b7b 20 69 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a iDb, 4, reg3);.
21b7c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
21b7d 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a umpHere(v, j1);.
21b7e 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 . /* This jus
21b7f 74 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 t creates a plac
21b80 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 e-holder record
21b81 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 in the sqlite_ma
21b82 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 ster table..
21b83 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 ** The record cr
21b84 65 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 eated does not c
21b85 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 ontain anything
21b86 79 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 yet. It will be
21b87 20 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a replaced. **
21b88 20 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 by the real ent
21b89 72 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 ry in code gener
21b8a 61 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 ated at sqlite3E
21b8b 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a ndTable().. *
21b8c 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 *. ** The row
21b8d 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 id for the new e
21b8e 6e 74 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20 ntry is left in
21b8f 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d register pParse-
21b90 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 20 20 2a >regRowid.. *
21b91 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
21b92 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
21b93 77 20 74 61 62 6c 65 20 69 73 20 6c 65 66 74 20 w table is left
21b94 69 6e 20 72 65 67 20 70 50 61 72 73 65 2d 3e 72 in reg pParse->r
21b95 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 20 54 egRoot.. ** T
21b96 68 65 20 72 6f 77 69 64 20 61 6e 64 20 72 6f 6f he rowid and roo
21b97 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 76 61 t page number va
21b98 6c 75 65 73 20 61 72 65 20 6e 65 65 64 65 64 20 lues are needed
21b99 62 79 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 by the code that
21b9a 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45 . ** sqlite3E
21b9b 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 67 65 6e ndTable will gen
21b9c 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 erate.. */.#i
21b9d 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
21b9e 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 E_OMIT_VIEW) ||
21b9f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
21ba0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
21ba1 45 29 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 E). if( isVie
21ba2 77 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29 w || isVirtual )
21ba3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
21ba4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21ba5 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32 Integer, 0, reg2
21ba6 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e );. }else.#en
21ba7 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 dif. {.
21ba8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21ba9 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61 2(v, OP_CreateTa
21baa 62 6c 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b ble, iDb, reg2);
21bab 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
21bac 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c e3OpenMasterTabl
21bad 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a e(pParse, iDb);.
21bae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21baf 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp2(v, OP_NewR
21bb0 6f 77 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a owid, 0, reg1);.
21bb1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21bb2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
21bb3 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 , 0, reg3);.
21bb4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21bb5 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 3(v, OP_Insert,
21bb6 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 0, reg3, reg1);.
21bb7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
21bb8 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 hangeP5(v, OPFLA
21bb9 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 G_APPEND);. s
21bba 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 qlite3VdbeAddOp0
21bbb 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 (v, OP_Close);.
21bbc 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 }.. /* Normal
21bbd 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 (non-error) retu
21bbe 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b rn. */. return;
21bbf 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 .. /* If an err
21bc0 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 or occurs, we ju
21bc1 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e mp here */.begin
21bc2 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 _table_error:.
21bc3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
21bc4 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 , zName);. retu
21bc5 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rn;.}../*.** Thi
21bc6 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 s macro is used
21bc7 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 to compare two s
21bc8 74 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65 trings in a case
21bc9 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e -insensitive man
21bca 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c ner..** It is sl
21bcb 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 ightly faster th
21bcc 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 an calling sqlit
21bcd 65 33 53 74 72 49 43 6d 70 28 29 20 64 69 72 65 e3StrICmp() dire
21bce 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f ctly, but.** pro
21bcf 64 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64 duces larger cod
21bd0 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 e..**.** WARNING
21bd1 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 : This macro is
21bd2 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 not compatible w
21bd3 69 74 68 20 74 68 65 20 73 74 72 63 6d 70 28 29 ith the strcmp()
21bd4 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 family. It.** r
21bd5 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20 74 eturns true if t
21bd6 68 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61 he two strings a
21bd7 72 65 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 re equal, otherw
21bd8 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 ise false..*/.#d
21bd9 65 66 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c efine STRICMP(x,
21bda 20 79 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 y) (\.sqlite3Up
21bdb 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 perToLower[*(uns
21bdc 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29 igned char *)(x)
21bdd 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 ]== \.sqlite3U
21bde 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e pperToLower[*(un
21bdf 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79 signed char *)(y
21be0 29 5d 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 )] \.&& sqli
21be1 74 65 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31 te3StrICmp((x)+1
21be2 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a ,(y)+1)==0 )../*
21be3 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f .** Add a new co
21be4 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c lumn to the tabl
21be5 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e e currently bein
21be6 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a g constructed..*
21be7 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 *.** The parser
21be8 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 calls this routi
21be9 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ne once for each
21bea 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 column declarat
21beb 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 ion.** in a CREA
21bec 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 TE TABLE stateme
21bed 6e 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72 nt. sqlite3Star
21bee 74 54 61 62 6c 65 28 29 20 67 65 74 73 20 63 61 tTable() gets ca
21bef 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f lled.** first to
21bf0 20 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e get things goin
21bf1 67 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f g. Then this ro
21bf2 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
21bf3 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 for each.** colu
21bf4 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 mn..*/.SQLITE_PR
21bf5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21bf6 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 e3AddColumn(Pars
21bf7 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e e *pParse, Token
21bf8 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c *pName){. Tabl
21bf9 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 e *p;. int i;.
21bfa 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 char *z;. Colu
21bfb 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 mn *pCol;. sqli
21bfc 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
21bfd 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d ->db;. if( (p =
21bfe 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
21bff 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b le)==0 ) return;
21c00 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
21c01 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e COLUMN. if( p->
21c02 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 nCol+1>db->aLimi
21c03 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 t[SQLITE_LIMIT_C
21c04 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 OLUMN] ){. sq
21c05 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
21c06 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
21c07 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 columns on %s",
21c08 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 p->zName);. r
21c09 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 eturn;. }.#endi
21c0a 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e f. z = sqlite3N
21c0b 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
21c0c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a pName);. if( z
21c0d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
21c0e 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 for(i=0; i<p->nC
21c0f 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 ol; i++){. if
21c10 28 20 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e ( STRICMP(z, p->
21c11 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 aCol[i].zName) )
21c12 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
21c13 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
21c14 22 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d "duplicate colum
21c15 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b n name: %s", z);
21c16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
21c17 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 Free(db, z);.
21c18 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
21c19 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e . }. if( (p->n
21c1a 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b Col & 0x7)==0 ){
21c1b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 . Column *aNe
21c1c 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 w;. aNew = sq
21c1d 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 lite3DbRealloc(d
21c1e 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 b,p->aCol,(p->nC
21c1f 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e ol+8)*sizeof(p->
21c20 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 aCol[0]));. i
21c21 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( aNew==0 ){.
21c22 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
21c23 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 e(db, z);.
21c24 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
21c25 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 p->aCol = aNew
21c26 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 ;. }. pCol = &
21c27 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d p->aCol[p->nCol]
21c28 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c ;. memset(pCol,
21c29 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 0, sizeof(p->aC
21c2a 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d ol[0]));. pCol-
21c2b 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 >zName = z;. .
21c2c 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e /* If there is n
21c2d 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64 o type specified
21c2e 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 , columns have t
21c2f 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e he default affin
21c30 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e ity. ** 'NONE'.
21c31 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 If there is a t
21c32 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74 ype specified, t
21c33 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f hen sqlite3AddCo
21c34 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a lumnType() will.
21c35 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e ** be called n
21c36 65 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d ext to set pCol-
21c37 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63 >affinity correc
21c38 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c tly.. */. pCol
21c39 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c ->affinity = SQL
21c3a 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
21c3b 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a p->nCol++;.}../*
21c3c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21c3d 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
21c3e 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 e parser while i
21c3f 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a n the middle of.
21c40 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 ** parsing a CRE
21c41 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
21c42 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c ent. A "NOT NUL
21c43 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 L" constraint ha
21c44 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f s.** been seen o
21c45 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 n a column. Thi
21c46 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 s routine sets t
21c47 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 he notNull flag
21c48 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e on.** the column
21c49 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 currently under
21c4a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a construction..*
21c4b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21c4c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
21c4d 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 NotNull(Parse *p
21c4e 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 Parse, int onErr
21c4f 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b or){. Table *p;
21c50 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
21c51 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 (p = pParse->pNe
21c52 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 wTable)==0 ) ret
21c53 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 urn;. i = p->nC
21c54 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30 ol-1;. if( i>=0
21c55 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f ) p->aCol[i].no
21c56 74 4e 75 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 tNull = (u8)onEr
21c57 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 ror;.}../*.** Sc
21c58 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 an the column ty
21c59 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c pe name zType (l
21c5a 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e 64 ength nType) and
21c5b 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 return the.** a
21c5c 73 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e 69 ssociated affini
21c5d 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 ty type..**.** T
21c5e 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
21c5f 20 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 a case-independ
21c60 65 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a 54 ent search of zT
21c61 79 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 ype for the .**
21c62 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 substrings in th
21c63 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c e following tabl
21c64 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 e. If one of the
21c65 20 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a 2a substrings is.*
21c66 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 * found, the cor
21c67 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e responding affin
21c68 69 74 79 20 69 73 20 72 65 74 75 72 6e 65 64 2e ity is returned.
21c69 20 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 If zType contai
21c6a 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 ns.** more than
21c6b 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 one of the subst
21c6c 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20 74 rings, entries t
21c6d 6f 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f 66 oward the top of
21c6e 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 .** the table t
21c6f 61 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46 6f ake priority. Fo
21c70 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 r example, if zT
21c71 79 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 ype is 'BLOBINT'
21c72 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 , .** SQLITE_AFF
21c73 5f 49 4e 54 45 47 45 52 20 69 73 20 72 65 74 75 _INTEGER is retu
21c74 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 rned..**.** Subs
21c75 74 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66 69 tring | Affi
21c76 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d nity.** --------
21c77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
21c78 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 --------.** 'INT
21c79 27 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 ' | SQLI
21c7a 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a TE_AFF_INTEGER.*
21c7b 2a 20 27 43 48 41 52 27 20 20 20 20 20 20 20 20 * 'CHAR'
21c7c 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 | SQLITE_AFF_TEX
21c7d 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 T.** 'CLOB'
21c7e 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
21c7f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 TEXT.** 'TEXT'
21c80 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 | SQLITE_A
21c81 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 FF_TEXT.** 'BLOB
21c82 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
21c83 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 E_AFF_NONE.** 'R
21c84 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53 51 EAL' | SQ
21c85 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a LITE_AFF_REAL.**
21c86 20 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 'FLOA' |
21c87 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
21c88 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20 20 .** 'DOUB'
21c89 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 | SQLITE_AFF_R
21c8a 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e EAL.**.** If non
21c8b 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 e of the substri
21c8c 6e 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76 65 ngs in the above
21c8d 20 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 table are found
21c8e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f ,.** SQLITE_AFF_
21c8f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 74 75 72 NUMERIC is retur
21c90 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
21c91 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
21c92 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 te3AffinityType(
21c93 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 79 const Token *pTy
21c94 70 65 29 7b 0a 20 20 75 33 32 20 68 20 3d 20 30 pe){. u32 h = 0
21c95 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 53 ;. char aff = S
21c96 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
21c97 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 C;. const unsig
21c98 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 20 3d 20 ned char *zIn =
21c99 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63 6f 6e 73 pType->z;. cons
21c9a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
21c9b 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70 65 2d 3e *zEnd = &pType->
21c9c 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a 0a 20 20 z[pType->n];..
21c9d 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 45 6e 64 while( zIn!=zEnd
21c9e 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c ){. h = (h<<
21c9f 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65 8) + sqlite3Uppe
21ca0 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e 5d 3b 0a rToLower[*zIn];.
21ca1 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 zIn++;. i
21ca2 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 f( h==(('c'<<24)
21ca3 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c +('h'<<16)+('a'<
21ca4 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20 <8)+'r') ){
21ca5 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20 /* CHAR
21ca6 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 */. aff = S
21ca7 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 20 QLITE_AFF_TEXT;
21ca8 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 . }else if( h
21ca9 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c ==(('c'<<24)+('l
21caa 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b '<<16)+('o'<<8)+
21cab 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 'b') ){ /*
21cac 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61 CLOB */. a
21cad 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
21cae 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 TEXT;. }else
21caf 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34 if( h==(('t'<<24
21cb0 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 27 )+('e'<<16)+('x'
21cb1 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 <<8)+'t') ){
21cb2 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20 /* TEXT */.
21cb3 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
21cb4 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d _AFF_TEXT;. }
21cb5 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 62 else if( h==(('b
21cb6 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 '<<24)+('l'<<16)
21cb7 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20 +('o'<<8)+'b')
21cb8 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20 /* BLOB
21cb9 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28 61 */. && (a
21cba 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
21cbb 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53 UMERIC || aff==S
21cbc 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 20 QLITE_AFF_REAL)
21cbd 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 ){. aff = S
21cbe 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
21cbf 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21cc0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
21cc1 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 NT. }else if(
21cc2 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b 28 h==(('r'<<24)+(
21cc3 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 'e'<<16)+('a'<<8
21cc4 29 2b 27 6c 27 29 20 20 20 20 20 20 20 20 20 20 )+'l')
21cc5 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20 20 /* REAL */.
21cc6 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 && aff==SQLIT
21cc7 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b E_AFF_NUMERIC ){
21cc8 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c . aff = SQL
21cc9 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 ITE_AFF_REAL;.
21cca 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 }else if( h==(
21ccb 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c ('f'<<24)+('l'<<
21ccc 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 27 16)+('o'<<8)+'a'
21ccd 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 4c ) /* FL
21cce 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 OA */. &&
21ccf 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 aff==SQLITE_AFF
21cd0 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 _NUMERIC ){.
21cd1 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
21cd2 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c FF_REAL;. }el
21cd3 73 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27 3c se if( h==(('d'<
21cd4 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 <24)+('o'<<16)+(
21cd5 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 'u'<<8)+'b')
21cd6 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f /* DOUB */
21cd7 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d . && aff=
21cd8 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 =SQLITE_AFF_NUME
21cd9 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 RIC ){. aff
21cda 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 = SQLITE_AFF_RE
21cdb 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d AL;.#endif. }
21cdc 65 6c 73 65 20 69 66 28 20 28 68 26 30 78 30 30 else if( (h&0x00
21cdd 46 46 46 46 46 46 29 3d 3d 28 28 27 69 27 3c 3c FFFFFF)==(('i'<<
21cde 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 27 16)+('n'<<8)+'t'
21cdf 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20 2a ) ){ /* INT *
21ce0 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 /. aff = SQ
21ce1 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
21ce2 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
21ce3 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 }. }.. retu
21ce4 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn aff;.}../*.**
21ce5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
21ce6 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 called by the p
21ce7 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 arser while in t
21ce8 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 he middle of.**
21ce9 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 parsing a CREATE
21cea 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
21ceb 2e 20 20 54 68 65 20 70 46 69 72 73 74 20 74 6f . The pFirst to
21cec 6b 65 6e 20 69 73 20 74 68 65 20 66 69 72 73 74 ken is the first
21ced 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 .** token in the
21cee 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b sequence of tok
21cef 65 6e 73 20 74 68 61 74 20 64 65 73 63 72 69 62 ens that describ
21cf0 65 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 e the type of th
21cf1 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72 e.** column curr
21cf2 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
21cf3 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73 truction. pLas
21cf4 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74 6f t is the last to
21cf5 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 ken.** in the se
21cf6 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68 69 quence. Use thi
21cf7 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f s information to
21cf8 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74 72 construct a str
21cf9 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74 ing.** that cont
21cfa 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e 61 6d ains the typenam
21cfb 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 e of the column
21cfc 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74 20 73 and store that s
21cfd 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 70 tring.** in zTyp
21cfe 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 e..*/ .SQLITE_PR
21cff 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21d00 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
21d01 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
21d02 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 oken *pType){.
21d03 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 Table *p;. int
21d04 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f i;. Column *pCo
21d05 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 l;. sqlite3 *db
21d06 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 ;.. if( (p = pP
21d07 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 arse->pNewTable)
21d08 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
21d09 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 i = p->nCol-1;.
21d0a 20 69 66 28 20 69 3c 30 20 29 20 72 65 74 75 72 if( i<0 ) retur
21d0b 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e n;. pCol = &p->
21d0c 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62 20 3d 20 aCol[i];. db =
21d0d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 73 71 pParse->db;. sq
21d0e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
21d0f 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20 pCol->zType);.
21d10 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 pCol->zType = sq
21d11 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
21d12 65 6e 28 64 62 2c 20 70 54 79 70 65 29 3b 0a 20 en(db, pType);.
21d13 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 pCol->affinity
21d14 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 = sqlite3Affinit
21d15 79 54 79 70 65 28 70 54 79 70 65 29 3b 0a 7d 0a yType(pType);.}.
21d16 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 ./*.** The expre
21d17 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66 ssion is the def
21d18 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
21d19 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
21d1a 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a added column.**
21d1b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 75 of the table cu
21d1c 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f rrently under co
21d1d 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
21d1e 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 * Default value
21d1f 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 expressions must
21d20 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 be constant. R
21d21 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f aise an exceptio
21d22 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 n if this.** is
21d23 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a not the case..**
21d24 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21d25 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
21d26 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 e parser while i
21d27 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a n the middle of.
21d28 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 ** parsing a CRE
21d29 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
21d2a 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
21d2b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21d2c 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c te3AddDefaultVal
21d2d 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ue(Parse *pParse
21d2e 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a , Expr *pExpr){.
21d2f 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f Table *p;. Co
21d30 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 lumn *pCol;. sq
21d31 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
21d32 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 se->db;. if( (p
21d33 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 = pParse->pNewT
21d34 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 able)!=0 ){.
21d35 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f 6c pCol = &(p->aCol
21d36 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 20 [p->nCol-1]);.
21d37 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 if( !sqlite3Ex
21d38 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 prIsConstantOrFu
21d39 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20 29 7b nction(pExpr) ){
21d3a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
21d3b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
21d3c 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 default value of
21d3d 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 20 column [%s] is
21d3e 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a 20 not constant",.
21d3f 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a pCol->z
21d40 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Name);. }else
21d41 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 {. /* A cop
21d42 79 20 6f 66 20 70 45 78 70 72 20 69 73 20 75 73 y of pExpr is us
21d43 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 ed instead of th
21d44 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61 73 20 70 e original, as p
21d45 45 78 70 72 20 63 6f 6e 74 61 69 6e 73 0a 20 20 Expr contains.
21d46 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 ** tokens th
21d47 61 74 20 70 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 at point to vola
21d48 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20 54 68 65 tile memory. The
21d49 20 27 73 70 61 6e 27 20 6f 66 20 74 68 65 20 65 'span' of the e
21d4a 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 xpression.
21d4b 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 62 ** is required b
21d4c 79 20 70 72 61 67 6d 61 20 74 61 62 6c 65 5f 69 y pragma table_i
21d4d 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 nfo.. */.
21d4e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
21d4f 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e elete(db, pCol->
21d50 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 pDflt);. pC
21d51 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69 ol->pDflt = sqli
21d52 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
21d53 45 78 70 72 2c 20 45 58 50 52 44 55 50 5f 52 45 Expr, EXPRDUP_RE
21d54 44 55 43 45 7c 45 58 50 52 44 55 50 5f 53 50 41 DUCE|EXPRDUP_SPA
21d55 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 N);. }. }.
21d56 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
21d57 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a e(db, pExpr);.}.
21d58 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 ./*.** Designate
21d59 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
21d5a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 for the table.
21d5b 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 pList is a list
21d5c 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 of names .** of
21d5d 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f columns that fo
21d5e 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b rm the primary k
21d5f 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 ey. If pList is
21d60 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a NULL, then the.
21d61 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 ** most recently
21d62 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 added column of
21d63 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 the table is th
21d64 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a e primary key..*
21d65 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e *.** A table can
21d66 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e have at most on
21d67 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 e primary key.
21d68 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 If the table alr
21d69 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 eady has.** a pr
21d6a 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 imary key (and t
21d6b 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e his is the secon
21d6c 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 d primary key) t
21d6d 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a hen create an.**
21d6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 error..**.** If
21d6f 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
21d70 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 is on a single
21d71 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 column whose dat
21d72 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 atype is INTEGER
21d73 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c ,.** then we wil
21d74 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 l try to use tha
21d75 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 t column as the
21d76 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 rowid. Set the
21d77 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 Table.iPKey.** f
21d78 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c ield of the tabl
21d79 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 e under construc
21d7a 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 tion to be the i
21d7b 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 ndex of the.** I
21d7c 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
21d7d 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c EY column. Tabl
21d7e 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 e.iPKey is set t
21d7f 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 o -1 if there is
21d80 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 .** no INTEGER P
21d81 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a RIMARY KEY..**.*
21d82 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 * If the key is
21d83 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 not an INTEGER P
21d84 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
21d85 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 create a unique
21d86 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 .** index for th
21d87 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 e key. No index
21d88 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 is created for
21d89 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
21d8a 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f KEYs..*/.SQLITE_
21d8b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21d8c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 ite3AddPrimaryKe
21d8d 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 y(. Parse *pPar
21d8e 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e se, /* Parsin
21d8f 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
21d90 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
21d91 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c /* List of fiel
21d92 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e d names to be in
21d93 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f dexed */. int o
21d94 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 nError, /*
21d95 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 What to do with
21d96 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e a uniqueness con
21d97 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 flict */. int a
21d98 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 utoInc, /*
21d99 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f True if the AUTO
21d9a 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 INCREMENT keywor
21d9b 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a d is present */.
21d9c 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 int sortOrder
21d9d 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f /* SQLITE_SO
21d9e 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 _ASC or SQLITE_S
21d9f 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 O_DESC */.){. T
21da0 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 able *pTab = pPa
21da1 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
21da2 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 char *zType =
21da3 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 0;. int iCol =
21da4 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 -1, i;. if( pTa
21da5 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 b==0 || IN_DECLA
21da6 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 RE_VTAB ) goto p
21da7 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b rimary_key_exit;
21da8 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 . if( pTab->tab
21da9 46 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 Flags & TF_HasPr
21daa 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 imaryKey ){.
21dab 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21dac 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 pParse, . "
21dad 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 table \"%s\" has
21dae 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 more than one p
21daf 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 rimary key", pTa
21db0 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 b->zName);. g
21db1 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f oto primary_key_
21db2 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 exit;. }. pTab
21db3 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 ->tabFlags |= TF
21db4 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a _HasPrimaryKey;.
21db5 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
21db6 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 {. iCol = pTa
21db7 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 b->nCol - 1;.
21db8 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c pTab->aCol[iCol
21db9 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b ].isPrimKey = 1;
21dba 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f . }else{. fo
21dbb 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
21dbc 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
21dbd 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 for(iCol=0; i
21dbe 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 Col<pTab->nCol;
21dbf 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 iCol++){.
21dc0 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
21dc1 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e Cmp(pList->a[i].
21dc2 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f zName, pTab->aCo
21dc3 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d l[iCol].zName)==
21dc4 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 0 ){. b
21dc5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
21dc6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
21dc7 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f ( iCol<pTab->nCo
21dc8 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 l ){. pTa
21dc9 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 b->aCol[iCol].is
21dca 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 PrimKey = 1;.
21dcb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
21dcc 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e f( pList->nExpr>
21dcd 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 1 ) iCol = -1;.
21dce 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 }. if( iCol>=0
21dcf 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e && iCol<pTab->n
21dd0 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 Col ){. zType
21dd1 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 = pTab->aCol[iC
21dd2 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 ol].zType;. }.
21dd3 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 if( zType && sq
21dd4 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 lite3StrICmp(zTy
21dd5 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d pe, "INTEGER")==
21dd6 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 0. && sor
21dd7 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 tOrder==SQLITE_S
21dd8 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 O_ASC ){. pTa
21dd9 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b b->iPKey = iCol;
21dda 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f . pTab->keyCo
21ddb 6e 66 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 nf = (u8)onError
21ddc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 75 ;. assert( au
21ddd 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f toInc==0 || auto
21dde 49 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 Inc==1 );. pT
21ddf 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 ab->tabFlags |=
21de0 61 75 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 autoInc*TF_Autoi
21de1 6e 63 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 ncrement;. }els
21de2 65 20 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b e if( autoInc ){
21de3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
21de4 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 OMIT_AUTOINCREME
21de5 4e 54 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 NT. sqlite3Er
21de6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
21de7 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 AUTOINCREMENT is
21de8 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e only allowed on
21de9 20 61 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e an ". "IN
21dea 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
21deb 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 Y");.#endif. }e
21dec 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
21ded 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 CreateIndex(pPar
21dee 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 se, 0, 0, 0, pLi
21def 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 st, onError, 0,
21df0 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 0, sortOrder, 0)
21df1 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 30 3b ;. pList = 0;
21df2 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 . }..primary_ke
21df3 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 y_exit:. sqlite
21df4 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 3ExprListDelete(
21df5 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 pParse->db, pLis
21df6 74 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a t);. return;.}.
21df7 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
21df8 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e CHECK constrain
21df9 74 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 t to the table c
21dfa 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 urrently under c
21dfb 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a onstruction..*/.
21dfc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21dfd 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 oid sqlite3AddCh
21dfe 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 eckConstraint(.
21dff 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
21e00 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
21e01 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 ntext */. Expr
21e02 2a 70 43 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 *pCheckExpr /*
21e03 54 68 65 20 63 68 65 63 6b 20 65 78 70 72 65 73 The check expres
21e04 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c sion */.){. sql
21e05 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
21e06 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 e->db;.#ifndef S
21e07 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b QLITE_OMIT_CHECK
21e08 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d . Table *pTab =
21e09 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
21e0a 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62 20 26 le;. if( pTab &
21e0b 26 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 & !IN_DECLARE_VT
21e0c 41 42 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 AB ){. /* The
21e0d 20 43 48 45 43 4b 20 65 78 70 72 65 73 73 69 6f CHECK expressio
21e0e 6e 20 6d 75 73 74 20 62 65 20 64 75 70 6c 69 63 n must be duplic
21e0f 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 6f 6b ated so that tok
21e10 65 6e 73 20 72 65 66 65 72 0a 20 20 20 20 2a 2a ens refer. **
21e11 20 74 6f 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61 to malloced spa
21e12 63 65 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 28 ce and not the (
21e13 65 70 68 65 6d 65 72 61 6c 29 20 74 65 78 74 20 ephemeral) text
21e14 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 of the CREATE TA
21e15 42 4c 45 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 BLE. ** state
21e16 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 70 54 61 62 ment */. pTab
21e17 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 ->pCheck = sqlit
21e18 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54 e3ExprAnd(db, pT
21e19 61 62 2d 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20 ab->pCheck, .
21e1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
21e1c 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
21e1d 2c 20 70 43 68 65 63 6b 45 78 70 72 2c 20 30 29 , pCheckExpr, 0)
21e1e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
21e1f 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
21e20 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72 e(db, pCheckExpr
21e21 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
21e22 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 the collation fu
21e23 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f nction of the mo
21e24 73 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73 st recently pars
21e25 65 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a ed table column.
21e26 2a 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 ** to the CollSe
21e27 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 q given..*/.SQLI
21e28 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
21e29 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 sqlite3AddCollat
21e2a 65 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 eType(Parse *pPa
21e2b 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b rse, Token *pTok
21e2c 65 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b en){. Table *p;
21e2d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 . int i;. char
21e2e 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 *zColl;
21e2f 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 /* Dequote
21e30 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 d name of collat
21e31 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ion sequence */.
21e32 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a sqlite3 *db;..
21e33 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 if( (p = pPars
21e34 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 e->pNewTable)==0
21e35 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d ) return;. i =
21e36 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 p->nCol-1;. db
21e37 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
21e38 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 zColl = sqlite3
21e39 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
21e3a 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 , pToken);. if(
21e3b 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e !zColl ) return
21e3c 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ;.. if( sqlite3
21e3d 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 LocateCollSeq(pP
21e3e 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 arse, zColl, -1)
21e3f 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 ){. Index *p
21e40 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c Idx;. p->aCol
21e41 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c [i].zColl = zCol
21e42 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 l;. . /* If
21e43 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65 the column is de
21e44 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d 65 clared as "<name
21e45 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43 4f > PRIMARY KEY CO
21e46 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20 LLATE <type>",.
21e47 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e ** then an in
21e48 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65 65 dex may have bee
21e49 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68 69 n created on thi
21e4a 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20 s column before
21e4b 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 the. ** colla
21e4c 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61 64 tion type was ad
21e4d 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68 69 ded. Correct thi
21e4e 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20 63 s if it is the c
21e4f 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
21e50 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 for(pIdx=p->pInd
21e51 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 ex; pIdx; pIdx=p
21e52 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Idx->pNext){.
21e53 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d assert( pIdx-
21e54 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 >nColumn==1 );.
21e55 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 if( pIdx->a
21e56 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b iColumn[0]==i ){
21e57 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61 . pIdx->a
21e58 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43 zColl[0] = p->aC
21e59 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 ol[i].zColl;.
21e5a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c }. }. }el
21e5b 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 se{. sqlite3D
21e5c 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 bFree(db, zColl)
21e5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
21e5e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
21e5f 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 urns the collati
21e60 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 on sequence for
21e61 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 database native
21e62 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 text.** encoding
21e63 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 identified by t
21e64 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c he string zName,
21e65 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a length nName..*
21e66 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 *.** If the requ
21e67 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 ested collation
21e68 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 sequence is not
21e69 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f available, or no
21e6a 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69 t available.** i
21e6b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e n the database n
21e6c 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 ative encoding,
21e6d 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 the collation fa
21e6e 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 ctory is invoked
21e6f 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 to.** request i
21e70 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 t. If the collat
21e71 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73 ion factory does
21e72 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68 not supply such
21e73 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20 a sequence,.**
21e74 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65 and the sequence
21e75 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e is available in
21e76 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e another text en
21e77 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61 coding, then tha
21e78 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 t is.** returned
21e79 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 instead..**.**
21e7a 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f If no versions o
21e7b 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
21e7c 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 65 collations seque
21e7d 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62 6c nce are availabl
21e7e 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72 e, or.** another
21e7f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e error occurs, N
21e80 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
21e81 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 and an error mes
21e82 73 61 67 65 20 77 72 69 74 74 65 6e 20